zero class_signature when allocing
[swftools.git] / lib / as3 / parser.y
index 152a001..72689ee 100644 (file)
@@ -41,8 +41,8 @@
 %union tokenunion {
     tokenptr_t token;
 
-    class_signature_t*class_signature;
-    class_signature_list_t*class_signature_list;
+    classinfo_t*classinfo;
+    classinfo_list_t*classinfo_list;
 
     int number_int;
     unsigned int number_uint;
@@ -52,7 +52,6 @@
     typedcode_list_t*value_list;
     param_t* param;
     param_list_t* param_list;
-    writeable_t writeable;
     char*string;
 }
 
@@ -84,8 +83,9 @@
 %token<token> KW_SET "set"
 %token<token> KW_STATIC
 %token<token> KW_IMPORT "import"
+%token<token> KW_RETURN "return"
 %token<token> KW_INTERFACE "interface"
-%token<token> KW_NULL
+%token<token> KW_NULL "null"
 %token<token> KW_VAR "var"
 %token<token> KW_DYNAMIC
 %token<token> KW_OVERRIDE
 %token<token> T_STAR '*'
 %token<token> T_DOT '.'
 
+%type <token> X_IDENTIFIER VARCONST
 %type <code> CODE
 %type <code> CODEPIECE
 %type <code> CODEBLOCK MAYBECODE
 %type <token> PACKAGE_DECLARATION
 %type <token> FUNCTION_DECLARATION
-%type <code> VARIABLE_DECLARATION
+%type <code> VARIABLE_DECLARATION ONE_VARIABLE VARIABLE_LIST
 %type <token> CLASS_DECLARATION
 %type <token> NAMESPACE_DECLARATION
 %type <token> INTERFACE_DECLARATION
 %type <code> VOIDEXPRESSION
-%type <value> EXPRESSION
+%type <value> EXPRESSION NONCOMMAEXPRESSION
 %type <value> MAYBEEXPRESSION
 %type <value> E
-%type <writeable> LH
 %type <value> CONSTANT
-%type <code> FOR IF WHILE MAYBEELSE BREAK
+%type <code> FOR IF WHILE MAYBEELSE BREAK RETURN
 %type <token> USE_NAMESPACE
-%type <code> ASSIGNMENT FOR_INIT
+%type <code> FOR_INIT
 %type <token> IMPORT
-%type <class_signature> MAYBETYPE
+%type <classinfo> MAYBETYPE
 %type <token> GETSET
 %type <param> PARAM
 %type <param_list> PARAM_LIST
 %type <param_list> MAYBE_PARAM_LIST
 %type <token> MODIFIERS
 %type <token> MODIFIER_LIST
-%type <class_signature_list> IMPLEMENTS_LIST
-%type <class_signature> EXTENDS
-%type <class_signature_list> EXTENDS_LIST
-%type <class_signature> PACKAGEANDCLASS
-%type <class_signature_list> PACKAGEANDCLASS_LIST
-%type <token> MULTILEVELIDENTIFIER
-%type <class_signature> TYPE
+%type <classinfo_list> IMPLEMENTS_LIST
+%type <classinfo> EXTENDS
+%type <classinfo_list> EXTENDS_LIST
+%type <classinfo> CLASS PACKAGEANDCLASS QNAME
+%type <classinfo_list> QNAME_LIST
+%type <classinfo> TYPE
 %type <token> VAR
 //%type <token> VARIABLE
 %type <value> VAR_READ
 %type <value> NEW
-%type <token> X_IDENTIFIER
+//%type <token> T_IDENTIFIER
 %type <token> MODIFIER
 %type <token> PACKAGE
 %type <value> FUNCTIONCALL
 %type <value_list> MAYBE_EXPRESSION_LIST EXPRESSION_LIST MAYBE_PARAM_VALUES
 
-// precendence: from low to high
+// precedence: from low to high
 // http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000012.html
 
 %left prec_none
 %nonassoc '&'
 %nonassoc "!=" "==" "===" "<=" '<' ">=" '>' // TODO: support "a < b < c" syntax?
 %nonassoc "is"
+%left prec_belowminus
 %left '-'
 %left '+'
 %left "<<"
 %nonassoc "else"
 %left '('
 
+// needed for "return" precedence:
+%nonassoc T_STRING T_REGEXP
+%nonassoc T_INT T_UINT T_BYTE T_SHORT T_FLOAT
+%nonassoc "new" "false" "true" "null"
+
+%left prec_highest
+
      
 %{
 
@@ -274,18 +281,23 @@ typedef struct _state {
        a method (like initializing local registers) */
     code_t*initcode;
 
-    abc_method_body_t*m;
-    
     import_list_t*wildcard_imports;
     dict_t*imports;
     char has_own_imports;
    
     /* class data */
-    char*classname;
+    classinfo_t*clsinfo;
     abc_class_t*cls;
+    code_t*cls_init;
+    code_t*cls_static_init;
+    
+    /* method data */
+    memberinfo_t*minfo;
+    abc_method_body_t*m;
 
     array_t*vars;
     int local_var_base;
+    char late_binding;
 } state_t;
 
 static state_t* state = 0;
@@ -393,23 +405,23 @@ static void endpackage()
 }
 
 char*globalclass=0;
-static void startclass(token_t*modifiers, token_t*name, class_signature_t*extends, class_signature_list_t*implements, char interface)
+static void startclass(token_t*modifiers, token_t*name, classinfo_t*extends, classinfo_list_t*implements, char interface)
 {
     if(state->cls) {
         syntaxerror("inner classes now allowed"); 
     }
     new_state();
-    state->classname = name->text;
+    char*classname = name->text;
 
     token_list_t*t=0;
-    class_signature_list_t*mlist=0;
+    classinfo_list_t*mlist=0;
     /*printf("entering class %s\n", name->text);
     printf("  modifiers: ");for(t=modifiers->tokens;t;t=t->next) printf("%s ", t->token->text);printf("\n");
     if(extends) 
         printf("  extends: %s.%s\n", extends->package, extends->name);
     printf("  implements (%d): ", list_length(implements));
     for(mlist=implements;mlist;mlist=mlist->next)  {
-        printf("%s ", mlist->class_signature?mlist->class_signature->name:0);
+        printf("%s ", mlist->classinfo?mlist->classinfo->name:0);
     }
     printf("\n");
     */
@@ -445,23 +457,28 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
         syntaxerror("public classes only allowed inside a package");
     }
 
