segfaults fixed. print_symbol_table format updated for dynamic column width.

This commit is contained in:
Scarlett
2025-04-07 23:53:08 -04:00
parent e7ee370dcf
commit 87659ebf46
2 changed files with 978 additions and 961 deletions

6
.clang-format Normal file
View File

@ -0,0 +1,6 @@
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
AllowShortStringsOnSingleLine: true
BreakStringLiterals: false
ReflowComments: false

View File

@ -17,7 +17,6 @@ void printdebug_impl(char *file, int line, const char *format, ...) {
// primitive additional info only stores the size of that type
AdInfo *CreatePrimitiveInfo(int size) {
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
info->PrimAdInfo = (primitive_info *)malloc(sizeof(primitive_info));
info->PrimAdInfo->size = size;
@ -32,7 +31,8 @@ int getPrimSize(TableNode *definition) {
return -1;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getPrimSize function. "
printdebug(
"passed an undefined entry to getPrimSize function. "
"Invalid.");
return -1;
}
@ -53,17 +53,20 @@ int getPrimSize(TableNode *definition) {
// calculated at runtime so bounds checking only needs to be done then
AdInfo *CreateArrayInfo(int dim, TableNode *type) {
if (type == NULL) {
printdebug("passed a NULL reference to "
printdebug(
"passed a NULL reference to "
"CreateArrayInfo. Invalid.");
return NULL;
}
if (type == undefined) {
printdebug("passed an undefined reference to "
printdebug(
"passed an undefined reference to "
"CreateArrayInfo. Invalid.");
return NULL;
}
if (type == undefined) {
printdebug("passed a undefined type reference to "
printdebug(
"passed a undefined type reference to "
"CreateArrayInfo. Invalid.");
return NULL;
}
@ -78,12 +81,14 @@ AdInfo *CreateArrayInfo(int dim, TableNode *type) {
// This gets the number of dimensions from array info
int getNumArrDim(TableNode *definition) {
if (definition == NULL) {
printdebug("passed an NULL entry to getNumArrDim "
printdebug(
"passed an NULL entry to getNumArrDim "
"function. Invalid.");
return -1;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getNumArrDim "
printdebug(
"passed an undefined entry to getNumArrDim "
"function. Invalid.");
return -1;
}
@ -97,12 +102,14 @@ int getNumArrDim(TableNode *definition) {
// the entry of that type
TableNode *getArrType(TableNode *definition) {
if (definition == NULL) {
printdebug("passed an NULL entry to getArrType "
printdebug(
"passed an NULL entry to getArrType "
"function. Invalid.");
return NULL;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getArrType "
printdebug(
"passed an undefined entry to getArrType "
"function. Invalid.");
return NULL;
}
@ -130,12 +137,14 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
// anyways.
int getRecLength(TableNode *definition) {
if (definition == NULL) {
printdebug("passed an NULL entry to getRecLength "
printdebug(
"passed an NULL entry to getRecLength "
"function. Invalid.");
return -1;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getRecLength "
printdebug(
"passed an undefined entry to getRecLength "
"function. Invalid.");
return -1;
}
@ -149,12 +158,14 @@ int getRecLength(TableNode *definition) {
// This gets the array. Needs to up be updated to get the scope instead
SymbolTable *getRecList(TableNode *definition) {
if (definition == NULL) {
printdebug("passed a NULL entry to getRecList "
printdebug(
"passed a NULL entry to getRecList "
"function. Invalid.");
return NULL;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getRecList "
printdebug(
"passed an undefined entry to getRecList "
"function. Invalid.");
return NULL;
}
@ -215,12 +226,14 @@ AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) {
// out in table if needed)
int getStartLine(TableNode *definition) {
if (definition == NULL) {
printdebug("passed a NULL entry to getStartLine "
printdebug(
"passed a NULL entry to getStartLine "
"function. Invalid.");
return -1;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getStartLine "
printdebug(
"passed an undefined entry to getStartLine "
"function. Invalid.");
return -1;
}
@ -235,12 +248,14 @@ int getStartLine(TableNode *definition) {
TableNode *setStartLine(TableNode *tn, int start) {
if (tn == NULL) {
printdebug("passing in a NULL entry to setStartLine. "
printdebug(
"passing in a NULL entry to setStartLine. "
"invalid");
return undefined;
}
if (tn == undefined) {
printdebug("passing in an undefined entry to setStartLine. "
printdebug(
"passing in an undefined entry to setStartLine. "
"invalid");
return undefined;
}
@ -251,17 +266,20 @@ TableNode *setStartLine(TableNode *tn, int start) {
// not used or used.
bool getAsKeyword(TableNode *definition) {
if (definition == NULL) {
printdebug("passed a NULL entry to getAsKeyword "
printdebug(
"passed a NULL entry to getAsKeyword "
"function. Invalid.");
return false;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getAsKeyword "
printdebug(
"passed an undefined entry to getAsKeyword "
"function. Invalid.");
return false;
}
if (strcmp(getType(definition), "primitive function") != 0) {
printdebug("not checking if a function is called with as or "
printdebug(
"not checking if a function is called with as or "
"not (%s) -- "
"invalid op",
getType(definition));
@ -272,12 +290,14 @@ bool getAsKeyword(TableNode *definition) {
TableNode *setAsKeyword(TableNode *tn, bool as) {
if (tn == NULL) {
printdebug("passing in a NULL entry to setAsKeyword. "
printdebug(
"passing in a NULL entry to setAsKeyword. "
"invalid");
return undefined;
}
if (tn == undefined) {
printdebug("passing in an undefined entry to setAsKeyword. "
printdebug(
"passing in an undefined entry to setAsKeyword. "
"invalid");
return undefined;
}
@ -288,22 +308,26 @@ TableNode *setAsKeyword(TableNode *tn, bool as) {
// stores the type of a function (parameter type and return type)
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
if (parameter == NULL) {
printdebug("passed a NULL parameter to "
printdebug(
"passed a NULL parameter to "
"CreateFunctionTypeInfo. Invalid.");
return NULL;
}
if (parameter == undefined) {
printdebug("passed an undefined parameter to "
printdebug(
"passed an undefined parameter to "
"CreateFunctionTypeInfo. Invalid.");
return NULL;
}
if (returntype == NULL) {
printdebug("passed a NULL return type to "
printdebug(
"passed a NULL return type to "
"CreateFunctionTypeInfo. Invalid.");
return NULL;
}
if (returntype == undefined) {
printdebug("passed an undefined return type to "
printdebug(
"passed an undefined return type to "
"CreateFunctionTypeInfo. Invalid.");
return NULL;
}
@ -317,12 +341,14 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
// returns parameter type of a function
TableNode *getParameter(TableNode *definition) {
if (definition == NULL) {
printdebug("passed a NULL entry to getParameter "
printdebug(
"passed a NULL entry to getParameter "
"function. Invalid.");
return undefined;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getParameter "
printdebug(
"passed an undefined entry to getParameter "
"function. Invalid.");
return undefined;
}
@ -336,12 +362,14 @@ TableNode *getParameter(TableNode *definition) {
// returns return type of a function
TableNode *getReturn(TableNode *definition) {
if (definition == NULL) {
printdebug("passed a NULL entry to getReturn "
printdebug(
"passed a NULL entry to getReturn "
"function. Invalid.");
return NULL;
}
if (definition == undefined) {
printdebug("passed an undefined entry to getReturn "
printdebug(
"passed an undefined entry to getReturn "
"function. Invalid.");
return NULL;
}
@ -387,7 +415,8 @@ SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
// types
SymbolTable *init(SymbolTable *start) {
if (start->Parent_Scope != NULL) {
printdebug("%s[FATAL] Cannot initialize a scope that is not "
printdebug(
"%s[FATAL] Cannot initialize a scope that is not "
"the parent scope",
COLOR_RED);
return NULL;
@ -525,12 +554,14 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
return undefined;
}
if (type == NULL) {
printdebug("passed in a NULL type reference to "
printdebug(
"passed in a NULL type reference to "
"populate a table node. Invalid.");
return undefined;
}
if (type == undefined) {
printdebug("passed in an undefined type reference to "
printdebug(
"passed in an undefined type reference to "
"populate a table node. Invalid.");
return undefined;
}
@ -636,7 +667,6 @@ int getAdInfoType(TableNode *tn) {
TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
AdInfo *ad) {
if (table == NULL) {
printdebug("Null reference to table");
return undefined;
@ -660,7 +690,8 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
if (tag < 1 && tag > 11) {
printdebug("Note- not passing in valid 'tag' identifier to "
printdebug(
"Note- not passing in valid 'tag' identifier to "
"create entry function. Setting tag to undefined");
newEntry->tag = TYPE_UNDEFINED;
} else {
@ -740,7 +771,8 @@ char *getName(TableNode *tn) {
int getLine(SymbolTable *st) {
if (st == NULL) {
printdebug("passed a NULL symbol table to getLine function. "
printdebug(
"passed a NULL symbol table to getLine function. "
"Invalid.");
return -1;
}
@ -748,7 +780,8 @@ int getLine(SymbolTable *st) {
}
int getColumn(SymbolTable *st) {
if (st == NULL) {
printdebug("passed a NULL symbol table to getColumn function. "
printdebug(
"passed a NULL symbol table to getColumn function. "
"Invalid.");
return -1;
}
@ -756,12 +789,14 @@ int getColumn(SymbolTable *st) {
}
TableNode *addName(TableNode *tn, char *str) {
if (tn == NULL) {
printdebug("passed a Null table node to the addName "
printdebug(
"passed a Null table node to the addName "
"function. Invalid.");
return undefined;
}
if (tn == undefined) {
printdebug("passed an undefined table node to the addName "
printdebug(
"passed an undefined table node to the addName "
"function. Invalid.");
return undefined;
}
@ -787,7 +822,8 @@ TableNode *addName(TableNode *tn, char *str) {
SymbolTable *setLineNumber(SymbolTable *st, int line) {
if (st == NULL) {
printdebug("passed a Null Symbol Table to the setLineNumber "
printdebug(
"passed a Null Symbol Table to the setLineNumber "
"function. Invalid.");
return st;
}
@ -797,7 +833,8 @@ SymbolTable *setLineNumber(SymbolTable *st, int line) {
SymbolTable *setColumnNumber(SymbolTable *st, int column) {
if (st == NULL) {
printdebug("passed a Null Symbol Table to the setColumnNumber "
printdebug(
"passed a Null Symbol Table to the setColumnNumber "
"function. Invalid.");
return st;
}
@ -837,159 +874,129 @@ TableNode *look_up(SymbolTable *table, char *x) {
return look_up(table->Parent_Scope, x);
}
void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
int col_widths[5] = {30, 8, 8, 35, 35};
void printline(FILE *file_ptr);
void printline(FILE *file_ptr) {
for (int i = 0; i < 5; i++) {
for (int ii = 0; ii < col_widths[i]; ii++) {
fprintf(file_ptr, "-");
}
fprintf(file_ptr, ":");
}
fprintf(file_ptr, "\n");
}
void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5);
void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5) {
if (label3 == -100) {
fprintf(file_ptr, "%-*s: %0*d : %*s :%-*s:%-*s\n",
col_widths[0], (label1 ? label1 : ""),
col_widths[1] - 2, label2,
col_widths[2] - 2, "",
col_widths[3], (label4 ? label4 : ""),
col_widths[4], (label5 ? label5 : ""));
} else {
fprintf(file_ptr, "%-*s: %0*d : %0*d :%-*s:%-*s\n",
col_widths[0], (label1 ? label1 : ""),
col_widths[1] - 2, label2,
col_widths[2] - 2, label3,
col_widths[3], (label4 ? label4 : ""),
col_widths[4], (label5 ? label5 : ""));
}
}
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);
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");
fprintf(file_ptr, "%-*s:%-*s:%-*s:%-*s:%-*s:\n",
col_widths[0], " NAME",
col_widths[1], " SCOPE",
col_widths[2], " PARENT",
col_widths[3], " TYPE",
col_widths[4], " EXTRA ANNOTATION");
}
TableNode *entrie = table->entries;
fprintf(file_ptr,
"-------------------------:--------:--------:------------------"
"--------:------------------------------\n");
int parant_scope = 0;
int current_scope = 0;
TableNode *entry = table->entries;
printline(file_ptr);
int parentScopeNum = 0;
int currentScopeNum = 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;
parentScopeNum = getParent(table)->Line_Number * 1000 + getParent(table)->Column_Number;
currentScopeNum = table->Line_Number * 1000 + table->Column_Number;
} else {
current_scope = 1001;
currentScopeNum = 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) {
fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: "
"%-30s\n",
entrie->theName, current_scope,
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 (entry == NULL) {
st_fprint(file_ptr, "", currentScopeNum, parentScopeNum, "", " Empty Scope");
}
}
if (getAdInfoType(entrie) == TYPE_RECORD) {
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) {
for (; entry != NULL; entry = getNextEntry(entry)) {
if (getAdInfoType(entry) == TYPE_ARRAY_TYPE) {
char *arrayType = (char *)malloc(100);
sprintf(arrayType, " %d -> %s", entry->additionalinfo->ArrayAdInfo->numofdimensions,
entry->additionalinfo->ArrayAdInfo->typeofarray->theName);
fprintf(
file_ptr,
"%-25s: %06d : : %-25s: size-%d "
"bytes\n",
entrie->theName, current_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, arrayType, " Type of Array");
} else {
fprintf(
file_ptr,
"%-25s: %06d : %06d : %-25s: size-%-30d "
"bytes\n",
entrie->theName, current_scope,
parant_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, arrayType, " Type of Array");
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (parant_scope == 0) {
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");
if (getAdInfoType(entry) == TYPE_RECORD) {
char *recordAdInfo = (char *)malloc(100);
sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements);
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record", recordAdInfo);
} 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");
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record", recordAdInfo);
}
}
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");
if (getAdInfoType(entry) == TYPE_PRIMITIVE) {
char *primAdInfo = (char *)malloc(100);
sprintf(primAdInfo, " size-%d bytes", entry->additionalinfo->PrimAdInfo->size);
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo);
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope,
parant_scope, getType(entrie),
"User Defined");
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Primitive", primAdInfo);
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope,
"undefined", "undefined entry");
if (getAdInfoType(entry) == TYPE_FUNCTION_TYPE) {
char *functiontype = (char *)malloc(100);
sprintf(functiontype, " %s -> %s", entry->additionalinfo->FunTypeAdInfo->parameter->theName,
entry->additionalinfo->FunTypeAdInfo->returntype->theName);
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, functiontype, " Type of Function");
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope,
parant_scope, "undefined",
"undefined entry");
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, functiontype, " Type of Function");
}
}
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Function", " Function Declaration");
} else {
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Function", " Function Declaration");
}
}
if (getAdInfoType(entry) == TYPE_UNDEFINED) {
if (parentScopeNum == 0) {
st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " undefined", "undefined entry");
} else {
st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " undefined", "undefined entry");
}
}
}
if (getChildren(table) != NULL) {
ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) {
@ -999,16 +1006,14 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if ((node->table)->Line_Number == -1) {
continue;
} else {
print_symbol_table(node->table,
file_ptr);
print_symbol_table(node->table, file_ptr);
}
}
}
}
if (getParent(table) == NULL) {
fprintf(file_ptr,
"-------------------------:--------:--------:----------"
"----------------:------------------------------\n");
if (table->Parent_Scope == NULL) {
printline(file_ptr);
}
}
// get top most symbol table
@ -1033,7 +1038,6 @@ SymbolTable *getAncestor(SymbolTable *table) {
}
SymbolTable *removeEntry(SymbolTable *scope, char *search) {
if (scope == NULL) {
return NULL;
}
@ -1063,7 +1067,6 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) {
// 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);
TableNode *entry2 = look_up(cur, secondID);
if (entry1 == NULL) {
@ -1109,12 +1112,14 @@ bool typeCheck(char *firstID, char *secondID) {
SymbolTable *getParent(SymbolTable *st) {
if (st == NULL) {
printdebug("passed a NULL symbol table to getParent function. "
printdebug(
"passed a NULL symbol table to getParent function. "
"Invalid.");
return NULL;
}
if (st->Parent_Scope == NULL) {
printdebug("passed a top level scope to getParent function. "
printdebug(
"passed a top level scope to getParent function. "
"Invalid.");
return st;
}
@ -1179,6 +1184,12 @@ TableNode *printTableNode(TableNode *tn) {
tn->theType->theName);
printdebug(" %sTag: %s%d", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->tag);
if (tn->next == NULL) {
printdebug(" %sNext: %sNULL", COLOR_YELLOW, COLOR_LIGHTBLUE);
} else {
printdebug(" %sNext: %s%s (tn)", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->next->theName);
}
if (tn->tag == TYPE_RECORD_TYPE || tn->tag == TYPE_RECORD) {
printdebug(" %sAdditional Info: %sRecAdInfo", COLOR_YELLOW,
COLOR_LIGHTBLUE);