implemented new()
[swftools.git] / lib / as3 / parser.y
index 6055295..152a001 100644 (file)
@@ -1,3 +1,25 @@
+/* parser.lex
+
+   Routines for compiling Flash2 AVM2 ABC Actionscript
+
+   Extension module for the rfxswf library.
+   Part of the swftools package.
+
+   Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 %{
 #include <stdlib.h>
 #include <stdio.h>
@@ -9,6 +31,7 @@
 #include "registry.h"
 #include "code.h"
 #include "opcodes.h"
+
 %}
 
 //%glr-parser
 
 %union tokenunion {
     tokenptr_t token;
-    multiname_t*multiname;
-    multiname_list_t*multiname_list;
+
+    class_signature_t*class_signature;
+    class_signature_list_t*class_signature_list;
+
     int number_int;
     unsigned int number_uint;
     double number_float;
-    struct _code*code;
-    struct _code_list*code_list;
+    code_t*code;
+    typedcode_t value;
+    typedcode_list_t*value_list;
+    param_t* param;
+    param_list_t* param_list;
+    writeable_t writeable;
+    char*string;
 }
 
 
 %token<token> T_IDENTIFIER
-%token<token> T_STRING
+%token<string> T_STRING
 %token<token> T_REGEXP
 %token<token> T_EMPTY
 %token<number_int> T_INT
@@ -38,7 +68,7 @@
 %token<number_float> T_FLOAT
 
 %token<token> KW_IMPLEMENTS
-%token<token> KW_NAMESPACE
+%token<token> KW_NAMESPACE "namespace"
 %token<token> KW_PACKAGE "package"
 %token<token> KW_PROTECTED
 %token<token> KW_PUBLIC
 %token<token> KW_FINAL
 %token<token> KW_GET "get"
 %token<token> KW_EXTENDS
-%token<token> KW_FALSE "False"
-%token<token> KW_TRUE "True"
+%token<token> KW_FALSE "false"
+%token<token> KW_TRUE "true"
 %token<token> KW_BOOLEAN "Boolean"
 %token<token> KW_UINT "uint"
 %token<token> KW_INT "int"
+%token<token> KW_WHILE "while"
 %token<token> KW_NUMBER "Number"
 %token<token> KW_STRING "String"
+%token<token> KW_IF "if"
+%token<token> KW_ELSE  "else"
+%token<token> KW_BREAK   "break"
 %token<token> KW_IS "is"
 %token<token> KW_AS "as"
 
 %token<token> T_EQEQ "=="
+%token<token> T_EQEQEQ "==="
+%token<token> T_NE "!="
 %token<token> T_LE "<="
 %token<token> T_GE ">="
 %token<token> T_DIVBY "/=" 
 %token<token> T_STAR '*'
 %token<token> T_DOT '.'
 
-%type <token> CODE
-%type <code> CODEPIECE CODEPIECE2
+%type <code> CODE
+%type <code> CODEPIECE
+%type <code> CODEBLOCK MAYBECODE
 %type <token> PACKAGE_DECLARATION
 %type <token> FUNCTION_DECLARATION
 %type <code> VARIABLE_DECLARATION
 %type <token> CLASS_DECLARATION
 %type <token> NAMESPACE_DECLARATION
 %type <token> INTERFACE_DECLARATION
-%type <code> EXPRESSION
-%type <code> MAYBEEXPRESSION
-%type <code> E
-%type <code> CONSTANT
-%type <token> FOR
-%type <token> USE
-%type <token> ASSIGNMENT
+%type <code> VOIDEXPRESSION
+%type <value> EXPRESSION
+%type <value> MAYBEEXPRESSION
+%type <value> E
+%type <writeable> LH
+%type <value> CONSTANT
+%type <code> FOR IF WHILE MAYBEELSE BREAK
+%type <token> USE_NAMESPACE
+%type <code> ASSIGNMENT FOR_INIT
 %type <token> IMPORT
-%type <multiname> MAYBETYPE
-%type <token> PACKAGESPEC
+%type <class_signature> MAYBETYPE
 %type <token> GETSET
-%type <token> PARAM
-%type <token> PARAMS
-%type <token> PARAM_LIST
+%type <param> PARAM
+%type <param_list> PARAM_LIST
+%type <param_list> MAYBE_PARAM_LIST
 %type <token> MODIFIERS
 %type <token> MODIFIER_LIST
-%type <multiname_list> IMPLEMENTS_LIST
-%type <multiname> EXTENDS
-%type <multiname_list> EXTENDS_LIST
-%type <multiname> PACKAGEANDCLASS
-%type <multiname_list> PACKAGEANDCLASS_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 <multiname> TYPE
+%type <class_signature> TYPE
 %type <token> VAR
-%type <token> VARIABLE
-%type <code> VAR_READ
-%type <token> NEW
+//%type <token> VARIABLE
+%type <value> VAR_READ
+%type <value> NEW
 %type <token> X_IDENTIFIER
 %type <token> MODIFIER
 %type <token> PACKAGE
-%type <code> FUNCTIONCALL
-%type <code_list> MAYBE_EXPRESSION_LIST EXPRESSION_LIST
+%type <value> FUNCTIONCALL
+%type <value_list> MAYBE_EXPRESSION_LIST EXPRESSION_LIST MAYBE_PARAM_VALUES
 
 // precendence: from low to high
 // http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000012.html
 %nonassoc '|'
 %nonassoc '^'
 %nonassoc '&'
-%nonassoc "!=" "==" "<=" '<' ">=" '>' // TODO: support "a < b < c" syntax?
+%nonassoc "!=" "==" "===" "<=" '<' ">=" '>' // TODO: support "a < b < c" syntax?
 %nonassoc "is"
 %left '-'
 %left '+'
 %nonassoc "as"
 %left '.' ".." "::"
 %nonassoc T_IDENTIFIER
+%left below_semicolon
+%left ';'
+%nonassoc "else"
 %left '('
 
      
@@ -216,7 +257,7 @@ static char* concat3str(const char* t1, const char* t2, const char* t3)
 }
 
 typedef struct _import {
-    char*path;
+    char*package;
 } import_t;
 
 DECLARE_LIST(import);
@@ -229,33 +270,87 @@ typedef struct _state {
 
     char*package;     
     char*function;
+    /* code that needs to be executed at the start of
+       a method (like initializing local registers) */
+    code_t*initcode;
+
     abc_method_body_t*m;