-    if(registry_findclass(package, state->classname)) {
-        syntaxerror("Package \"%s\" already contains a class called \"%s\"", package, state->classname);
+    if(registry_findclass(package, classname)) {
+        syntaxerror("Package \"%s\" already contains a class called \"%s\"", package, classname);
     }
     
-    class_signature_t* classname = class_signature_register(access, package, state->classname);
+    state->clsinfo = classinfo_register(access, package, classname);
 
-    MULTINAME(classname2,classname);
+    MULTINAME(classname2,state->clsinfo);
     
     multiname_t*extends2 = sig2mname(extends);
 
+    /*if(extends) {
+        state->cls_init = abc_getlocal_0(state->cls_init);
+        state->cls_init = abc_constructsuper(state->cls_init, 0);
+    }*/
+
     state->cls = abc_class_new(state->file, &classname2, extends2);
     if(final) abc_class_final(state->cls);
     if(sealed) abc_class_sealed(state->cls);
     if(interface) abc_class_interface(state->cls);
 
     for(mlist=implements;mlist;mlist=mlist->next) {
-        MULTINAME(m, mlist->class_signature);
+        MULTINAME(m, mlist->classinfo);
         abc_class_add_interface(state->cls, &m);
     }
 
@@ -470,7 +487,7 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
 
     abc_method_body_t*m = state->init->method->body;
     __ getglobalscope(m);
-    class_signature_t*s = extends;
+    classinfo_t*s = extends;
 
     int count=0;
     
@@ -485,9 +502,8 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
         __ getlex2(m, s2);
         multiname_destroy(s2);
 
-        __ pushscope(m);
+        __ pushscope(m); count++;
         m->code = m->code->prev->prev; // invert
-        count++;
     }
     /* continue appending after last op end */
     while(m->code && m->code->next) m->code = m->code->next; 
@@ -498,9 +514,15 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
     if(extends2) {
         __ getlex2(m, extends2);
         __ dup(m);
-        __ pushscope(m); // we get a Verify Error #1107 if this is not the top scope
+        /* notice: we get a Verify Error #1107 if the top elemnt on the scope
+           stack is not the superclass */
+        __ pushscope(m);count++;
     } else {
         __ pushnull(m);
+        /* notice: we get a verify error #1107 if the top element on the scope 
+           stack is not the global object */
+        __ getlocal_0(m);
+        __ pushscope(m);count++;
     }
     __ newclass(m,state->cls);
     while(count--) {
@@ -509,11 +531,11 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
     __ setslot(m, slotindex);
 
     /* flash.display.MovieClip handling */
-    if(!globalclass && public && class_signature_equals(registry_getMovieClip(),extends)) {
+    if(!globalclass && public && classinfo_equals(registry_getMovieClip(),extends)) {
         if(state->package && state->package[0]) {
-            globalclass = concat3str(state->package, ".", state->classname);
+            globalclass = concat3str(state->package, ".", classname);
         } else {
-            globalclass = strdup(state->classname);
+            globalclass = strdup(classname);
         }
     }
     multiname_destroy(extends2);
@@ -521,37 +543,56 @@ static void startclass(token_t*modifiers, token_t*name, class_signature_t*extend
 
 static void endclass()
 {
-    /*printf("leaving class %s\n", state->classname);*/
+    if(state->cls_init) {
+        if(!state->cls->constructor) {
+            abc_method_body_t*m = abc_class_constructor(state->cls, 0, 0);
+            m->code = code_append(m->code, state->cls_init);
+            m->code = abc_returnvoid(m->code);
+        } else {
+            code_t*c = state->cls->constructor->body->code;
+            c = code_append(state->cls_init, c);
+            state->cls->constructor->body->code = c;
+
+        }
+    }
+    if(state->cls_static_init) {
+        if(!state->cls->static_constructor) {
+            abc_method_body_t*m = abc_class_staticconstructor(state->cls, 0, 0);
+            m->code = state->cls_static_init;
+        } else {
+            state->cls->static_constructor->body->code = 
+                code_append(state->cls_static_init, state->cls->static_constructor->body->code);
+        }
+    }
+
     old_state();
 }
 static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
-                          param_list_t*params, class_signature_t*type)
+                          param_list_t*params, classinfo_t*type)
 {
     token_list_t*t;
     new_state();
     state->function = name->text;
     
-    /*printf("entering function %s\n", name->text);
-    if(ns)
-        printf("  namespace: %s\n", ns->text);
-    printf("  getset: %s\n", getset->text);
-    printf("  params: ");for(t=params->tokens;t;t=t->next) printf("%s ", t->token->text);printf("\n");
-    printf("  mod: ");for(t=mod->tokens;t;t=t->next) printf("%s ", t->token->text);printf("\n");
-    if(type)
-        printf("  type: %s.%s\n", type->package, type->name);
-    print_imports();*/
-    
     if(state->m) {
         syntaxerror("not able to start another method scope");
     }
 
     multiname_t*type2 = sig2mname(type);
-
-    if(!strcmp(state->classname,name->text)) {
+    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);
@@ -563,17 +604,20 @@ static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
     for(p=params;p;p=p->next) {
         array_append(state->vars, p->param->name, 0);
     }
-
-    __ getlocal_0(state->m);
-    __ pushscope(state->m);
-
-    multiname_destroy(type2);
 }
-static void endfunction()
+static void endfunction(code_t*body)
 {
-    /*printf("leaving function %s\n", state->function);*/
-    __ returnvoid(state->m);
+    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();
 }
 
@@ -606,7 +650,7 @@ void extend_s(token_t*list, char*seperator, token_t*add) {
     list->text[l1+l2+l3]=0;
 }
 
-static int find_variable(char*name, class_signature_t**m)
+static int find_variable(char*name, classinfo_t**m)
 {
     state_list_t* s = state_stack;
     while(s) {
@@ -619,21 +663,39 @@ static int find_variable(char*name, class_signature_t**m)
         }
         s = s->next;
     }
-    syntaxerror("undefined variable: %s", name);
+    return -1;
 } 
