-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymTab.cpp
More file actions
85 lines (71 loc) · 2.25 KB
/
SymTab.cpp
File metadata and controls
85 lines (71 loc) · 2.25 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "SymTab.h"
using namespace std;
SymTab::SymTab() {
MethodDesc* readInt = new MethodDesc("readInt", 1, 0, 0);
MethodDesc* readFloat = new MethodDesc("readFloat", 2, 0, 0);
MethodDesc* println = new MethodDesc("println", 0, 0, 0);
MethodDesc* printInt = new MethodDesc("printInt", 0, 1, 1);
MethodDesc* printFloat = new MethodDesc("printFloat", 0, 1, 2);
MethodDesc* printString = new MethodDesc("printString", 0, 1, 4);
SimpleIOMap.insert(pair<string, Entry*>(readInt->getName(), readInt));
SimpleIOMap.insert(pair<string, Entry*>(readFloat->getName(), readFloat));
SimpleIOMap.insert(pair<string, Entry*>(println->getName(), println));
SimpleIOMap.insert(pair<string, Entry*>(printInt->getName(), printInt));
SimpleIOMap.insert(pair<string, Entry*>(printFloat->getName(), printFloat));
SimpleIOMap.insert(pair<string, Entry*>(printString->getName(), printString));
ClassDesc* SimpleIO = new ClassDesc("SimpleIO", NULL);
pkgScope.insert(pair<string, Entry*>(SimpleIO->getName(), SimpleIO));
currentMap = pkgScope;
}
SymTab::~SymTab() {
pkgScope.clear();
classMap.clear();
currentMap.clear();
}
void
SymTab::openClassScope() {
pkgScope = currentMap;
currentMap = classMap;
}
void
SymTab::openMethodScope() {
map<string, Entry *> methodScope;
classMap = currentMap;
currentMap = methodScope;
}
void
SymTab::closeMethodScope() {
currentMap = classMap;
}
void
SymTab::install(Entry* e) {
map<string, Entry*>::iterator it;
it = currentMap.find(e->getName());
if (it != currentMap.end()) {
printf("***ERROR: Duplicate Entry.\n");
errorNum++;
}
else
currentMap.insert(pair<string, Entry*>(e->getName(), e));
}
Entry*
SymTab::lookup(const char* name) {
map<string, Entry*>::iterator it;
it = currentMap.find(name);
if (it == currentMap.end()) {
it = classMap.find(name);
if (it == classMap.end()) {
it = pkgScope.find(name);
if (it == pkgScope.end()) {
it = SimpleIOMap.find(name);
if (it == SimpleIOMap.end()) {
return NULL;
} else
return it->second;
} else
return it->second;
} else
return it->second;
} else
return it->second;
}