updated lookups to return undefined entry if invalid

This commit is contained in:
Partho Bhattacharya
2025-03-28 13:47:53 -04:00
parent 2654308396
commit 36694a2f4a
3 changed files with 47 additions and 25 deletions

View File

@ -18,6 +18,7 @@ TableNode *stri;
TableNode *boo;
TableNode *recprime;
TableNode *funtypeprime;
TableNode *undefined;
typedef enum {
// First 4 below are primitive types that are all encapsulated in
@ -228,7 +229,7 @@ AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) {
// gets the line at which the function was first defined. (Can be used to print
// out in table if needed)
int getStartLine(TableNode *definition) {
if (strcmp(getType(definition), "function primitive") != 0) {
if (strcmp(getType(definition), "primitive function") != 0) {
printf("not checking the start line of a function -- invalid "
"op\n");
return 0;
@ -247,7 +248,7 @@ TableNode* setStartLine(TableNode* tn, int start){
// checks if "as" keyword was used for function definition. Either 0 or 1 for
// not used or used.
bool getAsKeyword(TableNode *definition) {
if (strcmp(getType(definition), "function primitive") != 0) {
if (strcmp(getType(definition), "primitive function") != 0) {
printf("not checking if a function is called with as or not -- "
"invalid op\n");
return NULL;
@ -275,7 +276,7 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
}
// returns parameter type of a function
TableNode *getParameter(TableNode *definition) {
if (strcmp(getType(definition), "function type primitive") != 0) {
if (strcmp(getType(definition), "primitive function type") != 0) {
printf(
"not checking the parameter of a function -- invalid op\n");
return NULL;
@ -284,7 +285,7 @@ TableNode *getParameter(TableNode *definition) {
}
// returns return type of a function
TableNode *getReturn(TableNode *definition) {
if (strcmp(getType(definition), "function type primitive") != 0) {
if (strcmp(getType(definition), "primitive function type") != 0) {
printf("not checking the return of a function -- invalid op\n");
return NULL;
}
@ -394,6 +395,12 @@ SymbolTable *init(SymbolTable *start) {
funtypeprime->additionalinfo = NULL;
funtypeprime->next = NULL;
undefined = (TableNode *)malloc(sizeof(TableNode));
undefined->theName = "undefined";
undefined->theType = NULL;
undefined->additionalinfo = NULL;
undefined->next = NULL;
integ->theType = prime;
addr->theType = prime;
chara->theType = prime;
@ -522,24 +529,26 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
char *getType(TableNode *tn) {
if(tn == NULL){
printf("passed a NULL table entry to getType\n");
return "";
return getName(undefined);
}
if(tn->theType == NULL){
printf("type of entry is currently NULL, undefined type \n");
return "";
return getName(undefined);
}
return tn->theType->theName; }
char *getName(TableNode *tn) {
if(tn == NULL){
printf("passed a NULL table entry to getName\n");
return "";
return undefined->theName;
}
if(tn->theName == NULL){
printf("name of entry is currently NULL, undefined \n");
return "";
return undefined->theName;
}
return tn->theName;
}
int getLine(SymbolTable *st) { return st->Line_Number; }
int getColumn(SymbolTable *st) { return st->Column_Number; }
TableNode* addName(TableNode *tn, char* str){
@ -609,17 +618,20 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
return entrie;
}
}
return NULL;
return undefined;
}
//check current table and all parents
TableNode *look_up(SymbolTable *table, char *x) {
if (table == NULL) {
return NULL;
printf("passed in empty scope. error.\n");
return undefined;
}
TableNode *ret = table_lookup(table, x);
if (ret != NULL) {
if (ret != NULL && ret != undefined) {
return ret;
}
printf("could not find %s in scope that started at line %d and column %d so moving up a scope\n"
,x,getLine(table),getColumn(table));
return look_up(table->Parent_Scope, x);
}