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