From: Peep P. <so...@us...> - 2004-04-03 20:31:29
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5316 Modified Files: compile.c debug.c interpret.c interpret.h lang.y lpc.h Log Message: Initial support for arrays. Index: interpret.h =================================================================== RCS file: /cvsroot/agd/server/src/interpret.h,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- interpret.h 1 Apr 2004 19:42:49 -0000 1.12 +++ interpret.h 3 Apr 2004 20:19:08 -0000 1.13 @@ -11,6 +11,7 @@ #define F_PUSH_STRING 4 #define F_PUSH_NULL_OBJECT 5 #define F_PUSH_VOID 6 /* For 'return;' */ +#define F_PUSH_ARRAY 7 #define F_POP 10 /* Pops a variable off the stack. */ #define F_PUSH_LVAR 11 /* Pushes local variable <n> on stack. */ Index: lpc.h =================================================================== RCS file: /cvsroot/agd/server/src/lpc.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- lpc.h 28 Mar 2004 17:57:10 -0000 1.7 +++ lpc.h 3 Apr 2004 20:19:08 -0000 1.8 @@ -15,6 +15,7 @@ #define T_LVALUE 0x10 /* Used by interpret.c. I'd rather not have it here, but as it also needs an union member, it would be too cluttered otherwise. */ +#define T_SPECIAL 0x20 /* For initializing variables in compile.c and lang.y */ #define T_ARRAY 0x80 /* Indicates an array datatype. */ #define ST_MALLOC 1 Index: interpret.c =================================================================== RCS file: /cvsroot/agd/server/src/interpret.c,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- interpret.c 1 Apr 2004 19:17:14 -0000 1.17 +++ interpret.c 3 Apr 2004 20:19:08 -0000 1.18 @@ -149,6 +149,15 @@ CHECK_SP() } +void push_array(array_t *a, int type) +{ + sp->type = T_ARRAY | type; + sp->u.a = a; + /*ref_array(sp->u.a);*/ + sp++; + CHECK_SP() +} + void do_assign(variable_t *lval, variable_t *rval) { *lval = *rval; @@ -284,6 +293,18 @@ case F_PUSH_NULL_OBJECT: push_object(NULL); break; + case F_PUSH_ARRAY: + { + array_t *a = type_xmalloc(array_t); + init_array(a); + arg[0] = *++cp; + arg[1] = *++cp; + for(tmpi=0;tmpi<arg[1];tmpi++) { + array_push(a, --sp); + } + push_array(a, arg[0]); + } + break; case F_POP: pop_stack(); break; Index: lang.y =================================================================== RCS file: /cvsroot/agd/server/src/lang.y,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- lang.y 1 Apr 2004 19:10:53 -0000 1.15 +++ lang.y 3 Apr 2004 20:19:08 -0000 1.16 @@ -81,7 +81,7 @@ %type <var> var_def %type <arr> call_args call_arg_list -%type <expr> expr optional_expr +%type <expr> expr optional_expr expr_list expr_list2 %type <arr> slice_expr %type <arr> return if else do_while while for break continue %type <arr> fun_call call_other @@ -393,7 +393,7 @@ L_DATA_TYPE optional_star { $$ = $1 | $2; - global_type = $1; + global_type = $$; } ; @@ -598,7 +598,7 @@ lval->name = $1; lval->type = global_type; - $$.type = T_ARRAY; + $$.type = T_SPECIAL; $$.u.a = type_xmalloc(array_t); init_array($$.u.a); array_push($$.u.a, lval); @@ -1274,8 +1274,63 @@ array_push(&$$.arr, (void *) F_PUSH_INT); array_push(&$$.arr, (void *) $1); } + | L_OPEN_ARRAY expr_list L_CLOSE_ARRAY + { + $$.lval = 0; + $$.side_effect = $2.side_effect; + $$.type = $2.type | T_ARRAY; + init_array(&$$.arr); + $2.arr.length--; + array_concat(&$$.arr, &$2.arr); + array_push(&$$.arr, (void *) F_PUSH_ARRAY); + array_push(&$$.arr, (void *) $2.type); + array_push(&$$.arr, (void *) $2.arr.data[$2.arr.length]); + $2.arr.data = realloc($2.arr.data, sizeof(void *) * $2.arr.length); + } ; +expr_list: + /* empty */ + { + $$.type = $$.side_effect = 0; + init_array(&$$.arr); + array_push(&$$.arr, (void *) 0); + } + | expr_list2 + { + $$.type = $1.type; + $$.side_effect = $1.side_effect; + init_array(&$$.arr); + array_concat(&$$.arr, &$1.arr); + } + ; + +expr_list2: + expr + { + $$.type = $1.type; + $$.side_effect = $1.side_effect; + init_array(&$$.arr); + array_concat(&$$.arr, &$1.arr); + array_push(&$$.arr, (void *) 1); + } + | expr ',' expr_list2 + { + $$.side_effect = $1.side_effect || $3.side_effect; + if($1.type != $3.type) { + comp_error("all elements in array constant must be of same type"); + /*$$.type = T_MIXED;*/ + } + $$.type = $1.type; + init_array(&$$.arr); + array_concat(&$$.arr, &$1.arr); + $3.arr.length--; + array_concat(&$$.arr, &$3.arr); + array_push(&$$.arr, $3.arr.data[$3.arr.length] + 1); + $3.arr.data = realloc($3.arr.data, sizeof(void *) * $3.arr.length); + } + ; + slice_expr: expr { @@ -1336,30 +1391,13 @@ $$ = strcat($$, $2); } ; - -/* - -expr_list: - / * empty * / - | expr ',' expr_list - { - - } - ; - -array: - L_OPEN_ARRAY expr_list L_CLOSE_ARRAY - { - } - ; -*/ - + optional_star: /* empty */ { $$ = 0; } - | '*' + | L_MUL { $$ = T_ARRAY; } Index: compile.c =================================================================== RCS file: /cvsroot/agd/server/src/compile.c,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- compile.c 1 Apr 2004 19:17:14 -0000 1.19 +++ compile.c 3 Apr 2004 20:19:08 -0000 1.20 @@ -27,7 +27,7 @@ static array_t *curr_block; static array_t block_history; -#ifdef YYDEBUG +#if YYDEBUG || 1 extern int yydebug; #endif @@ -365,10 +365,11 @@ for(i=0;i<vars->length;i++) { variable_t *var = vars->data[i]; array_t *assign = NULL; - if(var->type == T_ARRAY) { + if(var->type == T_SPECIAL) { /* One way to do this would be to remember all the global assignments * and later insert them into the create() function, if one is defined * in the source. */ + type &= ~T_ARRAY; if(type == ID_GVAR) { comp_warning("initializing global variables unimplemented"); } else /* LVAR */ { @@ -377,13 +378,13 @@ var = var->u.a->data[0]; } - /*init_var(var);*/ if(type == ID_GVAR) { - /*array_push(&cob->variables, var);*/ /* this_ob is set to the compiled object during compilation. */ array_push(&this_ob->variables, var); } else /* LVAR */{ - switch(var->type) { + if(var->type & T_ARRAY) + array_push(curr_block, (void *) F_PUSH_VOID); + else switch(var->type & ~T_ARRAY) { case T_INT: array_push(curr_block, (void *) F_PUSH_INT); if(var->u.i) { @@ -596,7 +597,7 @@ this_file = path; /* curr_fn = ob->name;*/ -#if defined(DEBUG) && YYDEBUG +#if defined(DEBUG) && YYDEBUG || 1 if(conf.debuglevel > 4) { yydebug = 1; } Index: debug.c =================================================================== RCS file: /cvsroot/agd/server/src/debug.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- debug.c 1 Apr 2004 19:10:53 -0000 1.10 +++ debug.c 3 Apr 2004 20:19:08 -0000 1.11 @@ -95,6 +95,8 @@ return "F_PUSH_VOID"; case F_PUSH_NULL_OBJECT: return "F_PUSH_NULL_OBJECT"; + case F_PUSH_ARRAY: + return "F_PUSH_ARRAY"; case F_PUSH_LVAR: return "F_PUSH_LVAR"; case F_PUSH_LVAR_LVALUE: @@ -190,6 +192,12 @@ s = a->data[++*i]; printf("\"%s\"", s); break; + case F_PUSH_ARRAY: + num = (int)a->data[++*i]; + printf("type %d ", num); + num = (int)a->data[++*i]; + printf("%d", num); + break; case F_CALL_DFUN: index = (int)a->data[++*i]; num = (int)a->data[++*i]; |