-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymTable.cpp
More file actions
136 lines (110 loc) · 3.23 KB
/
SymTable.cpp
File metadata and controls
136 lines (110 loc) · 3.23 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
#include "SymTable.h"
#include "AST.h"
using namespace std;
IdInfo* SymTable::getId(string* s) {
if (ids.count(*s))
return &ids[*s];
return NULL;
}
void SymTable::addSym(string* type, string*name, string* category, vector<string> params) {
IdInfo var(type, name, category);
var.params = params;
ids[*name] = var;
}
bool SymTable::existsId(string* var) {
if(ids.count(*var)) return true;
if (parent) return parent->existsId(var);
return false;
}
bool SymTable::existsIdLocal(string* var) {
return ids.count(*var) > 0;
}
void SymTable::setClassScopeForId(string* idName, SymTable* scope) {
if(ids.count(*idName)) {
ids[*idName].classScope = scope;
}
}
void printSpaces(int n) {
if (n < 1) n = 0;
for (int i = 0; i < n; i++) {
cout << " ";
}
}
void SymTable::printVars() {
cout << "Scope: " << name;
if (parent) {
cout << " (Parent: " << parent->name << ")";
} else {
cout << " (Root)";
}
cout << endl;
for (const pair<string, IdInfo>& v : ids) {
const IdInfo& info = v.second;
cout << " Symbol: " << info.name;
printSpaces(15 - info.name.length());
cout << "| Category: " << info.category;
printSpaces(5 - info.category.length());
cout << "| Type: " << info.type;
printSpaces(10 - info.type.length());
if (info.category == "var" || info.category == "param") {
cout << "| Value: " << info.val.toString();
}
if (!info.params.empty()) {
cout << " | Param Types: [";
for (size_t i = 0; i < info.params.size(); ++i) {
cout << info.params[i];
if (i < info.params.size() - 1) {
cout << ", ";
}
}
cout << "]";
}
cout << endl;
}
cout << endl;
}
string SymTable::getType(string* name) {
if (ids.count(*name)) return ids[*name].type;
if (parent) return parent->getType(name);
return "";
}
SymTable* SymTable::getClassScope(string* className) {
// Căutăm clasa în scope-ul curent
if(ids.count(*className) && ids[*className].category == "class") {
return ids[*className].classScope;
}
// Dacă nu e în scope-ul curent, căutăm în parent
if(parent) return parent->getClassScope(className);
return NULL;
}
bool SymTable::hasField(string* className, string* fieldName) {
SymTable* classTable = getClassScope(className);
if(classTable) {
return classTable->existsId(fieldName);
}
return false;
}
string SymTable::getFieldType(string* className, string* fieldName) {
SymTable* classTable = getClassScope(className);
if(classTable) {
return classTable->getType(fieldName);
}
return "";
}
SymTable::~SymTable() {//dadea eroare ca e declarat si nefolosit
// nimic de eliberat explicit
}
Value SymTable::getValue(string name) {
if (ids.count(name)) {
return ids[name].val;
}
if (parent) return parent->getValue(name);
return Value();
}
void SymTable::setValue(string name, Value v) {
if (ids.count(name)) {
ids[name].val = v;
} else if (parent) {
parent->setValue(name, v);
}
}