-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.java
More file actions
94 lines (84 loc) · 2.8 KB
/
SymbolTable.java
File metadata and controls
94 lines (84 loc) · 2.8 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
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class SymbolTable {
public enum SymbolKind {
FIELD,
METHOD,
PARAMETER,
LOCAL
}
public static final class Symbol {
public final String name;
public final SymbolKind kind;
public final TypeInfo type;
public final boolean isFinal;
public final List<String> parameterNames;
public final List<TypeInfo> parameterTypes;
public Symbol(String name, SymbolKind kind, TypeInfo type, boolean isFinal, List<String> parameterNames,
List<TypeInfo> parameterTypes) {
this.name = name;
this.kind = kind;
this.type = type;
this.isFinal = isFinal;
if (parameterNames == null) {
this.parameterNames = Collections.emptyList();
} else {
this.parameterNames = new ArrayList<>(parameterNames);
}
if (parameterTypes == null) {
this.parameterTypes = Collections.emptyList();
} else {
this.parameterTypes = new ArrayList<>(parameterTypes);
}
}
}
private final Deque<Map<String, Symbol>> scopes = new ArrayDeque<>();
private final List<Symbol> globalDeclarations = new ArrayList<>();
public SymbolTable() {
scopes.push(new LinkedHashMap<>());
}
public void enterScope() {
scopes.push(new LinkedHashMap<>());
}
public void exitScope() {
if (scopes.size() <= 1) {
return;
}
scopes.pop();
}
public Symbol declare(String name, SymbolKind kind, TypeInfo type, boolean isFinal, List<String> parameterNames,
List<TypeInfo> parameterTypes) throws SemanticException {
Map<String, Symbol> current = scopes.peek();
if (current.containsKey(name)) {
throw new SemanticException("same-scope redeclaration of '" + name + "'");
}
Symbol symbol = new Symbol(name, kind, type, isFinal, parameterNames, parameterTypes);
current.put(name, symbol);
if (scopes.size() == 1) {
globalDeclarations.add(symbol);
}
return symbol;
}
public Symbol lookup(String name) {
for (Map<String, Symbol> scope : scopes) {
Symbol symbol = scope.get(name);
if (symbol != null) {
return symbol;
}
}
return null;
}
public Symbol lookupGlobal(String name) {
for (Symbol symbol : globalDeclarations) {
if (symbol.name.equals(name)) {
return symbol;
}
}
return null;
}
}