X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fas3%2Fparser.y;h=a0e58ddb8a6f08cac2d9fa587077370fa128268b;hb=94b8b908c3a7c2143b45122060d5a646de218c73;hp=b11c5eae6c2c6c5fe1bd4e1e69499657b5319578;hpb=fe0554f343c5f4d4bd9affdab418b3db2f7eb949;p=swftools.git diff --git a/lib/as3/parser.y b/lib/as3/parser.y index b11c5ea..a0e58dd 100644 --- a/lib/as3/parser.y +++ b/lib/as3/parser.y @@ -81,6 +81,7 @@ %token KW_NATIVE %token KW_FUNCTION "function" %token KW_UNDEFINED "undefined" +%token KW_CONTINUE "continue" %token KW_FOR "for" %token KW_CLASS "class" %token KW_CONST "const" @@ -94,7 +95,7 @@ %token KW_INTERFACE "interface" %token KW_NULL "null" %token KW_VAR "var" -%token KW_DYNAMIC +%token KW_DYNAMIC "dynamic" %token KW_OVERRIDE %token KW_FINAL %token KW_GET "get" @@ -114,6 +115,7 @@ %token KW_BREAK "break" %token KW_IS "is" %token KW_AS "as" +%token KW_DO "do" %token T_EQEQ "==" %token T_EQEQEQ "===" @@ -140,7 +142,7 @@ %token T_USHR ">>>" %token T_SHR ">>" -%type X_IDENTIFIER PACKAGE +%type X_IDENTIFIER PACKAGE MAYBELABEL %type VARCONST %type CODE %type CODEPIECE @@ -156,7 +158,7 @@ %type MAYBEEXPRESSION %type E DELETE %type CONSTANT -%type FOR IF WHILE MAYBEELSE BREAK RETURN +%type FOR IF WHILE DO_WHILE MAYBEELSE BREAK RETURN CONTINUE %type USE_NAMESPACE %type FOR_INIT %type IMPORT @@ -482,7 +484,7 @@ static void startclass(int flags, char*classname, classinfo_t*extends, classinfo printf("\n"); */ - if(flags&~(FLAG_INTERNAL|FLAG_PUBLIC|FLAG_FINAL)) + if(flags&~(FLAG_INTERNAL|FLAG_PUBLIC|FLAG_FINAL|FLAG_DYNAMIC)) syntaxerror("invalid modifier(s)"); if((flags&(FLAG_PUBLIC|FLAG_INTERNAL)) == (FLAG_PUBLIC|FLAG_INTERNAL)) @@ -707,6 +709,21 @@ code_t* killvars(code_t*c) return c; } +void check_code_for_break(code_t*c) +{ + while(c) { + if(c->opcode == OPCODE___BREAK__) { + char*name = string_cstr(c->data[0]); + syntaxerror("Unresolved \"break %s\"", name); + } + if(c->opcode == OPCODE___CONTINUE__) { + char*name = string_cstr(c->data[0]); + syntaxerror("Unresolved \"continue %s\"", name); + } + c=c->prev; + } +} + static void check_constant_against_type(classinfo_t*t, constant_t*c) { @@ -858,7 +875,13 @@ static void endfunction(token_t*ns, int flags, enum yytokentype getset, char*nam syntaxerror("non-optional parameter not allowed after optional parameters"); } } - f->body->code = body; + check_code_for_break(body); + + if(f->body) + f->body->code = body; + else //interface + if(body) + syntaxerror("interface methods can't have a method body"); old_state(); } @@ -870,16 +893,30 @@ char is_subtype_of(classinfo_t*type, classinfo_t*supertype) return 1; // FIXME } -void breakjumpsto(code_t*c, code_t*jump) +void breakjumpsto(code_t*c, char*name, code_t*jump) { - while(c->prev) - c=c->prev; while(c) { if(c->opcode == OPCODE___BREAK__) { - c->opcode = OPCODE_JUMP; - c->branch = jump; + string_t*name2 = c->data[0]; + if(!name2->len || !strncmp(name2->str, name, name2->len)) { + c->opcode = OPCODE_JUMP; + c->branch = jump; + } } - c = c->next; + c=c->prev; + } +} +void continuejumpsto(code_t*c, char*name, code_t*jump) +{ + while(c) { + if(c->opcode == OPCODE___CONTINUE__) { + string_t*name2 = c->data[0]; + if(!name2->len || !strncmp(name2->str, name, name2->len)) { + c->opcode = OPCODE_JUMP; + c->branch = jump; + } + } + c = c->prev; } } @@ -1163,8 +1200,12 @@ PROGRAM: MAYBECODE MAYBECODE: CODE {$$=$1;/*TODO: do something with this code if we're not in a function*/} MAYBECODE: {$$=code_new();} -CODE: CODE CODEPIECE {$$=code_append($1,$2);} -CODE: CODEPIECE {$$=$1;} +CODE: CODE CODEPIECE { + $$=code_append($1,$2); +} +CODE: CODEPIECE { + $$=$1; +} CODEPIECE: PACKAGE_DECLARATION {$$=code_new();/*enters a scope*/} CODEPIECE: CLASS_DECLARATION {$$=code_new();/*enters a scope*/} @@ -1176,7 +1217,9 @@ CODEPIECE: VARIABLE_DECLARATION {$$=$1} CODEPIECE: VOIDEXPRESSION {$$=$1} CODEPIECE: FOR {$$=$1} CODEPIECE: WHILE {$$=$1} +CODEPIECE: DO_WHILE {$$=$1} CODEPIECE: BREAK {$$=$1} +CODEPIECE: CONTINUE {$$=$1} CODEPIECE: RETURN {$$=$1} CODEPIECE: IF {$$=$1} CODEPIECE: NAMESPACE_DECLARATION {/*TODO*/$$=code_new();} @@ -1269,43 +1312,74 @@ IF : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE { $$ = killvars($$);old_state(); } +MAYBELABEL : T_IDENTIFIER ':' {$$=$1;} +MAYBELABEL : {$$="";} + FOR_INIT : {$$=code_new();} FOR_INIT : VARIABLE_DECLARATION FOR_INIT : VOIDEXPRESSION -FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK { +FOR : MAYBELABEL "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK { $$ = code_new(); - $$ = code_append($$, $4); + $$ = code_append($$, $5); code_t*loopstart = $$ = abc_label($$); - $$ = code_append($$, $6.c); + $$ = code_append($$, $7.c); code_t*myif = $$ = abc_iffalse($$, 0); - $$ = code_append($$, $10); - $$ = code_append($$, $8); + $$ = code_append($$, $11); + code_t*cont = $$ = abc_nop($$); + $$ = code_append($$, $9); $$ = abc_jump($$, loopstart); code_t*out = $$ = abc_nop($$); - breakjumpsto($$, out); + breakjumpsto($$, $1, out); + continuejumpsto($$, $1, cont); myif->branch = out; $$ = killvars($$);old_state(); } -WHILE : "while" '(' {new_state();} EXPRESSION ')' CODEBLOCK { +WHILE : MAYBELABEL "while" '(' {new_state();} EXPRESSION ')' CODEBLOCK { $$ = code_new(); code_t*myjmp = $$ = abc_jump($$, 0); code_t*loopstart = $$ = abc_label($$); - $$ = code_append($$, $6); - myjmp->branch = $$ = abc_nop($$); - $$ = code_append($$, $4.c); + $$ = code_append($$, $7); + code_t*cont = $$ = abc_nop($$); + myjmp->branch = cont; + $$ = code_append($$, $5.c); $$ = abc_iftrue($$, loopstart); code_t*out = $$ = abc_nop($$); - breakjumpsto($$, out); + breakjumpsto($$, $1, out); + continuejumpsto($$, $1, cont); - $$ = killvars($$);old_state(); + $$ = killvars($$); + old_state(); } -BREAK : "break" { - $$ = abc___break__(0); +DO_WHILE : MAYBELABEL "do" {new_state();} CODEBLOCK "while" '(' EXPRESSION ')' { + $$ = code_new(); + code_t*loopstart = $$ = abc_label($$); + $$ = code_append($$, $4); + code_t*cont = $$ = abc_nop($$); + $$ = code_append($$, $7.c); + $$ = abc_iftrue($$, loopstart); + code_t*out = $$ = abc_nop($$); + breakjumpsto($$, $1, out); + continuejumpsto($$, $1, cont); + $$ = killvars($$); + old_state(); +} + +BREAK : "break" %prec prec_none { + $$ = abc___break__(0, ""); +} +BREAK : "break" T_IDENTIFIER { + $$ = abc___break__(0, $2); +} +CONTINUE : "continue" %prec prec_none { + $$ = abc___continue__(0, ""); +} +CONTINUE : "continue" T_IDENTIFIER { + $$ = abc___continue__(0, $2); } /* ------------ packages and imports ---------------- */ @@ -1952,6 +2026,17 @@ E : E '[' E ']' { $$.t = 0; // array elements have unknown type } +E : '[' MAYBE_EXPRESSION_LIST ']' { + $$.c = code_new(); + typedcode_list_t*l = 0; + int len = 0; + for(l=$2;l;l=l->next) { + $$.c = code_append($$.c, l->typedcode->c);len++; + } + $$.c = abc_newarray($$.c, len); + $$.t = registry_getarrayclass(); +} + E : E "*=" E { code_t*c = $3.c; if(BOTH_INT($1,$3)) {