-    import_list_t*imports;
+    
+    import_list_t*wildcard_imports;
+    dict_t*imports;
+    char has_own_imports;
    
     /* class data */
     char*classname;
     abc_class_t*cls;
 
     array_t*vars;
-
+    int local_var_base;
 } state_t;
 
 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);
+
 static state_list_t*state_stack=0;
 
-void initialize_state()
+static void new_state()
 {
     NEW(state_t, s);
     NEW(state_list_t, sl);
+
+    state_t*oldstate = state;
+    if(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 = sl->state = s;
+    state = s;
+    state->level++;
+    state->vars = array_new();
+    state->initcode = 0;
+    state->has_own_imports = 0;
+}
+static void state_has_imports()
+{
+    state->wildcard_imports = list_clone(state->wildcard_imports);
+    state->imports = dict_clone(state->imports);
+    state->has_own_imports = 1;
+}
+
+static void old_state()
+{
+    if(!state_stack || !state_stack->next)
+        syntaxerror("invalid nesting");
+    state_t*oldstate = state;
+    state_list_t*old = state_stack;
+    state_stack = state_stack->next;
+    free(old);
+    state = state_stack->state;
+    /*if(state->initcode) {
+        printf("residual initcode\n");
+        code_dump(state->initcode, 0, 0, "", stdout);
+    }*/
+    if(oldstate->has_own_imports) {
+        list_free(oldstate->wildcard_imports);
+        dict_destroy(oldstate->imports);oldstate->imports=0;
+    }
+    state->initcode = code_append(state->initcode, oldstate->initcode);
+}
+void initialize_state()
+{
+    new_state();
 
     state->file = abc_file_new();
     state->file->flags &= ~ABCFILE_LAZY;
-    state->level = 0;
     
     state->init = abc_initscript(state->file, 0, 0);
     abc_method_body_t*m = state->init->method->body;
@@ -267,7 +362,7 @@ void initialize_state()
 }
 void* finalize_state()
 {
-    if(state->level) {
+    if(state->level!=1) {
         syntaxerror("unexpected end of file");
     }
     abc_method_body_t*m = state->init->method->body;
@@ -280,32 +375,6 @@ void* finalize_state()
     return state->file;
 }
 
-static void new_state()
-{
-    NEW(state_t, s);
-    NEW(state_list_t, sl);
-
-    if(state->m) {
-        syntaxerror("not able to start another method scope");
-    }
-
-    memcpy(s, state, sizeof(state_t)); //shallow copy
-    sl->next = state_stack;
-    sl->state = s;
-    state_stack = sl;
-    state = s;
-    state->level++;
-}
-static void old_state()
-{
-    if(!state_stack || !state_stack->next)
-        syntaxerror("invalid nesting");
-    state_t*oldstate = state;
-    state_list_t*old = state_stack;
-    state_stack = state_stack->next;
-    free(old);
-    state = state_stack->state;
-}
 
 static void startpackage(token_t*t) 
 {
@@ -314,34 +383,36 @@ static void startpackage(token_t*t)
     } 
     new_state();
     char*name = t?t->text:"";
-    printf("entering package \"%s\"\n", name);
+    /*printf("entering package \"%s\"\n", name);*/
     state->package = name;
 }
 static void endpackage()
 {
-    printf("leaving package \"%s\"\n", state->package);
+    /*printf("leaving package \"%s\"\n", state->package);*/
     old_state();
 }
 
 char*globalclass=0;
-static void startclass(token_t*modifiers, token_t*name, multiname_t*extends, multiname_list_t*implements)
+static void startclass(token_t*modifiers, token_t*name, class_signature_t*extends, class_signature_list_t*implements, char interface)
 {
     if(state->cls) {
         syntaxerror("inner classes now allowed"); 
     }
     new_state();
     state->classname = name->text;
-    printf("entering class %s\n", name->text);
+
     token_list_t*t=0;
+    class_signature_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");
-    printf("  extends: %s\n", multiname_tostring(extends));
-
-    multiname_list_t*mlist=0;
+    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 ", multiname_tostring(mlist->multiname));
+        printf("%s ", mlist->class_signature?mlist->class_signature->name:0);
     }
     printf("\n");
+    */
 
     char public=0,internal=0,final=0,sealed=1;
     for(t=modifiers->tokens;t;t=t->next) {
@@ -361,40 +432,59 @@ static void startclass(token_t*modifiers, token_t*name, multiname_t*extends, mul
         syntaxerror("public and internal not supported at the same time.");
 
     /* create the class name, together with the proper attributes */
-    multiname_t* classname = 0;
-    if(!public && !state->package)
-        classname = multiname_new(namespace_new_private(current_filename), state->classname);
-    else if(!public && state->package)
-        classname = multiname_new(namespace_new_packageinternal(state->package), state->classname);
-    else if(state->package)
-        classname = multiname_new(namespace_new_package(state->package), state->classname);
-    else
+    int access=0;
+    char*package=0;
+
+    if(!public && !state->package) {
+        access = ACCESS_PRIVATE; package = current_filename;
+    } else if(!public && state->package) {
+        access = ACCESS_PACKAGEINTERNAL; package = state->package;
+    } else if(state->package) {
+        access = ACCESS_PACKAGE; package = state->package;
+    } else {
         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);
+    }
+    
+    class_signature_t* classname = class_signature_register(access, package, state->classname);
 
-    state->cls = abc_class_new(state->file, classname, extends);
+    MULTINAME(classname2,classname);
+    
+    multiname_t*extends2 = sig2mname(extends);
+
+    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) {
-        abc_class_add_interface(state->cls, mlist->multiname);
+        MULTINAME(m, mlist->class_signature);
+        abc_class_add_interface(state->cls, &m);
     }
 
     /* now write the construction code for this class */
-    int slotindex = abc_initscript_addClassTrait(state->init, classname, state->cls);
+    int slotindex = abc_initscript_addClassTrait(state->init, &classname2, state->cls);
 
     abc_method_body_t*m = state->init->method->body;
     __ getglobalscope(m);
-    multiname_t*s = extends;
+    class_signature_t*s = extends;
 
     int count=0;
     
     while(s) {
         //TODO: take a look at the current scope stack, maybe 
         //      we can re-use something
-        s = registry_getsuperclass(s);
+        s = s->superclass;
         if(!s) 
         break;
-        __ getlex2(m, s);
+       
+        multiname_t*s2 = sig2mname(s);
+        __ getlex2(m, s2);
+        multiname_destroy(s2);
+
         __ pushscope(m);
         m->code = m->code->prev->prev; // invert
         count++;
@@ -405,95 +495,89 @@ static void startclass(token_t*modifiers, token_t*name, multiname_t*extends, mul
     /* TODO: if this is one of *our* classes, we can also 
              do a getglobalscope/getslot <nr> (which references
              the init function's slots) */
-    __ getlex2(m, extends);
-    __ dup(m);
-    __ pushscope(m); // we get a Verify Error #1107 if this is not the top scope
+    if(extends2) {
+        __ getlex2(m, extends2);
+        __ dup(m);
+        __ pushscope(m); // we get a Verify Error #1107 if this is not the top scope
+    } else {
+        __ pushnull(m);
+    }
     __ newclass(m,state->cls);
     while(count--) {
         __ popscope(m);
     }
     __ setslot(m, slotindex);
 
-    if(!globalclass && public && multiname_equals(registry_getMovieClip(),extends)) {
+    /* flash.display.MovieClip handling */
+    if(!globalclass && public && class_signature_equals(registry_getMovieClip(),extends)) {
         if(state->package && state->package[0]) {
             globalclass = concat3str(state->package, ".", state->classname);
         } else {
             globalclass = strdup(state->classname);
         }
     }
+    multiname_destroy(extends2);
 }
 
 static void endclass()
 {
-    printf("leaving class %s\n", state->classname);
+    /*printf("leaving class %s\n", state->classname);*/
     old_state();
 }
-static void addimport(token_t*t)
-{
-    NEW(import_t,i);
-    i->path = t->text;
-    list_append(state->imports, i);
-}
-static void print_imports()
-{
-    import_list_t*l = state->imports;
-    while(l) {
-        printf("  import %s\n", l->import->path);
-        l = l->next;
-    }
-}
 static void startfunction(token_t*ns, token_t*mod, token_t*getset, token_t*name,
-                          token_t*params, multiname_t*type)
+                          param_list_t*params, class_signature_t*type)
 {
     token_list_t*t;
     new_state();
     state->function = name->text;
-    printf("entering function %s\n", 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");
-    printf("  type: %s\n", multiname_tostring(type));
-    print_imports();
+    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)) {
-        state->m = abc_class_constructor(state->cls, type, 0);
+        state->m = abc_class_constructor(state->cls, type2, 0);
     } else {
-        state->m = abc_class_method(state->cls, type, name->text, 0);
+        state->m = abc_class_method(state->cls, type2, name->text, 0);
     }
-    state->vars = array_new();
+    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);
+    }
 
     __ getlocal_0(state->m);
     __ pushscope(state->m);
