Skip to content

Commit

Permalink
Adding undefined object detection, routinecallValue, and debug cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PATH242 committed Apr 11, 2023
1 parent faa13ec commit e0e0ada
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
48 changes: 39 additions & 9 deletions semantic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ namespace analyzer
node->type->accept(this);
}
ast::Type *var_type = actual_type;
//std::cout<<"I'm a param and my name is " << node->name << " and type is " << type_to_string(var_type) << '\n';
varDeclSymbolTable[node->name] = var_type;
varStack.push_back({node->name, var_type});
routine_vars_n ++ ;
Expand Down Expand Up @@ -171,8 +170,6 @@ namespace analyzer
{
var_type = expr_type;
}
//std::cout<<"I'm a var and my name is " << node->name << " and type is " << type_to_string(var_type) << '\n';
//std::cout<<"Expression type is " << type_to_string(expr_type) << '\n';
varDeclSymbolTable[node->name] = var_type;
varStack.push_back({node->name, var_type});
routine_vars_n ++;
Expand All @@ -192,8 +189,6 @@ namespace analyzer
}
rhs_type = actual_type;
typecheck_types(lhs_type, rhs_type);
std::cout <<node->var->name << '\n';
std::cout<<type_to_string(lhs_type) << ' ' << type_to_string(rhs_type) << '\n';
};
void Semantic::visitRoutineCall(ast::RoutineCall *node)
{
Expand Down Expand Up @@ -422,16 +417,13 @@ namespace analyzer
err_undefined_obj(node->name);
}
current_var_type = varDeclSymbolTable[node->name];
//std::cout<<"I'm current var type: " << type_to_string(current_var_type) << '\n';
if(node->accesses){
for (auto AV : node->accesses->accesses)
{
if(AV)
AV->accept(this);
}
}
//std::cout<< node->name << '\n';
//std::cout<<"I'm current var type: " << type_to_string(current_var_type) << '\n';
actual_type = current_var_type;
};
void Semantic::visitArrayAccess(ast::ArrayAccess* node){
Expand All @@ -449,7 +441,6 @@ namespace analyzer
typecheck_types(actual_type, new ast::IntegerType());
if(auto casted_type = dynamic_cast<ast::ArrayType *>(my_current_type)){
current_var_type = casted_type->type;
std::cout<<"current var type : " << type_to_string(my_current_type) << '\n';
}
}

Expand Down Expand Up @@ -482,4 +473,43 @@ namespace analyzer
}
}
}
void Semantic::visitRoutineCallValue(ast::RoutineCallValue *node)
{
if (routineDeclTable.find(node->name) == routineDeclTable.end())
{
err_undefined_obj(node->name);
}
// for (auto arg : node->args->exprs)
// {
// if (arg)
// {
// arg->accept(this);

// }
// }
auto routine = routineDeclTable[node->name];
int size1 = 0, size2 = 0;
if(node->args){
size1 = node->args->exprs.size();
}
if(routine->params){
size2 = routine->params->decls.size();
}
if(size1 != size2){
err_wrong_params_number(size1, size2);
}
if(node->args && routine->params){
for (int i = 0; i < node->args->exprs.size(); i++){
if(routine->params->decls[i]->type && node->args->exprs[i]){
if(node->args->exprs[i]){
actual_type = nullptr;
node->args->exprs[i]->accept(this);
}
if(actual_type){
typecheck_types(actual_type,routine->params->decls[i]->type);
}
}
}
}
};
}
2 changes: 1 addition & 1 deletion semantic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace analyzer
void visitIntegerValue(ast::IntegerValue *p);
void visitRealValue(ast::RealValue *p);
void visitBooleanValue(ast::BooleanValue *p);
void visitRoutineCallValue(ast::RoutineCallValue *p){} // not implemented
void visitRoutineCallValue(ast::RoutineCallValue *p);

void visitType(ast::Type *p){}
void visitPrimitiveType(ast::PrimitiveType *p){}
Expand Down
2 changes: 1 addition & 1 deletion tests/inputs/invalid/syntax/undefined_routine.crbc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
routine div (x : real, y : real) : integer is
routine div (x : real, y : real) : real is

var z : real;
z := x / y;
Expand Down

0 comments on commit e0e0ada

Please sign in to comment.