diff --git a/include/ast/scope.hpp b/include/ast/scope.hpp index aa3a57b..daaca55 100644 --- a/include/ast/scope.hpp +++ b/include/ast/scope.hpp @@ -30,7 +30,7 @@ class LocalScope : public ScopeBase { std::unordered_map local_variables; bool add_variable(const std::string &name, const ExprTypeInfo &type) override { if (!VariableNameAvailable(name, 0)) { - throw std::runtime_error("Variable name " + name + " is not available"); + return false; } local_variables[name] = type; return true; @@ -78,7 +78,7 @@ class ClassDefScope : public ScopeBase { std::unordered_map> member_functions; bool add_variable(const std::string &name, const ExprTypeInfo &type) override { if (!VariableNameAvailable(name, 0)) { - throw std::runtime_error("Variable name " + name + " is not available"); + return false; } member_variables[name] = type; return true; @@ -98,6 +98,11 @@ class ClassDefScope : public ScopeBase { if (ttl == 0 && IsKeyWord(name)) { return false; } + if (ttl == 0) { + if (member_variables.find(name) != member_variables.end()) { + return false; + } + } if (member_functions.find(name) != member_functions.end()) { return false; } @@ -139,7 +144,7 @@ class GlobalScope : public ScopeBase { } bool add_variable(const std::string &name, const ExprTypeInfo &type) override { if (!VariableNameAvailable(name, 0)) { - throw std::runtime_error("Variable name " + name + " is not available"); + return false; } global_variables[name] = type; return true; @@ -148,13 +153,15 @@ class GlobalScope : public ScopeBase { if (ttl == 0 && IsKeyWord(name)) { return false; } - if (global_variables.find(name) != global_variables.end()) { - return false; + if (ttl == 0) { + if (global_variables.find(name) != global_variables.end()) { + return false; + } } - if (classes.find(name) != classes.end()) { - return false; - } - if (classes.find(name) != classes.end()) { + // if (classes.find(name) != classes.end()) { + // return false; + // } + if (global_functions.find(name) != global_functions.end()) { return false; } return true; diff --git a/src/semantic/visitor.cpp b/src/semantic/visitor.cpp index 90835d2..31802f2 100644 --- a/src/semantic/visitor.cpp +++ b/src/semantic/visitor.cpp @@ -453,6 +453,12 @@ std::any Visitor::visitDefine_statement(MXParser::Define_statementContext *conte def_stmt->end_char_pos = context->getStop()->getCharPositionInLine(); assert(nodetype_stk.size() > 0); def_stmt->current_scope = nodetype_stk.back().second; + if (nodetype_stk.size() > 0 && (nodetype_stk.back().first == ASTNodeType::IfStatement || + nodetype_stk.back().first == ASTNodeType::WhileStatement || + nodetype_stk.back().first == ASTNodeType::ForStatement)) { + def_stmt->current_scope = std::make_shared(); + def_stmt->current_scope->parent = nodetype_stk.back().second.get(); + } nodetype_stk.push_back({ASTNodeType::DefinitionStatement, def_stmt->current_scope}); std::cerr << std::string(nodetype_stk.size() * 2, ' ') << "Adding a definition statement" << std::endl; std::string define_type_base;