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 6434b7dfeb
commit 2ed5372761
2 changed files with 58 additions and 16 deletions

View File

@ -550,6 +550,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 {
@ -563,8 +578,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();
@ -752,8 +782,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 {
@ -864,7 +896,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();
@ -878,15 +915,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;
@ -900,6 +934,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){
@ -914,6 +950,7 @@ expression:
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
*/
$$ = node;
} else {

View File

@ -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: