added 'a op= b' implementation
[swftools.git] / lib / as3 / parser.y
index 72689ee..b794944 100644 (file)
@@ -51,8 +51,9 @@
     typedcode_t value;
     typedcode_list_t*value_list;
     param_t* param;
-    param_list_t* param_list;
+    params_t params;
     char*string;
+    constant_t*constant;
 }
 
 
 %token<token> T_GE ">="
 %token<token> T_DIVBY "/=" 
 %token<token> T_MODBY "%="
+%token<token> T_MULBY "*="
 %token<token> T_PLUSBY "+=" 
 %token<token> T_MINUSBY "-="
 %token<token> T_SHRBY ">>="
 %token<token> T_MINUSMINUS "--"
 %token<token> T_PLUSPLUS "++"
 %token<token> T_DOTDOT ".."
+%token<token> T_DOTDOTDOT "..."
 %token<token> T_SHL "<<"
 %token<token> T_USHR ">>>"
 %token<token> T_SHR ">>"
 %type <classinfo> MAYBETYPE
 %type <token> GETSET
 %type <param> PARAM
-%type <param_list> PARAM_LIST
-%type <param_list> MAYBE_PARAM_LIST
+%type <params> PARAM_LIST
+%type <params> MAYBE_PARAM_LIST
 %type <token> MODIFIERS
 %type <token> MODIFIER_LIST
+%type <constant> STATICCONSTANT MAYBESTATICCONSTANT
 %type <classinfo_list> IMPLEMENTS_LIST
 %type <classinfo> EXTENDS
 %type <classinfo_list> EXTENDS_LIST
 
 %left prec_none
 %right '?' ':'
-%nonassoc '='
-%nonassoc "/=" "%="
-%nonassoc "+=" "-="
-%nonassoc ">>="
-%nonassoc "<<="
-%nonassoc ">>>="
-%nonassoc "||"
-%nonassoc "&&"
+%right '=' "/=" "%=" "*=" "+=" "-=" ">>=" "<<=" ">>>="
+%left "||"
+%left "&&"
 %nonassoc '|'
 %nonassoc '^'
 %nonassoc '&'
@@ -295,18 +294,37 @@ typedef struct _state {
     memberinfo_t*minfo;
     abc_method_body_t*m;
 
-    array_t*vars;
-    int local_var_base;
+    dict_t*vars;
     char late_binding;
 } state_t;
 
+typedef struct _global {
+    int variable_count;
+} global_t;
+
+static global_t*global = 0;
 static state_t* state = 0;
 
 DECLARE_LIST(state);
 
 #define MULTINAME(m,x) multiname_t m;namespace_t m##_ns;registry_fill_multiname(&m, &m##_ns, x);
 
+/* warning: list length of namespace set is undefined */
+#define MULTINAME_LATE(m, access, package) \
+    namespace_t m##_ns = {access, package}; \
+    namespace_set_t m##_nsset; \
+    namespace_list_t m##_l;m##_l.next = 0; \
+    m##_nsset.namespaces = &m##_l; \
+    m##_nsset = m##_nsset; \
+    m##_l.namespace = &m##_ns; \
+    multiname_t m = {MULTINAMEL, 0, &m##_nsset, 0};
+
 static state_list_t*state_stack=0;
+    
+static void init_globals()
+{
+    global = rfx_calloc(sizeof(global_t));
+}
 
 static void new_state()
 {
@@ -318,16 +336,13 @@ static void new_state()
         memcpy(s, state, sizeof(state_t)); //shallow copy
     sl->next = state_stack;
     sl->state = s;
-    if(oldstate) {
-        s->local_var_base = array_length(oldstate->vars) + oldstate->local_var_base;
-    }
     if(!s->imports) {
         s->imports = dict_new();
     }
     state_stack = sl;
     state = s;
     state->level++;
-    state->vars = array_new();
+    state->vars = dict_new();
     state->initcode = 0;
     state->has_own_imports = 0;
 }
@@ -359,18 +374,49 @@ static void old_state()
 }
 void initialize_state()
 {
+    init_globals();
     new_state();
 
     state->file = abc_file_new();
     state->file->flags &= ~ABCFILE_LAZY;
     
     state->init = abc_initscript(state->file, 0, 0);
-    abc_method_body_t*m = state->init->method->body;
-    __ getlocal_0(m);
-    __ pushscope(m);
-    __ findpropstrict(m, "[package]::trace");
-    __ pushstring(m, "[entering global init function]");
-    __ callpropvoid(m, "[package]::trace", 1);
+    code_t*c = state->init->method->body->code;
+
+    c = abc_getlocal_0(c);
+    c = abc_pushscope(c);
+  
+    /* findpropstrict doesn't just return a scope object- it
+       also makes it "active" somehow. Push local_0 on the
+       scope stack and read it back with findpropstrict, it'll
+       contain properties like "trace". Trying to find the same
+       property on a "vanilla" local_0 yields only a "undefined" */
+    //c = abc_findpropstrict(c, "[package]::trace");
+    
+    /*c = abc_getlocal_0(c);
+    c = abc_findpropstrict(c, "[package]::trace");
+    c = abc_coerce_a(c);
+    c = abc_setlocal_1(c);
+
+    c = abc_pushbyte(c, 0);
+    c = abc_setlocal_2(c);
+   
+    code_t*xx = c = abc_label(c);
+    c = abc_findpropstrict(c, "[package]::trace");
+    c = abc_pushstring(c, "prop:");
+    c = abc_hasnext2(c, 1, 2);
+    c = abc_dup(c);
+    c = abc_setlocal_3(c);
+    c = abc_callpropvoid(c, "[package]::trace", 2);
+    c = abc_getlocal_3(c);
+    c = abc_kill(c, 3);
+    c = abc_iftrue(c,xx);*/
+
+    c = abc_findpropstrict(c, "[package]::trace");
+    c = abc_pushstring(c, "[entering global init function]");
+    c = abc_callpropvoid(c, "[package]::trace", 1);
+    
+    state->init->method->body->code = c;
 }
 void* finalize_state()
 {
@@ -567,60 +613,6 @@ static void endclass()
 
     old_state();
 }
