From 33347f305197dcb715ee05bfb715fdfb55bbbcf5 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 1 May 2025 20:55:07 -0400 Subject: [PATCH] It works for if and while statments. Number of params not going well --- src/grammar.y | 105 ++++++++++++--------- src/intermediate_code.c | 4 +- tests/sprint2/test/sp2_llnode.alpha | 1 - tests/sprint3/test/sp3_multiple_args.alpha | 9 +- 4 files changed, 69 insertions(+), 50 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 3eaa991..dbe97b7 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -542,37 +542,48 @@ compound_statement statement_list { compound_statement: - WHILE L_PAREN -{ - S_Push(TrueList, S_Init(), 0); - S_Push(FalseList, S_Init(), 0); -} expression R_PAREN { - emit_label(label_gen()); - emit_backpatch(S_Pop(TrueList), getLabel(current)); -} sblock { - $$ = $7; - emit_label(label_gen()); - emit_backpatch(S_Pop(FalseList), getLabel(current)); -} - | 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) { + WHILE L_PAREN { + S_Push(TrueList, S_Init(), 0); + S_Push(FalseList, S_Init(), 0); + int *l = calloc(1, sizeof(int)); + *l = label_gen(); + emit_label(*l); + S_Push(stack, l, 2); + } expression R_PAREN { + emit_label(label_gen()); + emit_backpatch(S_Pop(TrueList), getLabel(current)); + } sblock { $$ = $7; - } else if ($7 == $10) { - $$ = $7; - }else if((getAdInfoType((TableNode*)$7) == TYPE_ARRAY_TYPE) && ((TableNode*)$10)==addr){ - $$ = $7; - }else if((getAdInfoType((TableNode*)$7) == TYPE_RECORD_TYPE) && ((TableNode*)$10)==addr){ - $$ = $7; - }else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_ARRAY_TYPE)){ - $$ = $10; - }else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_RECORD_TYPE)){ - $$ = $10; + int l = label_gen(); + emit_backpatch(S_Pop(FalseList), l); + emit_goto(*(int*)(S_Pop(stack))); + emit_label(l); + } + | IF L_PAREN { + S_Push(TrueList, S_Init(), 0); + S_Push(FalseList, S_Init(), 0); + }expression R_PAREN THEN { + 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 { 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)); @@ -841,13 +852,12 @@ expression: | expression LESS_THAN expression { 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); + 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){ @@ -873,7 +883,19 @@ expression: char* temp = temp_var_gen(); 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)); + 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); $$ = node; } else { @@ -1022,15 +1044,8 @@ assignable: t= TYPE_UNDEFINED; 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); - int a = 0; -/* - if(S_IsEmpty(stack)){ - int a = S_Size(S_Peek(stack)); - } -*/ + int a = S_Size(S_Peek(stack)); emit_push_all(S_Peek(stack)); S_Pop(stack); emit_function_call(node, a, tn_or_const(NODE, $1)); diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 27ea353..d2868be 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -496,7 +496,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 GOTO %d\n", i->index, get_string(i->operand1), i->label @@ -504,7 +504,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 diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index dfec950..c78687d 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -15,7 +15,6 @@ function make_list : list make_list (a) := { [integer:orig_a; llnode: ret; llnode: curr; llnode: temp] - if (a < 0 | a = 0) then { return null; } else { diff --git a/tests/sprint3/test/sp3_multiple_args.alpha b/tests/sprint3/test/sp3_multiple_args.alpha index 8475d92..f069230 100644 --- a/tests/sprint3/test/sp3_multiple_args.alpha +++ b/tests/sprint3/test/sp3_multiple_args.alpha @@ -12,7 +12,12 @@ bar (r,s) := { entry (arg) := { [ integer: result ; rec: w] - result := bar(1,2); + while ( result = result ) { + while ( result < w.y ) { + result := 8; + } + result := 9; + } result := bar('c', 7); return 0; -} \ No newline at end of file +}