|
|
|
@ -57,8 +57,8 @@ class Scope {
|
|
|
|
|
return var;
|
|
|
|
|
} else {
|
|
|
|
|
auto ptr = new Variable();
|
|
|
|
|
vars_[name] = std::unique_ptr<Variable>(ptr);
|
|
|
|
|
var_names_[ptr] = name;
|
|
|
|
|
name_to_var_[name] = std::unique_ptr<Variable>(ptr);
|
|
|
|
|
var_to_name_[ptr] = name;
|
|
|
|
|
return GetVariable(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -70,8 +70,8 @@ class Scope {
|
|
|
|
|
* from it's parent scope. Return nullptr if not found.
|
|
|
|
|
*/
|
|
|
|
|
Variable* GetVariable(const std::string& name) const {
|
|
|
|
|
auto it = vars_.find(name);
|
|
|
|
|
if (it != vars_.end()) {
|
|
|
|
|
auto it = name_to_var_.find(name);
|
|
|
|
|
if (it != name_to_var_.end()) {
|
|
|
|
|
return it->second.get();
|
|
|
|
|
} else if (parent_ != nullptr) {
|
|
|
|
|
return parent_->GetVariable(name);
|
|
|
|
@ -86,21 +86,21 @@ class Scope {
|
|
|
|
|
* Find if there is a Variable in this scope and it's parent scope
|
|
|
|
|
*/
|
|
|
|
|
bool HasVariable(const std::string& name) const {
|
|
|
|
|
return (vars_.find(name) != vars_.end() ||
|
|
|
|
|
return (name_to_var_.find(name) != name_to_var_.end() ||
|
|
|
|
|
(parent_ && parent_->HasVariable(name)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetVariableName(Variable* const var) const {
|
|
|
|
|
try {
|
|
|
|
|
return var_names_.at(var);
|
|
|
|
|
return var_to_name_.at(var);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::unordered_map<Variable*, std::string> var_names_;
|
|
|
|
|
std::unordered_map<std::string, std::unique_ptr<Variable>> vars_;
|
|
|
|
|
std::unordered_map<Variable*, std::string> var_to_name_;
|
|
|
|
|
std::unordered_map<std::string, std::unique_ptr<Variable>> name_to_var_;
|
|
|
|
|
std::shared_ptr<Scope> parent_{nullptr};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|