-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.java
More file actions
128 lines (107 loc) · 3.04 KB
/
Copy pathSymbolTable.java
File metadata and controls
128 lines (107 loc) · 3.04 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
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
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.Map;
public class SymbolTable {
Deque<Map<String, Symbol>> table;
ClassDeclSymbol thisSymbol;
public SymbolTable() {
table = new ArrayDeque<Map<String, Symbol>>();
}
public void enter(){
Map<String, Symbol> scope = new LinkedHashMap<String, Symbol>();
table.push(scope);
}
public void enter(Map<String, Symbol> parentScope){
Map<String, Symbol> scope = new LinkedHashMap<String, Symbol>();
scope.putAll(parentScope);
table.push(scope);
}
public Map<String, Symbol> exit(){
return table.pop();
}
public Map<String, Symbol> peek(){
return table.peek();
}
public Symbol insert(String name, Symbol symbol) throws Exception {
Map<String, Symbol> scope = table.peek();
return scope.putIfAbsent(name, symbol);
}
public Symbol lookup(String name){
boolean found = false;
for(Map<String, Symbol> scope: table){
if(!scope.containsKey(name)){
continue;
} else {
found = true;
return scope.get(name);
}
}
if(!found){
return null;
}
return null;
}
public Symbol lookupField(String name){
boolean found = false;
for(Map<String, Symbol> scope: table){
if(!scope.containsKey(name)){
continue;
} else {
found = true;
if(scope.get(name) instanceof FunctionSymbol){
continue;
}
return scope.get(name);
}
}
if(!found){
return null;
}
return null;
}
public FunctionSymbol lookupMethod(String name){
boolean found = false;
for(Map<String, Symbol> scope: table){
if(!scope.containsKey(name)){
continue;
} else {
found = true;
if(!(scope.get(name) instanceof FunctionSymbol)){
continue;
}
return (FunctionSymbol)scope.get(name);
}
}
if(!found){
return null;
}
return null;
}
public ClassDeclSymbol lookupType(String name){
if(table.size() > 0){
Map<String, Symbol> scope = table.getLast();
if(scope.containsKey(name)){
return (ClassDeclSymbol)scope.get(name);
} else {
return null;
}
} else {
return null;
}
}
public void insertThis(String name){
thisSymbol = lookupType(name);
}
public void insertThis(ClassDeclSymbol symbol){
thisSymbol = symbol;
}
public ClassDeclSymbol getThis(){
return thisSymbol;
}
public void print(){
for(Map<String, Symbol> scope: table){
System.out.println(scope);
}
}
}