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

@ -201,60 +201,70 @@ expression:
$$=strdup("undefined");}else{$$=$2;}}
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
printf("mismatch at line %d and column %d. Invalid type being negated is %s\n",
@1.first_line,@1.first_column,$2);}}
| expression ADD expression
{printf("add expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression SUB_OR_NEG expression
{printf("sub or neg expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression MUL expression
{printf("multiply expression\n");
if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression DIV expression
{printf("divide expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression REM expression
{printf("remainder expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression AND expression
{printf("AND expression\n");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression OR expression
{printf("OR\n");if(strcmp($1,$3)==0 &&
strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression LESS_THAN expression
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression EQUAL_TO expression {printf("equals check expression\n");
if(strcmp($1,$3)==0){$$=strdup("Boolean");}
else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s\n",
@2.first_line,@2.first_column,$1,$3);$$=strdup("undefined");}}
| assignable {printf("assignable expression\n");$$=$1;}
| assignable {printf("assignable expression. current type is %s\n",$1);$$=$1;}
| L_PAREN expression R_PAREN {printf("paren expression\n");$$=$2;}
| L_PAREN expression R_PAREN {printf("paren expression. current type is %s\n",$2);$$=$2;}
| memOp assignable {$$ = strdup("address");}
;

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);
}

View File

@ -18,7 +18,7 @@ bar2 as (r,s) := {
entry(arg) := {
[ integer: result ; rec: w]
result := foo(5);
w := reserve(w); (* see types.alpha reserve returns a value of type address, which can be assigned to array and record variables*)
w := reserve w; (* see types.alpha reserve returns a value of type address, which can be assigned to array and record variables*)
w.x := 5;
w.y := 7;
result := bar1(w); (* pass w (a rec type value) to bar1 *)