From a4dc3d90be97b38e78677ee3ae9394c1e38e10ab Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 8 Apr 2025 16:01:36 -0400 Subject: [PATCH] update --- src/grammar.y | 67 +++++++++++++------ src/symbol_table.c | 8 ++- tests/sprint2/test/sp2_presidence.alpha | 6 +- .../sprint3/test/sp3_and_or_type_check.alpha | 3 + .../test/sp3_primitive_type_check.alpha | 23 +++++++ 5 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 tests/sprint3/test/sp3_primitive_type_check.alpha diff --git a/src/grammar.y b/src/grammar.y index 946c9ad..e70363d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -326,14 +326,14 @@ sblock: dblock: L_BRACKET - {if(getLine(cur)==0){ - setLineNumber(cur, @1.first_line); - setColumnNumber(cur,@1.first_line);} - else{ - cur = CreateScope(cur,@1.first_line,@1.first_column); + { + if (getLine(cur) == 0) { + setLineNumber(cur, @1.first_line); + setColumnNumber(cur,@1.first_line); + } else{ + cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? } - } - + } declaration_list R_BRACKET; @@ -391,22 +391,45 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp(getName((TableNode*)$1), getName((TableNode*)$3)) == 0) { - printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getType((TableNode*)$1), "array") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - } else if((strcmp(getType((TableNode*)$1), "record") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - } else if((strcmp(getType((TableNode*)$1), "function type primitive") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - // } else if () { + bool passCheck = false; + TableNode * left = (TableNode*)$1; + TableNode * right = (TableNode*)$3; + + printTableNode((TableNode*)$1); + printTableNode((TableNode*)$3); - // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { - // printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3); - } else { + if (strcmp(getType(right), "primitive") == 0) { + if (strcmp((getType(left)),(getName(right))) == 0) { + printdebug("%s[☺] Passed primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + } + + if(strcmp(getName(left), getName(right)) == 0) { + printdebug("Passed standard type check; assignable = expression"); + passCheck = true; + } + + if((strcmp(getType(left), "array") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + if((strcmp(getType(left), "record") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + if((strcmp(getType(left), "function type primitive") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + // Type check fails: + if (!passCheck) { printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); - printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType((TableNode*)$1), getType((TableNode*)$3), COLOR_WHITE); - printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType((TableNode*)$1)); + printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType(left), getType(right), COLOR_WHITE); + printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(left)); } } @@ -624,6 +647,8 @@ assignable: //char *funtype = getType(look_up(cur, $1)); printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); + + TableNode * typeNode = table_lookup(getAncestor(cur), getType((TableNode*)$1)); TableNode *param = getParameter(typeNode); printTableNode(param); diff --git a/src/symbol_table.c b/src/symbol_table.c index 86fca5b..debc4d5 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -913,7 +913,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-*s:%-*s:%-*s:%-*s:%-*s:\n", - col_widths[0], " NAME", + col_widths[0], "NAME", col_widths[1], " SCOPE", col_widths[2], " PARENT", col_widths[3], " TYPE", @@ -965,7 +965,11 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (parentScopeNum == 0) { st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Primitive", primAdInfo); + printdebug("%sTHIS ONE", COLOR_RED); + printTableNode(entry); + char *primType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(primType, " %s", getType(entry)); + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, primType, primAdInfo); } } diff --git a/tests/sprint2/test/sp2_presidence.alpha b/tests/sprint2/test/sp2_presidence.alpha index 2d60209..d60611d 100644 --- a/tests/sprint2/test/sp2_presidence.alpha +++ b/tests/sprint2/test/sp2_presidence.alpha @@ -1,8 +1,10 @@ type main: string -> integer +type rec: [integer: rec_x; integer: rec_y] + function entry: main entry (arg) := { - [integer:x] - x := 3 + 2 * 8; + [integer: arg_x; integer: arg_y; rec: arg_record; Boolean: arg_bool] + arg_x := 3 + 2 * 8; return 0; } diff --git a/tests/sprint3/test/sp3_and_or_type_check.alpha b/tests/sprint3/test/sp3_and_or_type_check.alpha index 450aee0..c01fc10 100644 --- a/tests/sprint3/test/sp3_and_or_type_check.alpha +++ b/tests/sprint3/test/sp3_and_or_type_check.alpha @@ -31,6 +31,9 @@ test (arg) := { b := b & 1; b := 1 & b; b := 1 = 1; + + + b := 1 = b; return 0; diff --git a/tests/sprint3/test/sp3_primitive_type_check.alpha b/tests/sprint3/test/sp3_primitive_type_check.alpha new file mode 100644 index 0000000..6980aba --- /dev/null +++ b/tests/sprint3/test/sp3_primitive_type_check.alpha @@ -0,0 +1,23 @@ +(* + Testing the following type checks: + - integer : primitive + - character : primitive + - boolean : primitive + + - address (not included, special case) +*) + + + +type main: string -> integer +function entry: main + +entry (arg) := { + [integer: i; address: add; character: char; Boolean: bool] + + i := 3 + 2 * 8; + char := 'a'; + bool := true; + + return 0; +}