diff --git a/src/grammar.y b/src/grammar.y index 3431a18..3eaa991 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -544,11 +544,15 @@ compound_statement statement_list { compound_statement: WHILE L_PAREN { + S_Push(TrueList, S_Init(), 0); + S_Push(FalseList, S_Init(), 0); } expression R_PAREN { emit_label(label_gen()); + emit_backpatch(S_Pop(TrueList), getLabel(current)); } sblock { $$ = $7; emit_label(label_gen()); + emit_backpatch(S_Pop(FalseList), getLabel(current)); } | IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());} sblock ELSE {emit_label(label_gen());} @@ -837,7 +841,20 @@ expression: | expression LESS_THAN expression { emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); + Stack * t = S_Peek(TrueList); + if(t==NULL){ + t = S_Init(); + S_Push(TrueList, t, 1); + } + S_Push(t, current, 1); + S_Push(S_Peek(TrueList), current, 1); emit_goto(0); + t = S_Peek(FalseList); + if(t==NULL){ + t = S_Init(); + S_Push(FalseList, t, 1); + } + S_Push(t, current, 1); printdebug("less than expression"); if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) { char* temp = temp_var_gen(); diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 8f22de3..27ea353 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -57,6 +57,12 @@ int S_Size(Stack *s){ } return s->size; } + +void emit_backpatch(Stack * s, int l){ + for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ + i->label = l; + } +} //_______________________________________________________________________ char * temp = NULL; @@ -72,6 +78,7 @@ void emit_push_all(Stack * s){ for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ current->next = i; i->prev = current; + i->index = current->index + 1; current = i; current->next = NULL; } diff --git a/src/intermediate_code.h b/src/intermediate_code.h index c85c2bc..f31bcc8 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -164,3 +164,4 @@ void set_label(Instruction * i, int label); bool isConst(TNodeOrConst * tnc); int label_gen(); void backpatch(Stack *s, int l); +void emit_backpatch(Stack *s, int l);