-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolTable.cpp
138 lines (119 loc) · 2.87 KB
/
SymbolTable.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "SymbolTable.h"
#include <algorithm>
SymbolTable::SymbolTable(SymbolTable* outer)
: outer_(outer)
{
if (outer) {
level_ = outer->level_ + 1;
} else {
level_ = 0;
}
}
HashSymtab::HashSymtab(SymbolTable* outer, bool deeper)
: SymbolTable(outer), deeper_(deeper)
{}
SymtabEntry* HashSymtab::findInCurr(const std::string& name)
{
auto res = table_.find(name);
if (res != table_.end()) {
// found
return &res->second;
} else {
return nullptr;
}
}
SymtabEntry* HashSymtab::find(const std::string& name)
{
auto ptr = findInCurr(name);
if (ptr) {
return ptr;
} else {
if (level_ == 0) {
return nullptr;
} else {
return outer_->find(name);
}
}
}
bool HashSymtab::insert(const std::string& name,
const std::shared_ptr<Type>& type, Token const* tok)
{
if (deeper_) {
auto res = outer_->findInCurr(name);
if (res) {
return false;
}
}
auto pr = table_.insert({name, {type, tok, level_}});
if (pr.second) {
pr.first->second.pname = &pr.first->first;
}
return pr.second; // denote whether the insertion took place
}
size_t HashSymtab::erase(const std::string& n)
{
return table_.erase(n);
}
HashSymtab::iterator HashSymtab::begin()
{
return table_.begin();
}
HashSymtab::iterator HashSymtab::end()
{
return table_.end();
}
ListSymtab::ListSymtab(SymbolTable* outer)
: SymbolTable(outer)
{}
SymtabEntry* ListSymtab::findInCurr(const std::string& name)
{
auto found = std::find_if(table_.begin(), table_.end(), [&name](const value_type& v) {
return v.first == name;
});
if (found != table_.end()) {
return &found->second;
} else {
return nullptr;
}
}
SymtabEntry* ListSymtab::find(const std::string& name)
{
auto ptr = findInCurr(name);
if (ptr) {
return ptr;
} else {
if (level_ == 0) {
return nullptr;
} else {
return outer_->find(name);
}
}
}
bool ListSymtab::insert(const std::string& name,
const std::shared_ptr<Type>& type, Token const* tok)
{
// auto found = find(name);
if (findInCurr(name)) {
return false;
} else {
table_.push_back({name, {type, tok, level_}});
table_.back().second.pname = &table_.back().first;
return true;
}
}
void ListSymtab::push_back(const std::string& n, const std::shared_ptr<Type>& t,
Token const* tok)
{
table_.push_back({n, {t, tok, level_}});
}
size_t ListSymtab::erase(const std::string& name)
{
auto it = std::find_if(table_.begin(), table_.end(), [&name](const value_type& v) {
return v.first == name;
});
if (it == table_.end()) {
return 0;
}
table_.erase(it);
return 1;
}