This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.h
134 lines (113 loc) · 3.34 KB
/
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
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
// Created by rng70
#ifndef COMPILER_SYMBOLTABLE_H
#define COMPILER_SYMBOLTABLE_H
class SymbolTable {
int sizeOfTable;
ScopeTable *currentScope;
int counter;
public:
explicit SymbolTable(int size) {
this->counter = 1;
this->sizeOfTable = size;
currentScope = new ScopeTable(sizeOfTable, counter);
}
void setCurrentScope(ScopeTable *p) {
this->currentScope = p;
}
ScopeTable *getCurrentScope() {
return this->currentScope;
}
int getCurrentScopeID(){
return currentScope->getID();
}
// required methods for offline
void EnterScope(FILE *f) {
counter++;
ScopeTable *newScopeTable = new ScopeTable(this->sizeOfTable, counter, currentScope->getTableIdTracker());
// Increment the table tracker value
currentScope->setTableIdTracker();
ScopeTable *parentScope = currentScope;
currentScope = newScopeTable;
currentScope->setParentScope(parentScope);
// fprintf(f,"New ScopeTable with Id %d created\n",newScopeTable->getStringifyID().c_str());
}
void ExitScope(FILE *f) {
//currentScope->setTableIdTracker();
ScopeTable *temp = currentScope;
currentScope = temp->getParentScope();
// fprintf(f,"ScopeTable with ID %d removed\n",temp->getStringifyID().c_str());
delete temp;
}
bool Insert(std::string s, std::string type) {
if (currentScope->Insert(s, type))
return true;
return false;
}
bool InsertModified(SymbolInfo* s)
{
return currentScope->InsertModified(s);
}
bool Remove(std::string s) {
if (currentScope->Delete(s))
return true;
return false;
}
SymbolInfo *LookUp(std::string s) {
ScopeTable *temp = currentScope;
SymbolInfo *yolo;
while (temp != 0) {
yolo = temp->LookUP(s);
if (yolo != 0)
return yolo;
temp = temp->getParentScope();
}
return 0;
}
int IDlookUpWithParam(std::string Name){
SymbolInfo *result;
ScopeTable *temp;
temp = currentScope;
while(temp!=0){
result = temp->LookUP(Name);
if(result != 0){
return temp->getID();
}else{
temp = temp->getParentScope();
}
}
return -1;
}
SymbolInfo* currentScopeLookUp(std::string Name){
SymbolInfo* result;
result = currentScope->LookUP(Name);
if(result!=0)
return result;
return 0;
}
void printCurrentScopeTable(FILE *f) {
currentScope->printModified(f);
}
void printAllTable(FILE* f) {
ScopeTable *recursive = currentScope;
while (recursive != 0) {
recursive->printModified(f);
recursive = recursive->getParentScope();
}
}
~SymbolTable() {
ScopeTable *child, *parent;
child = currentScope;
while (child != 0) {
parent = child->getParentScope();
//child->printModified();
delete child;
//child->~ScopeTable();
child = parent;
}
//child->printModified();
delete child;
//parent->printModified();
delete parent;
}
};
#endif //COMPILER_SYMBOLTABLE_H