diff --git a/src/grammar.y b/src/grammar.y index 2db7495..4ac0f0d 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -266,13 +266,20 @@ simple_statement: { if(strcmp($1, $3) == 0) { printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp($1, "rec") == 0) && (strcmp($3, "address") == 0)) { - printdebug("Passed rec type check; rec = address"); - } else if((strcmp($1, "array") == 0) && (strcmp($3, "address") == 0)) { - printdebug("Passed array type check; array = address"); + } 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 () { + + // } 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))); } } @@ -387,32 +394,35 @@ expression: // add array case // include type check for ablock in arrays - ablock is always the int of the elements in array/rec assignable: - ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} - | assignable { - cur = CreateScope(cur, -1,-1); - }ablock { - int type = getAdInfoType(look_up(getParent(cur), $1)); - if (type == TYPE_FUNCTION_DECLARATION) { - 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); - } - //this isn't very efficient, but will hopefully work - while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getType(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; - } + ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} + | assignable + { + cur = CreateScope(cur, -1,-1); + } + ablock + { + int type = getAdInfoType(look_up(getParent(cur), $1)); + if (type == TYPE_FUNCTION_DECLARATION) { + 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); + } + //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",getName(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))); @@ -433,7 +443,7 @@ assignable: printdebug("expected only integer expressions in array ablock at line %d column %d", @3.first_line, @3.first_column); } } - $$ = $1; + $$ = getName(getArrType(look_up(getParent(cur), $1))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); } cur = getParent(cur); diff --git a/tests/sprint3/test/sp3_carls_second_mistake.alpha b/tests/sprint3/test/sp3_carls_second_mistake.alpha index 9eb4864..26c64d2 100644 --- a/tests/sprint3/test/sp3_carls_second_mistake.alpha +++ b/tests/sprint3/test/sp3_carls_second_mistake.alpha @@ -11,15 +11,15 @@ entry(arg) := { another_name(1) := 'a'; another_name(2) := 'r'; another_name(3) := 'l'; - a_of_s := reserve a_of_s(3); - a_of_s(0) := one_name; - a_of_s(1) := another_name; + many_names := reserve many_names(3); + many_names(0) := one_name; + many_names(1) := another_name; many_names(2) := reserve many_names(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"; - many_names(2)(1) := "a"; - many_names(2)(2) := "r"; - many_names(2)(3) := "t"; - many_names(2)(4) := "h"; - many_names(2)(5) := "o"; + many_names(2)(0) := 'P'; + many_names(2)(1) := 'a'; + many_names(2)(2) := 'r'; + many_names(2)(3) := 't'; + many_names(2)(4) := 'h'; + many_names(2)(5) := 'o'; return 0; } \ No newline at end of file