Merge branch 'Dev' into Sprint2-Partho_Symbol_Tabke-FE-t#NoTask

This commit is contained in:
Moroseui
2025-03-10 12:02:54 -04:00
committed by GitHub
2 changed files with 112 additions and 108 deletions

View File

@ -9,33 +9,34 @@
char * typey = "type"; char * typey = "type";
char * funy = "function"; char * funy = "function";
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) { SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable)); SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
table->Line_Number = Line; table->Line_Number = Line;
table->Column_Number = Column; table->Column_Number = Column;
table->Parent_Scope = ParentScope; table->Parent_Scope = ParentScope;
table->Children_Scope = NULL; table->Children_Scope = NULL;
table->entries = NULL; table->entries = NULL;
if (ParentScope != NULL) { if (ParentScope != NULL) {
if (ParentScope->Children_Scope == NULL) { if (ParentScope->Children_Scope == NULL) {
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
newEntry->next = NULL; newEntry->next = NULL;
// newEntry->prev = NULL; // newEntry->prev = NULL;
newEntry->table = table; newEntry->table = table;
ParentScope->Children_Scope = newEntry; ParentScope->Children_Scope = newEntry;
} else { } else {
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
// newEntry->prev = NULL; // newEntry->prev = NULL;
newEntry->table = table; newEntry->table = table;
ListOfTable* oldEntry = ParentScope->Children_Scope; ListOfTable* oldEntry = ParentScope->Children_Scope;
ParentScope->Children_Scope = newEntry; ParentScope->Children_Scope = newEntry;
newEntry->next = oldEntry; newEntry->next = oldEntry;
} }
} }
return table; return table;
} }
//create entry just for things below top level scope //create entry just for things below top level scope
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) { TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
if(table ==NULL || table->Parent_Scope == NULL){ if(table ==NULL || table->Parent_Scope == NULL){
printf("Null reference to table given for create entry or given top level scope which is invalid\n"); printf("Null reference to table given for create entry or given top level scope which is invalid\n");
return NULL; return NULL;
@ -90,78 +91,82 @@ if(table_lookup(table,id) != NULL){
newEntry->next = oldEntry; newEntry->next = oldEntry;
return newEntry; return newEntry;
} }
} }
TableNode* table_lookup(SymbolTable* table, char* x) { TableNode* table_lookup(SymbolTable* table, char* x) {
TableNode* entrie = table->entries; TableNode* entrie = table->entries;
for (; entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = entrie->next) {
if (!strcmp(entrie->theName, x)) { if (!strcmp(entrie->theName, x)) {
return entrie; return entrie;
} }
} }
return NULL; return NULL;
} }
TableNode* look_up(SymbolTable* table, char* x) { TableNode* look_up(SymbolTable* table, char* x) {
if (table == NULL) { if (table == NULL) {
return NULL; return NULL;
} }
TableNode* ret = table_lookup(table, x); TableNode* ret = table_lookup(table, x);
if (ret != NULL) { if (ret != NULL) {
return ret; return ret;
} }
return look_up(table->Parent_Scope, x); return look_up(table->Parent_Scope, x);
} }
void print_symbol_table(SymbolTable* table, FILE* file_ptr) { void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE",
"PARENT", "TYPE", "Extra annotation"); "PARENT", "TYPE", "Extra annotation");
} }
TableNode* entrie = table->entries; TableNode* entrie = table->entries;
fprintf(file_ptr, fprintf(file_ptr,
"-----------------:--------:--------:----------------------:---------" "-----------------:--------:--------:----------------------:---------"
"--------------------\n"); "--------------------\n");
int parant_scope = 0; int parant_scope = 0;
int current_scope = 0; int current_scope = 0;
if (table->Parent_Scope != NULL) { if (table->Parent_Scope != NULL) {
parant_scope = table->Parent_Scope->Line_Number * 1000 + parant_scope = table->Parent_Scope->Line_Number * 1000 +
table->Parent_Scope->Column_Number; table->Parent_Scope->Column_Number;
current_scope = table->Line_Number * 1000 + table->Column_Number; current_scope = table->Line_Number * 1000 + table->Column_Number;
} else { } else {
current_scope = 1001; current_scope = 1001;
} }
if ( entrie == NULL ) {
for (; entrie != NULL; entrie = entrie->next) { fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
if (parant_scope == 0) { current_scope, parant_scope, "", "Empty Scope");
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", }
entrie->theName, current_scope, entrie->theType, for (; entrie != NULL; entrie = entrie->next) {
"Extra annotation"); if (parant_scope == 0) {
} else { fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, entrie->theName, current_scope, entrie->theType,
current_scope, parant_scope, entrie->theType, "Extra annotation"); "Extra annotation");
} } else {
} fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
if (table->Children_Scope != NULL) { current_scope, parant_scope, entrie->theType, "Extra annotation");
ListOfTable* node = table->Children_Scope; }
for (; node != NULL; node = node->next) { }
print_symbol_table(node->table, file_ptr); if (table->Children_Scope != NULL) {
} ListOfTable* node = table->Children_Scope;
} for (; node != NULL; node = node->next) {
if (table->Parent_Scope == NULL) { print_symbol_table(node->table, file_ptr);
fprintf(file_ptr, }
"-----------------:--------:--------:----------------------:-------" }
"----------------------\n"); if (table->Parent_Scope == NULL) {
} fprintf(file_ptr,
"-----------------:--------:--------:----------------------:-------"
"----------------------\n");
}
} }
SymbolTable* getAncestor(SymbolTable* table) { SymbolTable* getAncestor(SymbolTable* table) {
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
// if table has no parent, return itself // if table has no parent, return itself
return table; return table;
} else { } else {
// call function recursively to grab ancestor // call function recursively to grab ancestor
return getAncestor(table->Parent_Scope); return getAncestor(table->Parent_Scope);
} }
} }
SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; } SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; }
@ -179,15 +184,15 @@ int getColumn(SymbolTable* st) { return st->Column_Number; }
// standalone test of table and entry creation // standalone test of table and entry creation
/* /*
int main(){ int main(){
char* String = "STRING"; char* String = "STRING";
char* X = "X"; char* X = "X";
SymbolTable* Second = CreateScope(NULL, 2,2); SymbolTable* Second = CreateScope(NULL, 2,2);
printf("Line number is %d, Column number of scope is printf("Line number is %d, Column number of scope is
%d\n",Second->Line_Number,Second->Column_Number); TableNode* First_Entry = %d\n",Second->Line_Number,Second->Column_Number); TableNode* First_Entry =
CreateEntry(Second,String,X); CreateEntry(Second,String,X);
printf("The type of the first entry is %s\n",First_Entry->theType); printf("The type of the first entry is %s\n",First_Entry->theType);
return 0; return 0;
} }
*/ */

