Type check errors

This commit is contained in:
Scarlett
2025-04-16 16:30:58 -04:00
parent 5a23ef2756
commit c72e7a2a28
6 changed files with 128 additions and 89 deletions

View File

@ -19,9 +19,11 @@
%{
#include "../src/symbol_table.c"
void yyerror(const char *err);
extern FILE *asc_flag;
extern bool tc_flag;
int token_tracker;
TableNode * tn;
// int counter;
void error_type(TableNode * left, TableNode * right, const char *format, ...);
%}
%union {
@ -169,10 +171,9 @@ definition:
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);
error_type(undefined, undefined, "Undefined node declared.");
}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);
error_type(undefined, undefined, "Not a valid function declaration.");
}
else {
printdebug("setting as keyword to true");
@ -186,7 +187,7 @@ definition:
//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);
error_type(undefined, undefined, "Undefined parameter in function definition.");
}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
@ -197,7 +198,7 @@ definition:
|| type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_RECORD
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter));
error_type(parameter, undefined, "Invalid type (%s) of parameter in function definition.", getAdInfo(parameter));
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}
if(type_of_param_type == TYPE_UNDEFINED){
@ -225,7 +226,7 @@ definition:
|| 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));
error_type(entry, undefined, "Invalid type (%s) of parameter in function definition.", getAdInfo(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));
@ -256,9 +257,9 @@ definition:
} idlist R_PAREN ASSIGN sblock {
TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1)));
if ($8 == undefined) {
printdebug("sblock return type is undefined");
error_type(undefined, undefined, "Expected %s as return type but got undefined (possibly NULL). Differing return types in function.", getName(expected));
} else if ($8 != expected) {
printdebug("expected %s as return type but got %s", getName(expected), getName($8));
error_type(undefined, undefined, "Expected %s as return type but got %s. Differing return types in function.", getName(expected), getName($8));
} else {
printdebug("CORRECT RETURN TYPE!!!");
}
@ -273,7 +274,7 @@ function_declaration:
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);
error_type(undefined, undefined, "Function declatation (%s) is not a valid function type", $2);
CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false));
}
@ -285,7 +286,7 @@ function_declaration:
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);
error_type(undefined, undefined, "Function declatation (%s) is not a valid function type", $3);
CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false));
}
@ -430,7 +431,7 @@ declaration:
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);
error_type(undefined, undefined, "Undefined type passed in declaration list");
CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1));
}
else if(d == TYPE_FUNCTION_TYPE) {
@ -453,7 +454,7 @@ declaration:
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);
error_type(undefined, undefined, "Invalid type passed in declaration list.");
CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1));
}
}
@ -537,51 +538,39 @@ WHILE L_PAREN expression R_PAREN sblock {
simple_statement:
assignable ASSIGN expression
{
bool passCheck = false;
TableNode * left = (TableNode*)$1;
TableNode * right = (TableNode*)$3;
printTableNode((TableNode*)$1);
printTableNode((TableNode*)$3);
{
TableNode* node;
if((getAdInfoType((TableNode*)$1) == TYPE_PRIMITIVE_TYPE||
getAdInfoType((TableNode*)$1) == TYPE_ARRAY_TYPE||
getAdInfoType((TableNode*)$1) == TYPE_RECORD_TYPE||
getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_TYPE)){
node = (TableNode*)$1;
}else if((getAdInfoType((TableNode*)$1) == TYPE_FUNCTION_DECLARATION)||
(getAdInfoType((TableNode*)$1) == TYPE_ARRAY)||
(getAdInfoType((TableNode*)$1) == TYPE_RECORD)||
(getAdInfoType((TableNode*)$1) == TYPE_PRIMITIVE)){
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;
node = getTypeEntry((TableNode*)$1);
} else{
error_type(undefined, undefined, "Invalid type passed to assignable.");
node = undefined;
}
}
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));
if((getAdInfoType(node) == TYPE_ARRAY_TYPE||
getAdInfoType(node) == TYPE_RECORD_TYPE) &&
(strcmp(getName((TableNode*)$3),"address") == 0)){
printdebug("%s[☺] Passed array/record type check; %s = %s", COLOR_GREEN, getName(node), getName((TableNode*)$3));
}
else if(getAdInfoType(node) == getAdInfoType((TableNode*)$3)){
printdebug("%s[☺] Passed type check; %s = %s", COLOR_GREEN, getName(node), getName((TableNode*)$3));
} else {
error_type(node, (TableNode*)$3, "");
}
$$ = undefined;
}
| RETURN expression {$$ = $2;}
;
@ -644,7 +633,7 @@ expression:
$$=(TableNode*)$2;
} else {
$$=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));
error_type((TableNode*)$2, boo, "");
}
}
@ -654,8 +643,8 @@ expression:
if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) {
$$=(TableNode*)$1;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -665,8 +654,8 @@ expression:
if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) {
$$=(TableNode*)$1;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -676,8 +665,8 @@ expression:
if((TableNode*)$1 == (TableNode*)$3 && (TableNode*)$1 == integ) {
$$=(TableNode*)$1;
} else{
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -687,8 +676,8 @@ expression:
if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && ((TableNode*)$1 == integ)) {
$$=(TableNode*)$1;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -698,8 +687,8 @@ expression:
if($1 == $3 && $1 == integ) {
$$=$1;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -709,8 +698,8 @@ expression:
if($1 == $3 && $1 == boo){
$$=$1;
} else{
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -720,8 +709,8 @@ expression:
if((strcmp(getName((TableNode*)$1),getName((TableNode*)$3))==0) && $1 == boo) {
$$=$1;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -731,8 +720,8 @@ expression:
if($1 == $3 && $1==integ) {
$$=boo;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -742,8 +731,8 @@ expression:
if($1 == $3 && $1 != undefined) {
$$=boo;
} else {
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;
error_type((TableNode*)$1, (TableNode*)$3, "");
}
}
@ -766,7 +755,7 @@ expression:
$$= ((TableNode*)$1);
}
else {
printdebug("[TYPE CHECK] assignable passing up an invalid type to expression");
error_type(undefined, undefined, "Invalid type passed to expression.");
$$= ((TableNode*)$1);
}
@ -782,10 +771,9 @@ expression:
{
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));
error_type(undefined, undefined, "Invalid memOp expression (%s).", getName((TableNode*)$2));
$$=undefined;
}
}
@ -989,6 +977,51 @@ types:
void yyerror(const char *err) {
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d", err,yytext,yylloc.first_line,yylloc.first_column);
void error_type(TableNode * left, TableNode * right, const char *format, ...) {
int line = yylloc.first_line;
int column = yylloc.first_column;
if (tc_flag) {
yyerror("");
if (strcmp(format, "") == 0) {
if (asc_flag != NULL) {
fprintf(asc_flag, "(%d:%d) ** TYPE ERROR: %s != %s\n", line, column, getName(left), getName(right));
} else {
fprintf(stderr, "%s(%d:%d) ** TYPE ERROR%s: %s%s %s!= %s%s\n",
COLOR_RED, line, column, COLOR_WHITE, COLOR_YELLOW, getName(left), COLOR_WHITE, COLOR_YELLOW, getName(right), COLOR_WHITE);
}
} else {
if (asc_flag != NULL) {
fprintf(asc_flag, "(%d:%d) ** TYPE ERROR: ", line, column);
va_list args;
va_start(args, format);
vfprintf(asc_flag, format, args);
va_end(args);
fprintf(asc_flag, "\n");
} else {
fprintf(stderr, "%s(%d:%d) ** TYPE ERROR%s: %s", COLOR_RED, line, column, COLOR_WHITE, COLOR_YELLOW);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "%s\n", COLOR_WHITE);
}
}
}
}
void yyerror(const char *err) {
int line = yylloc.first_line;
int column = yylloc.first_column;
// Grammar Fallback Case
if (strcmp(err, "syntax error") == 0) {
if (asc_flag != NULL) {
fprintf(asc_flag, "(%d:%d) ** SYNTAX ERROR: Incorrect syntax at token '%s'\n", line, column, yytext);
}
else {
fprintf(stderr, "%s(%d:%d) ** SYNTAX ERROR%s: Incorrect syntax at token '%s%s%s'\n",
COLOR_RED, line, column, COLOR_WHITE, COLOR_YELLOW, yytext, COLOR_WHITE);
}
}
}