-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSymTab.cpp
More file actions
67 lines (57 loc) · 1.53 KB
/
SymTab.cpp
File metadata and controls
67 lines (57 loc) · 1.53 KB
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
#include <iostream>
#include "SymTab.hpp"
#include "Globals.hpp"
SymTab::SymTab() {
i = 0;
std::map<std::string, TypeDescriptor*> firstSymTab;
symTabs.push_back(firstSymTab);
}
bool SymTab::isDefined(std::string vName) {
return symTabs[i].find(vName) != symTabs[i].end();
}
void SymTab::setValueFor(std::string vName, TypeDescriptor* td) {
// Define a variable by setting its initial value.
if (verbose) {
std::cout << vName << " <- ";
td->print();
std::cout << std::endl;
}
if (isDefined(vName)) {
delete symTabs[i].find(vName)->second;
symTabs[i].erase(vName);
}
symTabs[i][vName] = td;
}
TypeDescriptor* SymTab::getValueFor(std::string vName) {
if( ! isDefined(vName)) {
std::cout << "SymTab::getValueFor: " << vName << " has not been defined.\n";
exit(1);
}
if (verbose) {
std::cout << "SymTab::getValueFor: " << vName << " contains ";
symTabs[i].find(vName)->second->print();
std::cout << std::endl;
}
return symTabs[i].find(vName)->second;
}
void SymTab::print() {
for(auto it = symTabs[i].begin(); it != symTabs[i].end(); it++) {
std::cout << it->first << " = ";
it->second->print();
std::cout << std::endl;
}
}
void SymTab::openScope(std::map<std::string, TypeDescriptor*> newSymTab) {
symTabs.push_back(newSymTab);
i++;
}
void SymTab::closeScope() {
symTabs.pop_back();
i--;
}
void SymTab::storeReturnValue(TypeDescriptor *val) {
returnVal = val;
}
TypeDescriptor* SymTab::getReturnValue() {
return returnVal;
}