diff --git a/src/grammar.y b/src/grammar.y index a7d173e..e98f9d0 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -23,7 +23,13 @@ extern bool tc_flag; int token_tracker; TableNode * tn; - void error_type(TableNode * left, TableNode * right, const char *format, ...); + typedef enum { + ERROR_RUNTIME = 1, + ERROR_SYNTAX = 2, + ERROR_TYPE = 3, + ERROR_UNDEFINED = 4 + } ErrorType; + void throw_error(ErrorType error_type, const char *format, ...); %} %union { @@ -171,9 +177,9 @@ definition: printdebug("see function def rule 1\n"); TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { - error_type(undefined, undefined, "Undefined node declared."); + printdebug("Undefined node declared."); }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ - error_type(undefined, undefined, "Not a valid function declaration."); + throw_error(ERROR_SYNTAX, "Not a valid function declaration."); } else { printdebug("setting as keyword to true"); @@ -187,7 +193,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) { - error_type(undefined, undefined, "Undefined parameter in function definition."); + throw_error(ERROR_TYPE, "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 @@ -198,7 +204,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 - error_type(parameter, undefined, "Invalid type (%s) of parameter in function definition.", getAdInfo(parameter)); + throw_error(ERROR_TYPE, "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){ @@ -226,7 +232,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 - error_type(entry, undefined, "Invalid type (%s) of parameter in function definition.", getAdInfo(entry)); + throw_error(ERROR_TYPE, "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)); @@ -257,9 +263,9 @@ definition: } idlist R_PAREN ASSIGN sblock { TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1))); if ($8 == undefined) { - error_type(undefined, undefined, "Expected %s as return type but got undefined (possibly NULL). Differing return types in function.", getName(expected)); + throw_error(ERROR_TYPE, "Expected %s as return type but got undefined (possibly NULL). Differing return types in function.", getName(expected)); } else if ($8 != expected) { - error_type(undefined, undefined, "Expected %s as return type but got %s. Differing return types in function.", getName(expected), getName($8)); + throw_error(ERROR_TYPE, "Expected %s as return type but got %s. Differing return types in function.", getName(expected), getName($8)); } else { printdebug("CORRECT RETURN TYPE!!!"); } @@ -274,7 +280,7 @@ function_declaration: CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); } else{ - error_type(undefined, undefined, "Function declatation (%s) is not a valid function type", $2); + throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $2); CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); } @@ -286,7 +292,7 @@ function_declaration: CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); } else{ - error_type(undefined, undefined, "Function declatation (%s) is not a valid function type", $3); + throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $3); CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); } @@ -431,7 +437,7 @@ declaration: printdebug("ID/TYPE: %s, ID: %s", getName((TableNode*)$1), $3) ; int d = getAdInfoType((TableNode*)$1); if(d == TYPE_UNDEFINED) { - error_type(undefined, undefined, "Undefined type passed in declaration list"); + printdebug("Undefined type passed in declaration list"); CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } else if(d == TYPE_FUNCTION_TYPE) { @@ -454,7 +460,7 @@ declaration: d = TYPE_PRIMITIVE; CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); }else { - error_type(undefined, undefined, "Invalid type passed in declaration list."); + printdebug("Invalid type passed in declaration list."); CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)); } } @@ -552,7 +558,7 @@ simple_statement: node = getTypeEntry((TableNode*)$1); } else{ - error_type(undefined, undefined, "Invalid type passed to assignable."); + printdebug("Invalid type passed to assignable."); node = undefined; } @@ -564,7 +570,7 @@ simple_statement: 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, ""); + throw_error(ERROR_TYPE, "%s != %s", getName(node), getName((TableNode*)$3)); } $$ = undefined; @@ -633,7 +639,7 @@ expression: $$=(TableNode*)$2; } else { $$=undefined; - error_type((TableNode*)$2, boo, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$2), getName(boo)); } } @@ -644,7 +650,7 @@ expression: $$=(TableNode*)$1; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -655,7 +661,7 @@ expression: $$=(TableNode*)$1; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -666,7 +672,7 @@ expression: $$=(TableNode*)$1; } else{ $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -677,7 +683,7 @@ expression: $$=(TableNode*)$1; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -688,7 +694,7 @@ expression: $$=$1; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -699,7 +705,7 @@ expression: $$=$1; } else{ $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -710,7 +716,7 @@ expression: $$=$1; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -721,7 +727,7 @@ expression: $$=boo; } else { $$=undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -732,7 +738,7 @@ expression: $$=boo; } else { $$ = undefined; - error_type((TableNode*)$1, (TableNode*)$3, ""); + throw_error(ERROR_TYPE, "%s != %s", getName((TableNode*)$1), getName((TableNode*)$3)); } } @@ -755,7 +761,7 @@ expression: $$= ((TableNode*)$1); } else { - error_type(undefined, undefined, "Invalid type passed to expression."); + printdebug("Invalid type passed to expression."); $$= ((TableNode*)$1); } @@ -773,7 +779,7 @@ expression: if(d == TYPE_ARRAY_TYPE || d == TYPE_ARRAY || d == TYPE_RECORD_TYPE || d == TYPE_RECORD) { $$ = addr; } else { - error_type(undefined, undefined, "Invalid memOp expression (%s).", getName((TableNode*)$2)); + throw_error(ERROR_TYPE, "Invalid memOp expression (%s).", getName((TableNode*)$2)); $$=undefined; } } @@ -977,35 +983,48 @@ types: -void error_type(TableNode * left, TableNode * right, const char *format, ...) { +void throw_error(ErrorType error_type, const char *format, ...) { int line = yylloc.first_line; int column = yylloc.first_column; + char * error_name = ""; + + switch (error_type) { + case ERROR_RUNTIME: + error_name = malloc(strlen("RUNTIME") + 1); + strcpy(error_name, "RUNTIME"); + break; + case ERROR_SYNTAX: + error_name = malloc(strlen("SYNTAX") + 1); + strcpy(error_name, "SYNTAX"); + break; + case ERROR_TYPE: + error_name = malloc(strlen("TYPE") + 1); + strcpy(error_name, "TYPE"); + break; + case ERROR_UNDEFINED: + error_name = malloc(strlen("UNDEFINED") + 1); + strcpy(error_name, "UNDEFINED"); + break; + } + + 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); - } + if (asc_flag != NULL) { + fprintf(asc_flag, "(%d:%d) ** %s ERROR: ", line, column, error_name); + va_list args; + va_start(args, format); + vfprintf(asc_flag, format, args); + va_end(args); + fprintf(asc_flag, "\n"); } 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); - } + fprintf(stderr, "%s(%d:%d) ** %s ERROR%s: %s", COLOR_RED, line, column, error_name, COLOR_WHITE, COLOR_YELLOW); + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fprintf(stderr, "%s\n", COLOR_WHITE); } } }