+static int find_variable_safe(char*name, classinfo_t**m)
+{
+    int i = find_variable(name, m);
+    if(i<0)
+        syntaxerror("undefined variable: %s", name);
+    return i;
+}
 static char variable_exists(char*name) 
 {
     return array_contains(state->vars, name);
 }
-static int new_variable(char*name, class_signature_t*type)
+static int new_variable(char*name, classinfo_t*type)
 {
     return array_append(state->vars, name, type) + state->local_var_base;
 }
+#define TEMPVARNAME "__as3_temp__"
+static int gettempvar()
+{
+    int i = find_variable(TEMPVARNAME, 0);
+    if(i<0) {
+        return new_variable(TEMPVARNAME, 0);
+    } else {
+        return i;
+    }
+}
+
 code_t* killvars(code_t*c) 
 {
     int t;
     for(t=0;t<state->vars->num;t++) {
-        class_signature_t*type = array_getvalue(state->vars, 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)) {
@@ -643,11 +705,7 @@ code_t* killvars(code_t*c)
     return c;
 }
 
-class_signature_t*join_types(class_signature_t*type1, class_signature_t*type2, char op)
-{
-    return registry_getanytype(); // FIXME
-}
-char is_subtype_of(class_signature_t*type, class_signature_t*supertype)
+char is_subtype_of(classinfo_t*type, classinfo_t*supertype)
 {
     return 1; // FIXME
 }
@@ -664,8 +722,15 @@ void breakjumpsto(code_t*c, code_t*jump)
         c = c->next;
     }
 }
-code_t*converttype(code_t*c, class_signature_t*from, class_signature_t*to)
+
+classinfo_t*join_types(classinfo_t*type1, classinfo_t*type2, char op)
 {
+    return registry_getanytype(); // FIXME
+}
+code_t*converttype(code_t*c, classinfo_t*from, classinfo_t*to)
+{
+    if(from==to)
+        return c;
     if(!to) {
         /*TODO: can omit this if from is zero? */
         return abc_coerce_a(c);
@@ -681,7 +746,7 @@ code_t*converttype(code_t*c, class_signature_t*from, class_signature_t*to)
     return c;
 }
 
-code_t*defaultvalue(code_t*c, class_signature_t*type)
+code_t*defaultvalue(code_t*c, classinfo_t*type)
 {
     if(TYPE_IS_INT(type) || TYPE_IS_UINT(type) || TYPE_IS_FLOAT(type)) {
        c = abc_pushbyte(c, 0);
@@ -693,6 +758,78 @@ code_t*defaultvalue(code_t*c, class_signature_t*type)
     return c;
 }
 
+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)
+{
+    /* converts this:
+
+       [prefix code] [read instruction]
+
+       to this:
+
+       [prefix code] ([dup]) [read instruction] [setvar] [middlepart] [write instruction] [getvar]
+    */
+    
+    if(in->next)
+        syntaxerror("internal error");
+
+    int temp = gettempvar();
+
+    /* chop off read instruction */
+    code_t*prefix = in;
+    code_t*r = in;
+    if(r->prev) {
+        prefix = r->prev;r->prev = 0;
+        prefix->next=0;
+    } else {
+        prefix = 0;
+    }
+
+    /* generate the write instruction, and maybe append a dup to the prefix code */
+    code_t* write = abc_nop(middlepart);
+    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
+    } 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
+    } else if(r->opcode == OPCODE_GETLOCAL) { 
+        write->opcode = OPCODE_SETLOCAL;
+        write->data[0] = r->data[0];
+    } else if(r->opcode == OPCODE_GETLOCAL_0) { 
+        write->opcode = OPCODE_SETLOCAL_0;
+    } else if(r->opcode == OPCODE_GETLOCAL_1) { 
+        write->opcode = OPCODE_SETLOCAL_1;
+    } else if(r->opcode == OPCODE_GETLOCAL_2) { 
+        write->opcode = OPCODE_SETLOCAL_2;
+    } else if(r->opcode == OPCODE_GETLOCAL_3) { 
+        write->opcode = OPCODE_SETLOCAL_3;
+    } else {
+        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);
+
+    return c;
+}
+
+
 %}
 
 
@@ -718,8 +855,8 @@ CODEPIECE: VOIDEXPRESSION        {$$=$1}
 CODEPIECE: FOR                   {$$=$1}
 CODEPIECE: WHILE                 {$$=$1}
 CODEPIECE: BREAK                 {$$=$1}
+CODEPIECE: RETURN                {$$=$1}
 CODEPIECE: IF                    {$$=$1}
-CODEPIECE: ASSIGNMENT            {$$=$1}
 CODEPIECE: NAMESPACE_DECLARATION {/*TODO*/$$=code_new();}
 CODEPIECE: FUNCTION_DECLARATION  {/*TODO*/$$=code_new();}
 CODEPIECE: USE_NAMESPACE         {/*TODO*/$$=code_new();}
@@ -728,27 +865,21 @@ CODEBLOCK :  '{' MAYBECODE '}' {$$=$2;}
 CODEBLOCK :  CODEPIECE ';'             {$$=$1;}
 CODEBLOCK :  CODEPIECE %prec below_semicolon {$$=$1;}
 
