-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSymbolTable.java
More file actions
68 lines (61 loc) · 1.72 KB
/
SymbolTable.java
File metadata and controls
68 lines (61 loc) · 1.72 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
import java.util.ArrayList;
class SymbolTable
{
ArrayList<ArrayList<Pair<String,String>>> table;
public SymbolTable()
{
table = new ArrayList<ArrayList<Pair<String,String>>>();
table.add(new ArrayList<Pair<String,String>>());
}
public String get(String s) throws UTDLangException
{
for (int i = table.size()-1; i >= 0; --i)
for (Pair<String,String> p : table.get(i))
{
if (p.getKey().equals(s))
return p.getValue();
}
throw new UTDLangException("Error: variable not declared " + s);
}
public void addVar(String id, String t) throws UTDLangException
{
for (Pair<String,String> p : table.get(table.size()-1))
{
if (p.getKey().equals(id))
throw new UTDLangException("Error: tried to redeclare variable " + id);
}
table.get(table.size()-1).add(new Pair<String,String>(id,t));
return;
}
public void addRoutine(String id, InOutList params) throws UTDLangException
{
String pType = params.getType();
for (Pair<String,String> p : table.get(table.size()-1))
{
if (p.getKey().equals(id))
throw new UTDLangException("Error: tried to redeclare routine " + id);
}
table.get(table.size()-1).add(new Pair<String,String>(id,pType));
return;
}
public void startScope()
{
table.add( new ArrayList<Pair<String,String>>());
}
public void endScope()
{
table.remove(table.size()-1);
}
public String toString()
{
String ret = "";
String t = "";
for (ArrayList<Pair<String,String>> v : table)
{
for (Pair<String,String> p : v)
ret += t + p.getKey() + " " + p.getValue().toString() + "\n";
t += "\t";
}
return ret;
}
}