1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.elaboration.chr.CHRLiteral;
4 import org.simantics.scl.compiler.elaboration.chr.CHRQuery;
5 import org.simantics.scl.compiler.elaboration.chr.CHRRule;
6 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
7 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstAtom;
8 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstBinds;
9 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstConjunction;
10 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstEquals;
11 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstNegation;
12 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstQuery;
13 import org.simantics.scl.compiler.elaboration.chr.ast.CHRAstQueryVisitor;
14 import org.simantics.scl.compiler.elaboration.equation.EqBasic;
15 import org.simantics.scl.compiler.elaboration.equation.EqGuard;
16 import org.simantics.scl.compiler.elaboration.equation.Equation;
17 import org.simantics.scl.compiler.elaboration.equation.EquationVisitor;
18 import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor;
19 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
20 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
21 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
22 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
23 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
24 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
25 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
26 import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor;
27 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
28 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
29 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
30 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
31 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierTransformer;
32 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
33 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
34 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
35 import org.simantics.scl.compiler.elaboration.query.QAlternative;
36 import org.simantics.scl.compiler.elaboration.query.QAtom;
37 import org.simantics.scl.compiler.elaboration.query.QConjunction;
38 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
39 import org.simantics.scl.compiler.elaboration.query.QExists;
40 import org.simantics.scl.compiler.elaboration.query.QIf;
41 import org.simantics.scl.compiler.elaboration.query.QMapping;
42 import org.simantics.scl.compiler.elaboration.query.QNegation;
43 import org.simantics.scl.compiler.elaboration.query.Query;
44 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
45 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
46 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
47 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
49 public class StandardExpressionTransformer implements
50 ExpressionTransformer, QueryTransformer, ListQualifierTransformer, StatementVisitor,
51 EquationVisitor, CHRAstQueryVisitor {
54 public Expression transform(EAmbiguous expression) {
55 if(expression.resolvedExpression != null)
56 expression.resolvedExpression = expression.resolvedExpression.accept(this);
61 public Expression transform(EApply expression) {
62 expression.function = expression.function.accept(this);
63 for(int i=0;i<expression.parameters.length;++i)
64 expression.parameters[i] = expression.parameters[i].accept(this);
69 public Expression transform(EApplyType expression) {
70 expression.expression = expression.expression.accept(this);
75 public Expression transform(EAsPattern expression) {
76 expression.pattern = expression.pattern.accept(this);
81 public Expression transform(EBinary expression) {
82 expression.left = expression.left.accept(this);
83 for(EBinaryRightSide rs : expression.rights)
84 rs.right = rs.right.accept(this);
89 public Expression transform(EBind expression) {
90 expression.pattern = expression.pattern.accept(this);
91 expression.value = expression.value.accept(this);
92 expression.in = expression.in.accept(this);
97 public Expression transform(EViewPattern expression) {
98 expression.expression = expression.expression.accept(this);
99 expression.pattern = expression.pattern.accept(this);
104 public Expression transform(EBlock expression) {
105 for(Statement statement : expression.statements)
106 statement.accept(this);
111 public void visit(BindStatement statement) {
112 statement.pattern = statement.pattern.accept(this);
113 statement.value = statement.value.accept(this);
117 public void visit(GuardStatement statement) {
118 statement.value = statement.value.accept(this);
122 public void visit(LetStatement statement) {
123 statement.pattern = statement.pattern.accept(this);
124 statement.value = statement.value.accept(this);
128 public void visit(RuleStatement statement) {
129 statement.head = statement.head.accept(this);
130 statement.body = statement.body.accept(this);
134 public void visit(IncludeStatement statement) {
135 statement.value = statement.value.accept(this);
138 public void transform(CHRQuery query) {
139 for(CHRLiteral lit : query.literals)
140 for(int i=0;i<lit.parameters.length;++i)
141 lit.parameters[i] = lit.parameters[i].accept(this);
144 public void transform(CHRRuleset ruleset) {
145 for(CHRRule rule : ruleset.rules) {
146 transform(rule.head);
147 transform(rule.body);
152 public Expression transform(ECHRRuleset expression) {
153 expression.in = expression.in.accept(this);
154 transform(expression.ruleset);
159 public Expression transform(ECHRSelect expression) {
160 expression.expression = expression.expression.accept(this);
161 transform(expression.query);
166 public Expression transform(ECHRRulesetConstructor expression) {
167 transform(expression.ruleset);
172 public Expression transform(EConstant expression) {
177 public Expression transform(EEnforce expression) {
178 expression.query = expression.query.accept(this);
183 public Expression transform(EError expression) {
188 public Expression transform(EExternalConstant expression) {
193 public Expression transform(EFieldAccess expression) {
194 expression.parent = expression.parent.accept(this);
195 if(expression.accessor instanceof ExpressionAccessor) {
196 ExpressionAccessor expAcc = (ExpressionAccessor)expression.accessor;
197 expAcc.fieldName = expAcc.fieldName.accept(this);
203 public Expression transform(EGetConstraint expression) {
208 public Expression transform(EIf expression) {
209 expression.condition = expression.condition.accept(this);
210 expression.then_ = expression.then_.accept(this);
211 if(expression.else_ != null)
212 expression.else_ = expression.else_.accept(this);
217 public Expression transform(EIntegerLiteral expression) {
221 protected void transformCases(Case[] cases) {
222 for(Case case_ : cases) {
223 for(int i=0;i<case_.patterns.length;++i)
224 case_.patterns[i] = case_.patterns[i].accept(this);
225 case_.value = case_.value.accept(this);
230 public Expression transform(ELambda expression) {
231 transformCases(expression.cases);
236 public Expression transform(ELambdaType expression) {
237 expression.value = expression.value.accept(this);
242 public Expression transform(ELet expression) {
243 expression.in = expression.in.accept(this);
244 for(Assignment assign : expression.assignments) {
245 assign.pattern = assign.pattern.accept(this);
246 assign.value = assign.value.accept(this);
252 public Expression transform(EListComprehension expression) {
253 expression.head = expression.head.accept(this);
254 expression.qualifier = expression.qualifier.accept(this);
259 public Expression transform(EListLiteral expression) {
260 for(int i=0;i<expression.components.length;++i)
261 expression.components[i] = expression.components[i].accept(this);
266 public Expression transform(ELiteral expression) {
271 public Expression transform(EMatch expression) {
272 for(int i=0;i<expression.scrutinee.length;++i)
273 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
274 transformCases(expression.cases);
279 public Expression transform(EPlaceholder expression) {
280 expression.expression = expression.expression.accept(this);
285 public Expression transform(EPreLet expression) {
286 for(LetStatement statement : expression.assignments) {
287 statement.pattern = statement.pattern.accept(this);
288 statement.value = statement.value.accept(this);
290 expression.in = expression.in.accept(this);
295 public Expression transform(EPreRuleset expression) {
296 for(RuleStatement rule : expression.statements) {
297 rule.body = rule.body.accept(this);
298 rule.head = rule.head.accept(this);
300 expression.in = expression.in.accept(this);
305 public Expression transform(ECoveringBranchPoint expression) {
306 expression.expression = expression.expression.accept(this);
311 public Expression transform(ERange expression) {
312 expression.from = expression.from.accept(this);
313 expression.to = expression.to.accept(this);
318 public Expression transform(ERealLiteral expression) {
323 public Expression transform(ERecord expression) {
324 for(FieldAssignment field : expression.fields)
325 if(field.value != null)
326 field.value = field.value.accept(this);
331 public Expression transform(ERuleset expression) {
332 for(ERuleset.DatalogRule rule : expression.rules) {
333 rule.body = rule.body.accept(this);
334 for(int i=0;i<rule.headParameters.length;++i)
335 rule.headParameters[i] = rule.headParameters[i].accept(this);
337 expression.in = expression.in.accept(this);
342 public Expression transform(ESelect expression) {
343 expression.query = expression.query.accept(this);
344 expression.expression = expression.expression.accept(this);
349 public Expression transform(ESimpleLambda expression) {
350 expression.value = expression.value.accept(this);
355 public Expression transform(ESimpleLet expression) {
356 expression.value = expression.value.accept(this);
357 expression.in = expression.in.accept(this);
362 public Expression transform(EStringLiteral expression) {
367 public Expression transform(ETransformation expression) {
368 expression.seed = expression.seed.accept(this);
373 public Expression transform(ETypeAnnotation expression) {
374 expression.value = expression.value.accept(this);
379 public Expression transform(EVar expression) {
384 public Expression transform(EVariable expression) {
389 public Expression transform(EWhen expression) {
390 expression.query = expression.query.accept(this);
391 expression.action = expression.action.accept(this);
396 public Expression transform(GuardedExpressionGroup expression) {
397 for(GuardedExpression ge : expression.expressions) {
398 for(int i=0;i<ge.guards.length;++i)
399 ge.guards[i] = ge.guards[i].accept(this);
400 ge.value = ge.value.accept(this);
406 public ListQualifier transform(ListAssignment qualifier) {
407 qualifier.pattern = qualifier.pattern.accept(this);
408 qualifier.value = qualifier.value.accept(this);
413 public ListQualifier transform(ListGenerator qualifier) {
414 qualifier.pattern = qualifier.pattern.accept(this);
415 qualifier.value = qualifier.value.accept(this);
420 public ListQualifier transform(ListGuard qualifier) {
421 qualifier.condition = qualifier.condition.accept(this);
426 public ListQualifier transform(ListSeq qualifier) {
427 qualifier.a = qualifier.a.accept(this);
428 qualifier.b = qualifier.b.accept(this);
433 public ListQualifier transform(ListThen qualifier) {
434 qualifier.left = qualifier.left.accept(this);
435 qualifier.transformer = qualifier.transformer.accept(this);
436 if(qualifier.by != null)
437 qualifier.by = qualifier.by.accept(this);
442 public Query transform(QAlternative query) {
443 for(int i=0;i<query.queries.length;++i)
444 query.queries[i] = query.queries[i].accept(this);
449 public Query transform(QAtom query) {
450 for(int i=0;i<query.parameters.length;++i)
451 query.parameters[i] = query.parameters[i].accept(this);
456 public Query transform(QConjunction query) {
457 for(int i=0;i<query.queries.length;++i)
458 query.queries[i] = query.queries[i].accept(this);
463 public Query transform(QDisjunction query) {
464 for(int i=0;i<query.queries.length;++i)
465 query.queries[i] = query.queries[i].accept(this);
470 public Query transform(QExists query) {
471 query.query = query.query.accept(this);
476 public Query transform(QIf query) {
477 query.condition = query.condition.accept(this);
478 query.thenQuery = query.thenQuery.accept(this);
479 query.elseQuery = query.elseQuery.accept(this);
484 public Query transform(QMapping query) {
485 for(int i=0;i<query.parameters.length;++i)
486 query.parameters[i] = query.parameters[i].accept(this);
491 public Query transform(QNegation query) {
492 query.query = query.query.accept(this);
497 public Query transform(QPreBinds query) {
498 query.left = query.left.accept(this);
499 query.right = query.right.accept(this);
504 public Query transform(QPreEquals query) {
505 query.left = query.left.accept(this);
506 query.right = query.right.accept(this);
511 public Query transform(QPreExists query) {
512 query.query = query.query.accept(this);
517 public Query transform(QPreGuard query) {
518 query.guard = query.guard.accept(this);
523 public Expression transform(EEquations expression) {
524 for(Equation equation : expression.equations)
525 equation.accept(this);
530 public void visit(EqBasic equation) {
531 equation.left = equation.left.accept(this);
532 equation.right = equation.right.accept(this);
536 public void visit(EqGuard equation) {
537 equation.guard = equation.guard.accept(this);
541 public void visit(CHRStatement statement) {
542 statement.head.accept(this);
543 statement.body.accept(this);
547 public void visit(CHRAstAtom query) {
548 query.expression = query.expression.accept(this);
552 public void visit(CHRAstBinds query) {
553 query.left = query.left.accept(this);
554 query.right = query.right.accept(this);
558 public void visit(CHRAstConjunction query) {
559 for(CHRAstQuery conjunct : query.conjuncts)
560 conjunct.accept(this);
564 public void visit(CHRAstEquals query) {
565 query.left = query.left.accept(this);
566 query.right = query.right.accept(this);
570 public void visit(CHRAstNegation query) {
571 query.subquery.accept(this);