-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPP_Visitor.java
More file actions
386 lines (326 loc) · 10.6 KB
/
Copy pathPP_Visitor.java
File metadata and controls
386 lines (326 loc) · 10.6 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import syntaxtree.*;
/*
* this class uses the Visitor pattern
* to pretty print the miniC program
* The idea is to have each node return its string representation!
*/
/*
* Video 1: https://brandeis.zoom.us/rec/share/1MhxpqLQM9OeNTOUnk6lNUReAgUGw0aMtWXXXoq9lN1VzqE1bYo8rIo-J2LGv4wZ.pbfOxx1qH-WXYhH8?startTime=1710985894000
* Video 2: https://brandeis.zoom.us/rec/share/bqNp_Dlf_PztN0WFbvI-6XCPUBPZnNmgHY9fuF7JMicsf5PJru62dqz7yHY_fw-O.lXGsx5cORhzcv4Tz?startTime=1710971702000
* Team: Xin Chen
*/
public class PP_Visitor implements Visitor
{
private String indentString(int indent) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4*indent; ++i) {
sb.append(' ');
}
return sb.toString();
}
public Object visit(Program node, Object data){
String m = (String) node.m.accept(this,data);
String c = (String) node.c.accept(this,data);
return m + "\n" + c;
}
public Object visit(MainClass node, Object data){
int indent = (int) data;
String i = (String) node.i.accept(this, data);
String s = (String) node.s.accept(this, data);
return "class " + i + "{\n" +
indentString(indent+1) + "public static void main(String[] args){\n" +
indentString(indent+2) + s + "\n" +
indentString(indent+1) + "}\n" +
indentString(indent) + "}\n";
}
public Object visit(ClassDecl node, Object data){
int indent = (int) data;
String i = (String) node.i.accept(this, data);
String v = (String) node.v.accept(this, indent + 1);
String m = (String) node.m.accept(this, indent + 1);
return "class " + i + "{\n" +
v + m + "}"
;
}
public Object visit(VarDecl node, Object data){
int indent = (int) data;
String t = (String) node.t.accept(this,indent);
String i = (String) node.i.accept(this,indent);
return indentString(indent)+t + " "+i +";\n";
}
public Object visit(MethodDecl node, Object data){
//
int indent = (int) data;
String type = (String) node.t.accept(this,indent);
String id = (String) node.i.accept(this,indent);
String formalList = "";
if (node.f!=null){
formalList = (String) node.f.accept(this,indent);
}
String varList = "";
if (node.v!=null){
varList = (String) node.v.accept(this,indent+1);
}
String statementList = "";
if (node.s!=null){
statementList = (String) node.s.accept(this,indent+1);
}
String expr = (String) node.e.accept(this,indent);
return
indentString(indent)+ "public " +
type+" "+id+"("+formalList+"){\n"+
varList +
statementList+
indentString(indent+1)+"return "+expr +";\n"+
indentString(indent)+"}\n";
}
public Object visit(Formal node, Object data){
//
int indent = (int) data;
String type = (String) node.t.accept(this,indent);
String id = (String) node.i.accept(this,indent);
return type+" "+id;
}
public Object visit(IntArrayType node, Object data){
return "int[]";
}
public Object visit(IntegerType node, Object data){
//
int indent = (int) data;
return "int";
}
public Object visit(BooleanType node, Object data){
//
int indent = (int) data;
return "boolean";
}
public Object visit(IdentifierType node, Object data){
return node.s;
}
public Object visit(Block node, Object data){
//
int indent = (int) data;
String result="";
if (node.slist!=null){
result = (String) node.slist.accept(this,indent+1);
}
return indentString(indent)+"{\n"+
result+
indentString(indent)+"}\n";
}
public Object visit(If node, Object data){
//
int indent = (int) data;
String expr = (String)node.e.accept(this,indent+1);
String s1 = (String) node.s1.accept(this,indent+1);
String s2 = (String) node.s2.accept(this,indent+1);
return indentString(indent)+
"if ("+expr+")\n"+
s1+
indentString(indent)+"else\n"+
s2;
}
public Object visit(While node, Object data){
int indent = (int) data;
String e = (String) node.e.accept(this, data);
String s = (String) node.s.accept(this, indent+1);
return indentString(indent) +
"while(" + e + "){\n"
+ s + indentString(indent) + "}\n"
;
}
public Object visit(Print node, Object data){
//
int indent = (int) data;
String e = (String) node.e.accept(this,indent);
//--indent;
return indentString(indent) + "System.out.println("+e+");\n";
}
public Object visit(Assign node, Object data){
//
int indent = (int) data;
String i = (String) node.i.accept(this,indent);
String e = (String) node.e.accept(this,indent);
//--indent;
return indentString(indent)+i+" = "+e+";\n";
}
public Object visit(ArrayAssign node, Object data){
int indent = (int) data;
Identifier i = node.i;
Exp e1 = node.e1;
Exp e2 = node.e2;
String id = (String) i.accept(this,indent);
String exp1 = (String) e1.accept(this,indent);
String exp2 = (String) e2.accept(this,indent);
data = indentString(indent) + id+"["+exp1+"] = "+exp2+";\n";
return data;
}
public Object visit(And node, Object data){
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String e2 = (String) node.e2.accept(this,indent);
return e1+" && "+e2;
}
public Object visit(LessThan node, Object data){
//
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String e2 = (String) node.e2.accept(this,indent);
return e1+" < "+e2;
}
public Object visit(Plus node, Object data){
//
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String e2 = (String) node.e2.accept(this,indent);
return e1+" + "+e2;
}
public Object visit(Minus node, Object data){
//
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String e2 = (String) node.e2.accept(this,indent);
return e1+" - "+e2;
}
public Object visit(Times node, Object data){
//
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String e2 = (String) node.e2.accept(this,indent);
return e1+" * "+e2;
}
public Object visit(ArrayLookup node, Object data){
String e1 = (String) node.e1.accept(this,data);
String e2 = (String) node.e2.accept(this,data);
return e1 + "[" + e2 + "]";
}
public Object visit(ArrayLength node, Object data){
String e = (String) node.e.accept(this,data);
return e + ".length";
}
public Object visit(Call node, Object data){
//
int indent = (int) data;
String e1 = (String) node.e1.accept(this,indent);
String i = (String) node.i.accept(this,indent);
if (node.e2 != null){
String e2 = (String) node.e2.accept(this,indent);
return e1 + "." + i +"("+e2+")";
}
return e1 + "." + i +"()";
}
public Object visit(IntegerLiteral node, Object data){
int indent = (int) data;
int i = node.i;
return " "+i+" ";
}
public Object visit(True node, Object data){
//
int indent = (int) data;
//--indent;
return "true";
}
public Object visit(False node, Object data){
//
int indent = (int) data;
//--indent;
return "false";
}
public Object visit(IdentifierExp node, Object data){
int indent = (int) data;
String s = node.s;
//--indent;
return " "+s+" ";
}
public Object visit(This node, Object data){
return "this";
}
public Object visit(NewArray node, Object data){
String e = (String) node.e.accept(this,data);
return "new int["+e+"]";
}
public Object visit(NewObject node, Object data){
String i = (String) node.i.accept(this,data);
return "new "+i+"()";
}
public Object visit(Not node, Object data){
String e = (String) node.e.accept(this,data);
return "! " + e;
}
public Object visit(Identifier node, Object data){
int indent = (int) data;
String s = node.s;
//--indent;
return s;
}
public Object visit(ExpGroup node, Object data){
String e="";
if (node.e!=null){
e = (String) node.e.accept(this,data);
}
return "("+e+")";
}
public Object visit(ClassDeclList node, Object data){
//
String c = (String) node.c.accept(this,data);
String clist = "";
if(node.clist != null){
clist = (String) node.clist.accept(this,data);
}
return c + "\n" + clist;
}
public Object visit(ExpList node, Object data){
//
int indent = (int) data;
String e = (String) node.e.accept(this,indent);
if (node.elist!=null){
String e1 = (String) node.elist.accept(this,indent);
e = e1 + ","+e;
}
return e;
}
public Object visit(FormalList node, Object data){
//
int indent = (int) data;
String f = (String) node.f.accept(this,indent);
if (node.flist!=null){
String f1 = (String) node.flist.accept(this,indent);
f = f1 +","+f;
}
//--indent;
return f;
}
public Object visit(MethodDeclList node, Object data){
//
int indent = (int) data;
String m = (String) node.m.accept(this,indent);
if (node.mlist!=null){
String m2 = (String) node.mlist.accept(this,indent);
m = m2 +"\n"+m;
}
return m;
}
public Object visit(StatementList node, Object data){
//
int indent = (int) data;
String result="";
if (node.slist!=null){
String s = (String) node.slist.accept(this,indent);
result = result+s;
}
String t = (String) node.s.accept(this,indent);
//--indent;
result = result+t;
return result;
}
public Object visit(VarDeclList node, Object data){
//
int indent = (int) data;
String v = (String) node.v.accept(this,indent);
if (node.vlist!=null){
String vlist = (String) node.vlist.accept(this,indent);
v = vlist + v;
}
return v;
}
}