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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/**
** \file ast/pretty-printer.cc
** \brief Implementation of ast::PrettyPrinter.
*/
#include <ast/all.hh>
#include <ast/libast.hh>
#include <ast/pretty-printer.hh>
#include <misc/escape.hh>
#include <misc/indent.hh>
#include <misc/separator.hh>
#include <type/class.hh>
namespace ast
{
// Anonymous namespace: these functions are private to this file.
namespace
{
/// Output \a e on \a ostr.
inline std::ostream& operator<<(std::ostream& ostr, const Escapable& e)
{
if (escapes_display(ostr)
// FIXME DONE: Some code was deleted here.
&& e.escape_get())
ostr << "/* escaping */ ";
return ostr;
}
/// \brief Output \a e on \a ostr.
///
/// Used to factor the output of the name declared,
/// and its possible additional attributes.
inline std::ostream& operator<<(std::ostream& ostr, const Dec& e)
{
ostr << e.name_get();
if (bindings_display(ostr))
ostr << " /* " << &e << " */";
return ostr;
}
} // namespace
PrettyPrinter::PrettyPrinter(std::ostream& ostr)
: ostr_(ostr)
{}
void PrettyPrinter::operator()(const SimpleVar& e)
{
ostr_ << e.name_get();
if (bindings_display(ostr_))
ostr_ << " /* " << e.def_get() << " */";
}
void PrettyPrinter::operator()(const FieldVar& e)
{
// FIXME DONE: Some code was deleted here.
ostr_ << e.var_get() << "." << e.name_get();
}
/* Foo[10]. */
void PrettyPrinter::operator()(const SubscriptVar& e)
{
ostr_ << e.var_get() << '[' << misc::incindent << e.index_get()
<< misc::decindent << ']';
}
void PrettyPrinter::operator()(const CastExp& e)
{
ostr_ << "_cast(" << e.exp_get() << ", " << e.ty_get() << ')';
}
// FIXME DONE: Some code was deleted here.
void PrettyPrinter::operator()(const NilExp&) { ostr_ << "nil"; }
void PrettyPrinter::operator()(const IntExp& e) { ostr_ << e.value_get(); }
void PrettyPrinter::operator()(const StringExp& e)
{
ostr_ << "\"" << misc::escape(e.value_get()) << "\"";
}
void PrettyPrinter::operator()(const ObjectExp& e)
{
ostr_ << "new " << e.type_name_get();
if (bindings_display(ostr_))
ostr_ << " /* " << &e.type_name_get() << " */";
}
void PrettyPrinter::operator()(const CallExp& e)
{
ostr_ << e.name_get();
if (bindings_display(ostr_))
ostr_ << " /* " << e.def_get() << " */";
ostr_ << "(";
if (!e.args_get().empty())
{
ostr_ << misc::separate(e.args_get(), ", ");
}
ostr_ << ")";
}
void PrettyPrinter::operator()(const MethodCallExp& e)
{
ostr_ << e.object_get() << "." << e.name_get();
if (bindings_display(ostr_))
ostr_ << " /* " << e.def_get() << " */";
ostr_ << "(";
if (!e.args_get().empty())
{
ostr_ << misc::separate(e.args_get(), ", ");
}
ostr_ << ")";
}
void PrettyPrinter::operator()(const OpExp& e)
{
ostr_ << e.left_get() << " " << str(e.oper_get()) << " " << e.right_get();
}
void PrettyPrinter::operator()(const RecordExp& e)
{
ostr_ << e.type_name_get();
ostr_ << " { ";
if (!e.fields_get().empty())
{
ostr_ << misc::separate(e.fields_get(), ", ");
}
ostr_ << " }";
}
void PrettyPrinter::operator()(const RecordTy& e)
{
ostr_ << "{ ";
if (!e.fields_get().empty())
{
ostr_ << misc::separate(e.fields_get(), ", ");
}
ostr_ << " }";
}
void PrettyPrinter::operator()(const ArrayTy& e)
{
ostr_ << "array of " << e.base_type_get();
}
void PrettyPrinter::operator()(const ClassTy& e)
{
ostr_ << "class extends " << e.super_get() << "{" << misc::decendl;
if (!e.chunks_get().chunks_get().empty())
{
ostr_ << misc::separate(e.chunks_get(), "\n");
}
ostr_ << " }" << misc::decendl;
}
void PrettyPrinter::operator()(const Field& e)
{
ostr_ << e.name_get() << " : " << e.type_name_get();
}
void PrettyPrinter::operator()(const SeqExp& e)
{
if (e.exps_get().size() == 0)
ostr_ << "()";
else if (e.exps_get().size() == 1)
ostr_ << *e.exps_get().at(0);
else if (e.exps_get().size() > 1)
{
ostr_ << '(' << misc::incendl << *e.exps_get().at(0);
for (size_t i = 1; i < e.exps_get().size(); ++i)
{
ostr_ << ";" << misc::iendl << *e.exps_get().at(i);
}
ostr_ << misc::decendl << ")";
}
}
void PrettyPrinter::operator()(const AssignExp& e)
{
ostr_ << e.var_get() << " := " << e.exp_get();
}
void PrettyPrinter::operator()(const IfExp& e)
{
ostr_ << "(if " << e.test_get() << " then" << misc::incendl
<< e.thenclause_get() << misc::decendl;
ostr_ << "else" << misc::incendl << e.elseclause_get() << ")"
<< misc::decendl;
}
void PrettyPrinter::operator()(const WhileExp& e)
{
ostr_ << "(while ";
if (bindings_display(ostr_))
{
ostr_ << "/* " << &e << " /* ";
}
ostr_ << e.test_get() << " do" << misc::incendl << e.body_get() << ")"
<< misc::decendl;
}
void PrettyPrinter::operator()(const ForExp& e)
{
ostr_ << "(for ";
if (bindings_display(ostr_))
{
ostr_ << "/* " << &e << " */ ";
}
ostr_ << e.vardec_get().name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &e.vardec_get() << " */";
}
ostr_ << " := " << *e.vardec_get().init_get() << " to " << e.hi_get()
<< " do" << misc::incendl << e.body_get() << ")";
}
void PrettyPrinter::operator()(const BreakExp& e)
{
ostr_ << "break";
if (bindings_display(ostr_))
{
ostr_ << " /* " << e.def_get() << " */";
}
}
void PrettyPrinter::operator()(const LetExp& e)
{
ostr_ << "let" << misc::incendl << e.chunks_get() << misc::decendl << "in"
<< misc::incendl << e.body_get() << misc::decendl << "end";
}
void PrettyPrinter::operator()(const ArrayExp& e)
{
ostr_ << e.type_name_get() << "[" << e.size_get() << "] of "
<< e.init_get();
}
void PrettyPrinter::operator()(const FieldInit& e)
{
ostr_ << e.name_get() << " = " << e.init_get();
}
void PrettyPrinter::operator()(const VarChunk& e)
{
for (auto dec : e.decs_get())
{
dec->accept(*this);
ostr_ << misc::iendl;
}
}
void PrettyPrinter::operator()(const VarDec& e)
{
ostr_ << "var " << static_cast<Escapable>(e) << e.name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &e << " */";
}
if (e.type_name_get() != nullptr)
ostr_ << " : " << *e.type_name_get();
if (e.init_get() != nullptr)
ostr_ << " := " << *e.init_get();
else
ostr_ << " := 0";
}
void PrettyPrinter::operator()(const TypeChunk& e)
{
for (auto dec : e.decs_get())
{
dec->accept(*this);
ostr_ << misc::iendl;
}
}
void PrettyPrinter::operator()(const TypeDec& e)
{
if (const auto c = dynamic_cast<const ClassTy*>(&e.ty_get()); c)
{
ostr_ << "class " << e.name_get();
if (bindings_display(ostr_))
ostr_ << " /* " << &e << " */";
ostr_ << " extends " << c->super_get()
<< misc::iendl << "{" << misc::incendl;
if (!c->chunks_get().chunks_get().empty())
{
ostr_ << misc::separate(c->chunks_get(), "\n");
}
ostr_ << misc::decendl << "}";
}
else
{
ostr_ << "type " << e.name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &e << " */";
}
ostr_ << " = " << e.ty_get();
}
}
void PrettyPrinter::operator()(const NameTy& e)
{
ostr_ << e.name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << e.def_get() << " */";
}
}
void PrettyPrinter::operator()(const FunctionChunk& e)
{
for (auto dec : e.decs_get())
{
dec->accept(*this);
ostr_ << misc::iendl;
}
}
void PrettyPrinter::operator()(const FunctionDec& e)
{
if (e.body_get() == nullptr)
ostr_ << "primitive ";
else
ostr_ << "function ";
ostr_ << e.name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &e << " */";
}
ostr_ << "(";
auto decs = e.formals_get().decs_get();
if (!decs.empty())
{
ostr_ << static_cast<Escapable>(*decs.at(0)) << decs.at(0)->name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &decs.at(0) << " */";
}
ostr_ << " : " << *decs.at(0)->type_name_get();
for (size_t i = 1; i < decs.size(); ++i)
{
ostr_ << ", " << static_cast<Escapable>(*decs.at(i))
<< decs.at(i)->name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &decs.at(i) << " */";
}
ostr_ << " : " << *decs.at(i)->type_name_get();
}
}
ostr_ << ")";
if (e.result_get() != nullptr)
{
ostr_ << " : " << *e.result_get();
}
if (e.body_get() != nullptr)
{
ostr_ << " =" << misc::incendl << *e.body_get() << misc::decindent;
}
ostr_ << misc::iendl;
}
void PrettyPrinter::operator()(const MethodChunk& e)
{
for (auto dec : e.decs_get())
{
dec->accept(*this);
ostr_ << misc::iendl;
}
}
void PrettyPrinter::operator()(const MethodDec& e)
{
ostr_ << "method " << e.name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &e << " */";
}
ostr_ << "(";
auto decs = e.formals_get().decs_get();
if (!decs.empty())
{
ostr_ << static_cast<Escapable>(*decs.at(0)) << decs.at(0)->name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &decs.at(0) << " */";
}
ostr_ << " : " << *decs.at(0)->type_name_get();
for (size_t i = 1; i < decs.size(); ++i)
{
ostr_ << ", " << static_cast<Escapable>(*decs.at(i))
<< decs.at(i)->name_get();
if (bindings_display(ostr_))
{
ostr_ << " /* " << &decs.at(i) << " */";
}
ostr_ << " : " << *decs.at(i)->type_name_get();
}
}
ostr_ << ")";
if (e.result_get() != nullptr)
{
ostr_ << " : " << *e.result_get();
}
if (e.body_get() != nullptr)
{
ostr_ << " =" << misc::incendl << *e.body_get() << misc::decindent;
}
ostr_ << misc::iendl;
}
void PrettyPrinter::operator()(const AssertExp& e)
{
ostr_ << "assert " << e.cond_get();
}
} // namespace ast
|