View File

@ -4,24 +4,23 @@
#include <string.h> #include <string.h>
typedef struct ListOfTable { typedef struct ListOfTable {
struct SymbolTable* table; struct SymbolTable* table;
// struct ListOfTable* prev; // struct ListOfTable* prev;
struct ListOfTable* next; struct ListOfTable* next;
} ListOfTable; } ListOfTable;
typedef struct TableNode { typedef struct TableNode {
char* theType; char* theType;
char* theName; char* theName;
struct TableNode* next; struct TableNode* next;
} TableNode; } TableNode;
typedef struct SymbolTable { typedef struct SymbolTable {
TableNode* entries; TableNode* entries;
struct SymbolTable* Parent_Scope; struct SymbolTable* Parent_Scope;
struct ListOfTable* Children_Scope; struct ListOfTable* Children_Scope;
int Line_Number; int Line_Number;
int Column_Number; int Column_Number;
} SymbolTable; } SymbolTable;
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
@ -40,4 +39,4 @@ TableNode* getNextEntry(TableNode* tn);
char* getType(TableNode* tn); char* getType(TableNode* tn);
char* getName(TableNode* tn); char* getName(TableNode* tn);
int getLine(SymbolTable* st); int getLine(SymbolTable* st);
int getColumn(SymbolTable* st); int getColumn(SymbolTable* st);