While list is backpatching

This commit is contained in:
Meyer Simon
2025-05-01 16:43:00 -04:00
parent 99dffaee01
commit c2132ddd00
3 changed files with 25 additions and 0 deletions

View File

@ -544,11 +544,15 @@ compound_statement statement_list {
compound_statement: compound_statement:
WHILE L_PAREN WHILE L_PAREN
{ {
S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0);
} expression R_PAREN { } expression R_PAREN {
emit_label(label_gen()); emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock { } sblock {
$$ = $7; $$ = $7;
emit_label(label_gen()); emit_label(label_gen());
emit_backpatch(S_Pop(FalseList), getLabel(current));
} }
| IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());} | IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());}
sblock ELSE {emit_label(label_gen());} sblock ELSE {emit_label(label_gen());}
@ -837,7 +841,20 @@ 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)); 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); 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"); 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(); char* temp = temp_var_gen();

View File

@ -57,6 +57,12 @@ int S_Size(Stack *s){
} }
return s->size; 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; char * temp = NULL;
@ -72,6 +78,7 @@ void emit_push_all(Stack * s){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
current->next = i; current->next = i;
i->prev = current; i->prev = current;
i->index = current->index + 1;
current = i; current = i;
current->next = NULL; current->next = NULL;
} }

View File

@ -164,3 +164,4 @@ void set_label(Instruction * i, int label);
bool isConst(TNodeOrConst * tnc); bool isConst(TNodeOrConst * tnc);
int label_gen(); int label_gen();
void backpatch(Stack *s, int l); void backpatch(Stack *s, int l);
void emit_backpatch(Stack *s, int l);