1 package org.simantics.scl.compiler.elaboration.expressions.printing;
3 import java.util.Map.Entry;
5 import org.simantics.scl.compiler.elaboration.chr.CHRLiteral;
6 import org.simantics.scl.compiler.elaboration.chr.CHRQuery;
7 import org.simantics.scl.compiler.elaboration.chr.CHRRule;
8 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
9 import org.simantics.scl.compiler.elaboration.expressions.Assignment;
10 import org.simantics.scl.compiler.elaboration.expressions.Case;
11 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous;
12 import org.simantics.scl.compiler.elaboration.expressions.EApply;
13 import org.simantics.scl.compiler.elaboration.expressions.EApplyType;
14 import org.simantics.scl.compiler.elaboration.expressions.EAsPattern;
15 import org.simantics.scl.compiler.elaboration.expressions.EBinary;
16 import org.simantics.scl.compiler.elaboration.expressions.EBind;
17 import org.simantics.scl.compiler.elaboration.expressions.EBlock;
18 import org.simantics.scl.compiler.elaboration.expressions.ECHRRuleset;
19 import org.simantics.scl.compiler.elaboration.expressions.ECHRRulesetConstructor;
20 import org.simantics.scl.compiler.elaboration.expressions.ECHRSelect;
21 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
22 import org.simantics.scl.compiler.elaboration.expressions.ECoveringBranchPoint;
23 import org.simantics.scl.compiler.elaboration.expressions.EEnforce;
24 import org.simantics.scl.compiler.elaboration.expressions.EEquations;
25 import org.simantics.scl.compiler.elaboration.expressions.EError;
26 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
27 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
28 import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint;
29 import org.simantics.scl.compiler.elaboration.expressions.EIf;
30 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
31 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
32 import org.simantics.scl.compiler.elaboration.expressions.ELambdaType;
33 import org.simantics.scl.compiler.elaboration.expressions.ELet;
34 import org.simantics.scl.compiler.elaboration.expressions.EListComprehension;
35 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
36 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
37 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
38 import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder;
39 import org.simantics.scl.compiler.elaboration.expressions.EPreLet;
40 import org.simantics.scl.compiler.elaboration.expressions.ERange;
41 import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral;
42 import org.simantics.scl.compiler.elaboration.expressions.ERecord;
43 import org.simantics.scl.compiler.elaboration.expressions.ERuleset;
44 import org.simantics.scl.compiler.elaboration.expressions.ESelect;
45 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
46 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet;
47 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
48 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
49 import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation;
50 import org.simantics.scl.compiler.elaboration.expressions.EVar;
51 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
52 import org.simantics.scl.compiler.elaboration.expressions.EViewPattern;
53 import org.simantics.scl.compiler.elaboration.expressions.EWhen;
54 import org.simantics.scl.compiler.elaboration.expressions.Expression;
55 import org.simantics.scl.compiler.elaboration.expressions.ExpressionVisitor;
56 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression;
57 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup;
58 import org.simantics.scl.compiler.elaboration.expressions.Variable;
59 import org.simantics.scl.compiler.elaboration.java.EqRelation;
60 import org.simantics.scl.compiler.elaboration.java.MemberRelation;
61 import org.simantics.scl.compiler.elaboration.query.QAlternative;
62 import org.simantics.scl.compiler.elaboration.query.QAtom;
63 import org.simantics.scl.compiler.elaboration.query.QConjunction;
64 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
65 import org.simantics.scl.compiler.elaboration.query.QExists;
66 import org.simantics.scl.compiler.elaboration.query.QIf;
67 import org.simantics.scl.compiler.elaboration.query.QMapping;
68 import org.simantics.scl.compiler.elaboration.query.QNegation;
69 import org.simantics.scl.compiler.elaboration.query.Query;
70 import org.simantics.scl.compiler.elaboration.query.QueryVisitor;
71 import org.simantics.scl.compiler.elaboration.rules.SectionName;
72 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
74 public class ExpressionToStringVisitor implements ExpressionVisitor, QueryVisitor {
76 StringBuilder b = new StringBuilder();
79 public ExpressionToStringVisitor(StringBuilder b) {
83 public void show(Variable variable) {
85 b.append("NULL_VARIABLE");
87 b.append(variable.getName());
90 private void newLine() {
92 for(int i=0;i<indentation;++i)
96 public void showPar(Expression expression) {
97 boolean needsPar = false;
99 if(expression instanceof EPlaceholder)
100 expression = ((EPlaceholder)expression).expression;
101 else if(expression instanceof ETypeAnnotation)
102 expression = ((ETypeAnnotation)expression).getValue();
103 else if(expression instanceof EApplyType)
104 expression = ((EApplyType)expression).getExpression();
105 else if(expression instanceof ELambdaType)
106 expression = ((ELambdaType)expression).value;
107 else if(expression instanceof ECoveringBranchPoint)
108 expression = ((ECoveringBranchPoint)expression).expression;
112 if(expression instanceof EApply ||
113 expression instanceof EIf ||
114 expression instanceof ESimpleLambda ||
115 expression instanceof ESimpleLet)
119 expression.accept(this);
125 public void visit(EApply expression) {
126 showPar(expression.getFunction());
127 for(Expression parameter : expression.getParameters()) {
134 public void visit(EApplyType expression) {
135 expression.getExpression().accept(this);
139 public void visit(EAsPattern expression) {
140 show(expression.getVariable());
142 showPar(expression.getPattern());
146 public void visit(EBind expression) {
151 public void visit(EConstant expression) {
152 String name = expression.getValue().getName().name;
153 if(Character.isJavaIdentifierStart(name.charAt(0)))
156 b.append('(').append(name).append(')');
160 public void visit(EEnforce expression) {
161 b.append("enforce ");
162 expression.getQuery().accept(this);
166 public void visit(EError expression) {
171 public void visit(EExternalConstant expression) {
172 b.append(expression.getValue());
176 public void visit(EFieldAccess expression) {
177 b.append("EFieldAccess");
181 public void visit(EGetConstraint expression) {
182 b.append("EGetConstraint");
186 public void visit(EIf expression) {
188 expression.condition.accept(this);
192 expression.then_.accept(this);
193 if(expression.else_ != null) {
196 expression.else_.accept(this);
202 public void visit(QIf query) {
204 query.condition.accept(this);
208 query.thenQuery.accept(this);
211 query.elseQuery.accept(this);
216 public void visit(EIntegerLiteral expression) {
217 b.append(expression.getValue());
221 public void visit(ELambda expression) {
224 for(Case case_ : expression.getCases()) {
226 for(Expression pat : case_.patterns) {
232 case_.value.accept(this);
239 public void visit(EViewPattern expression) {
241 expression.expression.accept(this);
243 expression.pattern.accept(this);
248 public void visit(ELambdaType expression) {
249 expression.value.accept(this);
253 public void visit(ELet expression) {
256 printAsDo(expression);
261 public void visit(EListComprehension expression) {
262 b.append("EListComprehension");
266 public void visit(EListLiteral expression) {
268 boolean first = true;
269 for(Expression component : expression.getComponents()) {
274 component.accept(this);
280 public void visit(ELiteral expression) {
281 b.append(expression.getValue().toString());
285 public void visit(EMatch expression) {
287 for(Expression s : expression.getScrutinee()) {
293 for(Case case_ : expression.getCases()) {
295 for(Expression pat : case_.patterns) {
301 case_.value.accept(this);
308 public void visit(EPlaceholder expression) {
309 expression.expression.accept(this);
313 public void visit(ERealLiteral expression) {
314 b.append(expression.getValue());
318 public void visit(ERuleset expression) {
321 for(ERuleset.DatalogRule rule : expression.getRules()) {
327 expression.getIn().accept(this);
331 public void visit(ESelect expression) {
336 public void visit(ECHRSelect expression) {
337 b.append("ECHRSelect");
341 public void visit(ESimpleLambda expression) {
343 show(expression.getParameter());
344 while(expression.getValue() instanceof ESimpleLambda) {
345 expression = (ESimpleLambda)expression.getValue();
347 show(expression.getParameter());
350 expression.getValue().accept(this);
354 public void visit(ESimpleLet expression) {
357 printAsDo(expression);
361 private void printAsDo(Expression expression) {
362 if(expression instanceof ESimpleLet) {
363 ESimpleLet let = (ESimpleLet)expression;
364 Variable variable = let.getVariable();
365 Expression value = let.getValue();
366 if(variable == null || "_".equals(variable.getName()))
374 printAsDo(let.getIn());
376 else if(expression instanceof ELet) {
377 ELet let = (ELet)expression;
378 for(Assignment assignment : let.assignments) {
380 assignment.pattern.accept(this);
382 assignment.value.accept(this);
388 expression.accept(this);
393 public void visit(ETransformation expression) {
394 b.append("<transformation>");
398 public void visit(ETypeAnnotation expression) {
399 expression.getValue().accept(this);
403 public void visit(EVar expression) {
404 b.append(expression.name);
408 public void visit(EVariable expression) {
409 show(expression.getVariable());
413 public void visit(EWhen expression) {
415 expression.getQuery().accept(this);
417 expression.getAction().accept(this);
421 public void visit(GuardedExpressionGroup expression) {
422 boolean first = true;
423 for(GuardedExpression gexp : expression.expressions) {
429 for(int i=0;i<gexp.guards.length;++i) {
432 gexp.guards[i].accept(this);
435 gexp.value.accept(this);
440 public void visit(QAlternative query) {
441 b.append("QAlternative");
445 public void visit(QAtom query) {
446 if(query.relation == EqRelation.INSTANCE) {
447 query.parameters[0].accept(this);
449 query.parameters[1].accept(this);
451 else if(query.relation == MemberRelation.INSTANCE) {
452 query.parameters[0].accept(this);
454 query.parameters[1].accept(this);
457 b.append(query.relation);
458 for(Expression parameter : query.parameters) {
466 public void visit(QConjunction query) {
467 boolean first = true;
468 for(Query q : query.queries) {
478 public void visit(QDisjunction query) {
479 b.append("QDisjunction");
483 public void visit(QExists query) {
488 public void visit(QNegation query) {
489 b.append("QNegation");
493 public void visit(QMapping query) {
494 b.append(query.mappingRelation.name.name);
495 for(Expression parameter : query.parameters) {
497 parameter.accept(this);
501 public void visit(ERuleset.DatalogRule rule) {
502 b.append(rule.headRelation.getName());
503 for(Expression parameter : rule.headParameters) {
509 rule.body.accept(this);
513 public void visit(TransformationRule rule) {
514 b.append("rule ").append(rule.name.name).append(" where");
515 for(Entry<SectionName, Query[]> section : rule.sections.entrySet()) {
516 b.append("\n@").append(section.getKey().name());
517 for(Query query : section.getValue()) {
526 public void visit(ECoveringBranchPoint expression) {
527 expression.expression.accept(this);
531 public void visit(EEquations expression) {
536 public void visit(ECHRRuleset expression) {
537 b.append("ECHRRuleset");
541 public void visit(ECHRRulesetConstructor expression) {
542 b.append("ECHRRulesetConstructor");
545 public void visit(CHRRule rule) {
551 public void visit(CHRQuery query) {
552 boolean first = true;
553 for(CHRLiteral literal : query.literals) {
562 public void visit(CHRLiteral literal) {
563 if(literal.passive && literal.relation instanceof CHRConstraint)
564 b.append("@passive ");
565 if(literal.killAfterMatch)
567 b.append(literal.relation);
568 for(Expression parameter : literal.parameters) {
575 public void visit(EBinary expression) {
576 b.append("<EBinary>");
580 public void visit(EBlock expression) {
581 b.append("<EBlock>");
585 public void visit(EPreLet expression) {
586 b.append("<EPreLet>");
590 public void visit(ERange expression) {
592 expression.from.accept(this);
594 expression.to.accept(this);
599 public void visit(ERecord expression) {
600 b.append("<ERecord>");
604 public void visit(EStringLiteral expression) {
606 for(int i=0;i<expression.strings.length;++i) {
607 b.append(expression.strings[i]);
608 if(i < expression.expressions.length) {
610 expression.expressions[i].accept(this);
618 public void visit(EAmbiguous eAmbiguous) {
619 b.append("EAmbigious");