+ global->token2info = dict_lookup(global->file2token2info,
+ current_filename // use long version
+ );
+ if(!global->token2info) {
+ global->token2info = dict_new2(&ptr_type);
+ dict_put(global->file2token2info, current_filename, global->token2info);
+ }
+
+ if(as3_pass==1) {
+ state->method = rfx_calloc(sizeof(methodstate_t));
+ dict_put(global->token2info, (void*)(ptroff_t)as3_tokencount, state->method);
+ state->method->late_binding = 1; // init scripts use getglobalscope, so we need a getlocal0/pushscope
+ } else {
+ state->method = dict_lookup(global->token2info, (void*)(ptroff_t)as3_tokencount);
+ function_initvars(state->method, 0, 0, 1);
+ global->init = abc_initscript(global->file);
+ }
+}
+
+void finish_file()
+{
+ if(!state || state->level!=1) {
+ syntaxerror("unexpected end of file in pass %d", as3_pass);
+ }
+
+ if(as3_pass==2) {
+ code_t*header = method_header(state->method);
+ code_t*c = wrap_function(header, 0, global->init->method->body->code);
+ global->init->method->body->code = c;
+ free(state->method);state->method=0;
+ }
+
+ //free(state->package);state->package=0; // used in registry
+ state_destroy(state);state=0;
+}
+
+void initialize_parser()
+{
+ global = rfx_calloc(sizeof(global_t));
+ global->file = abc_file_new();
+ global->file->flags &= ~ABCFILE_LAZY;
+ global->file2token2info = dict_new();
+ global->token2info = 0;
+}
+
+void* finish_parser()
+{
+ dict_free_all(global->file2token2info, 1, (void*)dict_destroy);
+ global->token2info=0;
+ return global->file;
+}
+
+typedef struct _variable {
+ int index;
+ classinfo_t*type;
+ char init;
+ char is_parameter;
+ methodstate_t*is_inner_method;
+} variable_t;
+
+static variable_t* find_variable(state_t*s, char*name)
+{
+ while(s) {
+ variable_t*v = 0;
+ v = dict_lookup(s->vars, name);
+ if(v) return v;
+ if(s->new_vars) break;
+ s = s->old;
+ }
+ return 0;
+}
+static variable_t* find_slot(state_t*s, const char*name)
+{
+ if(s->method && s->method->slots)
+ return dict_lookup(s->method->slots, name);
+ return 0;
+}
+
+static variable_t* find_variable_safe(state_t*s, char*name)
+{
+ variable_t* v = find_variable(s, name);
+ if(!v)
+ syntaxerror("undefined variable: %s", name);
+ return v;
+}
+
+static char variable_exists(char*name)
+{
+ return dict_contains(state->vars, name);
+}
+
+static code_t*defaultvalue(code_t*c, classinfo_t*type)
+{
+ if(TYPE_IS_INT(type)) {
+ c = abc_pushbyte(c, 0);
+ } else if(TYPE_IS_UINT(type)) {
+ c = abc_pushuint(c, 0);
+ } else if(TYPE_IS_FLOAT(type)) {
+ c = abc_pushnan(c);
+ } else if(TYPE_IS_BOOLEAN(type)) {
+ c = abc_pushfalse(c);
+ } else if(!type) {
+ //c = abc_pushundefined(c);
+ syntaxerror("internal error: can't generate default value for * type");
+ } else {
+ c = abc_pushnull(c);
+ MULTINAME(m, type);
+ c = abc_coerce2(c, &m);
+ }
+ return c;
+}
+
+static int alloc_local()
+{
+ return state->method->variable_count++;
+}
+
+static variable_t* new_variable2(const char*name, classinfo_t*type, char init, char maybeslot)
+{
+ if(maybeslot) {
+ variable_t*v = find_slot(state, name);
+ if(v)
+ return v;
+ }
+
+ NEW(variable_t, v);
+ v->index = alloc_local();
+ v->type = type;
+ v->init = init;
+
+ if(name)
+ dict_put(state->vars, name, v);
+
+ return v;
+}
+static int new_variable(const char*name, classinfo_t*type, char init, char maybeslot)
+{
+ return new_variable2(name, type, init, maybeslot)->index;