From: Peep P. <so...@us...> - 2004-03-21 13:36:26
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29000/src Modified Files: lang.h lang.y lex.l Log Message: Added break; and continue; Index: lex.l =================================================================== RCS file: /cvsroot/agd/server/src/lex.l,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- lex.l 21 Mar 2004 13:02:37 -0000 1.9 +++ lex.l 21 Mar 2004 13:26:17 -0000 1.10 @@ -262,6 +262,8 @@ else RET(s, "else", L_ELSE); do RET(s, "do", L_DO); while RET(s, "while", L_WHILE); +break RET(s, "break", L_BREAK); +continue RET(s, "continue", L_CONTINUE); void yylval.i = T_VOID; RET(s, "void", L_DATA_TYPE); int yylval.i = T_INT; RET(s, "int", L_DATA_TYPE); string yylval.i = T_STRING; RET(s, "string", L_DATA_TYPE); Index: lang.h =================================================================== RCS file: /cvsroot/agd/server/src/lang.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- lang.h 21 Mar 2004 13:02:37 -0000 1.4 +++ lang.h 21 Mar 2004 13:26:17 -0000 1.5 @@ -34,43 +34,45 @@ L_ELSE = 260, L_DO = 261, L_WHILE = 262, - L_DATA_TYPE = 263, - L_IDENTIFIER = 264, - L_STRING = 265, - L_INTEGER = 266, - L_OPEN_MAPPING = 267, - L_CLOSE_MAPPING = 268, - L_OPEN_ARRAY = 269, - L_CLOSE_ARRAY = 270, - L_OPEN_FUNP = 271, - L_CLOSE_FUNP = 272, - L_ASSIGN = 273, - L_ME = 274, - L_PE = 275, - L_DE = 276, - L_MOE = 277, - L_MUE = 278, - L_POWE = 279, - L_AND = 280, - L_OR = 281, - L_NE = 282, - L_EQ = 283, - L_MINUS = 284, - L_PLUS = 285, - L_GE = 286, - L_LE = 287, - L_GT = 288, - L_LT = 289, - L_MOD = 290, - L_DIV = 291, - L_MUL = 292, - L_POW = 293, - L_DEREF = 294, - L_DEC = 295, - L_INC = 296, - L_NOT = 297, - L_NEG = 298, - L_TYPECAST = 299 + L_CONTINUE = 263, + L_BREAK = 264, + L_DATA_TYPE = 265, + L_IDENTIFIER = 266, + L_STRING = 267, + L_INTEGER = 268, + L_OPEN_MAPPING = 269, + L_CLOSE_MAPPING = 270, + L_OPEN_ARRAY = 271, + L_CLOSE_ARRAY = 272, + L_OPEN_FUNP = 273, + L_CLOSE_FUNP = 274, + L_ASSIGN = 275, + L_ME = 276, + L_PE = 277, + L_DE = 278, + L_MOE = 279, + L_MUE = 280, + L_POWE = 281, + L_AND = 282, + L_OR = 283, + L_NE = 284, + L_EQ = 285, + L_MINUS = 286, + L_PLUS = 287, + L_GE = 288, + L_LE = 289, + L_GT = 290, + L_LT = 291, + L_MOD = 292, + L_DIV = 293, + L_MUL = 294, + L_POW = 295, + L_DEREF = 296, + L_DEC = 297, + L_INC = 298, + L_NOT = 299, + L_NEG = 300, + L_TYPECAST = 301 }; #endif #define L_RETURN 258 @@ -78,49 +80,51 @@ #define L_ELSE 260 #define L_DO 261 #define L_WHILE 262 -#define L_DATA_TYPE 263 -#define L_IDENTIFIER 264 -#define L_STRING 265 -#define L_INTEGER 266 -#define L_OPEN_MAPPING 267 -#define L_CLOSE_MAPPING 268 -#define L_OPEN_ARRAY 269 -#define L_CLOSE_ARRAY 270 -#define L_OPEN_FUNP 271 -#define L_CLOSE_FUNP 272 -#define L_ASSIGN 273 -#define L_ME 274 -#define L_PE 275 -#define L_DE 276 -#define L_MOE 277 -#define L_MUE 278 -#define L_POWE 279 -#define L_AND 280 -#define L_OR 281 -#define L_NE 282 -#define L_EQ 283 -#define L_MINUS 284 -#define L_PLUS 285 -#define L_GE 286 -#define L_LE 287 -#define L_GT 288 -#define L_LT 289 -#define L_MOD 290 -#define L_DIV 291 -#define L_MUL 292 -#define L_POW 293 -#define L_DEREF 294 -#define L_DEC 295 -#define L_INC 296 -#define L_NOT 297 -#define L_NEG 298 -#define L_TYPECAST 299 +#define L_CONTINUE 263 +#define L_BREAK 264 +#define L_DATA_TYPE 265 +#define L_IDENTIFIER 266 +#define L_STRING 267 +#define L_INTEGER 268 +#define L_OPEN_MAPPING 269 +#define L_CLOSE_MAPPING 270 +#define L_OPEN_ARRAY 271 +#define L_CLOSE_ARRAY 272 +#define L_OPEN_FUNP 273 +#define L_CLOSE_FUNP 274 +#define L_ASSIGN 275 +#define L_ME 276 +#define L_PE 277 +#define L_DE 278 +#define L_MOE 279 +#define L_MUE 280 +#define L_POWE 281 +#define L_AND 282 +#define L_OR 283 +#define L_NE 284 +#define L_EQ 285 +#define L_MINUS 286 +#define L_PLUS 287 +#define L_GE 288 +#define L_LE 289 +#define L_GT 290 +#define L_LT 291 +#define L_MOD 292 +#define L_DIV 293 +#define L_MUL 294 +#define L_POW 295 +#define L_DEREF 296 +#define L_DEC 297 +#define L_INC 298 +#define L_NOT 299 +#define L_NEG 300 +#define L_TYPECAST 301 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) -#line 28 "lang.y" +#line 34 "lang.y" typedef union YYSTYPE { char *str; int i; @@ -131,7 +135,7 @@ def_id_t *id; } YYSTYPE; /* Line 1240 of yacc.c. */ -#line 135 "lang.h" +#line 139 "lang.h" # define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define YYSTYPE_IS_DECLARED 1 # define YYSTYPE_IS_TRIVIAL 1 Index: lang.y =================================================================== RCS file: /cvsroot/agd/server/src/lang.y,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- lang.y 21 Mar 2004 13:02:37 -0000 1.11 +++ lang.y 21 Mar 2004 13:26:17 -0000 1.12 @@ -3,11 +3,17 @@ #include "lpc_incl.h" #include "lex.h" +/* Used during compilation, replaced with F_JMP. */ +#define F_BREAK -5 +#define F_CONTINUE -6 + YYLTYPE yylloc; /* Lexical tie-ins. */ static int global_type, - curr_f_type; + curr_f_type, + break_allowed, + continue_allowed; extern int scope_level; void add_function(def_id_t *idp, int lineno, array_t *code); @@ -37,6 +43,7 @@ /* keywords */ %token L_RETURN L_IF L_ELSE L_DO L_WHILE +%token L_CONTINUE L_BREAK /* operands */ %token <i> L_DATA_TYPE @@ -78,7 +85,7 @@ %type <arr> call_args call_arg_list %type <expr> expr optional_expr range_expr -%type <arr> return if else do_while while +%type <arr> return if else do_while while break continue %type <arr> fun_call call_other %type <arr> statements statement %type <arr> block block_or_semi @@ -426,6 +433,24 @@ init_array(&$$); array_concat(&$$, &$1); } + | break ';' + { + if(!break_allowed) { + comp_error("illegal break"); + } + + init_array(&$$); + array_concat(&$$, &$1); + } + | continue ';' + { + if(!continue_allowed) { + comp_error("illegal continue"); + } + + init_array(&$$); + array_concat(&$$, &$1); + } | expr ';' { init_array(&$$); @@ -1323,31 +1348,72 @@ ; do_while: - L_DO statement L_WHILE '(' expr ')' + L_DO + { + break_allowed = continue_allowed = 1; + } + statement L_WHILE '(' expr ')' { init_array(&$$); array_push(&$$, (void *) F_JMP); - array_push(&$$, (void *) ($5.arr.length + 3)); - array_concat(&$$, &$5.arr); + array_push(&$$, (void *) ($6.arr.length + 3)); + array_concat(&$$, &$6.arr); array_push(&$$, (void *) F_JMPF); - array_push(&$$, (void *) ($2.length + 3)); - array_concat(&$$, &$2); + array_push(&$$, (void *) ($3.length + 3)); + array_concat(&$$, &$3); array_push(&$$, (void *) F_JMP); - array_push(&$$, (void *) -($2.length + $5.arr.length + 3)); + array_push(&$$, (void *) -($3.length + $6.arr.length + 3)); + break_allowed = continue_allowed = 0; } while: - L_WHILE '(' expr ')' statement + L_WHILE + { + break_allowed = continue_allowed = 1; + } + '(' expr ')' statement { + int i; + for(i=0;i<$6.length;i++) { + if((int)$6.data[i] == F_BREAK) { + $6.data[i] = (void *) F_JMP; + i++; + $6.data[i] = (void *) $6.length - i + 2; + } else if((int)$6.data[i] == F_CONTINUE) { + $6.data[i] = (void *) F_JMP; + i++; + $6.data[i] = (void *) -(i + $4.arr.length + 2); + } + } + init_array(&$$); - array_concat(&$$, &$3.arr); + array_concat(&$$, &$4.arr); array_push(&$$, (void *) F_JMPF); - array_push(&$$, (void *) ($5.length + 3)); - array_concat(&$$, &$5); + array_push(&$$, (void *) ($6.length + 3)); + array_concat(&$$, &$6); array_push(&$$, (void *) F_JMP); - array_push(&$$, (void *) -($5.length + $3.arr.length + 3)); + array_push(&$$, (void *) -($6.length + $4.arr.length + 3)); + break_allowed = continue_allowed = 1; } ; +break: + L_BREAK + { + init_array(&$$); + array_push(&$$, (void *) F_BREAK); + array_push(&$$, (void *) 0); + } + ; + +continue: + L_CONTINUE + { + init_array(&$$); + array_push(&$$, (void *) F_CONTINUE); + array_push(&$$, (void *) 0); + } + ; + fun_call: defined_name '(' call_args ')' { |