+
+    multiname_destroy(type2);
 }
 static void endfunction()
 {
-    printf("leaving function %s\n", state->function);
+    /*printf("leaving function %s\n", state->function);*/
     __ returnvoid(state->m);
 
     old_state();
 }
-static int newvariable(token_t*mod, token_t*varconst, token_t*name, multiname_t*type)
-{
-    token_list_t*t;
-    printf("defining new variable %s\n", name->text);
-    printf("  mod: ");for(t=mod->tokens;t;t=t->next) printf("%s ", t->token->text);printf("\n");
-    printf("  access: ");printf("%s\n", varconst->text);
-    printf("  type: ");printf("%s\n", multiname_tostring(type));
 
-    if(!state->vars) 
-        syntaxerror("not allowed to defined variables outside a method");
 
-    int index = array_find(state->vars, name->text);
-    if(index>=0) {
-        syntaxerror("Variable %s already defined", name->text);
-    } else {
-        index = array_append(state->vars, name->text, 0);
-    }
-    return index;
-}
 static token_t* empty_token()
 {
     NEW(token_t,t);
@@ -522,41 +606,322 @@ 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)
+{
+    state_list_t* s = state_stack;
+    while(s) {
+        int i = array_find(s->state->vars, name);
+        if(i>=0) {
+            if(m) {
+                *m = array_getvalue(s->state->vars, i);
+            }
+            return i + s->state->local_var_base;
+        }
+        s = s->next;
+    }
+    syntaxerror("undefined variable: %s", name);
+} 
+static char variable_exists(char*name) 
+{
+    return array_contains(state->vars, name);
+}
+static int new_variable(char*name, class_signature_t*type)
+{
+    return array_append(state->vars, name, type) + state->local_var_base;
+}
+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);
+        //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);
+        //}
+    }
+    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)
+{
+    return 1; // FIXME
+}
+
+void breakjumpsto(code_t*c, code_t*jump) 
+{
+    while(c->prev) 
+        c=c->prev;
+    while(c) {
+        if(c->opcode == OPCODE___BREAK__) {
+            c->opcode = OPCODE_JUMP;
+            c->branch = jump;
+        }
+        c = c->next;
+    }
+}
+code_t*converttype(code_t*c, class_signature_t*from, class_signature_t*to)
+{
+    if(!to) {
+        /*TODO: can omit this if from is zero? */
+        return abc_coerce_a(c);
+    }
+    if(TYPE_IS_NUMBER(from) && TYPE_IS_UINT(to)) {
+        MULTINAME(m, TYPE_UINT);
+        return abc_coerce2(c, &m);
+    }
+    if(TYPE_IS_NUMBER(from) && TYPE_IS_INT(to)) {
+        MULTINAME(m, TYPE_INT);
+        return abc_coerce2(c, &m);
+    }
+    return c;
+}
+
+code_t*defaultvalue(code_t*c, class_signature_t*type)
+{
+    if(TYPE_IS_INT(type) || TYPE_IS_UINT(type) || TYPE_IS_FLOAT(type)) {
+       c = abc_pushbyte(c, 0);
+    } else if(TYPE_IS_BOOLEAN(type)) {
+       c = abc_pushfalse(c);
+    } else {
+       c = abc_pushnull(c);
+    }
+    return c;
+}
+
 %}
 
