From 0446b0ae6b692e4943f7d9681e0911fc5458ba45 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Mon, 5 May 2025 17:15:52 -0400 Subject: [PATCH] Added some notes and fixed the bp --- src/grammar.y | 88 ++++++++++++++++------------ tests/sprint3/test/sp3_if_else.alpha | 13 ++-- tests/sprint4/test/sp4_cg_fib.alpha | 34 +++++++++++ 3 files changed, 88 insertions(+), 47 deletions(-) create mode 100644 tests/sprint4/test/sp4_cg_fib.alpha diff --git a/src/grammar.y b/src/grammar.y index 6fd864c..354f19d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -133,12 +133,12 @@ prototype: include_list: include_statement include_list | include_statement - ; + ; include_statement: - INCLUDE C_STRING + INCLUDE C_STRING ; @@ -214,17 +214,17 @@ definition: if(type_of_param_type == TYPE_FUNCTION_TYPE){ CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); - + } if(type_of_param_type == TYPE_ARRAY_TYPE){ CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); - + } if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter))==undefined; // throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} - + } } } else { @@ -248,13 +248,13 @@ definition: printdebug("undefined type of parameter inside record"); CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); - + } else { if(type_of_param_type == TYPE_FUNCTION_DECLARATION){ printdebug("function declaration of parameter inside record"); CreateEntry(cur, TYPE_FUNCTION_DECLARATION, getTypeEntry(entry),NULL, getAdInfo(entry)); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition."); - + } if(type_of_param_type == TYPE_ARRAY){ printdebug("array type of parameter inside record"); @@ -271,7 +271,7 @@ definition: printdebug("record type of parameter inside record"); CreateEntry(cur, TYPE_RECORD, getTypeEntry(entry),NULL, getAdInfo(entry)); //throw_error(ERROR_TYPE, "Duplicate defination of parameter in function definition.");} - } + } /*printdebug("creating entry of type %s for function", getType(entry)); CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ @@ -304,7 +304,7 @@ function_declaration: FUNCTION ID COLON ID { if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){ - //printf("%s\n",$2); + //printf("%s\n",$1); //printf("%s\n",getName(table_lookup(cur, $4))); if (CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false,NULL)) == undefined) { throw_error(ERROR_TYPE, "Duplicate defination of function in function declaration"); @@ -489,7 +489,7 @@ declaration: if(CreateEntry(cur,d,(TableNode*)$1,$3,NULL) == undefined){ throw_error(ERROR_TYPE, "Duplicate defination of function in declaration list"); - } + } } else if(d == TYPE_ARRAY_TYPE){ @@ -620,19 +620,27 @@ compound_statement: S_Push(TrueList, S_Init(), 0); S_Push(FalseList, S_Init(), 0); }expression R_PAREN THEN { - emit_label(label_gen()); + int l = label_gen(); + emit_conditional_jump(E_IF_X_TRUE, l, tn_or_const(NODE, $4)); + Stack * t = S_Peek(TrueList); + S_Push(t, current, 1); + + emit_goto(0); + t = S_Peek(FalseList); + S_Push(t, current, 1); + emit_label(l); emit_backpatch(S_Pop(TrueList), getLabel(current)); } sblock ELSE { // NOTE we are not going back to int l = label_gen(); emit_backpatch(S_Pop(FalseList), l); - S_Push(FalseList, S_Init(), 0); + S_Push(stack, S_Init(), 0); emit_goto(0); - S_Push(S_Peek(FalseList), current, 1); + S_Push(S_Peek(stack), current, 1); emit_label(l); } sblock { int l = label_gen(); - emit_backpatch(S_Pop(FalseList), l); + emit_backpatch(S_Pop(stack), l); emit_label(l); if ($8 == undefined && $11 != undefined) { $$ = $11; @@ -665,21 +673,22 @@ compound_statement: simple_statement: - assignable ASSIGN expression + assignable{ + S_Push(TrueList, S_Init(), 0); + S_Push(FalseList, S_Init(), 0); + } ASSIGN expression { // ---------------------------------------------------------------------------- emit_label(label_gen()); emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_backpatch(S_Pop(FalseList), getLabel(current)); // ---------------------------------------------------------------------------- - printdebug("simple statement"); TableNode* node; if((getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_FUNCTION_TYPE)|| (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_ARRAY_TYPE)|| (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_RECORD_TYPE)|| (getAdInfoType((getTypeEntry((TableNode*)$1))) == TYPE_PRIMITIVE_TYPE)){ - node = ((TableNode*)$1); } else { //printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1)))); @@ -689,28 +698,22 @@ simple_statement: //printf("%s\n\n", getType(getTypeEntry((TableNode*)$3))); node = undefined; } - - - if(getAdInfoType(node) == getAdInfoType((TableNode*)$3)){ - emit_assignment($1, tn_or_const(NODE, $3)); - printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3)); - } else if (getTypeEntry(getTypeEntry(node)) == arrayprim && getTypeEntry((TableNode*)$3) == addr) { - emit_assignment($1, tn_or_const(NODE, $3)); - printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3)); - } else if (getTypeEntry(getTypeEntry(node)) == recprime && getTypeEntry((TableNode*)$3) == addr) { - emit_assignment($1, tn_or_const(NODE, $3)); - printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$3)); - } - - - else { + if(getAdInfoType(node) == getAdInfoType((TableNode*)$4)){ + emit_assignment($1, tn_or_const(NODE, $4)); + printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4)); + } else if (getTypeEntry(getTypeEntry(node)) == arrayprim && getTypeEntry((TableNode*)$4) == addr) { + emit_assignment($1, tn_or_const(NODE, $4)); + printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4)); + } else if (getTypeEntry(getTypeEntry(node)) == recprime && getTypeEntry((TableNode*)$4) == addr) { + emit_assignment($1, tn_or_const(NODE, $4)); + printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getType(node), getType((TableNode*)$4)); + } else { //printf("%d\n",getAdInfoType((TableNode*)$1)); //printf("%d\n",getAdInfoType((TableNode*)$3)); //printf("%d\n",getAdInfoType((TableNode*)$1)); //printf("%d\n",getAdInfoType((TableNode*)$3)); - throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$3), getType((TableNode*)$3)); + throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$4), getType((TableNode*)$4)); } - $$ = undefined; } @@ -1020,9 +1023,10 @@ expression: char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); // ---------------------------------------------------------------------------- - emit_label(label_gen()); - emit_backpatch(S_Pop(TrueList), getLabel(current)); + // emit_label(label_gen()); + // emit_backpatch(S_Pop(TrueList), getLabel(current)); 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){ @@ -1037,6 +1041,7 @@ 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) { @@ -1054,9 +1059,8 @@ expression: char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); // ---------------------------------------------------------------------------- - emit_label(label_gen()); - emit_backpatch(S_Pop(TrueList), getLabel(current)); emit_binary_op(E_EQUAL_TO, 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){ @@ -1071,6 +1075,9 @@ expression: S_Push(FalseList, t, 1); } S_Push(t, current, 1); + */ + // emit_label(label_gen()); + // emit_backpatch(S_Pop(TrueList), getLabel(current)); // ---------------------------------------------------------------------------- printdebug("equals check expression"); if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { @@ -1089,6 +1096,7 @@ expression: } | L_PAREN expression R_PAREN + // TODO: We need to check if we need to backpatch here. { printdebug("paren expression. current type is %s",getType((TableNode*)$2)); $$=$2; @@ -1143,7 +1151,7 @@ expression: } cur = getParent(cur); - + /*TableNode * t = getFirstEntry(cur); TableNode * n = look_up(cur, $2); @@ -1419,6 +1427,7 @@ assignable: char* temp = temp_var_gen(); int t = 6; //emission + // arr._1 .... $$ = CreateEntry(cur,t, integ, temp, NULL); } } @@ -1452,6 +1461,7 @@ assignable: } TableNode* node = CreateEntry(cur,t, type, temp, NULL); + // getElementOffset(rec, ID) //NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK) //emit_field_access(char* node, char* record, $3) $$=node; diff --git a/tests/sprint3/test/sp3_if_else.alpha b/tests/sprint3/test/sp3_if_else.alpha index e8f4bd7..40c2580 100644 --- a/tests/sprint3/test/sp3_if_else.alpha +++ b/tests/sprint3/test/sp3_if_else.alpha @@ -12,15 +12,12 @@ bar (r,s) := { } entry (arg) := { - [ integer: result ; rec: w] - if ( result = result ) then { - if ( result < w.y ) then { - result := 8; - } else { - result := 9; - }(* *) + [ integer: r ; integer: s; Boolean: x] + x := (r < s) & x; + if ( r < s ) then { + r := 5; } else { - result := bar('c', 7); + r := 7; } return 0; } diff --git a/tests/sprint4/test/sp4_cg_fib.alpha b/tests/sprint4/test/sp4_cg_fib.alpha new file mode 100644 index 0000000..df740d1 --- /dev/null +++ b/tests/sprint4/test/sp4_cg_fib.alpha @@ -0,0 +1,34 @@ +(* TEST: [-asc -tc -cg -ir] *) + +#include "std.alpha" + + +type M : string -> integer +function entry : M + +type fib: integer -> integer +function Fib : fib + +Fib(i) := { + [ Boolean: a ; Boolean: b ; Boolean: c] + if( i = 0 ) then { + return 7; + } else { + i := i; + } + b := b | (a & c); + b := 2 < 3; + if(i = 1) then { + return 1; + } else { + return i + Fib(i - 1); + } +} + +entry (arg) := { + [ integer: x; integer: y ] + x := 2; + x := Fib(2); + y := printInteger(Fib(2)); + return 1; +}