-
Notifications
You must be signed in to change notification settings - Fork 30
/
ObAstEval.cpp
773 lines (733 loc) · 22.8 KB
/
ObAstEval.cpp
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* Copyright 2019, 2020 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Oberon parser/code model library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "ObAstEval.h"
#include "ObAst.h"
#include <QTextStream>
using namespace Ob;
using namespace Ob::Ast;
static inline QVariant expErr( QString* err, const QString& msg )
{
if( err )
*err = msg;
return QVariant();
}
static QVariant evalNamedConst(Ast::Expression* e, QString* err)
{
Ast::Named* n = e->getIdent();
QVariant res;
if( n && n->getTag() == Ast::Thing::T_Const )
res = Ast::thing_cast<Ast::Const*>(n)->d_val;
if( !res.isValid() )
return expErr( err, Ast::Model::tr("not a const expression") );
else
return res;
}
static bool setSet( Ast::Set& s, Ast::Expression* e, QString* err )
{
const QVariant v = Ast::Eval::evalConstExpr(e,err);
const qint64 b = v.toLongLong();
if( v.type() != QVariant::LongLong || b < 0 || b >= Ast::SET_BIT_LEN )
{
if( err )
*err = Ast::Model::tr("invalid set part");
return false;
}
s.set(b);
return true;
}
static bool setSet( Ast::Set& s, Ast::Expression* lhs, Ast::Expression* rhs, QString* err )
{
const QVariant lv = Ast::Eval::evalConstExpr(lhs,err);
const qint64 l = lv.toLongLong();
const QVariant rv = Ast::Eval::evalConstExpr(rhs,err);
const qint64 r = rv.toLongLong();
if( lv.type() != QVariant::LongLong || l < 0 || l >= Ast::SET_BIT_LEN ||
rv.type() != QVariant::LongLong || r < 0 || r >= Ast::SET_BIT_LEN )
{
if( err )
*err = Ast::Model::tr("invalid set part");
return false;
}
if( l <= r )
for( int b = l; b <= r; b++ )
s.set(b);
else
for( int b = r; b <= l; b++ )
s.set(b);
return true;
}
QVariant Ast::Eval::evalConstExpr(Ast::Expression* e, QString* err)
{
if( e == 0 )
return QVariant();
switch( e->getTag() )
{
case Thing::T_Literal:
return Ast::thing_cast<Literal*>(e)->d_val;
case Thing::T_SetExpr:
{
SetExpr* se = Ast::thing_cast<SetExpr*>(e);
Set s;
for(int i = 0; i < se->d_parts.size(); i++ )
{
if( se->d_parts[i]->getTag() == Thing::T_BinExpr )
{
BinExpr* be = Ast::thing_cast<BinExpr*>(se->d_parts[i].data());
if( be->d_op != BinExpr::Range )
return expErr( err, tr("invalid set part") );
if( !setSet( s, be->d_lhs.data(), be->d_rhs.data(), err) )
return QVariant();
}else if( !setSet( s, se->d_parts[i].data(), err) )
return QVariant();
}
return QVariant::fromValue(s);
}
break;
case Thing::T_IdentLeaf:
case Thing::T_IdentSel:
return evalNamedConst(e,err);
case Thing::T_UnExpr:
{
UnExpr* ue = Ast::thing_cast<UnExpr*>(e);
if( ue->d_op == UnExpr::SEL )
return evalNamedConst(e,err);
else
{
const QVariant v = evalConstExpr(ue->d_sub.data(),err);
if( !v.isValid() )
return v;
return Eval::unOp(ue->d_op, v,err);
}
}
break;
case Thing::T_BinExpr:
{
BinExpr* be = Ast::thing_cast<BinExpr*>(e);
switch( be->d_op )
{
case BinExpr::Range:
case BinExpr::Index:
case BinExpr::IS:
return expErr(err,tr("operator not supported for constants"));
}
const QVariant lhs = evalConstExpr(be->d_lhs.data(), err );
if( !lhs.isValid() )
return lhs;
const QVariant rhs = evalConstExpr(be->d_rhs.data(), err );
if( !rhs.isValid() )
return rhs;
return binOp( be->d_op, lhs, rhs, err );
}
break;
case Thing::T_CallExpr:
return expErr(err,tr("operation not supported in a const expression"));
default:
Q_ASSERT( false );
}
return QVariant();
}
QVariant Eval::NEG(const QVariant& v, QString* err)
{
if( v.type() == QVariant::Double )
return -v.toDouble();
if( v.type() == QVariant::LongLong )
return -v.toLongLong();
else
return expErr(err,tr("cannot invert sign of non numerical expression"));
}
QVariant Eval::NOT(const QVariant& v, QString* err)
{
if( v.type() == QVariant::Bool )
return !v.toBool();
return expErr(err,tr("cannot negate non boolean expression"));
}
QVariant Eval::ADD(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() + rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() + rhs.toDouble();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
return QVariant::fromValue( lhs.value<Set>() | rhs.value<Set>() );
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::SUB(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() - rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() - rhs.toDouble();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
{
const Set a = lhs.value<Set>();
const Set b = rhs.value<Set>();
Set res;
for( int j = 0; j < a.size(); j++ )
res.set( a.test(j) && !b.test(j) );
return QVariant::fromValue( res );
}else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::FDIV(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() / rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() / rhs.toDouble();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
{
const Set a = lhs.value<Set>();
const Set b = rhs.value<Set>();
Set res;
for( int j = 0; j < a.size(); j++ )
res.set( ( a.test(j) || b.test(j) ) && !( a.test(j) && b.test(j) ) );
return QVariant::fromValue( res );
}else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::MUL(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() * rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() * rhs.toDouble();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
return QVariant::fromValue( lhs.value<Set>() & rhs.value<Set>() );
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::DIV(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
{
const qint64 a = lhs.toLongLong();
const qint64 b = rhs.toLongLong();
// res = ( a - ( ( a % b + b ) % b ) ) / b;
// source: http://lists.inf.ethz.ch/pipermail/oberon/2019/013353.html
if (a < 0)
return qint64( (a - b + 1) / b );
else
return qint64( a / b );
}else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::MOD(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
{
const qint64 a = lhs.toLongLong();
const qint64 b = rhs.toLongLong();
// res = ( a % b + b ) % b;
// source: http://lists.inf.ethz.ch/pipermail/oberon/2019/013353.html
if (a < 0)
return qint64( (b - 1) + ((a - b + 1)) % b );
else
return qint64( a % b );
}else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::AND(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::Bool )
return lhs.toBool() && rhs.toBool();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::OR(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::Bool )
return lhs.toBool() || rhs.toBool();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::EQ(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() == rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() == rhs.toDouble();
else if( rhs.type() == QVariant::Bool )
return lhs.toBool() == rhs.toBool();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
return QVariant::fromValue( lhs.value<Set>() == rhs.value<Set>() );
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() == rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::NEQ(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() != rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() != rhs.toDouble();
else if( rhs.type() == QVariant::Bool )
return lhs.toBool() != rhs.toBool();
else if( lhs.canConvert<Set>() && rhs.canConvert<Set>() )
return QVariant::fromValue( lhs.value<Set>() != rhs.value<Set>() );
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() != rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::LE(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() < rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() < rhs.toDouble();
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() < rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::LEQ(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() <= rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() <= rhs.toDouble();
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() <= rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::GT(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() > rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() > rhs.toDouble();
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() > rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::GEQ(const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( rhs.type() == QVariant::LongLong )
return lhs.toLongLong() >= rhs.toLongLong();
else if( rhs.type() == QVariant::Double )
return lhs.toDouble() >= rhs.toDouble();
else if( lhs.type() == QVariant::ByteArray && rhs.type() == QVariant::ByteArray )
return lhs.toByteArray() >= rhs.toByteArray();
else
return expErr(err,tr("operand types incompatible with operator") );
}
QVariant Eval::IN(const QVariant& lhs, const QVariant& rhs, QString* err)
{
const qint64 b = lhs.toLongLong();
if( lhs.type() != QVariant::LongLong || !rhs.canConvert<Set>() || b < 0 || b >= SET_BIT_LEN )
return expErr(err,tr("invalid data type for operator IN") );
return rhs.value<Set>().test(b);
}
QVariant Eval::binOp(quint8 op, const QVariant& lhs, const QVariant& rhs, QString* err)
{
if( op == BinExpr::IN )
return IN(lhs,rhs,err);
if( rhs.type() != lhs.type() )
return expErr(err,tr("operands not of same type") );
switch( op )
{
case BinExpr::ADD:
return ADD(lhs,rhs,err);
case BinExpr::SUB:
return SUB(lhs,rhs,err);
case BinExpr::FDIV:
return FDIV(lhs,rhs,err);
case BinExpr::MUL:
return MUL(lhs,rhs,err);
case BinExpr::DIV:
return DIV(lhs,rhs,err);
case BinExpr::MOD:
return MOD(lhs,rhs,err);
case BinExpr::AND:
return AND(lhs,rhs,err);
case BinExpr::OR:
return OR(lhs,rhs,err);
case BinExpr::EQ:
return EQ(lhs,rhs,err);
case BinExpr::NEQ:
return NEQ(lhs,rhs,err);
case BinExpr::LT:
return LE(lhs,rhs,err);
case BinExpr::LEQ:
return LEQ(lhs,rhs,err);
case BinExpr::GT:
return GT(lhs,rhs,err);
case BinExpr::GEQ:
return GEQ(lhs,rhs,err);
default:
return expErr(err,tr("operator not supported"));
}
return QVariant();
}
QVariant Eval::unOp(quint8 op, const QVariant& v, QString* err)
{
switch( op )
{
case UnExpr::NEG:
return NEG(v,err);
case UnExpr::NOT:
return NOT(v,err);
default:
return expErr(err,tr("operator not supported"));
}
return QVariant();
}
struct Printer : public AstVisitor
{
bool namedType( Type* t )
{
if( t->d_ident && t->d_ident != curNamed )
{
out << "( TREF " << t->d_ident->d_name << " ) ";
return true;
}
return false;
}
void visit( BaseType* t)
{
if( namedType(t) )
return;
out << BaseType::s_typeName[t->d_type] << " ";
}
void visit( Pointer* t)
{
if( namedType(t) )
return;
out << "POINTER ";
if( t->d_to.isNull() )
out << "? ";
else
t->d_to->accept(this);
}
void visit( Array* t )
{
if( namedType(t) )
return;
out << "ARRAY " << t->d_len << " ";
if( t->d_type.isNull() )
out << "? ";
else
t->d_type->accept(this);
}
void visit( Record* t )
{
if( namedType(t) )
return;
out << "RECORD ";
if( !t->d_base.isNull() )
{
out << "( TREF ";
t->d_base->accept(this);
out << " )";
}
d_level++;
for( int i = 0; i < t->d_fields.size(); i++ )
{
out << endl;
out << ws() << t->d_fields[i]->d_name << " ";
if( t->d_fields[i].isNull() )
out << "? ";
else
t->d_fields[i]->d_type->accept(this);
}
d_level--;
}
void visit( ProcType* t )
{
if( namedType(t) )
return;
out << "PROC ";
if( !t->d_formals.isEmpty() )
out << "( ";
for( int i = 0; i < t->d_formals.size(); i++ )
{
out << t->d_formals[i]->d_name << " ";
// formals also appear as part of scope names
}
if( !t->d_formals.isEmpty() )
out << ")";
}
void visit( QualiType* t )
{
if( namedType(t) )
return;
t->d_quali->accept(this);
}
void visit( Field* n)
{
out << ws() << n->d_name << " ";
if( n->d_type.isNull() )
out << "? ";
else
n->d_type->accept(this);
out << endl;
}
void renderLive( Named* r )
{
out << " ";
if( r->d_liveFrom )
out << "live " << r->d_liveFrom << "-" << r->d_liveTo << " ";
if( r->d_usedFromSubs )
out << "subs ";
if( r->d_slotValid )
out << "slot " << r->d_slot;
}
void renderVar( Named* r )
{
out << r->d_name << " ";
if( r->d_type.isNull() )
out << "?";
else
r->d_type->accept(this);
renderLive(r);
out << endl;
}
void visit( Variable* n )
{
out << ws() << "V ";
renderVar( n );
}
void visit( LocalVar* n )
{
out << ws() << "V ";
renderVar( n );
}
void visit( Parameter* n )
{
out << ws() << "P ";
renderVar( n );
}
void visit( NamedType* n )
{
curNamed = n;
out << ws() << "T " << n->d_name << " ";
if( n->d_type.isNull() )
out << "? ";
else
n->d_type->accept(this);
out << endl;
curNamed = 0;
}
void visit( Const* n )
{
out << ws() << "C " << n->d_name << " ";
if( n->d_type.isNull() )
out << "? ";
else
n->d_type->accept(this);
out << "'" << n->d_val.toByteArray().simplified() << "'";
out << " " << endl;
}
void visit( Import* n)
{
out << ws() << "I " << n->d_name << " ";
out << n->d_mod->d_name;
out << endl;
}
void visit( Procedure* m )
{
out << ws() << "PROCEDURE " << m->d_name << " ";
if( m->d_type.isNull() )
out << "? ";
else
m->d_type->accept(this);
renderLive(m);
out << endl;
d_level++;
for( int i = 0; i < m->d_order.size(); i++ )
m->d_order[i]->accept(this);
if( !m->d_body.isEmpty() )
{
out << ws() << "BEGIN" << endl;
d_level++;
for( int i = 0; i < m->d_body.size(); i++ )
m->d_body[i]->accept(this);
d_level--;
}
d_level--;
}
void visit( BuiltIn* ) {}
void visit( Module* m )
{
out << ws() << ( m->d_isDef ? "DEFINITION " : "MODULE " ) << m->d_name << endl;
d_level++;
for( int i = 0; i < m->d_order.size(); i++ )
m->d_order[i]->accept(this);
if( !m->d_body.isEmpty() )
{
out << ws() << "BEGIN" << endl;
d_level++;
for( int i = 0; i < m->d_body.size(); i++ )
m->d_body[i]->accept(this);
d_level--;
}
d_level--;
}
void visit( Call* s)
{
out << ws();
s->d_what->accept(this);
out << endl;
}
void visit( Return* s)
{
out << ws() << "RETURN ";
s->d_what->accept(this);
out << endl;
}
void visit( Assign* s )
{
out << ws() << "ASSIG ";
s->d_lhs->accept(this);
out << ":= ";
s->d_rhs->accept(this);
out << endl;
}
void visit( IfLoop* s)
{
Q_ASSERT( s->d_if.size() == s->d_then.size() );
for( int i = 0; i < s->d_if.size(); i++ )
{
out << ws();
if( i == 0 )
out << ( s->d_op == IfLoop::IF ? "IF " : ( s->d_op == IfLoop::WHILE ? "WHILE " : "REPEAT " ) );
else
out << "ELSIF ";
s->d_if[i]->accept(this);
out << "THEN " << endl;
d_level++;
const StatSeq& body = s->d_then[i];
for( int j = 0; j < body.size(); j++ )
body[j]->accept(this);
d_level--;
}
if( !s->d_else.isEmpty() )
{
out << ws() << "ELSE" << endl;
d_level++;
const StatSeq& body = s->d_else;
for( int j = 0; j < body.size(); j++ )
body[j]->accept(this);
d_level--;
}
}
void visit( ForLoop* s )
{
out << ws() << "FOR " << s->d_id->getIdent()->d_name << " := ";
s->d_from->accept(this);
out << "TO ";
s->d_to->accept(this);
out << "BY ";
s->d_by->accept(this);
out << "DO " << endl;
d_level++;
const StatSeq& body = s->d_do;
for( int j = 0; j < body.size(); j++ )
body[j]->accept(this);
d_level--;
}
void visit( CaseStmt* s )
{
out << ws() << "SWITCH ";
s->d_exp->accept(this);
out << endl;
d_level++;
for( int i = 0; i < s->d_cases.size(); i++ )
{
out << ws() << "CASE ";
for( int j = 0; j < s->d_cases[i].d_labels.size(); j++ )
{
if( j != 0 )
out << "| ";
s->d_cases[i].d_labels[j]->accept(this);
}
out << endl;
d_level++;
const StatSeq& body = s->d_cases[i].d_block;
for( int j = 0; j < body.size(); j++ )
body[j]->accept(this);
d_level--;
}
d_level--;
}
void visit( Literal* e )
{
out << "'" << e->d_val.toByteArray().simplified() << "' ";
}
void visit( SetExpr* e )
{
out << "( SET ";
for( int i = 0; i < e->d_parts.size(); i++ )
e->d_parts[i]->accept(this);
out << ") ";
}
void visit( IdentLeaf* e )
{
out << e->d_ident->d_name << " ";
}
void visit( UnExpr* e)
{
out << "( " << UnExpr::s_opName[e->d_op] << " ";
e->d_sub->accept(this);
if( e->d_op == UnExpr::CAST && !e->d_type.isNull() )
e->d_type->accept(this);
out << ") ";
}
void visit( IdentSel* e)
{
out << "( . ";
e->d_sub->accept(this);
out << e->d_ident->d_name << " ) ";
}
void visit( CallExpr* e)
{
out << "( CALL ";
e->d_sub->accept(this);
for( int i = 0; i < e->d_actuals.size(); i++ )
e->d_actuals[i]->accept(this);
out << ") ";
}
void visit( BinExpr* e )
{
out << "( " << BinExpr::s_opName[e->d_op] << " ";
e->d_lhs->accept(this);
e->d_rhs->accept(this);
out << ") ";
}
QByteArray ws() const
{
QByteArray ws;
for( int i = 0; i < d_level; i++ )
ws += "| ";
return ws;
}
QTextStream& out;
Named* curNamed;
int d_level;
Printer(QTextStream& o):out(o),d_level(0),curNamed(0) {}
};
void Eval::render(QTextStream& out, Thing* m)
{
Printer p(out);
m->accept(&p);
}