-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.cpp
More file actions
239 lines (202 loc) · 6.62 KB
/
Copy pathSymbolTable.cpp
File metadata and controls
239 lines (202 loc) · 6.62 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "SymbolTable.h"
#include "hw3_output.hpp"
#include <iostream>
#define MAIN "main"
using namespace std;
Symbol::Symbol(const std::string& name, const ProtoType& type, int offset) : name(name), type(type.clone()), offset(offset), value() {} //check that string and type dont die after scope ends
Symbol::~Symbol() {
delete this->type;
}
const std::string& Symbol::getName() const {
return this->name;
}
ProtoType& Symbol::getType() const {
return *(this->type);
}
int Symbol::getOffset() const {
return this->offset;
}
Symbol::Symbol(const Symbol &sym) : name(sym.getName()), type(sym.type->clone()), offset(sym.offset), value(sym.value) {}
void Symbol::setValue(std::string value) {
//cout << "this is setValue :) setting value of " << value << endl;
this->value = value;
//cout << "new value is now " << this->value << endl;
}
std::string Symbol::getValue() {
//cout << "this is getValue :) returning the value of " << this->value << endl;
return this->value;
}
std::ostream& operator<< (std::ostream &os, const Symbol &symbol) {
std::string space(" ");
std::string type = symbol.getType().typeToString();
return os << symbol.getName() << space << type << space << std::to_string(symbol.getOffset());
}
std::ostream& operator<< (std::ostream &os, const SymbolTableEntry &symbolTableEntry) {
std::string carriage_return("\n");
for(const Symbol& entry : symbolTableEntry) {
os << entry << carriage_return;
}
return os;
}
SymbolTableEntry::SymbolTableEntry(const SymbolTableEntry& other) : std::vector<Symbol>(), lastParameter(other.lastParameter), lastOffset(other.lastOffset) {
for(unsigned long i = 0; i < other.size(); i++) {
this->push_back(other[i]);
}
}
void SymbolTable::pop() {
this->pop_front();
}
void SymbolTable::push(const SymbolTableEntry &entry) {
this->push_front(entry);
}
void SymbolTable::push(SymbolTableEntry &entry) {
this->push_front(entry);
}
SymbolTableEntry &SymbolTable::top() {
return this->front();
}
void SymbolTable::pushNewEntry() {
// cout << "pushed new Entry" << endl;
if(!this->empty()) {
// cout << this->top().getLastOffset() << endl;
this->push(SymbolTableEntry(this->top().getLastOffset())); //check if new is needed
} else {
this->push(SymbolTableEntry()); //check if new is needed
}
}
void SymbolTable::insertAtTop(const std::string &name, const ProtoType &type, bool isParameter) {
int offset = 0;
// cout << name << endl;
if (!type.isFunc()) { //check what happens when entry is empty
SymbolTableEntry& entry = this->top();
if (isParameter) {
offset = entry.getLastParameter() - 1;
entry.lastParameterInc();
} else {
offset = this->top().getLastOffset() + 1;
entry.lastOffsetInc();
// cout << this->top();
}
}
this->top().push_back(Symbol(name, type, offset)); //check if new is needed
}
SymbolTable::SymbolTable() : std::deque<SymbolTableEntry>() {
this->pushNewEntry();
}
bool SymbolTable::findId(const string &id) const {
// cout << id << endl;
for (const SymbolTableEntry& entry : *this) {
// cout << entry;
for (const Symbol& sym : entry) {
// cout << sym << endl;
// cout << sym.getName() << endl;
if (sym.getName() == id) {
return true;
}
}
}
return false;
}
bool SymbolTable::isMainExists() const {
if(this->findId(MAIN)) {
ProtoType& mainType = this->getIdType(MAIN);
if (mainType.isFunc()) {
Func& main = dynamic_cast<Func&>(mainType);
if (main.getReturnType().isVoid() && main.getStrList().empty()) {
return true;
}
}
}
return false;
}
ProtoType &SymbolTable::getIdType(const string &id) const {
for (const SymbolTableEntry& entry : *this) {
for (const Symbol& sym : entry) {
if (sym.getName() == id) {
return sym.getType();
}
}
}
throw Void(); // very very stupid.
}
bool SymbolTable::addParameters(const Formals ¶meters, int lineno) {
std::vector<std::string> names = parameters.getNames();
std::vector<ProtoType*> types = parameters.getTypes();
unsigned long len = names.size();
int count = 0;
string val;
for (unsigned long i = 0; i < len; ++i) {
if(this->findId(names[i])) {
output::errorDef(lineno, names[i]);
return true;
}
types[i]->setConst(true);
types[i]->setIsLiteral(true);
this->insertAtTop(names[i], *(types[i]), true);
val = "%";
val += std::to_string(count);
count++;
this->setValueById(names[i], val);
//cout << "for Daniel: " << this->getValueById(names[i]) << endl;
}
/*
for(int i = 0; i < len; i++) {
cout << "value of " << names[i] << " is " << this->getValueById(names[i]) << endl;
}
*/
return false;
}
int SymbolTable::getAbsoluteOffset(const string &id) {
int offset = 0;
bool isFound = false;
for (const SymbolTableEntry& entry : *this) {
if(!isFound) {
for (const Symbol &sym : entry) {
if (sym.getName() == id) {
isFound = true;
offset += sym.getOffset();
}
}
} else {
offset += entry.getLastOffset() == -1 ? 0 : entry.getLastOffset();
}
}
return offset;
}
void SymbolTable::setValueById(const string id, std::string value) {
for (SymbolTableEntry& entry: *this) {
for (Symbol& sym: entry) {
if(sym.getName() == id) {
//cout << "found id " << id << " and gave it value " << value << endl;
sym.setValue(value);
//cout << "new value of sym: " << sym.getValue() << endl;
return;
}
}
}
}
string SymbolTable::getValueById(const string id) {
for (SymbolTableEntry& entry: *this) {
for (Symbol& sym: entry) {
if(sym.getName() == id) {
//cout << "found id " << id << endl;
string val = sym.getValue();
//cout << "returning value of sym: " << val << endl;
return val;
}
}
}
return ""; //
}
int SymbolTableEntry::getLastOffset() const {
return this->lastOffset;
}
int SymbolTableEntry::getLastParameter() const {
return this->lastParameter;
}
void SymbolTableEntry::lastParameterInc() {
this->lastParameter--;
}
void SymbolTableEntry::lastOffsetInc() {
this->lastOffset++;
}