-
Notifications
You must be signed in to change notification settings - Fork 0
/
2005009_SymbolTable.h
86 lines (73 loc) · 2.02 KB
/
2005009_SymbolTable.h
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
#include "2005009_ScopeTable.h"
using namespace std;
class SymbolTable
{
private:
ScopeTable *currentScope;
int size;
public:
SymbolTable(int size)
{
this->size = size;
currentScope = new ScopeTable(size, 1);
}
~SymbolTable()
{
while(currentScope != NULL){
ScopeTable *temp = currentScope;
cout << "\tScopeTable# " << temp->getId() << " deleted" << endl;
currentScope = currentScope->getParentScope();
delete temp;
}
}
int getSize() { return size; }
ScopeTable* getCurrentScope() { return currentScope; }
void _enter()
{
int newID = currentScope->getCounter() + 1;
int id = currentScope->getId() + (newID);
ScopeTable *newScope = new ScopeTable(size, id, currentScope);
currentScope->setCounter(newID);
currentScope = newScope;
}
bool _exit()
{
if(currentScope->getParentScope() == NULL)
return false;
ScopeTable *temp = currentScope;
currentScope = currentScope->getParentScope();
delete temp;
return true;
}
bool _insert(string name, string type, SymbolInfo *pointTo = NULL)
{
return currentScope->_insert(name, type, pointTo);
}
bool _remove(string name)
{
return currentScope->_delete(name);
}
SymbolInfo* _lookUp(string name)
{
ScopeTable *temp = currentScope;
while(temp != NULL){
SymbolInfo *info = temp->_lookUp(name);
if(info != NULL)
return info;
temp = temp->getParentScope();
}
return NULL;
}
void _printCurrentScope(ofstream &out)
{
currentScope->_print(out);
}
void _printAllScope(ofstream &out)
{
ScopeTable *temp = currentScope;
while(temp != NULL){
temp->_print(out);
temp = temp->getParentScope();
}
}
};