From 36694a2f4ae815e0bb2827fd198aab4272b79a4c Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Fri, 28 Mar 2025 13:47:53 -0400 Subject: [PATCH] updated lookups to return undefined entry if invalid --- src/grammar.y | 34 +++++++++++++-------- src/symbol_table.c | 34 ++++++++++++++------- tests/sprint2/test/test_carls_mistake.alpha | 4 +-- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 6b2b70b..7ff44da 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -201,60 +201,70 @@ expression: $$=strdup("undefined");}else{$$=$2;}} | NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined"); - printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}} + printf("mismatch at line %d and column %d. Invalid type being negated is %s\n", + @1.first_line,@1.first_column,$2);}} | expression ADD expression {printf("add expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression SUB_OR_NEG expression {printf("sub or neg expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression MUL expression {printf("multiply expression\n"); if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression DIV expression {printf("divide expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression REM expression {printf("remainder expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression AND expression {printf("AND expression\n");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression OR expression {printf("OR\n");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression LESS_THAN expression {printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column); + else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n", + @2.first_line,@2.first_column,$1,$3); $$=strdup("undefined");}} | expression EQUAL_TO expression {printf("equals check expression\n"); if(strcmp($1,$3)==0){$$=strdup("Boolean");} else if((strcmp($1,"array")==0||strcmp($1,"record")==0|| strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");} - else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}} + else{printf("mismatch at line %d and column %d. Invalid types %s and %s\n", + @2.first_line,@2.first_column,$1,$3);$$=strdup("undefined");}} - | assignable {printf("assignable expression\n");$$=$1;} + | assignable {printf("assignable expression. current type is %s\n",$1);$$=$1;} - | L_PAREN expression R_PAREN {printf("paren expression\n");$$=$2;} + | L_PAREN expression R_PAREN {printf("paren expression. current type is %s\n",$2);$$=$2;} | memOp assignable {$$ = strdup("address");} ; diff --git a/src/symbol_table.c b/src/symbol_table.c index 59a27c9..dabcafa 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -18,6 +18,7 @@ TableNode *stri; TableNode *boo; TableNode *recprime; TableNode *funtypeprime; +TableNode *undefined; typedef enum { // First 4 below are primitive types that are all encapsulated in @@ -228,7 +229,7 @@ AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) { // gets the line at which the function was first defined. (Can be used to print // out in table if needed) int getStartLine(TableNode *definition) { - if (strcmp(getType(definition), "function primitive") != 0) { + if (strcmp(getType(definition), "primitive function") != 0) { printf("not checking the start line of a function -- invalid " "op\n"); return 0; @@ -247,7 +248,7 @@ TableNode* setStartLine(TableNode* tn, int start){ // checks if "as" keyword was used for function definition. Either 0 or 1 for // not used or used. bool getAsKeyword(TableNode *definition) { - if (strcmp(getType(definition), "function primitive") != 0) { + if (strcmp(getType(definition), "primitive function") != 0) { printf("not checking if a function is called with as or not -- " "invalid op\n"); return NULL; @@ -275,7 +276,7 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) { } // returns parameter type of a function TableNode *getParameter(TableNode *definition) { - if (strcmp(getType(definition), "function type primitive") != 0) { + if (strcmp(getType(definition), "primitive function type") != 0) { printf( "not checking the parameter of a function -- invalid op\n"); return NULL; @@ -284,7 +285,7 @@ TableNode *getParameter(TableNode *definition) { } // returns return type of a function TableNode *getReturn(TableNode *definition) { - if (strcmp(getType(definition), "function type primitive") != 0) { + if (strcmp(getType(definition), "primitive function type") != 0) { printf("not checking the return of a function -- invalid op\n"); return NULL; } @@ -394,6 +395,12 @@ SymbolTable *init(SymbolTable *start) { funtypeprime->additionalinfo = NULL; funtypeprime->next = NULL; + undefined = (TableNode *)malloc(sizeof(TableNode)); + undefined->theName = "undefined"; + undefined->theType = NULL; + undefined->additionalinfo = NULL; + undefined->next = NULL; + integ->theType = prime; addr->theType = prime; chara->theType = prime; @@ -522,24 +529,26 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, char *getType(TableNode *tn) { if(tn == NULL){ printf("passed a NULL table entry to getType\n"); - return ""; + return getName(undefined); } if(tn->theType == NULL){ printf("type of entry is currently NULL, undefined type \n"); - return ""; + return getName(undefined); } return tn->theType->theName; } + char *getName(TableNode *tn) { if(tn == NULL){ printf("passed a NULL table entry to getName\n"); - return ""; + return undefined->theName; } if(tn->theName == NULL){ printf("name of entry is currently NULL, undefined \n"); - return ""; + return undefined->theName; } return tn->theName; } + int getLine(SymbolTable *st) { return st->Line_Number; } int getColumn(SymbolTable *st) { return st->Column_Number; } TableNode* addName(TableNode *tn, char* str){ @@ -609,17 +618,20 @@ TableNode *table_lookup(SymbolTable *table, char *x) { return entrie; } } - return NULL; + return undefined; } //check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { - return NULL; + printf("passed in empty scope. error.\n"); + return undefined; } TableNode *ret = table_lookup(table, x); - if (ret != NULL) { + if (ret != NULL && ret != undefined) { return ret; } + printf("could not find %s in scope that started at line %d and column %d so moving up a scope\n" + ,x,getLine(table),getColumn(table)); return look_up(table->Parent_Scope, x); } diff --git a/tests/sprint2/test/test_carls_mistake.alpha b/tests/sprint2/test/test_carls_mistake.alpha index fb75ca3..f2b3703 100644 --- a/tests/sprint2/test/test_carls_mistake.alpha +++ b/tests/sprint2/test/test_carls_mistake.alpha @@ -18,10 +18,10 @@ bar2 as (r,s) := { entry(arg) := { [ integer: result ; rec: w] result := foo(5); - w := reserve(w); (* see types.alpha – reserve returns a value of type address, which can be assigned to array and record variables*) + w := reserve w; (* see types.alpha – reserve returns a value of type address, which can be assigned to array and record variables*) w.x := 5; w.y := 7; result := bar1(w); (* pass w (a rec type value) to bar1 *) result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *) return 0; -} \ No newline at end of file +}