From fac92f62f731f7fa5a4f10d5dcfd384be1c4fb64 Mon Sep 17 00:00:00 2001 From: Scarlett Date: Mon, 31 Mar 2025 21:18:21 -0400 Subject: [PATCH] segfaults fixed. need to verify what is being passed to functions. --- src/symbol_table.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index a4cee49..e68c886 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -968,10 +968,24 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { }*/ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { + if (table == NULL) { + printdebug( + "%s[FATAL] passed in NULL table to print_symbol_table", + COLOR_RED); + return; + } + + if (table->Parent_Scope != NULL) { + printdebug("%s[WARNING] passed in a non-top level scope to " + "print_symbol_table", + COLOR_ORANGE); + } + if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation"); } + TableNode *entrie = table->entries; fprintf(file_ptr, "-----------------:--------:--------:----------------" "------:---------" @@ -1213,7 +1227,19 @@ bool typeCheck(char *firstID, char *secondID) { return false; } -SymbolTable *getParent(SymbolTable *st) { return st->Parent_Scope; } +SymbolTable *getParent(SymbolTable *st) { + if (st == NULL) { + printdebug("passed a NULL symbol table to getParent function. " + "Invalid."); + return NULL; + } + if (st->Parent_Scope == NULL) { + printdebug("passed a top level scope to getParent function. " + "Invalid."); + return NULL; + } + return st->Parent_Scope; +} ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; } SymbolTable *getFirstChild(ListOfTable *lt) { return lt->table; } @@ -1222,17 +1248,16 @@ TableNode *getFirstEntry(SymbolTable *st) { return st->entries; } // Segfaults when passed an invalid table node! TableNode *getNextEntry(TableNode *tn) { - if (tn == NULL) { + if (tn == NULL) { printdebug("passed a NULL table node to getNextEntry"); return undefined; - } - if (tn == undefined) { + } + if (tn == undefined) { printdebug("passed an undefined table node to getNextEntry"); return undefined; - } - return tn->next; } - + return tn->next; +} // uncomment the below main function along with the headers above for a simple // standalone test of table and entry creation