This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolTable.java
112 lines (98 loc) · 2.4 KB
/
SymbolTable.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cop5556sp17;
import java.util.Stack;
import java.util.HashMap;
import java.util.ArrayList;
import cop5556sp17.AST.Dec;
public class SymbolTable {
//TODO add fields
final int pervasiveID;
int current_scope;
int next_scope;
int instance_count;
Stack<Integer> scope_stack;
HashMap<String, ArrayList<Dec>> hashTable;
public SymbolTable() {
//TODO: IMPLEMENT THIS
pervasiveID = 0;
current_scope = 0;
next_scope = 1;
instance_count = 0;
scope_stack = new Stack<Integer>();
hashTable = new HashMap<String, ArrayList<Dec>>();
enterScope();
}
/**
* to be called when block entered
*/
public void enterScope(){
//TODO: IMPLEMENT THIS
current_scope = next_scope++;
scope_stack.push(current_scope);
}
/**
* leaves scope
*/
public void leaveScope(){
//TODO: IMPLEMENT THIS
scope_stack.pop();
current_scope = scope_stack.peek();
}
public boolean insert(String ident, Dec dec){
//TODO: IMPLEMENT THIS
ArrayList<Dec> chain = hashTable.get(ident);
if(chain == null) {
dec.setScopeID(current_scope);
ArrayList<Dec> list = new ArrayList<Dec>();
list.add(dec);
hashTable.put(ident, list);
instance_count++;
}
else {
for(Dec tempDec : chain) {
if(tempDec.getIdent().getText().equals(ident)) {
if(tempDec.getScopeID() == current_scope)
return false;
}
}
dec.setScopeID(current_scope);
chain.add(dec);
instance_count++;
}
return true;
}
public Dec lookup(String ident){
//TODO: IMPLEMENT THIS
Dec pervasive = null;
Dec best = null;
ArrayList<Dec> chain = hashTable.get(ident);
if(chain == null)
return null;
for(Dec tempDec : chain) {
if(tempDec.getIdent().getText().equals(ident)) {
if(tempDec.getScopeID() == pervasiveID)
pervasive = tempDec;
else {
for(int i = scope_stack.size()-1; i >= 0; i--) {
if(scope_stack.get(i) == tempDec.getScopeID()) {
best = tempDec;
break;
}
else if(best != null && scope_stack.get(i) == best.getScopeID())
break;
}
}
}
}
if(best != null)
return best;
else if(pervasive != null)
return pervasive;
else
return null;
}
@Override
public String toString() {
//TODO: IMPLEMENT THIS
return "Current scope: " + current_scope + " Instances count: " + instance_count;
}
}