From 4468dbd02f653c35d89dddc8dff747b6c1f4622e Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 21 Feb 2025 09:32:06 -0500 Subject: [PATCH] Fixed some functionality in symbol_table and added print_symbole_table t#29 --- print_symbol_table.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ symbol_table.c | 48 ++++++++++++++++------------------ 2 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 print_symbol_table.c diff --git a/print_symbol_table.c b/print_symbol_table.c new file mode 100644 index 0000000..91393e8 --- /dev/null +++ b/print_symbol_table.c @@ -0,0 +1,62 @@ +#include +#include "symbol_table.h" +#include "symbol_table.c" + +void print_symbol_table(SymbolTable *table, FILE *file_ptr){ + if(table->Parent_Scope == NULL){ + fprintf(file_ptr, "%-17s:%-8s:%-8s:%-22s:%-29s\n", "NAME", "SCOPE", "PARANT", "TYPE", "Extra annotation"); + } + TableNode * entrie = table->entries; + fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n"); + int parant_scope = 0; + int current_scope = 0; + if(table->Parent_Scope != NULL){ + parant_scope = table->Parent_Scope->Line_Number*1000 + table->Parent_Scope->Column_Number; + current_scope = table->Line_Number*1000 + table->Column_Number; + } else { + current_scope = 1001; + } + + for(; entrie != NULL; entrie = entrie->next){ + fprintf(file_ptr, "%-17s: %06d : %06d :%-22s:%-29s\n", + entrie->theName, current_scope, parant_scope, + entrie->theType, "Extra annotation"); + } + if (table->Children_Scope != NULL){ + ListOfTable* node = table->Children_Scope; + for(; node != NULL; node = node->next){ + print_symbol_table(node->table, file_ptr); + } + } + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n"); + } +} +/* + +int main(void){ + SymbolTable * parant = CreateScope(NULL, 1,1); + // Value* v = calloc(1, sizeof(Value)); + CreateEntry(parant, &"primitive", &"Boolean", NULL, 0); + CreateEntry(parant, &"primitive", &"character", NULL, 0); + CreateEntry(parant, &"primitive", &"integer", NULL, 0); + CreateEntry(parant, &"1 -> character", &"string", NULL, 0); + CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); + CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); + CreateEntry(parant, &"int2int", &"square", NULL, 0); + CreateEntry(parant, &"string2int", &"entry", NULL, 0); + SymbolTable * child = CreateScope(parant, 14,14); + CreateEntry(child, &"integer", &"x", NULL, 0); + SymbolTable * second = CreateScope(parant, 14,14); + CreateEntry(second, &"string", &"arg", NULL, 0); + CreateEntry(second, &"integer", &"input", NULL, 0); + CreateEntry(second, &"integer", &"expected", NULL, 0); + CreateEntry(second, &"integer", &"actual", NULL, 0); + CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); + SymbolTable * third = CreateScope(second, 33,44); + CreateEntry(third, &"BOO", &"arg", NULL, 0); + CreateEntry(third, &"YAZOO", &"input", NULL, 0); + print_symbol_table(parant, stderr); + return 0; +} +*/ diff --git a/symbol_table.c b/symbol_table.c index 2c3f3a2..0e4c3d0 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -30,7 +30,7 @@ typedef struct TableNode{ //this next value is an int for string types to tell you how far to traverse a buffer for the string int StrLength; }TableNode; - + typedef struct SymbolTable{ TableNode* entries; struct SymbolTable* Parent_Scope; @@ -40,7 +40,7 @@ typedef struct SymbolTable{ }SymbolTable; */ - + SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); table->Line_Number = Line; @@ -66,31 +66,27 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){ } return table; } - - -TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ - if(table->entries == NULL){ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; - return newEntry; - } else{ - TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); - newEntry->theType = typeOf; - newEntry->theName = id; - newEntry->value = value; - newEntry->StrLength = StringLength; - TableNode* oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; - return newEntry; - } - } -//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation -/* + +TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ + TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); + newEntry->theType = typeOf; + newEntry->theName = id; + newEntry->value = value; + newEntry->StrLength = StringLength; + if(table->entries == NULL){ + table->entries = newEntry; + return newEntry; + } else{ + TableNode* oldEntry = table->entries; + table->entries = newEntry; + newEntry->next = oldEntry; + return newEntry; + } +} + +//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation +/* int main(){ char* String = "STRING"; char* X = "X";