+VARIABLE_DECLARATION : "var" VARIABLE_LIST {$$=$2;}
+VARIABLE_DECLARATION : "const" 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($1))
+ syntaxerror("Variable %s already defined", $1);
+
+ if(!is_subtype_of($3.t, $2)) {
+ syntaxerror("Can't convert %s to %s", $3.t->name,
+ $2->name);
+ }
+
+ int index = new_variable($1, $2, 1);
+
+ if($2) {
+ if($3.c->prev || $3.c->opcode != OPCODE_PUSHUNDEFINED) {
+ $$ = $3.c;
+ $$ = converttype($$, $3.t, $2);
+ $$ = abc_setlocal($$, index);
+ } else {
+ $$ = defaultvalue(0, $2);
+ $$ = abc_setlocal($$, index);
+ }
+ } else {
+ if($3.c->prev || $3.c->opcode != OPCODE_PUSHUNDEFINED) {
+ $$ = $3.c;
+ $$ = abc_coerce_a($$);
+ $$ = abc_setlocal($$, index);
+ } else {
+ $$ = code_new();
+ }
+ }
+
+ /* that's the default for a local register, anyway
+ else {
+ state->method->initcode = abc_pushundefined(state->method->initcode);
+ state->method->initcode = abc_setlocal(state->method->initcode, index);
+ }*/
+ //printf("variable %s -> %d (%s)\n", $2->text, index, $4.t?$4.t->name:"");
+}
+
+/* ------------ control flow ------------------------- */
+
+MAYBEELSE: %prec below_else {$$ = code_new();}
+MAYBEELSE: "else" CODEBLOCK {$$=$2;}
+//MAYBEELSE: ';' "else" CODEBLOCK {$$=$3;}
+
+IF : "if" '(' {new_state();} EXPRESSION ')' CODEBLOCK MAYBEELSE {
+
+ $$ = code_new();
+ $$ = code_append($$, $4.c);
+ code_t*myjmp,*myif = $$ = abc_iffalse($$, 0);
+
+ $$ = code_append($$, $6);
+ if($7) {
+ myjmp = $$ = abc_jump($$, 0);
+ }
+ myif->branch = $$ = abc_nop($$);
+ if($7) {
+ $$ = code_append($$, $7);
+ myjmp->branch = $$ = abc_nop($$);
+ }
+ $$ = var_block($$);
+ old_state();
+}
+
+FOR_INIT : {$$=code_new();}
+FOR_INIT : VARIABLE_DECLARATION
+FOR_INIT : VOIDEXPRESSION
+
+// TODO: why doesn't an %prec above_identifier resolve the r-r conflict here?
+FOR_IN_INIT : "var" T_IDENTIFIER MAYBETYPE {
+ $$=$2;new_variable($2,$3,1);
+}
+FOR_IN_INIT : T_IDENTIFIER {
+ $$=$1;
+}
+
+FOR_START : T_FOR '(' {new_state();$$.name=$1;$$.each=0;}
+FOR_START : T_FOR "each" '(' {new_state();$$.name=$1;$$.each=1;}
+
+FOR : FOR_START FOR_INIT ';' EXPRESSION ';' VOIDEXPRESSION ')' CODEBLOCK {
+ if($1.each) syntaxerror("invalid syntax: ; not allowed in for each statement");
+ $$ = code_new();
+ $$ = code_append($$, $2);
+ code_t*loopstart = $$ = abc_label($$);
+ $$ = code_append($$, $4.c);
+ code_t*myif = $$ = abc_iffalse($$, 0);
+ $$ = code_append($$, $8);
+ code_t*cont = $$ = abc_nop($$);
+ $$ = code_append($$, $6);
+ $$ = abc_jump($$, loopstart);
+ code_t*out = $$ = abc_nop($$);
+ breakjumpsto($$, $1.name, out);
+ continuejumpsto($$, $1.name, cont);
+ myif->branch = out;
+
+ $$ = var_block($$);
+ old_state();
+}
+
+FOR_IN : FOR_START FOR_IN_INIT "in" EXPRESSION ')' CODEBLOCK {
+ variable_t*var = find_variable($2);
+ char*tmp1name = concat2($2, "__tmp1__");
+ int it = new_variable(tmp1name, TYPE_INT, 0);
+ char*tmp2name = concat2($2, "__array__");
+ int array = new_variable(tmp1name, 0, 0);
+
+ $$ = code_new();
+ $$ = code_append($$, $4.c);
+ $$ = abc_coerce_a($$);
+ $$ = abc_setlocal($$, array);
+ $$ = abc_pushbyte($$, 0);
+ $$ = abc_setlocal($$, it);
+
+ code_t*loopstart = $$ = abc_label($$);
+
+ $$ = abc_hasnext2($$, array, it);
+ code_t*myif = $$ = abc_iffalse($$, 0);
+ $$ = abc_getlocal($$, array);
+ $$ = abc_getlocal($$, it);
+ if(!$1.each)
+ $$ = abc_nextname($$);
+ else
+ $$ = abc_nextvalue($$);
+ $$ = converttype($$, 0, var->type);
+ $$ = abc_setlocal($$, var->index);
+
+ $$ = code_append($$, $6);
+ $$ = abc_jump($$, loopstart);
+
+ code_t*out = $$ = abc_nop($$);
+ breakjumpsto($$, $1.name, out);
+ continuejumpsto($$, $1.name, loopstart);
+
+ myif->branch = out;
+
+ $$ = var_block($$);
+ old_state();
+
+ free(tmp1name);
+ free(tmp2name);
+}
+
+WHILE : T_WHILE '(' {new_state();} EXPRESSION ')' CODEBLOCK {
+
+ $$ = code_new();
+
+ code_t*myjmp = $$ = abc_jump($$, 0);
+ code_t*loopstart = $$ = abc_label($$);
+ $$ = code_append($$, $6);
+ code_t*cont = $$ = abc_nop($$);
+ myjmp->branch = cont;
+ $$ = code_append($$, $4.c);
+ $$ = abc_iftrue($$, loopstart);
+ code_t*out = $$ = abc_nop($$);
+ breakjumpsto($$, $1, out);
+ continuejumpsto($$, $1, cont);
+
+ $$ = var_block($$);
+ old_state();
+}
+
+DO_WHILE : T_DO {new_state();} CODEBLOCK "while" '(' EXPRESSION ')' {
+ $$ = code_new();
+ code_t*loopstart = $$ = abc_label($$);
+ $$ = code_append($$, $3);
+ code_t*cont = $$ = abc_nop($$);
+ $$ = code_append($$, $6.c);
+ $$ = abc_iftrue($$, loopstart);
+ code_t*out = $$ = abc_nop($$);
+ breakjumpsto($$, $1, out);
+ continuejumpsto($$, $1, cont);
+
+ $$ = var_block($$);
+ old_state();
+}