+
 %%
 
-PROGRAM: MAYBECODE
+/* ------------ code blocks / statements ---------------- */
 
-MAYBECODE: CODE
-MAYBECODE: 
+PROGRAM: MAYBECODE
 
-CODE: CODE CODEPIECE2 {$$=$1;}
-CODE: CODEPIECE2 {$$=empty_token();}
+MAYBECODE: CODE {$$=$1;}
+MAYBECODE:      {$$=code_new();}
 
-CODEPIECE2: CODEPIECE {
-    if(state->m) {
-        state->m->code = code_append(state->m->code, $1);
-    }
-}
+CODE: CODE CODEPIECE {$$=code_append($1,$2);}
+CODE: CODEPIECE {$$=$1;}
 
-CODEPIECE: ';'                   {/*TODO*/$$=code_new();}
+CODEPIECE: PACKAGE_DECLARATION   {$$=code_new();/*enters a scope*/}
+CODEPIECE: CLASS_DECLARATION     {$$=code_new();/*enters a scope*/}
+CODEPIECE: INTERFACE_DECLARATION {/*TODO*/$$=code_new();}
+CODEPIECE: IMPORT                {$$=code_new();/*adds imports to current scope*/}
+CODEPIECE: ';'                   {$$=code_new();}
 CODEPIECE: VARIABLE_DECLARATION  {$$=$1}
