64 lines
1.5 KiB
C++
64 lines
1.5 KiB
C++
#include "include/memory.h"
|
|
using namespace std;
|
|
|
|
Memory::Memory(void) {
|
|
scopes.emplace_back();
|
|
scopes.back().depth = 0;
|
|
}
|
|
|
|
bool Memory::contains(string identifier) {
|
|
for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) {
|
|
Scope& scope = *rit;
|
|
if (scope.vars.contains(identifier)) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Memory::contains_top(string identifier) {
|
|
Scope& top = scopes.back();
|
|
return top.vars.contains(identifier);
|
|
}
|
|
|
|
void Memory::add_scope(ScopeType type) {
|
|
Scope& top = scopes.back();
|
|
scopes.emplace_back();
|
|
scopes.back().depth = top.depth + 1;
|
|
scopes.back().type = type;
|
|
}
|
|
|
|
void Memory::remove_scope(void) {
|
|
scopes.pop_back();
|
|
}
|
|
|
|
MemoryVar& Memory::get(string identifier) {
|
|
for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) {
|
|
Scope& scope = *rit;
|
|
if (scope.vars.contains(identifier)) return scope.vars[identifier];
|
|
}
|
|
|
|
throw exception();
|
|
}
|
|
|
|
void Memory::declare(string identifier, Type type) {
|
|
Scope& top = scopes.back();
|
|
top.vars[identifier].type = type;
|
|
}
|
|
|
|
void Memory::update(string identifier, EvalResult value) {
|
|
for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) {
|
|
Scope& scope = *rit;
|
|
if (scope.vars.contains(identifier)) {
|
|
scope.vars[identifier].value = value;
|
|
scope.vars[identifier].initialized = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
throw exception();
|
|
}
|
|
|
|
Scope& Memory::_debug_top(void) {
|
|
Scope& top = scopes.back();
|
|
return top;
|
|
} |