1
|
/**
|
2
|
*
|
3
|
* OOAS Compiler (Deprecated)
|
4
|
*
|
5
|
* Copyright 2015, Institute for Software Technology, Graz University of
|
6
|
* Technology. Portions are copyright 2015 by the AIT Austrian Institute
|
7
|
* of Technology. All rights reserved.
|
8
|
*
|
9
|
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
10
|
*
|
11
|
* Please notice that this version of the OOAS compiler is considered de-
|
12
|
* precated. Only the Java version is maintained.
|
13
|
*
|
14
|
* Contributors:
|
15
|
* Willibald Krenn (TU Graz/AIT)
|
16
|
* Stefan Tiran (TU Graz/AIT)
|
17
|
*/
|
18
|
|
19
|
|
20
|
/*
|
21
|
* ooaPrintVisitor
|
22
|
*
|
23
|
* Prints AST in some sort of OOA - Syntax.
|
24
|
*/
|
25
|
|
26
|
using System;
|
27
|
using System.Collections.Generic;
|
28
|
using System.Text;
|
29
|
using TUG.Mogentes.Codegen;
|
30
|
|
31
|
namespace TUG.Mogentes
|
32
|
{
|
33
|
public delegate void Action();
|
34
|
|
35
|
public sealed class OoaPrintVisitor : OoaCompleteAstTraversalVisitor
|
36
|
{
|
37
|
private bool writeTypes;
|
38
|
private Identifier currentTypeDef;
|
39
|
private OoaCodeEmitter output;
|
40
|
|
41
|
|
42
|
|
43
|
private void PrintSubElementOrNull(IAst anElem)
|
44
|
{
|
45
|
if (anElem == null)
|
46
|
output.Append("<null>");
|
47
|
else
|
48
|
anElem.Accept(this);
|
49
|
}
|
50
|
|
51
|
private void PrintEnumeration(System.Collections.IEnumerable arg)
|
52
|
{
|
53
|
int i = 0;
|
54
|
foreach (IAst sym in arg)
|
55
|
{
|
56
|
if (i != 0)
|
57
|
output.Append(", ");
|
58
|
i++;
|
59
|
PrintSubElementOrNull(sym);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/*print the type identifier, or the type definition by calling the anAction delegate.*/
|
64
|
private void PrintType(UlyssesType atype, Action anAction)
|
65
|
{
|
66
|
if ( (atype.identifier != null)
|
67
|
&& (!atype.isAnonymousType)
|
68
|
&& (!ReferenceEquals(currentTypeDef, atype.identifier) || !writeTypes))
|
69
|
{
|
70
|
bool write = writeTypes;
|
71
|
writeTypes = false;
|
72
|
atype.identifier.Accept(this);
|
73
|
writeTypes = write;
|
74
|
}
|
75
|
else
|
76
|
anAction();
|
77
|
|
78
|
}
|
79
|
|
80
|
|
81
|
|
82
|
public override void visit(MainModule mainModule)
|
83
|
{
|
84
|
int i = 0;
|
85
|
bool haveInstance = mainModule.instance != null;
|
86
|
/*print all types*/
|
87
|
writeTypes = true;
|
88
|
output.IncIndent();
|
89
|
output.AppendLine("types");
|
90
|
foreach (var type in mainModule.symbolTable.symbolList)
|
91
|
{
|
92
|
if (type.kind != IdentifierKind.TypeIdentifier)
|
93
|
continue;
|
94
|
|
95
|
if (i != 0)
|
96
|
output.AppendLine(";");
|
97
|
i++;
|
98
|
if (!haveInstance || ((TypeIdentifier)type).type.kind != TypeKind.OoActionSystemType)
|
99
|
type.Accept(this);
|
100
|
}
|
101
|
|
102
|
if (haveInstance)
|
103
|
mainModule.instance.Accept(this);
|
104
|
|
105
|
output.DecIndent();
|
106
|
output.AppendLine("");
|
107
|
output.IncIndent();
|
108
|
output.IncIndent();
|
109
|
output.AppendLine("system");
|
110
|
writeTypes = false;
|
111
|
if (!haveInstance)
|
112
|
mainModule.systemDescription.Accept(this);
|
113
|
else
|
114
|
output.Append(mainModule.instance.identifier.tokenText);
|
115
|
}
|
116
|
|
117
|
public override void visit(TypeIdentifier typeIdentifier)
|
118
|
{
|
119
|
output.Append(typeIdentifier.tokenText);
|
120
|
if (writeTypes)
|
121
|
{
|
122
|
currentTypeDef = typeIdentifier;
|
123
|
output.Append(" = ");
|
124
|
PrintSubElementOrNull(typeIdentifier.type);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
/*write out type definitions*/
|
129
|
public override void visit(BoolType boolType)
|
130
|
{
|
131
|
output.Append("bool");
|
132
|
}
|
133
|
|
134
|
public override void visit(CharType charType)
|
135
|
{
|
136
|
output.Append("char");
|
137
|
}
|
138
|
|
139
|
public override void visit(IntType intType)
|
140
|
{
|
141
|
PrintType(intType, () =>
|
142
|
{
|
143
|
output.Append("int [");
|
144
|
output.Append(intType.rangeLow.ToString());
|
145
|
output.Append(" .. ");
|
146
|
output.Append(intType.rangeHigh.ToString());
|
147
|
output.Append("]");
|
148
|
});
|
149
|
}
|
150
|
|
151
|
public override void visit(FloatType floatType)
|
152
|
{
|
153
|
PrintType(floatType, () =>
|
154
|
{
|
155
|
output.Append("float [");
|
156
|
output.Append(floatType.low.ToString());
|
157
|
output.Append(" .. ");
|
158
|
output.Append(floatType.high.ToString());
|
159
|
output.Append("] /*");
|
160
|
output.Append(floatType.precision.ToString());
|
161
|
output.Append("*/");
|
162
|
});
|
163
|
}
|
164
|
|
165
|
|
166
|
public override void visit(EnumIdentifier enumIdentifier)
|
167
|
{
|
168
|
output.Append(enumIdentifier.tokenText);
|
169
|
if (enumIdentifier.HaveValue)
|
170
|
output.Append(String.Format(" = {0}", enumIdentifier.Value));
|
171
|
}
|
172
|
|
173
|
public override void visit(EnumType enumType)
|
174
|
{
|
175
|
PrintType(enumType, () =>
|
176
|
{
|
177
|
output.Append("{");
|
178
|
PrintEnumeration(enumType.listOfEnumSymbols);
|
179
|
output.Append("}");
|
180
|
});
|
181
|
}
|
182
|
|
183
|
|
184
|
public override void visit(ListType listType)
|
185
|
{
|
186
|
PrintType(listType, () =>
|
187
|
{
|
188
|
output.Append("list [");
|
189
|
output.Append(listType.maxNumberOfElements.ToString());
|
190
|
output.Append("] of ");
|
191
|
PrintSubElementOrNull(listType.innerType);
|
192
|
});
|
193
|
}
|
194
|
|
195
|
public override void visit(MapType mapType)
|
196
|
{
|
197
|
PrintType(mapType, () =>
|
198
|
{
|
199
|
output.Append("map [");
|
200
|
output.Append(mapType.maxNumberOfElements.ToString());
|
201
|
output.Append("] ");
|
202
|
PrintSubElementOrNull(mapType.fromType);
|
203
|
output.Append(" to ");
|
204
|
PrintSubElementOrNull(mapType.toType);
|
205
|
});
|
206
|
}
|
207
|
|
208
|
public override void visit(TupleType tupleType)
|
209
|
{
|
210
|
PrintType(tupleType, () =>
|
211
|
{
|
212
|
output.Append("(");
|
213
|
PrintEnumeration(tupleType.innerTypes);
|
214
|
output.Append(")");
|
215
|
});
|
216
|
}
|
217
|
|
218
|
public override void visit(OpaqueType opaqueType)
|
219
|
{
|
220
|
PrintSubElementOrNull(opaqueType.resolvedType);
|
221
|
|
222
|
output.Append(" /*opagque*/");
|
223
|
}
|
224
|
|
225
|
public override void visit(QrType qrType)
|
226
|
{
|
227
|
PrintType(qrType, () =>
|
228
|
{
|
229
|
output.Append("qspace of [");
|
230
|
PrintEnumeration(qrType.landmarks);
|
231
|
output.Append("]");
|
232
|
});
|
233
|
}
|
234
|
|
235
|
public override void visit(LandmarkIdentifier landmarkIdentifier)
|
236
|
{
|
237
|
output.Append(landmarkIdentifier.tokenText);
|
238
|
}
|
239
|
|
240
|
public override void visit(OoActionSystemType ooActionSystemType)
|
241
|
{
|
242
|
PrintType(ooActionSystemType, () =>
|
243
|
{
|
244
|
if (ooActionSystemType.autoConstruction)
|
245
|
output.Append("autocons ");
|
246
|
|
247
|
|
248
|
output.Append("system ");
|
249
|
if (ooActionSystemType.baseType != null)
|
250
|
output.Append(String.Format("({0})", ooActionSystemType.baseType.identifier.tokenText));
|
251
|
output.AppendLine("");
|
252
|
|
253
|
|
254
|
output.IncIndent();
|
255
|
output.AppendLine("|[");
|
256
|
|
257
|
|
258
|
// get a list of interesting symbols
|
259
|
List<AttributeIdentifier> attrs = new List<AttributeIdentifier>();
|
260
|
List<MethodIdentifier> methods = new List<MethodIdentifier>();
|
261
|
List<NamedActionIdentifier> namedActions = new List<NamedActionIdentifier>();
|
262
|
|
263
|
foreach (var sym in ooActionSystemType.symbols.symbolList)
|
264
|
{
|
265
|
if (sym.kind == IdentifierKind.AttributeIdentifier)
|
266
|
attrs.Add((AttributeIdentifier)sym);
|
267
|
else if (sym.kind == IdentifierKind.MethodIdentifier)
|
268
|
methods.Add((MethodIdentifier)sym);
|
269
|
else if (sym.kind == IdentifierKind.NamedActionIdentifier)
|
270
|
namedActions.Add((NamedActionIdentifier)sym);
|
271
|
}
|
272
|
|
273
|
int i = 0;
|
274
|
if (attrs.Count > 0)
|
275
|
{
|
276
|
// variables
|
277
|
output.IncIndent();
|
278
|
output.AppendLine("var");
|
279
|
|
280
|
foreach (var attr in attrs)
|
281
|
{
|
282
|
if (i != 0)
|
283
|
output.AppendLine(";");
|
284
|
i++;
|
285
|
PrintSubElementOrNull(attr);
|
286
|
}
|
287
|
|
288
|
output.DecIndent();
|
289
|
output.AppendLine("");
|
290
|
}
|
291
|
bool writeTypesSave = writeTypes;
|
292
|
writeTypes = false;
|
293
|
|
294
|
i = 0;
|
295
|
if (methods.Count > 0)
|
296
|
{
|
297
|
output.IncIndent();
|
298
|
output.AppendLine("methods");
|
299
|
|
300
|
foreach (var method in methods)
|
301
|
{
|
302
|
if (i != 0)
|
303
|
output.AppendLine(";");
|
304
|
i++;
|
305
|
PrintSubElementOrNull(method);
|
306
|
}
|
307
|
|
308
|
output.DecIndent();
|
309
|
output.AppendLine("");
|
310
|
}
|
311
|
|
312
|
i = 0;
|
313
|
if (namedActions.Count > 0)
|
314
|
{
|
315
|
output.IncIndent();
|
316
|
output.AppendLine("actions");
|
317
|
|
318
|
foreach (var action in namedActions)
|
319
|
{
|
320
|
if (i != 0)
|
321
|
output.AppendLine(";");
|
322
|
i++;
|
323
|
PrintSubElementOrNull(action);
|
324
|
}
|
325
|
|
326
|
output.DecIndent();
|
327
|
output.AppendLine("");
|
328
|
}
|
329
|
|
330
|
if ((ooActionSystemType.doOdBlock != null) && (ooActionSystemType.doOdBlock.statements.Count > 0))
|
331
|
{
|
332
|
output.IncIndent();
|
333
|
output.AppendLine("do");
|
334
|
|
335
|
PrintSubElementOrNull(ooActionSystemType.doOdBlock);
|
336
|
|
337
|
output.DecIndent();
|
338
|
output.AppendLine("");
|
339
|
output.AppendLine("od");
|
340
|
}
|
341
|
output.DecIndent();
|
342
|
output.AppendLine("");
|
343
|
output.Append("]|");
|
344
|
writeTypes = writeTypesSave;
|
345
|
});
|
346
|
}
|
347
|
|
348
|
public override void visit(NullType nullType)
|
349
|
{
|
350
|
output.Append("nil");
|
351
|
}
|
352
|
|
353
|
|
354
|
public override void visit(AttributeIdentifier attributeIdentifier)
|
355
|
{
|
356
|
if (attributeIdentifier.isStatic)
|
357
|
output.Append("static ");
|
358
|
if (attributeIdentifier.isObservable)
|
359
|
output.Append("obs ");
|
360
|
if (attributeIdentifier.isControllable)
|
361
|
output.Append("ctr ");
|
362
|
|
363
|
output.Append(attributeIdentifier.tokenText);
|
364
|
if (writeTypes)
|
365
|
{
|
366
|
output.Append(": ");
|
367
|
PrintSubElementOrNull(attributeIdentifier.type);
|
368
|
output.Append(" = ");
|
369
|
PrintSubElementOrNull(attributeIdentifier.initializer);
|
370
|
}
|
371
|
}
|
372
|
|
373
|
|
374
|
public override void visit<T>(ValueExpression<T> valueExpression)
|
375
|
{
|
376
|
if (valueExpression.value != null)
|
377
|
output.Append(valueExpression.value.ToString());
|
378
|
else
|
379
|
output.Append("nil");
|
380
|
}
|
381
|
|
382
|
public override void visit(AbortStatement abortStatement)
|
383
|
{
|
384
|
output.Append("abort");
|
385
|
}
|
386
|
|
387
|
public override void visit(Assignment assignment)
|
388
|
{
|
389
|
PrintEnumeration(assignment.places);
|
390
|
output.Append(" := ");
|
391
|
PrintEnumeration(assignment.values);
|
392
|
if (assignment.nondetExpression != null)
|
393
|
{
|
394
|
output.IncIndent();
|
395
|
output.AppendLine("");
|
396
|
output.Append("with ");
|
397
|
assignment.nondetExpression.Accept(this);
|
398
|
|
399
|
if (assignment.symbols.symbolList.Count > 0)
|
400
|
{
|
401
|
output.Append(" /* vars: ");
|
402
|
foreach (var localVar in assignment.symbols.symbolList)
|
403
|
{
|
404
|
localVar.Accept(this);
|
405
|
output.Append(" ");
|
406
|
}
|
407
|
output.Append("*/");
|
408
|
}
|
409
|
output.DecIndent();
|
410
|
output.AppendLine("");
|
411
|
}
|
412
|
}
|
413
|
|
414
|
public override void visit(KillStatement killStatement)
|
415
|
{
|
416
|
output.Append("kill (");
|
417
|
PrintSubElementOrNull(killStatement.someOne);
|
418
|
output.Append(")");
|
419
|
}
|
420
|
|
421
|
public override void visit(AccessExpression accessExpression)
|
422
|
{
|
423
|
if (accessExpression.right != null)
|
424
|
{
|
425
|
output.Append("(");
|
426
|
PrintSubElementOrNull(accessExpression.left);
|
427
|
output.Append(").");
|
428
|
PrintSubElementOrNull(accessExpression.right);
|
429
|
}
|
430
|
else
|
431
|
{
|
432
|
output.Append("(");
|
433
|
PrintSubElementOrNull(accessExpression.left);
|
434
|
output.Append(")");
|
435
|
}
|
436
|
}
|
437
|
|
438
|
private void PrintOperator(Expression expression)
|
439
|
{
|
440
|
switch (expression.kind)
|
441
|
{
|
442
|
case ExpressionKind.abs: // T_ABS:
|
443
|
output.Append("abs");
|
444
|
break;
|
445
|
case ExpressionKind.card: // T_CARD:
|
446
|
output.Append("card");
|
447
|
break;
|
448
|
case ExpressionKind.dom: // T_DOM:
|
449
|
output.Append("dom");
|
450
|
break;
|
451
|
case ExpressionKind.range: // T_RNG:
|
452
|
output.Append("range");
|
453
|
break;
|
454
|
case ExpressionKind.merge: // T_MERGE:
|
455
|
output.Append("merge");
|
456
|
break;
|
457
|
case ExpressionKind.len: // T_LEN:
|
458
|
output.Append("len");
|
459
|
break;
|
460
|
case ExpressionKind.elems: // T_ELEMS:
|
461
|
output.Append("elems");
|
462
|
break;
|
463
|
case ExpressionKind.head: // T_HEAD:
|
464
|
output.Append("head");
|
465
|
break;
|
466
|
case ExpressionKind.tail: // T_TAIL:
|
467
|
output.Append("tail");
|
468
|
break;
|
469
|
case ExpressionKind.conc: // T_CONC:
|
470
|
output.Append("conc");
|
471
|
break;
|
472
|
case ExpressionKind.inds: // T_INDS:
|
473
|
output.Append("inds");
|
474
|
break;
|
475
|
case ExpressionKind.dinter: // T_DINTER:
|
476
|
output.Append("dinter");
|
477
|
break;
|
478
|
case ExpressionKind.dunion: // T_DUNION:
|
479
|
output.Append("dunion");
|
480
|
break;
|
481
|
case ExpressionKind.domresby: // T_DOMRESBY:
|
482
|
output.Append("domresby");
|
483
|
break;
|
484
|
case ExpressionKind.domresto: // T_DOMRESTO:
|
485
|
output.Append("domresto");
|
486
|
break;
|
487
|
case ExpressionKind.rngresby: // T_RNGRESBY:
|
488
|
output.Append("rngresby");
|
489
|
break;
|
490
|
case ExpressionKind.rngresto: // T_RNGRESTO:
|
491
|
output.Append("rngresto");
|
492
|
break;
|
493
|
case ExpressionKind.div: // T_DIV:
|
494
|
output.Append("div");
|
495
|
break;
|
496
|
case ExpressionKind.idiv: // T_IDIV:
|
497
|
output.Append("idiv");
|
498
|
break;
|
499
|
case ExpressionKind.mod: // T_MOD:
|
500
|
output.Append("mod");
|
501
|
break;
|
502
|
case ExpressionKind.prod: // T_PROD:
|
503
|
output.Append("*");
|
504
|
break;
|
505
|
case ExpressionKind.inter: // T_INTER:
|
506
|
output.Append("inter");
|
507
|
break;
|
508
|
case ExpressionKind.sum: // T_SUM:
|
509
|
output.Append("+");
|
510
|
break;
|
511
|
case ExpressionKind.minus: // T_MINUS:
|
512
|
output.Append("-");
|
513
|
break;
|
514
|
case ExpressionKind.union: // T_UNION:
|
515
|
output.Append("union");
|
516
|
break;
|
517
|
case ExpressionKind.diff: // T_DIFF:
|
518
|
output.Append("diff");
|
519
|
break;
|
520
|
case ExpressionKind.munion: // T_MUNION:
|
521
|
output.Append("munion");
|
522
|
break;
|
523
|
case ExpressionKind.seqmod_mapoverride: // T_SEQMOD_MAPOVERRIDE:
|
524
|
output.Append("seqmod_mapoverride");
|
525
|
break;
|
526
|
case ExpressionKind.less:
|
527
|
output.Append("<");
|
528
|
break;
|
529
|
case ExpressionKind.lessequal:
|
530
|
output.Append("<=");
|
531
|
break;
|
532
|
case ExpressionKind.greater:
|
533
|
output.Append(">");
|
534
|
break;
|
535
|
case ExpressionKind.greaterequal:
|
536
|
output.Append(">=");
|
537
|
break;
|
538
|
case ExpressionKind.equal:
|
539
|
output.Append("=");
|
540
|
break;
|
541
|
case ExpressionKind.notequal:
|
542
|
output.Append("<>");
|
543
|
break;
|
544
|
case ExpressionKind.subset:
|
545
|
output.Append("subset");
|
546
|
break;
|
547
|
case ExpressionKind.elemin:
|
548
|
output.Append("in");
|
549
|
break;
|
550
|
case ExpressionKind.notelemin:
|
551
|
output.Append("not in");
|
552
|
break;
|
553
|
case ExpressionKind.and: // T_AND:
|
554
|
output.Append("and");
|
555
|
break;
|
556
|
|
557
|
case ExpressionKind.or: // T_OR:
|
558
|
output.Append("or");
|
559
|
break;
|
560
|
case ExpressionKind.implies: // T_IMPLIES:
|
561
|
output.Append("=>");
|
562
|
break;
|
563
|
case ExpressionKind.biimplies: // T_BIIMPLIES:
|
564
|
output.Append("<=>");
|
565
|
break;
|
566
|
|
567
|
case ExpressionKind.Primed:
|
568
|
output.Append("'");
|
569
|
break;
|
570
|
|
571
|
|
572
|
case ExpressionKind.Cast:
|
573
|
output.Append("(");
|
574
|
if (expression.type != null)
|
575
|
output.Append(expression.type.ToString());
|
576
|
else
|
577
|
output.Append("Cast??");
|
578
|
output.Append(")");
|
579
|
break;
|
580
|
|
581
|
default:
|
582
|
output.Append(Enum.GetName(typeof(ExpressionKind), expression.kind));
|
583
|
break;
|
584
|
}
|
585
|
}
|
586
|
|
587
|
public override void visit(TypeExpression typeExpression)
|
588
|
{
|
589
|
PrintSubElementOrNull(typeExpression.type);
|
590
|
}
|
591
|
|
592
|
public override void visit(BinaryOperator binaryOperator)
|
593
|
{
|
594
|
output.IncIndent();
|
595
|
output.AppendLine("(");
|
596
|
PrintSubElementOrNull(binaryOperator.left);
|
597
|
output.DecIndent();
|
598
|
output.AppendLine("");
|
599
|
output.AppendLine(")");
|
600
|
|
601
|
PrintOperator(binaryOperator); output.AppendLine("");
|
602
|
|
603
|
output.IncIndent();
|
604
|
output.AppendLine("(");
|
605
|
PrintSubElementOrNull(binaryOperator.right);
|
606
|
output.DecIndent();
|
607
|
output.AppendLine("");
|
608
|
output.AppendLine(")");
|
609
|
}
|
610
|
|
611
|
public override void visit(UnaryOperator unaryOperator)
|
612
|
{
|
613
|
PrintOperator(unaryOperator);
|
614
|
output.Append("(");
|
615
|
PrintSubElementOrNull(unaryOperator.child);
|
616
|
output.Append(")");
|
617
|
}
|
618
|
|
619
|
public override void visit(UnresolvedIdentifierExpression unresolvedIdentifierExpression)
|
620
|
{
|
621
|
output.Append(unresolvedIdentifierExpression.tokenText);
|
622
|
}
|
623
|
|
624
|
public override void visit(CallExpression callExpression)
|
625
|
{
|
626
|
if (callExpression.child != null)
|
627
|
callExpression.child.Accept(this);
|
628
|
if ((callExpression.arguments != null) && (callExpression.arguments.Count > 0))
|
629
|
{
|
630
|
output.Append("(");
|
631
|
PrintEnumeration(callExpression.arguments);
|
632
|
output.Append(")");
|
633
|
}
|
634
|
else
|
635
|
{
|
636
|
output.Append("()");
|
637
|
}
|
638
|
}
|
639
|
|
640
|
public override void visit(TupleMapAccessExpression tupleMapAccessExpression)
|
641
|
{
|
642
|
if (tupleMapAccessExpression.child != null)
|
643
|
tupleMapAccessExpression.child.Accept(this);
|
644
|
output.Append("[");
|
645
|
PrintSubElementOrNull(tupleMapAccessExpression.argument);
|
646
|
output.Append("]");
|
647
|
}
|
648
|
|
649
|
public override void visit(TupleConstructor tupleConstructor)
|
650
|
{
|
651
|
output.Append(tupleConstructor.tupleType.tokenText);
|
652
|
output.Append("(");
|
653
|
PrintEnumeration(tupleConstructor.values);
|
654
|
output.Append(")");
|
655
|
}
|
656
|
|
657
|
public override void visit(ListConstructor listConstructor)
|
658
|
{
|
659
|
output.Append("[");
|
660
|
PrintEnumeration(listConstructor.elements);
|
661
|
if (listConstructor.hasComprehension)
|
662
|
{
|
663
|
output.Append("| var ");
|
664
|
int i = 0;
|
665
|
foreach (var element in listConstructor.comprehensionVariables.symbolList)
|
666
|
{
|
667
|
if (i != 0)
|
668
|
output.Append("; ");
|
669
|
i++;
|
670
|
PrintSubElementOrNull(element);
|
671
|
}
|
672
|
output.AppendLine(" &");
|
673
|
PrintSubElementOrNull(listConstructor.comprehension);
|
674
|
}
|
675
|
output.Append("]");
|
676
|
}
|
677
|
|
678
|
public override void visit(QValConstructor qValConstructor)
|
679
|
{
|
680
|
output.Append("qval(");
|
681
|
PrintSubElementOrNull(qValConstructor.value[0]);
|
682
|
if (qValConstructor.value.Length == 2)
|
683
|
{
|
684
|
output.Append(" .. ");
|
685
|
PrintSubElementOrNull(qValConstructor.value[1]);
|
686
|
}
|
687
|
output.Append(",");
|
688
|
output.Append(qValConstructor.valueDeriv.ToString());
|
689
|
output.Append(")");
|
690
|
}
|
691
|
|
692
|
|
693
|
private bool make_parens = false;
|
694
|
|
695
|
public override void visit(NondetBlock nondetBlock)
|
696
|
{
|
697
|
int i = 0;
|
698
|
if (make_parens)
|
699
|
{
|
700
|
output.IncIndent();
|
701
|
output.AppendLine("(");
|
702
|
}
|
703
|
foreach (var smt in nondetBlock.statements)
|
704
|
{
|
705
|
if (i != 0)
|
706
|
{
|
707
|
output.DecIndent();
|
708
|
output.AppendLine("");
|
709
|
output.IncIndent();
|
710
|
output.AppendLine("[] ");
|
711
|
}
|
712
|
i++;
|
713
|
smt.Accept(this);
|
714
|
}
|
715
|
if (make_parens)
|
716
|
{
|
717
|
output.DecIndent();
|
718
|
output.AppendLine("");
|
719
|
output.AppendLine(")");
|
720
|
}
|
721
|
}
|
722
|
|
723
|
public override void visit(PrioBlock prioBlock)
|
724
|
{
|
725
|
int i = 0;
|
726
|
if (make_parens)
|
727
|
{
|
728
|
output.IncIndent();
|
729
|
output.AppendLine("(");
|
730
|
}
|
731
|
foreach (var smt in prioBlock.statements)
|
732
|
{
|
733
|
if (i != 0)
|
734
|
{
|
735
|
output.DecIndent();
|
736
|
output.AppendLine("");
|
737
|
output.IncIndent();
|
738
|
output.AppendLine("// ");
|
739
|
}
|
740
|
i++;
|
741
|
smt.Accept(this);
|
742
|
}
|
743
|
if (make_parens)
|
744
|
{
|
745
|
output.DecIndent();
|
746
|
output.AppendLine("");
|
747
|
output.AppendLine(")");
|
748
|
}
|
749
|
}
|
750
|
|
751
|
public override void visit(SeqBlock seqBlock)
|
752
|
{
|
753
|
int i = 0;
|
754
|
bool old_makeparens = make_parens;
|
755
|
output.IncIndent();
|
756
|
output.AppendLine("(");
|
757
|
if (seqBlock.symbols.symbolList.Count > 0)
|
758
|
{
|
759
|
output.Append("var ");
|
760
|
foreach (var id in seqBlock.symbols.symbolList)
|
761
|
{
|
762
|
output.Append(id.tokenText);
|
763
|
output.Append(": ");
|
764
|
PrintSubElementOrNull(id);
|
765
|
}
|
766
|
output.Append(": ");
|
767
|
}
|
768
|
foreach (var smt in seqBlock.statements)
|
769
|
{
|
770
|
if (smt == null)
|
771
|
continue;
|
772
|
if (i != 0)
|
773
|
{
|
774
|
output.DecIndent();
|
775
|
output.AppendLine("");
|
776
|
output.IncIndent();
|
777
|
output.AppendLine("; ");
|
778
|
}
|
779
|
i++;
|
780
|
make_parens = true;
|
781
|
smt.Accept(this);
|
782
|
make_parens = old_makeparens;
|
783
|
}
|
784
|
output.DecIndent();
|
785
|
output.AppendLine("");
|
786
|
output.AppendLine(")");
|
787
|
}
|
788
|
|
789
|
public override void visit(Call call)
|
790
|
{
|
791
|
call.callExpression.Accept(this);
|
792
|
}
|
793
|
|
794
|
public override void visit(GuardedCommand guardedCommand)
|
795
|
{
|
796
|
output.Append("requires ");
|
797
|
PrintSubElementOrNull(guardedCommand.guard);
|
798
|
output.IncIndent();
|
799
|
output.AppendLine(" :");
|
800
|
|
801
|
guardedCommand.body.Accept(this);
|
802
|
|
803
|
output.DecIndent();
|
804
|
output.AppendLine("");
|
805
|
output.AppendLine("end");
|
806
|
}
|
807
|
|
808
|
public override void visit(IdentifierExpression identifierExpression)
|
809
|
{
|
810
|
output.Append(identifierExpression.identifier.tokenText);
|
811
|
}
|
812
|
|
813
|
public override void visit(NondetIdentifierList nondetIdentifierList)
|
814
|
{
|
815
|
int i = 0;
|
816
|
foreach (var smt in nondetIdentifierList.identifiers)
|
817
|
{
|
818
|
if (i != 0)
|
819
|
{
|
820
|
output.DecIndent();
|
821
|
output.AppendLine("");
|
822
|
output.IncIndent();
|
823
|
output.AppendLine("[] ");
|
824
|
}
|
825
|
i++;
|
826
|
smt.Accept(this);
|
827
|
}
|
828
|
}
|
829
|
|
830
|
public override void visit(PrioIdentifierList prioIdentifierList)
|
831
|
{
|
832
|
int i = 0;
|
833
|
foreach (var smt in prioIdentifierList.identifiers)
|
834
|
{
|
835
|
if (i != 0)
|
836
|
{
|
837
|
output.DecIndent();
|
838
|
output.AppendLine("");
|
839
|
output.IncIndent();
|
840
|
output.AppendLine("//");
|
841
|
}
|
842
|
i++;
|
843
|
smt.Accept(this);
|
844
|
}
|
845
|
}
|
846
|
|
847
|
|
848
|
public override void visit(SeqIdentifierList seqIdentifierList)
|
849
|
{
|
850
|
int i = 0;
|
851
|
output.IncIndent();
|
852
|
output.AppendLine("(");
|
853
|
foreach (var smt in seqIdentifierList.identifiers)
|
854
|
{
|
855
|
if (i != 0)
|
856
|
{
|
857
|
output.AppendLine("; ");
|
858
|
}
|
859
|
i++;
|
860
|
smt.Accept(this);
|
861
|
}
|
862
|
output.DecIndent();
|
863
|
output.AppendLine("");
|
864
|
output.AppendLine(")");
|
865
|
}
|
866
|
|
867
|
public override void visit(SkipStatement skipStatement)
|
868
|
{
|
869
|
output.Append("skip");
|
870
|
}
|
871
|
|
872
|
public override void visit(TernaryOperator ternaryOperator)
|
873
|
{
|
874
|
if (ternaryOperator.kind == ExpressionKind.conditional)
|
875
|
{
|
876
|
output.Append("if");
|
877
|
PrintSubElementOrNull(ternaryOperator.left);
|
878
|
output.IncIndent();
|
879
|
output.AppendLine("then");
|
880
|
PrintSubElementOrNull(ternaryOperator.mid);
|
881
|
output.DecIndent();
|
882
|
output.AppendLine("");
|
883
|
output.IncIndent();
|
884
|
output.AppendLine("else");
|
885
|
PrintSubElementOrNull(ternaryOperator.right);
|
886
|
output.DecIndent();
|
887
|
output.AppendLine("");
|
888
|
output.Append("end");
|
889
|
}
|
890
|
else if (ternaryOperator.kind == ExpressionKind.foldLR ||
|
891
|
ternaryOperator.kind == ExpressionKind.foldRL)
|
892
|
{
|
893
|
PrintSubElementOrNull(ternaryOperator.left);
|
894
|
if (ternaryOperator.mid != null)
|
895
|
{
|
896
|
output.Append(" :: (");
|
897
|
PrintSubElementOrNull(ternaryOperator.mid);
|
898
|
output.Append(")");
|
899
|
}
|
900
|
if (ternaryOperator.kind == ExpressionKind.foldLR)
|
901
|
output.Append(" :>: (");
|
902
|
else
|
903
|
output.Append(" :<: (");
|
904
|
PrintSubElementOrNull(ternaryOperator.right);
|
905
|
output.Append(")");
|
906
|
}
|
907
|
else
|
908
|
throw new NotImplementedException();
|
909
|
}
|
910
|
|
911
|
public override void visit(MethodIdentifier methodIdentifier)
|
912
|
{
|
913
|
if (((FunctionType)methodIdentifier.type).isMiracleSafe ?? false)
|
914
|
output.Append("/*BASIC*/ ");
|
915
|
|
916
|
|
917
|
FunctionType atype = (FunctionType)methodIdentifier.type;
|
918
|
|
919
|
output.Append(methodIdentifier.tokenText);
|
920
|
output.Append("(");
|
921
|
int j = 0;
|
922
|
foreach (var x in methodIdentifier.parameter)
|
923
|
{
|
924
|
if (j != 0)
|
925
|
output.Append(", ");
|
926
|
else
|
927
|
j++;
|
928
|
output.Append(x.tokenText);
|
929
|
output.Append(": ");
|
930
|
PrintSubElementOrNull(x.type);
|
931
|
}
|
932
|
output.Append(")");
|
933
|
if (atype.returnType != null)
|
934
|
{
|
935
|
output.Append(": ");
|
936
|
PrintSubElementOrNull(atype.returnType);
|
937
|
}
|
938
|
output.AppendLine("");
|
939
|
|
940
|
output.IncIndent();
|
941
|
output.AppendLine("var");
|
942
|
int i = 0;
|
943
|
foreach (var sym in methodIdentifier.symbolTable.symbolList)
|
944
|
{
|
945
|
if (i != 0)
|
946
|
output.AppendLine(";");
|
947
|
i++;
|
948
|
output.Append(sym.tokenText); output.Append(": "); PrintSubElementOrNull(sym.type);
|
949
|
}
|
950
|
output.DecIndent();
|
951
|
output.AppendLine("");
|
952
|
|
953
|
PrintSubElementOrNull(methodIdentifier.body);
|
954
|
output.Append("end");
|
955
|
}
|
956
|
|
957
|
public override void visit(NamedActionIdentifier namedActionIdentifier)
|
958
|
{
|
959
|
if (((FunctionType)namedActionIdentifier.type).isMiracleSafe ?? false)
|
960
|
output.Append("/*BASIC*/ ");
|
961
|
|
962
|
output.Append(namedActionIdentifier.tokenText);
|
963
|
output.Append("(");
|
964
|
FunctionType atype = (FunctionType)namedActionIdentifier.type;
|
965
|
PrintEnumeration(atype.parameter);
|
966
|
output.Append(")");
|
967
|
output.AppendLine("");
|
968
|
|
969
|
output.IncIndent();
|
970
|
output.AppendLine("var");
|
971
|
int i = 0;
|
972
|
foreach (var sym in namedActionIdentifier.symbolTable.symbolList)
|
973
|
{
|
974
|
if (i != 0)
|
975
|
output.AppendLine(";");
|
976
|
i++;
|
977
|
output.Append(sym.tokenText); output.Append(": "); PrintSubElementOrNull(sym.type);
|
978
|
}
|
979
|
output.DecIndent();
|
980
|
output.AppendLine("");
|
981
|
|
982
|
PrintSubElementOrNull(namedActionIdentifier.body);
|
983
|
output.Append("end");
|
984
|
}
|
985
|
|
986
|
public override void visit(MapConstructor mapConstructor)
|
987
|
{
|
988
|
output.Append("{");
|
989
|
int i = 0;
|
990
|
foreach (var x in mapConstructor.items)
|
991
|
{
|
992
|
if (i != 0)
|
993
|
output.Append(", ");
|
994
|
i++;
|
995
|
PrintSubElementOrNull(x.key);
|
996
|
output.Append(" -> ");
|
997
|
PrintSubElementOrNull(x.value);
|
998
|
}
|
999
|
output.Append("}");
|
1000
|
}
|
1001
|
|
1002
|
public override void visit(SetConstructor setConstructor)
|
1003
|
{
|
1004
|
output.Append("[");
|
1005
|
PrintEnumeration(setConstructor.items);
|
1006
|
if (setConstructor.hasComprehension)
|
1007
|
{
|
1008
|
output.Append("| var ");
|
1009
|
int i = 0;
|
1010
|
foreach (var elem in setConstructor.comprehensionVariables.symbolList)
|
1011
|
{
|
1012
|
if (i != 0)
|
1013
|
output.Append("; ");
|
1014
|
i++;
|
1015
|
PrintSubElementOrNull(elem);
|
1016
|
}
|
1017
|
output.AppendLine(" &");
|
1018
|
PrintSubElementOrNull(setConstructor.comprehension);
|
1019
|
}
|
1020
|
output.Append("]");
|
1021
|
}
|
1022
|
|
1023
|
public override void visit(ExpressionVariableIdentifier expressionVariableIdentifier)
|
1024
|
{
|
1025
|
output.Append(expressionVariableIdentifier.tokenText); output.Append(": ");
|
1026
|
PrintSubElementOrNull(expressionVariableIdentifier.type);
|
1027
|
}
|
1028
|
|
1029
|
|
1030
|
public override void visit(ObjectConstructor objectConstructor)
|
1031
|
{
|
1032
|
output.Append("new (");
|
1033
|
PrintSubElementOrNull(objectConstructor.type);
|
1034
|
output.Append(")");
|
1035
|
}
|
1036
|
|
1037
|
public override string ToString()
|
1038
|
{
|
1039
|
return output.ToString();
|
1040
|
}
|
1041
|
|
1042
|
public OoaPrintVisitor(ParserState aState)
|
1043
|
: base(aState)
|
1044
|
{
|
1045
|
writeTypes = true;
|
1046
|
output = new OoaCodeEmitter();
|
1047
|
}
|
1048
|
}
|
1049
|
}
|