From f217500b5517a92e532d01fbdc49cdad9976f521 Mon Sep 17 00:00:00 2001 From: Scarlett Date: Fri, 28 Mar 2025 16:11:42 -0400 Subject: [PATCH] removed most seg faults --- Makefile | 9 +++- src/grammar.y | 6 +-- src/symbol_table.c | 101 +++++++++++++++++++++++---------------------- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 815aec5..729ca3d 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ CFLAGS := YACC := bison TESTS-S1 := $(wildcard tests/sprint1/test/*.alpha) +TESTS-S3 := $(wildcard tests/sprint3/test/*.alpha) TESTS-S2 := $(wildcard tests/sprint2/test/*.alpha) compiler: clean runner @@ -32,13 +33,19 @@ runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o debug: CFLAGS += -DDEBUG=1 debug: clean compiler -test: test-s1 test-s2 +test: test-s1 test-s3 test-s2 test-s1: chmod +x ./check.sh $(foreach test, $(TESTS-S1), ./$(EXE) -tok $(test);) ./check.sh + +test-s3: + chmod +x ./check.sh + $(foreach test, $(TESTS-S3), ./$(EXE) -st $(test);) + ./check.sh + test-s2: chmod +x ./check.sh $(foreach test, $(TESTS-S2), ./$(EXE) -st $(test);) diff --git a/src/grammar.y b/src/grammar.y index b464641..5238aac 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -246,10 +246,10 @@ simple_statement: ; assignable: - ID {$$ = $1; } + ID {$$ = getType(look_up(cur,$1));} | assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));} - | assignable rec_op ID {if(undefined != (tn = table_lookup(getRecList(look_up(cur, $1)), $3))) - {$$ = getType(tn);}} + | assignable rec_op ID {if(undefined != table_lookup(getRecList(look_up(cur, $1)), $3)){ + {$$ = getName(table_lookup(getRecList(look_up(cur, $1)), $3));}};} ; rec_op : diff --git a/src/symbol_table.c b/src/symbol_table.c index 123c6a8..4b31b1e 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -105,7 +105,7 @@ AdInfo *CreatePrimitiveInfo(int size) { // only gets the size of a primitive type int getPrimSize(TableNode *definition) { - if (definition == NULL){ + if (definition == NULL || definition == undefined){ printf("passed an NULL entry to getPrimSize function. Invalid.\n"); return -1; } @@ -143,7 +143,7 @@ AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) { } // This gets the number of dimensions from array info int getNumArrDim(TableNode *definition) { - if (definition == NULL){ + if (definition == NULL|| definition == undefined){ printf("passed an NULL entry to getNumArrDim function. Invalid.\n"); return -1; } @@ -158,7 +158,7 @@ int getNumArrDim(TableNode *definition) { TableNode *getArrType(TableNode *definition) { if (strcmp(getType(definition), "array") != 0) { printf("not checking the type of an array -- invalid op\n"); - return NULL; + return undefined; } return definition->additionalinfo->ArrayAdInfo->typeofarray; } @@ -196,9 +196,9 @@ SymbolTable *getRecList(TableNode *definition) { } TableNode* setRecSize(TableNode* tn, int n){ - if(tn == NULL){ + if(tn == NULL||tn==undefined){ printf("passed in NULL entry for setRecSize. Invalid\n"); - return NULL; + return undefined; } tn->additionalinfo->RecAdInfo->numofelements = n; return tn; @@ -215,12 +215,6 @@ int getRecSize(SymbolTable* tn){ } // below function takes a bool to see if parameter should be decomposed or not -assignable: - ID {$$ = $1; } - | assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));} - | assignable rec_op ID {TableNode * tn; if(NULL != (tn = table_lookup(getRecList(look_up(cur, $1)), $3))) - {$$ = getType(tn);} - } ;// note that functions only take one input and have one output // using "as" the input record can be decomposed to give the illusion of // multiple inputs Below function also has the line number where the function is @@ -245,9 +239,9 @@ int getStartLine(TableNode *definition) { } TableNode* setStartLine(TableNode* tn, int start){ - if(tn == NULL){ - printf("passing in a NULL entry to setStartLine. invalid\n"); - return NULL; + if(tn == NULL||tn==undefined){ + printf("passing in a NULL or undefined entry to setStartLine. invalid\n"); + return undefined; } tn->additionalinfo->FunDecAdInfo->startlinenumber = start; return tn; @@ -258,15 +252,15 @@ bool getAsKeyword(TableNode *definition) { if (strcmp(getType(definition), "primitive function") != 0) { printf("not checking if a function is called with as or not -- " "invalid op\n"); - return NULL; + return 0; } return definition->additionalinfo->FunDecAdInfo->regularoras; } TableNode* setAsKeyword(TableNode* tn, bool as){ - if(tn == NULL){ - printf("passing in a NULL entry to setAsKeyword. invalid\n"); - return NULL; + if(tn == NULL||tn==undefined){ + printf("passing in a NULL or undefined entry to setAsKeyword. invalid\n"); + return undefined; } tn->additionalinfo->FunDecAdInfo->regularoras = as; return tn; @@ -286,7 +280,7 @@ TableNode *getParameter(TableNode *definition) { if (strcmp(getType(definition), "primitive function type") != 0) { printf( "not checking the parameter of a function -- invalid op\n"); - return NULL; + return undefined; } return definition->additionalinfo->FunTypeAdInfo->parameter; } @@ -294,7 +288,7 @@ TableNode *getParameter(TableNode *definition) { TableNode *getReturn(TableNode *definition) { if (strcmp(getType(definition), "primitive function type") != 0) { printf("not checking the return of a function -- invalid op\n"); - return NULL; + return undefined; } return definition->additionalinfo->FunTypeAdInfo->returntype; } @@ -444,17 +438,17 @@ TableNode* recprime; TableNode* funtypeprime; */ TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ - if(tn == NULL){ - printf("passed in an invalid table node to modify (NULL).\n"); - return NULL; + if(tn == NULL||tn==undefined){ + printf("passed in an invalid table node to modify (NULL or undefined).\n"); + return undefined; } - if(type == NULL){ - printf("passed in a NULL type reference to populate a table node. Invalid.\n"); - return NULL; + if(type == NULL||type==undefined){ + printf("passed in a NULL or undefined type reference to populate a table node. Invalid.\n"); + return undefined; } if(info == NULL){ printf("passed in a NULL info reference to populate a table node. Invalid.\n"); - return NULL; + return undefined; } tn->theType = type; tn->additionalinfo = info; @@ -463,12 +457,12 @@ TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ } int getAdInfoType(TableNode* tn){ - if(tn == NULL){ - printf("passing in NULL table entry. Invalid\n"); + if(tn == NULL||tn==undefined){ + printf("passing in NULL or undefined table entry. Invalid\n"); return -1; } - if(tn->theType == NULL){ - printf("Entry being passed in has a null reference for theType. Invalid.\n"); + if(tn->theType == NULL|| tn->theType == undefined){ + printf("Entry being passed in has a null or undefined reference for theType. Invalid.\n"); return -1; } if(strcmp(getType(tn),getName(integ))==0){ @@ -508,7 +502,7 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, if (table == NULL) { printf("Null reference to table"); - return NULL; + return undefined; } /* TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); @@ -517,11 +511,11 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, return NULL; } */ - if (typeOf == NULL) { - printf("This is not pointing to a proper definition\n"); - return NULL; + if (typeOf == NULL||typeOf == undefined) { + printf("This is not pointing to a proper definition (either NULL or undefined)\n"); + return undefined; } - TableNode *newEntry = (TableNode *)malloc(sizeof(TableNode)); + TableNode *newEntry = (TableNode *)calloc(1,sizeof(TableNode)); newEntry->theType = typeOf /*topDef*/; newEntry->theName = id; newEntry->additionalinfo = ad; @@ -537,23 +531,23 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } char *getType(TableNode *tn) { - if(tn == NULL){ - printf("passed a NULL table entry to getType\n"); + if(tn == NULL||tn==undefined){ + printf("passed a NULL or undefined table entry to getType\n"); return getName(undefined); } - if(tn->theType == NULL){ - printf("type of entry is currently NULL, undefined type \n"); + if(tn->theType == NULL|| tn->theType == undefined){ + printf("type of entry is currently NULL or undefined type \n"); return getName(undefined); } return tn->theType->theName; } char *getName(TableNode *tn) { - if(tn == NULL){ - printf("passed a NULL table entry to getName\n"); + if(tn == NULL||tn==undefined){ + //printf("passed a NULL or undefined table entry to getName\n"); return undefined->theName; } if(tn->theName == NULL){ - printf("name of entry is currently NULL, undefined \n"); + //printf("name of entry is currently NULL, undefined \n"); return undefined->theName; } return tn->theName; @@ -562,10 +556,20 @@ char *getName(TableNode *tn) { int getLine(SymbolTable *st) { return st->Line_Number; } int getColumn(SymbolTable *st) { return st->Column_Number; } TableNode* addName(TableNode *tn, char* str){ - if(tn == NULL){ - printf("passed a Null table node to the addName function. Invalid./n"); - return tn; + if(tn == NULL||tn==undefined){ + printf("passed a Null or undefined table node to the addName function. Invalid./n"); + return undefined; } + if(tn->theName != NULL){ + printf("Name doesn't look like it is empty before you change. Are you sure you need to update name?/n"); + return undefined; + } + if(str == NULL){ + printf("passed a NULL string to the addName function. Invalid./n"); + return undefined; + } + tn->theName = str; + return tn; } SymbolTable* setLineNumber(SymbolTable *st,int line){ @@ -647,7 +651,6 @@ TableNode *look_up(SymbolTable *table, char *x) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) { - return; if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation"); @@ -811,11 +814,11 @@ bool typeCheck(char *firstID, char *secondID) { TableNode *entry1 = look_up(cur, firstID); TableNode *entry2 = look_up(cur, secondID); - if (entry1 == NULL) { + if (entry1 == NULL|| entry1 == undefined) { printf("first type not defined\n"); return false; } - if (entry2 == NULL) { + if (entry2 == NULL|| entry2 == undefined) { printf("second type not defined\n"); return false; }