From d587ed9f3bc7639ca5435363ecf80e2cd81774cd Mon Sep 17 00:00:00 2001 From: Scarlett Date: Fri, 28 Mar 2025 23:33:08 -0400 Subject: [PATCH] Updated --- Makefile | 4 +- src/grammar.y | 2 +- src/runner.c | 13 +- src/symbol_table.c | 635 ++++++++++-------- test.sh | 18 +- .../{ => test}/sp2_function_types.alpha | 0 6 files changed, 383 insertions(+), 289 deletions(-) rename tests/sprint2/{ => test}/sp2_function_types.alpha (100%) diff --git a/Makefile b/Makefile index 0f04da9..4b92e65 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ debug: clean compiler test: chmod +x ./check.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 sp3 ./test.sh sp4 @@ -46,7 +46,7 @@ test: test-s1: chmod +x ./check.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 test-s2: diff --git a/src/grammar.y b/src/grammar.y index b7438bd..8ff8a8f 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -122,7 +122,7 @@ prototype: definition: TYPE ID COLON { - printf("Currently see a record definition for %s\n", $2); + printf("Currently see a record definition for %s\n", $2); tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); if (table_lookup(getAncestor(cur), $2) == NULL) { printf("rec not found \n"); diff --git a/src/runner.c b/src/runner.c index 68198f8..a87b527 100644 --- a/src/runner.c +++ b/src/runner.c @@ -9,8 +9,19 @@ extern TableNode *addr; extern TableNode *chara; extern TableNode *stri; extern TableNode *boo; +extern bool DEBUG; +extern char * COLOR_YELLOW; +extern char * COLOR_BLUE; +extern char * COLOR_WHITE; 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) { fprintf(stderr, INVALID); return -1; @@ -221,4 +232,4 @@ void exit_scope() { return; } cur = cur->Parent_Scope; -} +} \ No newline at end of file diff --git a/src/symbol_table.c b/src/symbol_table.c index 26d8798..8fa87d2 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -19,7 +19,7 @@ TableNode *boo; TableNode *recprime; TableNode *funtypeprime; TableNode *undefined; -//AdInfo *Undefined_function_type_info; +// AdInfo *Undefined_function_type_info; typedef enum { // 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 // size TYPE_PRIMITIVE = 6, - //likely NULL - TYPE_ALL_ELSE = 7, - TYPE_UNDEFINED = 8 + // likely NULL + TYPE_ALL_ELSE = 7, + TYPE_UNDEFINED = 8 } types; @@ -106,14 +106,15 @@ AdInfo *CreatePrimitiveInfo(int size) { // only gets the size of a primitive type int getPrimSize(TableNode *definition) { - if (definition == NULL || definition == undefined){ - printf("passed an NULL entry to getPrimSize function. Invalid.\n"); - return -1; - } - if (definition->additionalinfo == NULL){ - printf("node has NULL additionalinfo. Invalid.\n"); - return -1; - } + if (definition == NULL || definition == undefined) { + printf( + "passed an NULL entry to getPrimSize function. Invalid.\n"); + return -1; + } + if (definition->additionalinfo == NULL) { + printf("node has NULL additionalinfo. Invalid.\n"); + return -1; + } if (strcmp(getType(definition), "primitive") != 0) { printf("not checking the size of a primitive -- invalid op\n"); return 0; @@ -134,8 +135,9 @@ int getPrimSize(TableNode *definition) { // 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 AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) { - if(type == NULL || type == undefined){ - printf("passed a NULL or undefined type reference to CreateArrayInfo. Invalid.\n"); + if (type == NULL || type == undefined) { + printf("passed a NULL or undefined type reference to " + "CreateArrayInfo. Invalid.\n"); return NULL; } 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 int getNumArrDim(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getNumArrDim function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getNumArrDim " + "function. Invalid.\n"); return -1; } 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 // the entry of that type TableNode *getArrType(TableNode *definition) { - if(definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getArrType function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getArrType " + "function. Invalid.\n"); return NULL; } 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 // anyways. int getRecLength(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getRecLength function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getRecLength " + "function. Invalid.\n"); return -1; } 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 SymbolTable *getRecList(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getRecList function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getRecList " + "function. Invalid.\n"); return NULL; } if (strcmp(getType(definition), "record") != 0) { @@ -212,34 +218,34 @@ SymbolTable *getRecList(TableNode *definition) { return definition->additionalinfo->RecAdInfo->recordScope; } -TableNode* setRecSize(TableNode* tn, int n){ - if(tn == NULL||tn==undefined){ - printf("passed in NULL entry for setRecSize. Invalid\n"); - return undefined; - } - tn->additionalinfo->RecAdInfo->numofelements = n; - return tn; - } - -int getRecSize(SymbolTable* tn){ - if(tn == NULL){ +TableNode *setRecSize(TableNode *tn, int n) { + if (tn == NULL || tn == undefined) { + printf("passed in NULL entry for setRecSize. Invalid\n"); + return undefined; + } + tn->additionalinfo->RecAdInfo->numofelements = n; + return tn; +} + +int getRecSize(SymbolTable *tn) { + if (tn == NULL) { printf("passed in NULL SymbolTable for getRecSize. Invalid\n"); return -1; } - int s = 0; - TableNode* cur = getFirstEntry(tn); - if (cur != NULL){ - while(getNextEntry(cur) != NULL){ - s++; - cur = getNextEntry(cur); - } - return s; + int s = 0; + TableNode *cur = getFirstEntry(tn); + if (cur != NULL) { + while (getNextEntry(cur) != NULL) { + s++; + cur = getNextEntry(cur); + } + return s; } -return -1; - } + return -1; +} // 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 // multiple inputs Below function also has the line number where the function is // 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 // out in table if needed) int getStartLine(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getStartLine function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getStartLine " + "function. Invalid.\n"); return -1; } if (strcmp(getType(definition), "primitive function") != 0) { @@ -266,19 +273,21 @@ int getStartLine(TableNode *definition) { return definition->additionalinfo->FunDecAdInfo->startlinenumber; } -TableNode* setStartLine(TableNode* tn, int start){ - if(tn == NULL||tn==undefined){ - printf("passing in a NULL or undefined entry to setStartLine. invalid\n"); - return undefined; - } - tn->additionalinfo->FunDecAdInfo->startlinenumber = start; - return tn; - } +TableNode *setStartLine(TableNode *tn, int start) { + if (tn == NULL || tn == undefined) { + printf("passing in a NULL or undefined entry to setStartLine. " + "invalid\n"); + return undefined; + } + tn->additionalinfo->FunDecAdInfo->startlinenumber = start; + return tn; +} // checks if "as" keyword was used for function definition. Either 0 or 1 for // not used or used. bool getAsKeyword(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getAsKeyword function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getAsKeyword " + "function. Invalid.\n"); return false; } if (strcmp(getType(definition), "primitive function") != 0) { @@ -289,23 +298,26 @@ bool getAsKeyword(TableNode *definition) { return definition->additionalinfo->FunDecAdInfo->regularoras; } -TableNode* setAsKeyword(TableNode* tn, bool as){ - if(tn == NULL||tn==undefined){ - printf("passing in a NULL or undefined entry to setAsKeyword. invalid\n"); +TableNode *setAsKeyword(TableNode *tn, bool as) { + if (tn == NULL || tn == undefined) { + printf("passing in a NULL or undefined entry to setAsKeyword. " + "invalid\n"); return undefined; } - tn->additionalinfo->FunDecAdInfo->regularoras = as; - return tn; - } - + tn->additionalinfo->FunDecAdInfo->regularoras = as; + return tn; +} + // stores the type of a function (parameter type and return type) AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) { - if(parameter == NULL||parameter == undefined){ - printf("passed a NULL or undefined parameter to CreateFunctionTypeInfo. Invalid.\n"); + if (parameter == NULL || parameter == undefined) { + printf("passed a NULL or undefined parameter to " + "CreateFunctionTypeInfo. Invalid.\n"); return NULL; } - if(returntype == NULL||returntype == undefined){ - printf("passed a NULL or undefined return type to CreateFunctionTypeInfo. Invalid.\n"); + if (returntype == NULL || returntype == undefined) { + printf("passed a NULL or undefined return type to " + "CreateFunctionTypeInfo. Invalid.\n"); return NULL; } AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); @@ -317,8 +329,9 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) { } // returns parameter type of a function TableNode *getParameter(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getParameter function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getParameter " + "function. Invalid.\n"); return undefined; } if (strcmp(getType(definition), "primitive function type") != 0) { @@ -330,8 +343,9 @@ TableNode *getParameter(TableNode *definition) { } // returns return type of a function TableNode *getReturn(TableNode *definition) { - if (definition == NULL|| definition == undefined){ - printf("passed an NULL or undefined entry to getReturn function. Invalid.\n"); + if (definition == NULL || definition == undefined) { + printf("passed an NULL or undefined entry to getReturn " + "function. Invalid.\n"); return NULL; } 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"); return NULL; } - integ = (TableNode *)calloc(1,sizeof(TableNode)); - addr = (TableNode *)calloc(1,sizeof(TableNode)); - chara = (TableNode *)calloc(1,sizeof(TableNode)); - stri = (TableNode *)calloc(1,sizeof(TableNode)); - boo = (TableNode *)calloc(1,sizeof(TableNode)); + integ = (TableNode *)calloc(1, sizeof(TableNode)); + addr = (TableNode *)calloc(1, sizeof(TableNode)); + chara = (TableNode *)calloc(1, sizeof(TableNode)); + stri = (TableNode *)calloc(1, sizeof(TableNode)); + boo = (TableNode *)calloc(1, sizeof(TableNode)); // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); start->entries = integ; integ->next = addr; @@ -450,7 +464,8 @@ SymbolTable *init(SymbolTable *start) { undefined->additionalinfo = NULL; undefined->next = NULL; - //Undefined_function_type_info = CreateFunctionTypeInfo(undefined, undefined); + // Undefined_function_type_info = CreateFunctionTypeInfo(undefined, + // undefined); integ->theType = prime; addr->theType = prime; @@ -487,67 +502,71 @@ TableNode* boo; TableNode* recprime; TableNode* funtypeprime; */ -TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ - if(tn == NULL||tn==undefined){ - printf("passed in an invalid table node to modify (NULL or 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"); - return undefined; - } - if(info == NULL){ - printf("passed in a NULL info reference to populate a table node. Invalid.\n"); - return undefined; - } - tn->theType = type; - tn->additionalinfo = info; - //returning reference to modified table node - return tn; - } +TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) { + if (tn == NULL || tn == undefined) { + printf("passed in an invalid table node to modify (NULL or " + "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"); + return undefined; + } + if (info == NULL) { + printf("passed in a NULL info reference to populate a table " + "node. Invalid.\n"); + return undefined; + } + tn->theType = type; + tn->additionalinfo = info; + // returning reference to modified table node + return tn; +} -int getAdInfoType(TableNode* tn){ - if(tn == NULL||tn==undefined){ - printf("passing in NULL or undefined table entry. Invalid\n"); - return -1; - } - if(tn->theType == NULL|| tn->theType == undefined){ - printf("Entry being passed in has a null or undefined reference for theType. Invalid.\n"); - return -1; - } - if(strcmp(getName(tn),getName(integ))==0){ - return TYPE_PRIMITIVE; - } - if(strcmp(getName(tn),getName(addr))==0){ +int getAdInfoType(TableNode *tn) { + if (tn == NULL || tn == undefined) { + printf("passing in NULL or undefined table entry. Invalid\n"); + return -1; + } + if (tn->theType == NULL || tn->theType == undefined) { + printf("Entry being passed in has a null or undefined " + "reference for theType. Invalid.\n"); + return -1; + } + if (strcmp(getName(tn), getName(integ)) == 0) { return TYPE_PRIMITIVE; } - if(strcmp(getName(tn),getName(chara))==0){ + if (strcmp(getName(tn), getName(addr)) == 0) { 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; } - if(strcmp(getName(tn),getName(boo))==0){ + if (strcmp(getName(tn), getName(boo)) == 0) { return TYPE_PRIMITIVE; } - if(strcmp(getName(tn),getName(recprime))==0){ + if (strcmp(getName(tn), getName(recprime)) == 0) { return TYPE_RECORD; } - if(strcmp(getName(tn),getName(funtypeprime))==0){ + if (strcmp(getName(tn), getName(funtypeprime)) == 0) { return TYPE_FUNCTION_TYPE; } - if(strcmp(getName(tn),getName(arrayprim))==0){ + if (strcmp(getName(tn), getName(arrayprim)) == 0) { return TYPE_ARRAY; } - if(strcmp(getName(tn),getName(undefined))==0){ - return TYPE_UNDEFINED; - } - else{ + if (strcmp(getName(tn), getName(undefined)) == 0) { + return TYPE_UNDEFINED; + } else { 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) { printf("Null reference to table"); @@ -561,11 +580,12 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, AdInfo * return NULL; } */ - if (typeOf == NULL||typeOf == undefined) { - printf("This is not pointing to a proper definition (either NULL or undefined)\n"); + if (typeOf == NULL || typeOf == undefined) { + printf("This is not pointing to a proper definition (either " + "NULL or undefined)\n"); return undefined; } - TableNode *newEntry = (TableNode *)calloc(1,sizeof(TableNode)); + TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); newEntry->theType = typeOf /*topDef*/; newEntry->theName = id; newEntry->additionalinfo = ad; @@ -581,74 +601,85 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, AdInfo * } char *getType(TableNode *tn) { - if(tn == NULL||tn==undefined){ - printf("passed a NULL or undefined table entry to getType\n"); - 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 == NULL || tn == undefined) { + printf("passed a NULL or undefined table entry to getType\n"); + return getName(undefined); } - if(tn->theName == NULL){ - //printf("name of entry is currently NULL, undefined \n"); - return undefined->theName; + if (tn->theType == NULL || tn->theType == undefined) { + printf("type of entry is currently NULL or undefined type \n"); + return getName(undefined); } - return tn->theName; + return tn->theType->theName; } -int getLine(SymbolTable *st) { - if(st == NULL){ - printf("passed a NULL symbol table to getLine function. Invalid.\n"); +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) { + // 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 st->Line_Number; } -int getColumn(SymbolTable *st) { - if(st == NULL){ - printf("passed a NULL symbol table to getColumn function. Invalid.\n"); + return st->Line_Number; +} +int getColumn(SymbolTable *st) { + if (st == NULL) { + printf("passed a NULL symbol table to getColumn function. " + "Invalid.\n"); return -1; } - return st->Column_Number; } -TableNode* addName(TableNode *tn, char* str){ - if(tn == NULL||tn==undefined){ - printf("passed a Null or undefined table node to the addName function. Invalid./n"); - return undefined; - } - 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 st->Column_Number; +} +TableNode *addName(TableNode *tn, char *str) { + if (tn == NULL || tn == undefined) { + printf("passed a Null or undefined table node to the addName " + "function. Invalid./n"); return undefined; - } - if(str == NULL){ - printf("passed a NULL string 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; - } + } + if (str == NULL) { + printf( + "passed a NULL string to the addName function. Invalid./n"); + return undefined; + } tn->theName = str; return tn; } -SymbolTable* setLineNumber(SymbolTable *st,int line){ - if(st == NULL){ - printf("passed a Null Symbol Table to the setLineNumber function. Invalid./n"); - 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"); +SymbolTable *setLineNumber(SymbolTable *st, int line) { + if (st == NULL) { + printf("passed a Null Symbol Table to the setLineNumber " + "function. Invalid./n"); 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 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) { - if(table == NULL) { + if (table == NULL) { printf("passed in empty scope. error.\n"); return undefined; } @@ -698,23 +729,24 @@ TableNode *table_lookup(SymbolTable *table, char *x) { } return undefined; } -//check current table and all parents +// check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { - printf("passed in empty scope. error.\n"); + printf("passed in empty scope. error.\n"); return undefined; } TableNode *ret = table_lookup(table, x); 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)); + 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); } /* void print_symbol_table(SymbolTable *table, FILE *file_ptr) { - + if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "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) { - + if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "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", "", current_scope, parant_scope, "", "Empty Scope"); } - for (;entrie != NULL; entrie = entrie->next) { - if (getAdInfoType(entrie) == TYPE_ARRAY){ - 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) { + for (; entrie != NULL; entrie = entrie->next) { + if (getAdInfoType(entrie) == TYPE_ARRAY) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-17s: %06d : :%-21s: elements-%-28d\n", - entrie->theName, current_scope, - "record", - entrie->additionalinfo->RecAdInfo->numofelements); - } else { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: elements-%-28d\n", - entrie->theName, current_scope, parant_scope, - "record", - entrie->additionalinfo->RecAdInfo->numofelements); + 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_PRIMITIVE){ - if (parant_scope == 0) { + if (getAdInfoType(entrie) == TYPE_RECORD) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-17s: %06d : :%-21s: size-%-28d bytes\n", - entrie->theName, current_scope, - "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } else { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: size-%-28d bytes\n", - entrie->theName, current_scope, parant_scope, - "Primitive", - entrie->additionalinfo->PrimAdInfo->size); + fprintf(file_ptr, + "%-17s: %06d : :%-21s: " + "elements-%-28d\n", + entrie->theName, current_scope, + "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } else { + fprintf(file_ptr, + "%-17s: %06d : %06d : %-21s: " + "elements-%-28d\n", + entrie->theName, current_scope, + parant_scope, "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } } - } - if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE){ - if (parant_scope == 0) { + if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-17s: %06d : : %-21s -> %-21s: %-28s\n", - entrie->theName, current_scope, - entrie->additionalinfo->FunTypeAdInfo->parameter->theName, - entrie->additionalinfo->FunTypeAdInfo->returntype->theName, - "Type of 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"); + fprintf( + file_ptr, + "%-17s: %06d : :%-21s: size-%-28d " + "bytes\n", + entrie->theName, current_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } else { + fprintf( + file_ptr, + "%-17s: %06d : %06d : %-21s: size-%-28d " + "bytes\n", + entrie->theName, current_scope, + parant_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } } - } - if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION){ - if (parant_scope == 0) { + if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-17s: %06d : :%-21s: %-28s\n", - entrie->theName, current_scope, - getType(entrie), - "Function"); - } else { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", - entrie->theName, current_scope, parant_scope, - getType(entrie), - "Function"); + fprintf(file_ptr, + "%-17s: %06d : : %-21s -> " + "%-21s: %-28s\n", + entrie->theName, current_scope, + entrie->additionalinfo->FunTypeAdInfo + ->parameter->theName, + entrie->additionalinfo->FunTypeAdInfo + ->returntype->theName, + "Type of 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_UNDEFINED){ - if (parant_scope == 0) { + if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) { + 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"); + fprintf(file_ptr, + "%-17s: %06d : :%-21s: %-28s\n", + entrie->theName, current_scope, + getType(entrie), "Function"); + } else { + fprintf(file_ptr, + "%-17s: %06d : %06d : %-21s: %-28s\n", + entrie->theName, current_scope, + parant_scope, getType(entrie), + "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) { ListOfTable *node = table->Children_Scope; for (; node != NULL; node = node->next) { @@ -898,12 +955,12 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { "----------------------\n"); } } -//get top most symbol table +// get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { - if(table == NULL){ - printf("passing a NULL reference to getAncestor. Invalid.\n"); - return NULL; - } + if (table == NULL) { + printf("passing a NULL reference to getAncestor. Invalid.\n"); + return NULL; + } if (table->Parent_Scope == NULL) { // if table has no parent, return itself return table; @@ -941,16 +998,17 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) { 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) { TableNode *entry1 = look_up(cur, firstID); TableNode *entry2 = look_up(cur, secondID); - if (entry1 == NULL|| entry1 == undefined) { + if (entry1 == NULL || entry1 == undefined) { printf("first type not defined\n"); return false; } - if (entry2 == NULL|| entry2 == undefined) { + if (entry2 == NULL || entry2 == undefined) { printf("second type not defined\n"); 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); return 0; } - */ \ No newline at end of file + */ + +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; +} \ No newline at end of file diff --git a/test.sh b/test.sh index e35809e..9c32bed 100755 --- a/test.sh +++ b/test.sh @@ -48,7 +48,7 @@ if [ $# -eq 0 ]; then if [ -f "$file" ]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -60,7 +60,7 @@ if [ $# -eq 0 ]; then if [ -f "$file" ]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -72,7 +72,7 @@ if [ $# -eq 0 ]; then if [ -f "$file" ]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -84,7 +84,7 @@ if [ $# -eq 0 ]; then if [ -f "$file" ]; then filename=$(basename -- "$file") 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}" switchfunc fi @@ -106,7 +106,7 @@ else if [ -f "$1" ]; then filename=$(basename -- "$1") 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}" exit 1 else @@ -121,7 +121,7 @@ else if [[ "$file" == *"$1"* ]]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -132,7 +132,7 @@ else if [[ "$file" == *"$1"* ]]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -143,7 +143,7 @@ else if [[ "$file" == *"$1"* ]]; then filename=$(basename -- "$file") 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" switchfunc fi @@ -154,7 +154,7 @@ else if [[ "$file" == *"$1"* ]]; then filename=$(basename -- "$file") 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" switchfunc fi diff --git a/tests/sprint2/sp2_function_types.alpha b/tests/sprint2/test/sp2_function_types.alpha similarity index 100% rename from tests/sprint2/sp2_function_types.alpha rename to tests/sprint2/test/sp2_function_types.alpha