-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabsym.cpp
146 lines (111 loc) · 3.19 KB
/
tabsym.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
139
140
141
142
143
144
145
//
// Created by Adam Zvada on 17.06.17.
//
#include "tabsym.h"
SymboleTable::~SymboleTable() {
Symbole * curSymbole = _root;
for (auto val : _globalTable) {
delete val.second;
}
for (auto val : _localTable) {
delete val.second;
}
}
vector<Symbole *> SymboleTable::GetAllGlobalVar() const {
vector<Symbole *> allGlobal;
for (auto var : _globalTable) {
allGlobal.push_back(var.second);
}
return allGlobal;
}
vector<Symbole *> SymboleTable::GetAllLocalVar() const {
vector<Symbole *> allLocal;
for (auto var : _localTable) {
allLocal.push_back(var.second);
}
return allLocal;
}
void SymboleTable::DeclareConst(string ident, int val) {
auto it = _globalTable.find(ident);
if (it != _globalTable.end()) {
cout << "Constant \"" << ident << "\" was already decleared" << endl;
exit(1);
}
Symbole *newSymbole = new Symbole(ident, SymboleType::CONST, val);
if (!_isLocalScope) {
_globalTable.insert(make_pair(ident, newSymbole));
} else {
_localTable.insert(make_pair(ident, newSymbole));
}
}
void SymboleTable::DeclareVar(string ident) {
auto it = _globalTable.find(ident);
if (it != _globalTable.end()) {
cout << "Variable \"" << ident << "\" was already decleared" << endl;
exit(1);
}
Symbole * newSymbole = new Symbole(ident, SymboleType::VAR, 0);
if (!_isLocalScope) {
_globalTable.insert(make_pair(ident, newSymbole));
} else {
_localTable.insert(make_pair(ident, newSymbole));
}
}
void SymboleTable::DeclareVarArr(string ident, int start, int end) {
auto it = _globalTable.find(ident);
if (it != _globalTable.end()) {
cout << "Variable \"" << ident << "\" was already decleared" << endl;
exit(1);
}
Symbole * newSymbole = new Symbole(ident, SymboleType::ARRAY, start, end);
if (!_isLocalScope) {
_globalTable.insert(make_pair(ident, newSymbole));
} else {
_localTable.insert(make_pair(ident, newSymbole));
}
}
void SymboleTable::DeclareFunc(const string & name) {
//TODO
}
Symbole * SymboleTable::FindIdent(string & ident) const {
auto it = _globalTable.find(ident);
if (it == _globalTable.end()) {
return nullptr;
}
return it->second;
}
int SymboleTable::GetValue(string & ident) const {
Symbole * symbole = FindIdent(ident);
if (!symbole) {
Error(ident, "was not decleared");
return 0;
}
return symbole->value;
}
SymboleType SymboleTable::GetConstOrVar(string & ident, int * value) {
Symbole * symbole = FindIdent(ident);
switch (symbole->type) {
case CONST:
*value = symbole->value;
return CONST;
case VAR:
*value = symbole->value;
return VAR;
case ARRAY:
value = 0;
return ARRAY;
default:
Error(ident, "not decleared!");
exit(1);
}
}
void SymboleTable::EnterLocalScope() {
_isLocalScope = true;
}
void SymboleTable::LeaveLocalScope() {
_isLocalScope = false;
for (auto val : _localTable) {
delete val.second;
}
_localTable.clear();
}