updated to change strings to nodes in most locations

This commit is contained in:
Partho
2025-04-04 20:24:05 -04:00
parent 5e749eb1ac
commit f6abbbd67f
3 changed files with 327 additions and 229 deletions

View File

@ -85,7 +85,8 @@ typedef enum {
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8,
TYPE_RECORD = 9,
TYPE_ARRAY = 10
TYPE_ARRAY = 10,
TYPE_SYSTEM_DEFINED = 11 // for system defined entries like funprimetype etc.
} types;
@ -314,7 +315,7 @@ int getRecSize(SymbolTable *tn) {
"passed in NULL SymbolTable for getRecSize. Invalid");
return -1;
}
int s = 0;
int s = 1;
TableNode *cur = getFirstEntry(tn);
if (cur != NULL) {
while (getNextEntry(cur) != NULL) {
@ -551,6 +552,7 @@ SymbolTable *init(SymbolTable *start) {
prime->theType = NULL;
prime->additionalinfo = NULL;
prime->next = NULL;
prime->tag = TYPE_SYSTEM_DEFINED;
// not sure exatly how to get array types to look right so using a dummy
// Table Node below and updating the print symbol table function to
@ -560,7 +562,8 @@ SymbolTable *init(SymbolTable *start) {
arrayprim->theName = "array";
arrayprim->theType = NULL;
arrayprim->additionalinfo = NULL;
prime->next = NULL;
arrayprim->next = NULL;
prime->tag = TYPE_SYSTEM_DEFINED;
// funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
@ -570,6 +573,7 @@ SymbolTable *init(SymbolTable *start) {
funprime->theType = NULL;
funprime->additionalinfo = NULL;
funprime->next = NULL;
funprime->tag = TYPE_SYSTEM_DEFINED;
// record
recprime = (TableNode *)malloc(sizeof(TableNode));
@ -577,18 +581,21 @@ SymbolTable *init(SymbolTable *start) {
recprime->theType = NULL;
recprime->additionalinfo = NULL;
recprime->next = NULL;
recprime->tag = TYPE_SYSTEM_DEFINED;
funtypeprime = (TableNode *)malloc(sizeof(TableNode));
funtypeprime->theName = "primitive function type";
funtypeprime->theType = NULL;
funtypeprime->additionalinfo = NULL;
funtypeprime->next = NULL;
funtypeprime->tag = TYPE_SYSTEM_DEFINED;
undefined = (TableNode *)malloc(sizeof(TableNode));
undefined->theName = "undefined";
undefined->theType = NULL;
undefined->additionalinfo = NULL;
undefined->next = NULL;
undefined->tag = TYPE_SYSTEM_DEFINED;
// Undefined_function_type_info = CreateFunctionTypeInfo(undefined,
// undefined);
@ -605,11 +612,17 @@ SymbolTable *init(SymbolTable *start) {
// be the size of these primitive types. We can change these if needed
// to not be hard coded numbers as a reminder, stri below is defined as
// a one dimensional array of characters
integ->additionalinfo = CreatePrimitiveInfo(4);
addr->additionalinfo = CreatePrimitiveInfo(8);
chara->additionalinfo = CreatePrimitiveInfo(1);
integ->additionalinfo = CreatePrimitiveInfo(SIZE_INT);
addr->additionalinfo = CreatePrimitiveInfo(SIZE_ADDR);
chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR);
stri->additionalinfo = CreateArrayInfo(1, chara);
boo->additionalinfo = CreatePrimitiveInfo(1);
boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL);
integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr
chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara
stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri
boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo
// addr->additionalinfo = CreatePrimitiveInfo(8);
start->Line_Number = 1;
@ -661,12 +674,33 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
return tn;
}
AdInfo *getAdInfo(TableNode *tn) {
if (tn == NULL) {
printdebug("passed a NULL table entry to getAdInfo. Invalid.");
return NULL;
}
if (tn == undefined) {
printdebug(
"passed an undefined table entry to getAdInfo. Invalid.");
return NULL;
}
if (tn->additionalinfo == NULL) {
printdebug(
"no additional info found in the table node. Invalid.");
return NULL;
}
return tn->additionalinfo;
}
//simplified getAdInfoType
int getAdInfoType(TableNode *tn) {
if (tn == NULL) {
printdebug(
"passing in NULL table entry to getAdInfoType. Invalid");
return -1;
}
return tn->tag;
/*
if (tn == undefined) {
printdebug("passing in undefined table entry to getAdInfoType. "
"Invalid");
@ -725,11 +759,10 @@ int getAdInfoType(TableNode *tn) {
"passed in an entry that is not a primitive type, array, "
"or record. Invalid.");
return TYPE_FUNCTION_DECLARATION;
}
}*/
}
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
AdInfo *ad) {
TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) {
if (table == NULL) {
printdebug("Null reference to table");
@ -752,7 +785,14 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
return undefined;
}
TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
if(tag<1 && tag>11){
printdebug("Note- not passing in valid 'tag' identifier to create entry function. Setting tag to undefined");
newEntry->tag = TYPE_UNDEFINED;
} else{
newEntry->tag = tag;
}
newEntry->theType = typeOf /*topDef*/;
newEntry->theName = id;
newEntry->additionalinfo = ad;
@ -769,6 +809,8 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
}
}
TableNode *getTypeEntry(TableNode *tn) {
if (tn == NULL) {
printdebug("passed a NULL table entry to getType");
@ -853,9 +895,9 @@ TableNode *addName(TableNode *tn, char *str) {
return undefined;
}
if (tn->theName != NULL) {
printdebug(
"Name doesn't look like it is empty before you change. "
"Are you sure you need to update name?");
//printdebug(
//"Name doesn't look like it is empty before you change. "
//"Are you sure you need to update name?");
if (str != NULL) {
tn->theName = str;
return tn;
@ -1011,178 +1053,177 @@ 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 == NULL) {
printdebug(
"%s[FATAL] passed in NULL table to print_symbol_table",
COLOR_RED);
return;
}
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation");
}
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,
"-------------------------:--------:--------:------------------"
"--------:------------------------------\n");
int parant_scope = 0;
int current_scope = 0;
if (table->Parent_Scope != NULL) {
parant_scope = getParent(table)->Line_Number * 1000 +
getParent(table)->Column_Number;
current_scope =
table->Line_Number * 1000 + table->Column_Number;
} else {
current_scope = 1001;
}
if (entrie == NULL) {
fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "",
current_scope, parant_scope, "", "Empty Scope");
}
for (; entrie != NULL; entrie = getNextEntry(entrie)) {
if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
if (parant_scope == 0) {
TableNode *entrie = table->entries;
fprintf(file_ptr, "-----------------:--------:--------:----------------"
"------:---------"
"--------------------\n");
int parant_scope = 0;
int current_scope = 0;
if (table->Parent_Scope != NULL) {
parant_scope = getParent(table)->Line_Number * 1000 +
getParent(table)->Column_Number;
current_scope =
table->Line_Number * 1000 + table->Column_Number;
} else {
current_scope = 1001;
}
if (entrie == NULL) {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
current_scope, parant_scope, "", "Empty Scope");
}
for (; entrie != NULL; entrie = getNextEntry(entrie)) {
if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : : %-21d -> "
"%-21s: %-28s\n",
entrie->theName, current_scope,
entrie->additionalinfo->ArrayAdInfo
->numofdimensions,
entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName,
"Type of Array");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21d -> %-21s: "
"%-28s\n",
entrie->theName, current_scope,
parant_scope,
entrie->additionalinfo->ArrayAdInfo
->numofdimensions,
entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName,
"Type of Array");
}
}
if (getAdInfoType(entrie) == TYPE_RECORD) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : :%-21s: "
"elements-%-28d\n",
entrie->theName, current_scope,
"record",
entrie->additionalinfo->RecAdInfo
->numofelements);
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s: "
"elements-%-28d\n",
entrie->theName, current_scope,
parant_scope, "record",
entrie->additionalinfo->RecAdInfo
->numofelements);
}
}
if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
if (parant_scope == 0) {
fprintf(
file_ptr,
"%-17s: %06d : :%-21s: size-%-28d "
"bytes\n",
entrie->theName, current_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
} else {
fprintf(
file_ptr,
"%-17s: %06d : %06d : %-21s: size-%-28d "
"bytes\n",
fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: "
"%-30s\n",
entrie->theName, current_scope,
parant_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (parant_scope == 0) {
entrie->additionalinfo->ArrayAdInfo
->numofdimensions,
entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName,
"Type of Array");
} else {
fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: "
"%-30s\n",
entrie->theName, current_scope,
parant_scope,
entrie->additionalinfo->ArrayAdInfo
->numofdimensions,
entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName,
"Type of Array");
}
}
if (getAdInfoType(entrie) == TYPE_RECORD) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : : %-21s -> "
"%-21s: %-28s\n",
entrie->theName, current_scope,
entrie->additionalinfo->FunTypeAdInfo
->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo
->returntype->theName,
"Type of Function");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s -> %-21s: "
"%-28s\n",
entrie->theName, current_scope,
parant_scope,
entrie->additionalinfo->FunTypeAdInfo
->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo
->returntype->theName,
"Type of Function");
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-25s: %06d : : %-25s: "
"elements-%-30d\n",
entrie->theName, current_scope,
"record",
entrie->additionalinfo->RecAdInfo
->numofelements);
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: "
"elements-%-30d\n",
entrie->theName, current_scope,
parant_scope, "record",
entrie->additionalinfo->RecAdInfo
->numofelements);
}
}
if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n",
entrie->theName, current_scope,
getType(entrie), "User Defined");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope,
parant_scope, getType(entrie),
"User Defined");
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) {
fprintf(
file_ptr,
"%-25s: %06d : : %-25s: size-%d "
"bytes\n",
entrie->theName, current_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
} else {
fprintf(
file_ptr,
"%-25s: %06d : %06d : %-25s: size-%-30d "
"bytes\n",
entrie->theName, current_scope,
parant_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n",
entrie->theName, current_scope,
"undefined", "undefined entry");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope,
parant_scope, "undefined",
"undefined entry");
}
}
}
if (getChildren(table) != NULL) {
ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) {
if ((node->table) == NULL) {
print_symbol_table(node->table, file_ptr);
} else {
if ((node->table)->Line_Number == -1) {
continue;
} else {
print_symbol_table(node->table,
file_ptr);
}
}
}
}
if (getParent(table) == NULL) {
fprintf(file_ptr, "-----------------:--------:--------:--------"
"--------------:-------"
"----------------------\n");
}
fprintf(file_ptr,
"%-25s: %06d : : %-25s -> "
"%-25s: %-30s\n",
entrie->theName, current_scope,
entrie->additionalinfo->FunTypeAdInfo
->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo
->returntype->theName,
"Type of Function");
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s -> %-21s: "
"%-30s\n",
entrie->theName, current_scope,
parant_scope,
entrie->additionalinfo->FunTypeAdInfo
->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo
->returntype->theName,
"Type of Function");
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope,
getType(entrie), "User Defined");
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope,
parant_scope, getType(entrie),
"User Defined");
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope,
"undefined", "undefined entry");
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope,
parant_scope, "undefined",
"undefined entry");
}
}
}
if (getChildren(table) != NULL) {
ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) {
if ((node->table) == NULL) {
print_symbol_table(node->table, file_ptr);
} else {
if ((node->table)->Line_Number == -1) {
continue;
} else {
print_symbol_table(node->table,
file_ptr);
}
}
}
}
if (getParent(table) == NULL) {
fprintf(file_ptr,
"-------------------------:--------:--------:----------"
"----------------:------------------------------\n");
}
}
// get top most symbol table
SymbolTable *getAncestor(SymbolTable *table) {