From: Peep P. <so...@us...> - 2004-03-18 20:56:22
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1708 Modified Files: lang.y Log Message: String ranges. Index: lang.y =================================================================== RCS file: /cvsroot/agd/server/src/lang.y,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- lang.y 16 Mar 2004 20:12:21 -0000 1.5 +++ lang.y 18 Mar 2004 20:46:39 -0000 1.6 @@ -15,7 +15,8 @@ int compare_args(array_t *arg1, array_t *arg2); #define MAKE_LVALUE(x) if((int)x.arr.data[0] == F_PUSH_LVAR) x.arr.data[0] = (void *)F_PUSH_LVAR_LVALUE;\ - else if((int)x.arr.data[0] == F_PUSH_GVAR) x.arr.data[0] = (void*)F_PUSH_GVAR_LVALUE; + else if((int)x.arr.data[0] == F_PUSH_GVAR) x.arr.data[0] = (void*)F_PUSH_GVAR_LVALUE;\ + else if((int)x.arr.data[x.arr.length-1] == F_RANGE) x.arr.data[x.arr.length-1] = (void*)F_RANGE_LVALUE\ %} %expect 1 @@ -46,14 +47,16 @@ /* operators */ %right L_ASSIGN %left L_OR L_AND -%left L_LT L_GT L_LE L_GE %left L_EQ L_NE %left L_PLUS L_MINUS +%left ':' +%left '?' +%left L_LT L_GT L_LE L_GE %left L_MUL L_DIV L_MOD -%right '?' ':' %left L_DEREF /* L_DEREF _is_ a valid lval for call_other. i.e: ob->get_another_ob()->call(), as long as ob->get_another_ob() returns T_OBJECT. */ +%right '[' ']' %nonassoc L_NEG L_NOT L_INC L_DEC %nonassoc L_TYPECAST @@ -66,7 +69,7 @@ %type <var> var_def %type <arr> call_args call_arg_list -%type <expr> expr optional_expr +%type <expr> expr optional_expr range_expr %type <arr> return if else do_while while %type <arr> fun_call call_other %type <arr> statements statement @@ -136,7 +139,6 @@ define_id(v->name, ID_ARG, v->type, NULL); } } - /*scope_level--;*/ $<i>$ = yylloc.line; } block_or_semi @@ -234,7 +236,7 @@ { def_id_t *idp; int i; - idp = $<id>2; + idp = $<id>3; idp->args = copy_array(&$5, NULL); scope_level++; @@ -249,7 +251,6 @@ define_id(v->name, ID_ARG, v->type, NULL); } } - /*scope_level--;*/ $<i>$ = yylloc.line; } block_or_semi @@ -285,10 +286,12 @@ case T_VOID: array_push(&$8, (void *) F_PUSH_VOID); break; - array_push(&$8, (void *) F_RETURN); - if(warn) - comp_error("control reaches end of non-void function"); } + array_push(&$8, (void *) F_RETURN); + if(warn) { + comp_error("control reaches end of non-void function"); + } + } add_function(idp, $<i>7, &$8); @@ -759,6 +762,7 @@ $$.side_effect = 1; $$.lval = $$.direct_type = 0; $$.type = $2.type; + init_array(&$$.arr); MAKE_LVALUE($2); array_concat(&$$.arr, &$2.arr); @@ -770,6 +774,7 @@ $$.side_effect = 1; $$.lval = $$.direct_type = 0; $$.type = $2.type; + init_array(&$$.arr); if($2.direct_type) { array_push(&$$.arr, (void *) F_PUSH_INT); @@ -783,7 +788,8 @@ { check_operand(F_NOT, $2.type, 0); $$.lval = $$.side_effect = $$.direct_type = 0; - $$.type = $2.type; + $$.type = T_INT; + init_array(&$$.arr); array_concat(&$$.arr, &$2.arr); array_push(&$$.arr, (void *) F_NOT); @@ -794,6 +800,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); array_concat(&$$.arr, &$1.arr); @@ -806,6 +813,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); array_concat(&$$.arr, &$1.arr); @@ -818,6 +826,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); array_concat(&$$.arr, &$1.arr); @@ -830,6 +839,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); array_concat(&$$.arr, &$1.arr); @@ -842,6 +852,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); array_concat(&$$.arr, &$1.arr); @@ -866,6 +877,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); @@ -888,6 +900,7 @@ $$.lval = $$.direct_type = 0; $$.side_effect = $1.side_effect || $3.side_effect; + $$.type = T_INT; init_array(&$$.arr); @@ -925,32 +938,7 @@ array_push(&$$.arr, (void *) $5.arr.length + 1); array_concat(&$$.arr, &$5.arr); } - | string - { - $$.lval = $$.side_effect = 0; - $$.direct_type = 1; - $$.type = T_STRING; - /* int index = add_string_to_table($1); - array_push(&$$.arr, (void *) F_PUSH_STRING); - array_push(&$$.arr, (void *) index); */ - init_array(&$$.arr); - array_push(&$$.arr, (void *) F_PUSH_STRING); - array_push(&$$.arr, $1); - } - | L_INTEGER - { - $$.lval = $$.side_effect = 0; - $$.direct_type = 1; - $$.type = T_INT; - init_array(&$$.arr); - array_push(&$$.arr, (void *) F_PUSH_INT); - array_push(&$$.arr, (void *) $1); - } - | defined_name '[' expr ']' - { - comp_error("ranges not supported yet"); - } - | '(' expr ')' /* Probably needs a precedence fix. */ + | '(' expr ')' { $$ = $2; /* Could cause problems. */ } @@ -975,14 +963,18 @@ { $$.side_effect = 1; $$.lval = $$.direct_type = 0; - $$.type = (int) array_pop(&$1); + init_array(&$$.arr); - array_concat(&$$.arr, &$1); + if($1.length) { + $$.type = (int) array_pop(&$1); + array_concat(&$$.arr, &$1); + } } | defined_name { $$.side_effect = $$.direct_type = 0; $$.lval = 1; + $$.type = 0; init_array(&$$.arr); if($1) { @@ -999,6 +991,19 @@ $$.type = $1->lpc_type; } } + | expr '[' range_expr ']' + { + $$.lval = 1; + $$.direct_type = 0; + $$.side_effect = 0; + $$.type = T_INT; + check_operand(F_RANGE, $1.type, 0); + + init_array(&$$.arr); + array_concat(&$$.arr, &$1.arr); + array_concat(&$$.arr, &$3.arr); + array_push(&$$.arr, (void *) F_RANGE); + } | expr L_ASSIGN expr { if(!$1.lval) { @@ -1018,8 +1023,50 @@ array_concat(&$$.arr, &$3.arr); array_push(&$$.arr, (void *) F_ASSIGN); } + | string + { + $$.lval = $$.side_effect = 0; + $$.direct_type = 1; + $$.type = T_STRING; + /* int index = add_string_to_table($1); + array_push(&$$.arr, (void *) F_PUSH_STRING); + array_push(&$$.arr, (void *) index); */ + init_array(&$$.arr); + array_push(&$$.arr, (void *) F_PUSH_STRING); + array_push(&$$.arr, $1); + } + | L_INTEGER + { + $$.lval = $$.side_effect = 0; + $$.direct_type = 1; + $$.type = T_INT; + init_array(&$$.arr); + array_push(&$$.arr, (void *) F_PUSH_INT); + array_push(&$$.arr, (void *) $1); + } ; - + +range_expr: + expr + { + if($1.type != T_INT) { + comp_error("index must be integer"); + } + $$ = $1; + } +/* | L_INTEGER '..' L_INTEGER + { + if($3 > $1) { + comp_error("range second element should be bigger than first"); + } + array_push(&$$, (void *) F_BEGIN); + array_push(&$$, (void *) $1); + array_push(&$$, (void *) F_END); + array_push(&$$, (void *) $3); + } +*/ + ; + string: string_con2 ; @@ -1036,6 +1083,8 @@ L_STRING | string_con2 L_STRING { + printf("$1.len: %d; $2.len: %d\n", strlen($1), strlen($2)); + printf("$1: %s; $2: %s\n", $1, $2); $$ = strcat($1, $2); } ; @@ -1145,6 +1194,11 @@ expr_t *e = $3.data[i]; array_push(&tempargs, (void *) e->type); } + + if(!$1) { + break; + } + i = compare_args($1->args, &tempargs); switch(i) { @@ -1162,7 +1216,7 @@ break; default: /* > 0 */ sprintf(buf, "Argument %d type mismatch for %s - expected %d, got %d", - i, $1, $1->args->data[i], tempargs.data[i]); + i, $1->name, $1->args->data[i], tempargs.data[i]); comp_error(buf); break; } @@ -1229,7 +1283,7 @@ int i; array_t *realargs; - if($1.type != T_OBJECT || ($1.type == T_STRING && !$1.direct_type)) { + if($1.type != T_OBJECT && !($1.type == T_STRING && $1.direct_type)) { comp_error("not an object to '->'"); } |