updated lookups to return undefined entry if invalid
This commit is contained in:
@ -201,60 +201,70 @@ expression:
|
|||||||
$$=strdup("undefined");}else{$$=$2;}}
|
$$=strdup("undefined");}else{$$=$2;}}
|
||||||
|
|
||||||
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
|
| 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
|
| expression ADD expression
|
||||||
{printf("add expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
|
{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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression SUB_OR_NEG expression
|
| expression SUB_OR_NEG expression
|
||||||
{printf("sub or neg expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
|
{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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression MUL expression
|
| expression MUL expression
|
||||||
{printf("multiply expression\n");
|
{printf("multiply expression\n");
|
||||||
if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
|
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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression DIV expression
|
| expression DIV expression
|
||||||
{printf("divide expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
|
{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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression REM expression
|
| expression REM expression
|
||||||
{printf("remainder expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
|
{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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression AND expression
|
| expression AND expression
|
||||||
{printf("AND expression\n");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
|
{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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression OR expression
|
| expression OR expression
|
||||||
{printf("OR\n");if(strcmp($1,$3)==0 &&
|
{printf("OR\n");if(strcmp($1,$3)==0 &&
|
||||||
strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
|
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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression LESS_THAN expression
|
| expression LESS_THAN expression
|
||||||
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
|
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
|
||||||
strcmp($1,"integer")==0){$$=strdup("Boolean");}
|
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");}}
|
$$=strdup("undefined");}}
|
||||||
|
|
||||||
| expression EQUAL_TO expression {printf("equals check expression\n");
|
| expression EQUAL_TO expression {printf("equals check expression\n");
|
||||||
if(strcmp($1,$3)==0){$$=strdup("Boolean");}
|
if(strcmp($1,$3)==0){$$=strdup("Boolean");}
|
||||||
else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
|
else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
|
||||||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
|
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");}
|
| memOp assignable {$$ = strdup("address");}
|
||||||
;
|
;
|
||||||
|
@ -18,6 +18,7 @@ TableNode *stri;
|
|||||||
TableNode *boo;
|
TableNode *boo;
|
||||||
TableNode *recprime;
|
TableNode *recprime;
|
||||||
TableNode *funtypeprime;
|
TableNode *funtypeprime;
|
||||||
|
TableNode *undefined;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
// First 4 below are primitive types that are all encapsulated in
|
// 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
|
// gets the line at which the function was first defined. (Can be used to print
|
||||||
// out in table if needed)
|
// out in table if needed)
|
||||||
int getStartLine(TableNode *definition) {
|
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 "
|
printf("not checking the start line of a function -- invalid "
|
||||||
"op\n");
|
"op\n");
|
||||||
return 0;
|
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
|
// checks if "as" keyword was used for function definition. Either 0 or 1 for
|
||||||
// not used or used.
|
// not used or used.
|
||||||
bool getAsKeyword(TableNode *definition) {
|
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 -- "
|
printf("not checking if a function is called with as or not -- "
|
||||||
"invalid op\n");
|
"invalid op\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -275,7 +276,7 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
|
|||||||
}
|
}
|
||||||
// returns parameter type of a function
|
// returns parameter type of a function
|
||||||
TableNode *getParameter(TableNode *definition) {
|
TableNode *getParameter(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "function type primitive") != 0) {
|
if (strcmp(getType(definition), "primitive function type") != 0) {
|
||||||
printf(
|
printf(
|
||||||
"not checking the parameter of a function -- invalid op\n");
|
"not checking the parameter of a function -- invalid op\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -284,7 +285,7 @@ TableNode *getParameter(TableNode *definition) {
|
|||||||
}
|
}
|
||||||
// returns return type of a function
|
// returns return type of a function
|
||||||
TableNode *getReturn(TableNode *definition) {
|
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");
|
printf("not checking the return of a function -- invalid op\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -394,6 +395,12 @@ SymbolTable *init(SymbolTable *start) {
|
|||||||
funtypeprime->additionalinfo = NULL;
|
funtypeprime->additionalinfo = NULL;
|
||||||
funtypeprime->next = NULL;
|
funtypeprime->next = NULL;
|
||||||
|
|
||||||
|
undefined = (TableNode *)malloc(sizeof(TableNode));
|
||||||
|
undefined->theName = "undefined";
|
||||||
|
undefined->theType = NULL;
|
||||||
|
undefined->additionalinfo = NULL;
|
||||||
|
undefined->next = NULL;
|
||||||
|
|
||||||
integ->theType = prime;
|
integ->theType = prime;
|
||||||
addr->theType = prime;
|
addr->theType = prime;
|
||||||
chara->theType = prime;
|
chara->theType = prime;
|
||||||
@ -522,24 +529,26 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
|
|||||||
char *getType(TableNode *tn) {
|
char *getType(TableNode *tn) {
|
||||||
if(tn == NULL){
|
if(tn == NULL){
|
||||||
printf("passed a NULL table entry to getType\n");
|
printf("passed a NULL table entry to getType\n");
|
||||||
return "";
|
return getName(undefined);
|
||||||
}
|
}
|
||||||
if(tn->theType == NULL){
|
if(tn->theType == NULL){
|
||||||
printf("type of entry is currently NULL, undefined type \n");
|
printf("type of entry is currently NULL, undefined type \n");
|
||||||
return "";
|
return getName(undefined);
|
||||||
}
|
}
|
||||||
return tn->theType->theName; }
|
return tn->theType->theName; }
|
||||||
|
|
||||||
char *getName(TableNode *tn) {
|
char *getName(TableNode *tn) {
|
||||||
if(tn == NULL){
|
if(tn == NULL){
|
||||||
printf("passed a NULL table entry to getName\n");
|
printf("passed a NULL table entry to getName\n");
|
||||||
return "";
|
return undefined->theName;
|
||||||
}
|
}
|
||||||
if(tn->theName == NULL){
|
if(tn->theName == NULL){
|
||||||
printf("name of entry is currently NULL, undefined \n");
|
printf("name of entry is currently NULL, undefined \n");
|
||||||
return "";
|
return undefined->theName;
|
||||||
}
|
}
|
||||||
return tn->theName;
|
return tn->theName;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getLine(SymbolTable *st) { return st->Line_Number; }
|
int getLine(SymbolTable *st) { return st->Line_Number; }
|
||||||
int getColumn(SymbolTable *st) { return st->Column_Number; }
|
int getColumn(SymbolTable *st) { return st->Column_Number; }
|
||||||
TableNode* addName(TableNode *tn, char* str){
|
TableNode* addName(TableNode *tn, char* str){
|
||||||
@ -609,17 +618,20 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
|
|||||||
return entrie;
|
return entrie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return undefined;
|
||||||
}
|
}
|
||||||
//check current table and all parents
|
//check current table and all parents
|
||||||
TableNode *look_up(SymbolTable *table, char *x) {
|
TableNode *look_up(SymbolTable *table, char *x) {
|
||||||
if (table == NULL) {
|
if (table == NULL) {
|
||||||
return NULL;
|
printf("passed in empty scope. error.\n");
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
TableNode *ret = table_lookup(table, x);
|
TableNode *ret = table_lookup(table, x);
|
||||||
if (ret != NULL) {
|
if (ret != NULL && ret != undefined) {
|
||||||
return ret;
|
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);
|
return look_up(table->Parent_Scope, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,10 +18,10 @@ bar2 as (r,s) := {
|
|||||||
entry(arg) := {
|
entry(arg) := {
|
||||||
[ integer: result ; rec: w]
|
[ integer: result ; rec: w]
|
||||||
result := foo(5);
|
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.x := 5;
|
||||||
w.y := 7;
|
w.y := 7;
|
||||||
result := bar1(w); (* pass w (a rec type value) to bar1 *)
|
result := bar1(w); (* pass w (a rec type value) to bar1 *)
|
||||||
result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *)
|
result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user