-/* ------------ functions --------------------------- */
-
-FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
-                      MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' {
-    if(!state->m) syntaxerror("internal error: undefined function");
-    state->initcode = abc_nop(state->initcode);
-    state->initcode = abc_nop(state->initcode);
-    state->initcode = abc_nop(state->initcode);
-    state->m->code = code_append(state->initcode, $11);state->initcode=0;
-    endfunction()
-}
-
 /* ------------ variables --------------------------- */
 
-MAYBEEXPRESSION : '=' EXPRESSION {$$=$2;}
+MAYBEEXPRESSION : '=' NONCOMMAEXPRESSION {$$=$2;}
                 |                {$$.c=abc_pushundefined(0);
                                   $$.t=TYPE_ANY;
                                  }
 
 VAR : "const" | "var"
-VARIABLE_DECLARATION : VAR T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
+VARIABLE_DECLARATION : VAR VARIABLE_LIST {$$=$2;}
+
+VARIABLE_LIST: ONE_VARIABLE                   {$$ = $1;}
+VARIABLE_LIST: VARIABLE_LIST ',' ONE_VARIABLE {$$ = code_append($1, $3);}
+
+ONE_VARIABLE: {} T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION
+{
     if(variable_exists($2->text))
         syntaxerror("Variable %s already defined", $2->text);
    
@@ -794,18 +925,6 @@ VARIABLE_DECLARATION : VAR T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
     }*/
     printf("variable %s -> %d (%s)\n", $2->text, index, $4.t?$4.t->name:"");
 }
-ASSIGNMENT :           T_IDENTIFIER '=' EXPRESSION {
-    class_signature_t*type=0;
-    int i = find_variable($1->text, &type);
-    $$ = $3.c;
-    if(!type && $3.t) {
-        // convert to "any" type, the register is untyped
-        $$ = abc_coerce_a($$);
-    } else {
-        // TODO: convert ints to strings etc.
-    }
-    $$ = abc_setlocal($$, i);
-}
 
 /* ------------ control flow ------------------------- */
 
@@ -833,7 +952,8 @@ IF  : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE {
 }
 
 FOR_INIT : {$$=code_new();}
