-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBlockSymbolTable.java
More file actions
96 lines (79 loc) · 1.86 KB
/
Copy pathBlockSymbolTable.java
File metadata and controls
96 lines (79 loc) · 1.86 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
95
96
//Mark Klara
//mak241@pitt.edu
//CS 1622 - Project 3
//BlockSymbolTable.java
package symboltable;
import helper.*;
import java.util.List;
import java.util.Hashtable;
import java.util.Set;
public class BlockSymbolTable implements Scope
{
protected Scope parent;
protected Hashtable<String, Variable> vars;
protected Hashtable<String, BlockSymbolTable> blocks;
public BlockSymbolTable(Scope parent)
{
this.parent = parent;
vars = new Hashtable<String, Variable>();
blocks = new Hashtable<String, BlockSymbolTable>();
}
public Scope enterScope(String name)
{
return blocks.get(name);
}
public Scope exitScope()
{
return parent;
}
public void addBlock(String name)
{
blocks.put(name, new BlockSymbolTable(this));
}
public void addVariable(String name, String type)
{
vars.put(name, new Variable(name, type));
}
public Variable localVarLookup(String name)
{
return vars.get(name);
}
public Variable lookupVariable(String name)
{
Variable var = localVarLookup(name);
if(var != null)
{
return var;
}
else
{
return parent.lookupVariable(name);
}
}
public boolean lookupMethod(String name, String[] paramNames, String[] paramTypes, String returnType)
{
return parent.lookupMethod(name, paramNames, paramTypes, returnType);
}
public void printIndentation(int indentLevel)
{
System.out.println("");
for(int i = 0; i < indentLevel; i++)
{
System.out.print("\t");
}
}
public void print(int indentLevel)
{
List<String> keys = Helper.keysToSortedList(vars.keySet());
for(int i = 0; i < keys.size(); i++)
{
printIndentation(indentLevel);
System.out.print(vars.get(keys.get(i)).getType() + " " + vars.get(keys.get(i)).getName() + ";");
}
keys = Helper.keysToSortedList(blocks.keySet());
for(int i = 0; i < keys.size(); i++)
{
blocks.get(keys.get(i)).print(indentLevel+1);
}
}
}