-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scopes.cpp
57 lines (42 loc) · 1.21 KB
/
Scopes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Scopes.hpp"
#include "Expression.hpp"
Scopes::Scopes() {
this->curLabel = GlobalLabel;
this->variables.push_front(VariableMap());
this->commands[this->curLabel] = CommandVector();
}
std::string Scopes::_createLabel() {
this->ss << 'L' << this->nr++;
const std::string label = this->ss.str();
this->ss.clear();
return label;
}
void Scopes::pushScope() {
this->labels.push(this->curLabel);
this->curLabel = _createLabel();
this->variables.push_front(VariableMap());
this->commands[this->curLabel] = CommandVector();
}
void Scopes::popScope() {
this->lastLabel = this->curLabel;
this->variables.pop_front();
this->curLabel = this->labels.top();
this->labels.pop();
}
void Scopes::addCmd(Command* cmd) {
this->commands[this->curLabel].emplace_back(std::unique_ptr<Command>(cmd));
}
bool Scopes::createVar(const std::string& name, int16 size) {
if (this->getVar(name) != nullptr)
return false;
this->variables.front()[name] = patch::make_unique<Variable>(name, this->stackSize, size);
return true;
}
Variable* Scopes::getVar(const std::string& name) {
for (VariableMap& vmap : this->variables) {
auto it = vmap.find(name);
if (it != vmap.end())
return it->second.get();
}
return nullptr;
}