-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVarTable.java
More file actions
66 lines (60 loc) · 1.47 KB
/
VarTable.java
File metadata and controls
66 lines (60 loc) · 1.47 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
import java.util.ArrayList;
class VarTable
{
ArrayList<ArrayList<Pair<String,FullType>>> table;
public VarTable()
{
table = new ArrayList<ArrayList<Pair<String,FullType>>>();
table.add(new ArrayList<Pair<String,FullType>>());
}
public FullType getType(String s)
{
for (int i = table.size()-1; i >= 0; --i)
for (Pair<String,FullType> p : table.get(i))
{
if (p.getKey().equals(s))
return p.getValue();
}
return null;
}
public boolean add(String id, FullType t)
{
for (Pair<String,FullType> p : table.get(table.size()-1))
{
if (p.getKey().equals(id))
return false;
}
table.get(table.size()-1).add(new Pair<String,FullType>(id,t));
return true;
}
public boolean addFun(String id, FullType t)
{
for (Pair<String,FullType> p : table.get(table.size()-2))
{
if (p.getKey().equals(id))
return false;
}
table.get(table.size()-2).add(new Pair<String,FullType>(id,t));
return true;
}
public void enterScope()
{
table.add( new ArrayList<Pair<String,FullType>>());
}
public void exitScope()
{
table.remove(table.size()-1);
}
public String toString()
{
String ret = "";
String t = "";
for (ArrayList<Pair<String,FullType>> v : table)
{
for (Pair<String,FullType> p : v)
ret += t + p.getKey() + " " + p.getValue().toString() + "\n";
t += "\t";
}
return ret;
}
}