-CODEPIECE: PACKAGE_DECLARATION   {/*TODO*/$$=code_new();}
-CODEPIECE: IMPORT                {/*TODO*/$$=code_new();}
+CODEPIECE: VOIDEXPRESSION        {$$=$1}
+CODEPIECE: FOR                   {$$=$1}
+CODEPIECE: WHILE                 {$$=$1}
+CODEPIECE: BREAK                 {$$=$1}
+CODEPIECE: IF                    {$$=$1}
+CODEPIECE: ASSIGNMENT            {$$=$1}
 CODEPIECE: NAMESPACE_DECLARATION {/*TODO*/$$=code_new();}
-CODEPIECE: CLASS_DECLARATION     {/*TODO*/$$=code_new();}
-CODEPIECE: INTERFACE_DECLARATION {/*TODO*/$$=code_new();}
 CODEPIECE: FUNCTION_DECLARATION  {/*TODO*/$$=code_new();}
-CODEPIECE: EXPRESSION            {/*calculate and discard*/$$=$1;$$=abc_pop($$);}
-CODEPIECE: FOR                   {/*TODO*/$$=code_new();}
-CODEPIECE: USE                   {/*TODO*/$$=code_new();}
-CODEPIECE: ASSIGNMENT            {/*TODO*/$$=code_new();}
+CODEPIECE: USE_NAMESPACE         {/*TODO*/$$=code_new();}
+
+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;}
+                |                {$$.c=abc_pushundefined(0);
+                                  $$.t=TYPE_ANY;
+                                 }
+
+VAR : "const" | "var"
+VARIABLE_DECLARATION : VAR T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
+    if(variable_exists($2->text))
+        syntaxerror("Variable %s already defined", $2->text);
+   
+    if(!is_subtype_of($4.t, $3)) {
+        syntaxerror("Can't convert %s to %s", $4.t->name, 
+                                              $3->name);
+    }
+
+    int index = new_variable($2->text, $3);
+    
+    if($3) {
+        if($4.c->prev || $4.c->opcode != OPCODE_PUSHUNDEFINED) {
+            $$ = $4.c;
+            $$ = converttype($$, $4.t, $3);
+            $$ = abc_setlocal($$, index);
+        } else {
+            $$ = defaultvalue(0, $3);
+            $$ = abc_setlocal($$, index);
+        }
+
+        /* push default value for type on stack */
+        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($$);
+            $$ = abc_setlocal($$, index);
+        } else {
+            $$ = code_new();
+        }
+    }
+    
+    /* that's the default for a local register, anyway
+        else {
+        state->initcode = abc_pushundefined(state->initcode);
+        state->initcode = abc_setlocal(state->initcode, index);
+    }*/
+    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 ------------------------- */
+
+MAYBEELSE:  %prec prec_none {$$ = code_new();}
+MAYBEELSE: "else" CODEBLOCK {$$=$2;}
+//MAYBEELSE: ';' "else" CODEBLOCK {$$=$3;}
+
+IF  : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE {
+    $$ = state->initcode;state->initcode=0;
+
+    $$ = code_append($$, $4.c);
+    code_t*myjmp,*myif = $$ = abc_iffalse($$, 0);
+   
+    $$ = code_append($$, $6);
+    if($7) {
+        myjmp = $$ = abc_jump($$, 0);
+    }
+    myif->branch = $$ = abc_label($$);
+    if($7) {
+        $$ = code_append($$, $7);
+        myjmp->branch = $$ = abc_label($$);
+    }
+    
+    $$ = killvars($$);old_state();
+}
+
+FOR_INIT : {$$=code_new();}
+FOR_INIT : ASSIGNMENT | VARIABLE_DECLARATION | VOIDEXPRESSION
+
+FOR : "for" '(' {new_state();} FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK {
+    $$ = state->initcode;state->initcode=0;
+
+    $$ = code_append($$, $4);
+    code_t*loopstart = $$ = abc_label($$);
+    $$ = code_append($$, $6.c);
+    code_t*myif = $$ = abc_iffalse($$, 0);
+    $$ = code_append($$, $10);
+    $$ = code_append($$, $8);
+    $$ = abc_jump($$, loopstart);
+    code_t*out = $$ = abc_label($$);
+    breakjumpsto($$, out);
+    myif->branch = out;
+
+    $$ = killvars($$);old_state();
+}
+
+WHILE : "while" '(' {new_state();} EXPRESSION ')' CODEBLOCK {
+    $$ = state->initcode;state->initcode=0;
+
+    code_t*myjmp = $$ = abc_jump($$, 0);
+    code_t*loopstart = $$ = abc_label($$);
+    $$ = code_append($$, $6);
+    myjmp->branch = $$ = abc_label($$);
+    $$ = code_append($$, $4.c);
+    $$ = abc_iftrue($$, loopstart);
+    code_t*out = $$ = abc_label($$);
+    breakjumpsto($$, out);
+
+    $$ = killvars($$);old_state();
+}
+
+BREAK : "break" {
+    $$ = abc___break__(0);
+}
+
+/* ------------ packages and imports ---------------- */
 
 PACKAGE_DECLARATION : "package" MULTILEVELIDENTIFIER '{' {startpackage($2)} MAYBECODE '}' {endpackage()}
 PACKAGE_DECLARATION : "package" '{' {startpackage(0)} MAYBECODE '}' {endpackage()}
 
