-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowTreeVisitor.java
220 lines (186 loc) · 6.07 KB
/
ShowTreeVisitor.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*Name: Sachin Subhas Nambisan & Saarthi Baluja */
import absyn.*;
import java.lang.String;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class ShowTreeVisitor implements AbsynVisitor {
final static int SPACES = 4;
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private PrintStream ps = new PrintStream(baos); //creates a new stream to hold output
private PrintStream prev = System.out; //saves the prevois stream
public ShowTreeVisitor(){
System.setOut(ps); //all output now goes here
}
private void indent(int level) {
for (int i = 0; i < level * SPACES; i++)
System.out.print(" ");
}
public void visit(ExpList expList, int level) {
while (expList != null && expList.head != null) {
expList.head.accept(this, level);
expList = expList.tail;
}
}
public void visit(AssignExp exp, int level) {
indent(level);
System.out.println("AssignExp:");
level++;
exp.lhs.accept(this, level);
exp.rhs.accept(this, level);
}
public void visit(IfExp exp, int level) {
indent(level);
System.out.println("IfExp:");
level++;
exp.test.accept(this, level);
exp.thenpart.accept(this, level);
if (exp.elsepart != null)
exp.elsepart.accept(this, level);
}
public void visit(IntExp exp, int level) {
indent(level);
System.out.println("IntExp: " + exp.value);
}
public void visit(OpExp exp, int level) {
indent(level);
System.out.print("OpExp:");
switch (exp.op) {
case OpExp.PLUS:
System.out.println(" + ");
break;
case OpExp.MINUS:
System.out.println(" - ");
break;
case OpExp.MUL:
System.out.println(" * ");
break;
case OpExp.DIV:
System.out.println(" / ");
break;
case OpExp.EQ:
System.out.println(" = ");
break;
case OpExp.LT:
System.out.println(" < ");
break;
case OpExp.GT:
System.out.println(" > ");
break;
default:
System.out.println("Unrecognized operator at line " + exp.row + " and column " + exp.col);
}
level++;
exp.left.accept(this, level);
exp.right.accept(this, level);
}
public void visit(RepeatExp exp, int level) {
indent(level);
System.out.println("RepeatExp:");
level++;
exp.exps.accept(this, level);
exp.test.accept(this, level);
}
public void visit(VarExp exp, int level) {
indent(level);
System.out.println("VarExp: ");
exp.value.accept(this, level);
}
public void visit(WriteExp exp, int level) {
indent(level);
System.out.println("WriteExp:");
exp.output.accept(this, ++level);
}
public void visit(NameTy t, int level) {
indent(level);
// Get the string of the represented int
String typeString = "";
if (t.type == 0) {
typeString = "int";
} else {
typeString = "void";
}
System.out.println("NameTy: " + typeString);
}
public void visit(VarDecList varDecList, int level) {
while (varDecList != null && varDecList.head != null) {
varDecList.head.accept(this, level);
varDecList = varDecList.tail;
}
}
public void visit(DecList decList, int level) {
while (decList != null && decList.head != null) {
decList.head.accept(this, level);
decList = decList.tail;
}
}
public void visit(ArrayDec arr, int level) {
indent(level);
System.out.println("ArrayDec: ");
arr.type.accept(this, ++level);
indent(++level);
System.out.println("Name: " + arr.name);
if (arr.size != null)
arr.size.accept(this, ++level);
}
public void visit(SimpleDec dec, int level) {
indent(level);
System.out.println("SimpleDec: " + String.valueOf(dec.name));
dec.type.accept(this, ++level);
}
public void visit(FunctionDec dec, int level) {
indent(level);
System.out.println("FunctionDec: ");
dec.type.accept(this, ++level);
indent(++level);
System.out.println("Name: " + dec.func);
dec.params.accept(this, ++level);
dec.body.accept(this, ++level);
}
public void visit(CompoundExp exp, int level) {
indent(level);
System.out.println("CompoundExp: ");
if (exp.decs != null)
exp.decs.accept(this, ++level);
if (exp.exps != null)
exp.exps.accept(this, ++level);
}
public void visit(ReturnExp e, int level) {
indent(level);
System.out.println("ReturnExp: ");
if (e.exp != null)
e.exp.accept(this, ++level);
}
public void visit(WhileExp exp, int level) {
indent(level);
System.out.println("WhileExp: ");
exp.body.accept(this, ++level);
exp.test.accept(this, ++level);
}
public void visit(CallExp exp, int level) {
indent(level);
System.out.println("CallExp: " + exp.func + " ");
exp.args.accept(this, ++level);
}
public void visit(NilExp exp, int level) {
indent(level);
System.out.println("NilExp: ");
}
public void visit(IndexVar var, int level) {
indent(level);
System.out.println("IndexVar: " + var.name + " ");
var.index.accept(this, ++level);
}
public void visit(SimpleVar var, int level) {
indent(level);
System.out.println("SimpleVar: " + var.name + " ");
}
public void visit(ErrorExp exp, int level ){
indent(level);
System.out.println("null");
}
public String treeString(){
System.out.flush();
System.setOut(prev);
return baos.toString();
}
}