-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.cpp
More file actions
608 lines (548 loc) · 20.1 KB
/
SymbolTable.cpp
File metadata and controls
608 lines (548 loc) · 20.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "SymbolTable.h"
#include "AST.h"
using namespace TypeNms;
std::string fullMatrixForm(const std::vector<size_t> sizes) {
std::string output = "";
for (size_t size : sizes) {
output += "[" + std::to_string(size) + "]";
}
return output;
}
std::string Scope::scopeToString(const std::vector<std::string>& scope, ssize_t size) {
const char* const delim = "/";
std::string result;
if (size < 0) {
for (const auto& scopeStr : scope) {
result += scopeStr + delim;
}
}
else {
for (int i = 0; i < size; ++i) {
result += scope[i] + delim;
}
}
return result;
}
std::string Scope::scopeWithNameToString(const std::string& scope, const std::string& name) {
return scope + name;
}
bool Scope::encompassingScope(const std::string& active, const std::string& encompassing) {
return active.find(encompassing) != std::string::npos;
}
SymbolData::SymbolData(const std::string& scope, const std::string& name, const TypeNms::Type t, const Flag f, const std::vector<size_t>& sizes, const SymbolData* classDef) {
_scope = scope;
_name = name;
_type = t;
_sizes = sizes;
_isArray = (sizes.empty() == false);
_isInit = false;
switch (f) {
case Constant:
_isConst = true;
break;
case Function:
_isFunc = true;
break;
case Class:
_isClassDef = true;
break;
case InitList:
_isInitList = true;
_isInit = true;
break;
}
if (_isArray == false and (t == CUSTOM or _isFunc or _isClassDef)) {
_value = std::vector<SymbolData>();
}
if (_isArray) {
if (sizes.front() == 0) {
throw std::invalid_argument("Cannot construct array " + name + " of size 0");
}
if (t != CUSTOM || sizes.size() > 1) {
std::vector<SymbolData> vec;
vec.reserve(sizes.front());
for (size_t i = 0; i < sizes.front(); ++i) {
try {
vec.emplace_back(scope, name + "[" + std::to_string(i) + "]", t, f, std::vector<size_t>(sizes.begin() + 1, sizes.end()), classDef);
}
catch (std::invalid_argument& constructionError) {
throw std::invalid_argument("Cannot construct array " + name + " of size 0");
}
}
_value = vec;
}
else {
std::vector<SymbolData> vec;
vec.reserve(sizes.front());
for (size_t i = 0; i < sizes.front(); ++i) {
vec.emplace_back(classDef->instantiateClass(scope, name + "[" + std::to_string(i) + "]"));
}
_value = vec;
}
}
if (t == CUSTOM && _isInitList == false) {
if (classDef == nullptr) {
if (_isClassDef) {
_className = name;
}
else {
throw std::logic_error("Custom type constructed without any class-instance template");
}
}
else {
_className = classDef->name();
}
}
}
SymbolData::SymbolData(const std::string& scope, const std::string& name, const TypeNms::Type t, const Flag f, const Value& val) :
SymbolData(scope, name, t, f) {
_value = val;
}
SymbolData& SymbolData::setConst(bool value) {
if (_isConst and value == false) {
throw std::logic_error("Cannot make const symbol " + _name + " non-const");
}
if (value && _isInit == false) {
throw std::runtime_error("Cannot initialize const variable " + name() + " because it is not fully initialized");
}
_isConst = value;
if (_isConst && std::holds_alternative<std::vector<SymbolData>>(_value)) {
for (SymbolData& subSymbol : std::get<std::vector<SymbolData>>(_value)) {
try {
subSymbol.setConst(value);
}
catch(std::runtime_error& e) {
throw std::runtime_error("Cannot initialize const variable " + name() + " because it is not fully initialized");
}
}
}
return *this;
}
SymbolData& SymbolData::setType(TypeNms::Type type) {
_type = type;
return *this;
}
SymbolData& SymbolData::assign(const SymbolData& symbol) {
if (!sameType(*this, symbol)) {
throw std::runtime_error("Cannot assign symbol" + symbol.name() + " of type " + TypeNms::typeToStr(symbol.type()) + ( !symbol.className().empty() ? " " + symbol.className() : "") + " to symbol " + _name + " of type " + TypeNms::typeToStr(_type) );
}
if (symbol.isInit() == false) {
throw std::runtime_error("Cannot assign uninitialized symbol " + symbol.name() + " to symbol " + _name);
}
if (std::holds_alternative<std::vector<SymbolData>>(value()) == false) {
this->assign(symbol.value());
return *this;
}
std::vector<SymbolData>& thisVec = std::get<std::vector<SymbolData>>(_value);
const std::vector<SymbolData>& symbolVec = std::get<std::vector<SymbolData>>(symbol._value);
std::string from = TypeNms::typeToStr(symbol.type());
std::string to = TypeNms::typeToStr(type());
if (type() == CUSTOM) {
to += ' ' + className();
}
if (symbol.type() == CUSTOM) {
from += ' ' + symbol.className();
}
if (isArray()) {
to += fullMatrixForm(_sizes);
}
if (symbol.isArray()) {
from += fullMatrixForm(symbol._sizes);
}
if (sameType(*this, symbol) == false || thisVec.size() != symbolVec.size()) {
throw std::runtime_error("Cannot assign type: " + from + " to type: " + to);
}
_isInit = true;
for (size_t i = 0; i < thisVec.size(); ++i) {
thisVec[i].assign(symbolVec[i]);
}
return *this;
}
SymbolData& SymbolData::assign(const Value& val) {
using namespace TypeNms;
throwWhenUnassignable(val);
_isInit = true;
if (_type != CUSTOM and !_isArray) {
_value = val;
return *this;
}
if (!std::holds_alternative<std::vector<SymbolData>>(_value)) {
throw std::logic_error("Cannot iterate through array or non-base-type symbol " + _name + " in order to assign to it. Something has gone very wrong");
}
auto& vector = std::get<std::vector<SymbolData>>(_value);
auto& valueSymbols = std::get<std::vector<SymbolData>>(val);
for (size_t i = 0; i < std::min(vector.size(), valueSymbols.size()); ++i) {
vector[i].assign(valueSymbols[i].value());
}
return *this;
}
void SymbolData::throwWhenUnassignable(const Value& val) {
if (isConst()) {
throw std::runtime_error("Cannot assign to constant symbol " + _name);
}
if (isFunc()) {
throw std::runtime_error("Cannot assign to function symbol " + _name);
}
std::string valueType = "";
if (std::holds_alternative<int>(val)) {
if (type() == INT && isArray() == false) {
return;
}
valueType = TypeNms::typeToStr(INT);
}
else if (std::holds_alternative<float>(val)) {
if (type() == FLOAT && isArray() == false) {
return;
}
valueType = TypeNms::typeToStr(FLOAT);
}
else if (std::holds_alternative<char>(val)) {
if (type() == CHAR && isArray() == false) {
return;
}
valueType = TypeNms::typeToStr(CHAR);
}
else if (std::holds_alternative<bool>(val)) {
if (type() == BOOL && isArray() == false) {
return;
}
valueType = TypeNms::typeToStr(BOOL);
}
else if (std::holds_alternative<std::string>(val)) {
if (type() == STRING && isArray() == false) {
return;
}
valueType = TypeNms::typeToStr(STRING);
}
if (valueType.empty() == false) {
if (isArray() == true) {
throw std::runtime_error("Cannot assign " + valueType + " to an array identifier");
}
if (type() != CUSTOM) {
throw std::runtime_error("Cannot assign " + valueType + " to an identifier of type " + TypeNms::typeToStr(type()));
}
throw std::runtime_error("Cannot assign " + valueType + " to an identifier of type Class " + className());
}
// initializer list
const std::vector<SymbolData>& thisData = std::get<std::vector<SymbolData>>(value());
const std::vector<SymbolData>& otherData = std::get<std::vector<SymbolData>>(val);
if (thisData.size() < otherData.size()) {
throw std::runtime_error("Initializer list has too many parameters");
}
else if (thisData.size() > otherData.size()) {
Utils::printWarning("Initializer list has too few parameters for symbol" + ((_name.empty()) ? "" : " " + _name) + ". Part of symbol will remain uninitialized");
}
if (isArray()) {
for (size_t i = 0; i < otherData.size(); ++i) {
if (!sameType(thisData[0], otherData[i])) {
throw std::runtime_error("Element " + std::to_string(i) + " of initializer list is of a different type");
}
}
}
else {
for (size_t i = 0; i < otherData.size(); ++i) {
if (!sameType(thisData[i], otherData[i])) {
if (isArray()) {
throw std::runtime_error("Element " + std::to_string(i) + " of initializer list cannot be assigned to member " + thisData[i].name());
}
}
}
}
}
SymbolData& SymbolData::addSymbolToBeginning(const SymbolData& symbol) {
if (_isArray or !std::holds_alternative<std::vector<SymbolData>>(_value)) {
throw std::runtime_error("Cannot add symbol to symbol " + _name + " that does not hold other symbols");
}
std::vector<SymbolData>& vec = std::get<std::vector<SymbolData>>(_value);
vec.insert(vec.begin(), symbol);
return *this;
}
SymbolData& SymbolData::addSymbol(const SymbolData& symbol) {
if (_isArray or !std::holds_alternative<std::vector<SymbolData>>(_value)) {
throw std::runtime_error("Cannot add symbol to symbol " + _name + " that does not hold other symbols");
}
std::get<std::vector<SymbolData>>(_value).push_back(symbol);
return *this;
}
std::string SymbolData::name() const {
return _name;
}
std::string SymbolData::scope() const {
return _scope;
}
TypeNms::Type SymbolData::type() const {
return _type;
}
const SymbolData::Value& SymbolData::value() const {
return _value;
}
std::string SymbolData::className() const {
return _className;
}
bool SymbolData::isFunc() const {
return _isFunc;
}
bool SymbolData::isArray() const {
return _isArray;
}
bool SymbolData::isInit() const {
return _isInit;
}
bool SymbolData::isConst() const {
return _isConst;
}
const std::vector<size_t>& SymbolData::sizes() const {
return _sizes;
}
void SymbolData::recursiveScopeApply() {
if (std::holds_alternative<std::vector<SymbolData>>(value())) {
for (SymbolData& subSymbol : std::get<std::vector<SymbolData>>(_value)) {
subSymbol._scope = _scope;
subSymbol.recursiveScopeApply();
}
}
}
SymbolData SymbolData::instantiateClass(const std::string& scope, const std::string& name) const {
if (!_isClassDef) {
throw std::invalid_argument("Cannot clone non-class-template symbol " + _name);
}
SymbolData symbol(*this);
symbol._scope = scope;
symbol.recursiveScopeApply();
symbol._name = name;
symbol._className = _className;
return symbol;
}
SymbolData* SymbolData::member(const std::string& id) {
if (_isArray || _isFunc) {
return nullptr;
}
if (_type != TypeNms::Type::CUSTOM) {
return nullptr;
}
if (std::holds_alternative<std::vector<SymbolData>>(value()) == false) {
return nullptr;
}
for (SymbolData& storedSymbol : std::get<std::vector<SymbolData>>(_value)) {
if (storedSymbol.name() == id) {
return &storedSymbol;
}
}
return nullptr;
}
SymbolData* SymbolData::member(const size_t index) {
if (!_isArray) {
throw std::invalid_argument("Cannot apply index operator to non-array symbol");
}
std::vector<SymbolData>* vec = std::get_if<std::vector<SymbolData>>(&_value);
if (index >= vec->size()) {
return nullptr;
}
return &(*vec)[index];
}
bool SymbolData::hasSameTypeAs(const SymbolData& sym) const {
return sameType(*this, sym);
}
bool sameType(const SymbolData& a, const SymbolData& b) {
if (b._isInitList == false || std::holds_alternative<std::vector<SymbolData>>(a.value()) == false) {
if (a.type() != CUSTOM and !a.isFunc()) {
return a.type() == b.type();
}
if (a.type() != b.type()) {
return false;
}
if (a.type() == CUSTOM and b.type() == CUSTOM
and !a.isFunc() and !b.isFunc()
and !a.isArray() and !b.isArray()
and a.className() != b.className()) {
return false;
}
}
const auto& aData = std::get<std::vector<SymbolData>>(a.value());
const auto& bData = std::get<std::vector<SymbolData>>(b.value());
if (b._isInitList == false) {
if (aData.size() != bData.size()) {
return false;
}
if (a.isArray() != b.isArray() or a.isFunc() != b.isFunc()) {
return false;
}
}
for (size_t i = 0; i < aData.size(); ++i) {
if (!sameType(aData[i], bData[i])) {
return false;
}
}
return true;
}
void printSubsymbol(std::ostream& out, const SymbolData& sd, size_t depth) {
if (depth > 0) {
out << '\n' << std::string(depth, '\t');
}
if (!sd.isFunc()) {
out << (sd.isConst() ? "Constant" : "Variable") << " with type: " << typeToStr(sd.type()) + (sd.type() != CUSTOM ? "" : " " + sd._className)
<< (sd.isArray() ? fullMatrixForm(sd.sizes()) : "") << ", name: \'" + sd.name() + "\'" << (depth == 0 ? ", in scope \'" + sd.scope() + '\'' : "");
if (sd.type() == CUSTOM or sd.isArray()) {
for (const auto& subSymbol : std::get<std::vector<SymbolData>>(sd.value())) {
printSubsymbol(out, subSymbol, depth + 1);
}
}
else {
const auto val = sd.value();
out << ", with value " << sd.valueStr();
}
}
else {
out << "Function with type: " << typeToStr(sd._type) + (sd.type() != CUSTOM ? "" : " " + sd._className) << ", name: \'" + sd.name() + "\'" << (depth == 0 ? ", in scope \'" + sd.scope() + '\'' : "");
for (const auto& subSymbol : std::get<std::vector<SymbolData>>(sd.value())) {
printSubsymbol(out, subSymbol, depth + 1);
}
}
}
std::ostream& operator << (std::ostream& out, const SymbolData& sd) {
printSubsymbol(out, sd, 0);
return out;
}
std::string SymbolData::valueStr() const {
using namespace TypeNms;
if (!_isInit) {
return "uninitialized";
}
switch(type()) {
case INT :
return std::to_string(std::get<int>(_value));
case FLOAT: {
std::string floatStr = std::to_string(std::get<float>(_value));
while (floatStr.back() == '0') floatStr.pop_back();
if (floatStr.back() == '.') floatStr.push_back('0');
return floatStr;
}
case BOOL:
return (std::get<bool>(_value) == 1) ? "true" : "false";
case CHAR:
return "\'" + Utils::encodeStringValue(std::string() + std::get<char>(_value)) + "\'";
case STRING:
return "\"" + Utils::encodeStringValue(std::get<std::string>(_value)) + "\"";
default:
return "non-base-type";
}
}
std::string SymbolData::trueValueStr() const {
if (!_isInit) {
return "uninitialized";
}
switch(type()) {
case INT :
return std::to_string(std::get<int>(_value));
case FLOAT:
return std::to_string(std::get<float>(_value));
case BOOL:
return (std::get<bool>(_value) == 1) ? "true" : "false";
case CHAR:
return std::string(1, std::get<char>(_value));
case STRING:
return std::get<std::string>(_value);
default:
return "non-base-type";
}
}
// --- SymbolTable ---
SymbolTable& SymbolTable::add(const std::string& name, TypeNms::Type type, SymbolData::Flag flag, const std::vector<size_t>& sizes) {
_table.emplace(Scope::scopeWithNameToString(currentScope(), name), std::make_unique<SymbolData>(currentScope(), name, type, flag, sizes));
_orderedTable.push_back(_table.at(Scope::scopeWithNameToString(currentScope(), name)).get());
return *this;
}
SymbolTable& SymbolTable::add(const std::string& name, TypeNms::Type type, SymbolData::Flag flag, const std::vector<size_t>& sizes, const SymbolData* className) {
_table.emplace(Scope::scopeWithNameToString(currentScope(), name), std::make_unique<SymbolData>(currentScope(), name, type, flag, sizes, className));
_orderedTable.push_back(_table.at(Scope::scopeWithNameToString(currentScope(), name)).get());
return *this;
}
SymbolTable& SymbolTable::add(const SymbolData& symbol) {
_table.emplace(Scope::scopeWithNameToString(currentScope(), symbol.name()), std::make_unique<SymbolData>(symbol));
_orderedTable.push_back(_table.at(Scope::scopeWithNameToString(currentScope(), symbol.name())).get());
//std::cout << "Adding variable: " << Scope::scopeWithNameToString(currentScope(), symbol.name()) + "\n";
return *this;
}
SymbolTable& SymbolTable::addClass(const std::string& name) {
_classesTable.emplace(name, std::make_unique<SymbolData>(currentScope(), name, Type::CUSTOM, SymbolData::Flag::Class));
_orderedClassesTable.push_back(_classesTable.at(name).get());
return *this;
}
SymbolTable& SymbolTable::remove(const SymbolData& data) {
//std::cout << "Removed element " << data.scope() + data.name() << '\n';
auto it = _table.find(data.scope() + data.name());
if (it == _table.end()) {
return *this;
}
for (auto ordIt = _orderedTable.begin(); ordIt != _orderedTable.end(); ++ordIt) {
if (*ordIt == it->second.get()) {
_orderedTable.erase(ordIt);
break;
}
}
_table.erase(data.scope() + data.name());
return *this;
}
SymbolTable& SymbolTable::setReturnType(TypeNms::Type t, const std::string& className) {
_returnType = t;
_returnClass = className;
return *this;
}
bool SymbolTable::contains(const std::string& name) {
return _table.contains(Scope::scopeWithNameToString(currentScope(), name));
}
bool SymbolTable::sameReturnType(TypeNms::Type t, const std::string& className) {
return _returnType == t && _returnClass == className;
}
void SymbolTable::print(std::ostream& out) {
out << "\n--- CLASS TABLE ---\n" << std::endl;
for (const auto smb : _orderedClassesTable) {
out << *smb << '\n';
}
out << "\n--- SYMBOL TABLE ---\n" << std::endl;
for (const auto smb : _orderedTable) {
out << *smb << '\n';
}
}
std::string SymbolTable::currentScope() {
return Scope::scopeToString(_currentScopeHierarchy);
}
void SymbolTable::enterAnonymousScope() {
static size_t i = 0;
if (i == SIZE_MAX) {
throw std::runtime_error("Reached anon scope limit for program");
}
std::string scope = std::to_string(i++);
_currentScopeHierarchy.push_back(scope);
}
void SymbolTable::enterScope(const std::string& str) {
_currentScopeHierarchy.push_back(str);
}
void SymbolTable::exitScope() {
_currentScopeHierarchy.pop_back();
}
SymbolData* SymbolTable::find(const std::string& scopedName) {
auto it = _table.find(scopedName);
if (it != _table.end()) {
return it->second.get();
}
return nullptr;
}
SymbolData* SymbolTable::findId(const std::string& id) {
std::string scope = "";
for (int i = _currentScopeHierarchy.size(); i >= 0; --i) {
std::string joinedScope = Scope::scopeToString(_currentScopeHierarchy, i);
auto it = _table.find(joinedScope + id);
if (it != _table.end()) {
return it->second.get();
}
}
return nullptr;
}
SymbolData* SymbolTable::findClass(const std::string& name) {
auto it = _classesTable.find(name);
if (it != _classesTable.end()) {
return it->second.get();
}
return nullptr;
}