-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTigerSymbolTable.java
More file actions
80 lines (65 loc) · 1.64 KB
/
TigerSymbolTable.java
File metadata and controls
80 lines (65 loc) · 1.64 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
import java.io.*;
import java.util.*;
public class TigerSymbolTable {
private Map<String, Object> table = new HashMap<String, Object>();
private TigerSymbolTable parent = null;
public TigerSymbolTable(Map<String, Object> table) {
this.table = table;
}
public TigerSymbolTable() {
}
public Map<String, Object> getTable() {
return table;
}
public Object assign(String name, Object value) {
return table.put(name, value);
}
public void setParent(TigerSymbolTable parent) {
this.parent = parent;
}
public boolean hasParent() {
if (this.parent == null)
return false;
return true;
}
public TigerSymbolTable getParent() {
return this.parent;
}
public boolean isLocal(String varName) {
return table.containsKey(varName);
}
public Object getValue(String varName) {
if (table.containsKey(varName)) {
return table.get(varName);
}
if (this.hasParent()) {
return getParent().getValue(varName);
}
return null;
}
public Object removeLocally(String varName) {
return table.remove(varName);
}
public Object undefine(String name) {
Object undefinedObject = null;
if (table.containsKey(name)){
undefinedObject = table.remove(name);
}
if (hasParent())
{
Object parentTableRemovedElement = getParent().undefine(name);
if (undefinedObject == null)
{
undefinedObject = parentTableRemovedElement;
}
}
return undefinedObject;
}
//Deleting scope would be done in Visitor by assiging current scope pointer
//to its parent
public TigerSymbolTable newScope() {
TigerSymbolTable scope = new TigerSymbolTable();
scope.setParent(this);
return scope;
}
}