-static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
-                          param_list_t*params, classinfo_t*type)
-{
-    token_list_t*t;
-    new_state();
-    state->function = name->text;
-    
-    if(state->m) {
-        syntaxerror("not able to start another method scope");
-    }
-
-    multiname_t*type2 = sig2mname(type);
-    if(!strcmp(state->clsinfo->name,name->text)) {
-        state->m = abc_class_constructor(state->cls, type2, 0);
-    } else {
-        state->minfo = memberinfo_register(state->clsinfo, name->text, MEMBER_METHOD);
-        state->m = abc_class_method(state->cls, type2, name->text, 0);
-        state->minfo->slot = state->m->method->trait->slot_id;
-    }
-    if(getset->type == KW_GET) {
-        state->m->method->trait->kind = TRAIT_GETTER;
-    }
-    if(getset->type == KW_SET) {
-        state->m->method->trait->kind = TRAIT_SETTER;
-    }
-
-    param_list_t*p;
-    for(p=params;p;p=p->next) {
-        multiname_t*m = sig2mname(p->param->type);
-       list_append(state->m->method->parameters, m);
-    }
-
-    /* state->vars is initialized by state_new */
-    array_append(state->vars, "this", 0);
-    for(p=params;p;p=p->next) {
-        array_append(state->vars, p->param->name, 0);
-    }
-}
-static void endfunction(code_t*body)
-{
-    code_t*c = 0;
-    if(state->late_binding) {
-        c = abc_getlocal_0(c);
-        c = abc_pushscope(c);
-    }
-    c = code_append(c, state->initcode);
-    c = code_append(c, body);
-    c = abc_returnvoid(c);
-
-    if(state->m->code) syntaxerror("internal error");
-    state->m->code = c;
-    old_state();
-}
-
 
 static token_t* empty_token()
 {
@@ -650,16 +642,21 @@ void extend_s(token_t*list, char*seperator, token_t*add) {
     list->text[l1+l2+l3]=0;
 }
 
+typedef struct _variable {
+    int index;
+    classinfo_t*type;
+} variable_t;
+
 static int find_variable(char*name, classinfo_t**m)
 {
     state_list_t* s = state_stack;
     while(s) {
-        int i = array_find(s->state->vars, name);
-        if(i>=0) {
+        variable_t*v = dict_lookup(s->state->vars, name);
+        if(v) {
             if(m) {
-                *m = array_getvalue(s->state->vars, i);
+                *m = v->type;
             }
-            return i + s->state->local_var_base;
+            return v->index;
         }
         s = s->next;
     }
@@ -674,11 +671,15 @@ static int find_variable_safe(char*name, classinfo_t**m)
 }
 static char variable_exists(char*name) 
 {
-    return array_contains(state->vars, name);
+    return dict_lookup(state->vars, name)!=0;
 }
 static int new_variable(char*name, classinfo_t*type)
 {
-    return array_append(state->vars, name, type) + state->local_var_base;
+    NEW(variable_t, v);
+    v->index = global->variable_count;
+    v->type = type;
+    dict_put(state->vars, name, v);
+    return global->variable_count++;
 }
 #define TEMPVARNAME "__as3_temp__"
 static int gettempvar()
@@ -694,17 +695,117 @@ static int gettempvar()
 code_t* killvars(code_t*c) 
 {
     int t;
-    for(t=0;t<state->vars->num;t++) {
-        classinfo_t*type = array_getvalue(state->vars, t);
-        //do this always, otherwise register types don't match
-        //in the verifier when doing nested loops
-        //if(!TYPE_IS_BUILTIN_SIMPLE(type)) {
-            c = abc_kill(c, t+state->local_var_base);
-        //}
+    for(t=0;t<state->vars->hashsize;t++) {
+        dictentry_t*e =state->vars->slots[t];
+        while(e) {
+            variable_t*v = (variable_t*)e->data;
+            //do this always, otherwise register types don't match
+            //in the verifier when doing nested loops
+            //if(!TYPE_IS_BUILTIN_SIMPLE(type)) {
+            c = abc_kill(c, v->index);
+            e = e->next;
+        }
     }
     return c;
 }
 
+
+static void check_constant_against_type(classinfo_t*t, constant_t*c)
+{
+#define xassert(b) if(!(b)) syntaxerror("Invalid default value %s for type '%s'", constant_tostring(c), t->name)
+   if(TYPE_IS_NUMBER(t)) {
+        xassert(c->type == CONSTANT_FLOAT
+             || c->type == CONSTANT_INT
+             || c->type == CONSTANT_UINT);
+   } else if(TYPE_IS_UINT(t)) {
+        xassert(c->type == CONSTANT_UINT ||
+               (c->type == CONSTANT_INT && c->i>0));
+   } else if(TYPE_IS_INT(t)) {
+        xassert(c->type == CONSTANT_INT);
+   } else if(TYPE_IS_BOOLEAN(t)) {
+        xassert(c->type == CONSTANT_TRUE
+             || c->type == CONSTANT_FALSE);
+   }
+}
+
+static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
+                          params_t*params, classinfo_t*type)
+{
+    token_list_t*t;
+    new_state();
+    global->variable_count = 0;
+    state->function = name->text;
+    
+    if(state->m) {
+        syntaxerror("not able to start another method scope");
+    }
+
+    multiname_t*type2 = sig2mname(type);
+    if(!strcmp(state->clsinfo->name,name->text)) {
+        state->m = abc_class_constructor(state->cls, type2, 0);
+    } else {
+        state->minfo = memberinfo_register(state->clsinfo, name->text, MEMBER_METHOD);
+        state->minfo->return_type = type;
+        state->m = abc_class_method(state->cls, type2, name->text, 0);
+        // getslot on a member slot only returns "undefined", so no need
+        // to actually store these
+        //state->minfo->slot = state->m->method->trait->slot_id;
+    }
+    if(getset->type == KW_GET) {
+        state->m->method->trait->kind = TRAIT_GETTER;
+    }
+    if(getset->type == KW_SET) {
+        state->m->method->trait->kind = TRAIT_SETTER;
+    }
+    if(params->varargs) {
+        state->m->method->flags |= METHOD_NEED_REST;
+    }
+
+    char opt=0;
+    param_list_t*p=0;
+    for(p=params->list;p;p=p->next) {
+        if(params->varargs && !p->next) {
+            break; //varargs: omit last parameter in function signature
+        }
+        multiname_t*m = sig2mname(p->param->type);
+       list_append(state->m->method->parameters, m);
+        if(p->param->value) {
+            check_constant_against_type(p->param->type, p->param->value);
+            opt=1;list_append(state->m->method->optional_parameters, p->param->value);
+        } else if(opt) {
+            syntaxerror("non-optional parameter not allowed after optional parameters");
+        }
+    }
+
+    /* state->vars is initialized by state_new */
+    if(new_variable("this", state->clsinfo)!=0) syntaxerror("Internal error");
+
+    for(p=params->list;p;p=p->next) {
+        new_variable(p->param->name, p->param->type);
+    }
+}
+static void endfunction(code_t*body)
+{
+    code_t*c = 0;
+    if(state->late_binding) {
+        c = abc_getlocal_0(c);
+        c = abc_pushscope(c);
+    }
+    c = code_append(c, state->initcode);
+    c = code_append(c, body);
+
+    /* append return if necessary */
+    if(!c || c->opcode != OPCODE_RETURNVOID && 
+             c->opcode != OPCODE_RETURNVALUE)
+        c = abc_returnvoid(c);
+    
+    if(state->m->code) syntaxerror("internal error");
+    state->m->code = c;
+    old_state();
+}
+
+
+
 char is_subtype_of(classinfo_t*type, classinfo_t*supertype)
 {
     return 1; // FIXME
@@ -725,7 +826,13 @@ void breakjumpsto(code_t*c, code_t*jump)
 
 classinfo_t*join_types(classinfo_t*type1, classinfo_t*type2, char op)
 {
-    return registry_getanytype(); // FIXME
+    if(!type1 || !type2) 
+        return registry_getanytype();
+    if(TYPE_IS_ANY(type1) || TYPE_IS_ANY(type2))
+        return registry_getanytype();
+    if(type1 == type2)
+        return type1;
+    return registry_getanytype();
 }
 code_t*converttype(code_t*c, classinfo_t*from, classinfo_t*to)
 {
@@ -763,7 +870,12 @@ char is_pushundefined(code_t*c)
     return (c && !c->prev && !c->next && c->opcode == OPCODE_PUSHUNDEFINED);
 }
 
-static code_t* toreadwrite(code_t*in, code_t*middlepart)
+void parserassert(int b)
+{
+    if(!b) syntaxerror("internal error: assertion failed");
+}
+
+static code_t* toreadwrite(code_t*in, code_t*middlepart, char justassign, char readbefore)
 {
     /* converts this:
 
@@ -771,14 +883,15 @@ static code_t* toreadwrite(code_t*in, code_t*middlepart)
 
        to this:
 
-       [prefix code] ([dup]) [read instruction] [setvar] [middlepart] [write instruction] [getvar]
+       [prefix code] ([dup]) [read instruction] [middlepart] [setvar] [write instruction] [getvar]
     */
     
+    if(in && in->opcode == OPCODE_COERCE_A) {
+        in = code_cutlast(in);
+    }
     if(in->next)
         syntaxerror("internal error");
 
-    int temp = gettempvar();
-
     /* chop off read instruction */
     code_t*prefix = in;
     code_t*r = in;
@@ -789,19 +902,27 @@ static code_t* toreadwrite(code_t*in, code_t*middlepart)
         prefix = 0;
     }
 
+    char use_temp_var = readbefore;
+
     /* generate the write instruction, and maybe append a dup to the prefix code */
-    code_t* write = abc_nop(middlepart);
+    code_t* write = abc_nop(0);
     if(r->opcode == OPCODE_GETPROPERTY) {
         write->opcode = OPCODE_SETPROPERTY;
         multiname_t*m = (multiname_t*)r->data[0];
         write->data[0] = multiname_clone(m);
         if(m->type != QNAME)
             syntaxerror("illegal lvalue: can't assign a value to this expression (not a qname)");
-        prefix = abc_dup(prefix); // we need the object, too
+        if(!justassign) {
+            use_temp_var = 1;
+            prefix = abc_dup(prefix); // we need the object, too
+        }
     } else if(r->opcode == OPCODE_GETSLOT) {
         write->opcode = OPCODE_SETSLOT;
         write->data[0] = r->data[0];
-        prefix = abc_dup(prefix); // we need the object, too
+        if(!justassign) {
+            use_temp_var = 1;
+            prefix = abc_dup(prefix); // we need the object, too
+        }
     } else if(r->opcode == OPCODE_GETLOCAL) { 
         write->opcode = OPCODE_SETLOCAL;
         write->data[0] = r->data[0];
@@ -817,14 +938,56 @@ static code_t* toreadwrite(code_t*in, code_t*middlepart)
         code_dump(r, 0, 0, "", stdout);
         syntaxerror("illegal lvalue: can't assign a value to this expression");
     }
-    code_t* c = prefix;
-    c = code_append(c, r);
-
-    c = abc_dup(c);
-    c = abc_setlocal(c, temp);
-    c = code_append(c, middlepart);
-    c = abc_getlocal(c, temp);
-    c = abc_kill(c, temp);
+    code_t* c = 0;
+    
+    int temp = -1;
+    if(!justassign) {
+        if(use_temp_var) {
+            /* with getproperty/getslot, we have to be extra careful not
+               to execute the read code twice, as it might have side-effects
+               (e.g. if the property is in fact a setter/getter combination)
+
+               So read the value, modify it, and write it again,
+               using prefix only once and making sure (by using a temporary
+               register) that the return value is what we just wrote */
+            temp = gettempvar();
+            c = code_append(c, prefix);
+            c = code_append(c, r);
+            if(readbefore) {
+                c = abc_dup(c);
+                c = abc_setlocal(c, temp);
+            }
+            c = code_append(c, middlepart);
+            if(!readbefore) {
+                c = abc_dup(c);
+                c = abc_setlocal(c, temp);
+            }
+            c = code_append(c, write);
+            c = abc_getlocal(c, temp);
+            c = abc_kill(c, temp);
+        } else {
+            /* if we're allowed to execute the read code twice *and*
+               the middlepart doesn't modify the code, things are easier.
+            */
+            code_t* r2 = code_dup(r);
+            //c = code_append(c, prefix);
+            parserassert(!prefix);
+            c = code_append(c, r);
+            c = code_append(c, middlepart);
+            c = code_append(c, write);
+            c = code_append(c, r2);
+        }
+    } else {
+        /* even smaller version: overwrite the value without reading
+           it out first */
+        if(prefix) {
+            c = code_append(c, prefix);
+            c = abc_dup(c);
+        }
+        c = code_append(c, middlepart);
+        c = code_append(c, write);
+        c = code_append(c, r);
+    }
 
     return c;
 }
@@ -847,7 +1010,8 @@ CODE: CODEPIECE {$$=$1;}
 
 CODEPIECE: PACKAGE_DECLARATION   {$$=code_new();/*enters a scope*/}
 CODEPIECE: CLASS_DECLARATION     {$$=code_new();/*enters a scope*/}
-CODEPIECE: INTERFACE_DECLARATION {/*TODO*/$$=code_new();}
+CODEPIECE: FUNCTION_DECLARATION  {$$=code_new();/*enters a scope*/}
+CODEPIECE: INTERFACE_DECLARATION {$$=code_new();}
 CODEPIECE: IMPORT                {$$=code_new();/*adds imports to current scope*/}
 CODEPIECE: ';'                   {$$=code_new();}
 CODEPIECE: VARIABLE_DECLARATION  {$$=$1}
@@ -858,7 +1022,6 @@ CODEPIECE: BREAK                 {$$=$1}
 CODEPIECE: RETURN                {$$=$1}
 CODEPIECE: IF                    {$$=$1}
 CODEPIECE: NAMESPACE_DECLARATION {/*TODO*/$$=code_new();}
-CODEPIECE: FUNCTION_DECLARATION  {/*TODO*/$$=code_new();}
 CODEPIECE: USE_NAMESPACE         {/*TODO*/$$=code_new();}
 
 CODEBLOCK :  '{' MAYBECODE '}' {$$=$2;}
@@ -900,15 +1063,13 @@ ONE_VARIABLE: {} T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION
             $$ = abc_setlocal($$, index);
         }
 
-        /* push default value for type on stack */
-        state->initcode = defaultvalue(state->initcode, $3);
-        state->initcode = abc_setlocal(state->initcode, index);
+        /* if this is a typed variable:
+           push default value for type on stack */
+        if($3) {
+            state->initcode = defaultvalue(state->initcode, $3);
+            state->initcode = abc_setlocal(state->initcode, index);
+        }
     } else {
-        /* only bother to actually set this variable if its syntax is either
-            var x:type;
-           or
-            var x=expr;
-        */
         if($4.c->prev || $4.c->opcode != OPCODE_PUSHUNDEFINED) {
             $$ = $4.c;
             $$ = abc_coerce_a($$);
@@ -1057,6 +1218,8 @@ DECLARATION : ';'
 DECLARATION : SLOT_DECLARATION
 DECLARATION : FUNCTION_DECLARATION
 
+/* ------------ classes and interfaces (body, slots ) ------- */
+
 VARCONST: "var" | "const"
 SLOT_DECLARATION: MODIFIERS VARCONST T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
 
@@ -1085,8 +1248,65 @@ SLOT_DECLARATION: MODIFIERS VARCONST T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
     }
 }
 
