found one segfault. (not fixed yet)

This commit is contained in:
Scarlett
2025-04-07 15:51:45 -04:00
parent e42e537339
commit 4058b090a0
4 changed files with 287 additions and 181 deletions

View File

@ -33,6 +33,8 @@ compare_files() {
diff -q "$file" "$exp" > /dev/null diff -q "$file" "$exp" > /dev/null
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo -e "${GREEN}[✔] ${PURPLE}$filename ${WHITE}passed.${NOCOLOR}" echo -e "${GREEN}[✔] ${PURPLE}$filename ${WHITE}passed.${NOCOLOR}"
elif [[ ! -s "$file" ]]; then
echo -e "${RED}[✘] ${PURPLE}$filename ${WHITE}failed with an empty file. (did it segfault?)${NOCOLOR}"
else else
echo -e "\n${RED}[✘] ${PURPLE}$file ${WHITE}failed with an unexpected value...${NOCOLOR}" echo -e "\n${RED}[✘] ${PURPLE}$file ${WHITE}failed with an unexpected value...${NOCOLOR}"
diff --color=always "$file" "$exp" diff --color=always "$file" "$exp"

View File

@ -623,7 +623,12 @@ assignable:
printdebug("as function"); printdebug("as function");
//char *funtype = getType(look_up(cur, $1)); //char *funtype = getType(look_up(cur, $1));
printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); printdebug("%s", getType(look_up(cur, getName((TableNode*)$1))));
// Something fishy is going on here.......
TableNode *param = getParameter(look_up(getParent(cur), getName((TableNode*)$1))); TableNode *param = getParameter(look_up(getParent(cur), getName((TableNode*)$1)));
printTableNode(table_lookup(getAncestor(cur), getName((TableNode*)$1)));
SymbolTable *recList = getRecList(param); SymbolTable *recList = getRecList(param);
TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedRef = getFirstEntry(recList);
TableNode *lastCheckedAct = getFirstEntry(cur); TableNode *lastCheckedAct = getFirstEntry(cur);

View File

