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