+/* ------------ constants -------------------------------------- */
+
+MAYBESTATICCONSTANT: {$$=0;}
+MAYBESTATICCONSTANT: '=' STATICCONSTANT {$$=$2;}
+
+STATICCONSTANT : T_BYTE {$$ = constant_new_int($1);}
+STATICCONSTANT : T_INT {$$ = constant_new_int($1);}
+STATICCONSTANT : T_UINT {$$ = constant_new_uint($1);}
+STATICCONSTANT : T_FLOAT {$$ = constant_new_float($1);}
+STATICCONSTANT : T_STRING {$$ = constant_new_string($1);}
+//STATICCONSTANT : T_NAMESPACE {$$ = constant_new_namespace($1);}
+STATICCONSTANT : KW_TRUE {$$ = constant_new_true($1);}
+STATICCONSTANT : KW_FALSE {$$ = constant_new_false($1);}
+STATICCONSTANT : KW_NULL {$$ = constant_new_null($1);}
+
+/* ------------ classes and interfaces (body, functions) ------- */
+
+// non-vararg version
+MAYBE_PARAM_LIST: {
+    memset(&$$,0,sizeof($$));
+}
+MAYBE_PARAM_LIST: PARAM_LIST {
+    $$=$1;
+}
+
+// vararg version
+MAYBE_PARAM_LIST: "..." PARAM {
+    memset(&$$,0,sizeof($$));
+    $$.varargs=1;
+    list_append($$.list, $2);
+}
+MAYBE_PARAM_LIST: PARAM_LIST ',' "..." PARAM {
+    $$ =$1;
+    $$.varargs=1;
+    list_append($$.list, $4);
+}
+
+// non empty
+PARAM_LIST: PARAM_LIST ',' PARAM {
+    $$ = $1;
+    list_append($$.list, $3);
+}
+PARAM_LIST: PARAM {
+    memset(&$$,0,sizeof($$));
+    list_append($$.list, $1);
+}
+PARAM:  T_IDENTIFIER ':' TYPE MAYBESTATICCONSTANT {
+     $$ = malloc(sizeof(param_t));
+     $$->name=$1->text;
+     $$->type = $3;
+     $$->value = $4;
+}
+PARAM:  T_IDENTIFIER MAYBESTATICCONSTANT {
+     $$ = malloc(sizeof(param_t));
+     $$->name=$1->text;$$->type = TYPE_ANY;
+}
+
 FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
