-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.hpp
417 lines (365 loc) · 10.8 KB
/
symtable.hpp
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#define HASH_MUL 65599
using namespace std;
typedef struct SymbolTableEntry SymbolTableEntry;
class SymTable;
unsigned int scope;
bool return_flag;
stack<int> last_func;
stack<unsigned int> functionLocalsStack;
bool error = 0;
enum scopeSpace_t { program_var, function_local, formal_arg };
unsigned int anonymous_count;
unsigned int programVarOffset = 0;
unsigned int functionLocalOffset = 0;
unsigned int formalArgOffset = 0;
unsigned int scopeSpaceCounter = 1;
scopeSpace_t currScopeSpace() {
if (scopeSpaceCounter == 1)
return program_var;
else if (scopeSpaceCounter % 2 == 0)
return formal_arg;
else
return function_local;
}
unsigned currScopeOffset() {
switch (currScopeSpace()) {
case program_var:
return programVarOffset;
case function_local:
return functionLocalOffset;
case formal_arg:
return formalArgOffset;
default:
assert(0);
}
}
void inCurrScopeOffset() {
switch (currScopeSpace()) {
case program_var: {
++programVarOffset;
break;
}
case function_local: {
++functionLocalOffset;
break;
}
case formal_arg: {
++formalArgOffset;
break;
}
default:
assert(0);
}
}
void enterScopeSpace() { ++scopeSpaceCounter; }
void exitScopeSpace() {
assert(scopeSpaceCounter > 1);
--scopeSpaceCounter;
}
void resetFormalArgsOffset(){
formalArgOffset = 0;
}
void resetFunctionLocalOffset(){
functionLocalOffset = 0;
}
void restoreCurrScopeOffset(unsigned int n){
switch (currScopeSpace())
{
case program_var:
programVarOffset = n;
break;
case function_local:
functionLocalOffset = n;
break;
case formal_arg:
formalArgOffset = n;
break;
default:
assert(0);
}
}
/* συναρτήσεις βιβλιοθήκης LIBFUNC
συναρτήσεις προγράμματος USERFUNC
global οι μεταβλητές GLOBAL
τα τυπικά ορίσματα συναρτήσεων */
enum SymbolType { GLOBAL, LOCAL, FORMAL, USERFUNC, LIBFUNC };
typedef struct Variable {
const char *name;
unsigned int scope;
unsigned int line;
} Variable;
typedef struct Function {
const char *name;
vector<SymbolTableEntry *> args;
unsigned int iaddress;
unsigned int totalLocals;
unsigned int scope;
unsigned int line;
} Function;
typedef struct SymbolTableEntry {
bool isActive;
union {
Variable *varVal;
Function *funcVal;
} value;
enum SymbolType type;
unsigned int offset;
scopeSpace_t space;
/*For phase 4 -> No clue tho */
unsigned taddress;
SymbolTableEntry *next;
SymbolTableEntry *scope_next;
} SymbolTableEntry;
class SymTable {
private:
SymbolTableEntry **symbol_table;
SymbolTableEntry *dummy;
unsigned int size;
vector<SymbolTableEntry *> scopes; // scopes.at(i) push_back(entry)
public:
static const char *get_name(SymbolTableEntry *entry) {
switch (entry->type) {
case USERFUNC:
case LIBFUNC:
return entry->value.funcVal->name;
case GLOBAL:
case LOCAL:
case FORMAL:
return entry->value.varVal->name;
}
return "NULL";
}
static unsigned int get_scope(SymbolTableEntry *entry) {
switch (entry->type) {
case USERFUNC:
case LIBFUNC:
return entry->value.funcVal->scope;
case GLOBAL:
case LOCAL:
case FORMAL:
return entry->value.varVal->scope;
}
}
static unsigned int get_lineno(SymbolTableEntry *entry) {
switch (entry->type) {
case USERFUNC:
case LIBFUNC:
return entry->value.funcVal->line;
case GLOBAL:
case LOCAL:
case FORMAL:
return entry->value.varVal->line;
}
}
static char *enumtostring(SymbolType s) {
switch (s) {
case USERFUNC:
return "user function";
case LIBFUNC:
return "library function";
case GLOBAL:
return "global var";
case LOCAL:
return "local var";
case FORMAL:
return "formal var";
}
}
static bool is_var(SymbolType symtyp) {
switch (symtyp) {
case USERFUNC:
case LIBFUNC:
default:
return false;
case GLOBAL:
case LOCAL:
case FORMAL:
return true;
}
}
void initialize() {
// init scopes
scope = 0;
anonymous_count = 0;
last_func.push(-1);
return_flag = false;
// insert all libfuncs
insert("sin", 0, LIBFUNC);
insert("cos", 0, LIBFUNC);
insert("sqrt", 0, LIBFUNC);
insert("print", 0, LIBFUNC);
insert("input", 0, LIBFUNC);
insert("typeof", 0, LIBFUNC);
insert("argument", 0, LIBFUNC);
insert("strtonum", 0, LIBFUNC);
insert("objectcopy", 0, LIBFUNC);
insert("totalarguments", 0, LIBFUNC);
insert("objectmemberkeys", 0, LIBFUNC);
insert("objecttotalmembers", 0, LIBFUNC);
printf("\n\n");
}
SymTable(unsigned int s_size = 100) : size(s_size) {
symbol_table = new SymbolTableEntry *[size];
dummy = new SymbolTableEntry();
dummy->next = NULL;
dummy->scope_next = NULL;
dummy->isActive = false;
dummy->type = LOCAL;
dummy->value.varVal = new Variable();
dummy->value.varVal->name = strdup("$dummy");
for (int i = 0; i < size; i++) { // idk why this wrorks
symbol_table[i] = dummy;
}
initialize();
}
unsigned int SymTable_hash(const char *name) {
size_t ui;
unsigned int uiHash = 0U;
for (ui = 0U; name[ui] != '\0'; ui++) uiHash = uiHash * HASH_MUL + name[ui];
return uiHash % size;
}
void insert_arg(SymbolTableEntry *newnode) {
// there was an error in the declaration of the function so dont do anything
if (last_func.top() != (scope - 1)) return;
SymbolTableEntry *function = scopes.at(last_func.top());
if (!function->isActive || is_var(function->type)) {
printf("\n__can't insert arguement__\n");
} else
function->value.funcVal->args.push_back(newnode);
}
// enum SymbolType { GLOBAL, LOCAL, FORMAL, USERFUNC, LIBFUNC };
SymbolTableEntry *insert(const char *name, unsigned int lineno,
SymbolType symtp, int myscope = scope) {
SymbolTableEntry *newnode = new SymbolTableEntry();
if (name == NULL) {
printf("____Null parameter in insert_____\n");
return NULL;
}
newnode->next = NULL;
newnode->scope_next = NULL;
newnode->isActive = true;
printf("(inserting %s in scope: %d as %s)\n", name, scope,
enumtostring(symtp));
newnode->type = symtp;
switch (symtp) {
case LIBFUNC:
myscope = 0;
case USERFUNC:
newnode->value.funcVal = new Function();
newnode->value.funcVal->line = lineno;
newnode->value.funcVal->scope = myscope;
newnode->value.funcVal->name = strdup(name);
break;
case FORMAL:
insert_arg(newnode);
case GLOBAL:
case LOCAL:
newnode->value.varVal = new Variable();
newnode->value.varVal->line = lineno;
newnode->value.varVal->scope = myscope;
newnode->value.varVal->name = strdup(name);
break;
}
newnode->next = symbol_table[SymTable_hash(name)];
symbol_table[SymTable_hash(name)] = newnode;
if (myscope > scopes.size()) {
// We got deeper in scopes without any vars in the scopes between
for (int i = scopes.size(); i <= myscope; i++) {
scopes.push_back(dummy);
}
} else if (myscope == scopes.size()) { // add new level of scopes
newnode->scope_next = NULL;
scopes.resize(myscope + 1);
} else { // connect to current scope list
newnode->scope_next = scopes.at(myscope);
}
scopes[myscope] = newnode;
return newnode;
}
/* local x; function f() calls this
* return -1: error libfunc/ redefinition
* return 0: need to be defined
* return 1: already declared refers to previous declaration
*/
SymbolTableEntry *lookUp_curscope(const char *name) {
SymbolTableEntry *curr = symbol_table[SymTable_hash(name)];
for (; curr; curr = curr->next) {
if (!curr->isActive || strcmp(name, get_name(curr))) continue;
// print libfunc error
if (curr->type == LIBFUNC) return curr;
if (scope == get_scope(curr)) return curr;
}
return NULL;
}
/* x() x= 5 x=7
* return -1: error libfunc/ redefinition
* return 0: need to be defined
* return 1: already declared refers to previous declaration
*/
SymbolTableEntry *lookUp_allscope(const char *name) {
SymbolTableEntry *curr = symbol_table[SymTable_hash(name)];
for (; curr; curr = curr->next) {
if (!curr->isActive || strcmp(name, get_name(curr))) continue;
// print libfunc error
if (curr->type == LIBFUNC || scope <= get_scope(curr) ) return curr;
}
return NULL;
}
int hide(int scope) {
if (scope >= scopes.size()) return -1;
SymbolTableEntry *curr = scopes.at(scope);
const char *curr_name = get_name(curr);
while (curr) {
curr->isActive = false;
curr = curr->scope_next;
}
return 0;
}
void print() {
SymbolTableEntry *curr;
unsigned int max_name = 0;
unsigned int max_lineno = 0;
for (int i = 0; i < scopes.size(); i++) {
curr = scopes.at(i);
while (curr != NULL) {
if (strlen(get_name(curr)) > max_name)
max_name = strlen(get_name(curr));
curr = curr->scope_next;
}
}
for (int i = 0; i < scopes.size(); i++) {
curr = scopes.at(i);
printf("\n%s Scope %d %s\n", string((max_name + 50) / 2, '-').c_str(), i,
string((max_name + 50) / 2, '-').c_str());
while (curr != NULL) {
if (!strcmp(curr->value.varVal->name, "$dummy")) {
// dummy node -> dont print
curr = curr->scope_next;
continue;
}
cout << setw(max_name - strlen(get_name(curr)) + 2) << "\""
<< get_name(curr) << "\""
<< setw(16 - strlen(enumtostring(curr->type)) + 3) << "["
<< enumtostring(curr->type) << "] ";
printf("(lineno:%3d) (scope:%2d) (active:%d)", get_lineno(curr),
get_scope(curr), curr->isActive);
if (curr->type == USERFUNC) {
printf("[args:");
for (unsigned i = 0; i < curr->value.funcVal->args.size(); i++)
printf("'%s'", get_name(curr->value.funcVal->args.at(i)));
printf("]");
}
printf("\n");
curr = curr->scope_next;
}
}
}
};