-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymTable.h
More file actions
254 lines (218 loc) · 6.23 KB
/
Copy pathSymTable.h
File metadata and controls
254 lines (218 loc) · 6.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
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
#ifndef SYMTABLE_H
#define SYMTABLE_H
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
class ASTNode;
struct SymbolInfo
{
string name;
string type;
string value;
string scopeCategory;
int size;
vector<string> paramTypes;
vector<string> paramNames; // Store parameter names for function calls
vector<ASTNode*>* funcBody = nullptr;
SymbolInfo() : name(""), type(""), value(""), scopeCategory(""), size(0) {}
SymbolInfo(string n, string t, string v = "", string cat = "variable")
: name(n), type(t), value(v), scopeCategory(cat), size(0) {}
};
class SymbolTable
{
public:
string scopeName;
SymbolTable* parent;
map<string, SymbolInfo> symbols;
vector<SymbolTable*> children;
SymbolTable(string name, SymbolTable* p = nullptr)
{
scopeName = name;
parent = p;
}
bool addSymbol(string name, string type, string category = "variable")
{
if (symbols.find(name) != symbols.end())
{
return false;
}
symbols.insert({name, SymbolInfo(name, type, "", category)});
return true;
}
bool addFunctionSymbol(string name, string type, vector<string> params)
{
if(symbols.find(name) != symbols.end())
{
return false;
}
SymbolInfo info(name,type, "", "function");
info.paramTypes = params;
symbols.insert({name, info});
return true;
}
SymbolInfo* findSymbol(string name)
{
if (symbols.find(name)!= symbols.end())
{
return &symbols[name];
}
if (parent != nullptr)
{
return parent->findSymbol(name);
}
return nullptr;
}
SymbolInfo* findSymbolLocal (string name)
{
if (symbols.find(name)!= symbols.end())
{
return &symbols[name];
}
return nullptr;
}
void printTable(ofstream& out, int indentLevel = 0)
{
string indent(indentLevel * 4, ' ');
out << indent << "=== SCOPE: " << scopeName << " ===" << endl;
if (parent)
{
out << indent << "Parent: " << parent->scopeName << endl;
}
out << indent << "Symbols:" << endl;
for (auto const& [key, val] : symbols)
{
out << indent << " [Name: " << val.name
<< ", Type: " << val.type
<< ", Cat: " <<val.scopeCategory
<< ", Val: " << val.value;
if(val.scopeCategory == "function" && !val.paramTypes.empty())
{
out << ", Params: (";
for(size_t i = 0 ; i < val.paramTypes.size(); i++)
{
out << val.paramTypes[i];
if (!val.paramNames.empty() && i < val.paramNames.size()) {
out << " " << val.paramNames[i];
}
if (i < val.paramTypes.size()-1) out << ", ";
}
out << ")";
}
out << "]" << endl;
}
out << endl;
for (auto child: children)
{
child->printTable(out, indentLevel + 1);
}
}
~SymbolTable()
{
for (auto child : children)
{
delete child;
}
}
};
class SymbolTableManager
{
public:
SymbolTable* globalScope;
SymbolTable* currentScope;
// Store all function body pointers for cleanup
vector<vector<ASTNode*>*> allFuncBodies;
SymbolTableManager() {
globalScope = new SymbolTable("Global");
currentScope = globalScope;
}
void enterScope(string name)
{
SymbolTable* newScope = new SymbolTable(name, currentScope);
currentScope->children.push_back(newScope);
currentScope = newScope;
}
SymbolInfo* getSymbol(string name)
{
return currentScope->findSymbol(name);
}
void exitScope() {
if (currentScope->parent != nullptr)
{
currentScope = currentScope->parent;
}
}
bool declareVariable(string name, string type, string category = "variable")
{
return currentScope->addSymbol(name, type, category);
}
bool declareFunction(string name, string type, vector<string> params)
{
return currentScope->addFunctionSymbol(name,type,params);
}
void updateFunctionParams(string name, vector<string> params, vector<string> names)
{
SymbolTable* searchScope = currentScope->parent;
if(searchScope)
{
auto it = searchScope->symbols.find(name);
if(it != searchScope->symbols.end())
{
it->second.paramTypes = params;
it->second.paramNames = names;
}
}
}
// Store function body
void storeFunctionBody(string name, vector<ASTNode*>* body)
{
SymbolTable* searchScope = currentScope->parent;
if(searchScope)
{
auto it = searchScope->symbols.find(name);
if(it != searchScope->symbols.end())
{
it->second.funcBody = body;
// Track for later cleanup
allFuncBodies.push_back(body);
}
}
}
bool exists(string name)
{
return currentScope->findSymbol(name) != nullptr;
}
SymbolTable* findClassScope(string className)
{
for(auto child : globalScope->children)
{
if(child->scopeName == className)
{
return child;
}
}
return nullptr;
}
void printAllTables(string filename)
{
ofstream out(filename);
if (out.is_open())
{
globalScope->printTable(out);
out.close();
}
}
// Get all function bodies for cleanup (called from main where ASTNode is defined)
vector<vector<ASTNode*>*>& getFuncBodies() {
return allFuncBodies;
}
~SymbolTableManager() {
// Note: Function body AST nodes should be cleaned up in main()
// where ASTNode is fully defined. We just delete the scope tree here.
delete globalScope;
}
};
#endif