upd: first version of atom

This commit is contained in:
2023-11-09 09:40:43 +08:00
parent a2a8c45af1
commit cfdc5e5a1c
5 changed files with 169 additions and 14 deletions

View File

@ -1,3 +1,36 @@
#include "names.h"
#include "utils.h"
#include "utils.h"
inline void VariableContainer::CreateFrame() {
StackScopes.push(std::unordered_map<std::string, std::any>());
}
void VariableContainer::DestroyFrame() {
if (StackScopes.empty()) throw FatalError("DestroyFrame: empty stack");
StackScopes.pop();
}
std::any VariableContainer::ReadVariable(const std::string &name) {
if (StackScopes.size()) {
auto &top = StackScopes.top();
if (top.find(name) != top.end()) return top[name];
}
if (GlobalScope.find(name) != GlobalScope.end()) return GlobalScope[name];
throw InterpretException(("ReadVariable: " + name + " not found").c_str());
}
void VariableContainer::WriteVariable(const std::string &name,
const std::any &value) {
if (StackScopes.empty()) {
GlobalScope[name] = value;
return;
}
auto &top = StackScopes.top();
if (top.find(name) != top.end()) {
top[name] = value;
return;
}
if (GlobalScope.find(name) != GlobalScope.end()) {
GlobalScope[name] = value;
return;
}
top[name] = value;
}