-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalSymbolTable.java
76 lines (64 loc) · 1.88 KB
/
GlobalSymbolTable.java
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
//
// written (and rewritten) by Tim Budd
//
import java.util.Hashtable;
class GlobalSymbolTable implements SymbolTable {
private Hashtable table = new Hashtable(); //let it grow automatically
public void enterConstant(String name, Ast value)
{ enterSymbol(new ConstantSymbol(name, value)); }
public void enterType(String name, Type type)
{ enterSymbol (new TypeSymbol(name, type)); }
public void enterVariable(String name, Type type)
{ enterSymbol (new GlobalSymbol(name, new AddressType(type), name)); }
public void enterFunction(String name, FunctionType ft)
{ enterSymbol (new GlobalSymbol(name, ft, name)); }
private void enterSymbol(Symbol s) {
table.put(s.name, s);
}
private Symbol findSymbol(String name) {
Symbol s = (Symbol)table.get(name);
if(s != null) {
return s;
} else {
return null;
}
}
public boolean nameDefined(String name) {
Symbol s = findSymbol(name);
if (s != null) return true;
else return false;
}
//TODO: i modified from original to meet ParseExceptions thrown specified in
//assignment description...not sure if this was necessary?
public Type lookupType(String name) throws ParseException {
Symbol s = findSymbol(name);
if(s != null) {
if(s instanceof TypeSymbol) {
TypeSymbol ts = (TypeSymbol) s;
return ts.type;
} else {
throw new ParseException(30);
}
} else {
throw new ParseException(42, name);
}
}
public Ast lookupName(Ast base, String name) throws ParseException {
Symbol s = findSymbol(name);
if (s == null)
throw new ParseException(42, name);
// now have a valid symbol
if (s instanceof GlobalSymbol) {
GlobalSymbol gs = (GlobalSymbol) s;
return new GlobalNode(gs.type, name);
}
if (s instanceof ConstantSymbol) {
ConstantSymbol cs = (ConstantSymbol) s;
return cs.value;
}
return null; // should never happen
}
public int size() {
return 0;
}
}