1 package org.simantics.scl.compiler.elaboration.expressions.printing;
\r
3 import java.util.Map.Entry;
\r
5 import org.simantics.scl.compiler.elaboration.expressions.Assignment;
\r
6 import org.simantics.scl.compiler.elaboration.expressions.Case;
\r
7 import org.simantics.scl.compiler.elaboration.expressions.EApply;
\r
8 import org.simantics.scl.compiler.elaboration.expressions.EApplyType;
\r
9 import org.simantics.scl.compiler.elaboration.expressions.EAsPattern;
\r
10 import org.simantics.scl.compiler.elaboration.expressions.EBind;
\r
11 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
\r
12 import org.simantics.scl.compiler.elaboration.expressions.EEnforce;
\r
13 import org.simantics.scl.compiler.elaboration.expressions.EEquations;
\r
14 import org.simantics.scl.compiler.elaboration.expressions.EError;
\r
15 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
\r
16 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
\r
17 import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint;
\r
18 import org.simantics.scl.compiler.elaboration.expressions.EIf;
\r
19 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
\r
20 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
\r
21 import org.simantics.scl.compiler.elaboration.expressions.ELambdaType;
\r
22 import org.simantics.scl.compiler.elaboration.expressions.ELet;
\r
23 import org.simantics.scl.compiler.elaboration.expressions.EListComprehension;
\r
24 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
\r
25 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
\r
26 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
\r
27 import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder;
\r
28 import org.simantics.scl.compiler.elaboration.expressions.ECoveringBranchPoint;
\r
29 import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral;
\r
30 import org.simantics.scl.compiler.elaboration.expressions.ERuleset;
\r
31 import org.simantics.scl.compiler.elaboration.expressions.ESelect;
\r
32 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
\r
33 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet;
\r
34 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
\r
35 import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation;
\r
36 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
\r
37 import org.simantics.scl.compiler.elaboration.expressions.EWhen;
\r
38 import org.simantics.scl.compiler.elaboration.expressions.Expression;
\r
39 import org.simantics.scl.compiler.elaboration.expressions.ExpressionVisitor;
\r
40 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression;
\r
41 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup;
\r
42 import org.simantics.scl.compiler.elaboration.expressions.Variable;
\r
43 import org.simantics.scl.compiler.elaboration.java.EqRelation;
\r
44 import org.simantics.scl.compiler.elaboration.java.MemberRelation;
\r
45 import org.simantics.scl.compiler.elaboration.query.QAlternative;
\r
46 import org.simantics.scl.compiler.elaboration.query.QAtom;
\r
47 import org.simantics.scl.compiler.elaboration.query.QConjunction;
\r
48 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
\r
49 import org.simantics.scl.compiler.elaboration.query.QExists;
\r
50 import org.simantics.scl.compiler.elaboration.query.QIf;
\r
51 import org.simantics.scl.compiler.elaboration.query.QMapping;
\r
52 import org.simantics.scl.compiler.elaboration.query.QNegation;
\r
53 import org.simantics.scl.compiler.elaboration.query.Query;
\r
54 import org.simantics.scl.compiler.elaboration.query.QueryVisitor;
\r
55 import org.simantics.scl.compiler.elaboration.rules.SectionName;
\r
56 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
\r
58 public class ExpressionToStringVisitor implements ExpressionVisitor, QueryVisitor {
\r
60 StringBuilder b = new StringBuilder();
\r
63 public ExpressionToStringVisitor(StringBuilder b) {
\r
67 public void show(Variable variable) {
\r
68 if(variable == null)
\r
69 b.append("NULL_VARIABLE");
\r
71 b.append(variable.getName());
\r
74 private void newLine() {
\r
76 for(int i=0;i<indentation;++i)
\r
80 public void showPar(Expression expression) {
\r
81 boolean needsPar = false;
\r
83 if(expression instanceof EPlaceholder)
\r
84 expression = ((EPlaceholder)expression).expression;
\r
85 else if(expression instanceof ETypeAnnotation)
\r
86 expression = ((ETypeAnnotation)expression).getValue();
\r
87 else if(expression instanceof EApplyType)
\r
88 expression = ((EApplyType)expression).getExpression();
\r
89 else if(expression instanceof ELambdaType)
\r
90 expression = ((ELambdaType)expression).value;
\r
91 else if(expression instanceof ECoveringBranchPoint)
\r
92 expression = ((ECoveringBranchPoint)expression).expression;
\r
96 if(expression instanceof EApply ||
\r
97 expression instanceof EIf ||
\r
98 expression instanceof ESimpleLambda ||
\r
99 expression instanceof ESimpleLet)
\r
103 expression.accept(this);
\r
109 public void visit(EApply expression) {
\r
110 showPar(expression.getFunction());
\r
111 for(Expression parameter : expression.getParameters()) {
\r
113 showPar(parameter);
\r
118 public void visit(EApplyType expression) {
\r
119 expression.getExpression().accept(this);
\r
123 public void visit(EAsPattern expression) {
\r
124 show(expression.getVariable());
\r
126 showPar(expression.getPattern());
\r
130 public void visit(EBind expression) {
\r
135 public void visit(EConstant expression) {
\r
136 String name = expression.getValue().getName().name;
\r
137 if(Character.isJavaIdentifierStart(name.charAt(0)))
\r
140 b.append('(').append(name).append(')');
\r
144 public void visit(EEnforce expression) {
\r
145 b.append("enforce ");
\r
146 expression.getQuery().accept(this);
\r
150 public void visit(EError expression) {
\r
151 b.append("EError");
\r
155 public void visit(EExternalConstant expression) {
\r
156 b.append(expression.getValue());
\r
160 public void visit(EFieldAccess expression) {
\r
161 b.append("EFieldAccess");
\r
165 public void visit(EGetConstraint expression) {
\r
166 b.append("EGetConstraint");
\r
170 public void visit(EIf expression) {
\r
172 expression.getCondition().accept(this);
\r
176 expression.getThen().accept(this);
\r
179 expression.getElse().accept(this);
\r
184 public void visit(QIf query) {
\r
186 query.condition.accept(this);
\r
190 query.thenQuery.accept(this);
\r
193 query.elseQuery.accept(this);
\r
198 public void visit(EIntegerLiteral expression) {
\r
199 b.append(expression.getValue());
\r
203 public void visit(ELambda expression) {
\r
206 for(Case case_ : expression.getCases()) {
\r
208 for(Expression pat : case_.patterns) {
\r
214 case_.value.accept(this);
\r
221 public void visit(ELambdaType expression) {
\r
222 expression.value.accept(this);
\r
226 public void visit(ELet expression) {
\r
229 printAsDo(expression);
\r
234 public void visit(EListComprehension expression) {
\r
235 b.append("EListComprehension");
\r
239 public void visit(EListLiteral expression) {
\r
241 boolean first = true;
\r
242 for(Expression component : expression.getComponents()) {
\r
247 component.accept(this);
\r
253 public void visit(ELiteral expression) {
\r
254 b.append(expression.getValue().toString());
\r
258 public void visit(EMatch expression) {
\r
260 for(Expression s : expression.getScrutinee()) {
\r
266 for(Case case_ : expression.getCases()) {
\r
268 for(Expression pat : case_.patterns) {
\r
274 case_.value.accept(this);
\r
281 public void visit(EPlaceholder expression) {
\r
282 expression.expression.accept(this);
\r
286 public void visit(ERealLiteral expression) {
\r
287 b.append(expression.getValue());
\r
291 public void visit(ERuleset expression) {
\r
294 for(ERuleset.DatalogRule rule : expression.getRules()) {
\r
300 expression.getIn().accept(this);
\r
304 public void visit(ESelect expression) {
\r
305 b.append("ESelect");
\r
309 public void visit(ESimpleLambda expression) {
\r
311 show(expression.getParameter());
\r
312 while(expression.getValue() instanceof ESimpleLambda) {
\r
313 expression = (ESimpleLambda)expression.getValue();
\r
315 show(expression.getParameter());
\r
318 expression.getValue().accept(this);
\r
322 public void visit(ESimpleLet expression) {
\r
325 printAsDo(expression);
\r
329 private void printAsDo(Expression expression) {
\r
330 if(expression instanceof ESimpleLet) {
\r
331 ESimpleLet let = (ESimpleLet)expression;
\r
332 Variable variable = let.getVariable();
\r
333 Expression value = let.getValue();
\r
334 if("_".equals(variable.getName()))
\r
340 value.accept(this);
\r
342 printAsDo(let.getIn());
\r
344 else if(expression instanceof ELet) {
\r
345 ELet let = (ELet)expression;
\r
346 for(Assignment assignment : let.assignments) {
\r
348 assignment.pattern.accept(this);
\r
350 assignment.value.accept(this);
\r
356 expression.accept(this);
\r
361 public void visit(ETransformation expression) {
\r
362 b.append("<transformation>");
\r
366 public void visit(ETypeAnnotation expression) {
\r
367 expression.getValue().accept(this);
\r
371 public void visit(EVariable expression) {
\r
372 show(expression.getVariable());
\r
376 public void visit(EWhen expression) {
\r
378 expression.getQuery().accept(this);
\r
380 expression.getAction().accept(this);
\r
384 public void visit(GuardedExpressionGroup expression) {
\r
385 boolean first = true;
\r
386 for(GuardedExpression gexp : expression.expressions) {
\r
392 for(int i=0;i<gexp.guards.length;++i) {
\r
395 gexp.guards[i].accept(this);
\r
398 gexp.value.accept(this);
\r
403 public void visit(QAlternative query) {
\r
404 b.append("QAlternative");
\r
408 public void visit(QAtom query) {
\r
409 if(query.relation == EqRelation.INSTANCE) {
\r
410 query.parameters[0].accept(this);
\r
412 query.parameters[1].accept(this);
\r
414 else if(query.relation == MemberRelation.INSTANCE) {
\r
415 query.parameters[0].accept(this);
\r
417 query.parameters[1].accept(this);
\r
420 b.append(query.relation);
\r
421 for(Expression parameter : query.parameters) {
\r
423 showPar(parameter);
\r
429 public void visit(QConjunction query) {
\r
430 boolean first = true;
\r
431 for(Query q : query.queries) {
\r
441 public void visit(QDisjunction query) {
\r
442 b.append("QDisjunction");
\r
446 public void visit(QExists query) {
\r
447 b.append("QExists");
\r
451 public void visit(QNegation query) {
\r
452 b.append("QNegation");
\r
456 public void visit(QMapping query) {
\r
457 b.append(query.mappingRelation.name.name);
\r
458 for(Expression parameter : query.parameters) {
\r
460 parameter.accept(this);
\r
464 public void visit(ERuleset.DatalogRule rule) {
\r
465 b.append(rule.headRelation.getName());
\r
466 for(Expression parameter : rule.headParameters) {
\r
468 showPar(parameter);
\r
472 rule.body.accept(this);
\r
476 public void visit(TransformationRule rule) {
\r
477 b.append("rule ").append(rule.name.name).append(" where");
\r
478 for(Entry<SectionName, Query[]> section : rule.sections.entrySet()) {
\r
479 b.append("\n@").append(section.getKey().name());
\r
480 for(Query query : section.getValue()) {
\r
482 query.accept(this);
\r
489 public void visit(ECoveringBranchPoint expression) {
\r
490 expression.expression.accept(this);
\r
494 public void visit(EEquations eEquations) {
\r