diff --git a/src/grammar.y b/src/grammar.y index ef5bbcf..374a3dd 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -603,6 +603,21 @@ compound_statement: emit_label(*l); S_Push(stack, l, 2); } expression R_PAREN { + emit_backpatch(S_Pop(TrueList), getLabel(current)); + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $4)); + Stack * t = S_Peek(TrueList); + if(t==NULL){ + t = S_Init(); + S_Push(TrueList, t, 1); + } + S_Push(t, 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); emit_label(label_gen()); emit_backpatch(S_Pop(TrueList), getLabel(current)); } sblock { @@ -616,8 +631,23 @@ compound_statement: S_Push(TrueList, S_Init(), 0); S_Push(FalseList, S_Init(), 0); }expression R_PAREN THEN { + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $4)); + Stack * t = S_Peek(TrueList); + if(t==NULL){ + t = S_Init(); + S_Push(TrueList, t, 1); + } + S_Push(t, 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); emit_label(label_gen()); emit_backpatch(S_Pop(TrueList), getLabel(current)); + } sblock ELSE { // NOTE we are not going back to int l = label_gen(); @@ -805,8 +835,10 @@ expression: if(getTypeEntry((TableNode*)$2) == boo) { char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); - emit_unary_op(E_NOT,node,tn_or_const(NODE,$2)); - //NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK) + Stack * t = S_Pop(TrueList); + Stack * f = S_Pop(FalseList); + S_Push(TrueList, f, 0); + S_Push(FalseList, t, 0); //result of unary operation $$ = node; } else { @@ -917,7 +949,12 @@ expression: | expression LESS_THAN expression { - emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); +// ---------------------------------------------------------------------------- + char* temp = temp_var_gen(); + TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); + emit_binary_op(E_LESS_THAN, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); + /* + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node)); Stack * t = S_Peek(TrueList); if(t==NULL){ t = S_Init(); @@ -931,15 +968,12 @@ expression: 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(); - TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); $$ = node; } else if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==boo){ - char* temp = temp_var_gen(); - TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); - emit_binary_op(E_LESS_THAN,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); $$ = node; }else { $$=undefined; @@ -953,6 +987,8 @@ expression: if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); + emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); + /* emit_conditional_jump(E_EQUAL_TO, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); Stack * t = S_Peek(TrueList); if(t==NULL){ @@ -967,6 +1003,7 @@ expression: S_Push(FalseList, t, 1); } S_Push(t, current, 1); + */ $$ = node; } else { diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 44c6016..c807096 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -58,6 +58,11 @@ int S_Size(Stack *s){ return s->size; } +void S_Merge(Stack *s1, Stack *s2){ + for (Instruction * i = S_Pop(s1); i; i = S_Pop(s1)){ + S_Push(s2, i, 1); + } +} void emit_backpatch(Stack * s, int l){ for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ i->label = l; @@ -501,7 +506,7 @@ void emit_as_file(FILE * out_file, Instruction * i){ break; case E_IF_X_TRUE: fprintf(out_file, - "%4.d: if %s GOTO %d\n", + "%4.d: if %s True GOTO %d\n", i->index, get_string(i->operand1), i->label @@ -509,7 +514,7 @@ void emit_as_file(FILE * out_file, Instruction * i){ break; case E_IF_X_FALSE: fprintf(out_file, - "%4.d: if %s false GOTO %d\n", + "%4.d: if %s False GOTO %d\n", i->index, get_string(i->operand1), i->label @@ -518,21 +523,21 @@ void emit_as_file(FILE * out_file, Instruction * i){ case E_LESS_THAN: // this feels wrong I need to TODO: this fprintf(out_file, - "%4.d: if ( %s < %s ) GOTO %d\n", + "%4.d: %s = %s < %s\n", i->index, + getName(i->result), get_string(i->operand1), - get_string(i->operand2), - i->label + get_string(i->operand2) ); break; case E_EQUAL_TO: // this feels wrong I need to TODO: this fprintf(out_file, - "%4.d: if ( %s = %s ) GOTO %d\n", + "%4.d: %s = %s == %s\n", i->index, + getName(i->result), get_string(i->operand1), - get_string(i->operand2), - i->label + get_string(i->operand2) ); break; case E_CALL: