It works for if and while statments. Number of params not going well

This commit is contained in:
Meyer Simon
2025-05-01 20:55:07 -04:00
parent c2132ddd00
commit 33347f3051
4 changed files with 69 additions and 50 deletions

View File

@ -542,37 +542,48 @@ compound_statement statement_list {
compound_statement: compound_statement:
WHILE L_PAREN WHILE L_PAREN {
{ S_Push(TrueList, S_Init(), 0);
S_Push(TrueList, S_Init(), 0); S_Push(FalseList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0); int *l = calloc(1, sizeof(int));
} expression R_PAREN { *l = label_gen();
emit_label(label_gen()); emit_label(*l);
emit_backpatch(S_Pop(TrueList), getLabel(current)); S_Push(stack, l, 2);
} sblock { } expression R_PAREN {
$$ = $7; emit_label(label_gen());
emit_label(label_gen()); emit_backpatch(S_Pop(TrueList), getLabel(current));
emit_backpatch(S_Pop(FalseList), getLabel(current)); } sblock {
}
| IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());}
sblock ELSE {emit_label(label_gen());}
sblock {
/*
*/
if ($7 == undefined && $10 != undefined) {
$$ = $10;
} else if ($7 != undefined && $10 == undefined) {
$$ = $7; $$ = $7;
} else if ($7 == $10) { int l = label_gen();
$$ = $7; emit_backpatch(S_Pop(FalseList), l);
}else if((getAdInfoType((TableNode*)$7) == TYPE_ARRAY_TYPE) && ((TableNode*)$10)==addr){ emit_goto(*(int*)(S_Pop(stack)));
$$ = $7; emit_label(l);
}else if((getAdInfoType((TableNode*)$7) == TYPE_RECORD_TYPE) && ((TableNode*)$10)==addr){ }
$$ = $7; | IF L_PAREN {
}else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_ARRAY_TYPE)){ S_Push(TrueList, S_Init(), 0);
$$ = $10; S_Push(FalseList, S_Init(), 0);
}else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_RECORD_TYPE)){ }expression R_PAREN THEN {
$$ = $10; emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock ELSE {
int l = label_gen();
emit_backpatch(S_Pop(FalseList), l);
emit_label(l);
} sblock {
if ($8 == undefined && $11 != undefined) {
$$ = $11;
} else if ($8 != undefined && $11 == undefined) {
$$ = $8;
} else if ($8 == $11) {
$$ = $8;
}else if((getAdInfoType((TableNode*)$8) == TYPE_ARRAY_TYPE) && ((TableNode*)$11)==addr){
$$ = $8;
}else if((getAdInfoType((TableNode*)$8) == TYPE_RECORD_TYPE) && ((TableNode*)$11)==addr){
$$ = $8;
}else if(((TableNode*)$8)==addr && (getAdInfoType((TableNode*)$11) == TYPE_ARRAY_TYPE)){
$$ = $11;
}else if(((TableNode*)$8)==addr && (getAdInfoType((TableNode*)$11) == TYPE_RECORD_TYPE)){
$$ = $11;
} else { } else {
printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column);
//printf("%s\n", getName((TableNode*)$6)); //printf("%s\n", getName((TableNode*)$6));
@ -841,13 +852,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)); emit_conditional_jump(E_LESS_THAN, 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){
t = S_Init(); t = S_Init();
S_Push(TrueList, t, 1); S_Push(TrueList, t, 1);
} }
S_Push(t, current, 1); S_Push(t, current, 1);
S_Push(S_Peek(TrueList), current, 1);
emit_goto(0); emit_goto(0);
t = S_Peek(FalseList); t = S_Peek(FalseList);
if(t==NULL){ if(t==NULL){
@ -873,7 +883,19 @@ expression:
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_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);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, 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);
$$ = node; $$ = node;
} else { } else {
@ -1022,15 +1044,8 @@ assignable:
t= TYPE_UNDEFINED; t= TYPE_UNDEFINED;
throw_error(ERROR_TYPE, "Undefined type returned by function."); throw_error(ERROR_TYPE, "Undefined type returned by function.");
} }
// TODO: add from stack
// TODO: emit_function_call
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL); TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
int a = 0; int a = S_Size(S_Peek(stack));
/*
if(S_IsEmpty(stack)){
int a = S_Size(S_Peek(stack));
}
*/
emit_push_all(S_Peek(stack)); emit_push_all(S_Peek(stack));
S_Pop(stack); S_Pop(stack);
emit_function_call(node, a, tn_or_const(NODE, $1)); emit_function_call(node, a, tn_or_const(NODE, $1));

View File

@ -496,7 +496,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 GOTO %d\n",
i->index, i->index,
get_string(i->operand1), get_string(i->operand1),
i->label i->label
@ -504,7 +504,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

View File

@ -15,7 +15,6 @@ function make_list : list
make_list (a) := { make_list (a) := {
[integer:orig_a; llnode: ret; llnode: curr; llnode: temp] [integer:orig_a; llnode: ret; llnode: curr; llnode: temp]
if (a < 0 | a = 0) then { if (a < 0 | a = 0) then {
return null; return null;
} else { } else {

View File

@ -12,7 +12,12 @@ bar (r,s) := {
entry (arg) := { entry (arg) := {
[ integer: result ; rec: w] [ integer: result ; rec: w]
result := bar(1,2); while ( result = result ) {
while ( result < w.y ) {
result := 8;
}
result := 9;
}
result := bar('c', 7); result := bar('c', 7);
return 0; return 0;
} }