more type check fixes

This commit is contained in:
Scarlett
2025-04-02 13:19:37 -04:00
parent 692025412e
commit 20c372f134
2 changed files with 50 additions and 40 deletions

View File

@ -266,13 +266,20 @@ simple_statement:
{ {
if(strcmp($1, $3) == 0) { if(strcmp($1, $3) == 0) {
printdebug("Passed standard type check; assignable = expression"); printdebug("Passed standard type check; assignable = expression");
} else if((strcmp($1, "rec") == 0) && (strcmp($3, "address") == 0)) { } else if((strcmp(getType(look_up(cur, $1)), "array") == 0) && (strcmp($3, "address") == 0)) {
printdebug("Passed rec type check; rec = address"); printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, $1, $3);
} else if((strcmp($1, "array") == 0) && (strcmp($3, "address") == 0)) { } else if((strcmp(getType(look_up(cur, $1)), "record") == 0) && (strcmp($3, "address") == 0)) {
printdebug("Passed array type check; array = address"); 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 { } 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("%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(" - 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 // add array case
// include type check for ablock in arrays - ablock is always the int of the elements in array/rec // include type check for ablock in arrays - ablock is always the int of the elements in array/rec
assignable: assignable:
ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);}
| assignable { | assignable
cur = CreateScope(cur, -1,-1); {
}ablock { cur = CreateScope(cur, -1,-1);
int type = getAdInfoType(look_up(getParent(cur), $1)); }
if (type == TYPE_FUNCTION_DECLARATION) { ablock
if (getAsKeyword(look_up(getParent(cur), $1))) { {
TableNode *param = getParameter(look_up(getParent(cur), $1)); int type = getAdInfoType(look_up(getParent(cur), $1));
SymbolTable *recList = getRecList(param); if (type == TYPE_FUNCTION_DECLARATION) {
TableNode *lastCheckedRef = getFirstEntry(recList); if (getAsKeyword(look_up(getParent(cur), $1))) {
TableNode *lastCheckedAct = getFirstEntry(cur); TableNode *param = getParameter(look_up(getParent(cur), $1));
while (getNextEntry(lastCheckedRef) != NULL) { SymbolTable *recList = getRecList(param);
lastCheckedRef = getNextEntry(lastCheckedRef); TableNode *lastCheckedRef = getFirstEntry(recList);
} TableNode *lastCheckedAct = getFirstEntry(cur);
//this isn't very efficient, but will hopefully work while (getNextEntry(lastCheckedRef) != NULL) {
while (lastCheckedAct != NULL && lastCheckedRef != NULL) { lastCheckedRef = getNextEntry(lastCheckedRef);
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); //this isn't very efficient, but will hopefully work
} while (lastCheckedAct != NULL && lastCheckedRef != NULL) {
lastCheckedAct = getNextEntry(lastCheckedAct); if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) {
TableNode *tn = getFirstEntry(recList); 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);
while (getNextEntry(tn) != lastCheckedRef) { }
tn = getNextEntry(tn); lastCheckedAct = getNextEntry(lastCheckedAct);
} TableNode *tn = getFirstEntry(recList);
lastCheckedRef = tn; while (getNextEntry(tn) != lastCheckedRef) {
} tn = getNextEntry(tn);
}
lastCheckedRef = tn;
}
} else { } else {
char *expected = getName(getParameter(look_up(getParent(cur), $1))); 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); 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); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);
} }
cur = getParent(cur); cur = getParent(cur);

View File

@ -11,15 +11,15 @@ entry(arg) := {
another_name(1) := 'a'; another_name(1) := 'a';
another_name(2) := 'r'; another_name(2) := 'r';
another_name(3) := 'l'; another_name(3) := 'l';
a_of_s := reserve a_of_s(3); many_names := reserve many_names(3);
a_of_s(0) := one_name; many_names(0) := one_name;
a_of_s(1) := another_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) := 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)(0) := 'P';
many_names(2)(1) := "a"; many_names(2)(1) := 'a';
many_names(2)(2) := "r"; many_names(2)(2) := 'r';
many_names(2)(3) := "t"; many_names(2)(3) := 't';
many_names(2)(4) := "h"; many_names(2)(4) := 'h';
many_names(2)(5) := "o"; many_names(2)(5) := 'o';
return 0; return 0;
} }