-FOR_INIT : ASSIGNMENT | VARIABLE_DECLARATION | VOIDEXPRESSION
+FOR_INIT : VARIABLE_DECLARATION
+FOR_INIT : VOIDEXPRESSION
 
 FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK {
     $$ = state->initcode;state->initcode=0;
@@ -873,18 +993,21 @@ BREAK : "break" {
 
 /* ------------ packages and imports ---------------- */
 
-PACKAGE_DECLARATION : "package" MULTILEVELIDENTIFIER '{' {startpackage($2)} MAYBECODE '}' {endpackage()}
-PACKAGE_DECLARATION : "package" '{' {startpackage(0)} MAYBECODE '}' {endpackage()}
+X_IDENTIFIER: T_IDENTIFIER
+            | "package"
 
 PACKAGE: PACKAGE '.' X_IDENTIFIER {$$ = concat3($1,$2,$3);}
 PACKAGE: X_IDENTIFIER             {$$=$1;}
 
-IMPORT : "import" PACKAGE '.' X_IDENTIFIER {
-       class_signature_t*c = registry_findclass($2->text, $4->text);
+PACKAGE_DECLARATION : "package" PACKAGE '{' {startpackage($2)} MAYBECODE '}' {endpackage()}
+PACKAGE_DECLARATION : "package" '{' {startpackage(0)} MAYBECODE '}' {endpackage()}
+
+IMPORT : "import" QNAME {
+       classinfo_t*c = $2;
        if(!c) 
-            syntaxerror("Couldn't import %s.%s\n", $2->text, $4->text);
+            syntaxerror("Couldn't import class\n");
        state_has_imports();
-       dict_put(state->imports, $4->text, c);
+       dict_put(state->imports, c->name, c);
        $$=0;
 }
 IMPORT : "import" PACKAGE '.' '*' {
@@ -895,7 +1018,7 @@ IMPORT : "import" PACKAGE '.' '*' {
        $$=0;
 }
 
-/* ------------ classes and interfaces -------------- */
+/* ------------ classes and interfaces (header) -------------- */
 
 MODIFIERS : {$$=empty_token();}
 MODIFIERS : MODIFIER_LIST {$$=$1}
@@ -904,82 +1027,211 @@ MODIFIER_LIST : MODIFIER               {$$=empty_token();extend($$,$1);}
 MODIFIER : KW_PUBLIC | KW_PRIVATE | KW_PROTECTED | KW_STATIC | KW_DYNAMIC | KW_FINAL | KW_OVERRIDE | KW_NATIVE | KW_INTERNAL
 
 EXTENDS : {$$=registry_getobjectclass();}
-EXTENDS : KW_EXTENDS PACKAGEANDCLASS {$$=$2;}
+EXTENDS : KW_EXTENDS QNAME {$$=$2;}
 
 EXTENDS_LIST : {$$=list_new();}
-EXTENDS_LIST : KW_EXTENDS PACKAGEANDCLASS_LIST {$$=$2;}
+EXTENDS_LIST : KW_EXTENDS QNAME_LIST {$$=$2;}
 
 IMPLEMENTS_LIST : {$$=list_new();}
-IMPLEMENTS_LIST : KW_IMPLEMENTS PACKAGEANDCLASS_LIST {$$=$2;}
+IMPLEMENTS_LIST : KW_IMPLEMENTS QNAME_LIST {$$=$2;}
 
 CLASS_DECLARATION : MODIFIERS "class" T_IDENTIFIER 
                               EXTENDS IMPLEMENTS_LIST 
                               '{' {startclass($1,$3,$4,$5, 0);} 
                               MAYBE_DECLARATION_LIST 
                               '}' {endclass();}
+
 INTERFACE_DECLARATION : MODIFIERS "interface" T_IDENTIFIER 
                               EXTENDS_LIST 
                               '{' {startclass($1,$3,0,$4,1);}
                               MAYBE_IDECLARATION_LIST 
                               '}' {endclass();}
 
-TYPE : PACKAGEANDCLASS {$$=$1;}
-     | '*'        {$$=registry_getanytype();}
-     |  "String"  {$$=registry_getstringclass();}
-     |  "int"     {$$=registry_getintclass();}
-     |  "uint"    {$$=registry_getuintclass();}
-     |  "Boolean" {$$=registry_getbooleanclass();}
-     |  "Number"  {$$=registry_getnumberclass();}
+/* ------------ classes and interfaces (body) -------------- */
 
-MAYBETYPE: ':' TYPE {$$=$2;}
-MAYBETYPE:          {$$=0;}
+MAYBE_DECLARATION_LIST : 
+MAYBE_DECLARATION_LIST : DECLARATION_LIST
+DECLARATION_LIST : DECLARATION
+DECLARATION_LIST : DECLARATION_LIST DECLARATION
+DECLARATION : ';'
+DECLARATION : SLOT_DECLARATION
+DECLARATION : FUNCTION_DECLARATION
 
-//FUNCTION_HEADER:      NAMESPACE MODIFIERS T_FUNCTION GETSET T_IDENTIFIER '(' PARAMS ')' 
-FUNCTION_HEADER:      MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
-                      MAYBETYPE
+VARCONST: "var" | "const"
+SLOT_DECLARATION: MODIFIERS VARCONST T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
 
-NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER
-NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_IDENTIFIER
-NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_STRING
+    memberinfo_t* info = memberinfo_register(state->clsinfo, $3->text, MEMBER_SLOT);
+    info->type = $4;
 
-//NAMESPACE :              {$$=empty_token();}
-//NAMESPACE : T_IDENTIFIER {$$=$1};
+    trait_t*t=0;
+    if($4) {
+        MULTINAME(m, $4);
+        t=abc_class_slot(state->cls, $3->text, &m);
+    } else {
+        t=abc_class_slot(state->cls, $3->text, 0);
+    }
+    if($2->type==KW_CONST) {
+        t->kind= TRAIT_CONST;
+    }
+    info->slot = t->slot_id;
+    if($5.c && !is_pushundefined($5.c)) {
+        code_t*c = 0;
+        c = abc_getlocal_0(c);
+        c = code_append(c, $5.c);
+        c = converttype(c, $5.t, $4);
+        c = abc_setslot(c, t->slot_id);
+        //c = abc_setproperty(c, $3->text); 
+        state->cls_init = code_append(state->cls_init, c);
+    }
+}
 
-CONSTANT : T_BYTE {$$.c = abc_pushbyte(0, $1);
-                   //MULTINAME(m, registry_getintclass());
-                   //$$.c = abc_coerce2($$.c, &m); // FIXME
-                   $$.t = TYPE_INT;
-                  }
-CONSTANT : T_SHORT {$$.c = abc_pushshort(0, $1);
-                    $$.t = TYPE_INT;
-                   }
-CONSTANT : T_INT {$$.c = abc_pushint(0, $1);
-                  $$.t = TYPE_INT;
-                 }
-CONSTANT : T_UINT {$$.c = abc_pushuint(0, $1);
-                   $$.t = TYPE_UINT;
-                  }
-CONSTANT : T_FLOAT {$$.c = abc_pushdouble(0, $1);
-                    $$.t = TYPE_FLOAT;
-                   }
-CONSTANT : T_STRING {$$.c = abc_pushstring(0, $1);
-                     $$.t = TYPE_STRING;
-                    }
-CONSTANT : KW_TRUE {$$.c = abc_pushtrue(0);
-                    $$.t = TYPE_BOOLEAN;
-                   }
-CONSTANT : KW_FALSE {$$.c = abc_pushfalse(0);
-                     $$.t = TYPE_BOOLEAN;
-                    }
-CONSTANT : KW_NULL {$$.c = abc_pushnull(0);
-                    $$.t = TYPE_NULL;
-                   }
+FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
+                      MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' 
+{
+    if(!state->m) syntaxerror("internal error: undefined function");
+    endfunction($11);
+}
 
-USE_NAMESPACE : "use" "namespace" T_IDENTIFIER
+/* ------------- package + class ids --------------- */
+
+CLASS: T_IDENTIFIER {
+
+    /* try current package */
+    $$ = registry_findclass(state->package, $1->text);
+
+    /* try explicit imports */
+    dictentry_t* e = dict_get_slot(state->imports, $1->text);
+    while(e) {
+        if($$)
+            break;
+        if(!strcmp(e->key, $1->text)) {
+            $$ = (classinfo_t*)e->data;
+        }
+        e = e->next;
+    }
+
+    /* try package.* imports */
+    import_list_t*l = state->wildcard_imports;
+    while(l) {
+        if($$)
+            break;
+        //printf("does package %s contain a class %s?\n", l->import->package, $1->text);
+        $$ = registry_findclass(l->import->package, $1->text);
+        l = l->next;
+    }
+
+    /* try global package */
+    if(!$$) {
+        $$ = registry_findclass("", $1->text);
+    }
+
+    if(!$$) syntaxerror("Could not find class %s\n", $1->text);
+}
+
+PACKAGEANDCLASS : PACKAGE '.' T_IDENTIFIER {
+    $$ = registry_findclass($1->text, $3->text);
+    if(!$$) syntaxerror("Couldn't find class %s.%s\n", $1->text, $3->text);
+}
+
+QNAME: PACKAGEANDCLASS
+     | CLASS
+
+
+/* ----------function calls, constructor calls ------ */
+
+MAYBE_PARAM_VALUES :  %prec prec_none {$$=0;}
+MAYBE_PARAM_VALUES : '(' MAYBE_EXPRESSION_LIST ')' {$$=$2}
+
+MAYBE_EXPRESSION_LIST : {$$=0;}
+MAYBE_EXPRESSION_LIST : EXPRESSION_LIST
+EXPRESSION_LIST : NONCOMMAEXPRESSION             {$$=list_new();
+                                                  typedcode_t*t = malloc(sizeof(typedcode_t));
+                                                  *t = $1;
+                                                  list_append($$, t);}
+EXPRESSION_LIST : EXPRESSION_LIST ',' NONCOMMAEXPRESSION {$$=$1;
+                                                  typedcode_t*t = malloc(sizeof(typedcode_t));
+                                                  *t = $3;
+                                                  list_append($$, t);}
+
+NEW : "new" CLASS MAYBE_PARAM_VALUES {
+    MULTINAME(m, $2);
+    $$.c = code_new();
+
+    /* TODO: why do we have to *find* our own classes? */
+    $$.c = abc_findpropstrict2($$.c, &m);
+
+    typedcode_list_t*l = $3;
+    int len = 0;
+    while(l) {
+        $$.c = code_append($$.c, l->typedcode->c); // push parameters on stack
+        l = l->next;
+        len ++;
+    }
+    $$.c = abc_constructprop2($$.c, &m, len);
+    $$.t = $2;
+}
+
+/* TODO: use abc_call (for calling local variables),
+         abc_callstatic (for calling own methods) 
+         call (for closures)
+*/
+FUNCTIONCALL : E '(' MAYBE_EXPRESSION_LIST ')' {
+    typedcode_list_t*l = $3;
+    int len = 0;
+    code_t*paramcode = 0;
+    while(l) {
+        paramcode = code_append(paramcode, l->typedcode->c); // push parameters on stack
+        l = l->next;
+        len ++;
+    }
+
+    $$.c = $1.c;
+    if($$.c->opcode == OPCODE_GETPROPERTY) {
+        multiname_t*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;
+        $$.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);
+    } else {
+        int i = find_variable_safe("this", 0);
+        $$.c = abc_getlocal($$.c, i);
+        $$.c = code_append($$.c, paramcode);
+        $$.c = abc_call($$.c, len);
+    }
+    /* TODO: look up the functions's return value */
+    $$.t = TYPE_ANY;
+}
 
+RETURN: "return" %prec prec_none {
+    $$ = abc_returnvoid(0);
+}
+RETURN: "return" EXPRESSION {
+    $$ = $2.c;
+    $$ = abc_returnvalue($$);
+}
+// ----------------------- expression types -------------------------------------
+
+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 = code_append($$.c,$3.c);
+    $$.t = $3.t;
+}
+VOIDEXPRESSION : EXPRESSION %prec prec_belowminus {$$=abc_pop($1.c);}
 
-EXPRESSION : E %prec prec_none  /*precendence below '-x'*/ {$$ = $1;}
-VOIDEXPRESSION : E %prec prec_none {$$=$1.c;/*calculate and discard*/$$=abc_pop($$);}
+// ----------------------- expression evaluation -------------------------------------
 
 E : CONSTANT
 E : VAR_READ %prec T_IDENTIFIER {$$ = $1;}
@@ -1010,24 +1262,45 @@ E : E "!=" E {$$.c = code_append($1.c,$3.c);$$.c = abc_equals($$.c);$$.c = abc_n
               $$.t = TYPE_BOOLEAN;
              }
 
-E : E "||" E {$$.c = $1.c;
-              $$.c=abc_dup($$.c);
+E : E "||" E {$$.t = join_types($1.t, $3.t, 'O');
+              $$.c = $1.c;
+              $$.c = converttype($$.c, $1.t, $$.t);
+              $$.c = abc_dup($$.c);
               code_t*jmp = $$.c = abc_iftrue($$.c, 0);
-              $$.c=abc_pop($$.c);
+              $$.c = abc_pop($$.c);
               $$.c = code_append($$.c,$3.c);
+              $$.c = converttype($$.c, $1.t, $$.t);
               code_t*label = $$.c = abc_label($$.c);
               jmp->branch = label;
-              $$.t = join_types($1.t, $3.t, 'O');
              }
-E : E "&&" E {$$.c = $1.c;
-              $$.c=abc_dup($$.c);
+E : E "&&" E {$$.t = join_types($1.t, $3.t, 'A');
+              $$.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 = abc_pop($$.c);
               $$.c = code_append($$.c,$3.c);
+              $$.c = converttype($$.c, $1.t, $$.t);
               code_t*label = $$.c = abc_label($$.c);
-              jmp->branch = label;
-              $$.t = join_types($1.t, $3.t, 'A');
+              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);
@@ -1046,96 +1319,152 @@ E : E '*' E {$$.c = code_append($1.c,$3.c);$$.c = abc_multiply($$.c);$$.c=abc_co
              $$.t = join_types($1.t, $3.t, '*');
             }
 
-E : E "as" TYPE
-E : E "is" TYPE
+E : E "as" E
+E : E "is" E
 E : '(' E ')' {$$=$2;}
 E : '-' E {$$=$2;}
 
-E : LH "+=" E {$$.c = $1.read;$$.c=code_append($$.c,$3.c);$$.c=abc_add($$.c);
-               class_signature_t*type = join_types($1.type, $3.t, '+');
-               $$.c=converttype($$.c, type, $1.type);
-               $$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
-               $$.t = $1.type;
-              }
-E : LH "-=" E {$$.c = $1.read;$$.c=code_append($$.c,$3.c);$$.c=abc_add($$.c);
-               class_signature_t*type = join_types($1.type, $3.t, '-');
-               $$.c=converttype($$.c, type, $1.type);
-               $$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
-               $$.t = $1.type;
+E : E "+=" E { 
+               code_t*c = $3.c;
+               if(TYPE_IS_INT($3.t) || TYPE_IS_UINT($3.t)) {
+                c=abc_add_i(c);
+               } else {
+                c=abc_add(c);
+               }
+               c=converttype(c, join_types($1.t, $3.t, '+'), $1.t);
+               
+               $$.c = toreadwrite($1.c, c);
+               $$.t = $1.t;
               }
+E : E "-=" E { code_t*c = $3.c; 
+               if(TYPE_IS_INT($3.t) || TYPE_IS_UINT($3.t)) {
+                c=abc_subtract_i(c);
+               } else {
+                c=abc_subtract(c);
+               }
+               c=converttype(c, join_types($1.t, $3.t, '-'), $1.t);
+               
+               $$.c = toreadwrite($1.c, c);
+               $$.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);
+              $$.t = $1.t;
+            }
 
 // TODO: use inclocal where appropriate
-E : LH "++" {$$.c = $1.read;$$.c=abc_increment($$.c);
-             class_signature_t*type = $1.type;
-             if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) type = TYPE_NUMBER;
-             $$.c=converttype($$.c, type, $1.type);
-             $$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
-             $$.t = $1.type;
-            }
-E : LH "--" {$$.c = $1.read;$$.c=abc_decrement($$.c);
-             class_signature_t*type = $1.type;
-             if(TYPE_IS_INT(type) || TYPE_IS_UINT(type)) type = TYPE_NUMBER;
-             $$.c=converttype($$.c, 0, $1.type);
-             $$.c=abc_dup($$.c);$$.c=code_append($$.c,$1.write);
-             $$.t = $1.type;
+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);
+             } else {
+                 c=abc_increment(c);
+                 type = TYPE_NUMBER;
+             }
+             c=converttype(c, type, $1.t);
+             $$.c = toreadwrite($1.c, c);
+             $$.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);
+             } else {
+                 c=abc_increment(c);
+                 type = TYPE_NUMBER;
+             }
+             c=converttype(c, type, $1.t);
+             $$.c = toreadwrite($1.c, c);
+             $$.t = $1.t;
             }
 
