updated get entry function to get string pointers instead of creating different strings. Also created a define function for top level scopes. Create Entry does not work for top level

This commit is contained in:
Partho Bhattacharya
2025-03-10 11:56:26 -04:00
parent abd24760e0
commit 2f04671123
4 changed files with 50 additions and 4 deletions

View File

@ -6,7 +6,8 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
char * typey = "type";
char * funy = "function";
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
table->Line_Number = Line;
@ -33,9 +34,19 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
return table;
}
//create entry just for things below top level scope
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
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");
return NULL;
}
TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
if(topDef == NULL){
printf("This type is not defined at the top level\n");
return NULL;
}
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theType = topDef->theName;
newEntry->theName = id;
if (table->entries == NULL) {
table->entries = newEntry;
@ -47,6 +58,40 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
return newEntry;
}
}
//we use false for type defs and true for functions for parameter of typeOf
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
if(table ==NULL || table->Parent_Scope != NULL){
printf("No valid table given for header defs\n");
return NULL;
}
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
//possible issues with referencing text instead of heap
if(typeOf == 0){
newEntry->theType = typey;
}
if (typeOf == 1){
newEntry->theType = funy;
}
if(table_lookup(table,id) != NULL){
printf("already defined at the top level, can't define duplicate names\n");
return NULL;
}
newEntry->theName = id;
if (table->entries == NULL) {
table->entries = newEntry;
return newEntry;
} else {
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
}
TableNode* table_lookup(SymbolTable* table, char* x) {
TableNode* entrie = table->entries;
for (; entrie != NULL; entrie = entrie->next) {