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/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 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 7b925f6..c6044fd 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -18,37 +18,45 @@ %{ #include "../src/symbol_table.c" - void yyerror(const char *err); int token_tracker; TableNode * tn; +// int counter; %} -%locations - %union { int integ; - char * words; + char* words; + void* tn; } +%locations + + %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 +%type sblock +%type compound_statement +%type simple_statement +%type statement_list +%type argument_list +%type ablock %token ID 101 -%token T_INTEGER 201 -%token T_ADDRESS 202 -%token T_BOOLEAN 203 -%token T_CHARACTER 204 -%token T_STRING 205 +%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 @@ -97,6 +105,7 @@ %precedence DOT %precedence RESERVE RELEASE + %% program: @@ -122,102 +131,164 @@ prototype: 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))); - if (table_lookup(getAncestor(cur), $2) == undefined) { - printdebug("rec not found"); - } + + 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))); + printdebug("Created a new scope"); + //if (look_up(cur, $2) == undefined) { + // printdebug("rec not found"); + //} } dblock { - setRecSize(table_lookup(getParent(cur), $2), getRecSize(cur)); + //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); } | 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((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, $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", $2, getName((TableNode*)$4), getName((TableNode*)$6)); + CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); } - | 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 { - 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); + | 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); } - R_PAREN ASSIGN sblock - - | ID - { - TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == undefined) { - printdebug("null check"); + 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( 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 (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, true); - } - cur = CreateScope(cur, 0, 0); - } - AS L_PAREN - { - TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - printdebug("%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) { - 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->theType, NULL, NULL); + 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 { + 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_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 + || 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", 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", 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_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){ + printdebug("array type of parameter inside record"); + CreateEntry(cur, TYPE_ARRAY, getTypeEntry(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);*/ + } + } + } + //counter = 0; + printdebug("Created a new scope after seeing a function definition"); + } 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!!!"); + } } - 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: FUNCTION ID COLON ID - { - CreateEntry(cur, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + { + 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, 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)); + + } } ; @@ -226,31 +297,57 @@ function_declaration: idlist: ID { + printdebug("idlist rule 1 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - while (strcmp(getName(entry),"undefined") != 0) { + while((getName(entry) != getName(undefined))){ entry = getNextEntry(entry); } - if (getNextEntry(entry) == NULL) { - printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); + if (entry == NULL){ + printdebug("mismatch in number of parameters passed to function"); } - addName(entry, $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); + //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 { - $$ = $4 + 1; + $$ = $4 + 1; } | ID - { + { printdebug("idlist rule 2 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - while (strcmp(getName(entry),"undefined") != 0) { + while((getName(entry) != getName(undefined))){ entry = getNextEntry(entry); } - if (getNextEntry(entry) != NULL) { - printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); + if (entry == NULL){ + printdebug("mismatch in number of parameters passed to function"); } - addName(entry, $1); - $$ = 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)); } ; @@ -260,6 +357,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); @@ -267,14 +365,22 @@ sblock: } statement_list { + //$$ = $3; + printdebug("Moving up a scope after seeing sblock"); cur = getParent(cur); } R_BRACE + {$$ = $3;} | L_BRACE { - if (getLine(cur) != 0 && getColumn(cur)) { + 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 @@ -283,23 +389,30 @@ sblock: } statement_list { + printdebug("Moving up a scope after seeing sblock with dblock"); cur = getParent(cur); + //$$ = $5; + } R_BRACE + {$$ = $5;} ; dblock: - L_BRACKET + + L_BRACKET { - if(getLine(cur)==0) { - setLineNumber(cur, @1.first_line); + if (getLine(cur) == 0) { + setLineNumber(cur, @1.first_line); setColumnNumber(cur,@1.first_line); - } else { - cur = CreateScope(cur,@1.first_line,@1.first_column); + 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; @@ -314,8 +427,35 @@ 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((TableNode*)$1), $3) ; + int d = getAdInfoType((TableNode*)$1); + if(d == TYPE_UNDEFINED) { + 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) { + printdebug("invalid (function) type passed in declaration list in dblock", @2.first_line, @2.first_column); + d = TYPE_FUNCTION_DECLARATION; + 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_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("[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)); + } } ; @@ -324,31 +464,73 @@ 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); + $$ = table_lookup(getAncestor(cur), $1); } | types { - printdebug("string of type is %s in types pattern of id_or_type rule.",$1); - $$ = $1; + printdebug("string of type is %s in types pattern of id_or_type rule.",getName((TableNode*)$1)); + $$ = (TableNode*)$1; } ; 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; + } ; @@ -356,26 +538,51 @@ compound_statement: simple_statement: assignable ASSIGN expression { - if(strcmp($1, $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 () { + 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 { - 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))); + 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 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)); + } + + $$ = undefined; } - | RETURN expression +| RETURN expression {$$ = $2;} ; @@ -388,8 +595,8 @@ rec_op: ablock: L_PAREN argument_list R_PAREN { - $$ = $2; - printdebug("ablock is %d", $$); + $$ = $2; + printdebug("ablock is %d", $$); } ; @@ -398,169 +605,194 @@ ablock: argument_list: expression COMMA argument_list { - CreateEntry(cur, look_up(cur, $1), "", NULL); - $$ = $3 + 1; - printdebug("[ARGUMENT_LIST] argument list is %d", $$); + CreateEntry(cur,getAdInfoType((TableNode*)$1), (TableNode*)$1, getName((TableNode*)$1), NULL); + $$ = $3 + 1; + printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur, look_up(cur, $1), "", NULL); - $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); + CreateEntry(cur,getAdInfoType((TableNode*)$1),(TableNode*)$1, getName((TableNode*)$1), NULL); + $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; - // will ALWAYS be a TYPE expression: constant { printdebug("constant expression"); - $$ = $1; + $$ = (TableNode*)$1; } | SUB_OR_NEG expression %prec UMINUS { printdebug("negative expression"); - if(strcmp($2,"integer") != 0) { + if((TableNode*)$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; + $$=(TableNode*)$2; } } | NOT expression { printdebug("not expression"); - if(strcmp($2,"Boolean")==0) { - $$=$2; + if((TableNode*)$2 == boo) { + $$=(TableNode*)$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("[TYPE CHECK] 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(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("integer"); + 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,$1,$3); - $$=strdup("undefined"); + 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; } } | expression SUB_OR_NEG expression { printdebug("sub or neg expression"); - if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { - $$=strdup("integer"); + 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,$1,$3); - $$=strdup("undefined"); + 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; } } | expression MUL expression { printdebug("multiply expression"); - if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { - $$=strdup("integer"); + 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,$1,$3); - $$=strdup("undefined"); + 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; } } | expression DIV expression { printdebug("divide expression"); - if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { - $$=strdup("integer"); + 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,$1,$3); - $$=strdup("undefined"); + 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; } } | 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("[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; } } | 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("[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; } } | expression OR expression { printdebug("OR"); - if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0) { - $$=strdup("Boolean"); + 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,$1,$3); - $$=strdup("undefined"); + 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; } } - + | 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("[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; } } | 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("[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; } } | assignable { - printdebug("assignable expression. current type is %s",$1); - $$=$1; + printdebug("assignable expression. current type is %s",getName((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_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); + } + else { + printdebug("[TYPE CHECK] assignable passing up an invalid type to expression"); + $$= ((TableNode*)$1); + } + } | L_PAREN expression R_PAREN { - printdebug("paren expression. current type is %s",$2); + printdebug("paren expression. current type is %s",getName((TableNode*)$2)); $$=$2; } | memOp assignable { - $$ = strdup("address"); + 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; + } } ; - +//UPDATED $$ for tablenodes to this point // prolly right, check back with me later // add array case @@ -568,8 +800,8 @@ expression: assignable: ID { - $$ = getType(look_up(cur,$1)); - printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); + $$ = getTypeEntry(look_up(cur,$1)); + printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getName((TableNode*)$$), $1); } | assignable @@ -579,59 +811,92 @@ assignable: } ablock { - int type = getAdInfoType(look_up(getParent(cur), $1)); + int type = getAdInfoType(look_up(getParent(cur), getName((TableNode*)$1))); 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 (getAsKeyword(look_up(getParent(cur), $1))) { - TableNode *param = getParameter(look_up(getParent(cur), $1)); - SymbolTable *recList = getRecList(param); - TableNode *lastCheckedRef = getFirstEntry(recList); - TableNode *lastCheckedAct = getFirstEntry(cur); - while (getNextEntry(lastCheckedRef) != NULL) { - lastCheckedRef = getNextEntry(lastCheckedRef); + 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)))); + + + + TableNode * typeNode = $1; + TableNode *param = getParameter(typeNode); + printTableNode(param); + + if (getAdInfoType(param) == TYPE_RECORD_TYPE) { + SymbolTable *recList = getRecList(param); + TableNode *lastCheckedRef = getFirstEntry(recList); + TableNode *lastCheckedAct = getFirstEntry(cur); + while (getNextEntry(lastCheckedRef) != NULL) { + 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 (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); + + } + 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"); + } } - //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); - } - lastCheckedAct = getNextEntry(lastCheckedAct); - TableNode *tn = getFirstEntry(recList); - while (getNextEntry(tn) != lastCheckedRef) { - tn = getNextEntry(tn); - } - lastCheckedRef = tn; - } } else { - char *expected = getName(getParameter(look_up(getParent(cur), $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); } + if ($3 != 1) { + printdebug("expected 1 argument but got %d", $3); + } } - - $$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $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) { 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((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); } - $$ = getName(getArrType(look_up(getParent(cur), $1))); - printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $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); } | 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((TableNode*)$1))), $3)) { + + $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName((TableNode*)$1))), $3); } - printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1); + printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName((TableNode*)($$)), getName($1)); } ; @@ -655,38 +920,38 @@ memOp: constant: C_STRING { - $$ = $1; - printdebug("string of C_STRING in constant is %s",$1); + $$ = $1; + printdebug("string of C_STRING in constant is %s",getName((TableNode*)$1)); } | C_INTEGER { - $$ = "integer"; + $$ = integ; printdebug("string of C_INTEGER in constant is integer"); } | C_NULL { - $$ = $1; - printdebug("string of C_NULL in constant is %s",$1); + $$ = $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); + $$ = $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); + $$ = $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); + $$ = $1; + printdebug("string of C_FALSE in constant is %s",getName((TableNode*)$1)); } ; @@ -697,25 +962,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/lexicalStructure.lex b/src/lexicalStructure.lex index 00b5a4d..e11ef39 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/runner.c b/src/runner.c index 3fdb399..6915d29 100644 --- a/src/runner.c +++ b/src/runner.c @@ -4,241 +4,219 @@ #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; + // If file is not found + if (alpha == NULL) { + fprintf(stderr, "INPUT FILE NOT FOUND\n"); + return -1; + } + yyin = alpha; + yyparse(); + + if (tok_flag != NULL) { + while (0 != (token = yylex())) { + // Don't delete me 🥺 } - yyin = alpha; + fclose(tok_flag); + } - // 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 (asc_flag != NULL) { + printf("Flag -asc is not implemented yet\n"); + fclose(asc_flag); + } - if (st_flag != NULL) { - yyparse(); + if (tc_flag != NULL) { + printf("Flag -tc is not implemented yet\n"); + fclose(tc_flag); + } - if (cur == NULL) { - printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED); - } + if (ir_flag != NULL) { + printf("Flag -ir is not implemented yet\n"); + fclose(ir_flag); + } - if (top == NULL) { - printdebug("%s[FATAL] top is null", COLOR_LIGHTRED); - } + 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; - } + if (yyin != NULL) { + fclose(yyin); + } - if (tc_flag != NULL) { - //SCARLETT NEEDS TO ADD THIS FUNCTIONALITY - } - - 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..3e71a7a 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; diff --git a/src/symbol_table.c b/src/symbol_table.c index f749b00..c1d3946 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -4,113 +4,122 @@ #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 (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; } // 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 undefined; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getArrType " + "function. Invalid."); + return undefined; + } + 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; } // Record type currently stores the number of elements as well as the types, in @@ -118,378 +127,608 @@ 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 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_DECLARATION) { + offsets[counter] = 8; + total_size = total_size + offsets[counter]; + largest = 8; + counter++; + } 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) { + offsets[counter] = getPrimSize(getTypeEntry(this)); + total_size = total_size + offsets[counter]; + largest = offsets[counter]; + counter++; + } else if (getAdInfoType(this) == TYPE_ARRAY) { + 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. 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) { + 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) { + 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)) { + int s = 8; + 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++; + //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(getTypeEntry(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("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 of type %s", + getType(definition)); + 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 -- " - "invalid op"); - 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 (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"); + 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 undefined; + } + if (definition == undefined) { + printdebug( + "passed an undefined entry to getReturn " + "function. Invalid."); + return undefined; + } + if (strcmp(getType(definition), "primitive function type") != 0) { + printdebug( + "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; } // 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 *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 = 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"; - // arr->theName= "array" + 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 - // 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; + // 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; - prime->next = NULL; + // 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; + // 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; + // 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 = (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 = (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; + reserve->theType = reservetype; + reservetype->theType = funtypeprime; + releasetype->theType = funtypeprime; + release->theType = releasetype; - // 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(4); - addr->additionalinfo = CreatePrimitiveInfo(8); - chara->additionalinfo = CreatePrimitiveInfo(1); - stri->additionalinfo = CreateArrayInfo(1, chara); - boo->additionalinfo = CreatePrimitiveInfo(1); - // addr->additionalinfo = CreatePrimitiveInfo(8); + // arr->theType=arrayprim; - start->Line_Number = 1; - start->Column_Number = 1; - start->Parent_Scope = NULL; - start->Children_Scope = NULL; + // 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); + reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false); + reservetype->additionalinfo = CreateFunctionTypeInfo(integ, addr); + releasetype->additionalinfo = CreateFunctionTypeInfo(addr, integ); + release->additionalinfo = CreateFunctionDeclarationInfo(0, false); - return 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 + 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; + start->Column_Number = 1; + start->Parent_Scope = NULL; + start->Children_Scope = NULL; + + return start; } /* TableNode* integ; @@ -501,44 +740,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; +} + +// simplified getAdInfoType int getAdInfoType(TableNode *tn) { - if (tn == NULL) { - printdebug( - "passing in NULL table entry to getAdInfoType. Invalid"); - return -1; - } + 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"); @@ -597,461 +859,508 @@ 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, +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)); - 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; } 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?"); - 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; + } + } + 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."); - return undefined; + if (table == NULL) { + printdebug("Could not find %s in any scope using lookup", x); + 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, 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, "-"); } - 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, false); + 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", getNumArrDim(entry), + getName(getArrType(entry))); + + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, arrayType, " Type of Array"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Type of Array"); + } + } + if (getAdInfoType(entry) == TYPE_ARRAY) { + char *arrayType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(arrayType, " %s", getType(entry)); + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, arrayType, " Array Instance"); + } else { + 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); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " Record Type", recordAdInfo); + } } - 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", 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, recordType, " Record Instance"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, recordType, " Record Instance"); + } } - 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_TYPE) { + char *primAdInfo = (char *)malloc(100); + sprintf(primAdInfo, " size-%d bytes", getPrimSize(entry)); + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " Primitive Type", primAdInfo); + } else { + char *primType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(primType, " %s", getType(entry)); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, primAdInfo); + } } - if (entrie == NULL) { - fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "", - current_scope, parant_scope, "", "Empty Scope"); + if (getAdInfoType(entry) == TYPE_PRIMITIVE) { + 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", primAdInfo); + } else { + char *primType = (char *)malloc(sizeof(getType(entry) + 1)); + sprintf(primType, " %s", getType(entry)); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, primType, " Primitive Instance"); + } } - 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_TYPE) { + char *functiontype = (char *)malloc(100); + sprintf(functiontype, " %s -> %s", getName(getParameter(entry)), + getName(getReturn(entry))); + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Type of Function"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Type of Function"); + } } - 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_FUNCTION_DECLARATION) { + char *functiontype = (char *)malloc(100); + sprintf(functiontype, " %s", getName(getReturn(entry))); + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Function Definition"); + } + } + + if (getAdInfoType(entry) == TYPE_UNDEFINED) { + if (parentScopeNum == 0) { + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, " undefined", "undefined entry"); + } else { + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, " undefined", "undefined entry"); + } + } + } + + if (getChildren(table) != NULL) { + ListOfTable *node = getChildren(table); + for (; node != NULL; node = getRestOfChildren(node)) { + if ((getFirstChild(node)) == NULL) { + print_symbol_table(getFirstChild(node), file_ptr); + } else { + if (getLine(getFirstChild(node)) == -1) { + continue; + } else { + print_symbol_table(getFirstChild(node), file_ptr); } + } } - if (getParent(table) == NULL) { - fprintf(file_ptr, - "-------------------------:--------:--------:----------" - "----------------:------------------------------\n"); - } + } + + if (getParent(table) == NULL) { + printline(file_ptr, true); + } } // 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 NULL; - } - 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; } @@ -1061,13 +1370,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->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; } diff --git a/src/symbol_table.h b/src/symbol_table.h index 5f87b5b..01d4458 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -4,71 +4,82 @@ #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 { - 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 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; - 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_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); @@ -93,12 +104,13 @@ 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); 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); @@ -115,9 +127,10 @@ 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__) +#define printdebug(format, ...) \ + printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) extern int yylex(void); extern char *yytext; @@ -154,4 +167,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; 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: 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/sprint2/test/sp2_carls_mistake.alpha b/tests/sprint2/test/sp2_carls_mistake.alpha index eb9a6f0..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 main: string -> integer +function entry: main function foo: T1 function bar1: T2 function bar2: T2 @@ -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; } @@ -32,3 +32,4 @@ entry (arg) := { 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; } + 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; 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/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index 2011a83..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; @@ -78,4 +78,4 @@ entry (arg) := { result := bar2(5,7); return 0; -} +} \ No newline at end of file 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/sprint2/test/sp2_simple.alpha b/tests/sprint2/test/sp2_simple.alpha index 5f60c1b..58934e6 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; -} +} \ 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..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/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 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..c01fc10 --- /dev/null +++ b/tests/sprint3/test/sp3_and_or_type_check.alpha @@ -0,0 +1,40 @@ +type rec: [integer: x; integer: y] +type main: rec -> integer +function test: main + +test (arg) := { + [integer:x; Boolean: b] + while (true) { + x := 0; + } + + while (7) { + x := 1; + } + + if (true) then { + x := 1; + } else { + x := 0; + } + + if (x) then { + 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 b042e5f..bc88a72 100644 --- a/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha +++ b/tests/sprint3/test/sp3_integer_binary_op_typecheck.alpha @@ -1,15 +1,19 @@ 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] + [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; - - return 0; + x := 3 + 2 * 8; + x := 3 - 2 / 8; + x := a * 2 % 8; + b2 := 3 * 2 % 8; + x := 3 % 2 * 8; + x := 3 + arr - 8; + x := r.x; + x := a.x; + + return 0; } 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 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; +} 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..8e123d0 --- /dev/null +++ b/tests/sprint3/test/sp3_record_size_check.alpha @@ -0,0 +1,3 @@ +type tom : [integer : x; integer: y] +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