-IMPORT : "import" PACKAGESPEC {addimport($2);}
+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);
+       if(!c) 
+            syntaxerror("Couldn't import %s.%s\n", $2->text, $4->text);
+       state_has_imports();
+       dict_put(state->imports, $4->text, c);
+       $$=0;
+}
+IMPORT : "import" PACKAGE '.' '*' {
+       NEW(import_t,i);
+       i->package = $2->text;
+       state_has_imports();
+       list_append(state->wildcard_imports, i);
+       $$=0;
+}
+
+/* ------------ classes and interfaces -------------- */
+
+MODIFIERS : {$$=empty_token();}
+MODIFIERS : MODIFIER_LIST {$$=$1}
+MODIFIER_LIST : MODIFIER MODIFIER_LIST {extend($2,$1);$$=$2;}
+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_LIST : {$$=list_new();}
+EXTENDS_LIST : KW_EXTENDS PACKAGEANDCLASS_LIST {$$=$2;}
+
+IMPLEMENTS_LIST : {$$=list_new();}
+IMPLEMENTS_LIST : KW_IMPLEMENTS PACKAGEANDCLASS_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();}
@@ -570,10 +935,8 @@ MAYBETYPE: ':' TYPE {$$=$2;}
 MAYBETYPE:          {$$=0;}
 
 //FUNCTION_HEADER:      NAMESPACE MODIFIERS T_FUNCTION GETSET T_IDENTIFIER '(' PARAMS ')' 
-FUNCTION_HEADER:      MODIFIERS "function" GETSET T_IDENTIFIER '(' PARAMS ')' 
+FUNCTION_HEADER:      MODIFIERS "function" GETSET T_IDENTIFIER '(' MAYBE_PARAM_LIST ')' 
                       MAYBETYPE
-FUNCTION_DECLARATION: MODIFIERS "function" GETSET T_IDENTIFIER '(' PARAMS ')' 
-                      MAYBETYPE '{' {startfunction(0,$1,$3,$4,$6,$8)} MAYBECODE '}' {endfunction()}
 
 NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER
 NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_IDENTIFIER
@@ -582,125 +945,220 @@ NAMESPACE_DECLARATION : MODIFIERS KW_NAMESPACE T_IDENTIFIER '=' T_STRING
 //NAMESPACE :              {$$=empty_token();}
 //NAMESPACE : T_IDENTIFIER {$$=$1};
 
-CONSTANT : T_BYTE {$$ = abc_pushbyte(0, $1);}
-CONSTANT : T_SHORT {$$ = abc_pushshort(0, $1);}
-CONSTANT : T_INT {$$ = abc_pushint(0, $1);}
-CONSTANT : T_UINT {$$ = abc_pushuint(0, $1);}
-CONSTANT : T_FLOAT {$$ = abc_pushdouble(0, $1);}
-CONSTANT : T_STRING {$$ = abc_pushstring(0, $1->text);}
-CONSTANT : KW_TRUE {$$ = abc_pushtrue(0);}
-CONSTANT : KW_FALSE {$$ = abc_pushfalse(0);}
-CONSTANT : KW_NULL {$$ = abc_pushnull(0);}
-
-VAR : "const" | "var"
-
-// type annotation
-// TODO: NAMESPACE
+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
 
-VARIABLE_DECLARATION : MODIFIERS VAR T_IDENTIFIER MAYBETYPE MAYBEEXPRESSION {
-    int i = newvariable($1,$2,$3,$4);
-    $$=code_new();
-    $$ = $5;
-    $$ = abc_setlocal($$, i);
-}
-
-MAYBEEXPRESSION : '=' EXPRESSION {$$=$2;}
-                |                {$$=code_new();}
 
 EXPRESSION : E %prec prec_none  /*precendence below '-x'*/ {$$ = $1;}
+VOIDEXPRESSION : E %prec prec_none {$$=$1.c;/*calculate and discard*/$$=abc_pop($$);}
 
 E : CONSTANT
 E : VAR_READ %prec T_IDENTIFIER {$$ = $1;}
