-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefinitionManager.cpp
executable file
·59 lines (47 loc) · 1.58 KB
/
DefinitionManager.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
58
59
#include "DefinitionManager.hpp"
#include "cons.hpp"
/// define static members
vector<DefinitionManager::DefMap> DefinitionManager::defs_stack_m;
DefinitionManager* DefinitionManager::instance = NULL;
DefinitionManager::DefinitionManager() {
add_stackframe(); /// creates global definition table
};
/// Singleton Pattern
DefinitionManager* DefinitionManager::Instance() {
if (instance == NULL) {
instance = new DefinitionManager();
}
return instance;
}
void DefinitionManager::add_stackframe() {
defs_stack_m.push_back(DefMap());
}
void DefinitionManager::pop_stackframe() throw (logic_error) {
if (defs_stack_m.size() < 1) {
throw logic_error("Logic error in the frame management of definition stack");
}
defs_stack_m.pop_back();
}
void DefinitionManager::add_definition(string key, Cell* c) throw (runtime_error) {
DefMap& def_map = defs_stack_m.back();
pair<DefMap::iterator, bool> ret = def_map.insert(pair<string, Cell*>(key, c));
if (ret.second == false) {
throw runtime_error("Can not redefine symbol!");
}
}
bool DefinitionManager::is_definition(string key) {
for (vector<DefMap>::reverse_iterator i = defs_stack_m.rbegin(); i < defs_stack_m.rend(); ++i) {
if (!((*i).find(key) == (*i).end())) {
return true;
}
}
return false;
}
Cell* DefinitionManager::get_definition(string key) const throw (runtime_error) {
for (vector<DefMap>::reverse_iterator i = defs_stack_m.rbegin(); i < defs_stack_m.rend(); ++i) {
if (!((*i).find(key) == (*i).end())) {
return (*i)[key];
}
}
throw runtime_error("Symbol is not defined!");
}