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
if [[ $? -eq 0 ]]; then
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
echo -e "\n${RED}[✘] ${PURPLE}$file ${WHITE}failed with an unexpected value...${NOCOLOR}"
diff --color=always "$file" "$exp"

View File

@ -623,7 +623,12 @@ assignable:
printdebug("as function");
//char *funtype = getType(look_up(cur, $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)));
printTableNode(table_lookup(getAncestor(cur), getName((TableNode*)$1)));
SymbolTable *recList = getRecList(param);
TableNode *lastCheckedRef = getFirstEntry(recList);
TableNode *lastCheckedAct = getFirstEntry(cur);

View File

@ -261,9 +261,10 @@ bool getAsKeyword(TableNode *definition) {
return false;
}
if (strcmp(getType(definition), "primitive function") != 0) {
printdebug(
"not checking if a function is called with as or not (%s) -- "
"invalid op", getType(definition));
printdebug("not checking if a function is called with as or "
"not (%s) -- "
"invalid op",
getType(definition));
return 0;
}
return definition->additionalinfo->FunDecAdInfo->regularoras;
@ -490,10 +491,10 @@ SymbolTable *init(SymbolTable *start) {
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
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
boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo
// addr->additionalinfo = CreatePrimitiveInfo(8);
start->Line_Number = 1;
@ -563,7 +564,7 @@ AdInfo *getAdInfo(TableNode *tn) {
return tn->additionalinfo;
}
//simplified getAdInfoType
// simplified getAdInfoType
int getAdInfoType(TableNode *tn) {
if (tn == NULL) {
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) {
printdebug("Null reference to table");
@ -656,12 +658,12 @@ TableNode *CreateEntry(SymbolTable *table, int tag, 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");
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{
} else {
newEntry->tag = tag;
}
newEntry->theType = typeOf /*topDef*/;
@ -680,8 +682,6 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
}
}
TableNode *getTypeEntry(TableNode *tn) {
if (tn == NULL) {
printdebug("passed a NULL table entry to getType");
@ -766,12 +766,12 @@ 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;
tn->theName = str;
return tn;
}
printdebug("passed a NULL string to the addName function");
return undefined;
@ -839,177 +839,177 @@ TableNode *look_up(SymbolTable *table, char *x) {
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, "%-25s: %-6s : %-6s : %-25s: %-30s\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, "%-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 (getAdInfoType(entrie) == TYPE_RECORD) {
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 (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) {
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,
"%-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,
"%-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,
"%-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 -> "
"%-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,
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");
}
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) {
@ -1138,3 +1138,101 @@ TableNode *getNextEntry(TableNode *tn) {
}
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 *getNextEntry(TableNode *tn);
TableNode * printTableNode(TableNode * tn);
void printdebug_impl(char *file, int line, const char *format, ...);
#define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)