-E : NEW                         {$$ = abc_pushundefined(0); /* FIXME */}
-E : T_REGEXP                    {$$ = abc_pushundefined(0); /* FIXME */}
+E : NEW                         {$$ = $1;}
+E : T_REGEXP                    {$$.c = abc_pushundefined(0); /* FIXME */
+                                 $$.t = TYPE_ANY;
+                                }
 E : FUNCTIONCALL
-E : E '<' E
-E : E '>' E
-E : E "<=" E
-E : E ">=" E
-E : E "==" E
-E : E '+' E
+E : E '<' E {$$.c = code_append($1.c,$3.c);$$.c = abc_greaterequals($$.c);$$.c=abc_not($$.c);
+             $$.t = TYPE_BOOLEAN;
+            }
+E : E '>' E {$$.c = code_append($1.c,$3.c);$$.c = abc_greaterthan($$.c);
+             $$.t = TYPE_BOOLEAN;
+            }
+E : E "<=" E {$$.c = code_append($1.c,$3.c);$$.c = abc_greaterthan($$.c);$$.c=abc_not($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+E : E ">=" E {$$.c = code_append($1.c,$3.c);$$.c = abc_greaterequals($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+E : E "==" E {$$.c = code_append($1.c,$3.c);$$.c = abc_equals($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+E : E "===" E {$$.c = code_append($1.c,$3.c);$$.c = abc_strictequals($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+E : E "!=" E {$$.c = code_append($1.c,$3.c);$$.c = abc_equals($$.c);$$.c = abc_not($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+
+E : E "||" E {$$.c = $1.c;
+              $$.c=abc_dup($$.c);
+              code_t*jmp = $$.c = abc_iftrue($$.c, 0);
+              $$.c=abc_pop($$.c);
+              $$.c = code_append($$.c,$3.c);
+              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);
+              code_t*jmp = $$.c = abc_iffalse($$.c, 0);
+              $$.c=abc_pop($$.c);
+              $$.c = code_append($$.c,$3.c);
+              code_t*label = $$.c = abc_label($$.c);
+              jmp->branch = label;
+              $$.t = join_types($1.t, $3.t, 'A');
+             }
+
+E : '!' E    {$$.c=$2.c;
+              $$.c = abc_not($$.c);
+              $$.t = TYPE_BOOLEAN;
+             }
+
 E : E '-' E
 E : E '/' E
-E : E '%' E
-E : E '*' E
-E : E "++"
-E : E "--"
+E : E '+' E {$$.c = code_append($1.c,$3.c);$$.c = abc_add($$.c);$$.c=abc_coerce_a($$.c);
+             $$.t = join_types($1.t, $3.t, '+');
+            }
+E : E '%' E {$$.c = code_append($1.c,$3.c);$$.c = abc_modulo($$.c);$$.c=abc_coerce_a($$.c);
+             $$.t = join_types($1.t, $3.t, '%');
+            }
+E : E '*' E {$$.c = code_append($1.c,$3.c);$$.c = abc_multiply($$.c);$$.c=abc_coerce_a($$.c);
+             $$.t = join_types($1.t, $3.t, '*');
+            }
+
 E : E "as" TYPE
 E : E "is" TYPE
 E : '(' E ')' {$$=$2;}
 E : '-' E {$$=$2;}
 
-NEW : "new" T_IDENTIFIER
-    | "new" T_IDENTIFIER '(' ')'
-    | "new" T_IDENTIFIER '(' EXPRESSION_LIST ')'
+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;
+              }
+
+// 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;
+            }
+
+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}
+
+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 ++;
+    }
+    $$.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) */
-        $$ = code_new();
-        $$ = abc_findproperty($$, $1->text);
-        code_list_t*l = $3;
-        // push parameters on stack
-        while(l) {
-            $$ = code_append($$, l->code);
-            l = l->next;
-        }
-        $$ = abc_callproperty($$, $1->text, list_length($3));
+    /* 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 : {}
+MAYBE_EXPRESSION_LIST : {$$=0;}
 MAYBE_EXPRESSION_LIST : EXPRESSION_LIST
-EXPRESSION_LIST : EXPRESSION                     {$$=list_new();list_append($$,$1);}
-EXPRESSION_LIST : EXPRESSION_LIST ',' EXPRESSION {list_append($$,$3);}
+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);}
 
 VAR_READ : T_IDENTIFIER {
-        int i = array_find(state->vars, $1->text);
-        if(i<0)
-            syntaxerror("unknown variable");
-        $$ = abc_getlocal(0, i);
+    int i = find_variable($1->text, &$$.t);
+    $$.c = abc_getlocal(0, i);
 }
-VARIABLE : T_IDENTIFIER
-VARIABLE : VARIABLE '.' T_IDENTIFIER
-VARIABLE : VARIABLE ".." T_IDENTIFIER // descendants
-VARIABLE : VARIABLE "::" VARIABLE // namespace declaration
-VARIABLE : VARIABLE "::" '[' EXPRESSION ']' // qualified expression
-VARIABLE : VARIABLE '[' EXPRESSION ']' // unqualified expression
-
-ASSIGNMENT :           VARIABLE           '=' EXPRESSION
-NEW_ASSIGNMENT : "var" VARIABLE MAYBETYPE '=' EXPRESSION
 
-FOR : "for" '(' NEW_ASSIGNMENT ';' EXPRESSION ';' EXPRESSION ')' '{' MAYBECODE '}'
-FOR : "for" '(' ASSIGNMENT     ';' EXPRESSION ';' EXPRESSION ')' '{' MAYBECODE '}'
-
-USE : "use" KW_NAMESPACE T_IDENTIFIER
+//VARIABLE : T_IDENTIFIER
+//VARIABLE : VARIABLE '.' T_IDENTIFIER
+//VARIABLE : VARIABLE ".." T_IDENTIFIER // descendants
+//VARIABLE : VARIABLE "::" VARIABLE // namespace declaration
+//VARIABLE : VARIABLE "::" '[' EXPRESSION ']' // qualified expression
+//VARIABLE : VARIABLE '[' EXPRESSION ']' // unqualified expression
 
 // keywords which also may be identifiers
 X_IDENTIFIER : T_IDENTIFIER | KW_PACKAGE
 
-PACKAGESPEC : PACKAGESPEC '.' PACKAGESPEC {if($1->text[0]=='*') syntaxerror("wildcard in the middle of path");
-                                           $$ = concat3($1,$2,$3);}
-PACKAGESPEC : X_IDENTIFIER                {$$=$1;}
-PACKAGESPEC : '*'                         {$$=$1;}
-
 GETSET : "get" {$$=$1;}
        | "set" {$$=$1;}
        |       {$$=empty_token();}
 
-CLASS_DECLARATION : MODIFIERS "class" T_IDENTIFIER EXTENDS IMPLEMENTS_LIST '{' {startclass($1,$3,$4,$5);} MAYBE_DECLARATION_LIST '}' {endclass();}
-INTERFACE_DECLARATION : MODIFIERS "interface" T_IDENTIFIER EXTENDS_LIST '{' MAYBE_IDECLARATION_LIST '}'
-
-PARAMS: {$$=empty_token();}
-PARAMS: PARAM_LIST {$$=$1;}
-PARAM_LIST: PARAM_LIST ',' PARAM {extend($1,$3);$$=$1;}
-PARAM_LIST: PARAM                {$$=empty_token();extend($$,$1);}
-PARAM:  T_IDENTIFIER ':' TYPE {$$=$1;}
-
-MODIFIERS : {$$=empty_token();}
-MODIFIERS : MODIFIER_LIST {$$=$1}
-MODIFIER_LIST : MODIFIER MODIFIER_LIST {extend($2,$1);$$=$2;}
-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
+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;}
 
 DECLARATION : VARIABLE_DECLARATION
 DECLARATION : FUNCTION_DECLARATION
@@ -708,22 +1166,47 @@ DECLARATION : FUNCTION_DECLARATION
 IDECLARATION : VARIABLE_DECLARATION
 IDECLARATION : FUNCTION_DECLARATION
 
-IMPLEMENTS_LIST : {$$=list_new();}
-IMPLEMENTS_LIST : KW_IMPLEMENTS PACKAGEANDCLASS_LIST {$$=$2;}
+//IDENTIFIER_LIST : T_IDENTIFIER ',' IDENTIFIER_LIST {extend($3,$1);$$=$3;}
+//IDENTIFIER_LIST : T_IDENTIFIER                     {$$=empty_token();extend($$,$1);}
 
-EXTENDS : {$$=registry_getobjectclass();}
-EXTENDS : KW_EXTENDS PACKAGEANDCLASS {$$=$2;}
+PACKAGEANDCLASS : T_IDENTIFIER {
 
-EXTENDS_LIST : {$$=list_new();}
-EXTENDS_LIST : KW_EXTENDS PACKAGEANDCLASS_LIST {$$=$2;}
+    /* try current package */
+    $$ = registry_findclass(state->package, $1->text);
 
-//IDENTIFIER_LIST : T_IDENTIFIER ',' IDENTIFIER_LIST {extend($3,$1);$$=$3;}
-//IDENTIFIER_LIST : T_IDENTIFIER                     {$$=empty_token();extend($$,$1);}
+    /* 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);
+    }
+
+    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);
+}
 
-PACKAGEANDCLASS : T_IDENTIFIER {$$ = registry_findclass(state->package, $1->text);}
-PACKAGEANDCLASS : PACKAGE '.' T_IDENTIFIER {$$ = registry_findclass($1->text, $3->text);}
-PACKAGE : X_IDENTIFIER
-PACKAGE : PACKAGE '.' X_IDENTIFIER {$$=$1;extend_s($$,".",$3);}
 
 MULTILEVELIDENTIFIER : MULTILEVELIDENTIFIER '.' X_IDENTIFIER {$$=$1;extend_s($$, ".", $3)}
 MULTILEVELIDENTIFIER : T_IDENTIFIER                 {$$=$1;extend($$,$1)};