-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRTree.java
101 lines (84 loc) · 1.69 KB
/
IRTree.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
// COMS22303: IR tree
import java.util.*;
class IRTree
{
private String op;
private ArrayList<IRTree> sub;
// Constructors for IR tree nodes with various numbers of subtrees
public IRTree()
{
this.op = op;
sub = new ArrayList<IRTree>();
}
public IRTree(String op)
{
this.op = op;
sub = new ArrayList<IRTree>();
}
public IRTree(String op, IRTree sub1)
{
this.op = op;
sub = new ArrayList<IRTree>();
sub.add(sub1);
}
public IRTree(String op, IRTree sub1, IRTree sub2)
{
this.op = op;
sub = new ArrayList<IRTree>();
sub.add(sub1);
sub.add(sub2);
}
public IRTree(String op, IRTree sub1, IRTree sub2, IRTree sub3)
{
this.op = op;
sub = new ArrayList<IRTree>();
sub.add(sub1);
sub.add(sub2);
sub.add(sub3);
}
public IRTree(String op, IRTree sub1, IRTree sub2, IRTree sub3, IRTree sub4, IRTree sub5)
{
this.op = op;
sub = new ArrayList<IRTree>();
sub.add(sub1);
sub.add(sub2);
sub.add(sub3);
sub.add(sub4);
sub.add(sub5);
}
// Methods to add operator and subtrees
public void setOp(String op)
{
this.op = op;
}
public void addSub(IRTree sub1)
{
sub.add(sub1);
}
// Methods to access operator and subtrees
public String getOp()
{
return op;
}
public IRTree getSub(int i)
{
if (i >= sub.size()) {
System.out.println("IRTree error accessing subtree "+i+" of "+op+" node");
}
return sub.get(i);
}
// toString
public String toString()
{
int i;
if (sub.size() == 0) {
return op;
}
String s = "("+op;
for (i=0; i<sub.size(); i++) {
s += " "+sub.get(i).toString();
}
s += ")";
return s;
}
}