-                      MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' 
+                      MAYBETYPE '{' {startfunction(0,$1,$3,$4,&$6,$8)} MAYBECODE '}' 
 {
     if(!state->m) syntaxerror("internal error: undefined function");
     endfunction($11);
@@ -1184,32 +1404,46 @@ FUNCTIONCALL : E '(' MAYBE_EXPRESSION_LIST ')' {
         l = l->next;
         len ++;
     }
-
+       
     $$.c = $1.c;
+    if($$.c->opcode == OPCODE_COERCE_A) {
+        $$.c = code_cutlast($$.c);
+    }
+
+    $$.t = TYPE_ANY;
+    multiname_t*name = 0;
     if($$.c->opcode == OPCODE_GETPROPERTY) {
-        multiname_t*name = multiname_clone($$.c->data[0]);
+        name = multiname_clone($$.c->data[0]);
         $$.c = code_cutlast($$.c);
         $$.c = code_append($$.c, paramcode);
         $$.c = abc_callproperty2($$.c, name, len);
     } else if($$.c->opcode == OPCODE_GETSLOT) {
         int slot = (int)(ptroff_t)$$.c->data[0];
         trait_t*t = abc_class_find_slotid(state->cls,slot);//FIXME
-        if(t->kind!=TRAIT_METHOD) {syntaxerror("not a function");}
-        
-        abc_method_t*m = t->method;
+        if(t->kind!=TRAIT_METHOD) {
+            //flash allows to assign closures to members.
+            //syntaxerror("not a function");
+        }
+        name = t->name;
         $$.c = code_cutlast($$.c);
         $$.c = code_append($$.c, paramcode);
-
-        //$$.c = abc_callmethod($$.c, m, len); //#1051 illegal early access binding
-        $$.c = abc_callproperty2($$.c, t->name, len);
+        //$$.c = abc_callmethod($$.c, t->method, len); //#1051 illegal early access binding
+        $$.c = abc_callproperty2($$.c, name, len);
     } else {
-        int i = find_variable_safe("this", 0);
-        $$.c = abc_getlocal($$.c, i);
+        $$.c = abc_getlocal_0($$.c);
         $$.c = code_append($$.c, paramcode);
         $$.c = abc_call($$.c, len);
     }
-    /* TODO: look up the functions's return value */
-    $$.t = TYPE_ANY;
+   
+    memberinfo_t*f = 0;
+   
+    if(TYPE_IS_FUNCTION($1.t) &&
+       (f = registry_findmember($1.t, "call"))) {
+        $$.t = f->return_type;
+    } else {
+        $$.c = abc_coerce_a($$.c);
+        $$.t = TYPE_ANY;
+    }
 }
 
 RETURN: "return" %prec prec_none {
@@ -1225,11 +1459,11 @@ NONCOMMAEXPRESSION : E        %prec prec_belowminus {$$=$1;}
 EXPRESSION : E                %prec prec_belowminus {$$ = $1;}
 EXPRESSION : EXPRESSION ',' E %prec prec_belowminus {
     $$.c = $1.c;
-    $$.c = abc_pop($$.c);
+    $$.c = cut_last_push($$.c);
     $$.c = code_append($$.c,$3.c);
     $$.t = $3.t;
 }
-VOIDEXPRESSION : EXPRESSION %prec prec_belowminus {$$=abc_pop($1.c);}
+VOIDEXPRESSION : EXPRESSION %prec prec_belowminus {$$=cut_last_push($1.c);}
 
 // ----------------------- expression evaluation -------------------------------------
 
@@ -1267,41 +1501,30 @@ E : E "||" E {$$.t = join_types($1.t, $3.t, 'O');
               $$.c = converttype($$.c, $1.t, $$.t);
               $$.c = abc_dup($$.c);
               code_t*jmp = $$.c = abc_iftrue($$.c, 0);
-              $$.c = abc_pop($$.c);
+              $$.c = cut_last_push($$.c);
               $$.c = code_append($$.c,$3.c);
-              $$.c = converttype($$.c, $1.t, $$.t);
+              $$.c = converttype($$.c, $3.t, $$.t);
               code_t*label = $$.c = abc_label($$.c);
               jmp->branch = label;
              }
-E : E "&&" E {$$.t = join_types($1.t, $3.t, 'A');
+E : E "&&" E {
+              $$.t = join_types($1.t, $3.t, 'A');
+              /*printf("%08x:\n",$1.t);
+              code_dump($1.c, 0, 0, "", stdout);
+              printf("%08x:\n",$3.t);
+              code_dump($3.c, 0, 0, "", stdout);
+              printf("joining %08x and %08x to %08x\n", $1.t, $3.t, $$.t);*/
               $$.c = $1.c;
               $$.c = converttype($$.c, $1.t, $$.t);
               $$.c = abc_dup($$.c);
               code_t*jmp = $$.c = abc_iffalse($$.c, 0);
-              $$.c = abc_pop($$.c);
+              $$.c = cut_last_push($$.c);
               $$.c = code_append($$.c,$3.c);
-              $$.c = converttype($$.c, $1.t, $$.t);
+              $$.c = converttype($$.c, $3.t, $$.t);
               code_t*label = $$.c = abc_label($$.c);
               jmp->branch = label;              
              }
 
-E : E '.' T_IDENTIFIER
-            {$$.c = $1.c;
-             if($$.t) {
-                 //namespace_t ns = {$$.t->access, (char*)$$.t->package};
-                 namespace_t ns = {$$.t->access, ""};
-                 multiname_t m = {QNAME, &ns, 0, $3->text};
-                 $$.c = abc_getproperty2($$.c, &m);
-                /* FIXME: get type of ($1.t).$3 */
-                 $$.t = registry_getanytype();
-             } else {
-                 namespace_t ns = {ACCESS_PACKAGE, ""};
-                 multiname_t m = {QNAME, &ns, 0, $3->text};
-                 $$.c = abc_getproperty2($$.c, &m);
-                 $$.t = registry_getanytype();
-             }
-            }
-
 E : '!' E    {$$.c=$2.c;
               $$.c = abc_not($$.c);
               $$.t = TYPE_BOOLEAN;
@@ -1324,6 +1547,55 @@ E : E "is" E
 E : '(' E ')' {$$=$2;}
 E : '-' E {$$=$2;}
 
+E : E '[' E ']' {
+  $$.c = $1.c;
+  $$.c = code_append($$.c, $3.c);
+  MULTINAME_LATE(m, $1.t?$1.t->access:ACCESS_PACKAGE, "");
+  $$.c = abc_getproperty2($$.c, &m);
+}
+
+E : E "*=" E { 
+               code_t*c = $3.c;
+               if(TYPE_IS_INT($3.t) || TYPE_IS_UINT($3.t)) {
+                c=abc_multiply_i(c);
+               } else {
+                c=abc_multiply(c);
+               }
+               c=converttype(c, join_types($1.t, $3.t, '*'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
+E : E "%=" E { 
+               code_t*c = abc_modulo($3.c);
+               c=converttype(c, join_types($1.t, $3.t, '%'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
+E : E "<<=" E { 
+               code_t*c = abc_lshift($3.c);
+               c=converttype(c, join_types($1.t, $3.t, '<'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
+E : E ">>=" E { 
+               code_t*c = abc_rshift($3.c);
+               c=converttype(c, join_types($1.t, $3.t, '>'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
+E : E ">>>=" E { 
+               code_t*c = abc_urshift($3.c);
+               c=converttype(c, join_types($1.t, $3.t, 'U'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
+E : E "/=" E { 
+               code_t*c = abc_divide($3.c);
+               c=converttype(c, join_types($1.t, $3.t, '/'), $1.t);
+               $$.c = toreadwrite($1.c, c, 0, 0);
+               $$.t = $1.t;
+              }
 E : E "+=" E { 
                code_t*c = $3.c;
                if(TYPE_IS_INT($3.t) || TYPE_IS_UINT($3.t)) {
@@ -1333,7 +1605,7 @@ E : E "+=" E {
                }
                c=converttype(c, join_types($1.t, $3.t, '+'), $1.t);
                
-               $$.c = toreadwrite($1.c, c);
+               $$.c = toreadwrite($1.c, c, 0, 0);
                $$.t = $1.t;
               }
 E : E "-=" E { code_t*c = $3.c; 
@@ -1344,14 +1616,13 @@ E : E "-=" E { code_t*c = $3.c;
                }
                c=converttype(c, join_types($1.t, $3.t, '-'), $1.t);
                
-               $$.c = toreadwrite($1.c, c);
+               $$.c = toreadwrite($1.c, c, 0, 0);
                $$.t = $1.t;
              }
 E : E '=' E { code_t*c = 0;
-              c = abc_pop(c);
               c = code_append(c, $3.c);
               c = converttype(c, $3.t, $1.t);
-              $$.c = toreadwrite($1.c, c);
+              $$.c = toreadwrite($1.c, c, 1, 0);
               $$.t = $1.t;
             }
 
@@ -1365,39 +1636,107 @@ E : E "++" { code_t*c = 0;
                  type = TYPE_NUMBER;
              }
              c=converttype(c, type, $1.t);
-             $$.c = toreadwrite($1.c, c);
+             $$.c = toreadwrite($1.c, c, 0, 1);
              $$.t = $1.t;
            }
 E : E "--" { code_t*c = 0;
              classinfo_t*type = $1.t;
              if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) {
-                 c=abc_increment_i(c);
+                 c=abc_decrement_i(c);
              } else {
-                 c=abc_increment(c);
+                 c=abc_decrement(c);
                  type = TYPE_NUMBER;
              }
              c=converttype(c, type, $1.t);
-             $$.c = toreadwrite($1.c, c);
+             $$.c = toreadwrite($1.c, c, 0, 1);
              $$.t = $1.t;
             }
 
+E : "++" E { code_t*c = 0;
+             classinfo_t*type = $2.t;
+             if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) {
+                 c=abc_increment_i(c);
+             } else {
+                 c=abc_increment(c);
+                 type = TYPE_NUMBER;
+             }
+             c=converttype(c, type, $2.t);
+             $$.c = toreadwrite($2.c, c, 0, 0);
+             $$.t = $2.t;
+           }
+
+E : "--" E { code_t*c = 0;
+             classinfo_t*type = $2.t;
+             if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) {
+                 c=abc_decrement_i(c);
+             } else {
+                 c=abc_decrement(c);
+                 type = TYPE_NUMBER;
+             }
+             c=converttype(c, type, $2.t);
+             $$.c = toreadwrite($2.c, c, 0, 0);
+             $$.t = $2.t;
+           }
+
+E : E '.' T_IDENTIFIER
+            {$$.c = $1.c;
+             if($$.t) {
+                 memberinfo_t*f = registry_findmember($$.t, $3->text);
+
+                 if(f && f->slot) {
+                     $$.c = abc_getslot($$.c, f->slot);
+                 } else {
+                     namespace_t ns = {$$.t->access, ""}; // needs to be "", not $$.t->package
+                     multiname_t m = {QNAME, &ns, 0, $3->text};
+                     $$.c = abc_getproperty2($$.c, &m);
+                 }
+                 /* determine type */
+                 if(f) {
+                    if(f->kind == MEMBER_METHOD) {
+                        $$.t = TYPE_FUNCTION(f);
+                    } else {
+                        $$.t = f->type;
+                    }
+                 } else {
+                    $$.c = abc_coerce_a($$.c);
+                    $$.t = registry_getanytype();
+                 }
+             } else {
+                 namespace_t ns = {ACCESS_PACKAGE, ""};
+                 multiname_t m = {QNAME, &ns, 0, $3->text};
+                 $$.c = abc_getproperty2($$.c, &m);
+                 $$.c = abc_coerce_a($$.c);
+                 $$.t = registry_getanytype();
+             }
+            }
+
 VAR_READ : T_IDENTIFIER {
     $$.t = 0;
     $$.c = 0;
     int i;
-    memberinfo_t*m;
+    memberinfo_t*f = 0;
     if((i = find_variable($1->text, &$$.t)) >= 0) {
+        // $1 is a local variable
         $$.c = abc_getlocal($$.c, i);
-    } else if((m = registry_findmember(state->clsinfo, $1->text))) {
-        $$.t = m->type;
-        if(m->slot>0) {
+    } else if((f = registry_findmember(state->clsinfo, $1->text))) {
+        // $1 is a function in this class
+        if(f->kind == MEMBER_METHOD) {
+            $$.t = TYPE_FUNCTION(f);
+        } else {
+            $$.t = f->type;
+        }
+        if(f->slot>0) {
             $$.c = abc_getlocal_0($$.c);
-            $$.c = abc_getslot($$.c, m->slot);
+            $$.c = abc_getslot($$.c, f->slot);
         } else {
+            namespace_t ns = {state->clsinfo->access, ""};
+            multiname_t m = {QNAME, &ns, 0, $1->text};
             $$.c = abc_getlocal_0($$.c);
-            $$.c = abc_getproperty($$.c, $1->text);
+            $$.c = abc_getproperty2($$.c, &m);
         }
     } else {
+        // let the avm2 resolve $1 
+        if(strcmp($1->text,"trace"))
         warning("Couldn't resolve %s, doing late binding", $1->text);
         state->late_binding = 1;
 
@@ -1477,15 +1816,6 @@ GETSET : "get" {$$=$1;}
        | "set" {$$=$1;}
        |       {$$=empty_token();}
 
-MAYBE_PARAM_LIST: {$$=list_new();}
-MAYBE_PARAM_LIST: PARAM_LIST {$$=$1;}
-PARAM_LIST: PARAM_LIST ',' PARAM {$$ =$1;         list_append($$, $3);}
-PARAM_LIST: PARAM                {$$ = list_new();list_append($$, $1);}
-PARAM:  T_IDENTIFIER ':' TYPE {$$ = malloc(sizeof(param_t));
-                               $$->name=$1->text;$$->type = $3;}
-PARAM:  T_IDENTIFIER          {$$ = malloc(sizeof(param_t));
-                               $$->name=$1->text;$$->type = TYPE_ANY;}
-
 IDECLARATION : VARIABLE_DECLARATION
 IDECLARATION : FUNCTION_DECLARATION