use labels for backward jumps, nop for forward jumps
[swftools.git] / lib / as3 / parser.y
index 7d5404c..ae48ed8 100644 (file)
@@ -87,6 +87,7 @@
 %token<token> KW_SET "set"
 %token<token> KW_VOID "void"
 %token<token> KW_STATIC
+%token<token> KW_INSTANCEOF "instanceof"
 %token<token> KW_IMPORT "import"
 %token<token> KW_RETURN "return"
 %token<token> KW_TYPEOF "typeof"
@@ -293,7 +294,7 @@ DECLARE_LIST(state);
     namespace_t m##_ns;\
     registry_fill_multiname(&m, &m##_ns, x);
                     
-#define MEMBER_MULTINAME(m,f) \
+#define MEMBER_MULTINAME(m,f,n) \
     multiname_t m;\
     namespace_t m##_ns;\
     if(f) { \
@@ -307,7 +308,7 @@ DECLARE_LIST(state);
         m.type = MULTINAME; \
         m.ns =0; \
         m.namespace_set = &nopackage_namespace_set; \
-        m.name = f->name; \
+        m.name = n; \
     }
 
 /* warning: list length of namespace set is undefined */
@@ -380,10 +381,6 @@ static void old_state()
         list_free(oldstate->wildcard_imports);
         dict_destroy(oldstate->imports);oldstate->imports=0;
     }
-    if(state->method)
-        state->method->initcode = 
-            code_append(state->method->initcode, 
-                        oldstate->method->initcode);
 }
 void initialize_state()
 {
@@ -530,8 +527,9 @@ static void startclass(int flags, char*classname, classinfo_t*extends, classinfo
 
     state->cls->abc = abc_class_new(global->file, &classname2, extends2);
     if(flags&FLAG_FINAL) abc_class_final(state->cls->abc);
-    if(flags&FLAG_DYNAMIC) abc_class_sealed(state->cls->abc);
+    if(!(flags&FLAG_DYNAMIC)) abc_class_sealed(state->cls->abc);
     if(interface) abc_class_interface(state->cls->abc);
+    abc_class_protectedNS(state->cls->abc, classname);
 
     for(mlist=implements;mlist;mlist=mlist->next) {
         MULTINAME(m, mlist->classinfo);
@@ -930,8 +928,12 @@ code_t*converttype(code_t*c, classinfo_t*from, classinfo_t*to)
 
 code_t*defaultvalue(code_t*c, classinfo_t*type)
 {
-    if(TYPE_IS_INT(type) || TYPE_IS_UINT(type) || TYPE_IS_FLOAT(type)) {
+    if(TYPE_IS_INT(type)) {
        c = abc_pushbyte(c, 0);
+    } else if(TYPE_IS_UINT(type)) {
+       c = abc_pushuint(c, 0);
+    } else if(TYPE_IS_FLOAT(type)) {
+       c = abc_pushnan(c);
     } else if(TYPE_IS_BOOLEAN(type)) {
        c = abc_pushfalse(c);
     } else {
@@ -1232,8 +1234,7 @@ MAYBEELSE: "else" CODEBLOCK {$$=$2;}
 //MAYBEELSE: ';' "else" CODEBLOCK {$$=$3;}
 
 IF  : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE {
-    $$ = state->method->initcode;state->method->initcode=0;
-
+    $$ = code_new();
     $$ = code_append($$, $4.c);
     code_t*myjmp,*myif = $$ = abc_iffalse($$, 0);
    
@@ -1241,10 +1242,10 @@ IF  : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE {
     if($7) {
         myjmp = $$ = abc_jump($$, 0);
     }
-    myif->branch = $$ = abc_label($$);
+    myif->branch = $$ = abc_nop($$);
     if($7) {
         $$ = code_append($$, $7);
-        myjmp->branch = $$ = abc_label($$);
+        myjmp->branch = $$ = abc_nop($$);
     }
     
     $$ = killvars($$);old_state();
@@ -1255,8 +1256,7 @@ FOR_INIT : VARIABLE_DECLARATION
 FOR_INIT : VOIDEXPRESSION
 
 FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK {
-    $$ = state->method->initcode;state->method->initcode=0;
-
+    $$ = code_new();
     $$ = code_append($$, $4);
     code_t*loopstart = $$ = abc_label($$);
     $$ = code_append($$, $6.c);
@@ -1264,7 +1264,7 @@ FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CO
     $$ = code_append($$, $10);
     $$ = code_append($$, $8);
     $$ = abc_jump($$, loopstart);
-    code_t*out = $$ = abc_label($$);
+    code_t*out = $$ = abc_nop($$);
     breakjumpsto($$, out);
     myif->branch = out;
 
@@ -1272,15 +1272,15 @@ FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CO
 }
 
 WHILE : "while" '(' {new_state();} EXPRESSION ')' CODEBLOCK {
-    $$ = state->method->initcode;state->method->initcode=0;
+    $$ = code_new();
 
     code_t*myjmp = $$ = abc_jump($$, 0);
     code_t*loopstart = $$ = abc_label($$);
     $$ = code_append($$, $6);
-    myjmp->branch = $$ = abc_label($$);
+    myjmp->branch = $$ = abc_nop($$);
     $$ = code_append($$, $4.c);
     $$ = abc_iftrue($$, loopstart);
-    code_t*out = $$ = abc_label($$);
+    code_t*out = $$ = abc_nop($$);
     breakjumpsto($$, out);
 
     $$ = killvars($$);old_state();
@@ -1894,6 +1894,12 @@ E : E "as" E {char use_astype=0; // flash player's astype works differently than
               }
              }
 
+E : E "instanceof" E 
+             {$$.c = code_append($1.c, $3.c);
+              $$.c = abc_instanceof($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+
 E : E "is" E {$$.c = code_append($1.c, $3.c);
               $$.c = abc_istypelate($$.c);
               $$.t = TYPE_BOOLEAN;
@@ -2084,7 +2090,7 @@ E : "super" '.' T_IDENTIFIER
 
               memberinfo_t*f = registry_findmember(t, $3);
               namespace_t ns = {flags2access(f->flags), ""};
-              MEMBER_MULTINAME(m, f);
+              MEMBER_MULTINAME(m, f, $3);
               $$.c = 0;
               $$.c = abc_getlocal_0($$.c);
               $$.c = abc_getsuper2($$.c, &m);
@@ -2107,7 +2113,7 @@ E : E '.' T_IDENTIFIER
                  if(f && f->slot && !noslot) {
                      $$.c = abc_getslot($$.c, f->slot);
                  } else {
-                     MEMBER_MULTINAME(m, f);
+                     MEMBER_MULTINAME(m, f, $3);
                      $$.c = abc_getproperty2($$.c, &m);
                  }
                  /* determine type */