int semantic_check(node *ast) { if(!ast) return 0; int btype; switch((int)ast->kind) { case UNKNOWN: break; case NSCOPE: lhs = semantic_check(ast->scope.declarations); if(lhs == -1) return -1; rhs = semantic_check(ast->scope.statements); if(rhs == -1) return -1; if(lhs > rhs) return lhs; else return rhs; break; case NDECLARATIONS: lhs = semantic_check(ast->declarations.declarations); if(lhs == -1) return -1; rhs = semantic_check(ast->declarations.declaration); if(rhs == -1) return -1; else return rhs; break; case NSTATEMENTS: lhs = semantic_check(ast->statements.statements); if(lhs == -1) return -1; rhs = semantic_check(ast->statements.statement); if(rhs == -1) return -1; else return rhs; break; case NTYPE_DECLARATION: if(isVarDeclared(sym_table, ast->type_declaration.id, scopeNum)) { fprintf(errorFile, "Error: Variable cannot be re-declared within the same scope\n"); errorOccurred = 1; return -1; } else return semantic_check(ast->type_declaration.type); break; case NASSIGN_DECLARATION: lhs = semantic_check(ast->assign_declaration.type); if(lhs == -1) return -1; rhs = semantic_check(ast->assign_declaration.expression); if(rhs == -1) return -1; if(isVarDeclared(sym_table, ast->assign_declaration.id, scopeNum)) { fprintf(errorFile, "Error: Variable cannot be re-declared within the same scope\n"); errorOccurred = 1; return -1; } if(lhs == rhs) return lhs; else if((lhs == IVEC2 || lhs == IVEC3 || lhs == IVEC4) && (rhs == INT)) return INT; else if((lhs == BVEC2 || lhs == BVEC3 || lhs == BVEC4) && (rhs == BOOL)) return BOOL; else if((lhs == VEC2 || lhs == VEC3 || lhs == VEC4) && (rhs == FLOAT)) return FLOAT; else { fprintf(errorFile, "Error: Declaration failed, type mismatch in assignment\n"); errorOccurred = 1; return -1; } break; case NCONST_DECLARATION: lhs = semantic_check(ast->const_declaration.type); if(lhs == -1) return -1; rhs = semantic_check(ast->const_declaration.expression); if(rhs == -1) return -1; type_class = get_tClass(sym_table, ast->const_declaration.type->id_variable.id); if(ast->const_declaration.type->kind != NINT_EXPR && ast->const_declaration.type->kind != NFLOAT_EXPR && ast->const_declaration.type->kind != NBOOL_EXPR && ast->const_declaration.type->kind != NTYPE_EXPR && ast->const_declaration.type->kind != NARRAY_VARIABLE && type_class != _CONST && type_class != UNIFORM) { fprintf(errorFile, "Error: 'const' qualified variables must be initialized with a literal value or with a uniform variable\n"); errorOccurred = 1; return -1; } if(lhs == rhs) return lhs; else if((lhs == IVEC2 || lhs == IVEC3 || lhs == IVEC4) && (rhs == INT)) return INT; else if((lhs == VEC2 || lhs == VEC3 || lhs == VEC4) && (rhs == FLOAT)) return FLOAT; else if((lhs == BVEC2 || lhs == BVEC3 || lhs == BVEC4) && (rhs == BOOL)) return BOOL; else { fprintf(errorFile, "Error: Type mismatch in assignment\n"); errorOccurred = 1; return -1; } break; case NASSIGN_STATEMENT: lhs = semantic_check(ast->assign_statement.variable); if(lhs == -1) return -1; rhs = semantic_check(ast->assign_statement.expression); if(rhs == -1) return -1; else return CheckTypes(rhs, lhs); break; case NIF_STATEMENT: lhs = semantic_check(ast->if_statement.condition); if(lhs == -1) return -1; if(lhs == BOOL) semantic_check(ast->if_statement.statement); else { fprintf(errorFile, "Error: Condition must be of type 'bool'\n"); errorOccurred = 1; return -1; } break; case NIF_ELSE_STATEMENT: lhs = semantic_check(ast->if_else_statement.condition); if(lhs == -1) return -1; if(lhs == BOOL) { semantic_check(ast->if_else_statement.statement); semantic_check(ast->if_else_statement.else_statement); } else { fprintf(errorFile, "Error: Condition must be of type 'bool'\n"); errorOccurred = 1; return -1; } break; case NSCOPE_STATEMENT: scopeNum++; check_prog_scope = semantic_check(ast->prog_scope.scope); scopeNum--; return check_prog_scope; break; //unary expression case NUNARY_EXPR: rhs = semantic_check(ast->unary_expr.right); if(rhs == -1) return -1; if(ast->unary_expr.op == MINUS_OPS) { if(rhs == BOOL || rhs == BVEC2|| rhs == BVEC3|| rhs == BVEC4){ fprintf(errorFile, "Error: All operands to arithmetic operators must have arithmetic types\n"); errorOccurred = 1; return -1; } else return rhs; } if(ast->unary_expr.op == NOT_OPS) { if(rhs != BOOL || rhs != BVEC2 || rhs != BVEC3 || rhs != BVEC4) { fprintf(errorFile, "Error: All operands to logical operators must have boolean types\n"); errorOccurred = 1; return -1; } else return rhs; } break; case NBINARY_EXPR: rhs = semantic_check(ast->binary_expr.right); if(rhs == -1) return -1; lhs = semantic_check(ast->binary_expr.left); btype = CheckTypes(rhs, lhs); if(lhs == -1){ return -1; }else if(btype == -1){ fprintf(errorFile, "Error: The operands to binary operators must have same base types\n"); errorOccurred = 1; return -1; }else if(vectorChecking(rhs) == 1 && vectorChecking(lhs) == 1){ if(vectorCompare(rhs, lhs) == 0){ fprintf(errorFile, "Error: The vector operands to binary operators must have same order\n"); errorOccurred = 1; return -1; }else{ btype = BOOL; ast->binary_expr.type = btype; return btype; } }else if(ast->binary_expr.op == AND_OPS || ast->binary_expr.op == OR_OPS ||ast->binary_expr.op == EQ_OPS || ast->binary_expr.op == NEQ_OPS){ if(rhs != BOOL || rhs != BVEC2 || rhs != BVEC3 || rhs != BVEC4 || lhs != BOOL || lhs != BVEC2 || lhs != BVEC3 || lhs != BVEC4){ fprintf(errorFile, "Error: All operands to logical operators must have boolean types\n"); errorOccurred = 1; return -1; }else if((vectorChecking(rhs) && !vectorChecking(lhs)) || (!vectorChecking(rhs) && vectorChecking(lhs))){ fprintf(errorFile, "Error: The operands to logical operators must be both vectors or both scalars\n"); errorOccurred = 1; return -1; }else{ btype = BOOL; ast->binary_expr.type = btype; return btype; } }else if(ast->binary_expr.op == LESS_OPS || ast->binary_expr.op == LEQ_OPS ||ast->binary_expr.op == GTR_OPS || ast->binary_expr.op == GEQ_OPS || ast->binary_expr.op == PLUS_OPS || ast->binary_expr.op == MINUS_OPS || ast->binary_expr.op == DIVIDE_OPS || ast->binary_expr.op == POW_OPS || ast->binary_expr.op == TIMES_OPS){ if(rhs == BOOL || rhs == BVEC2|| rhs == BVEC3|| rhs == BVEC4 || lhs == BOOL || lhs == BVEC2|| lhs == BVEC3|| lhs == BVEC4){ fprintf(errorFile, "Error: All operands to arithmetic operators must have arithmetic types.\n"); errorOccurred = 1; return -1; }else if( ast->binary_expr.op != TIMES_OPS && (vectorChecking(rhs) == 1 || vectorChecking(lhs) == 1)){ fprintf(errorFile, "Error: The operands to arithmetic operators (except for times) must be both vectors or both scalars\n"); errorOccurred = 1; return -1; }else{ btype = BOOL; ast->binary_expr.type = btype; return btype; } } else return -1; break; case NBRACKETS_EXPR: return semantic_check(ast->brackets_expr.expression); break; case NFUNC_EXPR: rhs = semantic_check(ast->func_expr.arguments_opt); if(rhs == -1) return -1; switch(ast->func_expr.func) { case 0: if(rhs == IVEC3 || rhs == IVEC4) return INT; else if(rhs == VEC3 || rhs == VEC4) return FLOAT; else { fprintf(errorFile, "Error: Function argument doesn't match as expected ('db3' supports arguments of type 'vec3', 'vec4', 'ivec3' & 'ivec4')\n"); errorOccurred = 1; return -1; } break; case 1: if(rhs == VEC4) return VEC4; else { fprintf(errorFile, "Error: Function argument doesn't match as expected ('lit' only supports argument of type 'vec4')\n"); errorOccurred = 1; return -1; } break; case 2: if(rhs == FLOAT || rhs == INT) return FLOAT; else { fprintf(errorFile, "Error: Function argument doesn't match as expected ('rsq' supports arguments of type 'int' & 'float')\n"); errorOccurred = 1; return -1; } break; default: fprintf(errorFile, "Error: Function name doesn't match as expected (supported function names - 'db3', 'lit' & 'rsq')\n"); errorOccurred = 1; return -1; break; } break; case NTYPE_EXPR: lhs = semantic_check(ast->type_expr.type); if(lhs == -1) return -1; rhs = semantic_check(ast->type_expr.arguments_opt); if(rhs == -1) return -1; numArgs = countArg(ast->type_expr.arguments_opt); if((lhs == IVEC2 && numArgs == 2) || (lhs == IVEC3 && numArgs == 3) || (lhs == IVEC4 && numArgs == 4) || (lhs == BVEC2 && numArgs == 2) || (lhs == BVEC3 && numArgs == 3) || (lhs == BVEC4 && numArgs == 4) || (lhs == VEC2 && numArgs == 2) || (lhs == VEC3 && numArgs == 3) || (lhs == VEC4 && numArgs == 4) || (lhs == BOOL && numArgs == 1) || (lhs == INT && numArgs == 1) || (lhs == FLOAT && numArgs == 1)) ; else { fprintf(errorFile, "Error: Costructors for basic types (bool, int, float) must have one argument and vector types must have as many arguments as there are elements in the vector\n"); errorOccurred = 1; return -1; } if(lhs == rhs) return lhs; else if((lhs == IVEC2 || lhs == IVEC3 || lhs == IVEC4) && (rhs == INT)) return INT; else if((lhs == VEC2 || lhs == VEC3 || lhs == VEC4) && (rhs == FLOAT)) return FLOAT; else if((lhs == BVEC2 || lhs == BVEC3 || lhs == BVEC4) && (rhs == BOOL)) return BOOL; else { fprintf(errorFile, "Error: Type mismatch found\n"); errorOccurred = 1; return -1; } break; case NVAR_EXPR: return semantic_check(ast->var_expr.variable); break; case NINT_EXPR: return INT; break; case NFLOAT_EXPR: return FLOAT; break; case NBOOL_EXPR: return BOOL; break; case NID_VARIABLE: id = ast->id_variable.id; if(!isVarDeclaredInScope(sym_table, id, scopeNum)) { fprintf(errorFile, "Error: Variable cannot be used before it is declared\n"); errorOccurred = 1; return -1; } else if(strcmp(id, "TEMP") == 0 || strcmp(id, "ADDRESS") == 0) { fprintf(errorFile, "Error: Reserved words can not be used as variable\n"); errorOccurred = 1; return -1; } else { type_class = get_tClass(sym_table, id); if(type_class == _CONST) { fprintf(errorFile, "Error: Variable cannot be used before it is declared\n"); errorOccurred = 1; return -1; } return get_data_type(sym_table, id); } break; case NARRAY_VARIABLE: x = ast->array_variable.index; type = get_data_type(sym_table, ast->array_variable.id); if(type == IVEC2 || type == IVEC3 || type == IVEC4 || type == BVEC2 || type == BVEC3 || type == BVEC4 || type == VEC2 || type == VEC3 || type == VEC4) ; else { fprintf(errorFile, "Error: Only 'vec' type supported\n"); errorOccurred = 1; return -1; } if((type == IVEC2 && x < 2) || (type == IVEC3 && x < 3) || (type == IVEC4 && x < 4) || (type == BVEC2 && x < 2) || (type == BVEC3 && x < 3) || (type == BVEC4 && x < 4) || (type == VEC2 && x < 2) || (type == VEC3 && x < 3) || (type == VEC4 && x < 4)) ; else { fprintf(errorFile, "Error: Index limit exceeded\n"); errorOccurred = 1; return -1; } if(type == IVEC2 || type == IVEC3 || type == IVEC4) return INT; if(type == BVEC2 || type == BVEC3 || type == BVEC4) return BOOL; if(type == VEC2 || type == VEC3 || type == VEC4) return FLOAT; break; case NARGS_ARGUMENTS: lhs = semantic_check(ast->args_arguments.arguments); if(lhs == -1) return -1; rhs = semantic_check(ast->args_arguments.expression); if(rhs == -1) return -1; else if(lhs == rhs) return lhs; else { fprintf(errorFile, "Error: Mismatch in arguments\n"); errorOccurred = 1; return -1; } break; case NEXPR_ARGUMENTS: return semantic_check(ast->expr_arguments.expression); break; case NARGUMENTS_OPT: return semantic_check(ast->arguments_opt.arguments); break; case NTYPE: return ast->type.type_kind; break; case NPROG_SCOPE: scopeNum++; check_prog_scope = semantic_check(ast->prog_scope.scope); scopeNum--; return check_prog_scope; break; default: return -1; break; } return 0; // failed checks }
/*********************************************************************** * Main program for the Compiler **********************************************************************/ int main (int argc, char *argv[]) { getOpts (argc, argv); /* Set up and apply command line options */ /*********************************************************************** * Compiler Initialization. * * If explicit initialization of any phase of the compilation is needed, * calls to initialization routines in the applicable modules are placed * here. **********************************************************************/ errorOccurred = FALSE; /*********************************************************************** * Start the Compilation **********************************************************************/ if (dumpSource) sourceDump(); /* Phase 1: Scanner. In phase 2 and after the following code should be * removed */ /* while (yylex()) if (errorOccurred) break; */ /* Phase 2: Parser -- should allocate an AST, storing the reference in the * global variable "ast", and build the AST there. */ if(1 == yyparse()) { return 0; // parse failed } build_table(ast); insert_predef(); //display(); if(semantic_check(ast)==-1){ //printf(); fprintf(stderr, "SYMANTIC CHECK FAILED\n"); } /* Phase 3: Call the AST dumping routine if requested */ if (dumpAST) ast_print(ast); /* Phase 4: Add code to call the code generation routine */ /* TODO: call your code generation routine here */ if (errorOccurred) fprintf(outputFile,"Failed to compile\n"); else // genCode(ast); ; /*********************************************************************** * Post Compilation Cleanup **********************************************************************/ /* Make calls to any cleanup or finalization routines here. */ ast_free(ast); /* Clean up files if necessary */ if (inputFile != DEFAULT_INPUT_FILE) fclose (inputFile); if (errorFile != DEFAULT_ERROR_FILE) fclose (errorFile); if (dumpFile != DEFAULT_DUMP_FILE) fclose (dumpFile); if (traceFile != DEFAULT_TRACE_FILE) fclose (traceFile); if (outputFile != DEFAULT_OUTPUT_FILE) fclose (outputFile); if (runInputFile != DEFAULT_RUN_INPUT_FILE) fclose (runInputFile); return 0; }