+ 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;
+}
+
+#define TEMPVARNAME "__as3_temp__"
+static int gettempvar()
+{
+ variable_t*v = find_variable(state, TEMPVARNAME);
+ if(v)
+ return v->index;
+ return new_variable(TEMPVARNAME, 0, 0, 0);
+}
+
+static code_t* var_block(code_t*body)
+{
+ code_t*c = 0;
+ code_t*k = 0;
+ int t;
+ int num=0;
+ for(t=0;t<state->vars->hashsize;t++) {
+ dictentry_t*e = state->vars->slots[t];
+ while(e) {
+ variable_t*v = (variable_t*)e->data;
+ if(v->type && v->init) {
+ c = defaultvalue(c, v->type);
+ c = abc_setlocal(c, v->index);
+ k = abc_kill(k, v->index);
+ num++;
+ }
+ e = e->next;
+ }
+ }
+
+ if(k) {
+ code_t*x = body;
+ while(x) {
+ if(x->opcode== OPCODE___BREAK__ ||
+ x->opcode== OPCODE___CONTINUE__) {
+ /* link kill code before break/continue */
+ code_t*e = code_dup(k);
+ code_t*s = code_start(e);
+ s->prev = x->prev;
+ if(x->prev) {
+ x->prev->next = s;
+ }
+ e->next = x;
+ x->prev = e;
+ }
+ x = x->prev;
+ }