fix fatal errors in symbol check

This commit is contained in:
2024-08-13 09:12:05 +00:00
parent a39625e4f2
commit 00501dae02
2 changed files with 22 additions and 9 deletions

View File

@ -30,7 +30,7 @@ class LocalScope : public ScopeBase {
std::unordered_map<std::string, ExprTypeInfo> 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<std::string, std::shared_ptr<FunctionScope>> 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;

View File

@ -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<LocalScope>();
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;