added a type check function in symbol table
This commit is contained in:
@ -10,6 +10,7 @@ char * typey = "type";
|
|||||||
char * funy = "function";
|
char * funy = "function";
|
||||||
TableNode* funprime;
|
TableNode* funprime;
|
||||||
TableNode* arrayprim;
|
TableNode* arrayprim;
|
||||||
|
extern SymbolTable* cur;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
//First 4 below are primitive types that are all encapsulated in primitive type
|
//First 4 below are primitive types that are all encapsulated in primitive type
|
||||||
@ -256,6 +257,11 @@ if(topDef == NULL){
|
|||||||
return newEntry;
|
return newEntry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* getType(TableNode* tn) { return tn->theType->theName; }
|
||||||
|
char* getName(TableNode* tn) { return tn->theName; }
|
||||||
|
int getLine(SymbolTable* st) { return st->Line_Number; }
|
||||||
|
int getColumn(SymbolTable* st) { return st->Column_Number; }
|
||||||
/*
|
/*
|
||||||
//we use false for type defs and true for functions for parameter of typeOf
|
//we use false for type defs and true for functions for parameter of typeOf
|
||||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||||
@ -368,6 +374,36 @@ SymbolTable* getAncestor(SymbolTable* table) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool typeCheck(char* firstID, char* secondID){
|
||||||
|
|
||||||
|
|
||||||
|
TableNode* entry1 = look_up(cur,firstID);
|
||||||
|
TableNode* entry2 = look_up(cur,secondID);
|
||||||
|
if(entry1==NULL){
|
||||||
|
printf("first type not defined\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(entry2==NULL){
|
||||||
|
printf("second type not defined\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(table_lookup(getAncestor(cur),getType(look_up(cur,firstID))) ==
|
||||||
|
table_lookup(getAncestor(cur),getType(look_up(cur,secondID)))){
|
||||||
|
if(strcmp(getType(look_up(cur,firstID)),"array")==0){
|
||||||
|
if(look_up(cur,firstID)->additionalinfo->ArrayAdInfo->numofdimensions ==
|
||||||
|
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->numofdimensions &&
|
||||||
|
look_up(cur,firstID)->additionalinfo->ArrayAdInfo->typeofarray ==
|
||||||
|
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->typeofarray){
|
||||||
|
return true;}
|
||||||
|
else{
|
||||||
|
return false;}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; }
|
SymbolTable* getParent(SymbolTable* st) { return st->Parent_Scope; }
|
||||||
|
|
||||||
ListOfTable* getChildren(SymbolTable* st) { return st->Children_Scope; }
|
ListOfTable* getChildren(SymbolTable* st) { return st->Children_Scope; }
|
||||||
@ -375,10 +411,6 @@ SymbolTable* getFirstChild(ListOfTable* lt) { return lt->table; }
|
|||||||
ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; }
|
ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; }
|
||||||
TableNode* getFirstEntry(SymbolTable* st) { return st->entries; }
|
TableNode* getFirstEntry(SymbolTable* st) { return st->entries; }
|
||||||
TableNode* getNextEntry(TableNode* tn) { return tn->next; }
|
TableNode* getNextEntry(TableNode* tn) { return tn->next; }
|
||||||
char* getType(TableNode* tn) { return tn->theType->theName; }
|
|
||||||
char* getName(TableNode* tn) { return tn->theName; }
|
|
||||||
int getLine(SymbolTable* st) { return st->Line_Number; }
|
|
||||||
int getColumn(SymbolTable* st) { return st->Column_Number; }
|
|
||||||
// uncomment the below main function along with the headers above for a simple
|
// uncomment the below main function along with the headers above for a simple
|
||||||
// standalone test of table and entry creation
|
// standalone test of table and entry creation
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user