From 1765878b85c9846e0b19134cdad9378fb6f1ace9 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 3 Apr 2025 17:51:18 -0400 Subject: [PATCH 01/45] rebased made progress on type checking function definitions and calls --- src/grammar.y | 85 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index b3f2334..cbb9ec1 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -199,33 +199,45 @@ definition: } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); } else { + setStartLine(node, @1.first_line); + setAsKeyword(node, false); + } + cur = CreateScope(cur, 0, 0); + } L_PAREN ID { + printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", + $1,$4); + CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); + }R_PAREN ASSIGN sblock + | ID { + TableNode *node = table_lookup(getAncestor(cur), $1); + if (node == undefined) { + printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ + printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + } + else { + printdebug("setting as keyword to true"); setStartLine(node, @1.first_line); setAsKeyword(node, true); } cur = CreateScope(cur, 0, 0); - } - AS L_PAREN - { + }AS L_PAREN { TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - printdebug("%s", getType(parameter)); + printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); - } else if(getAdInfoType(parameter) != TYPE_RECORD) { + }else if(getAdInfoType(parameter) != TYPE_RECORD){ printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime)); printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); - } else { - for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)) { - CreateEntry(cur, entry, NULL, NULL); + }else { + for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ + printdebug("creating entry of type %s for function", getType(entry)); + CreateEntry(cur, entry, "undefined", NULL); } } - } - idlist - { - printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$6); - } - R_PAREN ASSIGN sblock - ; - + } idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", + $1,$6);} R_PAREN ASSIGN sblock + ; function_declaration: @@ -403,22 +415,22 @@ ablock: argument_list: +<<<<<<< HEAD expression COMMA argument_list { - CreateEntry(cur, look_up(cur, $1), "", NULL); + CreateEntry(cur, look_up(cur, $1), $1, NULL); $$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur, look_up(cur, $1), "", NULL); + CreateEntry(cur, look_up(cur, $1), $1, NULL); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; - // will ALWAYS be a TYPE expression: constant @@ -575,6 +587,7 @@ expression: assignable: ID { + //$$ = $1; $$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); } @@ -588,29 +601,40 @@ assignable: { int type = getAdInfoType(look_up(getParent(cur), $1)); printdebug("%stype is %d", COLOR_PURPLE, type); + printdebug("%s", $1); if (type == TYPE_FUNCTION_DECLARATION) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); - if (getAsKeyword(look_up(getParent(cur), $1))) { - TableNode *param = getParameter(look_up(getParent(cur), $1)); + if (look_up(getParent(cur), $1)->additionalinfo->FunDecAdInfo->regularoras) { + printdebug("as function"); + //char *funtype = getType(look_up(cur, $1)); + printdebug("%s", getType(look_up(cur, $1))); + TableNode *param = getParameter(look_up(cur, getType(look_up(cur, $1)))); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); while (getNextEntry(lastCheckedRef) != NULL) { - lastCheckedRef = getNextEntry(lastCheckedRef); + lastCheckedRef = getNextEntry(lastCheckedRef); } + + //this isn't very efficient, but will hopefully work while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { - printdebug("expected %s expression in function call but got %s at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); + if (strcmp(getName(lastCheckedAct), getType(lastCheckedRef)) != 0) { + printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); + printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); } lastCheckedAct = getNextEntry(lastCheckedAct); TableNode *tn = getFirstEntry(recList); - while (getNextEntry(tn) != lastCheckedRef) { - tn = getNextEntry(tn); - } - lastCheckedRef = tn; - } + + if (tn != lastCheckedRef) { + while (getNextEntry(tn) != lastCheckedRef) { + tn = getNextEntry(tn); + } + lastCheckedRef = tn; + } else {break;} + } + } else { char *expected = getName(getParameter(look_up(getParent(cur), $1))); char *actual = getType(getFirstEntry(cur)); @@ -618,8 +642,7 @@ assignable: printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); } } - - $$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); + $$ = getName(getReturn((look_up(cur, getType(look_up(cur, $1)))))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); } else if (type == TYPE_ARRAY_TYPE) { From 3bae28dfefb5c061c07f5229b186d1a8dbb4bfc4 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 3 Apr 2025 19:04:43 -0400 Subject: [PATCH 02/45] started changing to pass up table nodes --- src/grammar.y | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index cbb9ec1..33599a3 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -47,15 +47,16 @@ %union { int integ; - char * words; + TableNode *tn; + char *words; } %type idlist -%type assignable -%type expression -%type constant -%type id_or_types -%type types +%type assignable +%type expression +%type constant +%type id_or_types +%type types %token ID 101 %token T_INTEGER 201 %token T_ADDRESS 202 @@ -141,36 +142,36 @@ prototype: definition: TYPE ID COLON { - printdebug("Currently see a record definition for %s", $2); + printdebug("Currently see a record definition for %s", $2); tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); - if (table_lookup(getAncestor(cur), $2) == undefined) { + if (look_up(cur, $2) == undefined) { printdebug("rec not found "); } } dblock { - setRecSize(table_lookup(getParent(cur), $2), getRecSize(cur)); + setRecSize(look_up(cur, $2), getRecSize(cur)); cur = getParent(cur); } | TYPE ID COLON C_INTEGER ARROW id_or_types { - printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, $6, $4); - CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6))); - printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, $6); + printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, getName($6), $4); + CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, $6)); + printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName($6)); } | function_declaration | TYPE ID COLON id_or_types ARROW id_or_types { - printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, $4, $6); - CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6))); + printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", getName($2), getName($4), getName($6)); + CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6)); } | ID { - TableNode *node = table_lookup(getAncestor(cur), $1); + TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { @@ -184,11 +185,11 @@ definition: L_PAREN ID { printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", $1,$4); - CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); + CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); } R_PAREN ASSIGN sblock - | ID + | ID //EVERYTHING UP UNTIL THIS POINT HAS BEEN CHECKED FOR $TN TYPES { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { @@ -236,7 +237,7 @@ definition: } } } idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", - $1,$6);} R_PAREN ASSIGN sblock + $1,$6);} R_PAREN ASSIGN sblock //check sblock type ; @@ -343,7 +344,8 @@ declaration: id_or_types: ID { - printdebug("string of id is %s in ID pattern of id_or_type rule.", $1); $$ = $1; + printdebug("string of id is %s in ID pattern of id_or_type rule.", $1); + $$ = look_up(cur, $1); } | types @@ -415,7 +417,6 @@ ablock: argument_list: -<<<<<<< HEAD expression COMMA argument_list { CreateEntry(cur, look_up(cur, $1), $1, NULL); @@ -587,8 +588,7 @@ expression: assignable: ID { - //$$ = $1; - $$ = getType(look_up(cur,$1)); + $$ = look_up(cur,$1); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); } @@ -685,37 +685,37 @@ memOp: constant: C_STRING { - $$ = $1; + $$ = $1; printdebug("string of C_STRING in constant is %s",$1); } | C_INTEGER { - $$ = "integer"; + $$ = integ; printdebug("string of C_INTEGER in constant is integer"); } | C_NULL { - $$ = $1; + $$ = $1; printdebug("string of C_NULL in constant is %s",$1); } | C_CHARACTER { - $$ = $1; + $$ = $1; printdebug("string of C_CHARACTER in constant is %s",$1); } | C_TRUE { - $$ = $1; + $$ = $1; printdebug("string of C_TRUE in constant is %s",$1); } | C_FALSE { - $$ = $1; + $$ = $1; printdebug("string of C_FALSE in constant is %s",$1); } From 0eb0b8097ce935f45765aa9d05b06ca2bf6421f1 Mon Sep 17 00:00:00 2001 From: Partho Date: Thu, 3 Apr 2025 20:05:53 -0400 Subject: [PATCH 03/45] updated lex to return the right values to the tokens (mostly table entries) --- src/grammar.y | 28 ++++++++++++++-------------- src/lexicalStructure.lex | 28 +++++++++++++++++++--------- src/symbol_table.c | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 33599a3..8481152 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -47,28 +47,28 @@ %union { int integ; - TableNode *tn; - char *words; + char* words; + TableNode* tn; } %type idlist %type assignable %type expression -%type constant +%type constant %type id_or_types %type types -%token ID 101 -%token T_INTEGER 201 -%token T_ADDRESS 202 -%token T_BOOLEAN 203 -%token T_CHARACTER 204 -%token T_STRING 205 +%token ID 101 +%token T_INTEGER 201 +%token T_ADDRESS 202 +%token T_BOOLEAN 203 +%token T_CHARACTER 204 +%token T_STRING 205 %token C_INTEGER 301 -%token C_NULL 302 -%token C_CHARACTER 303 -%token C_STRING 304 -%token C_TRUE 305 -%token C_FALSE 306 +%token C_NULL 302 +%token C_CHARACTER 303 +%token C_STRING 304 +%token C_TRUE 305 +%token C_FALSE 306 %token WHILE 401 %token IF 402 %token THEN 403 diff --git a/src/lexicalStructure.lex b/src/lexicalStructure.lex index 920cdc4..940681a 100644 --- a/src/lexicalStructure.lex +++ b/src/lexicalStructure.lex @@ -13,6 +13,16 @@ #endif extern SymbolTable * cur; extern FILE* tok_flag; + extern TableNode *funprime; + extern TableNode *funtypeprime; + extern TableNode *arrayprim; + extern TableNode *recprime; + extern TableNode *integ; + extern TableNode *addr; + extern TableNode *chara; + extern TableNode *stri; + extern TableNode *boo; + extern TableNode *undefined; extern void incr(int lnum,int cnum, int tok); extern void print_tok(int tok); @@ -36,10 +46,10 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] %% -"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_INTEGER;}} -"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.words = strdup(yytext);return T_ADDRESS;}} -"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_BOOLEAN;}} -"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.words = strdup(yytext);return T_CHARACTER;}} +"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.tn = integ;return T_INTEGER;}} +"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.tn = addr;return T_ADDRESS;}} +"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.tn = boo;return T_BOOLEAN;}} +"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.tn = chara;return T_CHARACTER;}} "while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {if(tok_flag != NULL){print_tok(WHILE);}incr(line_number,column_number,WHILE);return WHILE;}} "if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {if(tok_flag != NULL){print_tok(IF);}incr(line_number,column_number,IF);return IF;}} @@ -73,8 +83,8 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] "->" {if(DEBUG) {printf( "ARROW: %s (%d)\n", yytext, ARROW);} else {if(tok_flag != NULL){print_tok(ARROW);}incr(line_number,column_number,ARROW);return ARROW;}} {DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {if(tok_flag != NULL){print_tok(C_INTEGER);}incr(line_number,column_number,C_INTEGER);yylval.integ = atoi(yytext)/*words = strdup("integer")*/;return C_INTEGER;}} -'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {if(tok_flag != NULL){print_tok(C_CHARACTER);}incr(line_number,column_number,C_CHARACTER);yylval.words = strdup("character");return C_CHARACTER;}} -\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {if(tok_flag != NULL){print_tok(C_STRING);}incr(line_number,column_number,C_STRING);yylval.words = strdup("string");return C_STRING;}} +'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {if(tok_flag != NULL){print_tok(C_CHARACTER);}incr(line_number,column_number,C_CHARACTER);yylval.tn = chara;return C_CHARACTER;}} +\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {if(tok_flag != NULL){print_tok(C_STRING);}incr(line_number,column_number,C_STRING);yylval.tn = stri;return C_STRING;}} {COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {if(tok_flag != NULL){print_tok(COMMENT);}incr(line_number,column_number,COMMENT);/*return COMMENT;*/}} "(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {if(tok_flag != NULL){print_tok(L_PAREN);}incr(line_number,column_number,L_PAREN);return L_PAREN;}} @@ -84,9 +94,9 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] "{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {if(tok_flag != NULL){print_tok(L_BRACE);}incr(line_number,column_number,L_BRACE);return L_BRACE;}} "}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {if(tok_flag != NULL){print_tok(R_BRACE);}incr(line_number,column_number,R_BRACE);return R_BRACE;}} -"true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {if(tok_flag != NULL){print_tok(C_TRUE);}incr(line_number,column_number,C_TRUE);yylval.words = strdup("Boolean");return C_TRUE;}} -"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {if(tok_flag != NULL){print_tok(C_FALSE);}incr(line_number,column_number,C_FALSE);yylval.words = strdup("Boolean");return C_FALSE;}} -"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {if(tok_flag != NULL){print_tok(C_NULL);}incr(line_number,column_number,C_NULL);yylval.words = strdup("address");return C_NULL;}} +"true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {if(tok_flag != NULL){print_tok(C_TRUE);}incr(line_number,column_number,C_TRUE);yylval.tn = boo;return C_TRUE;}} +"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {if(tok_flag != NULL){print_tok(C_FALSE);}incr(line_number,column_number,C_FALSE);yylval.tn = boo;return C_FALSE;}} +"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {if(tok_flag != NULL){print_tok(C_NULL);}incr(line_number,column_number,C_NULL);yylval.tn = addr;return C_NULL;}} {ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {if(tok_flag != NULL){print_tok(ID);}incr(line_number,column_number,ID);yylval.words = strdup(yytext); return ID;}} diff --git a/src/symbol_table.c b/src/symbol_table.c index 97b4eac..ec960e1 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -769,6 +769,26 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } } +TableNode *getTypeEntry(TableNode *tn) { + if (tn == NULL) { + printdebug("passed a NULL table entry to getType"); + return undefined; + } + if (tn == undefined) { + printdebug("passed an undefined table entry to getType"); + return undefined; + } + if (tn->theType == NULL) { + printdebug("type of entry is currently NULL type"); + return undefined; + } + if (tn->theType == undefined) { + printdebug("type of entry is currently undefined type"); + return undefined; + } + return tn->theType; +} + char *getType(TableNode *tn) { if (tn == NULL) { printdebug("passed a NULL table entry to getType"); From 9b73c657467c973e703fc4664b732a6c7737aeb4 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 3 Apr 2025 17:34:18 -0400 Subject: [PATCH 04/45] allow renaming of tablenodes --- src/symbol_table.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index ec960e1..70f28d5 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -390,8 +390,8 @@ bool getAsKeyword(TableNode *definition) { } if (strcmp(getType(definition), "primitive function") != 0) { printdebug( - "not checking if a function is called with as or not -- " - "invalid op"); + "not checking if a function is called with as or not (%s) -- " + "invalid op", getType(definition)); return 0; } return definition->additionalinfo->FunDecAdInfo->regularoras; @@ -856,6 +856,11 @@ TableNode *addName(TableNode *tn, char *str) { printdebug( "Name doesn't look like it is empty before you change. " "Are you sure you need to update name?"); + if (str != NULL) { + tn->theName = str; + return tn; + } + printdebug("passed a NULL string to the addName function"); return undefined; } if (str == NULL) { @@ -1322,4 +1327,4 @@ TableNode *getNextEntry(TableNode *tn) { printdebug("The type of the first entry is %s",First_Entry->theType); return 0; } - */ \ No newline at end of file + */ From 4f62851575eec84cb9c7c2ff1a336130eb8659c0 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 3 Apr 2025 17:40:06 -0400 Subject: [PATCH 05/45] reseting carl's mistake to what it should be from my changes --- tests/sprint2/test/sp2_carls_mistake.alpha | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sprint2/test/sp2_carls_mistake.alpha b/tests/sprint2/test/sp2_carls_mistake.alpha index 2f4c1ef..daca47e 100644 --- a/tests/sprint2/test/sp2_carls_mistake.alpha +++ b/tests/sprint2/test/sp2_carls_mistake.alpha @@ -25,4 +25,4 @@ entry(arg) := { 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 *) return 0; -} +} \ No newline at end of file From 5e749eb1ac80c9338de329db044c2c49580554e6 Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 4 Apr 2025 10:16:45 -0400 Subject: [PATCH 06/45] updated 3756156 to use table nodes up until comment in grammar.y --- src/grammar.y | 144 +++++++++++++++++++++++++------------------------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 8481152..7b39031 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -189,9 +189,9 @@ definition: } R_PAREN ASSIGN sblock - | ID //EVERYTHING UP UNTIL THIS POINT HAS BEEN CHECKED FOR $TN TYPES + | ID { - TableNode *node = table_lookup(getAncestor(cur), $1); + TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { printdebug("null check"); } @@ -210,7 +210,7 @@ definition: CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); }R_PAREN ASSIGN sblock | ID { - TableNode *node = table_lookup(getAncestor(cur), $1); + TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ @@ -223,7 +223,7 @@ definition: } cur = CreateScope(cur, 0, 0); }AS L_PAREN { - TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); + TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); @@ -265,11 +265,11 @@ idlist: if (getNextEntry(entry) == NULL) { printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); } - addName(entry, $1); + addName(entry, 1); } COMMA idlist { - $$ = $4 + 1; + $$ = $4 + 1; } | ID @@ -334,8 +334,8 @@ declaration_list: declaration: id_or_types COLON ID { - printdebug("ID/TYPE: %s, ID: %s", $1, $3) ; - CreateEntry(cur,table_lookup(getAncestor(cur),$1),$3,NULL); + printdebug("ID/TYPE: %s, ID: %s", getName($1), $3) ; + CreateEntry(cur,$1,$3,NULL); } ; @@ -350,7 +350,7 @@ id_or_types: | types { - printdebug("string of type is %s in types pattern of id_or_type rule.",$1); + printdebug("string of type is %s in types pattern of id_or_type rule.",getName($1)); $$ = $1; } ; @@ -377,22 +377,22 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp($1, $3) == 0) { + if(strcmp(getType($1), getType($3)) == 0) { printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getType(look_up(cur, $1)), "array") == 0) && (strcmp($3, "address") == 0)) { - printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, $1, $3); - } else if((strcmp(getType(look_up(cur, $1)), "record") == 0) && (strcmp($3, "address") == 0)) { - printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, $1, $3); - } else if((strcmp(getType(look_up(cur, $1)), "function type primitive") == 0) && (strcmp($3, "address") == 0)) { - printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, $1, $3); + } else if((strcmp(getType($1), "array") == 0) && (strcmp($3, "address") == 0)) { + printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); + } else if((strcmp(getType($1), "record") == 0) && (strcmp($3, "address") == 0)) { + printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); + } else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getType($3), "address") == 0)) { + printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); // } else if () { // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { // printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3); } else { printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); - printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, $1, $3, COLOR_WHITE); - printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(look_up(cur, $1))); + printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType($1), getType($3), COLOR_WHITE); + printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType($1)); } } @@ -409,8 +409,8 @@ rec_op: ablock: L_PAREN argument_list R_PAREN { - $$ = $2; - printdebug("ablock is %d", $$); + $$ = $2; + printdebug("ablock is %d", $$); } ; @@ -419,15 +419,15 @@ ablock: argument_list: expression COMMA argument_list { - CreateEntry(cur, look_up(cur, $1), $1, NULL); - $$ = $3 + 1; - printdebug("[ARGUMENT_LIST] argument list is %d", $$); + CreateEntry(cur, $1, getName($1), NULL); + $$ = $3 + 1; + printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur, look_up(cur, $1), $1, NULL); - $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); + CreateEntry(cur, $1, getName($1), NULL); + $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; @@ -437,15 +437,15 @@ expression: constant { printdebug("constant expression"); - $$ = $1; + $$ = $1; } | SUB_OR_NEG expression %prec UMINUS { printdebug("negative expression"); - if(strcmp($2,"integer") != 0) { + if($2 != integ) { printdebug("cant negate something not an integer at line %d and column %d",@2.first_line,@2.first_column); - $$=strdup("undefined"); + $$=undefined; } else { $$=$2; } @@ -454,133 +454,133 @@ expression: | NOT expression { printdebug("not expression"); - if(strcmp($2,"Boolean")==0) { + if($2 == boo) { $$=$2; } else { - $$=strdup("undefined"); - printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,$2); + $$=undefined; + printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName($2)); } } | expression ADD expression { printdebug("add expression"); - if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("integer"); + if($1 == $3 && $1 == integ) { + $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression SUB_OR_NEG expression { printdebug("sub or neg expression"); - if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { - $$=strdup("integer"); + if($1 == $3 && $1 == integ) { + $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression MUL expression { printdebug("multiply expression"); - if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { - $$=strdup("integer"); + if($1 == $3 && $1 == integ) { + $$=$1; } else{ - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression DIV expression { printdebug("divide expression"); - if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("integer"); + if(strcmp($1 == $3 && $1 == integ) { + $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression REM expression { printdebug("remainder expression"); - if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("integer"); + if($1 == $3 && $1 == integ) { + $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression AND expression { printdebug("AND expression"); - if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0) { - $$=strdup("Boolean"); + if($1 == $3 && $1 == boo { + $$=$1; } else{ - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression OR expression { printdebug("OR"); - if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0) { - $$=strdup("Boolean"); + if(strcmp($1 == $3 && $1 == boo) { + $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } - + | expression LESS_THAN expression { printdebug("less than expression"); - if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("Boolean"); + if($1 == $3 && $1==integ) { + $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + $$=undefined; } } | expression EQUAL_TO expression { printdebug("equals check expression"); - if(strcmp($1,$3)==0) { - $$=strdup("Boolean"); + if($1 == $3 && $1 != undefined) { + $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,$1,$3); - $$=strdup("undefined"); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName($1),getName($3)); + $$ = undefined; } } | assignable { printdebug("assignable expression. current type is %s",$1); - $$=$1; + $$= getTypeEntry($1);//idk if this is correct } | L_PAREN expression R_PAREN { - printdebug("paren expression. current type is %s",$2); + printdebug("paren expression. current type is %s",getName($2)); $$=$2; } | memOp assignable { - $$ = strdup("address"); + $$ = addr; } ; - +//UPDATED $$ for tablenodes to this point // prolly right, check back with me later // add array case From f6abbbd67ffad3ddc8200be31dedc4f301bdea71 Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 4 Apr 2025 20:24:05 -0400 Subject: [PATCH 07/45] updated to change strings to nodes in most locations --- src/grammar.y | 151 +++++++++++------ src/symbol_table.c | 397 +++++++++++++++++++++++++-------------------- src/symbol_table.h | 8 + 3 files changed, 327 insertions(+), 229 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 7b39031..119960a 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -43,21 +43,23 @@ TableNode * tn; %} -%locations - %union { int integ; char* words; - TableNode* tn; + void* tn; } +%locations + %type idlist %type assignable %type expression %type constant %type id_or_types %type types -%token ID 101 +%type argument_list +%type ablock +%token ID 101 %token T_INTEGER 201 %token T_ADDRESS 202 %token T_BOOLEAN 203 @@ -143,13 +145,15 @@ definition: TYPE ID COLON { printdebug("Currently see a record definition for %s", $2); - tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); + tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); if (look_up(cur, $2) == undefined) { - printdebug("rec not found "); + printdebug("rec not found"); } } dblock { + //We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize + //and then putting it in the entry that we created above. setRecSize(look_up(cur, $2), getRecSize(cur)); cur = getParent(cur); } @@ -157,7 +161,7 @@ definition: | TYPE ID COLON C_INTEGER ARROW id_or_types { printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, getName($6), $4); - CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, $6)); + CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, $6)); printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName($6)); } @@ -165,8 +169,8 @@ definition: | TYPE ID COLON id_or_types ARROW id_or_types { - printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", getName($2), getName($4), getName($6)); - CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6)); + printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, getName($4), getName($6)); + CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6)); } | ID @@ -185,11 +189,25 @@ definition: L_PAREN ID { printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", $1,$4); - CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); + TableNode* type_of_param = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); + int type_of_param_type = getAdInfoType(type_of_param); + if( type_of_param_type == TYPE_UNDEFINED + || type_of_param_type == TYPE_FUNCTION_DECLARATION + || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_ALL_ELSE + || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused + printdebug("type of parameter is undefined or invalid at line %d, column %d", @4.first_line, @4.first_column); + type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + }else{ + printdebug("type of parameter is %s at line %d, column %d", getName(type_of_param), @4.first_line, @4.first_column); + } + + CreateEntry(cur,type_of_param_type, type_of_param, $4, getAdInfo(type_of_param)); } - R_PAREN ASSIGN sblock + R_PAREN ASSIGN sblock //leaving scope is being taken care of in sblock - | ID + /* | ID { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { @@ -208,13 +226,13 @@ definition: printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", $1,$4); CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); - }R_PAREN ASSIGN sblock + }R_PAREN ASSIGN sblock */ | ID { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + printdebug(" undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + printdebug("not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); } else { printdebug("setting as keyword to true"); @@ -232,9 +250,26 @@ definition: printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); }else { for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ - printdebug("creating entry of type %s for function", getType(entry)); - CreateEntry(cur, entry, "undefined", NULL); + int type_of_param_type = getAdInfoType(entry); + if( type_of_param_type == TYPE_UNDEFINED + || type_of_param_type == TYPE_FUNCTION_DECLARATION + || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_ALL_ELSE + || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused + printdebug("type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); + type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + }else{ + printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); + } + if(type_of_param_type == TYPE_UNDEFINED){ + CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + } else { + CreateEntry(cur,type_of_param_type, entry, NULL, getAdInfo(entry)); + /*printdebug("creating entry of type %s for function", getType(entry)); + CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ } + } } } idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$6);} R_PAREN ASSIGN sblock //check sblock type @@ -244,12 +279,12 @@ definition: function_declaration: FUNCTION ID COLON ID { - CreateEntry(cur, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); } | EXTERNAL FUNCTION ID COLON ID { - CreateEntry(cur, look_up(cur, $5), $3, NULL); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, NULL); } ; @@ -265,7 +300,7 @@ idlist: if (getNextEntry(entry) == NULL) { printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); } - addName(entry, 1); + addName(entry, $1); } COMMA idlist { @@ -290,8 +325,11 @@ idlist: sblock: L_BRACE { - if (getLine(cur) != 0 && getColumn(cur)) { + if (getLine(cur) != 0) { cur = CreateScope(cur,@1.first_line,@1.first_column); + } else { + setLineNumber(cur, @1.first_line); + setColumnNumber(cur,@1.first_line); } } statement_list @@ -302,8 +340,11 @@ sblock: | L_BRACE { - if (getLine(cur) != 0 && getColumn(cur)) { + if (getLine(cur) != 0) { cur = CreateScope(cur,@1.first_line,@1.first_column); + } else { + setLineNumber(cur, @1.first_line); + setColumnNumber(cur,@1.first_line); } } dblock @@ -320,7 +361,15 @@ sblock: dblock: - L_BRACKET declaration_list R_BRACKET; + L_BRACKET + {if(getLine(cur)==0){ + setLineNumber(cur, @1.first_line); + setColumnNumber(cur,@1.first_line);} + else{ + cur = CreateScope(cur,@1.first_line,@1.first_column); + } + } + declaration_list R_BRACKET; @@ -335,7 +384,7 @@ declaration: id_or_types COLON ID { printdebug("ID/TYPE: %s, ID: %s", getName($1), $3) ; - CreateEntry(cur,$1,$3,NULL); + CreateEntry(cur,getAdInfoType($1),$1,$3,getAdInfo($1)); } ; @@ -369,7 +418,7 @@ statement_list: compound_statement: WHILE L_PAREN expression R_PAREN sblock | IF L_PAREN expression R_PAREN THEN sblock ELSE sblock - | sblock + | sblock ; @@ -377,14 +426,14 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp(getType($1), getType($3)) == 0) { + if(strcmp(getName($1), getName($3)) == 0) { printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getType($1), "array") == 0) && (strcmp($3, "address") == 0)) { - printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); - } else if((strcmp(getType($1), "record") == 0) && (strcmp($3, "address") == 0)) { - printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); - } else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getType($3), "address") == 0)) { - printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getType($1), getType($3)); + } else if((strcmp(getName($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) { + printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); + } else if((strcmp(getName($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) { + printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); + } else if((strcmp(getName($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) { + printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); // } else if () { // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { @@ -419,14 +468,14 @@ ablock: argument_list: expression COMMA argument_list { - CreateEntry(cur, $1, getName($1), NULL); + CreateEntry(cur,getAdInfoType($1), $1, getName($1), NULL); $$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur, $1, getName($1), NULL); + CreateEntry(cur,getAdInfoType($1),$1, getName($1), NULL); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; @@ -498,7 +547,7 @@ expression: | expression DIV expression { printdebug("divide expression"); - if(strcmp($1 == $3 && $1 == integ) { + if((strcmp(getName($1),getName($3))==0) && ($1 == integ)) { $$=$1; } else { printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); @@ -520,7 +569,7 @@ expression: | expression AND expression { printdebug("AND expression"); - if($1 == $3 && $1 == boo { + if($1 == $3 && $1 == boo){ $$=$1; } else{ printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); @@ -531,7 +580,7 @@ expression: | expression OR expression { printdebug("OR"); - if(strcmp($1 == $3 && $1 == boo) { + if((strcmp(getName($1),getName($3))==0) && $1 == boo) { $$=$1; } else { printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); @@ -599,17 +648,17 @@ assignable: } ablock { - int type = getAdInfoType(look_up(getParent(cur), $1)); + int type = getAdInfoType(look_up(cur, getName($1))); printdebug("%stype is %d", COLOR_PURPLE, type); printdebug("%s", $1); if (type == TYPE_FUNCTION_DECLARATION) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); - if (look_up(getParent(cur), $1)->additionalinfo->FunDecAdInfo->regularoras) { + if (look_up(getParent(cur), getName($1))->additionalinfo->FunDecAdInfo->regularoras) { printdebug("as function"); //char *funtype = getType(look_up(cur, $1)); - printdebug("%s", getType(look_up(cur, $1))); - TableNode *param = getParameter(look_up(cur, getType(look_up(cur, $1)))); + printdebug("%s", getType(look_up(cur, getName($1)))); + TableNode *param = getParameter(look_up(cur, getType(look_up(cur, getName($1))))); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); @@ -636,32 +685,32 @@ assignable: } } else { - char *expected = getName(getParameter(look_up(getParent(cur), $1))); + char *expected = getName(getParameter(look_up(getParent(cur), getName($1)))); char *actual = getType(getFirstEntry(cur)); if (strcmp(expected, actual) != 0) { printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); } } - $$ = getName(getReturn((look_up(cur, getType(look_up(cur, $1)))))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); + $$ = getReturn((look_up(cur, getType(look_up(cur, getName($1)))))); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1)); } else if (type == TYPE_ARRAY_TYPE) { printdebug("%sEntering array call", COLOR_LIGHTGREEN); - if (getNumArrDim(look_up(getParent(cur), $1)) != $2) { - printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $2, @2.first_line, @2.first_column); + if (getNumArrDim(look_up(getParent(cur), getName($1))) != $2) { + printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName($1))), $2, @2.first_line, @2.first_column); } - $$ = getName(getArrType(look_up(getParent(cur), $1))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); + $$ = getArrType(look_up(getParent(cur), getName($1))); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1)); } cur = getParent(cur); } | assignable rec_op ID { - if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)) { - $$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)); + if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3)) { + $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3); } - printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1); + printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName($$), $1); } ; diff --git a/src/symbol_table.c b/src/symbol_table.c index 70f28d5..2e1af13 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -85,7 +85,8 @@ typedef enum { TYPE_ALL_ELSE = 7, TYPE_UNDEFINED = 8, TYPE_RECORD = 9, - TYPE_ARRAY = 10 + TYPE_ARRAY = 10, + TYPE_SYSTEM_DEFINED = 11 // for system defined entries like funprimetype etc. } types; @@ -314,7 +315,7 @@ int getRecSize(SymbolTable *tn) { "passed in NULL SymbolTable for getRecSize. Invalid"); return -1; } - int s = 0; + int s = 1; TableNode *cur = getFirstEntry(tn); if (cur != NULL) { while (getNextEntry(cur) != NULL) { @@ -551,6 +552,7 @@ SymbolTable *init(SymbolTable *start) { prime->theType = NULL; prime->additionalinfo = NULL; prime->next = NULL; + prime->tag = TYPE_SYSTEM_DEFINED; // not sure exatly how to get array types to look right so using a dummy // Table Node below and updating the print symbol table function to @@ -560,7 +562,8 @@ SymbolTable *init(SymbolTable *start) { arrayprim->theName = "array"; arrayprim->theType = NULL; arrayprim->additionalinfo = NULL; - prime->next = NULL; + arrayprim->next = NULL; + prime->tag = TYPE_SYSTEM_DEFINED; // funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL); @@ -570,6 +573,7 @@ SymbolTable *init(SymbolTable *start) { funprime->theType = NULL; funprime->additionalinfo = NULL; funprime->next = NULL; + funprime->tag = TYPE_SYSTEM_DEFINED; // record recprime = (TableNode *)malloc(sizeof(TableNode)); @@ -577,18 +581,21 @@ SymbolTable *init(SymbolTable *start) { recprime->theType = NULL; recprime->additionalinfo = NULL; recprime->next = NULL; + recprime->tag = TYPE_SYSTEM_DEFINED; funtypeprime = (TableNode *)malloc(sizeof(TableNode)); funtypeprime->theName = "primitive function type"; funtypeprime->theType = NULL; funtypeprime->additionalinfo = NULL; funtypeprime->next = NULL; + funtypeprime->tag = TYPE_SYSTEM_DEFINED; undefined = (TableNode *)malloc(sizeof(TableNode)); undefined->theName = "undefined"; undefined->theType = NULL; undefined->additionalinfo = NULL; undefined->next = NULL; + undefined->tag = TYPE_SYSTEM_DEFINED; // Undefined_function_type_info = CreateFunctionTypeInfo(undefined, // undefined); @@ -605,11 +612,17 @@ SymbolTable *init(SymbolTable *start) { // be the size of these primitive types. We can change these if needed // to not be hard coded numbers as a reminder, stri below is defined as // a one dimensional array of characters - integ->additionalinfo = CreatePrimitiveInfo(4); - addr->additionalinfo = CreatePrimitiveInfo(8); - chara->additionalinfo = CreatePrimitiveInfo(1); + integ->additionalinfo = CreatePrimitiveInfo(SIZE_INT); + addr->additionalinfo = CreatePrimitiveInfo(SIZE_ADDR); + chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR); stri->additionalinfo = CreateArrayInfo(1, chara); - boo->additionalinfo = CreatePrimitiveInfo(1); + boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); + + integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ + addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr + chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara + stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri + boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo // addr->additionalinfo = CreatePrimitiveInfo(8); start->Line_Number = 1; @@ -661,12 +674,33 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) { return tn; } +AdInfo *getAdInfo(TableNode *tn) { + if (tn == NULL) { + printdebug("passed a NULL table entry to getAdInfo. Invalid."); + return NULL; + } + if (tn == undefined) { + printdebug( + "passed an undefined table entry to getAdInfo. Invalid."); + return NULL; + } + if (tn->additionalinfo == NULL) { + printdebug( + "no additional info found in the table node. Invalid."); + return NULL; + } + return tn->additionalinfo; +} + +//simplified getAdInfoType int getAdInfoType(TableNode *tn) { if (tn == NULL) { printdebug( "passing in NULL table entry to getAdInfoType. Invalid"); return -1; } + return tn->tag; + /* if (tn == undefined) { printdebug("passing in undefined table entry to getAdInfoType. " "Invalid"); @@ -725,11 +759,10 @@ int getAdInfoType(TableNode *tn) { "passed in an entry that is not a primitive type, array, " "or record. Invalid."); return TYPE_FUNCTION_DECLARATION; - } + }*/ } -TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, - AdInfo *ad) { +TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) { if (table == NULL) { printdebug("Null reference to table"); @@ -752,7 +785,14 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, return undefined; } + TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); + if(tag<1 && tag>11){ + printdebug("Note- not passing in valid 'tag' identifier to create entry function. Setting tag to undefined"); + newEntry->tag = TYPE_UNDEFINED; + } else{ + newEntry->tag = tag; + } newEntry->theType = typeOf /*topDef*/; newEntry->theName = id; newEntry->additionalinfo = ad; @@ -769,6 +809,8 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } } + + TableNode *getTypeEntry(TableNode *tn) { if (tn == NULL) { printdebug("passed a NULL table entry to getType"); @@ -853,9 +895,9 @@ TableNode *addName(TableNode *tn, char *str) { return undefined; } if (tn->theName != NULL) { - printdebug( - "Name doesn't look like it is empty before you change. " - "Are you sure you need to update name?"); + //printdebug( + //"Name doesn't look like it is empty before you change. " + //"Are you sure you need to update name?"); if (str != NULL) { tn->theName = str; return tn; @@ -1011,178 +1053,177 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } }*/ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { + if (table == NULL) { + printdebug( + "%s[FATAL] passed in NULL table to print_symbol_table", + COLOR_RED); + return; + } - if (table == NULL) { - printdebug( - "%s[FATAL] passed in NULL table to print_symbol_table", - COLOR_RED); - return; - } + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", + "SCOPE", "PARENT", "TYPE", "Extra annotation"); + } - if (table->Parent_Scope == NULL) { - fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", - "SCOPE", "PARENT", "TYPE", "Extra annotation"); - } + TableNode *entrie = table->entries; + fprintf(file_ptr, + "-------------------------:--------:--------:------------------" + "--------:------------------------------\n"); + int parant_scope = 0; + int current_scope = 0; + if (table->Parent_Scope != NULL) { + parant_scope = getParent(table)->Line_Number * 1000 + + getParent(table)->Column_Number; + current_scope = + table->Line_Number * 1000 + table->Column_Number; + } else { + current_scope = 1001; + } + if (entrie == NULL) { + fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", + current_scope, parant_scope, "", "Empty Scope"); + } + for (; entrie != NULL; entrie = getNextEntry(entrie)) { + if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { + if (parant_scope == 0) { - TableNode *entrie = table->entries; - fprintf(file_ptr, "-----------------:--------:--------:----------------" - "------:---------" - "--------------------\n"); - int parant_scope = 0; - int current_scope = 0; - if (table->Parent_Scope != NULL) { - parant_scope = getParent(table)->Line_Number * 1000 + - getParent(table)->Column_Number; - current_scope = - table->Line_Number * 1000 + table->Column_Number; - } else { - current_scope = 1001; - } - if (entrie == NULL) { - fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", - current_scope, parant_scope, "", "Empty Scope"); - } - for (; entrie != NULL; entrie = getNextEntry(entrie)) { - if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { - 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, - "%-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_PRIMITIVE) { - 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", + fprintf(file_ptr, + "%-25s: %06d : : %d -> %-20s: " + "%-30s\n", entrie->theName, current_scope, - parant_scope, "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } - } - if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { - if (parant_scope == 0) { + entrie->additionalinfo->ArrayAdInfo + ->numofdimensions, + entrie->additionalinfo->ArrayAdInfo + ->typeofarray->theName, + "Type of Array"); + } else { + fprintf(file_ptr, + "%-25s: %06d : : %d -> %-20s: " + "%-30s\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, - "%-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_FUNCTION_DECLARATION) { - if (parant_scope == 0) { + fprintf(file_ptr, + "%-25s: %06d : : %-25s: " + "elements-%-30d\n", + entrie->theName, current_scope, + "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: " + "elements-%-30d\n", + entrie->theName, current_scope, + parant_scope, "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } + } + if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-17s: %06d : :%-21s: %-28s\n", - entrie->theName, current_scope, - getType(entrie), "User Defined"); - } else { - fprintf(file_ptr, - "%-17s: %06d : %06d : %-21s: %-28s\n", - entrie->theName, current_scope, - parant_scope, getType(entrie), - "User Defined"); - } - } - if (getAdInfoType(entrie) == TYPE_UNDEFINED) { - if (parant_scope == 0) { + fprintf( + file_ptr, + "%-25s: %06d : : %-25s: size-%d " + "bytes\n", + entrie->theName, current_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } else { + fprintf( + file_ptr, + "%-25s: %06d : %06d : %-25s: size-%-30d " + "bytes\n", + entrie->theName, current_scope, + parant_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } + } + if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { + 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 (getChildren(table) != NULL) { - ListOfTable *node = getChildren(table); - for (; node != NULL; node = node->next) { - if ((node->table) == NULL) { - print_symbol_table(node->table, file_ptr); - } else { - if ((node->table)->Line_Number == -1) { - continue; - } else { - print_symbol_table(node->table, - file_ptr); - } - } - } - } - if (getParent(table) == NULL) { - fprintf(file_ptr, "-----------------:--------:--------:--------" - "--------------:-------" - "----------------------\n"); - } + fprintf(file_ptr, + "%-25s: %06d : : %-25s -> " + "%-25s: %-30s\n", + entrie->theName, current_scope, + entrie->additionalinfo->FunTypeAdInfo + ->parameter->theName, + entrie->additionalinfo->FunTypeAdInfo + ->returntype->theName, + "Type of Function"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s -> %-21s: " + "%-30s\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 (parant_scope == 0) { + + fprintf(file_ptr, + "%-25s: %06d : : %-25s: %-30s\n", + entrie->theName, current_scope, + getType(entrie), "User Defined"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: %-30s\n", + entrie->theName, current_scope, + parant_scope, getType(entrie), + "User Defined"); + } + } + if (getAdInfoType(entrie) == TYPE_UNDEFINED) { + if (parant_scope == 0) { + + fprintf(file_ptr, + "%-25s: %06d : : %-25s: %-30s\n", + entrie->theName, current_scope, + "undefined", "undefined entry"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: %-30s\n", + entrie->theName, current_scope, + parant_scope, "undefined", + "undefined entry"); + } + } + } + if (getChildren(table) != NULL) { + ListOfTable *node = getChildren(table); + for (; node != NULL; node = node->next) { + if ((node->table) == NULL) { + print_symbol_table(node->table, file_ptr); + } else { + if ((node->table)->Line_Number == -1) { + continue; + } else { + print_symbol_table(node->table, + file_ptr); + } + } + } + } + if (getParent(table) == NULL) { + fprintf(file_ptr, + "-------------------------:--------:--------:----------" + "----------------:------------------------------\n"); + } } // get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { diff --git a/src/symbol_table.h b/src/symbol_table.h index de37098..f223b8a 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -3,6 +3,12 @@ #include #include +#define SIZE_INT 4 +#define SIZE_ADDR 8 +#define SIZE_CHAR 1 +#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be + + struct TableNode; typedef struct { @@ -58,9 +64,11 @@ typedef struct ListOfTable { struct ListOfTable *next; } ListOfTable; +//Table node to store typedef struct TableNode { // reference to the type entry that this is struct TableNode *theType; + int tag; char *theName; AdInfo *additionalinfo; struct TableNode *next; From a53a22530dc5e346afdc020ad9e7dbd95d085a3b Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 4 Apr 2025 20:29:41 -0400 Subject: [PATCH 08/45] added tests for sprint 2 and 3 --- .../sp2_invalid_multiple_params_no_as.alpha | 7 ++++ .../sprint3/test/sp3_and_or_type_check.alpha | 37 +++++++++++++++++++ .../sp3_integer_binary_op_typecheck.alpha | 25 +++++++++---- 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 tests/sprint2/test/sp2_invalid_multiple_params_no_as.alpha create mode 100644 tests/sprint3/test/sp3_and_or_type_check.alpha diff --git a/tests/sprint2/test/sp2_invalid_multiple_params_no_as.alpha b/tests/sprint2/test/sp2_invalid_multiple_params_no_as.alpha new file mode 100644 index 0000000..805fb6e --- /dev/null +++ b/tests/sprint2/test/sp2_invalid_multiple_params_no_as.alpha @@ -0,0 +1,7 @@ +type rec: [integer: x; integer: y] +type main: rec -> integer +function test: main + +test (arg, arg2) := { + return 0; +} \ No newline at end of file diff --git a/tests/sprint3/test/sp3_and_or_type_check.alpha b/tests/sprint3/test/sp3_and_or_type_check.alpha new file mode 100644 index 0000000..a6cf442 --- /dev/null +++ b/tests/sprint3/test/sp3_and_or_type_check.alpha @@ -0,0 +1,37 @@ +type rec: [integer: x; integer: y] +type main: rec -> integer +function test: main + +test (arg, arg2) := { + [integer:x; Boolean: b] + while (true) { + x := 0; + } + + while (7) { + x := 1; + } + + if (true) { + x := 1; + } else { + x := 0; + } + + if (x) { + x := 0; + } else { + x := 1; + } + + b := b | b; + b := b & b; + b := 1 | b; + b := b | 1; + b := b & 1; + b := 1 & b; + b := 1 = 1; + b := 1 = b; + + return 0; +} diff --git a/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha b/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha index 5b13147..d8670ff 100644 --- a/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha +++ b/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha @@ -1,9 +1,18 @@ -entry(arg) := { - [integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1; character : a] - x := 3 + 2 * 8; - x := 3 - 2 / 8; - x := a * 2 % 8; - b2 := 3 * 2 % 8; - x := 3 % 2 * 8; - x := 3 + arr - 8; +type main: string -> integer +type rec: [integer: x; integer: y] +function entry: main + +entry (arg) := { + [integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1; character : a; rec : r] + + x := 3 + 2 * 8; + x := 3 - 2 / 8; + x := a * 2 % 8; + b2 := 3 * 2 % 8; + x := 3 % 2 * 8; + x := 3 + arr - 8; + r.x; + a.x; + + return 0; } From a5e1acefde50d43f00c5170a936658a3aba69eb5 Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 4 Apr 2025 20:39:01 -0400 Subject: [PATCH 09/45] fixed sytnax errors in tests --- tests/sprint3/test/sp3_and_or_type_check.alpha | 6 +++--- tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/sprint3/test/sp3_and_or_type_check.alpha b/tests/sprint3/test/sp3_and_or_type_check.alpha index a6cf442..450aee0 100644 --- a/tests/sprint3/test/sp3_and_or_type_check.alpha +++ b/tests/sprint3/test/sp3_and_or_type_check.alpha @@ -2,7 +2,7 @@ type rec: [integer: x; integer: y] type main: rec -> integer function test: main -test (arg, arg2) := { +test (arg) := { [integer:x; Boolean: b] while (true) { x := 0; @@ -12,13 +12,13 @@ test (arg, arg2) := { x := 1; } - if (true) { + if (true) then { x := 1; } else { x := 0; } - if (x) { + if (x) then { x := 0; } else { x := 1; diff --git a/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha b/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha index d8670ff..8d8ad70 100644 --- a/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha +++ b/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha @@ -11,8 +11,8 @@ entry (arg) := { b2 := 3 * 2 % 8; x := 3 % 2 * 8; x := 3 + arr - 8; - r.x; - a.x; + x := r.x; + x := a.x; return 0; } From 6a6677ccb523cb391788ecb1dfc9aed8ea9b4012 Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 4 Apr 2025 21:12:11 -0400 Subject: [PATCH 10/45] checking diff --- src/grammar.y | 42 +++++++++++------------------------------- src/runner.c | 2 +- src/symbol_table.c | 2 +- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 119960a..1725d62 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -154,7 +154,7 @@ definition: { //We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize //and then putting it in the entry that we created above. - setRecSize(look_up(cur, $2), getRecSize(cur)); + setRecSize(look_up(getParent(cur), $2), getRecSize(cur)); cur = getParent(cur); } @@ -206,27 +206,7 @@ definition: CreateEntry(cur,type_of_param_type, type_of_param, $4, getAdInfo(type_of_param)); } R_PAREN ASSIGN sblock //leaving scope is being taken care of in sblock - - /* | ID - { - TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == undefined) { - printdebug("null check"); - } - if (node == undefined) { - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); - } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); - } else { - setStartLine(node, @1.first_line); - setAsKeyword(node, false); - } - cur = CreateScope(cur, 0, 0); - } L_PAREN ID { - printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", - $1,$4); - CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); - }R_PAREN ASSIGN sblock */ + | ID { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { @@ -394,7 +374,7 @@ id_or_types: ID { printdebug("string of id is %s in ID pattern of id_or_type rule.", $1); - $$ = look_up(cur, $1); + $$ = table_lookup(getAncestor(cur), $1); } | types @@ -428,11 +408,11 @@ simple_statement: { if(strcmp(getName($1), getName($3)) == 0) { printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getName($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) { + } else if((strcmp(getType($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) { printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); - } else if((strcmp(getName($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) { + } else if((strcmp(getType($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) { printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); - } else if((strcmp(getName($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) { + } else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) { printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); // } else if () { @@ -613,7 +593,7 @@ expression: | assignable { printdebug("assignable expression. current type is %s",$1); - $$= getTypeEntry($1);//idk if this is correct + $$= getTypeEntry($1); } | L_PAREN expression R_PAREN @@ -648,7 +628,7 @@ assignable: } ablock { - int type = getAdInfoType(look_up(cur, getName($1))); + int type = getAdInfoType(look_up(getParent(cur), getName($1))); printdebug("%stype is %d", COLOR_PURPLE, type); printdebug("%s", $1); @@ -658,7 +638,7 @@ assignable: printdebug("as function"); //char *funtype = getType(look_up(cur, $1)); printdebug("%s", getType(look_up(cur, getName($1)))); - TableNode *param = getParameter(look_up(cur, getType(look_up(cur, getName($1))))); + TableNode *param = getParameter(look_up(getParent(cur), getName($1))); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); @@ -669,7 +649,7 @@ assignable: //this isn't very efficient, but will hopefully work while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getName(lastCheckedAct), getType(lastCheckedRef)) != 0) { + if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); } @@ -691,7 +671,7 @@ assignable: printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); } } - $$ = getReturn((look_up(cur, getType(look_up(cur, getName($1)))))); + $$ = getReturn((table_lookup(getAncestor(cur), getType($1)))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1)); } else if (type == TYPE_ARRAY_TYPE) { diff --git a/src/runner.c b/src/runner.c index c6f94a3..b20f594 100644 --- a/src/runner.c +++ b/src/runner.c @@ -143,7 +143,7 @@ int run(FILE *alpha) { if (st_flag != NULL) { yyparse(); - print_symbol_table(getAncestor(cur), st_flag); + print_symbol_table(top, st_flag); fclose(st_flag); if (yyin != NULL) { fclose(yyin); diff --git a/src/symbol_table.c b/src/symbol_table.c index 2e1af13..08a0932 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1330,7 +1330,7 @@ SymbolTable *getParent(SymbolTable *st) { if (st->Parent_Scope == NULL) { printdebug("passed a top level scope to getParent function. " "Invalid."); - return NULL; + return st; } return st->Parent_Scope; } From e2aa9e7c12bb4702cd5b4e6b592269ccec040d99 Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 4 Apr 2025 21:40:13 -0400 Subject: [PATCH 11/45] fixed header for this branch --- src/symbol_table.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/symbol_table.h b/src/symbol_table.h index 53c2fa2..cf1273a 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -76,7 +76,8 @@ typedef enum { TYPE_ALL_ELSE = 7, TYPE_UNDEFINED = 8, TYPE_RECORD = 9, - TYPE_ARRAY = 10 + TYPE_ARRAY = 10, + TYPE_SYSTEM_DEFINED = 11 } types; AdInfo *CreatePrimitiveInfo(int size); @@ -101,7 +102,7 @@ SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column); SymbolTable *init(SymbolTable *start); TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info); int getAdInfoType(TableNode *tn); -TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, +TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad); char *getType(TableNode *tn); char *getName(TableNode *tn); From e42e53733916891633537a76372bb0cd2e988a5a Mon Sep 17 00:00:00 2001 From: Partho Date: Mon, 7 Apr 2025 12:32:20 -0400 Subject: [PATCH 12/45] ran through derefenced most if not all of the pointers --- src/grammar.y | 123 +++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 62 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 7dd6499..4a09304 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -18,7 +18,6 @@ %{ #include "../src/symbol_table.c" - void yyerror(const char *err); int token_tracker; TableNode * tn; @@ -142,17 +141,17 @@ definition: | TYPE ID COLON C_INTEGER ARROW id_or_types { - printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, getName($6), $4); - CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, $6)); - printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName($6)); + printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, getName((TableNode*)$6), $4); + CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, (TableNode*)$6)); + printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName((TableNode*)$6)); } | function_declaration | TYPE ID COLON id_or_types ARROW id_or_types { - printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, getName($4), getName($6)); - CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6)); + printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, getName((TableNode*)$4), getName((TableNode*)$6)); + CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); } | ID @@ -349,8 +348,8 @@ declaration_list: declaration: id_or_types COLON ID { - printdebug("ID/TYPE: %s, ID: %s", getName($1), $3) ; - CreateEntry(cur,getAdInfoType($1),$1,$3,getAdInfo($1)); + printdebug("ID/TYPE: %s, ID: %s", getName((TableNode*)$1), $3) ; + CreateEntry(cur,getAdInfoType((TableNode*)$1),(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } ; @@ -365,8 +364,8 @@ id_or_types: | types { - printdebug("string of type is %s in types pattern of id_or_type rule.",getName($1)); - $$ = $1; + printdebug("string of type is %s in types pattern of id_or_type rule.",getName((TableNode*)$1)); + $$ = (TableNode*)$1; } ; @@ -392,22 +391,22 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp(getName($1), getName($3)) == 0) { + if(strcmp(getName((TableNode*)$1), getName((TableNode*)$3)) == 0) { printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getType($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) { - printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); - } else if((strcmp(getType($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) { - printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); - } else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) { - printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName($1), getName($3)); + } else if((strcmp(getType((TableNode*)$1), "array") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { + printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); + } else if((strcmp(getType((TableNode*)$1), "record") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { + printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); + } else if((strcmp(getType((TableNode*)$1), "function type primitive") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { + printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); // } else if () { // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { // printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3); } else { printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); - printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType($1), getType($3), COLOR_WHITE); - printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType($1)); + printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType((TableNode*)$1), getType((TableNode*)$3), COLOR_WHITE); + printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType((TableNode*)$1)); } } @@ -434,14 +433,14 @@ ablock: argument_list: expression COMMA argument_list { - CreateEntry(cur,getAdInfoType($1), $1, getName($1), NULL); + CreateEntry(cur,getAdInfoType((TableNode*)$1), (TableNode*)$1, getName((TableNode*)$1), NULL); $$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur,getAdInfoType($1),$1, getName($1), NULL); + CreateEntry(cur,getAdInfoType((TableNode*)$1),(TableNode*)$1, getName((TableNode*)$1), NULL); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; @@ -452,38 +451,38 @@ expression: constant { printdebug("constant expression"); - $$ = $1; + $$ = (TableNode*)$1; } | SUB_OR_NEG expression %prec UMINUS { printdebug("negative expression"); - if($2 != integ) { + if((TableNode*)$2 != integ) { printdebug("cant negate something not an integer at line %d and column %d",@2.first_line,@2.first_column); $$=undefined; } else { - $$=$2; + $$=(TableNode*)$2; } } | NOT expression { printdebug("not expression"); - if($2 == boo) { - $$=$2; + if((TableNode*)$2 == boo) { + $$=(TableNode*)$2; } else { $$=undefined; - printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName($2)); + printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName((TableNode*)$2)); } } | expression ADD expression { printdebug("add expression"); - if($1 == $3 && $1 == integ) { - $$=$1; + if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { + $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -491,10 +490,10 @@ expression: | expression SUB_OR_NEG expression { printdebug("sub or neg expression"); - if($1 == $3 && $1 == integ) { - $$=$1; + if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { + $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -502,8 +501,8 @@ expression: | expression MUL expression { printdebug("multiply expression"); - if($1 == $3 && $1 == integ) { - $$=$1; + if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { + $$=(TableNode*)$1; } else{ printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); $$=undefined; @@ -513,10 +512,10 @@ expression: | expression DIV expression { printdebug("divide expression"); - if((strcmp(getName($1),getName($3))==0) && ($1 == integ)) { - $$=$1; + if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && ((TableNode*)$1 == integ)) { + $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -527,7 +526,7 @@ expression: if($1 == $3 && $1 == integ) { $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -538,7 +537,7 @@ expression: if($1 == $3 && $1 == boo){ $$=$1; } else{ - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -546,10 +545,10 @@ expression: | expression OR expression { printdebug("OR"); - if((strcmp(getName($1),getName($3))==0) && $1 == boo) { + if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && $1 == boo) { $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -560,7 +559,7 @@ expression: if($1 == $3 && $1==integ) { $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -571,20 +570,20 @@ expression: if($1 == $3 && $1 != undefined) { $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$ = undefined; } } | assignable { - printdebug("assignable expression. current type is %s",$1); - $$= getTypeEntry($1); + printdebug("assignable expression. current type is %s",getName((TableNode*)$1)); + $$= getTypeEntry((TableNode*)$1); } | L_PAREN expression R_PAREN { - printdebug("paren expression. current type is %s",getName($2)); + printdebug("paren expression. current type is %s",getName((TableNode*)$2)); $$=$2; } @@ -614,17 +613,17 @@ assignable: } ablock { - int type = getAdInfoType(look_up(getParent(cur), getName($1))); + int type = getAdInfoType(look_up(getParent(cur), getName((TableNode*)$1))); printdebug("%stype is %d", COLOR_PURPLE, type); - printdebug("%s", $1); + printdebug("%s", getName((TableNode*)$1)); if (type == TYPE_FUNCTION_DECLARATION) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); - if (look_up(getParent(cur), getName($1))->additionalinfo->FunDecAdInfo->regularoras) { + if (look_up(getParent(cur), getName((TableNode*)$1))->additionalinfo->FunDecAdInfo->regularoras) { printdebug("as function"); //char *funtype = getType(look_up(cur, $1)); - printdebug("%s", getType(look_up(cur, getName($1)))); - TableNode *param = getParameter(look_up(getParent(cur), getName($1))); + printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); + TableNode *param = getParameter(look_up(getParent(cur), getName((TableNode*)$1))); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); @@ -651,32 +650,32 @@ assignable: } } else { - char *expected = getName(getParameter(look_up(getParent(cur), getName($1)))); + char *expected = getName(getParameter(look_up(getParent(cur), getName((TableNode*)$1)))); char *actual = getType(getFirstEntry(cur)); if (strcmp(expected, actual) != 0) { printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); } } - $$ = getReturn((table_lookup(getAncestor(cur), getType($1)))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1)); + $$ = getReturn((table_lookup(getAncestor(cur), getType((TableNode*)$1)))); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName((TableNode*)$1)); } else if (type == TYPE_ARRAY_TYPE) { printdebug("%sEntering array call", COLOR_LIGHTGREEN); - if (getNumArrDim(look_up(getParent(cur), getName($1))) != $2) { - printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName($1))), $2, @2.first_line, @2.first_column); + if (getNumArrDim(look_up(getParent(cur), getName((TableNode*)$1))) != $2) { + printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName((TableNode*)$1))), $2, @2.first_line, @2.first_column); } - $$ = getArrType(look_up(getParent(cur), getName($1))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1)); + $$ = getArrType(look_up(getParent(cur), getName((TableNode*)$1))); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName((TableNode*)$1)); } cur = getParent(cur); } | assignable rec_op ID { - if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3)) { - $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3); + if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3)) { + $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3); } - printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName($$), $1); + printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName((TableNode*)($$)), $1); } ; From 4058b090a00023708d08c2f9452b00ed99dc7b55 Mon Sep 17 00:00:00 2001 From: Scarlett Date: Mon, 7 Apr 2025 15:51:45 -0400 Subject: [PATCH 13/45] found one segfault. (not fixed yet) --- check.sh | 2 + src/grammar.y | 5 + src/symbol_table.c | 460 +++++++++++++++++++++++++++------------------ src/symbol_table.h | 1 + 4 files changed, 287 insertions(+), 181 deletions(-) diff --git a/check.sh b/check.sh index bac18ec..cf7c946 100755 --- a/check.sh +++ b/check.sh @@ -33,6 +33,8 @@ compare_files() { diff -q "$file" "$exp" > /dev/null if [[ $? -eq 0 ]]; then echo -e "${GREEN}[✔] ${PURPLE}$filename ${WHITE}passed.${NOCOLOR}" + elif [[ ! -s "$file" ]]; then + echo -e "${RED}[✘] ${PURPLE}$filename ${WHITE}failed with an empty file. (did it segfault?)${NOCOLOR}" else echo -e "\n${RED}[✘] ${PURPLE}$file ${WHITE}failed with an unexpected value...${NOCOLOR}" diff --color=always "$file" "$exp" diff --git a/src/grammar.y b/src/grammar.y index 4a09304..b20c09e 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -623,7 +623,12 @@ assignable: printdebug("as function"); //char *funtype = getType(look_up(cur, $1)); printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); + + // Something fishy is going on here....... TableNode *param = getParameter(look_up(getParent(cur), getName((TableNode*)$1))); + printTableNode(table_lookup(getAncestor(cur), getName((TableNode*)$1))); + + SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); diff --git a/src/symbol_table.c b/src/symbol_table.c index 1cb6cc8..3e67b95 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -261,9 +261,10 @@ bool getAsKeyword(TableNode *definition) { return false; } if (strcmp(getType(definition), "primitive function") != 0) { - printdebug( - "not checking if a function is called with as or not (%s) -- " - "invalid op", getType(definition)); + printdebug("not checking if a function is called with as or " + "not (%s) -- " + "invalid op", + getType(definition)); return 0; } return definition->additionalinfo->FunDecAdInfo->regularoras; @@ -490,10 +491,10 @@ SymbolTable *init(SymbolTable *start) { boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ - addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr + addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri - boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo + boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo // addr->additionalinfo = CreatePrimitiveInfo(8); start->Line_Number = 1; @@ -563,7 +564,7 @@ AdInfo *getAdInfo(TableNode *tn) { return tn->additionalinfo; } -//simplified getAdInfoType +// simplified getAdInfoType int getAdInfoType(TableNode *tn) { if (tn == NULL) { printdebug( @@ -633,7 +634,8 @@ int getAdInfoType(TableNode *tn) { }*/ } -TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) { +TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, + AdInfo *ad) { if (table == NULL) { printdebug("Null reference to table"); @@ -656,12 +658,12 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, return undefined; } - TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); - if(tag<1 && tag>11){ - printdebug("Note- not passing in valid 'tag' identifier to create entry function. Setting tag to undefined"); + if (tag < 1 && tag > 11) { + printdebug("Note- not passing in valid 'tag' identifier to " + "create entry function. Setting tag to undefined"); newEntry->tag = TYPE_UNDEFINED; - } else{ + } else { newEntry->tag = tag; } newEntry->theType = typeOf /*topDef*/; @@ -680,8 +682,6 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, } } - - TableNode *getTypeEntry(TableNode *tn) { if (tn == NULL) { printdebug("passed a NULL table entry to getType"); @@ -766,12 +766,12 @@ TableNode *addName(TableNode *tn, char *str) { return undefined; } if (tn->theName != NULL) { - //printdebug( - //"Name doesn't look like it is empty before you change. " - //"Are you sure you need to update name?"); + // printdebug( + //"Name doesn't look like it is empty before you change. " + //"Are you sure you need to update name?"); if (str != NULL) { - tn->theName = str; - return tn; + tn->theName = str; + return tn; } printdebug("passed a NULL string to the addName function"); return undefined; @@ -839,177 +839,177 @@ TableNode *look_up(SymbolTable *table, char *x) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) { - if (table == NULL) { - printdebug( - "%s[FATAL] passed in NULL table to print_symbol_table", - COLOR_RED); - return; - } + if (table == NULL) { + printdebug( + "%s[FATAL] passed in NULL table to print_symbol_table", + COLOR_RED); + return; + } - if (table->Parent_Scope == NULL) { - fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", - "SCOPE", "PARENT", "TYPE", "Extra annotation"); - } + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", + "SCOPE", "PARENT", "TYPE", "Extra annotation"); + } - TableNode *entrie = table->entries; - fprintf(file_ptr, - "-------------------------:--------:--------:------------------" - "--------:------------------------------\n"); - int parant_scope = 0; - int current_scope = 0; - if (table->Parent_Scope != NULL) { - parant_scope = getParent(table)->Line_Number * 1000 + - getParent(table)->Column_Number; - current_scope = - table->Line_Number * 1000 + table->Column_Number; - } else { - current_scope = 1001; - } - if (entrie == NULL) { - fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", - current_scope, parant_scope, "", "Empty Scope"); - } - for (; entrie != NULL; entrie = getNextEntry(entrie)) { - if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { - if (parant_scope == 0) { + TableNode *entrie = table->entries; + fprintf(file_ptr, + "-------------------------:--------:--------:------------------" + "--------:------------------------------\n"); + int parant_scope = 0; + int current_scope = 0; + if (table->Parent_Scope != NULL) { + parant_scope = getParent(table)->Line_Number * 1000 + + getParent(table)->Column_Number; + current_scope = + table->Line_Number * 1000 + table->Column_Number; + } else { + current_scope = 1001; + } + if (entrie == NULL) { + fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", + current_scope, parant_scope, "", "Empty Scope"); + } + for (; entrie != NULL; entrie = getNextEntry(entrie)) { + if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-25s: %06d : : %d -> %-20s: " - "%-30s\n", - entrie->theName, current_scope, - entrie->additionalinfo->ArrayAdInfo - ->numofdimensions, - entrie->additionalinfo->ArrayAdInfo - ->typeofarray->theName, - "Type of Array"); - } else { - fprintf(file_ptr, - "%-25s: %06d : : %d -> %-20s: " - "%-30s\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, + "%-25s: %06d : : %d -> %-20s: " + "%-30s\n", + entrie->theName, current_scope, + entrie->additionalinfo->ArrayAdInfo + ->numofdimensions, + entrie->additionalinfo->ArrayAdInfo + ->typeofarray->theName, + "Type of Array"); + } else { + fprintf(file_ptr, + "%-25s: %06d : : %d -> %-20s: " + "%-30s\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, - "%-25s: %06d : : %-25s: " - "elements-%-30d\n", - entrie->theName, current_scope, - "record", - entrie->additionalinfo->RecAdInfo - ->numofelements); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: " - "elements-%-30d\n", - entrie->theName, current_scope, - parant_scope, "record", - entrie->additionalinfo->RecAdInfo - ->numofelements); - } - } - if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { - if (parant_scope == 0) { + fprintf(file_ptr, + "%-25s: %06d : : %-25s: " + "elements-%-30d\n", + entrie->theName, current_scope, + "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: " + "elements-%-30d\n", + entrie->theName, current_scope, + parant_scope, "record", + entrie->additionalinfo->RecAdInfo + ->numofelements); + } + } + if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { + if (parant_scope == 0) { - fprintf( - file_ptr, - "%-25s: %06d : : %-25s: size-%d " - "bytes\n", - entrie->theName, current_scope, "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } else { - fprintf( - file_ptr, - "%-25s: %06d : %06d : %-25s: size-%-30d " - "bytes\n", - entrie->theName, current_scope, - parant_scope, "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } - } - if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { - if (parant_scope == 0) { + fprintf( + file_ptr, + "%-25s: %06d : : %-25s: size-%d " + "bytes\n", + entrie->theName, current_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } else { + fprintf( + file_ptr, + "%-25s: %06d : %06d : %-25s: size-%-30d " + "bytes\n", + entrie->theName, current_scope, + parant_scope, "Primitive", + entrie->additionalinfo->PrimAdInfo->size); + } + } + if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-25s: %06d : : %-25s -> " - "%-25s: %-30s\n", - entrie->theName, current_scope, - entrie->additionalinfo->FunTypeAdInfo - ->parameter->theName, - entrie->additionalinfo->FunTypeAdInfo - ->returntype->theName, - "Type of Function"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s -> %-21s: " - "%-30s\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 (parant_scope == 0) { + fprintf(file_ptr, + "%-25s: %06d : : %-25s -> " + "%-25s: %-30s\n", + entrie->theName, current_scope, + entrie->additionalinfo->FunTypeAdInfo + ->parameter->theName, + entrie->additionalinfo->FunTypeAdInfo + ->returntype->theName, + "Type of Function"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s -> %-21s: " + "%-30s\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 (parant_scope == 0) { - fprintf(file_ptr, - "%-25s: %06d : : %-25s: %-30s\n", - entrie->theName, current_scope, - getType(entrie), "User Defined"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: %-30s\n", - entrie->theName, current_scope, - parant_scope, getType(entrie), - "User Defined"); - } - } - if (getAdInfoType(entrie) == TYPE_UNDEFINED) { - if (parant_scope == 0) { + fprintf(file_ptr, + "%-25s: %06d : : %-25s: %-30s\n", + entrie->theName, current_scope, + getType(entrie), "User Defined"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: %-30s\n", + entrie->theName, current_scope, + parant_scope, getType(entrie), + "User Defined"); + } + } + if (getAdInfoType(entrie) == TYPE_UNDEFINED) { + if (parant_scope == 0) { - fprintf(file_ptr, - "%-25s: %06d : : %-25s: %-30s\n", - entrie->theName, current_scope, - "undefined", "undefined entry"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: %-30s\n", - entrie->theName, current_scope, - parant_scope, "undefined", - "undefined entry"); - } - } - } - if (getChildren(table) != NULL) { - ListOfTable *node = getChildren(table); - for (; node != NULL; node = node->next) { - if ((node->table) == NULL) { - print_symbol_table(node->table, file_ptr); - } else { - if ((node->table)->Line_Number == -1) { - continue; - } else { - print_symbol_table(node->table, - file_ptr); - } - } - } - } - if (getParent(table) == NULL) { - fprintf(file_ptr, - "-------------------------:--------:--------:----------" - "----------------:------------------------------\n"); - } + fprintf(file_ptr, + "%-25s: %06d : : %-25s: %-30s\n", + entrie->theName, current_scope, + "undefined", "undefined entry"); + } else { + fprintf(file_ptr, + "%-25s: %06d : %06d : %-25s: %-30s\n", + entrie->theName, current_scope, + parant_scope, "undefined", + "undefined entry"); + } + } + } + if (getChildren(table) != NULL) { + ListOfTable *node = getChildren(table); + for (; node != NULL; node = node->next) { + if ((node->table) == NULL) { + print_symbol_table(node->table, file_ptr); + } else { + if ((node->table)->Line_Number == -1) { + continue; + } else { + print_symbol_table(node->table, + file_ptr); + } + } + } + } + if (getParent(table) == NULL) { + fprintf(file_ptr, + "-------------------------:--------:--------:----------" + "----------------:------------------------------\n"); + } } // get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { @@ -1138,3 +1138,101 @@ TableNode *getNextEntry(TableNode *tn) { } return tn->next; } + +// Prints all info about a table node +// Uses pointers to the table node to print the info +TableNode *printTableNode(TableNode *tn) { + if (DEBUG == 0) { + return tn; + } + + if (tn == NULL) { + printdebug("%s[PrintTN] Passed a NULL tablenode!", COLOR_RED); + return undefined; + } + if (tn->theName == NULL) { + printdebug("%s[PrintTN] Passed a tablenode with NULL name!", + COLOR_RED); + return undefined; + } + if (tn->theType == NULL) { + printdebug("%s[PrintTN] Passed a tablenode with NULL type!", + COLOR_RED); + return undefined; + } + if (tn == undefined) { + printdebug("%s[PrintTN] Passed an undefined tablenode!", + COLOR_RED); + return undefined; + } + if (tn->additionalinfo == NULL) { + printdebug( + "%s[PrintTN] Passed a tablenode with NULL additional info!", + COLOR_RED); + return undefined; + } + + printdebug("%s[PrintTN] Printing tablenode...", COLOR_ORANGE); + printdebug(" %sName: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->theName); + printdebug(" %sType: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->theType->theName); + printdebug(" %sTag: %s%d", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->tag); + + if (tn->tag == TYPE_RECORD_TYPE || tn->tag == TYPE_RECORD) { + printdebug(" %sAdditional Info: %sRecAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %snumberOfElements: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->RecAdInfo->numofelements); + if (tn->additionalinfo->RecAdInfo->recordScope == NULL) { + printdebug(" %srecordScope (line): %s(NULL)", + COLOR_YELLOW, COLOR_LIGHTBLUE); + } else { + printdebug(" %srecordScope (line): %s%d", + COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->RecAdInfo->recordScope + ->Line_Number); + } + } else if (tn->tag == TYPE_ARRAY_TYPE || tn->tag == TYPE_ARRAY) { + printdebug(" %sAdditional Info: %sArrayAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %snumberOfDimensions: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->ArrayAdInfo->numofdimensions); + printdebug( + " %stypeOfArray: %s%s", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->ArrayAdInfo->typeofarray->theName); + } else if (tn->tag == TYPE_FUNCTION_TYPE) { + printdebug(" %sAdditional Info: %sFunTypeAdInfo", + COLOR_YELLOW, COLOR_LIGHTBLUE); + printdebug( + " %sparameter: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->FunTypeAdInfo->parameter->theName); + printdebug( + " %sreturntype: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->FunTypeAdInfo->returntype->theName); + } else if (tn->tag == TYPE_FUNCTION_DECLARATION) { + printdebug(" %sAdditional Info: %sFunDecAdInfo", + COLOR_YELLOW, COLOR_LIGHTBLUE); + printdebug(" %sstartLineNumber: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->FunDecAdInfo->startlinenumber); + printdebug(" %sregularOrAs: %s%s", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->FunDecAdInfo->regularoras + ? "true" + : "false"); + } else if (tn->tag == TYPE_PRIMITIVE) { + printdebug(" %sAdditional Info: %sPrimAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %ssize: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->PrimAdInfo->size); + } else { + printdebug(" AdInfo not handled."); + } + + return tn; +} \ No newline at end of file diff --git a/src/symbol_table.h b/src/symbol_table.h index cf1273a..7c5322d 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -124,6 +124,7 @@ ListOfTable *getRestOfChildren(ListOfTable *lt); TableNode *getFirstEntry(SymbolTable *st); TableNode *getNextEntry(TableNode *tn); +TableNode * printTableNode(TableNode * tn); void printdebug_impl(char *file, int line, const char *format, ...); #define printdebug(format, ...) \ printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) From e7ee370dcfc53ff4756c12502b47f5cedcee126c Mon Sep 17 00:00:00 2001 From: Scarlett Date: Mon, 7 Apr 2025 17:00:24 -0400 Subject: [PATCH 14/45] fixed segfault, but am now having a new unknown one :( --- src/grammar.y | 7 +++---- src/symbol_table.c | 10 +++++----- tests/sprint2/test/sp2_llnode.alpha | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index b20c09e..946c9ad 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -624,10 +624,9 @@ assignable: //char *funtype = getType(look_up(cur, $1)); printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); - // Something fishy is going on here....... - TableNode *param = getParameter(look_up(getParent(cur), getName((TableNode*)$1))); - printTableNode(table_lookup(getAncestor(cur), getName((TableNode*)$1))); - + TableNode * typeNode = table_lookup(getAncestor(cur), getType((TableNode*)$1)); + TableNode *param = getParameter(typeNode); + printTableNode(param); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); diff --git a/src/symbol_table.c b/src/symbol_table.c index 3e67b95..cec150c 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1150,6 +1150,11 @@ TableNode *printTableNode(TableNode *tn) { printdebug("%s[PrintTN] Passed a NULL tablenode!", COLOR_RED); return undefined; } + if (tn == undefined) { + printdebug("%s[PrintTN] Passed an undefined tablenode!", + COLOR_RED); + return undefined; + } if (tn->theName == NULL) { printdebug("%s[PrintTN] Passed a tablenode with NULL name!", COLOR_RED); @@ -1160,11 +1165,6 @@ TableNode *printTableNode(TableNode *tn) { COLOR_RED); return undefined; } - if (tn == undefined) { - printdebug("%s[PrintTN] Passed an undefined tablenode!", - COLOR_RED); - return undefined; - } if (tn->additionalinfo == NULL) { printdebug( "%s[PrintTN] Passed a tablenode with NULL additional info!", diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index 2011a83..08048a7 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -78,4 +78,4 @@ entry (arg) := { result := bar2(5,7); return 0; -} +} \ No newline at end of file From 87659ebf46e07468c13dbb1904b6e45664c7b6ba Mon Sep 17 00:00:00 2001 From: Scarlett Date: Mon, 7 Apr 2025 23:53:08 -0400 Subject: [PATCH 15/45] segfaults fixed. print_symbol_table format updated for dynamic column width. --- .clang-format | 6 + src/symbol_table.c | 1933 ++++++++++++++++++++++---------------------- 2 files changed, 978 insertions(+), 961 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..504b122 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 0 +AllowShortStringsOnSingleLine: true +BreakStringLiterals: false +ReflowComments: false diff --git a/src/symbol_table.c b/src/symbol_table.c index cec150c..86fca5b 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -4,113 +4,120 @@ #include "symbol_table.h" void printdebug_impl(char *file, int line, const char *format, ...) { - if (DEBUG) { - printf("%s<%s> [%d]%s ", COLOR_DARKGRAY, file, line, - COLOR_WHITE); - va_list args; - va_start(args, format); - vprintf(format, args); - va_end(args); - printf("\n"); - } + if (DEBUG) { + printf("%s<%s> [%d]%s ", COLOR_DARKGRAY, file, line, + COLOR_WHITE); + va_list args; + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); + } } // primitive additional info only stores the size of that type AdInfo *CreatePrimitiveInfo(int size) { - - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); - info->PrimAdInfo = (primitive_info *)malloc(sizeof(primitive_info)); - info->PrimAdInfo->size = size; - return info; + AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); + info->PrimAdInfo = (primitive_info *)malloc(sizeof(primitive_info)); + info->PrimAdInfo->size = size; + return info; } // only gets the size of a primitive type int getPrimSize(TableNode *definition) { - if (definition == NULL) { - printdebug( - "passed an NULL entry to getPrimSize function. Invalid."); - return -1; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getPrimSize function. " - "Invalid."); - return -1; - } - if (definition->additionalinfo == NULL) { - printdebug("node has NULL additionalinfo. Invalid."); - return -1; - } - if (strcmp(getType(definition), "primitive") != 0) { - printdebug( - "not checking the size of a primitive -- invalid op"); - return 0; - } - return definition->additionalinfo->PrimAdInfo->size; + if (definition == NULL) { + printdebug( + "passed an NULL entry to getPrimSize function. Invalid."); + return -1; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getPrimSize function. " + "Invalid."); + return -1; + } + if (definition->additionalinfo == NULL) { + printdebug("node has NULL additionalinfo. Invalid."); + return -1; + } + if (strcmp(getType(definition), "primitive") != 0) { + printdebug( + "not checking the size of a primitive -- invalid op"); + return 0; + } + return definition->additionalinfo->PrimAdInfo->size; } // Only information stored in array info is the number of dimensions and the // 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, TableNode *type) { - if (type == NULL) { - printdebug("passed a NULL reference to " - "CreateArrayInfo. Invalid."); - return NULL; - } - if (type == undefined) { - printdebug("passed an undefined reference to " - "CreateArrayInfo. Invalid."); - return NULL; - } - if (type == undefined) { - printdebug("passed a undefined type reference to " - "CreateArrayInfo. Invalid."); - return NULL; - } - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); - info->ArrayAdInfo = (array_info *)malloc(sizeof(array_info)); - info->ArrayAdInfo->numofdimensions = dim; - info->ArrayAdInfo->typeofarray = type; - // avoiding storing any types like below - // int* dimensionsizes = loc; - return info; + if (type == NULL) { + printdebug( + "passed a NULL reference to " + "CreateArrayInfo. Invalid."); + return NULL; + } + if (type == undefined) { + printdebug( + "passed an undefined reference to " + "CreateArrayInfo. Invalid."); + return NULL; + } + if (type == undefined) { + printdebug( + "passed a undefined type reference to " + "CreateArrayInfo. Invalid."); + return NULL; + } + AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); + info->ArrayAdInfo = (array_info *)malloc(sizeof(array_info)); + info->ArrayAdInfo->numofdimensions = dim; + info->ArrayAdInfo->typeofarray = type; + // avoiding storing any types like below + // int* dimensionsizes = loc; + return info; } // This gets the number of dimensions from array info int getNumArrDim(TableNode *definition) { - if (definition == NULL) { - printdebug("passed an NULL entry to getNumArrDim " - "function. Invalid."); - return -1; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getNumArrDim " - "function. Invalid."); - return -1; - } - if (strcmp(getType(definition), "array") != 0) { - printdebug("not checking the dim of an array -- invalid op"); - return 0; - } - return definition->additionalinfo->ArrayAdInfo->numofdimensions; + if (definition == NULL) { + printdebug( + "passed an NULL entry to getNumArrDim " + "function. Invalid."); + return -1; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getNumArrDim " + "function. Invalid."); + return -1; + } + if (strcmp(getType(definition), "array") != 0) { + printdebug("not checking the dim of an array -- invalid op"); + return 0; + } + return definition->additionalinfo->ArrayAdInfo->numofdimensions; } // 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) { - printdebug("passed an NULL entry to getArrType " - "function. Invalid."); - return NULL; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getArrType " - "function. Invalid."); - return NULL; - } - if (strcmp(getType(definition), "array") != 0) { - printdebug("not checking the type of an array -- invalid op"); - return undefined; - } - return definition->additionalinfo->ArrayAdInfo->typeofarray; + if (definition == NULL) { + printdebug( + "passed an NULL entry to getArrType " + "function. Invalid."); + return NULL; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getArrType " + "function. Invalid."); + return NULL; + } + if (strcmp(getType(definition), "array") != 0) { + printdebug("not checking the type of an array -- invalid op"); + return undefined; + } + return definition->additionalinfo->ArrayAdInfo->typeofarray; } // Record type currently stores the number of elements as well as the types, in @@ -118,391 +125,413 @@ TableNode *getArrType(TableNode *definition) { // should probably instead be replaced by a reference to a scope in which those // elements are found. AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); - info->RecAdInfo = (record_info *)malloc(sizeof(record_info)); - info->RecAdInfo->numofelements = length; - // replace below with reference to a scope, not an array - info->RecAdInfo->recordScope = recordScope; - return info; + AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); + info->RecAdInfo = (record_info *)malloc(sizeof(record_info)); + info->RecAdInfo->numofelements = length; + // replace below with reference to a scope, not an array + info->RecAdInfo->recordScope = recordScope; + return info; } // This gets the number of elements that make up a record. // Perhaps this may not be needed since we need to iterate over all elements // anyways. int getRecLength(TableNode *definition) { - if (definition == NULL) { - printdebug("passed an NULL entry to getRecLength " - "function. Invalid."); - return -1; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getRecLength " - "function. Invalid."); - return -1; - } - if (strcmp(getType(definition), "record") != 0) { - printdebug( - "not checking the length of an record -- invalid op"); - return 0; - } - return definition->additionalinfo->RecAdInfo->numofelements; + if (definition == NULL) { + printdebug( + "passed an NULL entry to getRecLength " + "function. Invalid."); + return -1; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getRecLength " + "function. Invalid."); + return -1; + } + if (strcmp(getType(definition), "record") != 0) { + printdebug( + "not checking the length of an record -- invalid op"); + return 0; + } + return definition->additionalinfo->RecAdInfo->numofelements; } // This gets the array. Needs to up be updated to get the scope instead SymbolTable *getRecList(TableNode *definition) { - if (definition == NULL) { - printdebug("passed a NULL entry to getRecList " - "function. Invalid."); - return NULL; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getRecList " - "function. Invalid."); - return NULL; - } - if (strcmp(getType(definition), "record") != 0) { - printdebug( - "not checking the list of types of a record -- invalid " - "op"); - return NULL; - } - return definition->additionalinfo->RecAdInfo->recordScope; + if (definition == NULL) { + printdebug( + "passed a NULL entry to getRecList " + "function. Invalid."); + return NULL; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getRecList " + "function. Invalid."); + return NULL; + } + if (strcmp(getType(definition), "record") != 0) { + printdebug( + "not checking the list of types of a record -- invalid " + "op"); + return NULL; + } + return definition->additionalinfo->RecAdInfo->recordScope; } TableNode *setRecSize(TableNode *tn, int n) { - if (tn == NULL) { - printdebug("passed in NULL entry for setRecSize. Invalid"); - return undefined; - } - if (tn == undefined) { - printdebug("passed in undefined entry for setRecSize. Invalid"); - return undefined; - } - tn->additionalinfo->RecAdInfo->numofelements = n; - return tn; + if (tn == NULL) { + printdebug("passed in NULL entry for setRecSize. Invalid"); + return undefined; + } + if (tn == undefined) { + printdebug("passed in undefined entry for setRecSize. Invalid"); + return undefined; + } + tn->additionalinfo->RecAdInfo->numofelements = n; + return tn; } int getRecSize(SymbolTable *tn) { - if (tn == NULL) { - printdebug( - "passed in NULL SymbolTable for getRecSize. Invalid"); - return -1; - } - int s = 1; - TableNode *cur = getFirstEntry(tn); - if (cur != NULL) { - while (getNextEntry(cur) != NULL) { - s++; - cur = getNextEntry(cur); - } - return s; - } + if (tn == NULL) { + printdebug( + "passed in NULL SymbolTable for getRecSize. Invalid"); return -1; + } + int s = 1; + TableNode *cur = getFirstEntry(tn); + if (cur != NULL) { + while (getNextEntry(cur) != NULL) { + s++; + cur = getNextEntry(cur); + } + return s; + } + 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 AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) { - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); - info->FunDecAdInfo = (function_declaration_info *)malloc( - sizeof(function_declaration_info)); - info->FunDecAdInfo->startlinenumber = line; - info->FunDecAdInfo->regularoras = asorregular; - return info; + AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); + info->FunDecAdInfo = (function_declaration_info *)malloc( + sizeof(function_declaration_info)); + info->FunDecAdInfo->startlinenumber = line; + info->FunDecAdInfo->regularoras = asorregular; + return info; } // 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) { - printdebug("passed a NULL entry to getStartLine " - "function. Invalid."); - return -1; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getStartLine " - "function. Invalid."); - return -1; - } - if (strcmp(getType(definition), "primitive function") != 0) { - printdebug( - "not checking the start line of a function -- invalid " - "op"); - return 0; - } - return definition->additionalinfo->FunDecAdInfo->startlinenumber; + if (definition == NULL) { + printdebug( + "passed a NULL entry to getStartLine " + "function. Invalid."); + return -1; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getStartLine " + "function. Invalid."); + return -1; + } + if (strcmp(getType(definition), "primitive function") != 0) { + printdebug( + "not checking the start line of a function -- invalid " + "op"); + return 0; + } + return definition->additionalinfo->FunDecAdInfo->startlinenumber; } TableNode *setStartLine(TableNode *tn, int start) { - if (tn == NULL) { - printdebug("passing in a NULL entry to setStartLine. " - "invalid"); - return undefined; - } - if (tn == undefined) { - printdebug("passing in an undefined entry to setStartLine. " - "invalid"); - return undefined; - } - tn->additionalinfo->FunDecAdInfo->startlinenumber = start; - return tn; + if (tn == NULL) { + printdebug( + "passing in a NULL entry to setStartLine. " + "invalid"); + return undefined; + } + if (tn == undefined) { + printdebug( + "passing in an undefined entry to setStartLine. " + "invalid"); + 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) { - printdebug("passed a NULL entry to getAsKeyword " - "function. Invalid."); - return false; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getAsKeyword " - "function. Invalid."); - return false; - } - if (strcmp(getType(definition), "primitive function") != 0) { - printdebug("not checking if a function is called with as or " - "not (%s) -- " - "invalid op", - getType(definition)); - return 0; - } - return definition->additionalinfo->FunDecAdInfo->regularoras; + if (definition == NULL) { + printdebug( + "passed a NULL entry to getAsKeyword " + "function. Invalid."); + return false; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getAsKeyword " + "function. Invalid."); + return false; + } + if (strcmp(getType(definition), "primitive function") != 0) { + printdebug( + "not checking if a function is called with as or " + "not (%s) -- " + "invalid op", + getType(definition)); + return 0; + } + return definition->additionalinfo->FunDecAdInfo->regularoras; } TableNode *setAsKeyword(TableNode *tn, bool as) { - if (tn == NULL) { - printdebug("passing in a NULL entry to setAsKeyword. " - "invalid"); - return undefined; - } - if (tn == undefined) { - printdebug("passing in an undefined entry to setAsKeyword. " - "invalid"); - return undefined; - } - tn->additionalinfo->FunDecAdInfo->regularoras = as; - return tn; + if (tn == NULL) { + printdebug( + "passing in a NULL entry to setAsKeyword. " + "invalid"); + return undefined; + } + if (tn == undefined) { + printdebug( + "passing in an undefined entry to setAsKeyword. " + "invalid"); + return undefined; + } + 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) { - printdebug("passed a NULL parameter to " - "CreateFunctionTypeInfo. Invalid."); - return NULL; - } - if (parameter == undefined) { - printdebug("passed an undefined parameter to " - "CreateFunctionTypeInfo. Invalid."); - return NULL; - } - if (returntype == NULL) { - printdebug("passed a NULL return type to " - "CreateFunctionTypeInfo. Invalid."); - return NULL; - } - if (returntype == undefined) { - printdebug("passed an undefined return type to " - "CreateFunctionTypeInfo. Invalid."); - return NULL; - } - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); - info->FunTypeAdInfo = - (function_type_info *)malloc(sizeof(function_type_info)); - info->FunTypeAdInfo->parameter = parameter; - info->FunTypeAdInfo->returntype = returntype; - return info; + if (parameter == NULL) { + printdebug( + "passed a NULL parameter to " + "CreateFunctionTypeInfo. Invalid."); + return NULL; + } + if (parameter == undefined) { + printdebug( + "passed an undefined parameter to " + "CreateFunctionTypeInfo. Invalid."); + return NULL; + } + if (returntype == NULL) { + printdebug( + "passed a NULL return type to " + "CreateFunctionTypeInfo. Invalid."); + return NULL; + } + if (returntype == undefined) { + printdebug( + "passed an undefined return type to " + "CreateFunctionTypeInfo. Invalid."); + return NULL; + } + AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); + info->FunTypeAdInfo = + (function_type_info *)malloc(sizeof(function_type_info)); + info->FunTypeAdInfo->parameter = parameter; + info->FunTypeAdInfo->returntype = returntype; + return info; } // returns parameter type of a function TableNode *getParameter(TableNode *definition) { - if (definition == NULL) { - printdebug("passed a NULL entry to getParameter " - "function. Invalid."); - return undefined; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getParameter " - "function. Invalid."); - return undefined; - } - if (strcmp(getType(definition), "primitive function type") != 0) { - printdebug( - "not checking the parameter of a function -- invalid op"); - return undefined; - } - return definition->additionalinfo->FunTypeAdInfo->parameter; + if (definition == NULL) { + printdebug( + "passed a NULL entry to getParameter " + "function. Invalid."); + return undefined; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getParameter " + "function. Invalid."); + return undefined; + } + if (strcmp(getType(definition), "primitive function type") != 0) { + printdebug( + "not checking the parameter of a function -- invalid op"); + return undefined; + } + return definition->additionalinfo->FunTypeAdInfo->parameter; } // returns return type of a function TableNode *getReturn(TableNode *definition) { - if (definition == NULL) { - printdebug("passed a NULL entry to getReturn " - "function. Invalid."); - return NULL; - } - if (definition == undefined) { - printdebug("passed an undefined entry to getReturn " - "function. Invalid."); - return NULL; - } - if (strcmp(getType(definition), "primitive function type") != 0) { - printdebug( - "not checking the return of a function -- invalid op"); - return undefined; - } - return definition->additionalinfo->FunTypeAdInfo->returntype; + if (definition == NULL) { + printdebug( + "passed a NULL entry to getReturn " + "function. Invalid."); + return NULL; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getReturn " + "function. Invalid."); + return NULL; + } + if (strcmp(getType(definition), "primitive function type") != 0) { + printdebug( + "not checking the return of a function -- invalid op"); + return undefined; + } + return definition->additionalinfo->FunTypeAdInfo->returntype; } // creates a new scope (not the top scope though) SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) { - SymbolTable *table = (SymbolTable *)malloc(sizeof(SymbolTable)); - table->Line_Number = Line; - table->Column_Number = Column; - table->Parent_Scope = ParentScope; - table->Children_Scope = NULL; - table->entries = NULL; - if (ParentScope != NULL) { - if (ParentScope->Children_Scope == NULL) { - ListOfTable *newEntry = - (ListOfTable *)malloc(sizeof(ListOfTable)); - newEntry->next = NULL; - // newEntry->prev = NULL; - newEntry->table = table; - ParentScope->Children_Scope = newEntry; - } else { - ListOfTable *newEntry = - (ListOfTable *)malloc(sizeof(ListOfTable)); - // newEntry->prev = NULL; - newEntry->table = table; - ListOfTable *oldEntry = ParentScope->Children_Scope; - ParentScope->Children_Scope = newEntry; - newEntry->next = oldEntry; - } + SymbolTable *table = (SymbolTable *)malloc(sizeof(SymbolTable)); + table->Line_Number = Line; + table->Column_Number = Column; + table->Parent_Scope = ParentScope; + table->Children_Scope = NULL; + table->entries = NULL; + if (ParentScope != NULL) { + if (ParentScope->Children_Scope == NULL) { + ListOfTable *newEntry = + (ListOfTable *)malloc(sizeof(ListOfTable)); + newEntry->next = NULL; + // newEntry->prev = NULL; + newEntry->table = table; + ParentScope->Children_Scope = newEntry; + } else { + ListOfTable *newEntry = + (ListOfTable *)malloc(sizeof(ListOfTable)); + // newEntry->prev = NULL; + newEntry->table = table; + ListOfTable *oldEntry = ParentScope->Children_Scope; + ParentScope->Children_Scope = newEntry; + newEntry->next = oldEntry; } - return table; + } + return table; } // create entry just for things below top level scope // This function defines the integer, address, character, and bool primitive // types SymbolTable *init(SymbolTable *start) { - if (start->Parent_Scope != NULL) { - printdebug("%s[FATAL] Cannot initialize a scope that is not " - "the parent scope", - COLOR_RED); - 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)); - // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); - start->entries = integ; - integ->next = addr; - addr->next = chara; - chara->next = stri; - stri->next = boo; - // boo->next = arr; - boo->next = NULL; + if (start->Parent_Scope != NULL) { + printdebug( + "%s[FATAL] Cannot initialize a scope that is not " + "the parent scope", + COLOR_RED); + 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)); + // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); + start->entries = integ; + integ->next = addr; + addr->next = chara; + chara->next = stri; + stri->next = boo; + // boo->next = arr; + boo->next = NULL; - integ->theName = "integer"; - addr->theName = "address"; - chara->theName = "character"; - boo->theName = "Boolean"; - stri->theName = "string"; - // arr->theName= "array" + integ->theName = "integer"; + addr->theName = "address"; + chara->theName = "character"; + boo->theName = "Boolean"; + stri->theName = "string"; + // arr->theName= "array" - // root TableNode that all are pointing to but not in table - // This is only to solve the issue that all entries must have a name and - // a type and the type must point to an actual table entry Again, this - // primitive table entry isn't in the top scope. It is outside the top - // scope and is only there to facilitate the fact that these are - // primitive - TableNode *prime = (TableNode *)malloc(sizeof(TableNode)); - prime->theName = "primitive"; - prime->theType = NULL; - prime->additionalinfo = NULL; - prime->next = NULL; - prime->tag = TYPE_SYSTEM_DEFINED; + // root TableNode that all are pointing to but not in table + // This is only to solve the issue that all entries must have a name and + // a type and the type must point to an actual table entry Again, this + // primitive table entry isn't in the top scope. It is outside the top + // scope and is only there to facilitate the fact that these are + // primitive + TableNode *prime = (TableNode *)malloc(sizeof(TableNode)); + prime->theName = "primitive"; + prime->theType = NULL; + prime->additionalinfo = NULL; + prime->next = NULL; + prime->tag = TYPE_SYSTEM_DEFINED; - // not sure exatly how to get array types to look right so using a dummy - // Table Node below and updating the print symbol table function to - // access the additional information to print for array types, similar - // to function types when printing symbol table, if array is seen - arrayprim = (TableNode *)malloc(sizeof(TableNode)); - arrayprim->theName = "array"; - arrayprim->theType = NULL; - arrayprim->additionalinfo = NULL; - arrayprim->next = NULL; - prime->tag = TYPE_SYSTEM_DEFINED; + // not sure exatly how to get array types to look right so using a dummy + // Table Node below and updating the print symbol table function to + // access the additional information to print for array types, similar + // to function types when printing symbol table, if array is seen + arrayprim = (TableNode *)malloc(sizeof(TableNode)); + arrayprim->theName = "array"; + arrayprim->theType = NULL; + arrayprim->additionalinfo = NULL; + arrayprim->next = NULL; + prime->tag = TYPE_SYSTEM_DEFINED; - // funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL); + // funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL); - // similar workaround to arrays above - funprime = (TableNode *)malloc(sizeof(TableNode)); - funprime->theName = "primitive function"; - funprime->theType = NULL; - funprime->additionalinfo = NULL; - funprime->next = NULL; - funprime->tag = TYPE_SYSTEM_DEFINED; + // similar workaround to arrays above + funprime = (TableNode *)malloc(sizeof(TableNode)); + funprime->theName = "primitive function"; + funprime->theType = NULL; + funprime->additionalinfo = NULL; + funprime->next = NULL; + funprime->tag = TYPE_SYSTEM_DEFINED; - // record - recprime = (TableNode *)malloc(sizeof(TableNode)); - recprime->theName = "record"; - recprime->theType = NULL; - recprime->additionalinfo = NULL; - recprime->next = NULL; - recprime->tag = TYPE_SYSTEM_DEFINED; + // record + recprime = (TableNode *)malloc(sizeof(TableNode)); + recprime->theName = "record"; + recprime->theType = NULL; + recprime->additionalinfo = NULL; + recprime->next = NULL; + recprime->tag = TYPE_SYSTEM_DEFINED; - funtypeprime = (TableNode *)malloc(sizeof(TableNode)); - funtypeprime->theName = "primitive function type"; - funtypeprime->theType = NULL; - funtypeprime->additionalinfo = NULL; - funtypeprime->next = NULL; - funtypeprime->tag = TYPE_SYSTEM_DEFINED; + funtypeprime = (TableNode *)malloc(sizeof(TableNode)); + funtypeprime->theName = "primitive function type"; + funtypeprime->theType = NULL; + funtypeprime->additionalinfo = NULL; + funtypeprime->next = NULL; + funtypeprime->tag = TYPE_SYSTEM_DEFINED; - undefined = (TableNode *)malloc(sizeof(TableNode)); - undefined->theName = "undefined"; - undefined->theType = NULL; - undefined->additionalinfo = NULL; - undefined->next = NULL; - undefined->tag = TYPE_SYSTEM_DEFINED; + undefined = (TableNode *)malloc(sizeof(TableNode)); + undefined->theName = "undefined"; + undefined->theType = NULL; + undefined->additionalinfo = NULL; + undefined->next = NULL; + undefined->tag = TYPE_SYSTEM_DEFINED; - // Undefined_function_type_info = CreateFunctionTypeInfo(undefined, - // undefined); + // Undefined_function_type_info = CreateFunctionTypeInfo(undefined, + // undefined); - integ->theType = prime; - addr->theType = prime; - chara->theType = prime; - stri->theType = arrayprim; - boo->theType = prime; - // arr->theType=arrayprim; + integ->theType = prime; + addr->theType = prime; + chara->theType = prime; + stri->theType = arrayprim; + boo->theType = prime; + // arr->theType=arrayprim; - // filling in all the values for the additional info for initial types - // These numbers below for create primitive specifically are supposed to - // be the size of these primitive types. We can change these if needed - // to not be hard coded numbers as a reminder, stri below is defined as - // a one dimensional array of characters - integ->additionalinfo = CreatePrimitiveInfo(SIZE_INT); - addr->additionalinfo = CreatePrimitiveInfo(SIZE_ADDR); - chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR); - stri->additionalinfo = CreateArrayInfo(1, chara); - boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); + // filling in all the values for the additional info for initial types + // These numbers below for create primitive specifically are supposed to + // be the size of these primitive types. We can change these if needed + // to not be hard coded numbers as a reminder, stri below is defined as + // a one dimensional array of characters + integ->additionalinfo = CreatePrimitiveInfo(SIZE_INT); + addr->additionalinfo = CreatePrimitiveInfo(SIZE_ADDR); + chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR); + stri->additionalinfo = CreateArrayInfo(1, chara); + boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); - integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ - addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr - chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara - stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri - boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo - // addr->additionalinfo = CreatePrimitiveInfo(8); + integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ + addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr + chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara + stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri + boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo + // addr->additionalinfo = CreatePrimitiveInfo(8); - start->Line_Number = 1; - start->Column_Number = 1; - start->Parent_Scope = NULL; - start->Children_Scope = NULL; + start->Line_Number = 1; + start->Column_Number = 1; + start->Parent_Scope = NULL; + start->Children_Scope = NULL; - return start; + return start; } /* TableNode* integ; @@ -514,65 +543,67 @@ TableNode* recprime; TableNode* funtypeprime; */ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) { - if (tn == NULL) { - printdebug( - "passed in an NULL table node to populateTypeAndInfo."); - return undefined; - } - if (tn == undefined) { - printdebug( - "passed in an undefined table node to populateTypeAndInfo"); - return undefined; - } - if (type == NULL) { - printdebug("passed in a NULL type reference to " - "populate a table node. Invalid."); - return undefined; - } - if (type == undefined) { - printdebug("passed in an undefined type reference to " - "populate a table node. Invalid."); - return undefined; - } - if (info == NULL) { - printdebug( - "passed in a NULL info reference to populate a table " - "node. Invalid."); - return undefined; - } - tn->theType = type; - tn->additionalinfo = info; - // returning reference to modified table node - return tn; + if (tn == NULL) { + printdebug( + "passed in an NULL table node to populateTypeAndInfo."); + return undefined; + } + if (tn == undefined) { + printdebug( + "passed in an undefined table node to populateTypeAndInfo"); + return undefined; + } + if (type == NULL) { + printdebug( + "passed in a NULL type reference to " + "populate a table node. Invalid."); + return undefined; + } + if (type == undefined) { + printdebug( + "passed in an undefined type reference to " + "populate a table node. Invalid."); + return undefined; + } + if (info == NULL) { + printdebug( + "passed in a NULL info reference to populate a table " + "node. Invalid."); + return undefined; + } + tn->theType = type; + tn->additionalinfo = info; + // returning reference to modified table node + return tn; } AdInfo *getAdInfo(TableNode *tn) { - if (tn == NULL) { - printdebug("passed a NULL table entry to getAdInfo. Invalid."); - return NULL; - } - if (tn == undefined) { - printdebug( - "passed an undefined table entry to getAdInfo. Invalid."); - return NULL; - } - if (tn->additionalinfo == NULL) { - printdebug( - "no additional info found in the table node. Invalid."); - return NULL; - } - return tn->additionalinfo; + if (tn == NULL) { + printdebug("passed a NULL table entry to getAdInfo. Invalid."); + return NULL; + } + if (tn == undefined) { + printdebug( + "passed an undefined table entry to getAdInfo. Invalid."); + return NULL; + } + if (tn->additionalinfo == NULL) { + printdebug( + "no additional info found in the table node. Invalid."); + return NULL; + } + return tn->additionalinfo; } // simplified getAdInfoType int getAdInfoType(TableNode *tn) { - if (tn == NULL) { - printdebug( - "passing in NULL table entry to getAdInfoType. Invalid"); - return -1; - } - return tn->tag; - /* + if (tn == NULL) { + printdebug( + "passing in NULL table entry to getAdInfoType. Invalid"); + return -1; + } + return tn->tag; + /* if (tn == undefined) { printdebug("passing in undefined table entry to getAdInfoType. " "Invalid"); @@ -636,489 +667,463 @@ int getAdInfoType(TableNode *tn) { TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) { + if (table == NULL) { + printdebug("Null reference to table"); + return undefined; + } - if (table == NULL) { - printdebug("Null reference to table"); - return undefined; - } - - /* + /* TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); if(topDef == NULL){ printdebug("This type is not defined at the top level"); return NULL; } */ - if (typeOf == NULL) { - printdebug("Passing an NULL Type Node to Create Entry"); - return undefined; - } - if (typeOf == undefined) { - printdebug("Passing an undefined Type Node to Create Entry"); - return undefined; - } + if (typeOf == NULL) { + printdebug("Passing an NULL Type Node to Create Entry"); + return undefined; + } + if (typeOf == undefined) { + printdebug("Passing an undefined Type Node to Create Entry"); + return undefined; + } - TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); - if (tag < 1 && tag > 11) { - printdebug("Note- not passing in valid 'tag' identifier to " - "create entry function. Setting tag to undefined"); - newEntry->tag = TYPE_UNDEFINED; - } else { - newEntry->tag = tag; - } - newEntry->theType = typeOf /*topDef*/; - newEntry->theName = id; - newEntry->additionalinfo = ad; - if (table->entries == NULL) { - table->entries = newEntry; - printdebug("[CreateEntry] Adding %s to the symbol table", id); - return newEntry; - } else { - TableNode *oldEntry = table->entries; - table->entries = newEntry; - newEntry->next = oldEntry; - printdebug("[CreateEntry] Adding %s to the symbol table", id); - return newEntry; - } + TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); + if (tag < 1 && tag > 11) { + printdebug( + "Note- not passing in valid 'tag' identifier to " + "create entry function. Setting tag to undefined"); + newEntry->tag = TYPE_UNDEFINED; + } else { + newEntry->tag = tag; + } + newEntry->theType = typeOf /*topDef*/; + newEntry->theName = id; + newEntry->additionalinfo = ad; + if (table->entries == NULL) { + table->entries = newEntry; + printdebug("[CreateEntry] Adding %s to the symbol table", id); + return newEntry; + } else { + TableNode *oldEntry = table->entries; + table->entries = newEntry; + newEntry->next = oldEntry; + printdebug("[CreateEntry] Adding %s to the symbol table", id); + return newEntry; + } } TableNode *getTypeEntry(TableNode *tn) { - if (tn == NULL) { - printdebug("passed a NULL table entry to getType"); - return undefined; - } - if (tn == undefined) { - printdebug("passed an undefined table entry to getType"); - return undefined; - } - if (tn->theType == NULL) { - printdebug("type of entry is currently NULL type"); - return undefined; - } - if (tn->theType == undefined) { - printdebug("type of entry is currently undefined type"); - return undefined; - } - return tn->theType; + if (tn == NULL) { + printdebug("passed a NULL table entry to getType"); + return undefined; + } + if (tn == undefined) { + printdebug("passed an undefined table entry to getType"); + return undefined; + } + if (tn->theType == NULL) { + printdebug("type of entry is currently NULL type"); + return undefined; + } + if (tn->theType == undefined) { + printdebug("type of entry is currently undefined type"); + return undefined; + } + return tn->theType; } char *getType(TableNode *tn) { - if (tn == NULL) { - printdebug("passed a NULL table entry to getType"); - return getName(undefined); - } - if (tn == undefined) { - printdebug("passed an undefined table entry to getType"); - return getName(undefined); - } - if (tn->theType == NULL) { - printdebug("type of entry is currently NULL type"); - return getName(undefined); - } - if (tn->theType == undefined) { - printdebug("type of entry is currently undefined type"); - return getName(undefined); - } - return tn->theType->theName; + if (tn == NULL) { + printdebug("passed a NULL table entry to getType"); + return getName(undefined); + } + if (tn == undefined) { + printdebug("passed an undefined table entry to getType"); + return getName(undefined); + } + if (tn->theType == NULL) { + printdebug("type of entry is currently NULL type"); + return getName(undefined); + } + if (tn->theType == undefined) { + printdebug("type of entry is currently undefined type"); + return getName(undefined); + } + return tn->theType->theName; } char *getName(TableNode *tn) { - if (tn == NULL) { - printdebug("passed a NULL table entry to getName"); - return undefined->theName; - } - if (tn == undefined) { - printdebug("passed an undefined table entry to getName"); - return undefined->theName; - } - if (tn->theName == NULL) { - printdebug("name of entry is currently NULL, undefined"); - return undefined->theName; - } - return tn->theName; + if (tn == NULL) { + printdebug("passed a NULL table entry to getName"); + return undefined->theName; + } + if (tn == undefined) { + printdebug("passed an undefined table entry to getName"); + return undefined->theName; + } + if (tn->theName == NULL) { + printdebug("name of entry is currently NULL, undefined"); + return undefined->theName; + } + return tn->theName; } int getLine(SymbolTable *st) { - if (st == NULL) { - printdebug("passed a NULL symbol table to getLine function. " - "Invalid."); - return -1; - } - return st->Line_Number; + if (st == NULL) { + printdebug( + "passed a NULL symbol table to getLine function. " + "Invalid."); + return -1; + } + return st->Line_Number; } int getColumn(SymbolTable *st) { - if (st == NULL) { - printdebug("passed a NULL symbol table to getColumn function. " - "Invalid."); - return -1; - } - return st->Column_Number; + if (st == NULL) { + printdebug( + "passed a NULL symbol table to getColumn function. " + "Invalid."); + return -1; + } + return st->Column_Number; } TableNode *addName(TableNode *tn, char *str) { - if (tn == NULL) { - printdebug("passed a Null table node to the addName " - "function. Invalid."); - return undefined; + if (tn == NULL) { + printdebug( + "passed a Null table node to the addName " + "function. Invalid."); + return undefined; + } + if (tn == undefined) { + printdebug( + "passed an undefined table node to the addName " + "function. Invalid."); + return undefined; + } + if (tn->theName != NULL) { + // printdebug( + //"Name doesn't look like it is empty before you change. " + //"Are you sure you need to update name?"); + if (str != NULL) { + tn->theName = str; + return tn; } - if (tn == undefined) { - printdebug("passed an undefined table node to the addName " - "function. Invalid."); - return undefined; - } - if (tn->theName != NULL) { - // printdebug( - //"Name doesn't look like it is empty before you change. " - //"Are you sure you need to update name?"); - if (str != NULL) { - tn->theName = str; - return tn; - } - printdebug("passed a NULL string to the addName function"); - return undefined; - } - if (str == NULL) { - printdebug( - "passed a NULL string to the addName function. Invalid."); - return undefined; - } - tn->theName = str; - return tn; + printdebug("passed a NULL string to the addName function"); + return undefined; + } + if (str == NULL) { + printdebug( + "passed a NULL string to the addName function. Invalid."); + return undefined; + } + tn->theName = str; + return tn; } SymbolTable *setLineNumber(SymbolTable *st, int line) { - if (st == NULL) { - printdebug("passed a Null Symbol Table to the setLineNumber " - "function. Invalid."); - return st; - } - st->Line_Number = line; + if (st == NULL) { + printdebug( + "passed a Null Symbol Table to the setLineNumber " + "function. Invalid."); return st; + } + st->Line_Number = line; + return st; } SymbolTable *setColumnNumber(SymbolTable *st, int column) { - if (st == NULL) { - printdebug("passed a Null Symbol Table to the setColumnNumber " - "function. Invalid."); - return st; - } - st->Line_Number = column; + if (st == NULL) { + printdebug( + "passed a Null Symbol Table to the setColumnNumber " + "function. Invalid."); return st; + } + st->Line_Number = column; + return st; } // only check table that is given TableNode *table_lookup(SymbolTable *table, char *x) { - if (table == NULL) { - printdebug("passed in empty scope. error."); - return undefined; - } - TableNode *entrie = table->entries; - for (; entrie != NULL; entrie = entrie->next) { - if (!strcmp(entrie->theName, x)) { - return entrie; - } - } + if (table == NULL) { + printdebug("passed in empty scope. error."); return undefined; + } + TableNode *entrie = table->entries; + for (; entrie != NULL; entrie = entrie->next) { + if (!strcmp(entrie->theName, x)) { + return entrie; + } + } + return undefined; } // check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { - if (table == NULL) { - printdebug("passed in empty scope. error."); - return undefined; + if (table == NULL) { + printdebug("passed in empty scope. error."); + return undefined; + } + TableNode *ret = table_lookup(table, x); + if (ret != NULL && ret != undefined) { + return ret; + } + printdebug( + "could not find %s in scope that started at line %d and column " + "%d so moving up a scope", + x, getLine(table), getColumn(table)); + return look_up(table->Parent_Scope, x); +} + +int col_widths[5] = {30, 8, 8, 35, 35}; +void printline(FILE *file_ptr); +void printline(FILE *file_ptr) { + for (int i = 0; i < 5; i++) { + for (int ii = 0; ii < col_widths[i]; ii++) { + fprintf(file_ptr, "-"); } - TableNode *ret = table_lookup(table, x); - if (ret != NULL && ret != undefined) { - return ret; - } - printdebug( - "could not find %s in scope that started at line %d and column " - "%d so moving up a scope", - x, getLine(table), getColumn(table)); - return look_up(table->Parent_Scope, x); + fprintf(file_ptr, ":"); + } + fprintf(file_ptr, "\n"); +} + +void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5); +void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5) { + if (label3 == -100) { + fprintf(file_ptr, "%-*s: %0*d : %*s :%-*s:%-*s\n", + col_widths[0], (label1 ? label1 : ""), + col_widths[1] - 2, label2, + col_widths[2] - 2, "", + col_widths[3], (label4 ? label4 : ""), + col_widths[4], (label5 ? label5 : "")); + } else { + fprintf(file_ptr, "%-*s: %0*d : %0*d :%-*s:%-*s\n", + col_widths[0], (label1 ? label1 : ""), + col_widths[1] - 2, label2, + col_widths[2] - 2, label3, + col_widths[3], (label4 ? label4 : ""), + col_widths[4], (label5 ? label5 : "")); + } } void print_symbol_table(SymbolTable *table, FILE *file_ptr) { + if (table == NULL) { + printdebug("%s[FATAL] passed in NULL table to print_symbol_table", COLOR_RED); + return; + } - if (table == NULL) { - printdebug( - "%s[FATAL] passed in NULL table to print_symbol_table", - COLOR_RED); - return; + if (table->Parent_Scope == NULL) { + fprintf(file_ptr, "%-*s:%-*s:%-*s:%-*s:%-*s:\n", + col_widths[0], " NAME", + col_widths[1], " SCOPE", + col_widths[2], " PARENT", + col_widths[3], " TYPE", + col_widths[4], " EXTRA ANNOTATION"); + } + + TableNode *entry = table->entries; + printline(file_ptr); + int parentScopeNum = 0; + int currentScopeNum = 0; + + if (table->Parent_Scope != NULL) { + parentScopeNum = getParent(table)->Line_Number * 1000 + getParent(table)->Column_Number; + currentScopeNum = table->Line_Number * 1000 + table->Column_Number; + } else { + currentScopeNum = 1001; + } + + if (entry == NULL) { + st_fprint(file_ptr, "", currentScopeNum, parentScopeNum, "", " Empty Scope"); + } + + for (; entry != NULL; entry = getNextEntry(entry)) { + if (getAdInfoType(entry) == TYPE_ARRAY_TYPE) { + char *arrayType = (char *)malloc(100); + sprintf(arrayType, " %d -> %s", entry->additionalinfo->ArrayAdInfo->numofdimensions, + entry->additionalinfo->ArrayAdInfo->typeofarray->theName); + + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, arrayType, " Type of Array"); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, arrayType, " Type of Array"); + } } - if (table->Parent_Scope == NULL) { - fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", - "SCOPE", "PARENT", "TYPE", "Extra annotation"); + if (getAdInfoType(entry) == TYPE_RECORD) { + char *recordAdInfo = (char *)malloc(100); + sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements); + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record", recordAdInfo); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record", recordAdInfo); + } } - TableNode *entrie = table->entries; - fprintf(file_ptr, - "-------------------------:--------:--------:------------------" - "--------:------------------------------\n"); - int parant_scope = 0; - int current_scope = 0; - if (table->Parent_Scope != NULL) { - parant_scope = getParent(table)->Line_Number * 1000 + - getParent(table)->Column_Number; - current_scope = - table->Line_Number * 1000 + table->Column_Number; - } else { - current_scope = 1001; + if (getAdInfoType(entry) == TYPE_PRIMITIVE) { + char *primAdInfo = (char *)malloc(100); + sprintf(primAdInfo, " size-%d bytes", entry->additionalinfo->PrimAdInfo->size); + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Primitive", primAdInfo); + } } - if (entrie == NULL) { - fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", - current_scope, parant_scope, "", "Empty Scope"); + + if (getAdInfoType(entry) == TYPE_FUNCTION_TYPE) { + char *functiontype = (char *)malloc(100); + sprintf(functiontype, " %s -> %s", entry->additionalinfo->FunTypeAdInfo->parameter->theName, + entry->additionalinfo->FunTypeAdInfo->returntype->theName); + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, functiontype, " Type of Function"); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, functiontype, " Type of Function"); + } } - for (; entrie != NULL; entrie = getNextEntry(entrie)) { - if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) { - if (parant_scope == 0) { - fprintf(file_ptr, - "%-25s: %06d : : %d -> %-20s: " - "%-30s\n", - entrie->theName, current_scope, - entrie->additionalinfo->ArrayAdInfo - ->numofdimensions, - entrie->additionalinfo->ArrayAdInfo - ->typeofarray->theName, - "Type of Array"); - } else { - fprintf(file_ptr, - "%-25s: %06d : : %d -> %-20s: " - "%-30s\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, - "%-25s: %06d : : %-25s: " - "elements-%-30d\n", - entrie->theName, current_scope, - "record", - entrie->additionalinfo->RecAdInfo - ->numofelements); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: " - "elements-%-30d\n", - entrie->theName, current_scope, - parant_scope, "record", - entrie->additionalinfo->RecAdInfo - ->numofelements); - } - } - if (getAdInfoType(entrie) == TYPE_PRIMITIVE) { - if (parant_scope == 0) { - - fprintf( - file_ptr, - "%-25s: %06d : : %-25s: size-%d " - "bytes\n", - entrie->theName, current_scope, "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } else { - fprintf( - file_ptr, - "%-25s: %06d : %06d : %-25s: size-%-30d " - "bytes\n", - entrie->theName, current_scope, - parant_scope, "Primitive", - entrie->additionalinfo->PrimAdInfo->size); - } - } - if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { - if (parant_scope == 0) { - - fprintf(file_ptr, - "%-25s: %06d : : %-25s -> " - "%-25s: %-30s\n", - entrie->theName, current_scope, - entrie->additionalinfo->FunTypeAdInfo - ->parameter->theName, - entrie->additionalinfo->FunTypeAdInfo - ->returntype->theName, - "Type of Function"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s -> %-21s: " - "%-30s\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 (parant_scope == 0) { - - fprintf(file_ptr, - "%-25s: %06d : : %-25s: %-30s\n", - entrie->theName, current_scope, - getType(entrie), "User Defined"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: %-30s\n", - entrie->theName, current_scope, - parant_scope, getType(entrie), - "User Defined"); - } - } - if (getAdInfoType(entrie) == TYPE_UNDEFINED) { - if (parant_scope == 0) { - - fprintf(file_ptr, - "%-25s: %06d : : %-25s: %-30s\n", - entrie->theName, current_scope, - "undefined", "undefined entry"); - } else { - fprintf(file_ptr, - "%-25s: %06d : %06d : %-25s: %-30s\n", - entrie->theName, current_scope, - parant_scope, "undefined", - "undefined entry"); - } - } + if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Function", " Function Declaration"); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Function", " Function Declaration"); + } } - if (getChildren(table) != NULL) { - ListOfTable *node = getChildren(table); - for (; node != NULL; node = node->next) { - if ((node->table) == NULL) { - print_symbol_table(node->table, file_ptr); - } else { - if ((node->table)->Line_Number == -1) { - continue; - } else { - print_symbol_table(node->table, - file_ptr); - } - } + + if (getAdInfoType(entry) == TYPE_UNDEFINED) { + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " undefined", "undefined entry"); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " undefined", "undefined entry"); + } + } + } + + if (getChildren(table) != NULL) { + ListOfTable *node = getChildren(table); + for (; node != NULL; node = node->next) { + if ((node->table) == NULL) { + print_symbol_table(node->table, file_ptr); + } else { + if ((node->table)->Line_Number == -1) { + continue; + } else { + print_symbol_table(node->table, file_ptr); } + } } - if (getParent(table) == NULL) { - fprintf(file_ptr, - "-------------------------:--------:--------:----------" - "----------------:------------------------------\n"); - } + } + + if (table->Parent_Scope == NULL) { + printline(file_ptr); + } } // get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { - if (table == NULL) { - printdebug("passing a NULL reference to getAncestor. Invalid."); - return NULL; - } - if (table->Parent_Scope == NULL) { - // if table has no parent, return itself - printdebug("already at top scope!"); - if (table == cur) { - printdebug("passed in the current scope"); - } else { - printdebug("passed in a different scope"); - } - return table; + if (table == NULL) { + printdebug("passing a NULL reference to getAncestor. Invalid."); + return NULL; + } + if (table->Parent_Scope == NULL) { + // if table has no parent, return itself + printdebug("already at top scope!"); + if (table == cur) { + printdebug("passed in the current scope"); } else { - // call function recursively to grab ancestor - return getAncestor(table->Parent_Scope); + printdebug("passed in a different scope"); } + return table; + } else { + // call function recursively to grab ancestor + return getAncestor(table->Parent_Scope); + } } SymbolTable *removeEntry(SymbolTable *scope, char *search) { - - if (scope == NULL) { - return NULL; - } - if (scope->entries == NULL) { - return scope; - } - - TableNode *prev = NULL; - TableNode *now = scope->entries; - - while (now != NULL) { - if (strcmp(getName(now), search) == 0) { - if (prev == NULL) { - scope->entries = getNextEntry(now); - return scope; - } else { - prev->next = now->next; - return scope; - } - } - prev = now; - now = now->next; - } + if (scope == NULL) { + return NULL; + } + if (scope->entries == NULL) { return scope; + } + + TableNode *prev = NULL; + TableNode *now = scope->entries; + + while (now != NULL) { + if (strcmp(getName(now), search) == 0) { + if (prev == NULL) { + scope->entries = getNextEntry(now); + return scope; + } else { + prev->next = now->next; + return scope; + } + } + prev = now; + now = now->next; + } + return scope; } // 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) { - printdebug("first type is NULL in type check. Invalid."); - return false; - } - if (entry1 == undefined) { - printdebug("first type is undefined in type check. Invalid."); - return false; - } - if (entry2 == NULL) { - printdebug("second type is NULL in type check. Invalid."); - return false; - } - if (entry2 == undefined) { - printdebug("second type is undefined in type check. Invalid."); - 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; - } + TableNode *entry1 = look_up(cur, firstID); + TableNode *entry2 = look_up(cur, secondID); + if (entry1 == NULL) { + printdebug("first type is NULL in type check. Invalid."); return false; + } + if (entry1 == undefined) { + printdebug("first type is undefined in type check. Invalid."); + return false; + } + if (entry2 == NULL) { + printdebug("second type is NULL in type check. Invalid."); + return false; + } + if (entry2 == undefined) { + printdebug("second type is undefined in type check. Invalid."); + 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) { - if (st == NULL) { - printdebug("passed a NULL symbol table to getParent function. " - "Invalid."); - return NULL; - } - if (st->Parent_Scope == NULL) { - printdebug("passed a top level scope to getParent function. " - "Invalid."); - return st; - } - return st->Parent_Scope; + if (st == NULL) { + printdebug( + "passed a NULL symbol table to getParent function. " + "Invalid."); + return NULL; + } + if (st->Parent_Scope == NULL) { + printdebug( + "passed a top level scope to getParent function. " + "Invalid."); + return st; + } + return st->Parent_Scope; } ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; } @@ -1128,111 +1133,117 @@ TableNode *getFirstEntry(SymbolTable *st) { return st->entries; } // Segfaults when passed an invalid table node! TableNode *getNextEntry(TableNode *tn) { - if (tn == NULL) { - printdebug("passed a NULL table node to getNextEntry"); - return undefined; - } - if (tn == undefined) { - printdebug("passed an undefined table node to getNextEntry"); - return undefined; - } - return tn->next; + if (tn == NULL) { + printdebug("passed a NULL table node to getNextEntry"); + return undefined; + } + if (tn == undefined) { + printdebug("passed an undefined table node to getNextEntry"); + return undefined; + } + return tn->next; } // Prints all info about a table node // Uses pointers to the table node to print the info TableNode *printTableNode(TableNode *tn) { - if (DEBUG == 0) { - return tn; - } - - if (tn == NULL) { - printdebug("%s[PrintTN] Passed a NULL tablenode!", COLOR_RED); - return undefined; - } - if (tn == undefined) { - printdebug("%s[PrintTN] Passed an undefined tablenode!", - COLOR_RED); - return undefined; - } - if (tn->theName == NULL) { - printdebug("%s[PrintTN] Passed a tablenode with NULL name!", - COLOR_RED); - return undefined; - } - if (tn->theType == NULL) { - printdebug("%s[PrintTN] Passed a tablenode with NULL type!", - COLOR_RED); - return undefined; - } - if (tn->additionalinfo == NULL) { - printdebug( - "%s[PrintTN] Passed a tablenode with NULL additional info!", - COLOR_RED); - return undefined; - } - - printdebug("%s[PrintTN] Printing tablenode...", COLOR_ORANGE); - printdebug(" %sName: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, - tn->theName); - printdebug(" %sType: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, - tn->theType->theName); - printdebug(" %sTag: %s%d", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->tag); - - if (tn->tag == TYPE_RECORD_TYPE || tn->tag == TYPE_RECORD) { - printdebug(" %sAdditional Info: %sRecAdInfo", COLOR_YELLOW, - COLOR_LIGHTBLUE); - printdebug(" %snumberOfElements: %s%d", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->RecAdInfo->numofelements); - if (tn->additionalinfo->RecAdInfo->recordScope == NULL) { - printdebug(" %srecordScope (line): %s(NULL)", - COLOR_YELLOW, COLOR_LIGHTBLUE); - } else { - printdebug(" %srecordScope (line): %s%d", - COLOR_YELLOW, COLOR_LIGHTBLUE, - tn->additionalinfo->RecAdInfo->recordScope - ->Line_Number); - } - } else if (tn->tag == TYPE_ARRAY_TYPE || tn->tag == TYPE_ARRAY) { - printdebug(" %sAdditional Info: %sArrayAdInfo", COLOR_YELLOW, - COLOR_LIGHTBLUE); - printdebug(" %snumberOfDimensions: %s%d", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->ArrayAdInfo->numofdimensions); - printdebug( - " %stypeOfArray: %s%s", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->ArrayAdInfo->typeofarray->theName); - } else if (tn->tag == TYPE_FUNCTION_TYPE) { - printdebug(" %sAdditional Info: %sFunTypeAdInfo", - COLOR_YELLOW, COLOR_LIGHTBLUE); - printdebug( - " %sparameter: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, - tn->additionalinfo->FunTypeAdInfo->parameter->theName); - printdebug( - " %sreturntype: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, - tn->additionalinfo->FunTypeAdInfo->returntype->theName); - } else if (tn->tag == TYPE_FUNCTION_DECLARATION) { - printdebug(" %sAdditional Info: %sFunDecAdInfo", - COLOR_YELLOW, COLOR_LIGHTBLUE); - printdebug(" %sstartLineNumber: %s%d", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->FunDecAdInfo->startlinenumber); - printdebug(" %sregularOrAs: %s%s", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->FunDecAdInfo->regularoras - ? "true" - : "false"); - } else if (tn->tag == TYPE_PRIMITIVE) { - printdebug(" %sAdditional Info: %sPrimAdInfo", COLOR_YELLOW, - COLOR_LIGHTBLUE); - printdebug(" %ssize: %s%d", COLOR_YELLOW, - COLOR_LIGHTBLUE, - tn->additionalinfo->PrimAdInfo->size); - } else { - printdebug(" AdInfo not handled."); - } - + if (DEBUG == 0) { return tn; + } + + if (tn == NULL) { + printdebug("%s[PrintTN] Passed a NULL tablenode!", COLOR_RED); + return undefined; + } + if (tn == undefined) { + printdebug("%s[PrintTN] Passed an undefined tablenode!", + COLOR_RED); + return undefined; + } + if (tn->theName == NULL) { + printdebug("%s[PrintTN] Passed a tablenode with NULL name!", + COLOR_RED); + return undefined; + } + if (tn->theType == NULL) { + printdebug("%s[PrintTN] Passed a tablenode with NULL type!", + COLOR_RED); + return undefined; + } + if (tn->additionalinfo == NULL) { + printdebug( + "%s[PrintTN] Passed a tablenode with NULL additional info!", + COLOR_RED); + return undefined; + } + + printdebug("%s[PrintTN] Printing tablenode...", COLOR_ORANGE); + printdebug(" %sName: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->theName); + printdebug(" %sType: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->theType->theName); + printdebug(" %sTag: %s%d", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->tag); + + if (tn->next == NULL) { + printdebug(" %sNext: %sNULL", COLOR_YELLOW, COLOR_LIGHTBLUE); + } else { + printdebug(" %sNext: %s%s (tn)", COLOR_YELLOW, COLOR_LIGHTBLUE, tn->next->theName); + } + + if (tn->tag == TYPE_RECORD_TYPE || tn->tag == TYPE_RECORD) { + printdebug(" %sAdditional Info: %sRecAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %snumberOfElements: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->RecAdInfo->numofelements); + if (tn->additionalinfo->RecAdInfo->recordScope == NULL) { + printdebug(" %srecordScope (line): %s(NULL)", + COLOR_YELLOW, COLOR_LIGHTBLUE); + } else { + printdebug(" %srecordScope (line): %s%d", + COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->RecAdInfo->recordScope + ->Line_Number); + } + } else if (tn->tag == TYPE_ARRAY_TYPE || tn->tag == TYPE_ARRAY) { + printdebug(" %sAdditional Info: %sArrayAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %snumberOfDimensions: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->ArrayAdInfo->numofdimensions); + printdebug( + " %stypeOfArray: %s%s", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->ArrayAdInfo->typeofarray->theName); + } else if (tn->tag == TYPE_FUNCTION_TYPE) { + printdebug(" %sAdditional Info: %sFunTypeAdInfo", + COLOR_YELLOW, COLOR_LIGHTBLUE); + printdebug( + " %sparameter: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->FunTypeAdInfo->parameter->theName); + printdebug( + " %sreturntype: %s%s", COLOR_YELLOW, COLOR_LIGHTBLUE, + tn->additionalinfo->FunTypeAdInfo->returntype->theName); + } else if (tn->tag == TYPE_FUNCTION_DECLARATION) { + printdebug(" %sAdditional Info: %sFunDecAdInfo", + COLOR_YELLOW, COLOR_LIGHTBLUE); + printdebug(" %sstartLineNumber: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->FunDecAdInfo->startlinenumber); + printdebug(" %sregularOrAs: %s%s", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->FunDecAdInfo->regularoras + ? "true" + : "false"); + } else if (tn->tag == TYPE_PRIMITIVE) { + printdebug(" %sAdditional Info: %sPrimAdInfo", COLOR_YELLOW, + COLOR_LIGHTBLUE); + printdebug(" %ssize: %s%d", COLOR_YELLOW, + COLOR_LIGHTBLUE, + tn->additionalinfo->PrimAdInfo->size); + } else { + printdebug(" AdInfo not handled."); + } + + return tn; } \ No newline at end of file From a4dc3d90be97b38e78677ee3ae9394c1e38e10ab Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 8 Apr 2025 16:01:36 -0400 Subject: [PATCH 16/45] update --- src/grammar.y | 67 +++++++++++++------ src/symbol_table.c | 8 ++- tests/sprint2/test/sp2_presidence.alpha | 6 +- .../sprint3/test/sp3_and_or_type_check.alpha | 3 + .../test/sp3_primitive_type_check.alpha | 23 +++++++ 5 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 tests/sprint3/test/sp3_primitive_type_check.alpha diff --git a/src/grammar.y b/src/grammar.y index 946c9ad..e70363d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -326,14 +326,14 @@ sblock: dblock: L_BRACKET - {if(getLine(cur)==0){ - setLineNumber(cur, @1.first_line); - setColumnNumber(cur,@1.first_line);} - else{ - cur = CreateScope(cur,@1.first_line,@1.first_column); + { + if (getLine(cur) == 0) { + setLineNumber(cur, @1.first_line); + setColumnNumber(cur,@1.first_line); + } else{ + cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? } - } - + } declaration_list R_BRACKET; @@ -391,22 +391,45 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp(getName((TableNode*)$1), getName((TableNode*)$3)) == 0) { - printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp(getType((TableNode*)$1), "array") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - } else if((strcmp(getType((TableNode*)$1), "record") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - } else if((strcmp(getType((TableNode*)$1), "function type primitive") == 0) && (strcmp(getName((TableNode*)$3), "address") == 0)) { - printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName((TableNode*)$1), getName((TableNode*)$3)); - // } else if () { + bool passCheck = false; + TableNode * left = (TableNode*)$1; + TableNode * right = (TableNode*)$3; + + printTableNode((TableNode*)$1); + printTableNode((TableNode*)$3); - // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { - // printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3); - } else { + if (strcmp(getType(right), "primitive") == 0) { + if (strcmp((getType(left)),(getName(right))) == 0) { + printdebug("%s[☺] Passed primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + } + + if(strcmp(getName(left), getName(right)) == 0) { + printdebug("Passed standard type check; assignable = expression"); + passCheck = true; + } + + if((strcmp(getType(left), "array") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + if((strcmp(getType(left), "record") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + if((strcmp(getType(left), "function type primitive") == 0) && (strcmp(getName(right), "address") == 0)) { + printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName(left), getName(right)); + passCheck = true; + } + + // Type check fails: + if (!passCheck) { printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); - printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType((TableNode*)$1), getType((TableNode*)$3), COLOR_WHITE); - printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType((TableNode*)$1)); + printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType(left), getType(right), COLOR_WHITE); + printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(left)); } } @@ -624,6 +647,8 @@ assignable: //char *funtype = getType(look_up(cur, $1)); printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); + + TableNode * typeNode = table_lookup(getAncestor(cur), getType((TableNode*)$1)); TableNode *param = getParameter(typeNode); printTableNode(param); diff --git a/src/symbol_table.c b/src/symbol_table.c index 86fca5b..debc4d5 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -913,7 +913,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-*s:%-*s:%-*s:%-*s:%-*s:\n", - col_widths[0], " NAME", + col_widths[0], "NAME", col_widths[1], " SCOPE", col_widths[2], " PARENT", col_widths[3], " TYPE", @@ -965,7 +965,11 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (parentScopeNum == 0) { st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Primitive", primAdInfo); + printdebug("%sTHIS ONE", COLOR_RED); + printTableNode(entry); + char *primType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(primType, " %s", getType(entry)); + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, primType, primAdInfo); } } diff --git a/tests/sprint2/test/sp2_presidence.alpha b/tests/sprint2/test/sp2_presidence.alpha index 2d60209..d60611d 100644 --- a/tests/sprint2/test/sp2_presidence.alpha +++ b/tests/sprint2/test/sp2_presidence.alpha @@ -1,8 +1,10 @@ type main: string -> integer +type rec: [integer: rec_x; integer: rec_y] + function entry: main entry (arg) := { - [integer:x] - x := 3 + 2 * 8; + [integer: arg_x; integer: arg_y; rec: arg_record; Boolean: arg_bool] + arg_x := 3 + 2 * 8; return 0; } diff --git a/tests/sprint3/test/sp3_and_or_type_check.alpha b/tests/sprint3/test/sp3_and_or_type_check.alpha index 450aee0..c01fc10 100644 --- a/tests/sprint3/test/sp3_and_or_type_check.alpha +++ b/tests/sprint3/test/sp3_and_or_type_check.alpha @@ -31,6 +31,9 @@ test (arg) := { b := b & 1; b := 1 & b; b := 1 = 1; + + + b := 1 = b; return 0; diff --git a/tests/sprint3/test/sp3_primitive_type_check.alpha b/tests/sprint3/test/sp3_primitive_type_check.alpha new file mode 100644 index 0000000..6980aba --- /dev/null +++ b/tests/sprint3/test/sp3_primitive_type_check.alpha @@ -0,0 +1,23 @@ +(* + Testing the following type checks: + - integer : primitive + - character : primitive + - boolean : primitive + + - address (not included, special case) +*) + + + +type main: string -> integer +function entry: main + +entry (arg) := { + [integer: i; address: add; character: char; Boolean: bool] + + i := 3 + 2 * 8; + char := 'a'; + bool := true; + + return 0; +} From faf592a725d8881730f9d3fdb41862f646373913 Mon Sep 17 00:00:00 2001 From: Partho Date: Tue, 8 Apr 2025 18:12:11 -0400 Subject: [PATCH 17/45] Added most type checking. Entries seem printing mostly OK in table. --- src/grammar.y | 155 +++++++++++++++++++++++++++++++++------------ src/symbol_table.c | 13 +++- 2 files changed, 127 insertions(+), 41 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index e70363d..fa52d73 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -127,15 +127,17 @@ definition: printdebug("Currently see a record definition for %s", $2); tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); - if (look_up(cur, $2) == undefined) { - printdebug("rec not found"); - } + printdebug("Created a new scope"); + //if (look_up(cur, $2) == undefined) { + // printdebug("rec not found"); + //} } dblock { //We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize //and then putting it in the entry that we created above. setRecSize(look_up(getParent(cur), $2), getRecSize(cur)); + printdebug("Moving up a scope after seeing a record definition"); cur = getParent(cur); } @@ -158,14 +160,15 @@ definition: { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + printdebug("[TYPE CHECK] function not declared at line %d, column %d", @1.first_line, @1.first_column); } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { - printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + printdebug("[TYPE CHECK] function not declared at line %d, column %d", @1.first_line, @1.first_column); } else { setStartLine(node, @1.first_line); setAsKeyword(node, false); } cur = CreateScope(cur, 0, 0); + printdebug("Created a new scope"); } L_PAREN ID { @@ -178,7 +181,7 @@ definition: || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("type of parameter is undefined or invalid at line %d, column %d", @4.first_line, @4.first_column); + printdebug("[TYPE CHECK] type of parameter is undefined or invalid at line %d, column %d", @4.first_line, @4.first_column); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases }else{ printdebug("type of parameter is %s at line %d, column %d", getName(type_of_param), @4.first_line, @4.first_column); @@ -192,9 +195,9 @@ definition: TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { - printdebug(" undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); + printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ - printdebug("not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); + printdebug("[TYPE CHECK] not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); } else { printdebug("setting as keyword to true"); @@ -202,11 +205,12 @@ definition: setAsKeyword(node, true); } cur = CreateScope(cur, 0, 0); + printdebug("Created a new scope"); }AS L_PAREN { TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { - printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); + printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); }else if(getAdInfoType(parameter) != TYPE_RECORD){ printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime)); printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); @@ -220,7 +224,7 @@ definition: || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); + printdebug("[TYPE CHECK] type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases }else{ printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); @@ -241,13 +245,27 @@ definition: function_declaration: FUNCTION ID COLON ID - { + { + if(getAdInfoType(look_up(cur, $4))==TYPE_FUNCTION_TYPE){ CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); } + else{ + printdebug("[TYPE CHECK] function declaration of %s is not a valid function type at line %d, column %d", $2, @1.first_line, @1.first_column); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + + } + } | EXTERNAL FUNCTION ID COLON ID - { - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, NULL); + { + if(getAdInfoType(look_up(cur, $5))==TYPE_FUNCTION_TYPE){ + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); + } + else{ + printdebug("[TYPE CHECK] function declaration of %s is not a valid function type at line %d, column %d", $3, @1.first_line, @1.first_column); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); + + } } ; @@ -290,6 +308,7 @@ sblock: { if (getLine(cur) != 0) { cur = CreateScope(cur,@1.first_line,@1.first_column); + printdebug("Created a new scope"); } else { setLineNumber(cur, @1.first_line); setColumnNumber(cur,@1.first_line); @@ -297,6 +316,7 @@ sblock: } statement_list { + printdebug("Moving up a scope after seeing sblock"); cur = getParent(cur); } R_BRACE @@ -305,9 +325,11 @@ sblock: { if (getLine(cur) != 0) { cur = CreateScope(cur,@1.first_line,@1.first_column); + printdebug("Created a new scope when seeing an L brace"); } else { setLineNumber(cur, @1.first_line); setColumnNumber(cur,@1.first_line); + printdebug("Did not create a new scope when saw L Brace, set line number to %d", @1.first_line); } } dblock @@ -316,6 +338,7 @@ sblock: } statement_list { + printdebug("Moving up a scope after seeing sblock with dblock"); cur = getParent(cur); } R_BRACE @@ -330,8 +353,10 @@ dblock: if (getLine(cur) == 0) { setLineNumber(cur, @1.first_line); setColumnNumber(cur,@1.first_line); + printdebug("Did not create a new scope when saw dblock, set line number to %d instead", @1.first_line); } else{ cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? + printdebug("Created a new scope when seeing a dblock"); } } declaration_list R_BRACKET; @@ -349,7 +374,33 @@ declaration: id_or_types COLON ID { printdebug("ID/TYPE: %s, ID: %s", getName((TableNode*)$1), $3) ; - CreateEntry(cur,getAdInfoType((TableNode*)$1),(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + int d = getAdInfoType((TableNode*)$1); + if(d == TYPE_UNDEFINED) { + printdebug("undefined type at line %d and column %d", @2.first_line, @2.first_column); + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + } + else if(d == TYPE_FUNCTION_TYPE) { + printdebug("invalid (function) type passed in declaration list in dblock", @2.first_line, @2.first_column); + d = TYPE_FUNCTION_TYPE; + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + } + else if(d == TYPE_ARRAY_TYPE){ + printdebug("array variable at line %d and column %d", @2.first_line, @2.first_column); + d = TYPE_ARRAY; + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + } + else if(d == TYPE_RECORD_TYPE){ + printdebug("record variable at line %d and column %d", @2.first_line, @2.first_column); + d = TYPE_RECORD; + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + } + else if(d == TYPE_PRIMITIVE){ + printdebug("primitive variable at line %d and column %d", @2.first_line, @2.first_column); + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + }else { + printdebug("other invalid type passed at %d and column %d", @2.first_line, @2.first_column); + CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); + } } ; @@ -427,7 +478,7 @@ simple_statement: // Type check fails: if (!passCheck) { - printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); + printdebug("%s[TYPE CHECK] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType(left), getType(right), COLOR_WHITE); printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(left)); } @@ -495,7 +546,7 @@ expression: $$=(TableNode*)$2; } else { $$=undefined; - printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName((TableNode*)$2)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName((TableNode*)$2)); } } @@ -505,7 +556,7 @@ expression: if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -516,7 +567,7 @@ expression: if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -527,7 +578,7 @@ expression: if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) { $$=(TableNode*)$1; } else{ - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3)); $$=undefined; } } @@ -538,7 +589,7 @@ expression: if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && ((TableNode*)$1 == integ)) { $$=(TableNode*)$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -549,7 +600,7 @@ expression: if($1 == $3 && $1 == integ) { $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -560,7 +611,7 @@ expression: if($1 == $3 && $1 == boo){ $$=$1; } else{ - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -571,7 +622,7 @@ expression: if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && $1 == boo) { $$=$1; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -582,7 +633,7 @@ expression: if($1 == $3 && $1==integ) { $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$=undefined; } } @@ -593,7 +644,7 @@ expression: if($1 == $3 && $1 != undefined) { $$=boo; } else { - printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); + printdebug("[TYPE CHECK] mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName((TableNode*)$1),getName((TableNode*)$3)); $$ = undefined; } } @@ -601,7 +652,25 @@ expression: | assignable { printdebug("assignable expression. current type is %s",getName((TableNode*)$1)); - $$= getTypeEntry((TableNode*)$1); + if(getAdInfoType((TableNode*)$1) == TYPE_PRIMITIVE|| + getAdInfoType((TableNode*)$1) == TYPE_ARRAY || + getAdInfoType((TableNode*)$1) == TYPE_RECORD){ + printdebug("assignable passing up to expression is primitive, array instance, or record instance. Passing up its type"); + $$= getTypeEntry((TableNode*)$1); + } + + else if(getAdInfoType((TableNode*)$1) == TYPE_ARRAY_TYPE|| + getAdInfoType((TableNode*)$1) == TYPE_RECORD_TYPE|| + getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_TYPE|| + getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_DECLARATION){ + printdebug("assignable passing up to expression is array type, record type, function type, or function declaration"); + $$= ((TableNode*)$1); + } + else { + printdebug("[TYPE CHECK] assignable passing up an invalid type to expression"); + $$= ((TableNode*)$1); + } + } | L_PAREN expression R_PAREN @@ -612,7 +681,14 @@ expression: | memOp assignable { - $$ = addr; + int d = getAdInfoType((TableNode*)$2); + if(d == TYPE_ARRAY_TYPE || d == TYPE_ARRAY || d == TYPE_RECORD_TYPE || d == TYPE_RECORD) { + //printdebug("[TYPE CHECK] valid memOp expression"); + $$ = addr; + } else { + printdebug("[TYPE CHECK] invalid memOp expression at line %d and column %d.", @2.first_line,@2.first_column,getName((TableNode*)$2)); + $$=undefined; + } } ; @@ -626,7 +702,7 @@ assignable: ID { $$ = look_up(cur,$1); - printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); + printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getName((TableNode*)$$), $1); } | assignable @@ -686,7 +762,7 @@ assignable: } } $$ = getReturn((table_lookup(getAncestor(cur), getType((TableNode*)$1)))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName((TableNode*)$1)); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName((TableNode*)$$), getName((TableNode*)$1)); } else if (type == TYPE_ARRAY_TYPE) { printdebug("%sEntering array call", COLOR_LIGHTGREEN); @@ -694,7 +770,7 @@ assignable: printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName((TableNode*)$1))), $2, @2.first_line, @2.first_column); } $$ = getArrType(look_up(getParent(cur), getName((TableNode*)$1))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName((TableNode*)$1)); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName((TableNode*)$$), getName((TableNode*)$1)); } cur = getParent(cur); } @@ -702,6 +778,7 @@ assignable: | assignable rec_op ID { if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3)) { + $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3); } printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName((TableNode*)($$)), $1); @@ -729,7 +806,7 @@ constant: C_STRING { $$ = $1; - printdebug("string of C_STRING in constant is %s",$1); + printdebug("string of C_STRING in constant is %s",getName((TableNode*)$1)); } | C_INTEGER @@ -741,25 +818,25 @@ constant: | C_NULL { $$ = $1; - printdebug("string of C_NULL in constant is %s",$1); + printdebug("string of C_NULL in constant is %s",getName((TableNode*)$1)); } | C_CHARACTER { $$ = $1; - printdebug("string of C_CHARACTER in constant is %s",$1); + printdebug("string of C_CHARACTER in constant is %s",getName((TableNode*)$1)); } | C_TRUE { $$ = $1; - printdebug("string of C_TRUE in constant is %s",$1); + printdebug("string of C_TRUE in constant is %s",getName((TableNode*)$1)); } | C_FALSE { $$ = $1; - printdebug("string of C_FALSE in constant is %s",$1); + printdebug("string of C_FALSE in constant is %s",getName((TableNode*)$1)); } ; @@ -770,25 +847,25 @@ types: T_INTEGER { $$ = $1; - printdebug("string of T_INTEGER in types is %s",$1); + printdebug("string of T_INTEGER in types is %s",getName((TableNode*)$1)); } | T_ADDRESS { $$ = $1; - printdebug("string of T_ADDRESS in types is %s",$1); + printdebug("string of T_ADDRESS in types is %s",getName((TableNode*)$1)); } | T_CHARACTER { $$ = $1; - printdebug("string of T_CHARACTER in types is %s",$1); + printdebug("string of T_CHARACTER in types is %s",getName((TableNode*)$1)); } | T_BOOLEAN { $$ = $1; - printdebug("string of T_BOOLEAN in types is %s",$1); + printdebug("string of T_BOOLEAN in types is %s",getName((TableNode*)$1)); } ; diff --git a/src/symbol_table.c b/src/symbol_table.c index debc4d5..a4c909b 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -948,14 +948,23 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, arrayType, " Type of Array"); } } + if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { + char *recordAdInfo = (char *)malloc(100); + sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements); + if (parentScopeNum == 0) { + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record type", recordAdInfo); + } else { + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record type", recordAdInfo); + } + } if (getAdInfoType(entry) == TYPE_RECORD) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record", recordAdInfo); + st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record instance", recordAdInfo); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record", recordAdInfo); + st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record instance", recordAdInfo); } } From 3010ad651730e0a99d78df69c8f4f44631e823ca Mon Sep 17 00:00:00 2001 From: Partho Date: Tue, 8 Apr 2025 18:16:56 -0400 Subject: [PATCH 18/45] Added more debug statements --- src/symbol_table.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index a4c909b..c76043f 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -854,13 +854,14 @@ TableNode *table_lookup(SymbolTable *table, char *x) { return entrie; } } + printdebug("Could not find %s in scope using table_lookup", x); return undefined; } // check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { - printdebug("passed in empty scope. error."); + printdebug("Could not find %s in any scope using lookup",x); return undefined; } TableNode *ret = table_lookup(table, x); From d262bd9c3916dc47325a6ba044ad54890a2a7e18 Mon Sep 17 00:00:00 2001 From: Annie Date: Wed, 9 Apr 2025 13:13:20 -0400 Subject: [PATCH 19/45] removed as from function definitions --- src/grammar.y | 94 ++++++++++++++++++++------------------------------- 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index fa52d73..e12580d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -156,41 +156,6 @@ definition: CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); } - | ID - { - TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == undefined) { - printdebug("[TYPE CHECK] function not declared at line %d, column %d", @1.first_line, @1.first_column); - } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { - printdebug("[TYPE CHECK] function not declared at line %d, column %d", @1.first_line, @1.first_column); - } else { - setStartLine(node, @1.first_line); - setAsKeyword(node, false); - } - cur = CreateScope(cur, 0, 0); - printdebug("Created a new scope"); - } - L_PAREN ID - { - printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", $1,$4); - TableNode* type_of_param = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - int type_of_param_type = getAdInfoType(type_of_param); - if( type_of_param_type == TYPE_UNDEFINED - || type_of_param_type == TYPE_FUNCTION_DECLARATION - || type_of_param_type == TYPE_ARRAY - || type_of_param_type == TYPE_ALL_ELSE - || type_of_param_type == TYPE_SYSTEM_DEFINED - || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter is undefined or invalid at line %d, column %d", @4.first_line, @4.first_column); - type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases - }else{ - printdebug("type of parameter is %s at line %d, column %d", getName(type_of_param), @4.first_line, @4.first_column); - } - - CreateEntry(cur,type_of_param_type, type_of_param, $4, getAdInfo(type_of_param)); - } - R_PAREN ASSIGN sblock //leaving scope is being taken care of in sblock - | ID { TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { @@ -206,42 +171,55 @@ definition: } cur = CreateScope(cur, 0, 0); printdebug("Created a new scope"); - }AS L_PAREN { + } L_PAREN { TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); }else if(getAdInfoType(parameter) != TYPE_RECORD){ - printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime)); - printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); - - }else { - for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ - int type_of_param_type = getAdInfoType(entry); - if( type_of_param_type == TYPE_UNDEFINED + int type_of_param_type = getAdInfoType(parameter); + if( type_of_param_type == TYPE_UNDEFINED || type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_ARRAY || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); - type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases - }else{ - printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); - } - if(type_of_param_type == TYPE_UNDEFINED){ - CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); - } else { - CreateEntry(cur,type_of_param_type, entry, NULL, getAdInfo(entry)); - /*printdebug("creating entry of type %s for function", getType(entry)); - CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ + printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter)); + type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + } + if(type_of_param_type == TYPE_UNDEFINED){ + CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + } else { + CreateEntry(cur, getAdInfoType(parameter), parameter,NULL, getAdInfo(parameter)); + } + } else { + for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ + int type_of_param_type = getAdInfoType(entry); + if( type_of_param_type == TYPE_UNDEFINED + || type_of_param_type == TYPE_FUNCTION_DECLARATION + || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_ALL_ELSE + || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused + printdebug("[TYPE CHECK] type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); + type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + }else{ + printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); + } + if(type_of_param_type == TYPE_UNDEFINED){ + CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + } else { + CreateEntry(cur,type_of_param_type, entry, NULL, getAdInfo(entry)); + /*printdebug("creating entry of type %s for function", getType(entry)); + CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ + } } - } } - } idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", - $1,$6);} R_PAREN ASSIGN sblock //check sblock type - ; + } idlist { + printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$5); + } R_PAREN ASSIGN sblock //check sblock type + ; function_declaration: FUNCTION ID COLON ID From d17e99758f5e36774408824dc3c2ab2225f0b027 Mon Sep 17 00:00:00 2001 From: Annie Date: Wed, 9 Apr 2025 13:29:50 -0400 Subject: [PATCH 20/45] added offsets to record type structure --- src/symbol_table.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/symbol_table.h b/src/symbol_table.h index 7c5322d..e2fac84 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -24,6 +24,8 @@ typedef struct { typedef struct { int numofelements; struct SymbolTable *recordScope; + int totalsize; + int offsets[]; } record_info; typedef struct { @@ -164,4 +166,4 @@ extern char *COLOR_YELLOW; extern char *COLOR_LIGHTBLUE; extern char *COLOR_LIGHTPURPLE; extern char *COLOR_LIGHTCYAN; -extern char *COLOR_WHITE; \ No newline at end of file +extern char *COLOR_WHITE; From 859ff3fd032db8774c5d4a1e26d9c080f8ac3113 Mon Sep 17 00:00:00 2001 From: Partho Date: Wed, 9 Apr 2025 15:15:17 -0400 Subject: [PATCH 21/45] added offset code in symbol table for records --- src/grammar.y | 2 +- src/symbol_table.c | 163 +++++++++++++++++++++++++++++++++++++++++++++ src/symbol_table.h | 4 +- 3 files changed, 166 insertions(+), 3 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index e12580d..4b4d14c 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -680,7 +680,7 @@ assignable: ID { $$ = look_up(cur,$1); - printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getName((TableNode*)$$), $1); + printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getTypeEntry((TableNode*)$$), $1); } | assignable diff --git a/src/symbol_table.c b/src/symbol_table.c index c76043f..cd04f8a 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -135,6 +135,169 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { // This gets the number of elements that make up a record. // Perhaps this may not be needed since we need to iterate over all elements // anyways. + +int getRecTotal(TableNode* node){ + if(node == NULL){ + printdebug( + "passed a NULL node to getRecTotal. Invalid."); + return -1; + } + if(getAdInfoType(node) != TYPE_RECORD_TYPE){ + printdebug( + "passed an invalid node to getRecTotal. Invalid."); + return -1; + } + if(node->additionalinfo == NULL){ + printdebug( + "node has NULL additionalinfo. Invalid."); + return -1; + } + return node->additionalinfo->RecAdInfo->total_size; +} +TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { + if (node == NULL) { + printdebug( + "passed a NULL node to setRecOffsetInfo. Invalid."); + return undefined; + } + if (scope == NULL) { + printdebug( + "passed an NULL scope to setRecOffsetInfo. Invalid."); + return undefined; + } + if(getFirstEntry(scope) == NULL){ + printdebug( + "passed an empty scope to setRecOffsetInfo. Invalid."); + return undefined; + } + TableNode* this = getFirstEntry(scope); + int largest = 0; + int k = getRecLength(node); + int total_size = 0; + int counter = 0; + int *offsets = (int *)calloc(2 * k, sizeof(int)); + if(getAdInfoType(this) == TYPE_FUNCTION_TYPE){ + offsets[counter] = 8; + total_size = total_size + offsets[counter]; + largest = 8; + counter++; + } + else if(getAdInfoType(this) == TYPE_RECORD_TYPE){ + offsets[counter] = getRecTotal(this); + total_size = total_size + offsets[counter]; + largest = offsets[counter]; + counter++; + } + else if(getAdInfoType(this)==TYPE_PRIMITIVE){ + offsets[counter] = getPrimSize(this); + total_size = total_size + offsets[counter]; + largest = offsets[counter]; + counter++; + } + else if(getAdInfoType(this)==TYPE_ARRAY_TYPE){ + offsets[counter] = 8; + total_size = total_size + offsets[counter]; + largest = offsets[counter]; + counter++; + } + else { + printdebug( + "[TYPE CHECK] passed an invalid (first) parameter to a function definition."); + return undefined; + } + this = getNextEntry(this); + while(this != NULL){ + if(getAdInfoType(this) == TYPE_FUNCTION_TYPE){ + int s = 8; + if (s > largest) { + largest = s; + } + //make sure current location is aligned properly + offsets[counter] = (total_size % s); + total_size = total_size + offsets[counter]; + counter++; + //add in the size of the entry and increment + offsets[counter] = s; + total_size = total_size + offsets[counter]; + counter++; + this = getNextEntry(this); + } + else if(getAdInfoType(this) == TYPE_ARRAY_TYPE){ + int s = 8; + if (s > largest) { + largest = s; + } + //make sure current location is aligned properly + offsets[counter] = (total_size % s); + total_size = total_size + offsets[counter]; + counter++; + //add in the size of the entry and increment + offsets[counter] = s; + total_size = total_size + offsets[counter]; + counter++; + this = getNextEntry(this); + } + else if(getAdInfoType(this) == TYPE_RECORD_TYPE){ + int s = getRecTotal(this); + if (s > largest) { + largest = s; + } + //make sure current location is aligned properly + offsets[counter] = (total_size % s); + total_size = total_size + offsets[counter]; + counter++; + //add in the size of the entry and increment + offsets[counter] = s; + total_size = total_size + offsets[counter]; + counter++; + this = getNextEntry(this); + } + else if(getAdInfoType(this) == TYPE_PRIMITIVE){ + int s = getPrimSize(this); + if (s > largest) { + largest = s; + } + //make sure current location is aligned properly + offsets[counter] = (total_size % s); + total_size = total_size + offsets[counter]; + counter++; + //add in the size of the entry and increment + offsets[counter] = s; + total_size = total_size + offsets[counter]; + counter++; + this = getNextEntry(this); + }else{ + printdebug( + "[TYPE CHECK] passed an invalid parameter at position %d in record.",((counter+1)/2)); + return undefined; + } + } + //make sure that size of whole structure is aligned with largest element in struct: + offsets[counter] = (total_size % largest); + total_size = total_size + offsets[counter]; + node->additionalinfo->RecAdInfo->offsets = offsets; + node->additionalinfo->RecAdInfo->total_size = total_size; + return node; +} + +int* getRecOffsets(TableNode* node){ + if(node == NULL){ + printdebug( + "passed a NULL node to getRecTotal. Invalid."); + return NULL; + } + if(getAdInfoType(node) != TYPE_RECORD_TYPE){ + printdebug( + "passed an invalid node to getRecTotal. Invalid."); + return NULL; + } + if(node->additionalinfo == NULL){ + printdebug( + "node has NULL additionalinfo. Invalid."); + return NULL; + } + return node->additionalinfo->RecAdInfo->offsets; +} int getRecLength(TableNode *definition) { if (definition == NULL) { printdebug( diff --git a/src/symbol_table.h b/src/symbol_table.h index e2fac84..af2d827 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -24,8 +24,8 @@ typedef struct { typedef struct { int numofelements; struct SymbolTable *recordScope; - int totalsize; - int offsets[]; + int total_size; + int* offsets; } record_info; typedef struct { From d7d7d22c72719512544dd737027fbe26e0b72165 Mon Sep 17 00:00:00 2001 From: Annie Date: Fri, 11 Apr 2025 10:08:23 -0400 Subject: [PATCH 22/45] fixed seg fault for non record function calls --- src/grammar.y | 51 +++++++++++++--------- src/symbol_table.c | 4 +- tests/sprint2/test/sp2_carls_mistake.alpha | 6 +-- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 4b4d14c..029d545 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -707,31 +707,40 @@ assignable: TableNode *param = getParameter(typeNode); printTableNode(param); - SymbolTable *recList = getRecList(param); - TableNode *lastCheckedRef = getFirstEntry(recList); - TableNode *lastCheckedAct = getFirstEntry(cur); - while (getNextEntry(lastCheckedRef) != NULL) { - lastCheckedRef = getNextEntry(lastCheckedRef); - } + if (getAdInfoType(param) == TYPE_RECORD) { + SymbolTable *recList = getRecList(param); + TableNode *lastCheckedRef = getFirstEntry(recList); + TableNode *lastCheckedAct = getFirstEntry(cur); + while (getNextEntry(lastCheckedRef) != NULL) { + lastCheckedRef = getNextEntry(lastCheckedRef); + } - //this isn't very efficient, but will hopefully work - while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { - printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); - printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); - } - lastCheckedAct = getNextEntry(lastCheckedAct); - TableNode *tn = getFirstEntry(recList); - - if (tn != lastCheckedRef) { - while (getNextEntry(tn) != lastCheckedRef) { - tn = getNextEntry(tn); + //this isn't very efficient, but will hopefully work + while (lastCheckedAct != NULL && lastCheckedRef != NULL) { + if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { + printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); + printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); } - lastCheckedRef = tn; - } else {break;} + lastCheckedAct = getNextEntry(lastCheckedAct); + TableNode *tn = getFirstEntry(recList); + + if (tn != lastCheckedRef) { + while (getNextEntry(tn) != lastCheckedRef) { + tn = getNextEntry(tn); + } + lastCheckedRef = tn; + } else {break;} + } + } else { + if (strcmp(getName(param), getName(getFirstEntry(cur))) != 0) { + printdebug("expected %s expression in function call but got %s", getName(param), getName(getFirstEntry(cur))); + } + + if (getNextEntry(getFirstEntry(cur)) != NULL) { + printdebug("expected 1 parameter, but got multiple in function call"); + } } - } else { char *expected = getName(getParameter(look_up(getParent(cur), getName((TableNode*)$1)))); char *actual = getType(getFirstEntry(cur)); diff --git a/src/symbol_table.c b/src/symbol_table.c index cd04f8a..a187fdf 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -335,7 +335,7 @@ SymbolTable *getRecList(TableNode *definition) { if (strcmp(getType(definition), "record") != 0) { printdebug( "not checking the list of types of a record -- invalid " - "op"); + "op of type %s", getType(definition)); return NULL; } return definition->additionalinfo->RecAdInfo->recordScope; @@ -1423,4 +1423,4 @@ TableNode *printTableNode(TableNode *tn) { } return tn; -} \ No newline at end of file +} diff --git a/tests/sprint2/test/sp2_carls_mistake.alpha b/tests/sprint2/test/sp2_carls_mistake.alpha index 8d705a7..c85c310 100644 --- a/tests/sprint2/test/sp2_carls_mistake.alpha +++ b/tests/sprint2/test/sp2_carls_mistake.alpha @@ -14,11 +14,11 @@ foo (x) := { return x * x; } -bar1 (a) := { - return a.x * a.y; +bar1 (x, y) := { + return x * y; } -bar2 as (r,s) := { +bar2 (r,s) := { return r * s; } From df8c9fb6618b919e90c5f0fbd48b9e3f9a30830b Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 12:50:24 -0400 Subject: [PATCH 23/45] updated print symbol table to use getters --- src/symbol_table.c | 58 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index a187fdf..ce69570 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -105,13 +105,13 @@ TableNode *getArrType(TableNode *definition) { printdebug( "passed an NULL entry to getArrType " "function. Invalid."); - return NULL; + return undefined; } if (definition == undefined) { printdebug( "passed an undefined entry to getArrType " "function. Invalid."); - return NULL; + return undefined; } if (strcmp(getType(definition), "array") != 0) { printdebug("not checking the type of an array -- invalid op"); @@ -1103,93 +1103,93 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { for (; entry != NULL; entry = getNextEntry(entry)) { if (getAdInfoType(entry) == TYPE_ARRAY_TYPE) { char *arrayType = (char *)malloc(100); - sprintf(arrayType, " %d -> %s", entry->additionalinfo->ArrayAdInfo->numofdimensions, - entry->additionalinfo->ArrayAdInfo->typeofarray->theName); + sprintf(arrayType, " %d -> %s", getNumArrDim(entry), + getName(getArrType(entry))); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, arrayType, " Type of Array"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, arrayType, " Type of Array"); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, arrayType, " Type of Array"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Type of Array"); } } if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { char *recordAdInfo = (char *)malloc(100); - sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements); + sprintf(recordAdInfo, " elements-%d", getRecLength(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, "record type", recordAdInfo); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, "record type", recordAdInfo); } } if (getAdInfoType(entry) == TYPE_RECORD) { char *recordAdInfo = (char *)malloc(100); - sprintf(recordAdInfo, " elements-%d", entry->additionalinfo->RecAdInfo->numofelements); + sprintf(recordAdInfo, " elements-%d", getRecLength(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, "record instance", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, "record instance", recordAdInfo); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, "record instance", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, "record instance", recordAdInfo); } } if (getAdInfoType(entry) == TYPE_PRIMITIVE) { char *primAdInfo = (char *)malloc(100); - sprintf(primAdInfo, " size-%d bytes", entry->additionalinfo->PrimAdInfo->size); + sprintf(primAdInfo, " size-%d bytes", getPrimSize(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Primitive", primAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive", primAdInfo); } else { printdebug("%sTHIS ONE", COLOR_RED); printTableNode(entry); char *primType = (char *)malloc(sizeof(getType(entry) + 1)); sprintf(primType, " %s", getType(entry)); - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, primType, primAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo); } } if (getAdInfoType(entry) == TYPE_FUNCTION_TYPE) { char *functiontype = (char *)malloc(100); - sprintf(functiontype, " %s -> %s", entry->additionalinfo->FunTypeAdInfo->parameter->theName, - entry->additionalinfo->FunTypeAdInfo->returntype->theName); + sprintf(functiontype, " %s -> %s", getName(getParameter(entry)), + getName(getReturn(entry))); if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, functiontype, " Type of Function"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Type of Function"); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, functiontype, " Type of Function"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Type of Function"); } } if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " Function", " Function Declaration"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Function", " Function Declaration"); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " Function", " Function Declaration"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " Function", " Function Declaration"); } } if (getAdInfoType(entry) == TYPE_UNDEFINED) { if (parentScopeNum == 0) { - st_fprint(file_ptr, entry->theName, currentScopeNum, -100, " undefined", "undefined entry"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " undefined", "undefined entry"); } else { - st_fprint(file_ptr, entry->theName, currentScopeNum, parentScopeNum, " undefined", "undefined entry"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " undefined", "undefined entry"); } } } if (getChildren(table) != NULL) { ListOfTable *node = getChildren(table); - for (; node != NULL; node = node->next) { - if ((node->table) == NULL) { - print_symbol_table(node->table, file_ptr); + for (; node != NULL; node = getRestOfChildren(node)) { + if ((getFirstChild(node)) == NULL) { + print_symbol_table(getFirstChild(node), file_ptr); } else { - if ((node->table)->Line_Number == -1) { + if (getLine(getFirstChild(node)) == -1) { continue; } else { - print_symbol_table(node->table, file_ptr); + print_symbol_table(getFirstChild(node), file_ptr); } } } } - if (table->Parent_Scope == NULL) { + if (getParent(table) == NULL) { printline(file_ptr); } } From 8bc5997996719772f02c6fe08894b491b9c43e1d Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 12:55:14 -0400 Subject: [PATCH 24/45] fixed getReturn to not return NULL --- src/symbol_table.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index ce69570..2204ba3 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -528,13 +528,13 @@ TableNode *getReturn(TableNode *definition) { printdebug( "passed a NULL entry to getReturn " "function. Invalid."); - return NULL; + return undefined; } if (definition == undefined) { printdebug( "passed an undefined entry to getReturn " "function. Invalid."); - return NULL; + return undefined; } if (strcmp(getType(definition), "primitive function type") != 0) { printdebug( From 541a2ba44a22678b7fd7942ec8945533a406249d Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 13:08:43 -0400 Subject: [PATCH 25/45] tweaked get return --- src/symbol_table.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/symbol_table.c b/src/symbol_table.c index 2204ba3..913a32b 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -541,6 +541,11 @@ TableNode *getReturn(TableNode *definition) { "not checking the return of a function -- invalid op"); return undefined; } + if(definition->additionalinfo == NULL){ + printdebug( + "node has NULL additionalinfo. Invalid."); + return undefined; + } return definition->additionalinfo->FunTypeAdInfo->returntype; } From 1f0a2c189d0c98685d3ffb649cfff25e9ca08e52 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 11 Apr 2025 13:09:49 -0400 Subject: [PATCH 26/45] Added the debug flags to make --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0f7f411..c9797be 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ tmp/symbol_table.o: src/symbol_table.c src/symbol_table.h $(CC) $(CFLAGS) -o tmp/symbol_table.o -c src/symbol_table.c runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o - $(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/grammar.tab.c tmp/lex.yy.c + $(CC) $(CFLAGS) -o $(EXE) -g -ggdb tmp/runner.o tmp/grammar.tab.c tmp/lex.yy.c debug: CFLAGS += -DDEBUG=1 debug: clean compiler From 413a4854b4185461096318c5ba3ea1ff69fe2f32 Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 13:11:48 -0400 Subject: [PATCH 27/45] tweaked get parameter --- src/symbol_table.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/symbol_table.c b/src/symbol_table.c index 913a32b..89d133a 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -515,6 +515,11 @@ TableNode *getParameter(TableNode *definition) { "function. Invalid."); return undefined; } + if(definition->additionalinfo == NULL){ + printdebug( + "node has NULL additionalinfo. Invalid."); + return undefined; + } if (strcmp(getType(definition), "primitive function type") != 0) { printdebug( "not checking the parameter of a function -- invalid op"); From f6dabd8d0342f0ef30161e7aba7412bfe6d88c3e Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 14:02:34 -0400 Subject: [PATCH 28/45] working on make sure types pass properly in grammar --- src/grammar.y | 5 +++-- src/symbol_table.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 029d545..651c5ae 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -172,7 +172,8 @@ definition: cur = CreateScope(cur, 0, 0); printdebug("Created a new scope"); } L_PAREN { - TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); + TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1))); + //TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); @@ -359,7 +360,7 @@ declaration: } else if(d == TYPE_FUNCTION_TYPE) { printdebug("invalid (function) type passed in declaration list in dblock", @2.first_line, @2.first_column); - d = TYPE_FUNCTION_TYPE; + d = TYPE_FUNCTION_DECLARATION; CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } else if(d == TYPE_ARRAY_TYPE){ diff --git a/src/symbol_table.c b/src/symbol_table.c index 89d133a..1283d6a 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1124,11 +1124,11 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { char *recordAdInfo = (char *)malloc(100); - sprintf(recordAdInfo, " elements-%d", getRecLength(entry)); + sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, "record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " record type", recordAdInfo); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, "record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " record type", recordAdInfo); } } @@ -1136,15 +1136,15 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d", getRecLength(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, "record instance", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), "record instance"); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, "record instance", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "record instance"); } } if (getAdInfoType(entry) == TYPE_PRIMITIVE) { char *primAdInfo = (char *)malloc(100); - sprintf(primAdInfo, " size-%d bytes", getPrimSize(entry)); + sprintf(primAdInfo, " size-%d bytes", getPrimSize(getTypeEntry(entry))); if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive", primAdInfo); } else { @@ -1169,9 +1169,9 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Function", " Function Declaration"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Function Definition"); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " Function", " Function Declaration"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Function Definition"); } } From 3e1e159561443ac2bfb275ec0b3e28cd6f9947df Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 15:45:33 -0400 Subject: [PATCH 29/45] starting to work on grammar fixes --- src/grammar.y | 14 ++++++++------ src/symbol_table.c | 15 ++++++++++++++- src/symbol_table.h | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 651c5ae..24b7d15 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -174,14 +174,15 @@ definition: } L_PAREN { TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1))); //TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - printdebug("parameter type: %s", getType(parameter)); + printdebug("type of parameter: %s", getName(parameter)); if (parameter == undefined) { printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); - }else if(getAdInfoType(parameter) != TYPE_RECORD){ - int type_of_param_type = getAdInfoType(parameter); + }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ + int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h if( type_of_param_type == TYPE_UNDEFINED || type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_PRIMITIVE || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused @@ -355,7 +356,7 @@ declaration: printdebug("ID/TYPE: %s, ID: %s", getName((TableNode*)$1), $3) ; int d = getAdInfoType((TableNode*)$1); if(d == TYPE_UNDEFINED) { - printdebug("undefined type at line %d and column %d", @2.first_line, @2.first_column); + printdebug("[TYPE CHECK] undefined type at line %d and column %d", @2.first_line, @2.first_column); CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } else if(d == TYPE_FUNCTION_TYPE) { @@ -373,11 +374,12 @@ declaration: d = TYPE_RECORD; CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } - else if(d == TYPE_PRIMITIVE){ + else if(d == TYPE_PRIMITIVE_TYPE){ printdebug("primitive variable at line %d and column %d", @2.first_line, @2.first_column); + d = TYPE_PRIMITIVE; CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); }else { - printdebug("other invalid type passed at %d and column %d", @2.first_line, @2.first_column); + printdebug("[TYPE CHECK] other invalid type passed at %d and column %d", @2.first_line, @2.first_column); CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } } diff --git a/src/symbol_table.c b/src/symbol_table.c index 1283d6a..60752bd 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1142,6 +1142,19 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } } + if (getAdInfoType(entry) == TYPE_PRIMITIVE_TYPE) { + char *primAdInfo = (char *)malloc(100); + sprintf(primAdInfo, " size-%d bytes", getPrimSize(getTypeEntry(entry))); + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive Type", primAdInfo); + } else { + //printdebug("%sTHIS ONE", COLOR_RED); + printTableNode(entry); + char *primType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(primType, " %s", getType(entry)); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo); + } + } if (getAdInfoType(entry) == TYPE_PRIMITIVE) { char *primAdInfo = (char *)malloc(100); sprintf(primAdInfo, " size-%d bytes", getPrimSize(getTypeEntry(entry))); @@ -1152,7 +1165,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { printTableNode(entry); char *primType = (char *)malloc(sizeof(getType(entry) + 1)); sprintf(primType, " %s", getType(entry)); - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "Primitive Instance"); } } diff --git a/src/symbol_table.h b/src/symbol_table.h index af2d827..5f04f7c 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -79,7 +79,8 @@ typedef enum { TYPE_UNDEFINED = 8, TYPE_RECORD = 9, TYPE_ARRAY = 10, - TYPE_SYSTEM_DEFINED = 11 + TYPE_SYSTEM_DEFINED = 11, + TYPE_PRIMITIVE_TYPE = 12 } types; AdInfo *CreatePrimitiveInfo(int size); From 55bc098de5cf080845e06dc03cafd5c6af12116c Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 16:28:16 -0400 Subject: [PATCH 30/45] edited function definitions --- src/grammar.y | 41 ++++++++++++++++------ tests/sprint2/test/sp2_carls_mistake.alpha | 2 +- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 24b7d15..579ebd5 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -185,6 +185,7 @@ definition: || type_of_param_type == TYPE_PRIMITIVE || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_RECORD || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter)); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases @@ -192,7 +193,15 @@ definition: if(type_of_param_type == TYPE_UNDEFINED){ CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); } else { - CreateEntry(cur, getAdInfoType(parameter), parameter,NULL, getAdInfo(parameter)); + if(type_of_param_type == TYPE_FUNCTION_TYPE){ + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); + } + if(type_of_param_type == TYPE_ARRAY_TYPE){ + CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); + } + if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ + CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter)); + } } } else { for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ @@ -200,22 +209,32 @@ definition: if( type_of_param_type == TYPE_UNDEFINED || type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_PRIMITIVE || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_RECORD || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter being passed in to AS function definition is %s which is invalid", getName(entry)); + printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter)); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases }else{ - printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); + printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); + } + if(type_of_param_type == TYPE_UNDEFINED){ + CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + } else { + if(type_of_param_type == TYPE_FUNCTION_TYPE){ + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry)); + } + if(type_of_param_type == TYPE_ARRAY_TYPE){ + CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry)); + } + if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ + CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry)); + } + /*printdebug("creating entry of type %s for function", getType(entry)); + CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ + } } - if(type_of_param_type == TYPE_UNDEFINED){ - CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); - } else { - CreateEntry(cur,type_of_param_type, entry, NULL, getAdInfo(entry)); - /*printdebug("creating entry of type %s for function", getType(entry)); - CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ - } - } } } idlist { printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$5); diff --git a/tests/sprint2/test/sp2_carls_mistake.alpha b/tests/sprint2/test/sp2_carls_mistake.alpha index c85c310..45d53c3 100644 --- a/tests/sprint2/test/sp2_carls_mistake.alpha +++ b/tests/sprint2/test/sp2_carls_mistake.alpha @@ -5,7 +5,7 @@ type rec: [integer: x; integer: y] type T1: integer -> integer type T2: rec -> integer type arr: 1 -> integer - +type w : rec function foo: T1 function bar1: T2 function bar2: T2 From e8bad449493a8ff8f0dbc667a79420a840ee3e8a Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 16:37:57 -0400 Subject: [PATCH 31/45] fixed print symbol table and added primitive types --- src/symbol_table.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index 60752bd..ae81eb8 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -692,11 +692,11 @@ SymbolTable *init(SymbolTable *start) { stri->additionalinfo = CreateArrayInfo(1, chara); boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); - integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ - addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr - chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara + integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ + addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr + chara->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for chara stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri - boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo + boo->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for boo // addr->additionalinfo = CreatePrimitiveInfo(8); start->Line_Number = 1; @@ -1144,7 +1144,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (getAdInfoType(entry) == TYPE_PRIMITIVE_TYPE) { char *primAdInfo = (char *)malloc(100); - sprintf(primAdInfo, " size-%d bytes", getPrimSize(getTypeEntry(entry))); + sprintf(primAdInfo, " size-%d bytes", getPrimSize(entry)); if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive Type", primAdInfo); } else { From 3fc7a6371a9abe377fea41f1f8a473f7b24d3aab Mon Sep 17 00:00:00 2001 From: Partho Date: Fri, 11 Apr 2025 19:00:24 -0400 Subject: [PATCH 32/45] have to finish idlist rules --- src/grammar.y | 25 +++++++++++-- src/symbol_table.c | 36 +++++++++++-------- src/symbol_table.h | 1 + .../sprint3/test/sp3_record_size_check.alpha | 2 ++ 4 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 tests/sprint3/test/sp3_record_size_check.alpha diff --git a/src/grammar.y b/src/grammar.y index 579ebd5..dfc8db3 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -21,6 +21,7 @@ void yyerror(const char *err); int token_tracker; TableNode * tn; + int counter; %} %union { @@ -137,6 +138,8 @@ definition: //We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize //and then putting it in the entry that we created above. setRecSize(look_up(getParent(cur), $2), getRecSize(cur)); + //putting in all the offsets + setRecOffsetInfo(cur, look_up(getParent(cur),$2)); printdebug("Moving up a scope after seeing a record definition"); cur = getParent(cur); } @@ -236,6 +239,8 @@ definition: } } } + counter = 0; + printdebug("Created a new scope after seeing a function definition"); } idlist { printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$5); } R_PAREN ASSIGN sblock //check sblock type @@ -273,7 +278,15 @@ function_declaration: idlist: ID { + counter ++; TableNode *entry = getFirstEntry(cur); + int count = 1; + while(count largest) { largest = s; @@ -222,7 +222,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { counter++; this = getNextEntry(this); } - else if(getAdInfoType(this) == TYPE_ARRAY_TYPE){ + else if(getAdInfoType(this) == TYPE_ARRAY){ int s = 8; if (s > largest) { largest = s; @@ -237,12 +237,14 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { counter++; this = getNextEntry(this); } - else if(getAdInfoType(this) == TYPE_RECORD_TYPE){ - int s = getRecTotal(this); + else if((getAdInfoType(this) == TYPE_RECORD) && (node != getTypeEntry(this))){ + int s = getRecTotal(getTypeEntry(this)); if (s > largest) { largest = s; } //make sure current location is aligned properly + printTableNode(this); + printTableNode(node); offsets[counter] = (total_size % s); total_size = total_size + offsets[counter]; counter++; @@ -253,7 +255,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { this = getNextEntry(this); } else if(getAdInfoType(this) == TYPE_PRIMITIVE){ - int s = getPrimSize(this); + int s = getPrimSize(getTypeEntry(this)); if (s > largest) { largest = s; } @@ -1049,8 +1051,12 @@ TableNode *look_up(SymbolTable *table, char *x) { } int col_widths[5] = {30, 8, 8, 35, 35}; -void printline(FILE *file_ptr); -void printline(FILE *file_ptr) { +void printline(FILE *file_ptr, bool b); +void printline(FILE *file_ptr, bool b) { + if (b) { + fprintf(file_ptr, "oop\n"); + } + for (int i = 0; i < 5; i++) { for (int ii = 0; ii < col_widths[i]; ii++) { fprintf(file_ptr, "-"); @@ -1095,7 +1101,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } TableNode *entry = table->entries; - printline(file_ptr); + printline(file_ptr, false); int parentScopeNum = 0; int currentScopeNum = 0; @@ -1213,7 +1219,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } if (getParent(table) == NULL) { - printline(file_ptr); + printline(file_ptr, true); } } // get top most symbol table diff --git a/src/symbol_table.h b/src/symbol_table.h index 5f04f7c..6a33974 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -111,6 +111,7 @@ char *getType(TableNode *tn); char *getName(TableNode *tn); int getLine(SymbolTable *st); int getColumn(SymbolTable *st); +TableNode *getTypeEntry(TableNode *tn); TableNode *addName(TableNode *tn, char *str); SymbolTable *setLineNumber(SymbolTable *st, int line); SymbolTable *setColumnNumber(SymbolTable *st, int column); diff --git a/tests/sprint3/test/sp3_record_size_check.alpha b/tests/sprint3/test/sp3_record_size_check.alpha new file mode 100644 index 0000000..a6217fe --- /dev/null +++ b/tests/sprint3/test/sp3_record_size_check.alpha @@ -0,0 +1,2 @@ +type tom : [integer : x; integer: y] +type rec : [integer : x; tom : prev; character : c; character : d; Boolean: b; integer : y] \ No newline at end of file From 3baa95288a5c1e4cec342471940440c4dea28da8 Mon Sep 17 00:00:00 2001 From: Partho Date: Mon, 14 Apr 2025 02:01:51 -0400 Subject: [PATCH 33/45] fixed issue with not printing array instances in symbol table --- src/grammar.y | 179 +++++++++++++++------------- src/symbol_table.c | 11 ++ tests/sprint2/test/sp2_llnode.alpha | 6 +- 3 files changed, 108 insertions(+), 88 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index dfc8db3..79ff3db 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -21,7 +21,7 @@ void yyerror(const char *err); int token_tracker; TableNode * tn; - int counter; +// int counter; %} %union { @@ -159,29 +159,57 @@ definition: CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); } - | ID { - TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == undefined) { + | ID { + printdebug("see function def rule 1\n"); + TableNode *node = table_lookup(getAncestor(cur), $1); + if (node == undefined) { - printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); - }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ - printdebug("[TYPE CHECK] not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); - } - else { - printdebug("setting as keyword to true"); - setStartLine(node, @1.first_line); - setAsKeyword(node, true); - } - cur = CreateScope(cur, 0, 0); - printdebug("Created a new scope"); + printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); + }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ + printdebug("[TYPE CHECK] not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); + } + else { + printdebug("setting as keyword to true"); + setStartLine(node, @1.first_line); + setAsKeyword(node, true); + } + cur = CreateScope(cur, 0, 0); + printdebug("Created a new scope"); } L_PAREN { TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1))); //TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); printdebug("type of parameter: %s", getName(parameter)); - if (parameter == undefined) { - printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); - }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ - int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h + if (parameter == undefined) { + printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); + }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ + int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h + if( type_of_param_type == TYPE_UNDEFINED + || type_of_param_type == TYPE_FUNCTION_DECLARATION + || type_of_param_type == TYPE_ARRAY + || type_of_param_type == TYPE_PRIMITIVE + || type_of_param_type == TYPE_ALL_ELSE + || type_of_param_type == TYPE_SYSTEM_DEFINED + || type_of_param_type == TYPE_RECORD + || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused + printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter)); + type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + } + if(type_of_param_type == TYPE_UNDEFINED){ + CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); + } else { + if(type_of_param_type == TYPE_FUNCTION_TYPE){ + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); + } + if(type_of_param_type == TYPE_ARRAY_TYPE){ + CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); + } + if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ + CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter)); + } + } + } else { + for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ + int type_of_param_type = getAdInfoType(entry); if( type_of_param_type == TYPE_UNDEFINED || type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_ARRAY @@ -190,60 +218,31 @@ definition: || type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_RECORD || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter)); + printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter)); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases + }else{ + printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); } if(type_of_param_type == TYPE_UNDEFINED){ CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); } else { if(type_of_param_type == TYPE_FUNCTION_TYPE){ - CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry)); } if(type_of_param_type == TYPE_ARRAY_TYPE){ - CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); + CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry)); } if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ - CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter)); - } - } - } else { - for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ - int type_of_param_type = getAdInfoType(entry); - if( type_of_param_type == TYPE_UNDEFINED - || type_of_param_type == TYPE_FUNCTION_DECLARATION - || type_of_param_type == TYPE_ARRAY - || type_of_param_type == TYPE_PRIMITIVE - || type_of_param_type == TYPE_ALL_ELSE - || type_of_param_type == TYPE_SYSTEM_DEFINED - || type_of_param_type == TYPE_RECORD - || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter)); - type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases - }else{ - printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); - } - if(type_of_param_type == TYPE_UNDEFINED){ - CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); - } else { - if(type_of_param_type == TYPE_FUNCTION_TYPE){ - CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry)); - } - if(type_of_param_type == TYPE_ARRAY_TYPE){ - CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry)); - } - if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ - CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry)); - } + CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry)); + } /*printdebug("creating entry of type %s for function", getType(entry)); CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ - } - } + } } - counter = 0; + } + //counter = 0; printdebug("Created a new scope after seeing a function definition"); - } idlist { - printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$5); - } R_PAREN ASSIGN sblock //check sblock type + } idlist R_PAREN ASSIGN sblock //check sblock type ; @@ -278,24 +277,31 @@ function_declaration: idlist: ID { - counter ++; + printdebug("idlist rule 1 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - int count = 1; - while(counttheName == NULL){ + addName(entry, $1); + printdebug("name added to entry of type %s is %s in function parameter scope",getType(entry), $1); + } else { + printdebug("undefined types passed in to function scope. Improper."); + addName(entry, $1); } - addName(entry, $1); - printdebug("name added to entry is %s", $1); printTableNode(entry); + //while (strcmp(getName(entry),"undefined") != 0) { + // entry = getNextEntry(entry); + //} + //if (getNextEntry(entry) == NULL) { + // printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); + //} + //addName(entry, $1); + //printdebug("name added to entry is %s", $1); + //printTableNode(entry); } COMMA idlist { @@ -303,22 +309,25 @@ idlist: } | ID - { - counter ++; + { printdebug("idlist rule 2 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - int count = 1; - while(count1); - $$ = 1; + else if(entry->theName == NULL){ + addName(entry, $1); + printdebug("name added to entry of type %s is %s in function parameter scope",getType(entry), $1); + } else { + printdebug("undefined types passed in to function scope. Improper."); + addName(entry, $1); + } + printTableNode(entry); + printdebug("Name of entry is now %s", getName(entry)); + printdebug("Type of entry is %s", getType(entry)); + printdebug("tag is %d", getAdInfoType(entry)); } ; @@ -375,7 +384,7 @@ dblock: setColumnNumber(cur,@1.first_line); printdebug("Did not create a new scope when saw dblock, set line number to %d instead", @1.first_line); } else{ - cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? + //cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? printdebug("Created a new scope when seeing a dblock"); } } diff --git a/src/symbol_table.c b/src/symbol_table.c index 7cd8b01..5370912 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1128,6 +1128,17 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Type of Array"); } } + if (getAdInfoType(entry) == TYPE_ARRAY) { + char *arrayType = (char *)malloc(100); + sprintf(arrayType, " %d -> %s", getNumArrDim(entry), + getName(getArrType(entry))); + + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Array Instance"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Array Instance"); + } + } if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry)); diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index 08048a7..b102ba8 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -47,11 +47,11 @@ foo (x) := { return x * x; } -bar1 (a) := { - return a.x * a.y; +bar1(a,b) := { + return a * b; } -bar2 as (r,s) := { +bar2(r,s) := { if (r < s) then { while (!(r < s)) { r := r + 1; From 5c6ab345183ff646b8ba3fe28eab2ca1106aa10f Mon Sep 17 00:00:00 2001 From: Partho Date: Mon, 14 Apr 2025 11:43:49 -0400 Subject: [PATCH 34/45] fixed issue with records being size of their reference (8 bytes) not their actual total --- src/symbol_table.c | 14 ++++++++------ tests/sprint2/test/sp2_llnode.alpha | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index 5370912..897d787 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -182,8 +182,9 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { largest = 8; counter++; } - else if((getAdInfoType(this) == TYPE_RECORD) && (node != getTypeEntry(this))){ - offsets[counter] = getRecTotal(getTypeEntry(this)); + else if((getAdInfoType(this) == TYPE_RECORD)){ + offsets[counter] = 8; + printf("hitting record and adding to largest"); total_size = total_size + offsets[counter]; largest = offsets[counter]; counter++; @@ -202,7 +203,8 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { } else { printdebug( - "[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d",getAdInfoType(this)); + "[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d. Type of entry is %s. Name attempted to pass is %s.",getAdInfoType(this),getType(this),getName(this)); + return undefined; } this = getNextEntry(this); @@ -237,8 +239,8 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { counter++; this = getNextEntry(this); } - else if((getAdInfoType(this) == TYPE_RECORD) && (node != getTypeEntry(this))){ - int s = getRecTotal(getTypeEntry(this)); + else if((getAdInfoType(this) == TYPE_RECORD)){ + int s = 8; if (s > largest) { largest = s; } @@ -1365,7 +1367,7 @@ TableNode *getNextEntry(TableNode *tn) { // Uses pointers to the table node to print the info TableNode *printTableNode(TableNode *tn) { if (DEBUG == 0) { - return tn; + return tn; } if (tn == NULL) { diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index b102ba8..31f0c2b 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -48,6 +48,7 @@ foo (x) := { } bar1(a,b) := { + [integer : t] return a * b; } From f2db338257b93f91f9853397419e6dbcc471cb7d Mon Sep 17 00:00:00 2001 From: Partho Date: Mon, 14 Apr 2025 12:33:52 -0400 Subject: [PATCH 35/45] fixed type check issues with records as params --- src/grammar.y | 29 +++++++++++++++++------------ tests/sprint2/test/sp2_llnode.alpha | 1 - 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 79ff3db..b10933b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -208,32 +208,37 @@ definition: } } } else { + printdebug("record found"); for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ int type_of_param_type = getAdInfoType(entry); if( type_of_param_type == TYPE_UNDEFINED - || type_of_param_type == TYPE_FUNCTION_DECLARATION - || type_of_param_type == TYPE_ARRAY - || type_of_param_type == TYPE_PRIMITIVE + || type_of_param_type == TYPE_FUNCTION_TYPE + || type_of_param_type == TYPE_ARRAY_TYPE + || type_of_param_type == TYPE_PRIMITIVE_TYPE || type_of_param_type == TYPE_ALL_ELSE || type_of_param_type == TYPE_SYSTEM_DEFINED - || type_of_param_type == TYPE_RECORD + || type_of_param_type == TYPE_RECORD_TYPE || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused - printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter)); + printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getType(entry)); type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases }else{ - printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry)); + printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getType(entry)); } if(type_of_param_type == TYPE_UNDEFINED){ + printdebug("undefined type of parameter inside record"); CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); } else { - if(type_of_param_type == TYPE_FUNCTION_TYPE){ - CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry)); + if(type_of_param_type == TYPE_FUNCTION_DECLARATION){ + printdebug("function declaration of parameter inside record"); + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, getTypeEntry(entry),NULL, getAdInfo(entry)); } - if(type_of_param_type == TYPE_ARRAY_TYPE){ - CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry)); + if(type_of_param_type == TYPE_ARRAY){ + printdebug("array type of parameter inside record"); + CreateEntry(cur, TYPE_ARRAY, getTypeEntry(entry),NULL, getAdInfo(entry)); } - if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ - CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry)); + if(type_of_param_type == TYPE_PRIMITIVE){ + printdebug("primitive type of parameter inside record"); + CreateEntry(cur, TYPE_PRIMITIVE, getTypeEntry(entry),NULL, getAdInfo(entry)); } /*printdebug("creating entry of type %s for function", getType(entry)); CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index 31f0c2b..b102ba8 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -48,7 +48,6 @@ foo (x) := { } bar1(a,b) := { - [integer : t] return a * b; } From ccc3c57f1cbc2d8e061d44626b85eb708ff56820 Mon Sep 17 00:00:00 2001 From: Annie Date: Mon, 14 Apr 2025 21:36:52 -0400 Subject: [PATCH 36/45] got rid of invalid typ declaration --- tests/sprint2/test/sp2_carls_mistake.alpha | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/sprint2/test/sp2_carls_mistake.alpha b/tests/sprint2/test/sp2_carls_mistake.alpha index 45d53c3..26b8725 100644 --- a/tests/sprint2/test/sp2_carls_mistake.alpha +++ b/tests/sprint2/test/sp2_carls_mistake.alpha @@ -1,11 +1,11 @@ -type main: string -> integer -function entry: main - type rec: [integer: x; integer: y] + type T1: integer -> integer type T2: rec -> integer type arr: 1 -> integer -type w : rec + +type main: string -> integer +function entry: main function foo: T1 function bar1: T2 function bar2: T2 From 06db19042839c12de6a011f576ba41df94046d88 Mon Sep 17 00:00:00 2001 From: Annie Date: Mon, 14 Apr 2025 21:37:37 -0400 Subject: [PATCH 37/45] added array so test uses array instead of address --- tests/sprint2/test/sp2_integer_binary_op.alpha | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/sprint2/test/sp2_integer_binary_op.alpha b/tests/sprint2/test/sp2_integer_binary_op.alpha index 856224f..2dfeb73 100644 --- a/tests/sprint2/test/sp2_integer_binary_op.alpha +++ b/tests/sprint2/test/sp2_integer_binary_op.alpha @@ -1,8 +1,9 @@ type main: string -> integer function entry: main +type testarr : 1 -> integer entry (arg) := { - [integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1] + [integer:x; testarr: arr; testarr: arr2; Boolean : b2; Boolean : b1] x := 3 + 2 * 8; x := 3 - 2 / 8; From 2b1557b52c1b543a5734c6dfa70559b2f3c11ac2 Mon Sep 17 00:00:00 2001 From: Annie Date: Tue, 15 Apr 2025 00:53:03 -0400 Subject: [PATCH 38/45] worked on function call return type check and started checking return type --- src/grammar.y | 101 ++++++++++++++++---- tests/sprint2/test/sp2_sp2_arrayargs.alpha | 16 ++++ tests/sprint2/test/sp2_sp2_arrayargs.alpha~ | 16 ++++ tests/sprint3/test/sp3_multiple_args.alpha | 17 ++++ tests/sprint3/test/sp3_multiple_args.alpha~ | 17 ++++ 5 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 tests/sprint2/test/sp2_sp2_arrayargs.alpha create mode 100644 tests/sprint2/test/sp2_sp2_arrayargs.alpha~ create mode 100644 tests/sprint3/test/sp3_multiple_args.alpha create mode 100644 tests/sprint3/test/sp3_multiple_args.alpha~ diff --git a/src/grammar.y b/src/grammar.y index b10933b..ab9e3a2 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -32,12 +32,17 @@ %locations + %type idlist %type assignable %type expression %type constant %type id_or_types %type types +%type sblock +%type compound_statement +%type simple_statement +%type statement_list %type argument_list %type ablock %token ID 101 @@ -100,6 +105,7 @@ %precedence DOT %precedence RESERVE RELEASE + %% program: @@ -247,7 +253,16 @@ definition: } //counter = 0; printdebug("Created a new scope after seeing a function definition"); - } idlist R_PAREN ASSIGN sblock //check sblock type + } idlist R_PAREN ASSIGN sblock { + TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1))); + if ($8 == undefined) { + printdebug("sblock return type is undefined"); + } else if ($8 != expected) { + printdebug("expected %s as return type but got %s", getName(expected), getName($8)); + } else { + printdebug("CORRECT RETURN TYPE!!!"); + } + } ; @@ -350,6 +365,7 @@ sblock: } statement_list { + $$ = $3; printdebug("Moving up a scope after seeing sblock"); cur = getParent(cur); } @@ -374,6 +390,7 @@ sblock: { printdebug("Moving up a scope after seeing sblock with dblock"); cur = getParent(cur); + $$ = $5; } R_BRACE ; @@ -458,18 +475,59 @@ id_or_types: statement_list: - compound_statement statement_list - | compound_statement - | simple_statement SEMI_COLON statement_list - | simple_statement SEMI_COLON +compound_statement statement_list { + if ($1 == undefined && $2 != undefined) { + $$ = $2; + } else if ($1 != undefined && $2 == undefined) { + $$ = $1; + } else if ($1 == $2) { + $$ = $1; + } else { + printdebug("differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); + $$ = undefined; + } +} + | compound_statement { + $$ = $1; + } + | simple_statement SEMI_COLON statement_list{ + if ($1 == undefined && $3 != undefined) { + $$ = $3; + } else if ($1 != undefined && $3 == undefined) { + $$ = $1; + } else if ($1 == $3) { + $$ = $1; + } else { + printdebug("differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); + $$ = undefined; + } + } + | simple_statement SEMI_COLON { + $$ = $1; + } ; compound_statement: - WHILE L_PAREN expression R_PAREN sblock - | IF L_PAREN expression R_PAREN THEN sblock ELSE sblock - | sblock +WHILE L_PAREN expression R_PAREN sblock { + $$ = $5; +} + | IF L_PAREN expression R_PAREN THEN sblock ELSE sblock { + if ($6 == undefined && $8 != undefined) { + $$ = $8; + } else if ($6 != undefined && $8 == undefined) { + $$ = $6; + } else if ($6 == $8) { + $$ = $6; + } else { + printdebug("differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); + $$ = undefined; + } + } + | sblock { + $$ = $1; + } ; @@ -517,9 +575,11 @@ simple_statement: printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType(left), getType(right), COLOR_WHITE); printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(left)); } + + $$ = undefined; } - | RETURN expression +| RETURN expression {$$ = $2;} ; @@ -697,6 +757,7 @@ expression: else if(getAdInfoType((TableNode*)$1) == TYPE_ARRAY_TYPE|| getAdInfoType((TableNode*)$1) == TYPE_RECORD_TYPE|| getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_TYPE|| + getAdInfoType((TableNode*)$1) == TYPE_PRIMITIVE_TYPE || getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_DECLARATION){ printdebug("assignable passing up to expression is array type, record type, function type, or function declaration"); $$= ((TableNode*)$1); @@ -751,20 +812,20 @@ assignable: printdebug("%stype is %d", COLOR_PURPLE, type); printdebug("%s", getName((TableNode*)$1)); - if (type == TYPE_FUNCTION_DECLARATION) { + if (type == TYPE_FUNCTION_TYPE) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); if (look_up(getParent(cur), getName((TableNode*)$1))->additionalinfo->FunDecAdInfo->regularoras) { printdebug("as function"); //char *funtype = getType(look_up(cur, $1)); - printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); +// printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); - TableNode * typeNode = table_lookup(getAncestor(cur), getType((TableNode*)$1)); + TableNode * typeNode = $1; TableNode *param = getParameter(typeNode); printTableNode(param); - if (getAdInfoType(param) == TYPE_RECORD) { + if (getAdInfoType(param) == TYPE_RECORD_TYPE) { SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); @@ -772,12 +833,14 @@ assignable: lastCheckedRef = getNextEntry(lastCheckedRef); } - + if ($3 != getRecLength(param)) { + printdebug("expected %d arguments but got %d", getRecLength(param), $3); + } //this isn't very efficient, but will hopefully work while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { + if (getTypeEntry(lastCheckedRef) != getTypeEntry(lastCheckedAct)) { printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); - printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); + } lastCheckedAct = getNextEntry(lastCheckedAct); TableNode *tn = getFirstEntry(recList); @@ -804,8 +867,12 @@ assignable: if (strcmp(expected, actual) != 0) { printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); } + if ($3 != 1) { + printdebug("expected 1 argument but got %d", $3); + } } - $$ = getReturn((table_lookup(getAncestor(cur), getType((TableNode*)$1)))); + printTableNode(getReturn($1)); + $$ = getReturn($1); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName((TableNode*)$$), getName((TableNode*)$1)); } else if (type == TYPE_ARRAY_TYPE) { diff --git a/tests/sprint2/test/sp2_sp2_arrayargs.alpha b/tests/sprint2/test/sp2_sp2_arrayargs.alpha new file mode 100644 index 0000000..dfb0332 --- /dev/null +++ b/tests/sprint2/test/sp2_sp2_arrayargs.alpha @@ -0,0 +1,16 @@ +type string: 1 -> character +type a_of_s: 1 -> string + +(* maybe some other type definitions *) + +entry(arg) := { + [ string: one_name; string: another_name; a_of_s: many_names ] + another_name := reserve another_name(4); (* reserve space for an an array of character, with 4 members *) + many_names := reserve a_of_s(many_names); + many_names(0) := one_name; + many_names(1) := another_name; + many_names(2) := reserve a_of_s(2)(6); (* reserve space for an item of the same type as a_of_s(2), an array of character, with 6 members *) + many_names(2)(0) := "P"; + + return 0; +} \ No newline at end of file diff --git a/tests/sprint2/test/sp2_sp2_arrayargs.alpha~ b/tests/sprint2/test/sp2_sp2_arrayargs.alpha~ new file mode 100644 index 0000000..ce1673d --- /dev/null +++ b/tests/sprint2/test/sp2_sp2_arrayargs.alpha~ @@ -0,0 +1,16 @@ +type string: 1 -> character +type a_of_s: 1 -> string + +(* maybe some other type definitions *) + +entry(arg) := { + [ string: one_name; string: another_name; a_of_s: many_names ] + another_name := reserve another_name(4); (* reserve space for an an array of character, with 4 members *) + many_names := reserve a_of_s(3); + many_names(0) := one_name; + many_names(1) := another_name; + many_names(2) := reserve a_of_s(2)(6); (* reserve space for an item of the same type as a_of_s(2), an array of character, with 6 members *) + many_names(2)(0) := "P"; + + return 0; +} \ No newline at end of file diff --git a/tests/sprint3/test/sp3_multiple_args.alpha b/tests/sprint3/test/sp3_multiple_args.alpha new file mode 100644 index 0000000..e59c4b6 --- /dev/null +++ b/tests/sprint3/test/sp3_multiple_args.alpha @@ -0,0 +1,17 @@ +type rec: [character: x; integer: y] + +type T2: rec -> integer + +type main: string -> integer +function entry: main +function bar: T2 + +bar2 (r,s) := { + return r; +} + +entry (arg) := { + [ integer: result ; rec: w] + result := bar('c', 7); + return 0; +} \ No newline at end of file diff --git a/tests/sprint3/test/sp3_multiple_args.alpha~ b/tests/sprint3/test/sp3_multiple_args.alpha~ new file mode 100644 index 0000000..641c654 --- /dev/null +++ b/tests/sprint3/test/sp3_multiple_args.alpha~ @@ -0,0 +1,17 @@ +type rec: [character: x; integer: y] + +type T2: rec -> integer + +type main: string -> integer +function entry: main +function bar: T2 + +bar2 (r,s) := { + return s; +} + +entry (arg) := { + [ integer: result ; rec: w] + result := bar('c', 7); + return 0; +} \ No newline at end of file From 8057060f2683801948a3e6bdb5f5fb3b8bf94c75 Mon Sep 17 00:00:00 2001 From: Partho Date: Tue, 15 Apr 2025 01:40:18 -0400 Subject: [PATCH 39/45] fixed several small issues --- src/grammar.y | 9 ++++++--- src/symbol_table.c | 16 +++++++++------- tests/sprint2/test/sp2_simple.alpha | 2 +- tests/sprint3/test/sp3_record_size_check.alpha | 3 ++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index ab9e3a2..4e2aa1d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -365,11 +365,12 @@ sblock: } statement_list { - $$ = $3; + //$$ = $3; printdebug("Moving up a scope after seeing sblock"); cur = getParent(cur); } R_BRACE + {$$ = $3;} | L_BRACE { @@ -390,9 +391,10 @@ sblock: { printdebug("Moving up a scope after seeing sblock with dblock"); cur = getParent(cur); - $$ = $5; + //$$ = $5; } R_BRACE + {$$ = $5;} ; @@ -880,7 +882,8 @@ assignable: if (getNumArrDim(look_up(getParent(cur), getName((TableNode*)$1))) != $2) { printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName((TableNode*)$1))), $2, @2.first_line, @2.first_column); } - $$ = getArrType(look_up(getParent(cur), getName((TableNode*)$1))); + $$ = getArrType(look_up(cur, getName((TableNode*)$1))); + printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName((TableNode*)$$), getName((TableNode*)$1)); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName((TableNode*)$$), getName((TableNode*)$1)); } cur = getParent(cur); diff --git a/src/symbol_table.c b/src/symbol_table.c index 897d787..4232c16 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -92,9 +92,10 @@ int getNumArrDim(TableNode *definition) { "function. Invalid."); return -1; } - if (strcmp(getType(definition), "array") != 0) { - printdebug("not checking the dim of an array -- invalid op"); - return 0; + if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){ + printdebug( + "passed an invalid node to getNumArrDim. Seeing tag %d in getNumArrDim. Invalid.",getAdInfoType(definition)); + return -1; } return definition->additionalinfo->ArrayAdInfo->numofdimensions; } @@ -113,8 +114,9 @@ TableNode *getArrType(TableNode *definition) { "function. Invalid."); return undefined; } - if (strcmp(getType(definition), "array") != 0) { - printdebug("not checking the type of an array -- invalid op"); + if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){ + printdebug( + "passed an invalid node to getArrType. Seeing tag %d. Invalid.",getAdInfoType(definition)); return undefined; } return definition->additionalinfo->ArrayAdInfo->typeofarray; @@ -1132,8 +1134,8 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } if (getAdInfoType(entry) == TYPE_ARRAY) { char *arrayType = (char *)malloc(100); - sprintf(arrayType, " %d -> %s", getNumArrDim(entry), - getName(getArrType(entry))); + //sprintf(arrayType, " %d -> %s", getNumArrDim(entry), + // getName(getArrType(entry))); if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Array Instance"); diff --git a/tests/sprint2/test/sp2_simple.alpha b/tests/sprint2/test/sp2_simple.alpha index 5f60c1b..825fdcd 100644 --- a/tests/sprint2/test/sp2_simple.alpha +++ b/tests/sprint2/test/sp2_simple.alpha @@ -2,6 +2,6 @@ type main: string -> integer function entry: main entry(arg) := { - [int : x] + [integer : x] return 0; } diff --git a/tests/sprint3/test/sp3_record_size_check.alpha b/tests/sprint3/test/sp3_record_size_check.alpha index a6217fe..8e123d0 100644 --- a/tests/sprint3/test/sp3_record_size_check.alpha +++ b/tests/sprint3/test/sp3_record_size_check.alpha @@ -1,2 +1,3 @@ type tom : [integer : x; integer: y] -type rec : [integer : x; tom : prev; character : c; character : d; Boolean: b; integer : y] \ No newline at end of file +type rec : [integer : x; tom : prev; character : c; character : d; Boolean: b; integer : y] +type tricky : [Boolean : b1; integer : k1; Boolean : b2; integer : k2] \ No newline at end of file From c091927fe7e9aa778e5a4dc6276af1f45a34d79f Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 15 Apr 2025 14:46:00 -0400 Subject: [PATCH 40/45] carl --- tests/carl/Errors/entry.undeclaredType.alpha | 10 +++ .../Errors/entry.undeclaredType.alpha.asc | 16 ++++ tests/carl/Errors/entry.undeclaredVar.alpha | 7 ++ .../carl/Errors/entry.undeclaredVar.alpha.asc | 12 +++ tests/carl/Errors/error.operator.alpha | 14 ++++ tests/carl/Errors/error.operator.alpha.asc | 19 +++++ tests/carl/NoErrors/entry.definition.alpha | 8 ++ .../carl/NoErrors/entry.definition.alpha.asc | 10 +++ .../NoErrors/entry.duplicateDifferent.alpha | 11 +++ .../entry.duplicateDifferent.alpha.asc | 13 ++++ tests/carl/NoErrors/entry.duplicateSame.alpha | 9 +++ .../NoErrors/entry.duplicateSame.alpha.asc | 11 +++ tests/carl/NoErrors/entry.local.alpha | 11 +++ tests/carl/NoErrors/entry.local.alpha.asc | 13 ++++ tests/carl/NoErrors/error.none.alpha | 14 ++++ tests/carl/NoErrors/error.none.alpha.asc | 16 ++++ .../carl/NoErrors/function.declaration.alpha | 3 + .../NoErrors/function.declaration.alpha.asc | 5 ++ tests/carl/NoErrors/function.definition.alpha | 7 ++ .../NoErrors/function.definition.alpha.asc | 9 +++ tests/carl/NoErrors/functionValue.alpha | 73 ++++++++++++++++++ tests/carl/NoErrors/functionValue.alpha.asc | 75 ++++++++++++++++++ tests/carl/NoErrors/sample.good.alpha | 29 +++++++ tests/carl/NoErrors/sample.good.alpha.asc | 31 ++++++++ tests/carl/NoErrors/selectionSort.alpha | 68 ++++++++++++++++ tests/carl/NoErrors/selectionSort.alpha.asc | 70 +++++++++++++++++ tests/carl/NoErrors/type.array.alpha | 1 + tests/carl/NoErrors/type.array.alpha.asc | 3 + tests/carl/NoErrors/type.mapping.alpha | 2 + tests/carl/NoErrors/type.mapping.alpha.asc | 4 + tests/carl/NoErrors/type.record.alpha | 1 + tests/carl/NoErrors/type.record.alpha.asc | 3 + tests/carl/NoErrors/types.alpha | 75 ++++++++++++++++++ tests/carl/NoErrors/types.alpha.asc | 77 +++++++++++++++++++ 34 files changed, 730 insertions(+) create mode 100644 tests/carl/Errors/entry.undeclaredType.alpha create mode 100644 tests/carl/Errors/entry.undeclaredType.alpha.asc create mode 100644 tests/carl/Errors/entry.undeclaredVar.alpha create mode 100644 tests/carl/Errors/entry.undeclaredVar.alpha.asc create mode 100644 tests/carl/Errors/error.operator.alpha create mode 100644 tests/carl/Errors/error.operator.alpha.asc create mode 100644 tests/carl/NoErrors/entry.definition.alpha create mode 100644 tests/carl/NoErrors/entry.definition.alpha.asc create mode 100644 tests/carl/NoErrors/entry.duplicateDifferent.alpha create mode 100644 tests/carl/NoErrors/entry.duplicateDifferent.alpha.asc create mode 100644 tests/carl/NoErrors/entry.duplicateSame.alpha create mode 100644 tests/carl/NoErrors/entry.duplicateSame.alpha.asc create mode 100644 tests/carl/NoErrors/entry.local.alpha create mode 100644 tests/carl/NoErrors/entry.local.alpha.asc create mode 100644 tests/carl/NoErrors/error.none.alpha create mode 100644 tests/carl/NoErrors/error.none.alpha.asc create mode 100644 tests/carl/NoErrors/function.declaration.alpha create mode 100644 tests/carl/NoErrors/function.declaration.alpha.asc create mode 100644 tests/carl/NoErrors/function.definition.alpha create mode 100644 tests/carl/NoErrors/function.definition.alpha.asc create mode 100644 tests/carl/NoErrors/functionValue.alpha create mode 100644 tests/carl/NoErrors/functionValue.alpha.asc create mode 100644 tests/carl/NoErrors/sample.good.alpha create mode 100644 tests/carl/NoErrors/sample.good.alpha.asc create mode 100644 tests/carl/NoErrors/selectionSort.alpha create mode 100644 tests/carl/NoErrors/selectionSort.alpha.asc create mode 100644 tests/carl/NoErrors/type.array.alpha create mode 100644 tests/carl/NoErrors/type.array.alpha.asc create mode 100644 tests/carl/NoErrors/type.mapping.alpha create mode 100644 tests/carl/NoErrors/type.mapping.alpha.asc create mode 100644 tests/carl/NoErrors/type.record.alpha create mode 100644 tests/carl/NoErrors/type.record.alpha.asc create mode 100644 tests/carl/NoErrors/types.alpha create mode 100644 tests/carl/NoErrors/types.alpha.asc diff --git a/tests/carl/Errors/entry.undeclaredType.alpha b/tests/carl/Errors/entry.undeclaredType.alpha new file mode 100644 index 0000000..6b9531f --- /dev/null +++ b/tests/carl/Errors/entry.undeclaredType.alpha @@ -0,0 +1,10 @@ +type M : string -> integer + +function foo : M + +foo (s) := { + [ + int: x + ] + return 0; +} diff --git a/tests/carl/Errors/entry.undeclaredType.alpha.asc b/tests/carl/Errors/entry.undeclaredType.alpha.asc new file mode 100644 index 0000000..cd1d37c --- /dev/null +++ b/tests/carl/Errors/entry.undeclaredType.alpha.asc @@ -0,0 +1,16 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.undeclaredType.alpha +001: type M : string -> integer +002: +003: function foo : M +004: +005: foo (s) := { +006: [ +007: int: x + ^0 ^1 + LINE 7:9 ** ERROR #0: the name 'int', used here as a type, has not been declared at this point in the program. + LINE 7:14 ** ERROR #1: the name 'x' is being declared with an unknown type. + +008: ] +009: return 0; +010: } +011: diff --git a/tests/carl/Errors/entry.undeclaredVar.alpha b/tests/carl/Errors/entry.undeclaredVar.alpha new file mode 100644 index 0000000..6cd5fa7 --- /dev/null +++ b/tests/carl/Errors/entry.undeclaredVar.alpha @@ -0,0 +1,7 @@ +type M : string -> integer + +function entry : M + +entry(s) := { + return x; +} diff --git a/tests/carl/Errors/entry.undeclaredVar.alpha.asc b/tests/carl/Errors/entry.undeclaredVar.alpha.asc new file mode 100644 index 0000000..7c4e6c2 --- /dev/null +++ b/tests/carl/Errors/entry.undeclaredVar.alpha.asc @@ -0,0 +1,12 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.undeclaredVar.alpha +001: type M : string -> integer +002: +003: function entry : M +004: +005: entry(s) := { +006: return x; + ^0 + LINE 6:12 ** ERROR #0: the name 'x', used here as a variable name, has not been declared at this point in the program. + +007: } +008: diff --git a/tests/carl/Errors/error.operator.alpha b/tests/carl/Errors/error.operator.alpha new file mode 100644 index 0000000..a7b0691 --- /dev/null +++ b/tests/carl/Errors/error.operator.alpha @@ -0,0 +1,14 @@ +type string2int: string -> integer + +function entry : string2int + +entry(arg) := { + [ integer: i; integer: sum ] + sum := 0; + i := 0; + while (i < 10) { + sum = sum + i; + i := i + 1; + } + return 0; +} diff --git a/tests/carl/Errors/error.operator.alpha.asc b/tests/carl/Errors/error.operator.alpha.asc new file mode 100644 index 0000000..82f5614 --- /dev/null +++ b/tests/carl/Errors/error.operator.alpha.asc @@ -0,0 +1,19 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file error.operator.alpha +001: type string2int: string -> integer +002: +003: function entry : string2int +004: +005: entry(arg) := { +006: [ integer: i; integer: sum ] +007: sum := 0; +008: i := 0; +009: while (i < 10) { +010: sum = sum + i; + ^0 + LINE 10:13 ** ERROR #0: assignment operator (:=) expected but equality operator (=) found. + +011: i := i + 1; +012: } +013: return 0; +014: } +015: diff --git a/tests/carl/NoErrors/entry.definition.alpha b/tests/carl/NoErrors/entry.definition.alpha new file mode 100644 index 0000000..0003cd5 --- /dev/null +++ b/tests/carl/NoErrors/entry.definition.alpha @@ -0,0 +1,8 @@ +(* type string : 1 -> character *) +type M : string -> integer + +function entry : M + +entry(s) := { + return 0; +} diff --git a/tests/carl/NoErrors/entry.definition.alpha.asc b/tests/carl/NoErrors/entry.definition.alpha.asc new file mode 100644 index 0000000..07dbd42 --- /dev/null +++ b/tests/carl/NoErrors/entry.definition.alpha.asc @@ -0,0 +1,10 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.definition.alpha +001: (* type string : 1 -> character *) +002: type M : string -> integer +003: +004: function entry : M +005: +006: entry(s) := { +007: return 0; +008: } +009: diff --git a/tests/carl/NoErrors/entry.duplicateDifferent.alpha b/tests/carl/NoErrors/entry.duplicateDifferent.alpha new file mode 100644 index 0000000..5cbd668 --- /dev/null +++ b/tests/carl/NoErrors/entry.duplicateDifferent.alpha @@ -0,0 +1,11 @@ +type M : string -> integer + +function entry : M + +entry(s) := { + [ + integer: x; + character: x + ] + return x; +} diff --git a/tests/carl/NoErrors/entry.duplicateDifferent.alpha.asc b/tests/carl/NoErrors/entry.duplicateDifferent.alpha.asc new file mode 100644 index 0000000..65ae8d8 --- /dev/null +++ b/tests/carl/NoErrors/entry.duplicateDifferent.alpha.asc @@ -0,0 +1,13 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.duplicateDifferent.alpha +001: type M : string -> integer +002: +003: function entry : M +004: +005: entry(s) := { +006: [ +007: integer: x; +008: character: x +009: ] +010: return x; +011: } +012: diff --git a/tests/carl/NoErrors/entry.duplicateSame.alpha b/tests/carl/NoErrors/entry.duplicateSame.alpha new file mode 100644 index 0000000..554b9d8 --- /dev/null +++ b/tests/carl/NoErrors/entry.duplicateSame.alpha @@ -0,0 +1,9 @@ +type M : string -> integer +function entry : M + +entry(s) := { + [ + integer: x; integer: x + ] + return x; +} diff --git a/tests/carl/NoErrors/entry.duplicateSame.alpha.asc b/tests/carl/NoErrors/entry.duplicateSame.alpha.asc new file mode 100644 index 0000000..7861cc6 --- /dev/null +++ b/tests/carl/NoErrors/entry.duplicateSame.alpha.asc @@ -0,0 +1,11 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.duplicateSame.alpha +001: type M : string -> integer +002: function entry : M +003: +004: entry(s) := { +005: [ +006: integer: x; integer: x +007: ] +008: return x; +009: } +010: diff --git a/tests/carl/NoErrors/entry.local.alpha b/tests/carl/NoErrors/entry.local.alpha new file mode 100644 index 0000000..cff2062 --- /dev/null +++ b/tests/carl/NoErrors/entry.local.alpha @@ -0,0 +1,11 @@ +type M : string -> integer + +function entry : M + +entry(s) := { + [ + integer: x + ] + x := 0; + return x; +} diff --git a/tests/carl/NoErrors/entry.local.alpha.asc b/tests/carl/NoErrors/entry.local.alpha.asc new file mode 100644 index 0000000..e9882d4 --- /dev/null +++ b/tests/carl/NoErrors/entry.local.alpha.asc @@ -0,0 +1,13 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file entry.local.alpha +001: type M : string -> integer +002: +003: function entry : M +004: +005: entry(s) := { +006: [ +007: integer: x +008: ] +009: x := 0; +010: return x; +011: } +012: diff --git a/tests/carl/NoErrors/error.none.alpha b/tests/carl/NoErrors/error.none.alpha new file mode 100644 index 0000000..85aa4f2 --- /dev/null +++ b/tests/carl/NoErrors/error.none.alpha @@ -0,0 +1,14 @@ +type string2int: string -> integer + +function entry : string2int + +entry(arg) := { + [ integer: i ; integer: sum ] + sum := 0; + i := 0 ; + while (i < 10) { + sum := sum + i; + i := i + 1; + } + return 0; +} diff --git a/tests/carl/NoErrors/error.none.alpha.asc b/tests/carl/NoErrors/error.none.alpha.asc new file mode 100644 index 0000000..9fdcadb --- /dev/null +++ b/tests/carl/NoErrors/error.none.alpha.asc @@ -0,0 +1,16 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file error.none.alpha +001: type string2int: string -> integer +002: +003: function entry : string2int +004: +005: entry(arg) := { +006: [ integer: i ; integer: sum ] +007: sum := 0; +008: i := 0 ; +009: while (i < 10) { +010: sum := sum + i; +011: i := i + 1; +012: } +013: return 0; +014: } +015: diff --git a/tests/carl/NoErrors/function.declaration.alpha b/tests/carl/NoErrors/function.declaration.alpha new file mode 100644 index 0000000..69f210f --- /dev/null +++ b/tests/carl/NoErrors/function.declaration.alpha @@ -0,0 +1,3 @@ +type M : integer -> integer + +function f : M diff --git a/tests/carl/NoErrors/function.declaration.alpha.asc b/tests/carl/NoErrors/function.declaration.alpha.asc new file mode 100644 index 0000000..fca046a --- /dev/null +++ b/tests/carl/NoErrors/function.declaration.alpha.asc @@ -0,0 +1,5 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file function.declaration.alpha +001: type M : integer -> integer +002: +003: function f : M +004: diff --git a/tests/carl/NoErrors/function.definition.alpha b/tests/carl/NoErrors/function.definition.alpha new file mode 100644 index 0000000..b729610 --- /dev/null +++ b/tests/carl/NoErrors/function.definition.alpha @@ -0,0 +1,7 @@ +type M : integer -> integer + +function f : M + +f(x) := { + return x; +} diff --git a/tests/carl/NoErrors/function.definition.alpha.asc b/tests/carl/NoErrors/function.definition.alpha.asc new file mode 100644 index 0000000..e476758 --- /dev/null +++ b/tests/carl/NoErrors/function.definition.alpha.asc @@ -0,0 +1,9 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file function.definition.alpha +001: type M : integer -> integer +002: +003: function f : M +004: +005: f(x) := { +006: return x; +007: } +008: diff --git a/tests/carl/NoErrors/functionValue.alpha b/tests/carl/NoErrors/functionValue.alpha new file mode 100644 index 0000000..f521a42 --- /dev/null +++ b/tests/carl/NoErrors/functionValue.alpha @@ -0,0 +1,73 @@ +(* Type definitions *) + +(* mapping type *) +type string2int: string -> integer + +(* array of functions *) +type funArray: 1 -> string2int + +(* record of functions *) +type funRec: [ string2int: f; string2int: g ] + +(* function returning function *) +type integer_2_string2int: integer -> string2int + +(* function returning function *) +type string2int_2_integer: string2int -> integer + + +type iXiXc: [integer: a; integer: b; character: c] + +type iic2b: iXiXc -> Boolean + +(* Function declarations using the above type definitions *) +function a: string2int +function b: integer_2_string2int +function c: string2int_2_integer + +function d: iic2b + +d(x,y,z) := { + return (x < y & z < 'm'); +} + +function entry: string2int + +a(x) := { + [string : s] + s := x; + return 0; +} + +b(x) := { + [integer: i] + i := x; + return a; +} + +c(x) := { + [string: s] + s := "Hi!"; + return x(s); +} + + +(* Function definition + entry is the first function called + *) +entry(arg) := { + [integer: result; string2int: f; integer: temp] + temp := a("Hello"); + f := b(temp); + result := c(f); + if (d(1,2,'c')) + then { + result := 0; + } + else { + [ Boolean : b] + result := entry("hello"); + } + result := c(f); + return result; +} diff --git a/tests/carl/NoErrors/functionValue.alpha.asc b/tests/carl/NoErrors/functionValue.alpha.asc new file mode 100644 index 0000000..882d558 --- /dev/null +++ b/tests/carl/NoErrors/functionValue.alpha.asc @@ -0,0 +1,75 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file functionValue.alpha +001: (* Type definitions *) +002: +003: (* mapping type *) +004: type string2int: string -> integer +005: +006: (* array of functions *) +007: type funArray: 1 -> string2int +008: +009: (* record of functions *) +010: type funRec: [ string2int: f; string2int: g ] +011: +012: (* function returning function *) +013: type integer_2_string2int: integer -> string2int +014: +015: (* function returning function *) +016: type string2int_2_integer: string2int -> integer +017: +018: +019: type iXiXc: [integer: a; integer: b; character: c] +020: +021: type iic2b: iXiXc -> Boolean +022: +023: (* Function declarations using the above type definitions *) +024: function a: string2int +025: function b: integer_2_string2int +026: function c: string2int_2_integer +027: +028: function d: iic2b +029: +030: d(x,y,z) := { +031: return (x < y & z < 'm'); +032: } +033: +034: function entry: string2int +035: +036: a(x) := { +037: [string : s] +038: s := x; +039: return 0; +040: } +041: +042: b(x) := { +043: [integer: i] +044: i := x; +045: return a; +046: } +047: +048: c(x) := { +049: [string: s] +050: s := "Hi!"; +051: return x(s); +052: } +053: +054: +055: (* Function definition +056: entry is the first function called +057: *) +058: entry(arg) := { +059: [integer: result; string2int: f; integer: temp] +060: temp := a("Hello"); +061: f := b(temp); +062: result := c(f); +063: if (d(1,2,'c')) +064: then { +065: result := 0; +066: } +067: else { +068: [ Boolean : b] +069: result := entry("hello"); +070: } +071: result := c(f); +072: return result; +073: } +074: diff --git a/tests/carl/NoErrors/sample.good.alpha b/tests/carl/NoErrors/sample.good.alpha new file mode 100644 index 0000000..8a8b727 --- /dev/null +++ b/tests/carl/NoErrors/sample.good.alpha @@ -0,0 +1,29 @@ + +(* Type definitions *) +type int2int: integer -> integer +type string2int: string -> integer + +(* Function declarations + They use the above type definitions +*) +function square : int2int +function entry : string2int + +(* Function definition + Functions must be declared before they are defined + *) +square(x) := { + return x * x; +} + +(* Function definition + entry is the first function called + *) +entry(arg) := { + [ integer: input ; integer: expected ; integer: actual ; Boolean: result ] + input := 7; + expected := 49; + actual := square(input); + result := expected = actual; + return 0; +} diff --git a/tests/carl/NoErrors/sample.good.alpha.asc b/tests/carl/NoErrors/sample.good.alpha.asc new file mode 100644 index 0000000..a4bfa3a --- /dev/null +++ b/tests/carl/NoErrors/sample.good.alpha.asc @@ -0,0 +1,31 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file sample.good.alpha +001: +001: (* Type definitions *) +003: type int2int: integer -> integer +004: type string2int: string -> integer +005: +006: (* Function declarations +007: They use the above type definitions +008: *) +009: function square : int2int +010: function entry : string2int +011: +012: (* Function definition +013: Functions must be declared before they are defined +014: *) +015: square(x) := { +016: return x * x; +017: } +018: +019: (* Function definition +020: entry is the first function called +021: *) +022: entry(arg) := { +023: [ integer: input ; integer: expected ; integer: actual ; Boolean: result ] +024: input := 7; +025: expected := 49; +026: actual := square(input); +027: result := expected = actual; +028: return 0; +029: } +030: diff --git a/tests/carl/NoErrors/selectionSort.alpha b/tests/carl/NoErrors/selectionSort.alpha new file mode 100644 index 0000000..edd2076 --- /dev/null +++ b/tests/carl/NoErrors/selectionSort.alpha @@ -0,0 +1,68 @@ +(* Type definitions *) + +type string2int: string -> integer +type intArray: 1 -> integer +type intArrayXinteger: [ intArray: data; integer: index ] +type intArrayXinteger2integer: intArrayXinteger -> integer +type intArray2Boolean: intArray -> Boolean + + +(* Function declarations + They use the above type definitions +*) +function indexOfSmallest: intArrayXinteger2integer +function selectionSort: intArray2Boolean +function entry : string2int + +(* indexOfSmallest *) +indexOfSmallest (* as *) (data, startingIndex) := { + [ integer: indexOfSmallestSoFar; integer: i ] + indexOfSmallestSoFar := startingIndex; + i := 0 ; + while (i < data._1 ) { + if ( data(i) < data(indexOfSmallestSoFar) ) + then { + indexOfSmallestSoFar := i; + } + else { + i := i; + } + i := i + 1; + } + return indexOfSmallestSoFar; +} + + +(* selectionSort *) +selectionSort(data) := { + [ integer: i ] + i := 0 ; + while (i < data._1 ) { + [ integer: index; integer: temp ] + index := indexOfSmallest(data,i); + temp := data(index); + data(index) := data(i); + data(i) := temp; + i := i + 1; + } + return true; +} + + +(* Function definition + entry is the first function called + *) +entry(arg) := { + [ intArray: data; Boolean: _ ] + data := reserve data(8); + data(0) := 60; + data(1) := 80; + data(2) := 10; + data(3) := 50; + data(4) := 30; + data(5) := 40; + data(6) := 20; + data(7) := 70; + _ := selectionSort(data); + return 0; +} diff --git a/tests/carl/NoErrors/selectionSort.alpha.asc b/tests/carl/NoErrors/selectionSort.alpha.asc new file mode 100644 index 0000000..2a12877 --- /dev/null +++ b/tests/carl/NoErrors/selectionSort.alpha.asc @@ -0,0 +1,70 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file selectionSort.alpha +001: (* Type definitions *) +002: +003: type string2int: string -> integer +004: type intArray: 1 -> integer +005: type intArrayXinteger: [ intArray: data; integer: index ] +006: type intArrayXinteger2integer: intArrayXinteger -> integer +007: type intArray2Boolean: intArray -> Boolean +008: +009: +010: (* Function declarations +011: They use the above type definitions +012: *) +013: function indexOfSmallest: intArrayXinteger2integer +014: function selectionSort: intArray2Boolean +015: function entry : string2int +016: +017: (* indexOfSmallest *) +018: indexOfSmallest (* as *) (data, startingIndex) := { +019: [ integer: indexOfSmallestSoFar; integer: i ] +020: indexOfSmallestSoFar := startingIndex; +021: i := 0 ; +022: while (i < data._1 ) { +023: if ( data(i) < data(indexOfSmallestSoFar) ) +024: then { +025: indexOfSmallestSoFar := i; +026: } +027: else { +028: i := i; +029: } +030: i := i + 1; +031: } +032: return indexOfSmallestSoFar; +033: } +034: +035: +036: (* selectionSort *) +037: selectionSort(data) := { +038: [ integer: i ] +039: i := 0 ; +040: while (i < data._1 ) { +041: [ integer: index; integer: temp ] +042: index := indexOfSmallest(data,i); +043: temp := data(index); +044: data(index) := data(i); +045: data(i) := temp; +046: i := i + 1; +047: } +048: return true; +049: } +050: +051: +052: (* Function definition +053: entry is the first function called +054: *) +055: entry(arg) := { +056: [ intArray: data; Boolean: _ ] +057: data := reserve data(8); +058: data(0) := 60; +059: data(1) := 80; +060: data(2) := 10; +061: data(3) := 50; +062: data(4) := 30; +063: data(5) := 40; +064: data(6) := 20; +065: data(7) := 70; +066: _ := selectionSort(data); +067: return 0; +068: } +069: diff --git a/tests/carl/NoErrors/type.array.alpha b/tests/carl/NoErrors/type.array.alpha new file mode 100644 index 0000000..2b04ba0 --- /dev/null +++ b/tests/carl/NoErrors/type.array.alpha @@ -0,0 +1 @@ +type A : 1 -> integer diff --git a/tests/carl/NoErrors/type.array.alpha.asc b/tests/carl/NoErrors/type.array.alpha.asc new file mode 100644 index 0000000..8e6154d --- /dev/null +++ b/tests/carl/NoErrors/type.array.alpha.asc @@ -0,0 +1,3 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file type.array.alpha +001: type A : 1 -> integer +002: diff --git a/tests/carl/NoErrors/type.mapping.alpha b/tests/carl/NoErrors/type.mapping.alpha new file mode 100644 index 0000000..8b6bc50 --- /dev/null +++ b/tests/carl/NoErrors/type.mapping.alpha @@ -0,0 +1,2 @@ +type M : integer -> character + diff --git a/tests/carl/NoErrors/type.mapping.alpha.asc b/tests/carl/NoErrors/type.mapping.alpha.asc new file mode 100644 index 0000000..2ce8f0c --- /dev/null +++ b/tests/carl/NoErrors/type.mapping.alpha.asc @@ -0,0 +1,4 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file type.mapping.alpha +001: type M : integer -> character +002: +003: diff --git a/tests/carl/NoErrors/type.record.alpha b/tests/carl/NoErrors/type.record.alpha new file mode 100644 index 0000000..2c5e9c9 --- /dev/null +++ b/tests/carl/NoErrors/type.record.alpha @@ -0,0 +1 @@ +type R : [ integer : i ; character : c ] diff --git a/tests/carl/NoErrors/type.record.alpha.asc b/tests/carl/NoErrors/type.record.alpha.asc new file mode 100644 index 0000000..848948b --- /dev/null +++ b/tests/carl/NoErrors/type.record.alpha.asc @@ -0,0 +1,3 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file type.record.alpha +001: type R : [ integer : i ; character : c ] +002: diff --git a/tests/carl/NoErrors/types.alpha b/tests/carl/NoErrors/types.alpha new file mode 100644 index 0000000..0402f95 --- /dev/null +++ b/tests/carl/NoErrors/types.alpha @@ -0,0 +1,75 @@ +(* + + At compiler start-up your program should + create symbol table entries for the following + built-in types: + + Boolean (1 byte) + character (1 byte) + integer (4 bytes) + address (8 bytes) + + and the following privileged type (it has literals): + + type string: 1 -> character + + Your compiler can define other types during + its start-up routine as well, if it is helpful + to do so. + +*) + + + +type BooleanXBoolean: [Boolean: x; Boolean: y] +type characterXcharacter: [character: x; character: y] +type integerXinteger: [integer: x; integer: y] + +type Boolean2Boolean: Boolean -> Boolean +type integer2integer: integer -> integer + +type character2integer: character -> integer +type Boolean2integer: Boolean -> integer +type string2integer: string -> integer + +type integerXinteger2integer: integerXinteger -> integer + +type integerXinteger2Boolean: integerXinteger -> Boolean +type characterXcharacter2Boolean: characterXcharacter -> Boolean +type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean + + +type integer2address: integer -> address +type address2integer: address -> integer + + +(* The alpha library functions + You will be provided with x86-64 assembly + code implementations of these. +*) + +external function printInteger: integer2integer +external function printCharacter: character2integer +external function printBoolean: Boolean2integer + +(* + A declaration of the entry point function for your program + + You may assume that when starting this function will behave as if + it had been called from the C language main function like this: + + int main(int argc, char * argv[]) { + if (argc == 1) { + return entry(NULL); + } + else { + return entry(makeAlphaString(argv[1])); + } + } + + for some suitable definition of makeAlphaString which creates + an alpha string representation of its argument C string in memory + and returns a pointer to that alpha string. +*) + +function entry: string2integer diff --git a/tests/carl/NoErrors/types.alpha.asc b/tests/carl/NoErrors/types.alpha.asc new file mode 100644 index 0000000..8844865 --- /dev/null +++ b/tests/carl/NoErrors/types.alpha.asc @@ -0,0 +1,77 @@ +alpha parser, version 0.2 (2023-03-04) - Annotated Source Code for file types.alpha +001: (* +001: +002: At compiler start-up your program should +003: create symbol table entries for the following +004: built-in types: +005: +006: Boolean (1 byte) +007: character (1 byte) +008: integer (4 bytes) +009: address (8 bytes) +010: +011: and the following privileged type (it has literals): +012: +013: type string: 1 -> character +014: +015: Your compiler can define other types during +016: its start-up routine as well, if it is helpful +017: to do so. +018: +019: *) +021: +022: +023: +024: type BooleanXBoolean: [Boolean: x; Boolean: y] +025: type characterXcharacter: [character: x; character: y] +026: type integerXinteger: [integer: x; integer: y] +027: +028: type Boolean2Boolean: Boolean -> Boolean +029: type integer2integer: integer -> integer +030: +031: type character2integer: character -> integer +032: type Boolean2integer: Boolean -> integer +033: type string2integer: string -> integer +034: +035: type integerXinteger2integer: integerXinteger -> integer +036: +037: type integerXinteger2Boolean: integerXinteger -> Boolean +038: type characterXcharacter2Boolean: characterXcharacter -> Boolean +039: type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean +040: +041: +042: type integer2address: integer -> address +043: type address2integer: address -> integer +044: +045: +046: (* The alpha library functions +047: You will be provided with x86-64 assembly +048: code implementations of these. +049: *) +050: +051: external function printInteger: integer2integer +052: external function printCharacter: character2integer +053: external function printBoolean: Boolean2integer +054: +055: (* +056: A declaration of the entry point function for your program +057: +058: You may assume that when starting this function will behave as if +059: it had been called from the C language main function like this: +060: +061: int main(int argc, char * argv[]) { +062: if (argc == 1) { +063: return entry(NULL); +064: } +065: else { +066: return entry(makeAlphaString(argv[1])); +067: } +068: } +069: +070: for some suitable definition of makeAlphaString which creates +071: an alpha string representation of its argument C string in memory +072: and returns a pointer to that alpha string. +073: *) +074: +075: function entry: string2integer +076: From b7c6ebb3f2f75ca9fbcc17d77262297817390ed2 Mon Sep 17 00:00:00 2001 From: Annie Date: Tue, 15 Apr 2025 15:41:46 -0400 Subject: [PATCH 41/45] fixed -tok handling --- src/runner.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runner.c b/src/runner.c index 5aedc59..4962b98 100644 --- a/src/runner.c +++ b/src/runner.c @@ -106,8 +106,8 @@ int run(FILE *alpha) { yyin = alpha; // TOK FLAG - if (tok_flag != NULL) { - while (0 != (token = yylex())) { +// if (tok_flag != NULL) { + // while (0 != (token = yylex())) { // if (tok_flag != NULL) { // fprintf(tok_flag, "%d %d %3d \"%s\"\n", // line_number, column_number, @@ -131,7 +131,7 @@ int run(FILE *alpha) { yytext); } column_number += yyleng; */ - } + /* } fclose(tok_flag); if (yyin != NULL) { @@ -139,7 +139,7 @@ int run(FILE *alpha) { } return 0; } - +*/ if (st_flag != NULL) { yyparse(); From b325548b970b449f481cb274ceace09b8d2565ff Mon Sep 17 00:00:00 2001 From: Scarlett Date: Tue, 15 Apr 2025 16:25:51 -0400 Subject: [PATCH 42/45] flag updates --- src/runner.c | 358 ++++++++++++++++++++++++--------------------------- src/runner.h | 32 +++-- 2 files changed, 184 insertions(+), 206 deletions(-) diff --git a/src/runner.c b/src/runner.c index 4962b98..ba7f64e 100644 --- a/src/runner.c +++ b/src/runner.c @@ -4,242 +4,214 @@ #include "runner.h" 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; + } - if (argc == 1) { - fprintf(stderr, INVALID); - return -1; + else if (argc == 2) { + if (is_help(argv[1])) { + return 0; + } else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) { + no_flag = SET_FLAG; + alpha_file = fopen(argv[1], "r"); + } else { + fprintf(stderr, INVALID); + return -1; } + } - else if (argc == 2) { - if (is_help(argv[1])) { - return 0; - } else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) { - no_flag = SET_FLAG; - alpha_file = fopen(argv[1], "r"); - } else { - fprintf(stderr, INVALID); - return -1; + else { + if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != + 0) { + fprintf(stderr, INVALID); + return -1; + } else { + for (int i = 1; i < argc - 1; i++) { + if (check_flag(argv[i], argv[argc - 1]) != 0) { + fprintf(stderr, INVALID); + return -1; } + } + alpha_file = fopen(argv[argc - 1], "r"); } - - else { - if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != - 0) { - fprintf(stderr, INVALID); - return -1; - } else { - for (int i = 1; i < argc - 1; i++) { - if (check_flag(argv[i], argv[argc - 1]) != 0) { - fprintf(stderr, - "INVALID FLAG(S): Use -help to " - "view valid inputs \n"); - return -1; - } - } - alpha_file = fopen(argv[argc - 1], "r"); - } - } - return run(alpha_file); + } + return run(alpha_file); } int check_flag(char *arg, char *alpha) { - if (strcmp("-tok", arg) == 0) { - if (tok_flag == NULL) { - return new_file(arg, alpha); - } - fprintf(stderr, "FLAGS REPEAT\n"); - return -1; - } else if (strcmp("-st", arg) == 0) { - if (st_flag == NULL) { - return new_file(arg, alpha); - } - fprintf(stderr, "FLAGS REPEAT\n"); - return -1; - - } else if (strcmp("-tc", arg) == 0) { - if (tc_flag == NULL) { - return new_file(arg, alpha); - } - } else if (strcmp("-debug", arg) == 0) { - DEBUG = true; - return 0; - } else { - fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n"); - return -1; + if (strcmp("-tok", arg) == 0) { + if (tok_flag == NULL) { + return new_file(arg, alpha); } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-st", arg) == 0) { + if (st_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-asc", arg) == 0) { + if (asc_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-tc", arg) == 0) { + if (tc_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-ir", arg) == 0) { + if (ir_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-cg", arg) == 0) { + if (cg_flag == NULL) { + return new_file(arg, alpha); + } + fprintf(stderr, "FLAGS REPEAT\n"); + return -1; + } else if (strcmp("-debug", arg) == 0) { + DEBUG = true; + return 0; + } else { + fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n"); + return -1; + } } void incr(int lnum, int cnum, int tok) { - // if (tok == COMMENT) { - for (int i = 0; i < yyleng; i++) { - if (yytext[i] == '\n') { - line_number++; - column_number = 0; - } - column_number++; + for (int i = 0; i < yyleng; i++) { + if (yytext[i] == '\n') { + line_number++; + column_number = 0; } - // }else{ - // column_number += yyleng; - // } + column_number++; + } } void print_tok(int tok) { - fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, - yytext); + fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, yytext); } int run(FILE *alpha) { - int token; - top = cur = init(CreateScope(NULL, 1, 1)); + int token; + top = cur = init(CreateScope(NULL, 1, 1)); - // If file is not found - if (alpha == NULL) { - fprintf(stderr, "INPUT FILE NOT FOUND\n"); - return -1; - } - yyin = alpha; + // If file is not found + if (alpha == NULL) { + fprintf(stderr, "INPUT FILE NOT FOUND\n"); + return -1; + } + yyin = alpha; + yyparse(); - // TOK FLAG -// if (tok_flag != NULL) { - // while (0 != (token = yylex())) { - // if (tok_flag != NULL) { - // fprintf(tok_flag, "%d %d %3d \"%s\"\n", - // line_number, column_number, - // token, yytext); - // } - /*if (token == COMMENT) { - for (int i = 0; i < yyleng; i++) { - if (yytext[i] == '\n') { - line_number++; - column_number = 0; - } - column_number++; - } - continue; - } - if (token == 1999) { - printf( - "On line number %d and column - number %d we have an invalid " "character:%s\n", - line_number, column_number, - yytext); - } - column_number += yyleng; */ - /* } - fclose(tok_flag); + if (st_flag != NULL) { + print_symbol_table(top, st_flag); + fclose(st_flag); + } - if (yyin != NULL) { - fclose(yyin); - } - return 0; - } -*/ - if (st_flag != NULL) { - yyparse(); + if (asc_flag != NULL) { + printf("Flag -asc is not implemented yet\n"); + fclose(asc_flag); + } - if (cur == NULL) { - printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED); - } + if (tc_flag != NULL) { + printf("Flag -tc is not implemented yet\n"); + fclose(tc_flag); + } - if (top == NULL) { - printdebug("%s[FATAL] top is null", COLOR_LIGHTRED); - } + if (ir_flag != NULL) { + printf("Flag -ir is not implemented yet\n"); + fclose(ir_flag); + } + if (cg_flag != NULL) { + printf("Flag -cg is not implemented yet\n"); + fclose(cg_flag); + } - print_symbol_table(top, st_flag); - fclose(st_flag); - if (yyin != NULL) { - fclose(yyin); - } - return 0; - } + yyparse(); - if (tc_flag != NULL) { - //SCARLETT NEEDS TO ADD THIS FUNCTIONALITY - } + if (yyin != NULL) { + fclose(yyin); + } - yyparse(); - FILE *f = fdopen(1, "w"); - - print_symbol_table(top, f); - fclose(f); - - if (yyin != NULL) { - fclose(yyin); - } - - return 0; + return 0; } bool is_help(char *input) { - if (strcmp(input, "-help") == 0) { - printf("%s", HELP); - return true; - } + if (strcmp(input, "-help") == 0) { + printf("%s", HELP); + return true; + } - return false; + return false; } int new_file(char *arg, char *alpha) { - int type_len; - const char *basename = alpha; - const char *slash = strchr(alpha, '/'); + int type_len; + const char *basename = alpha; + const char *slash = strchr(alpha, '/'); - while (slash != NULL) { - basename = slash + 1; - slash = strchr(basename, '/'); - } + while (slash != NULL) { + basename = slash + 1; + slash = strchr(basename, '/'); + } - mkdir("./out", 0777); + mkdir("./out", 0777); - char *new_basename = calloc(strlen(basename) + 5, sizeof(char)); - strcpy(new_basename, "./out/"); - strcat(new_basename, basename); - basename = new_basename; + char *new_basename = calloc(strlen(basename) + 5, sizeof(char)); + strcpy(new_basename, "./out/"); + strcat(new_basename, basename); + basename = new_basename; - if (strcmp(arg, "-tok") == 0) { - type_len = TOK_LEN; - } else if (strcmp(arg, "-st") == 0) { - type_len = ST_LEN; - } else if (strcmp(arg, "-tc") == 0){ - type_len = TC_LEN; - }else { - fprintf(stderr, - "INVALID FLAG: Use -help to view valid inputs\n"); - return -1; - } + if (strcmp(arg, "-tok") == 0) { + type_len = TOK_LEN; + } else if (strcmp(arg, "-st") == 0) { + type_len = ST_LEN; + } else if (strcmp(arg, "-tc") == 0) { + type_len = TC_LEN; + } else { + fprintf(stderr, INVALID); + return -1; + } - // calculate lengths - int basename_len = strlen(basename); - char *file_name = - calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char)); + // calculate lengths + int basename_len = strlen(basename); + char *file_name = + calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char)); - // coy filename and add extension - strncpy(file_name, basename, basename_len - ALPHA_OFFSET); - strcat(file_name, "."); - strcat(file_name, arg + 1); + // coy filename and add extension + strncpy(file_name, basename, basename_len - ALPHA_OFFSET); + strcat(file_name, "."); + strcat(file_name, arg + 1); - if (strcmp(arg, "-tok") == 0) { - tok_flag = fopen(file_name, "w"); - } else if (strcmp(arg, "-st") == 0) { - st_flag = fopen(file_name, "w"); - - } else if (strcmp(arg, "-tc") == 0) { - tc_flag = fopen(file_name, "w"); - } - return 0; + if (strcmp(arg, "-tok") == 0) { + tok_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-st") == 0) { + st_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-asc") == 0) { + asc_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-tc") == 0) { + tc_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-ir") == 0) { + ir_flag = fopen(file_name, "w"); + } else if (strcmp(arg, "-cg") == 0) { + cg_flag = fopen(file_name, "w"); + } + return 0; } int is_alpha_file(char *alpha, int file_len) { - if (strcmp(".alpha", - alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) { - return -1; // not alpha file - } - return 0; // is alpha file + if (strcmp(".alpha", + alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) { + return -1; // not alpha file + } + return 0; // is alpha file } diff --git a/src/runner.h b/src/runner.h index 0483e12..0a878d9 100644 --- a/src/runner.h +++ b/src/runner.h @@ -5,20 +5,23 @@ #define TOK_LEN 3 #define ST_LEN 2 #define TC_LEN 2 -#define HELP \ - "HELP:\nHow to run the alpha compiler:\n./alpha [options] " \ - "program\nValid " \ - "options:\n-tok output the token number, token, line number, and " \ - "column " \ - "number for each of the tokens to the .tok file\n-st output the " \ - "symbol " \ - "table for the program to the .st file\n-help print this message " \ - "and exit " \ - "the alpha compiler\n-debug print debugging messages in grammar rules \n" +#define HELP \ + "HELP:\n" \ + " How to run the alpha compiler:\n" \ + " ./alpha [options] program\n" \ + "Valid options:\n" \ + " -tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n" \ + " -st output the symbol table for the program to the .st file\n" \ + " -asc output the annotated source code for the program to the .asc file, including syntax errors\n" \ + " -tc run the type checker and report type errors to the .asc file\n" \ + " -ir run the intermediate representation generator, writing output to the .ir file\n" \ + " -cg run the (x86 assembly) code generator, writing output to the .s file\n" \ + " -debug produce debugging messages to stderr\n" \ + " -help print this message and exit the alpha compiler\n" -#define SET_FLAG 1 // Used to set flags for arg types -#define INVALID \ - "INVALID INPUT: Include a .alpha file or use -help for more inputs \n" +#define SET_FLAG 1 // Used to set flags for arg types +#define INVALID \ + "INVALID INPUT: Include a .alpha file or use -help for more inputs \n" #include #include @@ -42,6 +45,9 @@ FILE *alpha_file; FILE *tok_flag = NULL; FILE *st_flag = NULL; FILE *tc_flag = NULL; +FILE *ir_flag = NULL; +FILE *cg_flag = NULL; +FILE *asc_flag = NULL; bool DEBUG = false; int no_flag = 0; int arg; From 90f9eb2f006a3136caa39aa61896dc0cd78f83c4 Mon Sep 17 00:00:00 2001 From: Partho Date: Tue, 15 Apr 2025 19:44:46 -0400 Subject: [PATCH 43/45] added entries for reserve and release --- src/grammar.y | 1 + src/symbol_table.c | 28 ++++++++++++++++++++++++++-- tests/sprint2/test/sp2_simple.alpha | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 4e2aa1d..0a03126 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -392,6 +392,7 @@ sblock: printdebug("Moving up a scope after seeing sblock with dblock"); cur = getParent(cur); //$$ = $5; + } R_BRACE {$$ = $5;} diff --git a/src/symbol_table.c b/src/symbol_table.c index 4232c16..cd44b40 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -605,20 +605,31 @@ SymbolTable *init(SymbolTable *start) { chara = (TableNode *)calloc(1, sizeof(TableNode)); stri = (TableNode *)calloc(1, sizeof(TableNode)); boo = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode* reservetype = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode* reserve = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode* releasetype = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode* release = (TableNode *)calloc(1, sizeof(TableNode)); // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); start->entries = integ; integ->next = addr; addr->next = chara; chara->next = stri; stri->next = boo; - // boo->next = arr; - boo->next = NULL; + boo->next = reservetype; + reservetype->next = reserve; + reserve->next = releasetype; + releasetype->next = release; + release->next = NULL; integ->theName = "integer"; addr->theName = "address"; chara->theName = "character"; boo->theName = "Boolean"; stri->theName = "string"; + reserve->theName = "reserve"; + reservetype->theName = "reserve type"; + releasetype->theName = "release type"; + release->theName = "release"; // arr->theName= "array" // root TableNode that all are pointing to but not in table @@ -685,6 +696,11 @@ SymbolTable *init(SymbolTable *start) { chara->theType = prime; stri->theType = arrayprim; boo->theType = prime; + reserve->theType = reservetype; + reservetype->theType = funtypeprime; + releasetype->theType = funtypeprime; + release->theType = releasetype; + // arr->theType=arrayprim; // filling in all the values for the additional info for initial types @@ -697,12 +713,20 @@ SymbolTable *init(SymbolTable *start) { chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR); stri->additionalinfo = CreateArrayInfo(1, chara); boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); + reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false); + reservetype->additionalinfo = CreateFunctionTypeInfo(integ, addr); + releasetype->additionalinfo = CreateFunctionTypeInfo(addr, integ); + release->additionalinfo = CreateFunctionDeclarationInfo(0, false); integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr chara->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for chara stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri boo->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for boo + reserve->tag = TYPE_FUNCTION_DECLARATION; + reservetype->tag = TYPE_FUNCTION_TYPE; + releasetype->tag = TYPE_FUNCTION_TYPE; + release->tag = TYPE_FUNCTION_DECLARATION; // addr->additionalinfo = CreatePrimitiveInfo(8); start->Line_Number = 1; diff --git a/tests/sprint2/test/sp2_simple.alpha b/tests/sprint2/test/sp2_simple.alpha index 825fdcd..58934e6 100644 --- a/tests/sprint2/test/sp2_simple.alpha +++ b/tests/sprint2/test/sp2_simple.alpha @@ -4,4 +4,4 @@ function entry: main entry(arg) := { [integer : x] return 0; -} +} \ No newline at end of file From b023ac01330bfd5997347e601f5ade9f4e9ec9a8 Mon Sep 17 00:00:00 2001 From: Partho Date: Tue, 15 Apr 2025 20:22:24 -0400 Subject: [PATCH 44/45] commented out second yyparse() in runner in run --- src/grammar.y | 2 +- src/runner.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 0a03126..c6044fd 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -896,7 +896,7 @@ assignable: $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3); } - printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName((TableNode*)($$)), $1); + printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName((TableNode*)($$)), getName($1)); } ; diff --git a/src/runner.c b/src/runner.c index ba7f64e..03e1933 100644 --- a/src/runner.c +++ b/src/runner.c @@ -136,7 +136,7 @@ int run(FILE *alpha) { fclose(cg_flag); } - yyparse(); + //yyparse(); if (yyin != NULL) { fclose(yyin); From 5a23ef275696d5f34b3aabd51e15aed2ef835f1f Mon Sep 17 00:00:00 2001 From: Scarlett Date: Wed, 16 Apr 2025 11:44:02 -0400 Subject: [PATCH 45/45] Fixed -tok, spacings in -st, and validation tests --- src/runner.c | 9 +- src/runner.h | 8 +- src/symbol_table.c | 120 ++++++++-------- src/symbol_table.h | 89 ++++++------ .../expected/sp2_carls_mistake.expected | 66 ++++----- .../expected/sp2_function_types.expected | 55 ++++---- .../expected/sp2_integer_binary_op.expected | 38 +++--- ...sp2_invalid_multiple_params_no_as.expected | 20 +++ .../expected/sp2_invalid_recop.expected | 37 ++--- .../expected/sp2_invalid_release.expected | 31 +++-- tests/sprint2/expected/sp2_library.expected | 75 +++++----- tests/sprint2/expected/sp2_llnode.expected | 128 +++++++++--------- tests/sprint2/expected/sp2_one_line.expected | 59 ++++---- .../sprint2/expected/sp2_presidence.expected | 36 +++-- tests/sprint2/expected/sp2_simple.expected | 28 ++-- .../expected/sp2_sp2_arrayargs.expected | 17 +++ .../sp2_valid_assignable_and_mem.expected | 39 +++--- .../expected/sp3_and_or_type_check.expected | 34 +++++ .../sp3_boolean_binary_op_typecheck.expected | 37 ++--- .../sp3_boolean_unary_op_typecheck.expected | 37 ++--- .../sp3_carls_second_mistake.expected | 37 ++--- .../sp3_integer_binary_op_typecheck.expected | 44 +++--- .../sp3_integer_unary_op_typecheck.expected | 37 ++--- .../expected/sp3_multiple_args.expected | 25 ++++ .../sp3_primitive_type_check.expected | 19 +++ .../expected/sp3_record_size_check.expected | 29 ++++ 26 files changed, 677 insertions(+), 477 deletions(-) create mode 100644 tests/sprint2/expected/sp2_invalid_multiple_params_no_as.expected create mode 100644 tests/sprint2/expected/sp2_sp2_arrayargs.expected create mode 100644 tests/sprint3/expected/sp3_and_or_type_check.expected create mode 100644 tests/sprint3/expected/sp3_multiple_args.expected create mode 100644 tests/sprint3/expected/sp3_primitive_type_check.expected create mode 100644 tests/sprint3/expected/sp3_record_size_check.expected diff --git a/src/runner.c b/src/runner.c index 03e1933..6915d29 100644 --- a/src/runner.c +++ b/src/runner.c @@ -111,6 +111,13 @@ int run(FILE *alpha) { yyin = alpha; yyparse(); + if (tok_flag != NULL) { + while (0 != (token = yylex())) { + // Don't delete me 🥺 + } + fclose(tok_flag); + } + if (st_flag != NULL) { print_symbol_table(top, st_flag); fclose(st_flag); @@ -136,8 +143,6 @@ int run(FILE *alpha) { fclose(cg_flag); } - //yyparse(); - if (yyin != NULL) { fclose(yyin); } diff --git a/src/runner.h b/src/runner.h index 0a878d9..3e71a7a 100644 --- a/src/runner.h +++ b/src/runner.h @@ -5,11 +5,11 @@ #define TOK_LEN 3 #define ST_LEN 2 #define TC_LEN 2 -#define HELP \ - "HELP:\n" \ +#define HELP \ + "HELP:\n" \ " How to run the alpha compiler:\n" \ - " ./alpha [options] program\n" \ - "Valid options:\n" \ + " ./alpha [options] program\n" \ + "Valid options:\n" \ " -tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n" \ " -st output the symbol table for the program to the .st file\n" \ " -asc output the annotated source code for the program to the .asc file, including syntax errors\n" \ diff --git a/src/symbol_table.c b/src/symbol_table.c index cd44b40..c1d3946 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -92,9 +92,9 @@ int getNumArrDim(TableNode *definition) { "function. Invalid."); return -1; } - if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){ + if (getAdInfoType(definition) != TYPE_ARRAY_TYPE) { printdebug( - "passed an invalid node to getNumArrDim. Seeing tag %d in getNumArrDim. Invalid.",getAdInfoType(definition)); + "passed an invalid node to getNumArrDim. Seeing tag %d in getNumArrDim. Invalid.", getAdInfoType(definition)); return -1; } return definition->additionalinfo->ArrayAdInfo->numofdimensions; @@ -114,9 +114,9 @@ TableNode *getArrType(TableNode *definition) { "function. Invalid."); return undefined; } - if(getAdInfoType(definition) != TYPE_ARRAY_TYPE){ + if (getAdInfoType(definition) != TYPE_ARRAY_TYPE) { printdebug( - "passed an invalid node to getArrType. Seeing tag %d. Invalid.",getAdInfoType(definition)); + "passed an invalid node to getArrType. Seeing tag %d. Invalid.", getAdInfoType(definition)); return undefined; } return definition->additionalinfo->ArrayAdInfo->typeofarray; @@ -138,25 +138,25 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { // Perhaps this may not be needed since we need to iterate over all elements // anyways. -int getRecTotal(TableNode* node){ - if(node == NULL){ +int getRecTotal(TableNode *node) { + if (node == NULL) { printdebug( "passed a NULL node to getRecTotal. Invalid."); - return -1; + return -1; } - if(getAdInfoType(node) != TYPE_RECORD_TYPE){ + if (getAdInfoType(node) != TYPE_RECORD_TYPE) { printdebug( "passed an invalid node to getRecTotal. Invalid."); return -1; } - if(node->additionalinfo == NULL){ + if (node->additionalinfo == NULL) { printdebug( "node has NULL additionalinfo. Invalid."); return -1; } return node->additionalinfo->RecAdInfo->total_size; } -TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { +TableNode *setRecOffsetInfo(SymbolTable *scope, TableNode *node) { if (node == NULL) { printdebug( "passed a NULL node to setRecOffsetInfo. Invalid."); @@ -167,51 +167,47 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { "passed an NULL scope to setRecOffsetInfo. Invalid."); return undefined; } - if(getFirstEntry(scope) == NULL){ + if (getFirstEntry(scope) == NULL) { printdebug( "passed an empty scope to setRecOffsetInfo. Invalid."); return undefined; } - TableNode* this = getFirstEntry(scope); + TableNode *this = getFirstEntry(scope); int largest = 0; int k = getRecLength(node); int total_size = 0; int counter = 0; int *offsets = (int *)calloc(2 * k, sizeof(int)); - if(getAdInfoType(this) == TYPE_FUNCTION_DECLARATION){ + if (getAdInfoType(this) == TYPE_FUNCTION_DECLARATION) { offsets[counter] = 8; total_size = total_size + offsets[counter]; largest = 8; counter++; - } - else if((getAdInfoType(this) == TYPE_RECORD)){ + } else if ((getAdInfoType(this) == TYPE_RECORD)) { offsets[counter] = 8; printf("hitting record and adding to largest"); total_size = total_size + offsets[counter]; largest = offsets[counter]; counter++; - } - else if(getAdInfoType(this)==TYPE_PRIMITIVE){ + } else if (getAdInfoType(this) == TYPE_PRIMITIVE) { offsets[counter] = getPrimSize(getTypeEntry(this)); total_size = total_size + offsets[counter]; largest = offsets[counter]; counter++; - } - else if(getAdInfoType(this)==TYPE_ARRAY){ + } else if (getAdInfoType(this) == TYPE_ARRAY) { offsets[counter] = 8; total_size = total_size + offsets[counter]; largest = offsets[counter]; counter++; - } - else { + } else { printdebug( - "[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d. Type of entry is %s. Name attempted to pass is %s.",getAdInfoType(this),getType(this),getName(this)); + "[TYPE CHECK] passed an invalid (first) parameter to a function definition. seeing %d. Type of entry is %s. Name attempted to pass is %s.", getAdInfoType(this), getType(this), getName(this)); return undefined; } this = getNextEntry(this); - while(this != NULL){ - if(getAdInfoType(this) == TYPE_FUNCTION_DECLARATION){ + while (this != NULL) { + if (getAdInfoType(this) == TYPE_FUNCTION_DECLARATION) { int s = 8; if (s > largest) { largest = s; @@ -225,8 +221,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { total_size = total_size + offsets[counter]; counter++; this = getNextEntry(this); - } - else if(getAdInfoType(this) == TYPE_ARRAY){ + } else if (getAdInfoType(this) == TYPE_ARRAY) { int s = 8; if (s > largest) { largest = s; @@ -240,8 +235,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { total_size = total_size + offsets[counter]; counter++; this = getNextEntry(this); - } - else if((getAdInfoType(this) == TYPE_RECORD)){ + } else if ((getAdInfoType(this) == TYPE_RECORD)) { int s = 8; if (s > largest) { largest = s; @@ -257,8 +251,7 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { total_size = total_size + offsets[counter]; counter++; this = getNextEntry(this); - } - else if(getAdInfoType(this) == TYPE_PRIMITIVE){ + } else if (getAdInfoType(this) == TYPE_PRIMITIVE) { int s = getPrimSize(getTypeEntry(this)); if (s > largest) { largest = s; @@ -272,9 +265,9 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { total_size = total_size + offsets[counter]; counter++; this = getNextEntry(this); - }else{ + } else { printdebug( - "[TYPE CHECK] passed an invalid parameter at position %d in record.",((counter+1)/2)); + "[TYPE CHECK] passed an invalid parameter at position %d in record.", ((counter + 1) / 2)); return undefined; } } @@ -286,18 +279,18 @@ TableNode *setRecOffsetInfo(SymbolTable* scope, TableNode *node) { return node; } -int* getRecOffsets(TableNode* node){ - if(node == NULL){ +int *getRecOffsets(TableNode *node) { + if (node == NULL) { printdebug( "passed a NULL node to getRecTotal. Invalid."); - return NULL; + return NULL; } - if(getAdInfoType(node) != TYPE_RECORD_TYPE){ + if (getAdInfoType(node) != TYPE_RECORD_TYPE) { printdebug( "passed an invalid node to getRecTotal. Invalid."); return NULL; } - if(node->additionalinfo == NULL){ + if (node->additionalinfo == NULL) { printdebug( "node has NULL additionalinfo. Invalid."); return NULL; @@ -341,7 +334,8 @@ SymbolTable *getRecList(TableNode *definition) { if (strcmp(getType(definition), "record") != 0) { printdebug( "not checking the list of types of a record -- invalid " - "op of type %s", getType(definition)); + "op of type %s", + getType(definition)); return NULL; } return definition->additionalinfo->RecAdInfo->recordScope; @@ -521,7 +515,7 @@ TableNode *getParameter(TableNode *definition) { "function. Invalid."); return undefined; } - if(definition->additionalinfo == NULL){ + if (definition->additionalinfo == NULL) { printdebug( "node has NULL additionalinfo. Invalid."); return undefined; @@ -552,7 +546,7 @@ TableNode *getReturn(TableNode *definition) { "not checking the return of a function -- invalid op"); return undefined; } - if(definition->additionalinfo == NULL){ + if (definition->additionalinfo == NULL) { printdebug( "node has NULL additionalinfo. Invalid."); return undefined; @@ -605,10 +599,10 @@ SymbolTable *init(SymbolTable *start) { chara = (TableNode *)calloc(1, sizeof(TableNode)); stri = (TableNode *)calloc(1, sizeof(TableNode)); boo = (TableNode *)calloc(1, sizeof(TableNode)); - TableNode* reservetype = (TableNode *)calloc(1, sizeof(TableNode)); - TableNode* reserve = (TableNode *)calloc(1, sizeof(TableNode)); - TableNode* releasetype = (TableNode *)calloc(1, sizeof(TableNode)); - TableNode* release = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode *reservetype = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode *reserve = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode *releasetype = (TableNode *)calloc(1, sizeof(TableNode)); + TableNode *release = (TableNode *)calloc(1, sizeof(TableNode)); // TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); start->entries = integ; integ->next = addr; @@ -721,7 +715,7 @@ SymbolTable *init(SymbolTable *start) { integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr chara->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for chara - stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri + stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri boo->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for boo reserve->tag = TYPE_FUNCTION_DECLARATION; reservetype->tag = TYPE_FUNCTION_TYPE; @@ -1064,7 +1058,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) { // check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { - printdebug("Could not find %s in any scope using lookup",x); + printdebug("Could not find %s in any scope using lookup", x); return undefined; } TableNode *ret = table_lookup(table, x); @@ -1157,33 +1151,33 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } } if (getAdInfoType(entry) == TYPE_ARRAY) { - char *arrayType = (char *)malloc(100); - //sprintf(arrayType, " %d -> %s", getNumArrDim(entry), - // getName(getArrType(entry))); - + char *arrayType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(arrayType, " %s", getType(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Array Instance"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, arrayType, " Array Instance"); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Array Instance"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Array Instance"); } } if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Record Type", recordAdInfo); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " record type", recordAdInfo); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " Record Type", recordAdInfo); } } if (getAdInfoType(entry) == TYPE_RECORD) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d", getRecLength(entry)); + char *recordType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(recordType, " %s", getType(entry)); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), "record instance"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, recordType, " Record Instance"); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "record instance"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, recordType, " Record Instance"); } } @@ -1193,8 +1187,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive Type", primAdInfo); } else { - //printdebug("%sTHIS ONE", COLOR_RED); - printTableNode(entry); char *primType = (char *)malloc(sizeof(getType(entry) + 1)); sprintf(primType, " %s", getType(entry)); st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo); @@ -1206,11 +1198,9 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive", primAdInfo); } else { - printdebug("%sTHIS ONE", COLOR_RED); - printTableNode(entry); char *primType = (char *)malloc(sizeof(getType(entry) + 1)); sprintf(primType, " %s", getType(entry)); - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), "Primitive Instance"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, " Primitive Instance"); } } @@ -1226,10 +1216,12 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { } if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { + char *functiontype = (char *)malloc(100); + sprintf(functiontype, " %s", getName(getReturn(entry))); if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Function Definition"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition"); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Function Definition"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Function Definition"); } } @@ -1393,7 +1385,7 @@ TableNode *getNextEntry(TableNode *tn) { // Uses pointers to the table node to print the info TableNode *printTableNode(TableNode *tn) { if (DEBUG == 0) { - return tn; + return tn; } if (tn == NULL) { diff --git a/src/symbol_table.h b/src/symbol_table.h index 6a33974..01d4458 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -7,80 +7,79 @@ #define SIZE_INT 4 #define SIZE_ADDR 8 #define SIZE_CHAR 1 -#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be - +#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be struct TableNode; typedef struct { - int size; + int size; } primitive_info; typedef struct { - int numofdimensions; - struct TableNode *typeofarray; + int numofdimensions; + struct TableNode *typeofarray; } array_info; typedef struct { - int numofelements; - struct SymbolTable *recordScope; - int total_size; - int* offsets; + int numofelements; + struct SymbolTable *recordScope; + int total_size; + int *offsets; } record_info; typedef struct { - int startlinenumber; - bool regularoras; + int startlinenumber; + bool regularoras; } function_declaration_info; typedef struct { - struct TableNode *parameter; - struct TableNode *returntype; + struct TableNode *parameter; + struct TableNode *returntype; } function_type_info; typedef union { - primitive_info *PrimAdInfo; - array_info *ArrayAdInfo; - record_info *RecAdInfo; - function_declaration_info *FunDecAdInfo; - function_type_info *FunTypeAdInfo; + primitive_info *PrimAdInfo; + array_info *ArrayAdInfo; + record_info *RecAdInfo; + function_declaration_info *FunDecAdInfo; + function_type_info *FunTypeAdInfo; } AdInfo; typedef struct ListOfTable { - struct SymbolTable *table; - struct ListOfTable *next; + struct SymbolTable *table; + struct ListOfTable *next; } ListOfTable; //Table node to store typedef struct TableNode { - struct TableNode *theType; - int tag; - char *theName; - AdInfo *additionalinfo; - struct TableNode *next; + struct TableNode *theType; + int tag; + char *theName; + AdInfo *additionalinfo; + struct TableNode *next; } TableNode; typedef struct SymbolTable { - TableNode *entries; - struct SymbolTable *Parent_Scope; - struct ListOfTable *Children_Scope; - int Line_Number; - int Column_Number; + TableNode *entries; + struct SymbolTable *Parent_Scope; + struct ListOfTable *Children_Scope; + int Line_Number; + int Column_Number; } SymbolTable; typedef enum { - TYPE_STRING = 1, - TYPE_ARRAY_TYPE = 2, - TYPE_RECORD_TYPE = 3, - TYPE_FUNCTION_DECLARATION = 4, - TYPE_FUNCTION_TYPE = 5, - TYPE_PRIMITIVE = 6, - TYPE_ALL_ELSE = 7, - TYPE_UNDEFINED = 8, - TYPE_RECORD = 9, - TYPE_ARRAY = 10, - TYPE_SYSTEM_DEFINED = 11, - TYPE_PRIMITIVE_TYPE = 12 + TYPE_STRING = 1, + TYPE_ARRAY_TYPE = 2, + TYPE_RECORD_TYPE = 3, + TYPE_FUNCTION_DECLARATION = 4, + TYPE_FUNCTION_TYPE = 5, + TYPE_PRIMITIVE = 6, + TYPE_ALL_ELSE = 7, + TYPE_UNDEFINED = 8, + TYPE_RECORD = 9, + TYPE_ARRAY = 10, + TYPE_SYSTEM_DEFINED = 11, + TYPE_PRIMITIVE_TYPE = 12 } types; AdInfo *CreatePrimitiveInfo(int size); @@ -128,10 +127,10 @@ ListOfTable *getRestOfChildren(ListOfTable *lt); TableNode *getFirstEntry(SymbolTable *st); TableNode *getNextEntry(TableNode *tn); -TableNode * printTableNode(TableNode * tn); +TableNode *printTableNode(TableNode *tn); void printdebug_impl(char *file, int line, const char *format, ...); -#define printdebug(format, ...) \ - printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) +#define printdebug(format, ...) \ + printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) extern int yylex(void); extern char *yytext; diff --git a/tests/sprint2/expected/sp2_carls_mistake.expected b/tests/sprint2/expected/sp2_carls_mistake.expected index 23a8694..cee5d6f 100644 --- a/tests/sprint2/expected/sp2_carls_mistake.expected +++ b/tests/sprint2/expected/sp2_carls_mistake.expected @@ -1,31 +1,35 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -bar2 : 001001 : : T2 : User Defined -bar1 : 001001 : : T2 : User Defined -foo : 001001 : : T1 : User Defined -arr : 001001 : : 1 -> integer : Type of Array -T2 : 001001 : : primitive function type : User Defined -T1 : 001001 : : primitive function type : User Defined -rec : 001001 : : record : elements-2 -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -w : 026000 : 001001 : rec : User Defined -result : 026000 : 001001 : integer : User Defined -arg : 026000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -r : 021000 : 001001 : integer : User Defined -s : 021000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -a : 017000 : 001001 : rec : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -x : 013000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 004000 : 001001 : integer : User Defined -x : 004000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +bar2 : 001001 : : undefined : Function Definition +bar1 : 001001 : : undefined : Function Definition +foo : 001001 : : undefined : Function Definition +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +arr : 001001 : : 1 -> integer : Type of Array +T2 : 001001 : : rec -> integer : Type of Function +T1 : 001001 : : integer -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +w : 025000 : 001001 : rec : Record Instance +result : 025000 : 001001 : integer : Primitive Instance +arg : 025000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +r : 021000 : 001001 : integer : Primitive Instance +s : 021000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 017000 : 001001 : integer : Primitive Instance +y : 017000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 013000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_function_types.expected b/tests/sprint2/expected/sp2_function_types.expected index bd1d2ce..391690b 100644 --- a/tests/sprint2/expected/sp2_function_types.expected +++ b/tests/sprint2/expected/sp2_function_types.expected @@ -1,26 +1,29 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : string2integer : User Defined -integer2integer2integerFunc: 001001 : : integer2integer2integer : User Defined -released : 001001 : : address2integer : User Defined -reserved : 001001 : : integer2address : User Defined -printBoolean : 001001 : : Boolean2integer : User Defined -printCharacter : 001001 : : character2integer : User Defined -printInteger : 001001 : : integer2integer : User Defined -integer2integer2integer : 001001 : : primitive function type : User Defined -address2integer : 001001 : : primitive function type : User Defined -integer2address : 001001 : : primitive function type : User Defined -Boolean2Boolean2Boolean : 001001 : : primitive function type : User Defined -character2character2Boolean: 001001 : : primitive function type : User Defined -integer2integer2Boolean : 001001 : : primitive function type : User Defined -string2integer : 001001 : : primitive function type : User Defined -Boolean2integer : 001001 : : primitive function type : User Defined -character2integer : 001001 : : primitive function type : User Defined -integer2integer : 001001 : : primitive function type : User Defined -Boolean2Boolean : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +integer2integer2integerFunc : 001001 : : undefined : Function Definition +released : 001001 : : undefined : Function Definition +reserved : 001001 : : undefined : Function Definition +printBoolean : 001001 : : undefined : Function Definition +printCharacter : 001001 : : undefined : Function Definition +printInteger : 001001 : : undefined : Function Definition +integer2integer2integer : 001001 : : integer2integer -> integer : Type of Function +address2integer : 001001 : : address -> integer : Type of Function +integer2address : 001001 : : integer -> address : Type of Function +Boolean2Boolean2Boolean : 001001 : : Boolean2Boolean -> Boolean : Type of Function +character2character2Boolean : 001001 : : undefined -> undefined : Type of Function +integer2integer2Boolean : 001001 : : integer2integer -> Boolean : Type of Function +string2integer : 001001 : : string -> integer : Type of Function +Boolean2integer : 001001 : : Boolean -> integer : Type of Function +character2integer : 001001 : : character -> integer : Type of Function +integer2integer : 001001 : : integer -> integer : Type of Function +Boolean2Boolean : 001001 : : Boolean -> Boolean : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition diff --git a/tests/sprint2/expected/sp2_integer_binary_op.expected b/tests/sprint2/expected/sp2_integer_binary_op.expected index 299c09d..d24c170 100644 --- a/tests/sprint2/expected/sp2_integer_binary_op.expected +++ b/tests/sprint2/expected/sp2_integer_binary_op.expected @@ -1,17 +1,21 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -b1 : 005000 : 001001 : Boolean : User Defined -b2 : 005000 : 001001 : Boolean : User Defined -arr2 : 005000 : 001001 : address : User Defined -arr : 005000 : 001001 : address : User Defined -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +testarr : 001001 : : 1 -> integer : Type of Array +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +b1 : 005000 : 001001 : Boolean : Primitive Instance +b2 : 005000 : 001001 : Boolean : Primitive Instance +arr2 : 005000 : 001001 : testarr : Array Instance +arr : 005000 : 001001 : testarr : Array Instance +x : 005000 : 001001 : integer : Primitive Instance +arg : 005000 : 001001 : string : Array Instance diff --git a/tests/sprint2/expected/sp2_invalid_multiple_params_no_as.expected b/tests/sprint2/expected/sp2_invalid_multiple_params_no_as.expected new file mode 100644 index 0000000..509c6eb --- /dev/null +++ b/tests/sprint2/expected/sp2_invalid_multiple_params_no_as.expected @@ -0,0 +1,20 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +test : 001001 : : undefined : Function Definition +main : 001001 : : rec -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +arg : 005000 : 001001 : integer : Primitive Instance +arg2 : 005000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_invalid_recop.expected b/tests/sprint2/expected/sp2_invalid_recop.expected index 5274102..642ad85 100644 --- a/tests/sprint2/expected/sp2_invalid_recop.expected +++ b/tests/sprint2/expected/sp2_invalid_recop.expected @@ -1,17 +1,20 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -rec : 001001 : : record : elements-2 -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -w : 007000 : 001001 : rec : User Defined -arg : 007000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 004000 : 001001 : integer : User Defined -x : 004000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +rec : 001001 : : Record Type : elements-2 size-8 bytes +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +w : 006000 : 001001 : rec : Record Instance +arg : 006000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 004000 : 001001 : integer : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_invalid_release.expected b/tests/sprint2/expected/sp2_invalid_release.expected index de26852..1ac0274 100644 --- a/tests/sprint2/expected/sp2_invalid_release.expected +++ b/tests/sprint2/expected/sp2_invalid_release.expected @@ -1,14 +1,17 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -rec : 001001 : : record : elements-2 -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -w : 004000 : 001001 : rec : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 001000 : 001001 : integer : User Defined -x : 001000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +rec : 001001 : : Record Type : elements-2 size-8 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +w : 003000 : 001001 : rec : Record Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_library.expected b/tests/sprint2/expected/sp2_library.expected index a1318bd..80a9611 100644 --- a/tests/sprint2/expected/sp2_library.expected +++ b/tests/sprint2/expected/sp2_library.expected @@ -1,36 +1,39 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : string2integer : User Defined -printBoolean : 001001 : : Boolean2integer : User Defined -printCharacter : 001001 : : character2integer : User Defined -printInteger : 001001 : : integer2integer : User Defined -address2integer : 001001 : : primitive function type : User Defined -integer2address : 001001 : : primitive function type : User Defined -BooleanXBoolean2Boolean : 001001 : : primitive function type : User Defined -characterXcharacter2Boolean: 001001 : : primitive function type : User Defined -integerXinteger2Boolean : 001001 : : primitive function type : User Defined -integerXinteger2integer : 001001 : : primitive function type : User Defined -string2integer : 001001 : : primitive function type : User Defined -Boolean2integer : 001001 : : primitive function type : User Defined -character2integer : 001001 : : primitive function type : User Defined -integer2integer : 001001 : : primitive function type : User Defined -Boolean2Boolean : 001001 : : primitive function type : User Defined -integerXinteger : 001001 : : record : elements-2 -characterXcharacter : 001001 : : record : elements-2 -BooleanXBoolean : 001001 : : record : elements-2 -string : 001001 : : 1 -> character : Type of Array -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -y : 015000 : 001001 : integer : User Defined -x : 015000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 014000 : 001001 : character : User Defined -x : 014000 : 001001 : character : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 013000 : 001001 : Boolean : User Defined -x : 013000 : 001001 : Boolean : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +printBoolean : 001001 : : undefined : Function Definition +printCharacter : 001001 : : undefined : Function Definition +printInteger : 001001 : : undefined : Function Definition +address2integer : 001001 : : address -> integer : Type of Function +integer2address : 001001 : : integer -> address : Type of Function +BooleanXBoolean2Boolean : 001001 : : BooleanXBoolean -> Boolean : Type of Function +characterXcharacter2Boolean : 001001 : : characterXcharacter -> Boolean : Type of Function +integerXinteger2Boolean : 001001 : : integerXinteger -> Boolean : Type of Function +integerXinteger2integer : 001001 : : integerXinteger -> integer : Type of Function +string2integer : 001001 : : string -> integer : Type of Function +Boolean2integer : 001001 : : Boolean -> integer : Type of Function +character2integer : 001001 : : character -> integer : Type of Function +integer2integer : 001001 : : integer -> integer : Type of Function +Boolean2Boolean : 001001 : : Boolean -> Boolean : Type of Function +integerXinteger : 001001 : : Record Type : elements-2 size-8 bytes +characterXcharacter : 001001 : : Record Type : elements-2 size-2 bytes +BooleanXBoolean : 001001 : : Record Type : elements-2 size-8 bytes +string : 001001 : : 1 -> character : Type of Array +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 015000 : 001001 : integer : Primitive Instance +x : 015000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 014000 : 001001 : character : Primitive Instance +x : 014000 : 001001 : character : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 013000 : 001001 : Boolean : Primitive Instance +x : 013000 : 001001 : Boolean : Primitive Instance diff --git a/tests/sprint2/expected/sp2_llnode.expected b/tests/sprint2/expected/sp2_llnode.expected index dfe8d53..ab13e6f 100644 --- a/tests/sprint2/expected/sp2_llnode.expected +++ b/tests/sprint2/expected/sp2_llnode.expected @@ -1,62 +1,66 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -make_list : 001001 : : list : User Defined -bar2 : 001001 : : T2 : User Defined -bar1 : 001001 : : T2 : User Defined -foo : 001001 : : T1 : User Defined -list : 001001 : : primitive function type : User Defined -llnode : 001001 : : record : elements-3 -T2 : 001001 : : primitive function type : User Defined -T1 : 001001 : : primitive function type : User Defined -rec : 001001 : : record : elements-2 -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -li : 070000 : 001001 : llnode : User Defined -w : 070000 : 001001 : rec : User Defined -result : 070000 : 001001 : integer : User Defined -arg : 070000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -r : 054000 : 001001 : integer : User Defined -s : 054000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -x : 060009 : 054000 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ - : 062028 : 060009 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 055021 : 054000 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 056026 : 055021 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ -a : 050000 : 001001 : rec : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -x : 046000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -temp : 017000 : 001001 : address : User Defined -curr : 017000 : 001001 : address : User Defined -ret : 017000 : 001001 : address : User Defined -orig_a : 017000 : 001001 : integer : User Defined -a : 017000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ - : 021012 : 017000 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 026023 : 021012 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 035020 : 026023 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 031034 : 026023 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ - : 019029 : 017000 : : Empty Scope --------------------------:--------:--------:--------------------------:------------------------------ -next : 008000 : 001001 : llnode : User Defined -val : 008000 : 001001 : integer : User Defined -prev : 008000 : 001001 : llnode : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 004000 : 001001 : integer : User Defined -x : 004000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +make_list : 001001 : : undefined : Function Definition +bar2 : 001001 : : undefined : Function Definition +bar1 : 001001 : : undefined : Function Definition +foo : 001001 : : undefined : Function Definition +list : 001001 : : integer -> llnode : Type of Function +llnode : 001001 : : Record Type : elements-3 size-24 bytes +T2 : 001001 : : rec -> integer : Type of Function +T1 : 001001 : : integer -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +li : 069000 : 001001 : llnode : Record Instance +w : 069000 : 001001 : rec : Record Instance +result : 069000 : 001001 : integer : Primitive Instance +arg : 069000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +r : 054000 : 001001 : integer : Primitive Instance +s : 054000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 059012 : 054000 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 062028 : 059012 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 055021 : 054000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 056026 : 055021 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +a : 050000 : 001001 : integer : Primitive Instance +b : 050000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 046000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +temp : 016000 : 001001 : address : Primitive Instance +curr : 016000 : 001001 : address : Primitive Instance +ret : 016000 : 001001 : address : Primitive Instance +orig_a : 016000 : 001001 : integer : Primitive Instance +a : 016000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 021012 : 016000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 026023 : 021012 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 035020 : 026023 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 031034 : 026023 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 019029 : 016000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +next : 008000 : 001001 : llnode : Record Instance +val : 008000 : 001001 : integer : Primitive Instance +prev : 008000 : 001001 : llnode : Record Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 004000 : 001001 : integer : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_one_line.expected b/tests/sprint2/expected/sp2_one_line.expected index 8dde170..dcdd20a 100644 --- a/tests/sprint2/expected/sp2_one_line.expected +++ b/tests/sprint2/expected/sp2_one_line.expected @@ -1,30 +1,29 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -bar2 : 001001 : : T2 : User Defined -bar1 : 001001 : : T2 : User Defined -foo : 001001 : : T1 : User Defined -T2 : 001001 : : primitive function type : User Defined -T1 : 001001 : : primitive function type : User Defined -rec : 001001 : : record : elements-2 -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -w : 001000 : 001001 : rec : User Defined -result : 001000 : 001001 : integer : User Defined -arg : 001000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -r : 001000 : 001001 : integer : User Defined -s : 001000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -a : 001000 : 001001 : rec : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -x : 001000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 001000 : 001001 : integer : User Defined -x : 001000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +bar2 : 001001 : : undefined : Function Definition +bar1 : 001001 : : undefined : Function Definition +foo : 001001 : : undefined : Function Definition +T2 : 001001 : : rec -> integer : Type of Function +T1 : 001001 : : integer -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 000000 : 001001 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +a : 001000 : 001001 : integer : Primitive Instance +undefined : 001000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 001000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_presidence.expected b/tests/sprint2/expected/sp2_presidence.expected index 70d9f83..80d2312 100644 --- a/tests/sprint2/expected/sp2_presidence.expected +++ b/tests/sprint2/expected/sp2_presidence.expected @@ -1,13 +1,23 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +rec : 001001 : : Record Type : elements-2 size-8 bytes +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +arg_bool : 006000 : 001001 : Boolean : Primitive Instance +arg_record : 006000 : 001001 : rec : Record Instance +arg_y : 006000 : 001001 : integer : Primitive Instance +arg_x : 006000 : 001001 : integer : Primitive Instance +arg : 006000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +rec_y : 002000 : 001001 : integer : Primitive Instance +rec_x : 002000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint2/expected/sp2_simple.expected b/tests/sprint2/expected/sp2_simple.expected index 8d55a97..649bea7 100644 --- a/tests/sprint2/expected/sp2_simple.expected +++ b/tests/sprint2/expected/sp2_simple.expected @@ -1,12 +1,16 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +x : 004000 : 001001 : integer : Primitive Instance +arg : 004000 : 001001 : string : Array Instance diff --git a/tests/sprint2/expected/sp2_sp2_arrayargs.expected b/tests/sprint2/expected/sp2_sp2_arrayargs.expected new file mode 100644 index 0000000..0db9d5a --- /dev/null +++ b/tests/sprint2/expected/sp2_sp2_arrayargs.expected @@ -0,0 +1,17 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +a_of_s : 001001 : : 1 -> string : Type of Array +string : 001001 : : 1 -> character : Type of Array +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +many_names : 006000 : 001001 : a_of_s : Array Instance +another_name : 006000 : 001001 : string : Array Instance +one_name : 006000 : 001001 : string : Array Instance diff --git a/tests/sprint2/expected/sp2_valid_assignable_and_mem.expected b/tests/sprint2/expected/sp2_valid_assignable_and_mem.expected index ab5cb2a..aa4b733 100644 --- a/tests/sprint2/expected/sp2_valid_assignable_and_mem.expected +++ b/tests/sprint2/expected/sp2_valid_assignable_and_mem.expected @@ -1,18 +1,21 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -T2 : 001001 : : primitive function type : User Defined -rec : 001001 : : record : elements-2 -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -w : 008000 : 001001 : rec : User Defined -arg : 008000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ -y : 004000 : 001001 : integer : User Defined -x : 004000 : 001001 : integer : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +T2 : 001001 : : rec -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +w : 007000 : 001001 : rec : Record Instance +arg : 007000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 004000 : 001001 : integer : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint3/expected/sp3_and_or_type_check.expected b/tests/sprint3/expected/sp3_and_or_type_check.expected new file mode 100644 index 0000000..600a1f9 --- /dev/null +++ b/tests/sprint3/expected/sp3_and_or_type_check.expected @@ -0,0 +1,34 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +test : 001001 : : undefined : Function Definition +main : 001001 : : rec -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-8 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +b : 005000 : 001001 : Boolean : Primitive Instance +x : 005000 : 001001 : integer : Primitive Instance +arg : 005000 : 001001 : integer : Primitive Instance +undefined : 005000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 023009 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 021014 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 017009 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 015017 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 011012 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 007015 : 005000 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint3/expected/sp3_boolean_binary_op_typecheck.expected b/tests/sprint3/expected/sp3_boolean_binary_op_typecheck.expected index 299c09d..1653f88 100644 --- a/tests/sprint3/expected/sp3_boolean_binary_op_typecheck.expected +++ b/tests/sprint3/expected/sp3_boolean_binary_op_typecheck.expected @@ -1,17 +1,20 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -b1 : 005000 : 001001 : Boolean : User Defined -b2 : 005000 : 001001 : Boolean : User Defined -arr2 : 005000 : 001001 : address : User Defined -arr : 005000 : 001001 : address : User Defined -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +b1 : 004000 : 001001 : Boolean : Primitive Instance +b2 : 004000 : 001001 : Boolean : Primitive Instance +arr2 : 004000 : 001001 : address : Primitive Instance +arr : 004000 : 001001 : address : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance +arg : 004000 : 001001 : string : Array Instance diff --git a/tests/sprint3/expected/sp3_boolean_unary_op_typecheck.expected b/tests/sprint3/expected/sp3_boolean_unary_op_typecheck.expected index 299c09d..1653f88 100644 --- a/tests/sprint3/expected/sp3_boolean_unary_op_typecheck.expected +++ b/tests/sprint3/expected/sp3_boolean_unary_op_typecheck.expected @@ -1,17 +1,20 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -b1 : 005000 : 001001 : Boolean : User Defined -b2 : 005000 : 001001 : Boolean : User Defined -arr2 : 005000 : 001001 : address : User Defined -arr : 005000 : 001001 : address : User Defined -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +b1 : 004000 : 001001 : Boolean : Primitive Instance +b2 : 004000 : 001001 : Boolean : Primitive Instance +arr2 : 004000 : 001001 : address : Primitive Instance +arr : 004000 : 001001 : address : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance +arg : 004000 : 001001 : string : Array Instance diff --git a/tests/sprint3/expected/sp3_carls_second_mistake.expected b/tests/sprint3/expected/sp3_carls_second_mistake.expected index 5d0682f..6f509e0 100644 --- a/tests/sprint3/expected/sp3_carls_second_mistake.expected +++ b/tests/sprint3/expected/sp3_carls_second_mistake.expected @@ -1,17 +1,20 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -a_of_s : 001001 : : 1 -> string : Type of Array -string : 001001 : : 1 -> character : Type of Array -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -many_names : 010000 : 001001 : a_of_s : User Defined -another_name : 010000 : 001001 : string : User Defined -one_name : 010000 : 001001 : string : User Defined -arg : 010000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +a_of_s : 001001 : : 1 -> string : Type of Array +string : 001001 : : 1 -> character : Type of Array +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +many_names : 009000 : 001001 : a_of_s : Array Instance +another_name : 009000 : 001001 : string : Array Instance +one_name : 009000 : 001001 : string : Array Instance +arg : 009000 : 001001 : string : Array Instance diff --git a/tests/sprint3/expected/sp3_integer_binary_op_typecheck.expected b/tests/sprint3/expected/sp3_integer_binary_op_typecheck.expected index a183cf5..8344120 100644 --- a/tests/sprint3/expected/sp3_integer_binary_op_typecheck.expected +++ b/tests/sprint3/expected/sp3_integer_binary_op_typecheck.expected @@ -1,18 +1,26 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -a : 005000 : 001001 : character : User Defined -b1 : 005000 : 001001 : Boolean : User Defined -b2 : 005000 : 001001 : Boolean : User Defined -arr2 : 005000 : 001001 : address : User Defined -arr : 005000 : 001001 : address : User Defined -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +rec : 001001 : : Record Type : elements-2 size-8 bytes +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +r : 006000 : 001001 : rec : Record Instance +a : 006000 : 001001 : character : Primitive Instance +b1 : 006000 : 001001 : Boolean : Primitive Instance +b2 : 006000 : 001001 : Boolean : Primitive Instance +arr2 : 006000 : 001001 : address : Primitive Instance +arr : 006000 : 001001 : address : Primitive Instance +x : 006000 : 001001 : integer : Primitive Instance +arg : 006000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 003000 : 001001 : integer : Primitive Instance +x : 003000 : 001001 : integer : Primitive Instance diff --git a/tests/sprint3/expected/sp3_integer_unary_op_typecheck.expected b/tests/sprint3/expected/sp3_integer_unary_op_typecheck.expected index 299c09d..1653f88 100644 --- a/tests/sprint3/expected/sp3_integer_unary_op_typecheck.expected +++ b/tests/sprint3/expected/sp3_integer_unary_op_typecheck.expected @@ -1,17 +1,20 @@ -NAME : SCOPE : PARENT : TYPE : Extra annotation --------------------------:--------:--------:--------------------------:------------------------------ -entry : 001001 : : main : User Defined -main : 001001 : : primitive function type : User Defined -integer : 001001 : : Primitive : size-4 bytes -address : 001001 : : Primitive : size-8 bytes -character : 001001 : : Primitive : size-1 bytes -string : 001001 : : 1 -> character : Type of Array -Boolean : 001001 : : Primitive : size-1 bytes --------------------------:--------:--------:--------------------------:------------------------------ -b1 : 005000 : 001001 : Boolean : User Defined -b2 : 005000 : 001001 : Boolean : User Defined -arr2 : 005000 : 001001 : address : User Defined -arr : 005000 : 001001 : address : User Defined -x : 005000 : 001001 : integer : User Defined -arg : 005000 : 001001 : string : User Defined --------------------------:--------:--------:--------------------------:------------------------------ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +b1 : 004000 : 001001 : Boolean : Primitive Instance +b2 : 004000 : 001001 : Boolean : Primitive Instance +arr2 : 004000 : 001001 : address : Primitive Instance +arr : 004000 : 001001 : address : Primitive Instance +x : 004000 : 001001 : integer : Primitive Instance +arg : 004000 : 001001 : string : Array Instance diff --git a/tests/sprint3/expected/sp3_multiple_args.expected b/tests/sprint3/expected/sp3_multiple_args.expected new file mode 100644 index 0000000..c0128e1 --- /dev/null +++ b/tests/sprint3/expected/sp3_multiple_args.expected @@ -0,0 +1,25 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +bar : 001001 : : undefined : Function Definition +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +T2 : 001001 : : rec -> integer : Type of Function +rec : 001001 : : Record Type : elements-2 size-6 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +w : 013000 : 001001 : rec : Record Instance +result : 013000 : 001001 : integer : Primitive Instance +arg : 013000 : 001001 : string : Array Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: + : 009000 : 001001 : : Empty Scope +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : character : Primitive Instance diff --git a/tests/sprint3/expected/sp3_primitive_type_check.expected b/tests/sprint3/expected/sp3_primitive_type_check.expected new file mode 100644 index 0000000..0a57752 --- /dev/null +++ b/tests/sprint3/expected/sp3_primitive_type_check.expected @@ -0,0 +1,19 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +entry : 001001 : : undefined : Function Definition +main : 001001 : : string -> integer : Type of Function +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +bool : 015000 : 001001 : Boolean : Primitive Instance +char : 015000 : 001001 : character : Primitive Instance +add : 015000 : 001001 : address : Primitive Instance +i : 015000 : 001001 : integer : Primitive Instance +arg : 015000 : 001001 : string : Array Instance diff --git a/tests/sprint3/expected/sp3_record_size_check.expected b/tests/sprint3/expected/sp3_record_size_check.expected new file mode 100644 index 0000000..a061979 --- /dev/null +++ b/tests/sprint3/expected/sp3_record_size_check.expected @@ -0,0 +1,29 @@ +NAME : SCOPE : PARENT : TYPE : EXTRA ANNOTATION : +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +tricky : 001001 : : Record Type : elements-4 size-16 bytes +rec : 001001 : : Record Type : elements-6 size-24 bytes +tom : 001001 : : Record Type : elements-2 size-8 bytes +integer : 001001 : : Primitive Type : size-4 bytes +address : 001001 : : Primitive Type : size-8 bytes +character : 001001 : : Primitive Type : size-1 bytes +string : 001001 : : 1 -> character : Type of Array +Boolean : 001001 : : Primitive Type : size-4 bytes +reserve type : 001001 : : integer -> address : Type of Function +reserve : 001001 : : undefined : Function Definition +release type : 001001 : : address -> integer : Type of Function +release : 001001 : : undefined : Function Definition +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +k2 : 003000 : 001001 : integer : Primitive Instance +b2 : 003000 : 001001 : Boolean : Primitive Instance +k1 : 003000 : 001001 : integer : Primitive Instance +b1 : 003000 : 001001 : Boolean : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 002000 : 001001 : integer : Primitive Instance +b : 002000 : 001001 : Boolean : Primitive Instance +d : 002000 : 001001 : character : Primitive Instance +c : 002000 : 001001 : character : Primitive Instance +prev : 002000 : 001001 : tom : Record Instance +x : 002000 : 001001 : integer : Primitive Instance +------------------------------:--------:--------:-----------------------------------:-----------------------------------: +y : 001000 : 001001 : integer : Primitive Instance +x : 001000 : 001001 : integer : Primitive Instance