part of execute_visitor, fixes

This commit is contained in:
ProgramSnail 2023-05-09 15:24:19 +03:00
parent 5167986ddf
commit e1b9d42da1
5 changed files with 78 additions and 12 deletions

View file

@ -70,6 +70,12 @@ public:
return contexts_.back().DefineVariable(name, value_id);
}
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
if (GetLocalType(name).has_value()) {
return false;
}
return contexts_.back().DefineLocalType(name, type_id);
}
bool RemoveVariable(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
@ -97,6 +103,16 @@ public:
return std::nullopt;
}
std::optional<utils::IdType> GetLocalType(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetLocalType(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
private:
class Context {
public:
@ -114,6 +130,14 @@ private:
return true;
}
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
if (local_types_.count(name) > 0) {
return false;
}
local_types_[name] = type_id;
return true;
}
bool RemoveVariable(const std::string& name) {
return variables_.erase(name);
}
@ -128,8 +152,19 @@ private:
return variable_iter->second;
}
std::optional<utils::IdType> GetLocalType(const std::string& name) {
auto local_abstract_type_iter = local_types_.find(name);
if (local_abstract_type_iter == local_types_.end()) {
return std::nullopt;
}
return local_abstract_type_iter->second;
}
private:
std::unordered_map<std::string, utils::IdType> variables_;
std::unordered_map<std::string, utils::IdType> local_types_;
};
std::vector<Context> contexts_;