-LH: T_IDENTIFIER {
-  int i = find_variable($1->text, &$$.type);
-  $$.read = abc_getlocal(0, i);
-  $$.write = abc_setlocal(0, i);
-}
-
-MAYBE_PARAM_VALUES :  %prec prec_none {$$=0;}
-MAYBE_PARAM_VALUES : '(' MAYBE_EXPRESSION_LIST ')' {$$=$2}
+VAR_READ : T_IDENTIFIER {
+    $$.t = 0;
+    $$.c = 0;
+    int i;
+    memberinfo_t*m;
+    if((i = find_variable($1->text, &$$.t)) >= 0) {
+        $$.c = abc_getlocal($$.c, i);
+    } else if((m = registry_findmember(state->clsinfo, $1->text))) {
+        $$.t = m->type;
+        if(m->slot>0) {
+            $$.c = abc_getlocal_0($$.c);
+            $$.c = abc_getslot($$.c, m->slot);
+        } else {
+            $$.c = abc_getlocal_0($$.c);
+            $$.c = abc_getproperty($$.c, $1->text);
+        }
+    } else {
+        warning("Couldn't resolve %s, doing late binding", $1->text);
+        state->late_binding = 1;
 
-NEW : "new" PACKAGEANDCLASS MAYBE_PARAM_VALUES {
-    MULTINAME(m, $2);
-    $$.c = code_new();
-    $$.c = abc_findpropstrict2($$.c, &m);
-    typedcode_list_t*l = $3;
-    int len = 0;
-    while(l) {
-        $$.c = code_append($$.c, l->typedcode->c); // push parameters on stack
-        l = l->next;
-        len ++;
+        $$.t = 0;
+        $$.c = abc_findpropstrict($$.c, $1->text);
+        $$.c = abc_getproperty($$.c, $1->text);
     }
-    $$.c = abc_constructprop2($$.c, &m, len);
-    $$.t = $2;
 }
 
-FUNCTIONCALL : T_IDENTIFIER '(' MAYBE_EXPRESSION_LIST ')' {
-    /* TODO: use abc_call (for calling local variables),
-             abc_callstatic (for calling own methods) */
-    $$.c = code_new();
-    $$.c = abc_findpropstrict($$.c, $1->text);
-    typedcode_list_t*l = $3;
-    int len = 0;
-    while(l) {
-        $$.c = code_append($$.c, l->typedcode->c); // push parameters on stack
-        l = l->next;
-        len ++;
-    }
-    $$.c = abc_callproperty($$.c, $1->text, len);
-    /* TODO: look up the functions's return value */
-    $$.t = TYPE_ANY;
-}
 
-MAYBE_EXPRESSION_LIST : {$$=0;}
-MAYBE_EXPRESSION_LIST : EXPRESSION_LIST
-EXPRESSION_LIST : EXPRESSION                     {$$=list_new();
-                                                  typedcode_t*t = malloc(sizeof(typedcode_t));
-                                                  *t = $1;
-                                                  list_append($$, t);}
-EXPRESSION_LIST : EXPRESSION_LIST ',' EXPRESSION {$$=$1;
-                                                  typedcode_t*t = malloc(sizeof(typedcode_t));
-                                                  *t = $3;
-                                                  list_append($$, t);}
+// ------------------------------------------------------------------------------
+
+
+TYPE : QNAME {$$=$1;}
+     | '*'        {$$=registry_getanytype();}
+     |  "String"  {$$=registry_getstringclass();}
+     |  "int"     {$$=registry_getintclass();}
+     |  "uint"    {$$=registry_getuintclass();}
+     |  "Boolean" {$$=registry_getbooleanclass();}
+     |  "Number"  {$$=registry_getnumberclass();}
+
+MAYBETYPE: ':' TYPE {$$=$2;}
+MAYBETYPE:          {$$=0;}
+
+//FUNCTION_HEADER:      NAMESPACE MODIFIERS T_FUNCTION GETSET T_IDENTIFIER '(' PARAMS ')' 
+FUNCTION_HEADER:      MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
+                      MAYBETYPE
+
+NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER
+NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_IDENTIFIER
+NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_STRING
+
+//NAMESPACE :              {$$=empty_token();}
+//NAMESPACE : T_IDENTIFIER {$$=$1};
+
+CONSTANT : T_BYTE {$$.c = abc_pushbyte(0, $1);
+                   //MULTINAME(m, registry_getintclass());
+                   //$$.c = abc_coerce2($$.c, &m); // FIXME
+                   $$.t = TYPE_INT;
+                  }
+CONSTANT : T_SHORT {$$.c = abc_pushshort(0, $1);
+                    $$.t = TYPE_INT;
+                   }
+CONSTANT : T_INT {$$.c = abc_pushint(0, $1);
+                  $$.t = TYPE_INT;
+                 }
+CONSTANT : T_UINT {$$.c = abc_pushuint(0, $1);
+                   $$.t = TYPE_UINT;
+                  }
+CONSTANT : T_FLOAT {$$.c = abc_pushdouble(0, $1);
+                    $$.t = TYPE_FLOAT;
+                   }
+CONSTANT : T_STRING {$$.c = abc_pushstring(0, $1);
+                     $$.t = TYPE_STRING;
+                    }
+CONSTANT : KW_TRUE {$$.c = abc_pushtrue(0);
+                    $$.t = TYPE_BOOLEAN;
+                   }
+CONSTANT : KW_FALSE {$$.c = abc_pushfalse(0);
+                     $$.t = TYPE_BOOLEAN;
+                    }
+CONSTANT : KW_NULL {$$.c = abc_pushnull(0);
+                    $$.t = TYPE_NULL;
+                   }
+
+USE_NAMESPACE : "use" "namespace" T_IDENTIFIER
 
-VAR_READ : T_IDENTIFIER {
-    int i = find_variable($1->text, &$$.t);
-    $$.c = abc_getlocal(0, i);
-}
 
 //VARIABLE : T_IDENTIFIER
 //VARIABLE : VARIABLE '.' T_IDENTIFIER
@@ -1144,9 +1473,6 @@ VAR_READ : T_IDENTIFIER {
 //VARIABLE : VARIABLE "::" '[' EXPRESSION ']' // qualified expression
 //VARIABLE : VARIABLE '[' EXPRESSION ']' // unqualified expression
 
-// keywords which also may be identifiers
-X_IDENTIFIER : T_IDENTIFIER | KW_PACKAGE
-
 GETSET : "get" {$$=$1;}
        | "set" {$$=$1;}
        |       {$$=empty_token();}
@@ -1160,64 +1486,15 @@ PARAM:  T_IDENTIFIER ':' TYPE {$$ = malloc(sizeof(param_t));
 PARAM:  T_IDENTIFIER          {$$ = malloc(sizeof(param_t));
                                $$->name=$1->text;$$->type = TYPE_ANY;}
 
-DECLARATION : VARIABLE_DECLARATION
-DECLARATION : FUNCTION_DECLARATION
-
 IDECLARATION : VARIABLE_DECLARATION
 IDECLARATION : FUNCTION_DECLARATION
 
 //IDENTIFIER_LIST : T_IDENTIFIER ',' IDENTIFIER_LIST {extend($3,$1);$$=$3;}
 //IDENTIFIER_LIST : T_IDENTIFIER                     {$$=empty_token();extend($$,$1);}
 
-PACKAGEANDCLASS : T_IDENTIFIER {
-
-    /* try current package */
-    $$ = registry_findclass(state->package, $1->text);
-
-    /* try explicit imports */
-    dictentry_t* e = dict_get_slot(state->imports, $1->text);
-    while(e) {
-        if($$)
-            break;
-        if(!strcmp(e->key, $1->text)) {
-            $$ = (class_signature_t*)e->data;
-        }
-        e = e->next;
-    }
-
-    /* try package.* imports */
-    import_list_t*l = state->wildcard_imports;
-    while(l) {
-        if($$)
-            break;
-        //printf("does package %s contain a class %s?\n", l->import->package, $1->text);
-        $$ = registry_findclass(l->import->package, $1->text);
-        l = l->next;
-    }
-
-    /* try global package */
-    if(!$$) {
-        $$ = registry_findclass("", $1->text);
-    }
+QNAME_LIST : QNAME {$$=list_new();list_append($$, $1);}
+QNAME_LIST : QNAME_LIST ',' QNAME {$$=$1;list_append($$,$3);}
 
-    if(!$$) syntaxerror("Could not find class %s\n", $1->text);
-}
-PACKAGEANDCLASS : PACKAGE '.' T_IDENTIFIER {
-    $$ = registry_findclass($1->text, $3->text);
-    if(!$$) syntaxerror("Couldn't find class %s.%s\n", $1->text, $3->text);
-}
-
-
-MULTILEVELIDENTIFIER : MULTILEVELIDENTIFIER '.' X_IDENTIFIER {$$=$1;extend_s($$, ".", $3)}
-MULTILEVELIDENTIFIER : T_IDENTIFIER                 {$$=$1;extend($$,$1)};
-
-PACKAGEANDCLASS_LIST : PACKAGEANDCLASS {$$=list_new();list_append($$, $1);}
-PACKAGEANDCLASS_LIST : PACKAGEANDCLASS_LIST ',' PACKAGEANDCLASS {$$=$1;list_append($$,$3);}
-
-MAYBE_DECLARATION_LIST : 
-MAYBE_DECLARATION_LIST : DECLARATION_LIST
-DECLARATION_LIST : DECLARATION
-DECLARATION_LIST : DECLARATION_LIST DECLARATION
 
 MAYBE_IDECLARATION_LIST : 
 MAYBE_IDECLARATION_LIST : IDECLARATION_LIST