-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScopeTable.java
108 lines (91 loc) · 2.83 KB
/
ScopeTable.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
package cool;
import java.util.*;
public class ScopeTable<T> {
class ClassScope{
public AST.class_ cl;
public int scope;
public ClassScope(AST.class_ c){
this.cl = c;
this.scope = 0;
}
}
private HashMap< ClassScope , ArrayList<HashMap<String,T>> > maps=new HashMap<ClassScope,ArrayList<HashMap<String, T>>>();
public AST.class_ currentClass;
public ScopeTable(){
}
void insertClass(AST.class_ c){
System.out.println("Inserting new class namespace");
maps.put(new ClassScope(c),new ArrayList<HashMap<String,T>>(15));
currentClass = c;
ClassScope cscope = searchTable(currentClass);
ArrayList<HashMap<String,T>> table = maps.get(cscope);
table.add(new HashMap<String,T>());
}
ClassScope searchTable(AST.class_ c){
for (HashMap.Entry<ClassScope,ArrayList<HashMap<String,T>>> m : maps.entrySet()){
ClassScope cscope = m.getKey();
if (cscope.cl.equals(c)){
return cscope;
}
}
return null;
}
void printTable (AST.class_ c){
ClassScope cs = searchTable(c);
for (HashMap.Entry<String,T> h : maps.get(cs).get(0).entrySet()){
System.out.println(h.getKey());
}
}
void insert(String s, T t){
ClassScope cscope = searchTable(currentClass);
ArrayList<HashMap<String,T>> table = maps.get(cscope);
if (table.get(cscope.scope) == null){
System.out.println("The HashMap is empty in the index:"+cscope.scope);
table.add(new HashMap<String,T>());
}
// System.out.println("The HashMap is empty in the index:"+cscope.scope);
table.get(cscope.scope).put(s,t);
System.out.println("The stored:" + s);
}
void enterScope(){
System.out.println("Entering Scope");
ClassScope cscope = searchTable(currentClass);
cscope.scope++;
maps.get(cscope).add(new HashMap<String,T>());
System.out.println("Entered");
}
void exitScope(){
System.out.println("Exiting Scope");
ClassScope cscope = searchTable(currentClass);
if (cscope.scope >0 ){
maps.get(cscope).remove(cscope.scope);
cscope.scope--;
}
System.out.println("Exited");
}
T lookUpClassSpace(String t){
System.out.println("Looking in the class namespace");
ClassScope cscope = searchTable(currentClass);
if (maps.get(cscope).size() == 0){
System.out.println("The scope table of class " + currentClass.name + " is empty");
//System.out.println("Nothing found in Class namespace");
return null;
}else{
return maps.get(cscope).get(0).get(t);
}
}
T lookUpLocal(String t){
System.out.println("Looking in the local scope");
ClassScope cscope = searchTable(currentClass);
return maps.get(cscope).get(cscope.scope).get(t);
}
T lookUpGlobal(String t){
System.out.println("Looking in the global scope");
ClassScope cscope = searchTable(currentClass);
for ( int i = cscope.scope; i>=0 ; i--){
if (maps.get(cscope).get(i).containsKey(t))
return maps.get(cscope).get(i).get(t);
}
return null;
}
}