I fixed the way we are doing the evaluation of == and < Getting to &&, || next. Not sure if ! is correct

This commit is contained in:
Meyer Simon
2025-05-03 12:52:23 -04:00
parent 8eb9a62af8
commit cd72d46ec3
2 changed files with 58 additions and 16 deletions

View File

@ -603,6 +603,21 @@ compound_statement:
emit_label(*l); emit_label(*l);
S_Push(stack, l, 2); S_Push(stack, l, 2);
} expression R_PAREN { } 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_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock { } sblock {
@ -616,8 +631,23 @@ compound_statement:
S_Push(TrueList, S_Init(), 0); S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0); S_Push(FalseList, S_Init(), 0);
}expression R_PAREN THEN { }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_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock ELSE { } sblock ELSE {
// NOTE we are not going back to // NOTE we are not going back to
int l = label_gen(); int l = label_gen();
@ -805,8 +835,10 @@ expression:
if(getTypeEntry((TableNode*)$2) == boo) { if(getTypeEntry((TableNode*)$2) == boo) {
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
emit_unary_op(E_NOT,node,tn_or_const(NODE,$2)); Stack * t = S_Pop(TrueList);
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK) Stack * f = S_Pop(FalseList);
S_Push(TrueList, f, 0);
S_Push(FalseList, t, 0);
//result of unary operation //result of unary operation
$$ = node; $$ = node;
} else { } else {
@ -917,7 +949,12 @@ expression:
| expression LESS_THAN 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); Stack * t = S_Peek(TrueList);
if(t==NULL){ if(t==NULL){
t = S_Init(); t = S_Init();
@ -931,15 +968,12 @@ expression:
S_Push(FalseList, t, 1); S_Push(FalseList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
*/
// ----------------------------------------------------------------------------
printdebug("less than expression"); printdebug("less than expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) { 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; $$ = node;
} else if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==boo){ } 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; $$ = node;
}else { }else {
$$=undefined; $$=undefined;
@ -953,6 +987,8 @@ expression:
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); 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)); emit_conditional_jump(E_EQUAL_TO, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
Stack * t = S_Peek(TrueList); Stack * t = S_Peek(TrueList);
if(t==NULL){ if(t==NULL){
@ -967,6 +1003,7 @@ expression:
S_Push(FalseList, t, 1); S_Push(FalseList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
*/
$$ = node; $$ = node;
} else { } else {

View File

@ -58,6 +58,11 @@ int S_Size(Stack *s){
return s->size; 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){ void emit_backpatch(Stack * s, int l){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
i->label = l; i->label = l;
@ -501,7 +506,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break; break;
case E_IF_X_TRUE: case E_IF_X_TRUE:
fprintf(out_file, fprintf(out_file,
"%4.d: if %s GOTO %d\n", "%4.d: if %s True GOTO %d\n",
i->index, i->index,
get_string(i->operand1), get_string(i->operand1),
i->label i->label
@ -509,7 +514,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break; break;
case E_IF_X_FALSE: case E_IF_X_FALSE:
fprintf(out_file, fprintf(out_file,
"%4.d: if %s false GOTO %d\n", "%4.d: if %s False GOTO %d\n",
i->index, i->index,
get_string(i->operand1), get_string(i->operand1),
i->label i->label
@ -518,21 +523,21 @@ void emit_as_file(FILE * out_file, Instruction * i){
case E_LESS_THAN: case E_LESS_THAN:
// this feels wrong I need to TODO: this // this feels wrong I need to TODO: this
fprintf(out_file, fprintf(out_file,
"%4.d: if ( %s < %s ) GOTO %d\n", "%4.d: %s = %s < %s\n",
i->index, i->index,
getName(i->result),
get_string(i->operand1), get_string(i->operand1),
get_string(i->operand2), get_string(i->operand2)
i->label
); );
break; break;
case E_EQUAL_TO: case E_EQUAL_TO:
// this feels wrong I need to TODO: this // this feels wrong I need to TODO: this
fprintf(out_file, fprintf(out_file,
"%4.d: if ( %s = %s ) GOTO %d\n", "%4.d: %s = %s == %s\n",
i->index, i->index,
getName(i->result),
get_string(i->operand1), get_string(i->operand1),
get_string(i->operand2), get_string(i->operand2)
i->label
); );
break; break;
case E_CALL: case E_CALL: