This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.java
More file actions
98 lines (86 loc) · 2.7 KB
/
Scope.java
File metadata and controls
98 lines (86 loc) · 2.7 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
97
98
import java.util.LinkedList;
public class Scope {
public Node root;
int scopeControl;
ScopeNode scopeInfo;
LinkedList<Integer> scopeIDs;
LinkedList<int[]> family;
Scope(Node r){
root = r;
scopeControl = 0;
scopeInfo = new ScopeNode(0);
scopeIDs = new LinkedList<>();
scopeIDs.add(0);
family = new LinkedList<>();
}
public Node run(){
int mainIndex = 0;
for(int i = 0; i < root.children.size(); i++){
if(root.children.get(i).value.equals("main")){
mainIndex = i;
i = root.children.size();
}
}
//prior to main scoping
for(int i = 0; i < mainIndex; i++){
scope(root.children.get(i),scopeControl, scopeInfo);
}
//main scoping
for(int i = mainIndex; i < root.children.size(); i++){
mainScopeAssign(root.children.get(i));
}
return root;
}
public ScopeNode scopeHierachy(){
/*
System.out.println("Family print:");
for(int i = 0; i < family.size(); i++){
System.out.println(family.get(i)[0] + " " + family.get(i)[1]);
}
*/
for(int i = 0; i < family.size(); i++){
buildFamily(scopeInfo, family.get(i));
}
return scopeInfo;
}
public void scope(Node n, int scope, ScopeNode sN){
if(n.value.equals("ProcDefs")){
scope++;
scopeControl++;
scope = scopeControl;
}
n.setScopeID(scope);
ScopeNode c = new ScopeNode(scope);
if(!scopeIDs.contains(scope)){
//System.out.println(sN.scopeID + " " + c.scopeID);
//sN.addChild(c);
scopeIDs.add(scope);
int[] collect = {sN.scopeID,c.scopeID};
family.add(collect);
}
for (int i = 0; i < n.children.size(); i++) {
scope(n.children.get(i),scope,c);
}
}
public void buildFamily(ScopeNode n,int[] arr) {
if(n.scopeID==arr[0]){
n.addChild(new ScopeNode(arr[1]));
}
for (int i = 0; i < n.children.size(); i++) {
buildFamily(n.children.get(i), arr);
}
}
public void mainScopeAssign(Node n) {
n.setScopeID(0);
for (int i = 0; i < n.children.size(); i++) {
mainScopeAssign(n.children.get(i));
}
}
public void printTree(Node n, String indent, boolean last) {
System.out.println(indent + "+- " + n.value);
indent += last ? " " : "| ";
for (int i = 0; i < n.children.size(); i++) {
printTree(n.children.get(i), indent, i == n.children.size() - 1);
}
}
}