From 24caa0e9a7217ad7272ef1828e9e4176dae1c63c Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 28 Mar 2025 10:18:18 -0400 Subject: [PATCH] Updeated the contents of st to Dev #t51 --- src/symbol_table.c | 89 ++++++++++++++++++++++++++++++++++++++-------- src/symbol_table.h | 4 +-- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index 205bcbe..4ce220b 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -27,23 +27,23 @@ typedef enum { // TYPE_BOOLEAN, // TYPE_ADDRESS, // Type String is an array of char enclosed in double quotes per lexer - TYPE_STRING, + TYPE_STRING = 1, // Array can be multidimensional. Information should be stored here - TYPE_ARRAY, + TYPE_ARRAY = 2, // Record is user defined types - TYPE_RECORD, + TYPE_RECORD = 3, // Declaring what type a particular function is without as - TYPE_FUNCTION_DECLARATION, + TYPE_FUNCTION_DECLARATION = 4, // Declaring what type a particular function is with as // TYPE_AS_FUNCTION_DECLARATION, // Declaring what type a function is (what the parameters and output // are) - TYPE_FUNCTION_TYPE, + TYPE_FUNCTION_TYPE = 5, // The Type being pointed to by the first 4 above that only stores the // size - TYPE_PRIMITIVE, + TYPE_PRIMITIVE = 6, //likely NULL - TYPE_ALL_ELSE + TYPE_ALL_ELSE = 7 } types; @@ -153,12 +153,12 @@ TableNode *getArrType(TableNode *definition) { // order, of what make up that type in an array. Unfortunately this second part // should probably instead be replaced by a reference to a scope in which those // elements are found. -AdInfo *CreateRecordInfo(int length, TableNode *typesarray) { +AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); info->RecAdInfo = (record_info *)malloc(sizeof(record_info)); info->RecAdInfo->numofelements = length; // replace below with reference to a scope, not an array - info->RecAdInfo->listoftypes = typesarray; + info->RecAdInfo->recordScope = recordScope; return info; } // This gets the number of elements that make up a record. @@ -172,13 +172,13 @@ int getRecLength(TableNode *definition) { return definition->additionalinfo->RecAdInfo->numofelements; } // This gets the array. Needs to up be updated to get the scope instead -TableNode *getRecList(TableNode *definition) { +SymbolTable *getRecList(TableNode *definition) { if (strcmp(getType(definition), "record") != 0) { printf("not checking the list of types of a record -- invalid " "op\n"); return NULL; } - return definition->additionalinfo->RecAdInfo->listoftypes; + return definition->additionalinfo->RecAdInfo->recordScope; } // below function takes a bool to see if parameter should be decomposed or not @@ -379,8 +379,34 @@ TableNode* boo; 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(type == NULL){ + printf("passed in a NULL type reference to populate a table node. Invalid.\n"); + return NULL; + } + if(info == NULL){ + printf("passed in a NULL info reference to populate a table node. Invalid.\n"); + return NULL; + } + tn->theType = type; + tn->additionalinfo = info; + //returning reference to modified table node + return tn; + } int getAdInfoType(TableNode* tn){ + if(tn == NULL){ + printf("passing in NULL table entry. Invalid\n"); + return -1; + } + if(tn->theType == NULL){ + printf("Entry being passed in has a null reference for theType. Invalid.\n"); + return -1; + } if(strcmp(getType(tn),getName(integ))==0){ return TYPE_PRIMITIVE; } @@ -443,11 +469,41 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } } -char *getType(TableNode *tn) { return tn->theType->theName; } +char *getType(TableNode *tn) { + if(tn == NULL){ + printf("passed a NULL table entry to getType\n"); + return ""; + } + if(tn->theType == NULL){ + printf("type of entry is currently NULL, undefined type \n"); + return ""; + } + return tn->theType->theName; } char *getName(TableNode *tn) { 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){ + if(tn == NULL){ + printf("passed a Null table node to the addName function. Invalid./n"); + return tn; + } +} +SymbolTable* setLineNumber(SymbolTable *st,int line){ + if(st == NULL){ + printf("passed a Null Symbol Table to the setLineNumber function. Invalid./n"); + return st; + } + st->Line_Number = line; + } + +SymbolTable* setColumnNumber(SymbolTable *st,int column){ + if(st == NULL){ + printf("passed a Null Symbol Table to the setColumnNumber function. Invalid./n"); + return st; + } + st->Line_Number = column; + } /* //we use false for type defs and true for functions for parameter of typeOf TableNode* Define(SymbolTable* table, bool typeOf, char* id) { @@ -483,6 +539,7 @@ names\n"); return NULL; } */ +//only check table that is given TableNode *table_lookup(SymbolTable *table, char *x) { TableNode *entrie = table->entries; for (; entrie != NULL; entrie = entrie->next) { @@ -492,6 +549,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) { } return NULL; } +//check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { return NULL; @@ -504,6 +562,8 @@ 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"); @@ -526,7 +586,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", current_scope, parant_scope, "", "Empty Scope"); } - for (; entrie != NULL; entrie = entrie->next) { + for (entrie != NULL; entrie = entrie->next;) { if (parant_scope == 0) { /*have to update*/ if (strcmp(entrie->theType->theName, "function primitive") || @@ -555,7 +615,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { "----------------------\n"); } } - +//get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { if (table->Parent_Scope == NULL) { // if table has no parent, return itself @@ -594,6 +654,7 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) { return scope; } +//almost certainly don't need to use the below function since type checking happens by passing types up the grammar bool typeCheck(char *firstID, char *secondID) { TableNode *entry1 = look_up(cur, firstID); diff --git a/src/symbol_table.h b/src/symbol_table.h index 84b3e5a..de37098 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -30,7 +30,7 @@ typedef struct { // similar to above we define a record to hold the number of elements // and an array of tablenodes (types) that it contains in the > int numofelements; - struct TableNode *listoftypes; + struct SymbolTable *recordScope; } record_info; typedef struct { @@ -91,7 +91,7 @@ int getPrimSize(TableNode *definition); int getNumArrDim(TableNode *definition); TableNode *getArrType(TableNode *definition); int getRecLength(TableNode *definition); -TableNode *getRecList(TableNode *definition); +SymbolTable *getRecList(TableNode *definition); int getStartLine(TableNode *definition); bool getAsKeyword(TableNode *definition); TableNode *getParameter(TableNode *definition);