-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool-tree.cc
661 lines (492 loc) · 12.2 KB
/
cool-tree.cc
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//////////////////////////////////////////////////////////
//
// file: cool-tree.cc
//
// This file defines the functions of each class
//
//////////////////////////////////////////////////////////
#include "tree.h"
#include "cool-tree.handcode.h"
#include "cool-tree.h"
// constructors' functions
Program program_class::copy_Program()
{
return new program_class(classes->copy_list());
}
void program_class::dump(ostream& stream, int n)
{
stream << pad(n) << "program\n";
classes->dump(stream, n+2);
}
Class_ class__class::copy_Class_()
{
return new class__class(copy_Symbol(name), copy_Symbol(parent), features->copy_list(), copy_Symbol(filename));
}
void class__class::dump(ostream& stream, int n)
{
stream << pad(n) << "class_\n";
dump_Symbol(stream, n+2, name);
dump_Symbol(stream, n+2, parent);
features->dump(stream, n+2);
dump_Symbol(stream, n+2, filename);
}
Feature method_class::copy_Feature()
{
return new method_class(copy_Symbol(name), formals->copy_list(), copy_Symbol(return_type), expr->copy_Expression());
}
void method_class::dump(ostream& stream, int n)
{
stream << pad(n) << "method\n";
dump_Symbol(stream, n+2, name);
formals->dump(stream, n+2);
dump_Symbol(stream, n+2, return_type);
expr->dump(stream, n+2);
}
Feature attr_class::copy_Feature()
{
return new attr_class(copy_Symbol(name), copy_Symbol(type_decl), init->copy_Expression());
}
void attr_class::dump(ostream& stream, int n)
{
stream << pad(n) << "attr\n";
dump_Symbol(stream, n+2, name);
dump_Symbol(stream, n+2, type_decl);
init->dump(stream, n+2);
}
Formal formal_class::copy_Formal()
{
return new formal_class(copy_Symbol(name), copy_Symbol(type_decl));
}
void formal_class::dump(ostream& stream, int n)
{
stream << pad(n) << "formal\n";
dump_Symbol(stream, n+2, name);
dump_Symbol(stream, n+2, type_decl);
}
Case branch_class::copy_Case()
{
return new branch_class(copy_Symbol(name), copy_Symbol(type_decl), expr->copy_Expression());
}
void branch_class::dump(ostream& stream, int n)
{
stream << pad(n) << "branch\n";
dump_Symbol(stream, n+2, name);
dump_Symbol(stream, n+2, type_decl);
expr->dump(stream, n+2);
}
Expression assign_class::copy_Expression()
{
return new assign_class(copy_Symbol(name), expr->copy_Expression());
}
void assign_class::dump(ostream& stream, int n)
{
stream << pad(n) << "assign\n";
dump_Symbol(stream, n+2, name);
expr->dump(stream, n+2);
}
Expression static_dispatch_class::copy_Expression()
{
return new static_dispatch_class(expr->copy_Expression(), copy_Symbol(type_name), copy_Symbol(name), actual->copy_list());
}
void static_dispatch_class::dump(ostream& stream, int n)
{
stream << pad(n) << "static_dispatch\n";
expr->dump(stream, n+2);
dump_Symbol(stream, n+2, type_name);
dump_Symbol(stream, n+2, name);
actual->dump(stream, n+2);
}
Expression dispatch_class::copy_Expression()
{
return new dispatch_class(expr->copy_Expression(), copy_Symbol(name), actual->copy_list());
}
void dispatch_class::dump(ostream& stream, int n)
{
stream << pad(n) << "dispatch\n";
expr->dump(stream, n+2);
dump_Symbol(stream, n+2, name);
actual->dump(stream, n+2);
}
Expression cond_class::copy_Expression()
{
return new cond_class(pred->copy_Expression(), then_exp->copy_Expression(), else_exp->copy_Expression());
}
void cond_class::dump(ostream& stream, int n)
{
stream << pad(n) << "cond\n";
pred->dump(stream, n+2);
then_exp->dump(stream, n+2);
else_exp->dump(stream, n+2);
}
Expression loop_class::copy_Expression()
{
return new loop_class(pred->copy_Expression(), body->copy_Expression());
}
void loop_class::dump(ostream& stream, int n)
{
stream << pad(n) << "loop\n";
pred->dump(stream, n+2);
body->dump(stream, n+2);
}
Expression typcase_class::copy_Expression()
{
return new typcase_class(expr->copy_Expression(), cases->copy_list());
}
void typcase_class::dump(ostream& stream, int n)
{
stream << pad(n) << "typcase\n";
expr->dump(stream, n+2);
cases->dump(stream, n+2);
}
Expression block_class::copy_Expression()
{
return new block_class(body->copy_list());
}
void block_class::dump(ostream& stream, int n)
{
stream << pad(n) << "block\n";
body->dump(stream, n+2);
}
Expression let_class::copy_Expression()
{
return new let_class(copy_Symbol(identifier), copy_Symbol(type_decl), init->copy_Expression(), body->copy_Expression());
}
void let_class::dump(ostream& stream, int n)
{
stream << pad(n) << "let\n";
dump_Symbol(stream, n+2, identifier);
dump_Symbol(stream, n+2, type_decl);
init->dump(stream, n+2);
body->dump(stream, n+2);
}
Expression plus_class::copy_Expression()
{
return new plus_class(e1->copy_Expression(), e2->copy_Expression());
}
void plus_class::dump(ostream& stream, int n)
{
stream << pad(n) << "plus\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression sub_class::copy_Expression()
{
return new sub_class(e1->copy_Expression(), e2->copy_Expression());
}
void sub_class::dump(ostream& stream, int n)
{
stream << pad(n) << "sub\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression mul_class::copy_Expression()
{
return new mul_class(e1->copy_Expression(), e2->copy_Expression());
}
void mul_class::dump(ostream& stream, int n)
{
stream << pad(n) << "mul\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression divide_class::copy_Expression()
{
return new divide_class(e1->copy_Expression(), e2->copy_Expression());
}
void divide_class::dump(ostream& stream, int n)
{
stream << pad(n) << "divide\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression neg_class::copy_Expression()
{
return new neg_class(e1->copy_Expression());
}
void neg_class::dump(ostream& stream, int n)
{
stream << pad(n) << "neg\n";
e1->dump(stream, n+2);
}
Expression lt_class::copy_Expression()
{
return new lt_class(e1->copy_Expression(), e2->copy_Expression());
}
void lt_class::dump(ostream& stream, int n)
{
stream << pad(n) << "lt\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression eq_class::copy_Expression()
{
return new eq_class(e1->copy_Expression(), e2->copy_Expression());
}
void eq_class::dump(ostream& stream, int n)
{
stream << pad(n) << "eq\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression leq_class::copy_Expression()
{
return new leq_class(e1->copy_Expression(), e2->copy_Expression());
}
void leq_class::dump(ostream& stream, int n)
{
stream << pad(n) << "leq\n";
e1->dump(stream, n+2);
e2->dump(stream, n+2);
}
Expression comp_class::copy_Expression()
{
return new comp_class(e1->copy_Expression());
}
void comp_class::dump(ostream& stream, int n)
{
stream << pad(n) << "comp\n";
e1->dump(stream, n+2);
}
Expression int_const_class::copy_Expression()
{
return new int_const_class(copy_Symbol(token));
}
void int_const_class::dump(ostream& stream, int n)
{
stream << pad(n) << "int_const\n";
dump_Symbol(stream, n+2, token);
}
Expression bool_const_class::copy_Expression()
{
return new bool_const_class(copy_Boolean(val));
}
void bool_const_class::dump(ostream& stream, int n)
{
stream << pad(n) << "bool_const\n";
dump_Boolean(stream, n+2, val);
}
Expression string_const_class::copy_Expression()
{
return new string_const_class(copy_Symbol(token));
}
void string_const_class::dump(ostream& stream, int n)
{
stream << pad(n) << "string_const\n";
dump_Symbol(stream, n+2, token);
}
Expression new__class::copy_Expression()
{
return new new__class(copy_Symbol(type_name));
}
void new__class::dump(ostream& stream, int n)
{
stream << pad(n) << "new_\n";
dump_Symbol(stream, n+2, type_name);
}
Expression isvoid_class::copy_Expression()
{
return new isvoid_class(e1->copy_Expression());
}
void isvoid_class::dump(ostream& stream, int n)
{
stream << pad(n) << "isvoid\n";
e1->dump(stream, n+2);
}
Expression no_expr_class::copy_Expression()
{
return new no_expr_class();
}
void no_expr_class::dump(ostream& stream, int n)
{
stream << pad(n) << "no_expr\n";
}
Expression object_class::copy_Expression()
{
return new object_class(copy_Symbol(name));
}
void object_class::dump(ostream& stream, int n)
{
stream << pad(n) << "object\n";
dump_Symbol(stream, n+2, name);
}
// interfaces used by Bison
Classes nil_Classes()
{
return new nil_node<Class_>();
}
Classes single_Classes(Class_ e)
{
return new single_list_node<Class_>(e);
}
Classes append_Classes(Classes p1, Classes p2)
{
return new append_node<Class_>(p1, p2);
}
Features nil_Features()
{
return new nil_node<Feature>();
}
Features single_Features(Feature e)
{
return new single_list_node<Feature>(e);
}
Features append_Features(Features p1, Features p2)
{
return new append_node<Feature>(p1, p2);
}
Formals nil_Formals()
{
return new nil_node<Formal>();
}
Formals single_Formals(Formal e)
{
return new single_list_node<Formal>(e);
}
Formals append_Formals(Formals p1, Formals p2)
{
return new append_node<Formal>(p1, p2);
}
Expressions nil_Expressions()
{
return new nil_node<Expression>();
}
Expressions single_Expressions(Expression e)
{
return new single_list_node<Expression>(e);
}
Expressions append_Expressions(Expressions p1, Expressions p2)
{
return new append_node<Expression>(p1, p2);
}
Cases nil_Cases()
{
return new nil_node<Case>();
}
Cases single_Cases(Case e)
{
return new single_list_node<Case>(e);
}
Cases append_Cases(Cases p1, Cases p2)
{
return new append_node<Case>(p1, p2);
}
Program program(Classes classes)
{
return new program_class(classes);
}
Class_ class_(Symbol name, Symbol parent, Features features, Symbol filename)
{
return new class__class(name, parent, features, filename);
}
Feature method(Symbol name, Formals formals, Symbol return_type, Expression expr)
{
return new method_class(name, formals, return_type, expr);
}
Feature attr(Symbol name, Symbol type_decl, Expression init)
{
return new attr_class(name, type_decl, init);
}
Formal formal(Symbol name, Symbol type_decl)
{
return new formal_class(name, type_decl);
}
Case branch(Symbol name, Symbol type_decl, Expression expr)
{
return new branch_class(name, type_decl, expr);
}
Expression assign(Symbol name, Expression expr)
{
return new assign_class(name, expr);
}
Expression static_dispatch(Expression expr, Symbol type_name, Symbol name, Expressions actual)
{
return new static_dispatch_class(expr, type_name, name, actual);
}
Expression dispatch(Expression expr, Symbol name, Expressions actual)
{
return new dispatch_class(expr, name, actual);
}
Expression cond(Expression pred, Expression then_exp, Expression else_exp)
{
return new cond_class(pred, then_exp, else_exp);
}
Expression loop(Expression pred, Expression body)
{
return new loop_class(pred, body);
}
Expression typcase(Expression expr, Cases cases)
{
return new typcase_class(expr, cases);
}
Expression block(Expressions body)
{
return new block_class(body);
}
Expression let(Symbol identifier, Symbol type_decl, Expression init, Expression body)
{
return new let_class(identifier, type_decl, init, body);
}
Expression plus(Expression e1, Expression e2)
{
return new plus_class(e1, e2);
}
Expression sub(Expression e1, Expression e2)
{
return new sub_class(e1, e2);
}
Expression mul(Expression e1, Expression e2)
{
return new mul_class(e1, e2);
}
Expression divide(Expression e1, Expression e2)
{
return new divide_class(e1, e2);
}
Expression neg(Expression e1)
{
return new neg_class(e1);
}
Expression lt(Expression e1, Expression e2)
{
return new lt_class(e1, e2);
}
Expression eq(Expression e1, Expression e2)
{
return new eq_class(e1, e2);
}
Expression leq(Expression e1, Expression e2)
{
return new leq_class(e1, e2);
}
Expression comp(Expression e1)
{
return new comp_class(e1);
}
Expression int_const(Symbol token)
{
return new int_const_class(token);
}
Expression bool_const(Boolean val)
{
return new bool_const_class(val);
}
Expression string_const(Symbol token)
{
return new string_const_class(token);
}
Expression new_(Symbol type_name)
{
return new new__class(type_name);
}
Expression isvoid(Expression e1)
{
return new isvoid_class(e1);
}
Expression no_expr()
{
return new no_expr_class();
}
Expression object(Symbol name)
{
return new object_class(name);
}