This commit is contained in:
Scarlett
2025-03-28 23:33:08 -04:00
parent aebb831fe8
commit d587ed9f3b
6 changed files with 383 additions and 289 deletions

View File

@ -37,7 +37,7 @@ debug: clean compiler
test: test:
chmod +x ./check.sh chmod +x ./check.sh
chmod +x ./test.sh chmod +x ./test.sh
$(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) || true);) $(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) -debug || true);)
./test.sh sp2 ./test.sh sp2
./test.sh sp3 ./test.sh sp3
./test.sh sp4 ./test.sh sp4
@ -46,7 +46,7 @@ test:
test-s1: test-s1:
chmod +x ./check.sh chmod +x ./check.sh
chmod +x ./test.sh chmod +x ./test.sh
$(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) || true);) $(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) -debug || true);)
./check.sh sp1 ./check.sh sp1
test-s2: test-s2:

View File

@ -122,7 +122,7 @@ prototype:
definition: definition:
TYPE ID COLON { TYPE ID COLON {
printf("Currently see a record definition for %s\n", $<words>2); printf("Currently see a record definition for %s\n", $<words>2);
tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
if (table_lookup(getAncestor(cur), $2) == NULL) { if (table_lookup(getAncestor(cur), $2) == NULL) {
printf("rec not found \n"); printf("rec not found \n");

View File

@ -9,8 +9,19 @@ extern TableNode *addr;
extern TableNode *chara; extern TableNode *chara;
extern TableNode *stri; extern TableNode *stri;
extern TableNode *boo; extern TableNode *boo;
extern bool DEBUG;
extern char * COLOR_YELLOW;
extern char * COLOR_BLUE;
extern char * COLOR_WHITE;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// if last argument is debug then set to true and ignore it for the rest of this file
if (argc > 1 && strcmp(argv[argc - 1], "-debug") == 0) {
DEBUG = true;
argc--;
}
if (argc == 1) { if (argc == 1) {
fprintf(stderr, INVALID); fprintf(stderr, INVALID);
return -1; return -1;
@ -221,4 +232,4 @@ void exit_scope() {
return; return;
} }
cur = cur->Parent_Scope; cur = cur->Parent_Scope;
} }

View File

@ -19,7 +19,7 @@ TableNode *boo;
TableNode *recprime; TableNode *recprime;
TableNode *funtypeprime; TableNode *funtypeprime;
TableNode *undefined; TableNode *undefined;
//AdInfo *Undefined_function_type_info; // AdInfo *Undefined_function_type_info;
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
@ -44,9 +44,9 @@ typedef enum {
// The Type being pointed to by the first 4 above that only stores the // The Type being pointed to by the first 4 above that only stores the
// size // size
TYPE_PRIMITIVE = 6, TYPE_PRIMITIVE = 6,
//likely NULL // likely NULL
TYPE_ALL_ELSE = 7, TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8 TYPE_UNDEFINED = 8
} types; } types;
@ -106,14 +106,15 @@ AdInfo *CreatePrimitiveInfo(int size) {
// only gets the size of a primitive type // only gets the size of a primitive type
int getPrimSize(TableNode *definition) { int getPrimSize(TableNode *definition) {
if (definition == NULL || definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL entry to getPrimSize function. Invalid.\n"); printf(
return -1; "passed an NULL entry to getPrimSize function. Invalid.\n");
} return -1;
if (definition->additionalinfo == NULL){ }
printf("node has NULL additionalinfo. Invalid.\n"); if (definition->additionalinfo == NULL) {
return -1; printf("node has NULL additionalinfo. Invalid.\n");
} return -1;
}
if (strcmp(getType(definition), "primitive") != 0) { if (strcmp(getType(definition), "primitive") != 0) {
printf("not checking the size of a primitive -- invalid op\n"); printf("not checking the size of a primitive -- invalid op\n");
return 0; return 0;
@ -134,8 +135,9 @@ int getPrimSize(TableNode *definition) {
// type stored in the array per professor, the actual size of the array is // type stored in the array per professor, the actual size of the array is
// calculated at runtime so bounds checking only needs to be done then // calculated at runtime so bounds checking only needs to be done then
AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) { AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
if(type == NULL || type == undefined){ if (type == NULL || type == undefined) {
printf("passed a NULL or undefined type reference to CreateArrayInfo. Invalid.\n"); printf("passed a NULL or undefined type reference to "
"CreateArrayInfo. Invalid.\n");
return NULL; return NULL;
} }
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
@ -148,8 +150,9 @@ AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
} }
// This gets the number of dimensions from array info // This gets the number of dimensions from array info
int getNumArrDim(TableNode *definition) { int getNumArrDim(TableNode *definition) {
if (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getNumArrDim function. Invalid.\n"); printf("passed an NULL or undefined entry to getNumArrDim "
"function. Invalid.\n");
return -1; return -1;
} }
if (strcmp(getType(definition), "array") != 0) { if (strcmp(getType(definition), "array") != 0) {
@ -161,8 +164,9 @@ int getNumArrDim(TableNode *definition) {
// This gets the type stored in an array from arrtype. It returns a reference to // This gets the type stored in an array from arrtype. It returns a reference to
// the entry of that type // the entry of that type
TableNode *getArrType(TableNode *definition) { TableNode *getArrType(TableNode *definition) {
if(definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getArrType function. Invalid.\n"); printf("passed an NULL or undefined entry to getArrType "
"function. Invalid.\n");
return NULL; return NULL;
} }
if (strcmp(getType(definition), "array") != 0) { if (strcmp(getType(definition), "array") != 0) {
@ -188,8 +192,9 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
// Perhaps this may not be needed since we need to iterate over all elements // Perhaps this may not be needed since we need to iterate over all elements
// anyways. // anyways.
int getRecLength(TableNode *definition) { int getRecLength(TableNode *definition) {
if (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getRecLength function. Invalid.\n"); printf("passed an NULL or undefined entry to getRecLength "
"function. Invalid.\n");
return -1; return -1;
} }
if (strcmp(getType(definition), "record") != 0) { if (strcmp(getType(definition), "record") != 0) {
@ -200,8 +205,9 @@ int getRecLength(TableNode *definition) {
} }
// This gets the array. Needs to up be updated to get the scope instead // This gets the array. Needs to up be updated to get the scope instead
SymbolTable *getRecList(TableNode *definition) { SymbolTable *getRecList(TableNode *definition) {
if (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getRecList function. Invalid.\n"); printf("passed an NULL or undefined entry to getRecList "
"function. Invalid.\n");
return NULL; return NULL;
} }
if (strcmp(getType(definition), "record") != 0) { if (strcmp(getType(definition), "record") != 0) {
@ -212,34 +218,34 @@ SymbolTable *getRecList(TableNode *definition) {
return definition->additionalinfo->RecAdInfo->recordScope; return definition->additionalinfo->RecAdInfo->recordScope;
} }
TableNode* setRecSize(TableNode* tn, int n){ TableNode *setRecSize(TableNode *tn, int n) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passed in NULL entry for setRecSize. Invalid\n"); printf("passed in NULL entry for setRecSize. Invalid\n");
return undefined; return undefined;
} }
tn->additionalinfo->RecAdInfo->numofelements = n; tn->additionalinfo->RecAdInfo->numofelements = n;
return tn; return tn;
} }
int getRecSize(SymbolTable* tn){ int getRecSize(SymbolTable *tn) {
if(tn == NULL){ if (tn == NULL) {
printf("passed in NULL SymbolTable for getRecSize. Invalid\n"); printf("passed in NULL SymbolTable for getRecSize. Invalid\n");
return -1; return -1;
} }
int s = 0; int s = 0;
TableNode* cur = getFirstEntry(tn); TableNode *cur = getFirstEntry(tn);
if (cur != NULL){ if (cur != NULL) {
while(getNextEntry(cur) != NULL){ while (getNextEntry(cur) != NULL) {
s++; s++;
cur = getNextEntry(cur); cur = getNextEntry(cur);
} }
return s; return s;
} }
return -1; return -1;
} }
// below function takes a bool to see if parameter should be decomposed or not // below function takes a bool to see if parameter should be decomposed or not
;// note that functions only take one input and have one output ; // note that functions only take one input and have one output
// using "as" the input record can be decomposed to give the illusion of // using "as" the input record can be decomposed to give the illusion of
// multiple inputs Below function also has the line number where the function is // multiple inputs Below function also has the line number where the function is
// first defined // first defined
@ -254,8 +260,9 @@ 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 (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getStartLine function. Invalid.\n"); printf("passed an NULL or undefined entry to getStartLine "
"function. Invalid.\n");
return -1; return -1;
} }
if (strcmp(getType(definition), "primitive function") != 0) { if (strcmp(getType(definition), "primitive function") != 0) {
@ -266,19 +273,21 @@ int getStartLine(TableNode *definition) {
return definition->additionalinfo->FunDecAdInfo->startlinenumber; return definition->additionalinfo->FunDecAdInfo->startlinenumber;
} }
TableNode* setStartLine(TableNode* tn, int start){ TableNode *setStartLine(TableNode *tn, int start) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passing in a NULL or undefined entry to setStartLine. invalid\n"); printf("passing in a NULL or undefined entry to setStartLine. "
return undefined; "invalid\n");
} return undefined;
tn->additionalinfo->FunDecAdInfo->startlinenumber = start; }
return tn; tn->additionalinfo->FunDecAdInfo->startlinenumber = start;
} return tn;
}
// 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 (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getAsKeyword function. Invalid.\n"); printf("passed an NULL or undefined entry to getAsKeyword "
"function. Invalid.\n");
return false; return false;
} }
if (strcmp(getType(definition), "primitive function") != 0) { if (strcmp(getType(definition), "primitive function") != 0) {
@ -289,23 +298,26 @@ bool getAsKeyword(TableNode *definition) {
return definition->additionalinfo->FunDecAdInfo->regularoras; return definition->additionalinfo->FunDecAdInfo->regularoras;
} }
TableNode* setAsKeyword(TableNode* tn, bool as){ TableNode *setAsKeyword(TableNode *tn, bool as) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passing in a NULL or undefined entry to setAsKeyword. invalid\n"); printf("passing in a NULL or undefined entry to setAsKeyword. "
"invalid\n");
return undefined; return undefined;
} }
tn->additionalinfo->FunDecAdInfo->regularoras = as; tn->additionalinfo->FunDecAdInfo->regularoras = as;
return tn; return tn;
} }
// stores the type of a function (parameter type and return type) // stores the type of a function (parameter type and return type)
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) { AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
if(parameter == NULL||parameter == undefined){ if (parameter == NULL || parameter == undefined) {
printf("passed a NULL or undefined parameter to CreateFunctionTypeInfo. Invalid.\n"); printf("passed a NULL or undefined parameter to "
"CreateFunctionTypeInfo. Invalid.\n");
return NULL; return NULL;
} }
if(returntype == NULL||returntype == undefined){ if (returntype == NULL || returntype == undefined) {
printf("passed a NULL or undefined return type to CreateFunctionTypeInfo. Invalid.\n"); printf("passed a NULL or undefined return type to "
"CreateFunctionTypeInfo. Invalid.\n");
return NULL; return NULL;
} }
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
@ -317,8 +329,9 @@ 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 (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getParameter function. Invalid.\n"); printf("passed an NULL or undefined entry to getParameter "
"function. Invalid.\n");
return undefined; return undefined;
} }
if (strcmp(getType(definition), "primitive function type") != 0) { if (strcmp(getType(definition), "primitive function type") != 0) {
@ -330,8 +343,9 @@ 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 (definition == NULL|| definition == undefined){ if (definition == NULL || definition == undefined) {
printf("passed an NULL or undefined entry to getReturn function. Invalid.\n"); printf("passed an NULL or undefined entry to getReturn "
"function. Invalid.\n");
return NULL; return NULL;
} }
if (strcmp(getType(definition), "primitive function type") != 0) { if (strcmp(getType(definition), "primitive function type") != 0) {
@ -379,11 +393,11 @@ SymbolTable *init(SymbolTable *start) {
"Cannot initialize a scope that is not the parent scope\n"); "Cannot initialize a scope that is not the parent scope\n");
return NULL; return NULL;
} }
integ = (TableNode *)calloc(1,sizeof(TableNode)); integ = (TableNode *)calloc(1, sizeof(TableNode));
addr = (TableNode *)calloc(1,sizeof(TableNode)); addr = (TableNode *)calloc(1, sizeof(TableNode));
chara = (TableNode *)calloc(1,sizeof(TableNode)); chara = (TableNode *)calloc(1, sizeof(TableNode));
stri = (TableNode *)calloc(1,sizeof(TableNode)); stri = (TableNode *)calloc(1, sizeof(TableNode));
boo = (TableNode *)calloc(1,sizeof(TableNode)); boo = (TableNode *)calloc(1, sizeof(TableNode));
// TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
start->entries = integ; start->entries = integ;
integ->next = addr; integ->next = addr;
@ -450,7 +464,8 @@ SymbolTable *init(SymbolTable *start) {
undefined->additionalinfo = NULL; undefined->additionalinfo = NULL;
undefined->next = NULL; undefined->next = NULL;
//Undefined_function_type_info = CreateFunctionTypeInfo(undefined, undefined); // Undefined_function_type_info = CreateFunctionTypeInfo(undefined,
// undefined);
integ->theType = prime; integ->theType = prime;
addr->theType = prime; addr->theType = prime;
@ -487,67 +502,71 @@ TableNode* boo;
TableNode* recprime; TableNode* recprime;
TableNode* funtypeprime; TableNode* funtypeprime;
*/ */
TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passed in an invalid table node to modify (NULL or undefined).\n"); printf("passed in an invalid table node to modify (NULL or "
return undefined; "undefined).\n");
} return undefined;
if(type == NULL||type==undefined){ }
printf("passed in a NULL or undefined type reference to populate a table node. Invalid.\n"); if (type == NULL || type == undefined) {
return undefined; printf("passed in a NULL or undefined type reference to "
} "populate a table node. Invalid.\n");
if(info == NULL){ return undefined;
printf("passed in a NULL info reference to populate a table node. Invalid.\n"); }
return undefined; if (info == NULL) {
} printf("passed in a NULL info reference to populate a table "
tn->theType = type; "node. Invalid.\n");
tn->additionalinfo = info; return undefined;
//returning reference to modified table node }
return tn; tn->theType = type;
} tn->additionalinfo = info;
// returning reference to modified table node
return tn;
}
int getAdInfoType(TableNode* tn){ int getAdInfoType(TableNode *tn) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passing in NULL or undefined table entry. Invalid\n"); printf("passing in NULL or undefined table entry. Invalid\n");
return -1; return -1;
} }
if(tn->theType == NULL|| tn->theType == undefined){ if (tn->theType == NULL || tn->theType == undefined) {
printf("Entry being passed in has a null or undefined reference for theType. Invalid.\n"); printf("Entry being passed in has a null or undefined "
return -1; "reference for theType. Invalid.\n");
} return -1;
if(strcmp(getName(tn),getName(integ))==0){ }
return TYPE_PRIMITIVE; if (strcmp(getName(tn), getName(integ)) == 0) {
}
if(strcmp(getName(tn),getName(addr))==0){
return TYPE_PRIMITIVE; return TYPE_PRIMITIVE;
} }
if(strcmp(getName(tn),getName(chara))==0){ if (strcmp(getName(tn), getName(addr)) == 0) {
return TYPE_PRIMITIVE; return TYPE_PRIMITIVE;
} }
if(strcmp(getName(tn),getName(stri))==0){ if (strcmp(getName(tn), getName(chara)) == 0) {
return TYPE_PRIMITIVE;
}
if (strcmp(getName(tn), getName(stri)) == 0) {
return TYPE_ARRAY; return TYPE_ARRAY;
} }
if(strcmp(getName(tn),getName(boo))==0){ if (strcmp(getName(tn), getName(boo)) == 0) {
return TYPE_PRIMITIVE; return TYPE_PRIMITIVE;
} }
if(strcmp(getName(tn),getName(recprime))==0){ if (strcmp(getName(tn), getName(recprime)) == 0) {
return TYPE_RECORD; return TYPE_RECORD;
} }
if(strcmp(getName(tn),getName(funtypeprime))==0){ if (strcmp(getName(tn), getName(funtypeprime)) == 0) {
return TYPE_FUNCTION_TYPE; return TYPE_FUNCTION_TYPE;
} }
if(strcmp(getName(tn),getName(arrayprim))==0){ if (strcmp(getName(tn), getName(arrayprim)) == 0) {
return TYPE_ARRAY; return TYPE_ARRAY;
} }
if(strcmp(getName(tn),getName(undefined))==0){ if (strcmp(getName(tn), getName(undefined)) == 0) {
return TYPE_UNDEFINED; return TYPE_UNDEFINED;
} } else {
else{
return TYPE_FUNCTION_DECLARATION; return TYPE_FUNCTION_DECLARATION;
} }
} }
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, AdInfo *ad) { TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
AdInfo *ad) {
if (table == NULL) { if (table == NULL) {
printf("Null reference to table"); printf("Null reference to table");
@ -561,11 +580,12 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, AdInfo *
return NULL; return NULL;
} }
*/ */
if (typeOf == NULL||typeOf == undefined) { if (typeOf == NULL || typeOf == undefined) {
printf("This is not pointing to a proper definition (either NULL or undefined)\n"); printf("This is not pointing to a proper definition (either "
"NULL or undefined)\n");
return undefined; return undefined;
} }
TableNode *newEntry = (TableNode *)calloc(1,sizeof(TableNode)); TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
newEntry->theType = typeOf /*topDef*/; newEntry->theType = typeOf /*topDef*/;
newEntry->theName = id; newEntry->theName = id;
newEntry->additionalinfo = ad; newEntry->additionalinfo = ad;
@ -581,74 +601,85 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, AdInfo *
} }
char *getType(TableNode *tn) { char *getType(TableNode *tn) {
if(tn == NULL||tn==undefined){ if (tn == NULL || tn == undefined) {
printf("passed a NULL or undefined table entry to getType\n"); printf("passed a NULL or undefined table entry to getType\n");
return getName(undefined); return getName(undefined);
}
if(tn->theType == NULL|| tn->theType == undefined){
printf("type of entry is currently NULL or undefined type \n");
return getName(undefined);
}
return tn->theType->theName; }
char *getName(TableNode *tn) {
if(tn == NULL||tn==undefined){
//printf("passed a NULL or undefined table entry to getName\n");
return undefined->theName;
} }
if(tn->theName == NULL){ if (tn->theType == NULL || tn->theType == undefined) {
//printf("name of entry is currently NULL, undefined \n"); printf("type of entry is currently NULL or undefined type \n");
return undefined->theName; return getName(undefined);
} }
return tn->theName; return tn->theType->theName;
} }
int getLine(SymbolTable *st) { char *getName(TableNode *tn) {
if(st == NULL){ if (tn == NULL || tn == undefined) {
printf("passed a NULL symbol table to getLine function. Invalid.\n"); // printf("passed a NULL or undefined table entry to
// getName\n");
return undefined->theName;
}
if (tn->theName == NULL) {
// printf("name of entry is currently NULL, undefined \n");
return undefined->theName;
}
return tn->theName;
}
int getLine(SymbolTable *st) {
if (st == NULL) {
printf("passed a NULL symbol table to getLine function. "
"Invalid.\n");
return -1; return -1;
} }
return st->Line_Number; } return st->Line_Number;
int getColumn(SymbolTable *st) { }
if(st == NULL){ int getColumn(SymbolTable *st) {
printf("passed a NULL symbol table to getColumn function. Invalid.\n"); if (st == NULL) {
printf("passed a NULL symbol table to getColumn function. "
"Invalid.\n");
return -1; return -1;
} }
return st->Column_Number; } return st->Column_Number;
TableNode* addName(TableNode *tn, char* str){ }
if(tn == NULL||tn==undefined){ TableNode *addName(TableNode *tn, char *str) {
printf("passed a Null or undefined table node to the addName function. Invalid./n"); if (tn == NULL || tn == undefined) {
return undefined; printf("passed a Null or undefined table node to the addName "
} "function. Invalid./n");
if(tn->theName != NULL){
printf("Name doesn't look like it is empty before you change. Are you sure you need to update name?/n");
return undefined; return undefined;
} }
if(str == NULL){ if (tn->theName != NULL) {
printf("passed a NULL string to the addName function. Invalid./n"); printf("Name doesn't look like it is empty before you change. "
"Are you sure you need to update name?/n");
return undefined; return undefined;
} }
if (str == NULL) {
printf(
"passed a NULL string to the addName function. Invalid./n");
return undefined;
}
tn->theName = str; tn->theName = str;
return tn; return tn;
} }
SymbolTable* setLineNumber(SymbolTable *st,int line){ SymbolTable *setLineNumber(SymbolTable *st, int line) {
if(st == NULL){ if (st == NULL) {
printf("passed a Null Symbol Table to the setLineNumber function. Invalid./n"); printf("passed a Null Symbol Table to the setLineNumber "
return st; "function. Invalid./n");
}
st->Line_Number = line;
return st;
}
SymbolTable* setColumnNumber(SymbolTable *st,int column){
if(st == NULL){
printf("passed a Null Symbol Table to the setColumnNumber function. Invalid./n");
return st; return st;
}
st->Line_Number = column;
return st;
} }
st->Line_Number = line;
return st;
}
SymbolTable *setColumnNumber(SymbolTable *st, int column) {
if (st == NULL) {
printf("passed a Null Symbol Table to the setColumnNumber "
"function. Invalid./n");
return st;
}
st->Line_Number = column;
return st;
}
/* /*
//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) {
@ -684,9 +715,9 @@ names\n"); return NULL;
} }
*/ */
//only check table that is given // only check table that is given
TableNode *table_lookup(SymbolTable *table, char *x) { TableNode *table_lookup(SymbolTable *table, char *x) {
if(table == NULL) { if (table == NULL) {
printf("passed in empty scope. error.\n"); printf("passed in empty scope. error.\n");
return undefined; return undefined;
} }
@ -698,23 +729,24 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
} }
return undefined; 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) {
printf("passed in empty scope. error.\n"); printf("passed in empty scope. error.\n");
return undefined; return undefined;
} }
TableNode *ret = table_lookup(table, x); TableNode *ret = table_lookup(table, x);
if (ret != NULL && ret != undefined) { 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" printf("could not find %s in scope that started at line %d and column "
,x,getLine(table),getColumn(table)); "%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);
} }
/* /*
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "PARENT", "TYPE", "Extra annotation");
@ -767,7 +799,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
} }
}*/ }*/
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "PARENT", "TYPE", "Extra annotation");
@ -790,102 +822,127 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
current_scope, parant_scope, "", "Empty Scope"); current_scope, parant_scope, "", "Empty Scope");
} }
for (;entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = entrie->next) {
if (getAdInfoType(entrie) == TYPE_ARRAY){ if (getAdInfoType(entrie) == TYPE_ARRAY) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : : %-21d -> %-21s: %-28s\n",
entrie->theName, current_scope,
entrie->additionalinfo->ArrayAdInfo->numofdimensions,
entrie->additionalinfo->ArrayAdInfo->typeofarray->theName,
"Type of Array");
} else {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21d -> %-21s: %-28s\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, fprintf(file_ptr,
"%-17s: %06d : :%-21s: elements-%-28d\n", "%-17s: %06d : : %-21d -> "
entrie->theName, current_scope, "%-21s: %-28s\n",
"record", entrie->theName, current_scope,
entrie->additionalinfo->RecAdInfo->numofelements); entrie->additionalinfo->ArrayAdInfo
} else { ->numofdimensions,
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: elements-%-28d\n", entrie->additionalinfo->ArrayAdInfo
entrie->theName, current_scope, parant_scope, ->typeofarray->theName,
"record", "Type of Array");
entrie->additionalinfo->RecAdInfo->numofelements); } else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21d -> %-21s: "
"%-28s\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 (getAdInfoType(entrie) == TYPE_PRIMITIVE){ if (parant_scope == 0) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-17s: %06d : :%-21s: size-%-28d bytes\n", "%-17s: %06d : :%-21s: "
entrie->theName, current_scope, "elements-%-28d\n",
"Primitive", entrie->theName, current_scope,
entrie->additionalinfo->PrimAdInfo->size); "record",
} else { entrie->additionalinfo->RecAdInfo
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: size-%-28d bytes\n", ->numofelements);
entrie->theName, current_scope, parant_scope, } else {
"Primitive", fprintf(file_ptr,
entrie->additionalinfo->PrimAdInfo->size); "%-17s: %06d : %06d : %-21s: "
"elements-%-28d\n",
entrie->theName, current_scope,
parant_scope, "record",
entrie->additionalinfo->RecAdInfo
->numofelements);
}
} }
} if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE){ if (parant_scope == 0) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(
"%-17s: %06d : : %-21s -> %-21s: %-28s\n", file_ptr,
entrie->theName, current_scope, "%-17s: %06d : :%-21s: size-%-28d "
entrie->additionalinfo->FunTypeAdInfo->parameter->theName, "bytes\n",
entrie->additionalinfo->FunTypeAdInfo->returntype->theName, entrie->theName, current_scope, "Primitive",
"Type of Function"); entrie->additionalinfo->PrimAdInfo->size);
} else { } else {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s -> %-21s: %-28s\n", fprintf(
entrie->theName, current_scope, parant_scope, file_ptr,
entrie->additionalinfo->FunTypeAdInfo->parameter->theName, "%-17s: %06d : %06d : %-21s: size-%-28d "
entrie->additionalinfo->FunTypeAdInfo->returntype->theName, "bytes\n",
"Type of Function"); entrie->theName, current_scope,
parant_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
}
} }
} if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION){ if (parant_scope == 0) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n", "%-17s: %06d : : %-21s -> "
entrie->theName, current_scope, "%-21s: %-28s\n",
getType(entrie), entrie->theName, current_scope,
"Function"); entrie->additionalinfo->FunTypeAdInfo
} else { ->parameter->theName,
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->additionalinfo->FunTypeAdInfo
entrie->theName, current_scope, parant_scope, ->returntype->theName,
getType(entrie), "Type of Function");
"Function"); } else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s -> %-21s: "
"%-28s\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 (getAdInfoType(entrie) == TYPE_UNDEFINED){ if (parant_scope == 0) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n", "%-17s: %06d : :%-21s: %-28s\n",
entrie->theName, current_scope, entrie->theName, current_scope,
"undefined", getType(entrie), "Function");
"undefined entry"); } else {
} else { fprintf(file_ptr,
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope, parant_scope, entrie->theName, current_scope,
"undefined", parant_scope, getType(entrie),
"undefined entry"); "Function");
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-17s: %06d : :%-21s: %-28s\n",
entrie->theName, current_scope,
"undefined", "undefined entry");
} else {
fprintf(file_ptr,
"%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope,
parant_scope, "undefined",
"undefined entry");
}
} }
} }
}
if (table->Children_Scope != NULL) { if (table->Children_Scope != NULL) {
ListOfTable *node = table->Children_Scope; ListOfTable *node = table->Children_Scope;
for (; node != NULL; node = node->next) { for (; node != NULL; node = node->next) {
@ -898,12 +955,12 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
"----------------------\n"); "----------------------\n");
} }
} }
//get top most symbol table // get top most symbol table
SymbolTable *getAncestor(SymbolTable *table) { SymbolTable *getAncestor(SymbolTable *table) {
if(table == NULL){ if (table == NULL) {
printf("passing a NULL reference to getAncestor. Invalid.\n"); printf("passing a NULL reference to getAncestor. Invalid.\n");
return NULL; return NULL;
} }
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
// if table has no parent, return itself // if table has no parent, return itself
return table; return table;
@ -941,16 +998,17 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) {
return scope; return scope;
} }
//almost certainly don't need to use the below function since type checking happens by passing types up the grammar // almost certainly don't need to use the below function since type checking
// happens by passing types up the grammar
bool typeCheck(char *firstID, char *secondID) { bool typeCheck(char *firstID, char *secondID) {
TableNode *entry1 = look_up(cur, firstID); TableNode *entry1 = look_up(cur, firstID);
TableNode *entry2 = look_up(cur, secondID); TableNode *entry2 = look_up(cur, secondID);
if (entry1 == NULL|| entry1 == undefined) { if (entry1 == NULL || entry1 == undefined) {
printf("first type not defined\n"); printf("first type not defined\n");
return false; return false;
} }
if (entry2 == NULL|| entry2 == undefined) { if (entry2 == NULL || entry2 == undefined) {
printf("second type not defined\n"); printf("second type not defined\n");
return false; return false;
} }
@ -1001,4 +1059,29 @@ TableNode *getNextEntry(TableNode *tn) { return tn->next; }
printf("The type of the first entry is %s\n",First_Entry->theType); printf("The type of the first entry is %s\n",First_Entry->theType);
return 0; return 0;
} }
*/ */
char *getLineStr();
char *COLOR_RED = "\033[0;31m";
char *COLOR_GREEN = "\033[0;32m";
char *COLOR_ORANGE = "\033[0;33m";
char *COLOR_BLUE = "\033[0;34m";
char *COLOR_PURPLE = "\033[0;35m";
char *COLOR_CYAN = "\033[0;36m";
char *COLOR_LIGHTGRAY = "\033[0;37m";
char *COLOR_DARKGRAY = "\033[1;30m";
char *COLOR_LIGHTRED = "\033[1;31m";
char *COLOR_LIGHTGREEN = "\033[1;32m";
char *COLOR_YELLOW = "\033[1;33m";
char *COLOR_LIGHTBLUE = "\033[1;34m";
char *COLOR_LIGHTPURPLE = "\033[1;35m";
char *COLOR_LIGHTCYAN = "\033[1;36m";
char *COLOR_WHITE = "\033[1;37m";
bool DEBUG = false;
char *getLineStr(int d) {
char *new_text = malloc(100);
sprintf(new_text, "%s[%d]%s ", COLOR_DARKGRAY, d, COLOR_WHITE);
return new_text;
}

18
test.sh
View File

@ -48,7 +48,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then if [ -f "$file" ]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -60,7 +60,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then if [ -f "$file" ]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -72,7 +72,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then if [ -f "$file" ]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -84,7 +84,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then if [ -f "$file" ]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n${WHITE}" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n${WHITE}"
switchfunc switchfunc
fi fi
@ -106,7 +106,7 @@ else
if [ -f "$1" ]; then if [ -f "$1" ]; then
filename=$(basename -- "$1") filename=$(basename -- "$1")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$1" ./alpha -st "$1" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}"
exit 1 exit 1
else else
@ -121,7 +121,7 @@ else
if [[ "$file" == *"$1"* ]]; then if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -132,7 +132,7 @@ else
if [[ "$file" == *"$1"* ]]; then if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -143,7 +143,7 @@ else
if [[ "$file" == *"$1"* ]]; then if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi
@ -154,7 +154,7 @@ else
if [[ "$file" == *"$1"* ]]; then if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file") filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}" echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" ./alpha -st "$file" -debug
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n" echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc switchfunc
fi fi