-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolTable.cpp
101 lines (82 loc) · 2.91 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
#include "SymbolTable.hpp"
#include <iostream>
#include "Scope.hpp"
SymbolTable::SymbolTable()
: root{std::make_unique<Scope>()}, current{root.get()} {}
void SymbolTable::enterScope(const std::string &name, Record *record) {
current = current->nextChild(name, record);
}
void SymbolTable::enterClassScope(Class *scopeClass) {
auto name = "Class: " + scopeClass->getID();
enterScope(name, scopeClass);
}
void SymbolTable::enterClassScope(const std::string &scopeName,
Record *record) {
auto name = "Class: " + scopeName;
enterScope(name, record);
}
void SymbolTable::enterMethodScope(Method *scopeMethod) {
auto name = "Method: " + scopeMethod->getID();
enterScope(name, scopeMethod);
}
void SymbolTable::enterMethodScope(const std::string &scopeName,
Record *record) {
auto name = "Method: " + scopeName;
enterScope(name, record);
}
void SymbolTable::exitScope() { current = current->getParent(); }
void SymbolTable::addVariable(const std::string &type, const std::string &id) {
current->addVariable(type, id);
}
void SymbolTable::addMethod(const std::string &type, const std::string &id) {
current->addMethod(type, id);
}
void SymbolTable::addClass(const std::string &id) { current->addClass(id); }
void SymbolTable::addIntegerVariable(const std::string &id) {
current->addVariable("int", id);
}
void SymbolTable::addBooleanVariable(const std::string &id) {
current->addVariable("boolean", id);
}
void SymbolTable::printTable(std::ostream &os) const {
int count = 0;
auto *scope = root.get();
os << "digraph {\n";
scope->printScope(count, os);
os << "}\n";
}
std::string SymbolTable::getCurrentScopeName() const {
return current->getName();
}
Scope *SymbolTable::getParentScope() const { return current->getParent(); }
Scope *SymbolTable::getCurrentScope() const { return current; }
Variable *SymbolTable::lookupVariable(const std::string &id) {
return current->lookupVariable(id);
}
Method *SymbolTable::lookupMethod(const std::string &id) {
return current->lookupMethod(id);
}
Class *SymbolTable::lookupClass(const std::string &id) {
return current->lookupClass(id);
}
Variable *SymbolTable::lookupVariableInScope(const std::string &key) {
return current->lookupVariableInScope(key);
}
Record *SymbolTable::getCurrentRecord() const { return current->getRecord(); }
Scope *SymbolTable::resolveScope(const std::string &className,
const std::string &methodName) {
auto *classLookup = lookupClass(className);
if (classLookup == nullptr) {
return nullptr;
}
enterClassScope(classLookup);
auto *methodLookup = classLookup->lookupMethod(methodName);
if (methodLookup == nullptr) {
return nullptr;
}
enterMethodScope(methodLookup);
Scope *scope = getCurrentScope();
exitScope();
exitScope();
return scope;
}