-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolTable.cpp
111 lines (96 loc) · 3.14 KB
/
symbolTable.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
#include <iostream>
#include "symbolTable.h"
SymbolTable::SymbolTable()
: currentScopeId (0) {
intDescriptor = new STTypeDescriptor(VT_INT);
charDescriptor = new STTypeDescriptor(VT_CHAR);
boolDescriptor = new STTypeDescriptor(VT_BOOL);
universe = new STEntry(ST_HEAD, 0);
currentScope = universe;
};
void SymbolTable::addVariable(VarType varType, std::string varName) {
addEntry(ST_VAR, varType, varName);
};
void SymbolTable::addFunction(VarType returnType, std::string functionName) {
addEntry(ST_FUNCTION, returnType, functionName);
};
void SymbolTable::addParam(VarType paramType, std::string paramName) {
addEntry(ST_PARAM, paramType, paramName);
}
void SymbolTable::addEntry(STEntryType entryType, VarType varType, std::string varName) {
STEntry * scopeIterator = currentScope;
while (scopeIterator->next != nullptr) {
if (scopeIterator->next->name == varName) {
std::cout << "Object name already exists in scope: " << varName << std::endl;
return;
} else {
scopeIterator = scopeIterator->next;
}
}
STTypeDescriptor * typeDescriptor = new STTypeDescriptor(varType);
STEntry * newEntry = new STEntry();
newEntry->name = varName;
newEntry->entryType = entryType;
newEntry->scopeId = currentScopeId;
newEntry->typeDescriptor = typeDescriptor;
scopeIterator->next = newEntry;
};
void SymbolTable::openScope(std::string scopeName) {
currentScopeId++;
STEntry * oldScope = currentScope;
currentScope = new STEntry(ST_HEAD, currentScopeId);
currentScope->name = scopeName;
// Fix references
currentScope->parentScope = oldScope;
oldScope->childScopes.push_back(currentScope);
}
void SymbolTable::closeScope() {
currentScope = currentScope->parentScope;
}
STEntry * SymbolTable::lookup(std::string name) {
STEntry * head = currentScope;
STEntry * currentEntry;
while (head != nullptr) {
currentEntry = head->next;
while (currentEntry != nullptr) {
if (currentEntry->name == name) {
return currentEntry;
}
currentEntry = currentEntry->next;
}
if(head == universe) {
break;
}
head = head->parentScope;
}
return nullptr;
}
STEntry * SymbolTable::lookupScope(std::string functionName) {
STEntry * head = universe;
for (auto & child : universe->childScopes) {
if (child->name == functionName) {
return child;
}
}
return nullptr;
}
void SymbolTable::print() {
STEntry * head = currentScope;
STEntry * currentEntry;
std::cout << "innerScope" << std::endl;
while (head != nullptr) {
std::cout << "| " << std::endl;
std::cout << "[ Head ]";
currentEntry = head->next;
while (currentEntry != nullptr) {
std::cout << " -> ";
std::cout << " [ " << currentEntry->name << " ] ";
currentEntry = currentEntry->next;
}
std::cout << " -> guard " << std::endl;
head = head->parentScope;
}
}
STEntry * SymbolTable::getRoot() {
return universe;
}