@ -261,9 +261,10 @@ bool getAsKeyword(TableNode *definition) {
return false; return false;
} }
if (strcmp(getType(definition), "primitive function") != 0) { if (strcmp(getType(definition), "primitive function") != 0) {
printdebug( printdebug("not checking if a function is called with as or "
"not checking if a function is called with as or not (%s) -- " "not (%s) -- "
"invalid op", getType(definition)); "invalid op",
getType(definition));
return 0; return 0;
} }
return definition->additionalinfo->FunDecAdInfo->regularoras; return definition->additionalinfo->FunDecAdInfo->regularoras;
@ -490,10 +491,10 @@ SymbolTable *init(SymbolTable *start) {
boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL);
integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr
chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara
stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri
boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo
// addr->additionalinfo = CreatePrimitiveInfo(8); // addr->additionalinfo = CreatePrimitiveInfo(8);
start->Line_Number = 1; start->Line_Number = 1;
@ -563,7 +564,7 @@ AdInfo *getAdInfo(TableNode *tn) {
return tn->additionalinfo; return tn->additionalinfo;
} }
//simplified getAdInfoType // simplified getAdInfoType
int getAdInfoType(TableNode *tn) { int getAdInfoType(TableNode *tn) {
if (tn == NULL) { if (tn == NULL) {
printdebug( printdebug(
@ -633,7 +634,8 @@ int getAdInfoType(TableNode *tn) {
}*/ }*/
} }
TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) { TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
AdInfo *ad) {
if (table == NULL) { if (table == NULL) {
printdebug("Null reference to table"); printdebug("Null reference to table");
@ -656,12 +658,12 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
return undefined; return undefined;
} }
TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
if(tag<1 && tag>11){ if (tag < 1 && tag > 11) {
printdebug("Note- not passing in valid 'tag' identifier to create entry function. Setting tag to undefined"); printdebug("Note- not passing in valid 'tag' identifier to "
"create entry function. Setting tag to undefined");
newEntry->tag = TYPE_UNDEFINED; newEntry->tag = TYPE_UNDEFINED;
} else{ } else {
newEntry->tag = tag; newEntry->tag = tag;
} }
newEntry->theType = typeOf /*topDef*/; newEntry->theType = typeOf /*topDef*/;
@ -680,8 +682,6 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
} }
} }
TableNode *getTypeEntry(TableNode *tn) { TableNode *getTypeEntry(TableNode *tn) {
if (tn == NULL) { if (tn == NULL) {
printdebug("passed a NULL table entry to getType"); printdebug("passed a NULL table entry to getType");
@ -766,12 +766,12 @@ TableNode *addName(TableNode *tn, char *str) {
return undefined; return undefined;
} }
if (tn->theName != NULL) { if (tn->theName != NULL) {
//printdebug( // printdebug(
//"Name doesn't look like it is empty before you change. " //"Name doesn't look like it is empty before you change. "
//"Are you sure you need to update name?"); //"Are you sure you need to update name?");
if (str != NULL) { if (str != NULL) {
tn->theName = str; tn->theName = str;
return tn; return tn;
} }
printdebug("passed a NULL string to the addName function"); printdebug("passed a NULL string to the addName function");
return undefined; return undefined;
@ -839,177 +839,177 @@ TableNode *look_up(SymbolTable *table, char *x) {
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table == NULL) { if (table == NULL) {
printdebug( printdebug(
"%s[FATAL] passed in NULL table to print_symbol_table", "%s[FATAL] passed in NULL table to print_symbol_table",
COLOR_RED); COLOR_RED);
return; return;
} }
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "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 = getParent(table)->Line_Number * 1000 + parant_scope = getParent(table)->Line_Number * 1000 +
getParent(table)->Column_Number; getParent(table)->Column_Number;
current_scope = current_scope =
table->Line_Number * 1000 + table->Column_Number; table->Line_Number * 1000 + table->Column_Number;
} else { } else {
current_scope = 1001; current_scope = 1001;
} }
if (entrie == NULL) { if (entrie == NULL) {
fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "",
current_scope, parant_scope, "", "Empty Scope"); current_scope, parant_scope, "", "Empty Scope");
} }
for (; entrie != NULL; entrie = getNextEntry(entrie)) { for (; entrie != NULL; entrie = getNextEntry(entrie)) {
if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: " "%-25s: %06d : : %d -> %-20s: "
"%-30s\n", "%-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
entrie->additionalinfo->ArrayAdInfo entrie->additionalinfo->ArrayAdInfo
->numofdimensions, ->numofdimensions,
entrie->additionalinfo->ArrayAdInfo entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName, ->typeofarray->theName,
"Type of Array"); "Type of Array");
} else { } else {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: " "%-25s: %06d : : %d -> %-20s: "
"%-30s\n", "%-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, parant_scope,
entrie->additionalinfo->ArrayAdInfo entrie->additionalinfo->ArrayAdInfo
->numofdimensions, ->numofdimensions,
entrie->additionalinfo->ArrayAdInfo entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName, ->typeofarray->theName,
"Type of Array"); "Type of Array");
} }
} }
if (getAdInfoType(entrie) == TYPE_RECORD) { if (getAdInfoType(entrie) == TYPE_RECORD) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s: " "%-25s: %06d : : %-25s: "
"elements-%-30d\n", "elements-%-30d\n",
entrie->theName, current_scope, entrie->theName, current_scope,
"record", "record",
entrie->additionalinfo->RecAdInfo entrie->additionalinfo->RecAdInfo
->numofelements); ->numofelements);
} else { } else {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: " "%-25s: %06d : %06d : %-25s: "
"elements-%-30d\n", "elements-%-30d\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, "record", parant_scope, "record",
entrie->additionalinfo->RecAdInfo entrie->additionalinfo->RecAdInfo
->numofelements); ->numofelements);
} }
} }
if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf( fprintf(
file_ptr, file_ptr,
"%-25s: %06d : : %-25s: size-%d " "%-25s: %06d : : %-25s: size-%d "
"bytes\n", "bytes\n",
entrie->theName, current_scope, "Primitive", entrie->theName, current_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size); entrie->additionalinfo->PrimAdInfo->size);
} else { } else {
fprintf( fprintf(
file_ptr, file_ptr,
"%-25s: %06d : %06d : %-25s: size-%-30d " "%-25s: %06d : %06d : %-25s: size-%-30d "
"bytes\n", "bytes\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, "Primitive", parant_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size); entrie->additionalinfo->PrimAdInfo->size);
} }
} }
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s -> " "%-25s: %06d : : %-25s -> "
"%-25s: %-30s\n", "%-25s: %-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
entrie->additionalinfo->FunTypeAdInfo entrie->additionalinfo->FunTypeAdInfo
->parameter->theName, ->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo entrie->additionalinfo->FunTypeAdInfo
->returntype->theName, ->returntype->theName,
"Type of Function"); "Type of Function");
} else { } else {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s -> %-21s: " "%-25s: %06d : %06d : %-25s -> %-21s: "
"%-30s\n", "%-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, parant_scope,
entrie->additionalinfo->FunTypeAdInfo entrie->additionalinfo->FunTypeAdInfo
->parameter->theName, ->parameter->theName,
entrie->additionalinfo->FunTypeAdInfo entrie->additionalinfo->FunTypeAdInfo
->returntype->theName, ->returntype->theName,
"Type of Function"); "Type of Function");
} }
} }
if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) { if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n", "%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
getType(entrie), "User Defined"); getType(entrie), "User Defined");
} else { } else {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n", "%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, getType(entrie), parant_scope, getType(entrie),
"User Defined"); "User Defined");
} }
} }
if (getAdInfoType(entrie) == TYPE_UNDEFINED) { if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n", "%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
"undefined", "undefined entry"); "undefined", "undefined entry");
} else { } else {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n", "%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, "undefined", parant_scope, "undefined",
"undefined entry"); "undefined entry");
} }
} }
} }
if (getChildren(table) != NULL) { if (getChildren(table) != NULL) {
ListOfTable *node = getChildren(table); ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) { for (; node != NULL; node = node->next) {
if ((node->table) == NULL) { if ((node->table) == NULL) {
print_symbol_table(node->table, file_ptr); print_symbol_table(node->table, file_ptr);
} else { } else {
if ((node->table)->Line_Number == -1) { if ((node->table)->Line_Number == -1) {
continue; continue;
} else { } else {
print_symbol_table(node->table, print_symbol_table(node->table,
file_ptr); file_ptr);
} }
} }
} }
} }
if (getParent(table) == NULL) { if (getParent(table) == NULL) {
fprintf(file_ptr, fprintf(file_ptr,
"-------------------------:--------:--------:----------" "-------------------------:--------:--------:----------"
"----------------:------------------------------\n"); "----------------:------------------------------\n");
} }
} }
// get top most symbol table // get top most symbol table
SymbolTable *getAncestor(SymbolTable *table) { SymbolTable *getAncestor(SymbolTable *table) {
@ -1138,3 +1138,101 @@ TableNode *getNextEntry(TableNode *tn) {
} }
return tn->next; return tn->next;
} }
// Prints all info about a table node
// Uses pointers to the table node to print the info
TableNode *printTableNode(TableNode *tn) {
if (DEBUG == 0) {
return tn;
}
if (tn == NULL) {
printdebug("%s[PrintTN] Passed a NULL tablenode!", COLOR_RED);
return undefined;
}
if (tn->theName == NULL) {
printdebug("%s[PrintTN] Passed a tablenode with NULL name!",
COLOR_RED);
return undefined;
}
if (tn->theType == NULL) {
printdebug("%s[PrintTN] Passed a tablenode with NULL type!",
COLOR_RED);
return undefined;
}
if (tn == undefined) {
printdebug("%s[PrintTN] Passed an undefined tablenode!",
COLOR_RED);
return undefined;
}
if (tn->additionalinfo == NULL) {
printdebug(
"%s[PrintTN] Passed a tablenode with NULL additional info!",
COLOR_RED);
return undefined;
}
printdebug("%s[PrintTN] Printing tablenode...", COLOR_ORANGE);
printdebug(" %sName: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE,
tn->theName);
printdebug(" %sType: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE,
tn->theType->theName);
printdebug(" %sTag: %s%d", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->tag);
if (tn->tag == TYPE_RECORD_TYPE || tn->tag == TYPE_RECORD) {
printdebug(" %sAdditional Info: %sRecAdInfo", COLOR_YELLOW,
COLOR_LIGHTBLUE);
printdebug(" %snumberOfElements: %s%d", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->RecAdInfo->numofelements);
if (tn->additionalinfo->RecAdInfo->recordScope == NULL) {
printdebug(" %srecordScope (line): %s(NULL)",
COLOR_YELLOW, COLOR_LIGHTBLUE);
} else {
printdebug(" %srecordScope (line): %s%d",
COLOR_YELLOW, COLOR_LIGHTBLUE,
tn->additionalinfo->RecAdInfo->recordScope
->Line_Number);
}
} else if (tn->tag == TYPE_ARRAY_TYPE || tn->tag == TYPE_ARRAY) {
printdebug(" %sAdditional Info: %sArrayAdInfo", COLOR_YELLOW,
COLOR_LIGHTBLUE);
printdebug(" %snumberOfDimensions: %s%d", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->ArrayAdInfo->numofdimensions);
printdebug(
" %stypeOfArray: %s%s", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->ArrayAdInfo->typeofarray->theName);
} else if (tn->tag == TYPE_FUNCTION_TYPE) {
printdebug(" %sAdditional Info: %sFunTypeAdInfo",
COLOR_YELLOW, COLOR_LIGHTBLUE);
printdebug(
" %sparameter: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE,
tn->additionalinfo->FunTypeAdInfo->parameter->theName);
printdebug(
" %sreturntype: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE,
tn->additionalinfo->FunTypeAdInfo->returntype->theName);
} else if (tn->tag == TYPE_FUNCTION_DECLARATION) {
printdebug(" %sAdditional Info: %sFunDecAdInfo",
COLOR_YELLOW, COLOR_LIGHTBLUE);
printdebug(" %sstartLineNumber: %s%d", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->FunDecAdInfo->startlinenumber);
printdebug(" %sregularOrAs: %s%s", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->FunDecAdInfo->regularoras
? "true"
: "false");
} else if (tn->tag == TYPE_PRIMITIVE) {
printdebug(" %sAdditional Info: %sPrimAdInfo", COLOR_YELLOW,
COLOR_LIGHTBLUE);
printdebug(" %ssize: %s%d", COLOR_YELLOW,
COLOR_LIGHTBLUE,
tn->additionalinfo->PrimAdInfo->size);
} else {
printdebug(" AdInfo not handled.");
}
return tn;
}

View File

@ -124,6 +124,7 @@ ListOfTable *getRestOfChildren(ListOfTable *lt);
TableNode *getFirstEntry(SymbolTable *st); TableNode *getFirstEntry(SymbolTable *st);
TableNode *getNextEntry(TableNode *tn); TableNode *getNextEntry(TableNode *tn);
TableNode * printTableNode(TableNode * tn);
void printdebug_impl(char *file, int line, const char *format, ...); void printdebug_impl(char *file, int line, const char *format, ...);
#define printdebug(format, ...) \ #define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)