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.CHRRule;
5 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
6 import org.simantics.scl.compiler.elaboration.equation.EqBasic;
7 import org.simantics.scl.compiler.elaboration.equation.EqGuard;
8 import org.simantics.scl.compiler.elaboration.equation.Equation;
9 import org.simantics.scl.compiler.elaboration.equation.EquationVisitor;
10 import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor;
11 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
17 import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor;
18 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
19 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
20 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
21 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
22 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierTransformer;
23 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
24 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
25 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
26 import org.simantics.scl.compiler.elaboration.query.QAlternative;
27 import org.simantics.scl.compiler.elaboration.query.QAtom;
28 import org.simantics.scl.compiler.elaboration.query.QConjunction;
29 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
30 import org.simantics.scl.compiler.elaboration.query.QExists;
31 import org.simantics.scl.compiler.elaboration.query.QIf;
32 import org.simantics.scl.compiler.elaboration.query.QMapping;
33 import org.simantics.scl.compiler.elaboration.query.QNegation;
34 import org.simantics.scl.compiler.elaboration.query.Query;
35 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
36 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
37 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
38 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
40 public class StandardExpressionTransformer implements
41 ExpressionTransformer, QueryTransformer, ListQualifierTransformer, StatementVisitor,
45 public Expression transform(EAmbiguous expression) {
50 public Expression transform(EApply expression) {
51 expression.function = expression.function.accept(this);
52 for(int i=0;i<expression.parameters.length;++i)
53 expression.parameters[i] = expression.parameters[i].accept(this);
58 public Expression transform(EApplyType expression) {
59 expression.expression = expression.expression.accept(this);
64 public Expression transform(EAsPattern expression) {
65 expression.pattern = expression.pattern.accept(this);
70 public Expression transform(EBinary expression) {
71 expression.left = expression.left.accept(this);
72 for(EBinaryRightSide rs : expression.rights)
73 rs.right = rs.right.accept(this);
78 public Expression transform(EBind expression) {
79 expression.pattern = expression.pattern.accept(this);
80 expression.value = expression.value.accept(this);
81 expression.in = expression.in.accept(this);
86 public Expression transform(EViewPattern expression) {
87 expression.expression = expression.expression.accept(this);
88 expression.pattern = expression.pattern.accept(this);
93 public Expression transform(EBlock expression) {
94 for(Statement statement : expression.statements)
95 statement.accept(this);
100 public void visit(BindStatement statement) {
101 statement.pattern = statement.pattern.accept(this);
102 statement.value = statement.value.accept(this);
106 public void visit(GuardStatement statement) {
107 statement.value = statement.value.accept(this);
111 public void visit(LetStatement statement) {
112 statement.pattern = statement.pattern.accept(this);
113 statement.value = statement.value.accept(this);
117 public void visit(RuleStatement statement) {
118 statement.head = statement.head.accept(this);
119 statement.body = statement.body.accept(this);
123 public void visit(IncludeStatement statement) {
124 statement.value = statement.value.accept(this);
127 public void transform(CHRRuleset ruleset) {
128 for(CHRRule rule : ruleset.rules) {
129 for(CHRLiteral lit : rule.head.literals)
130 for(int i=0;i<lit.parameters.length;++i)
131 lit.parameters[i] = lit.parameters[i].accept(this);
132 for(CHRLiteral lit : rule.body.literals)
133 for(int i=0;i<lit.parameters.length;++i)
134 lit.parameters[i] = lit.parameters[i].accept(this);
139 public Expression transform(ECHRRuleset expression) {
140 expression.in = expression.in.accept(this);
141 transform(expression.ruleset);
146 public Expression transform(ECHRRulesetConstructor expression) {
147 transform(expression.ruleset);
152 public Expression transform(EConstant expression) {
157 public Expression transform(EEnforce expression) {
158 expression.query = expression.query.accept(this);
163 public Expression transform(EError expression) {
168 public Expression transform(EExternalConstant expression) {
173 public Expression transform(EFieldAccess expression) {
174 expression.parent = expression.parent.accept(this);
175 if(expression.accessor instanceof ExpressionAccessor) {
176 ExpressionAccessor expAcc = (ExpressionAccessor)expression.accessor;
177 expAcc.fieldName = expAcc.fieldName.accept(this);
183 public Expression transform(EGetConstraint expression) {
188 public Expression transform(EIf expression) {
189 expression.condition = expression.condition.accept(this);
190 expression.then_ = expression.then_.accept(this);
191 if(expression.else_ != null)
192 expression.else_ = expression.else_.accept(this);
197 public Expression transform(EIntegerLiteral expression) {
201 protected void transformCases(Case[] cases) {
202 for(Case case_ : cases) {
203 for(int i=0;i<case_.patterns.length;++i)
204 case_.patterns[i] = case_.patterns[i].accept(this);
205 case_.value = case_.value.accept(this);
210 public Expression transform(ELambda expression) {
211 transformCases(expression.cases);
216 public Expression transform(ELambdaType expression) {
217 expression.value = expression.value.accept(this);
222 public Expression transform(ELet expression) {
223 expression.in = expression.in.accept(this);
224 for(Assignment assign : expression.assignments) {
225 assign.pattern = assign.pattern.accept(this);
226 assign.value = assign.value.accept(this);
232 public Expression transform(EListComprehension expression) {
233 expression.head = expression.head.accept(this);
234 expression.qualifier = expression.qualifier.accept(this);
239 public Expression transform(EListLiteral expression) {
240 for(int i=0;i<expression.components.length;++i)
241 expression.components[i] = expression.components[i].accept(this);
246 public Expression transform(ELiteral expression) {
251 public Expression transform(EMatch expression) {
252 for(int i=0;i<expression.scrutinee.length;++i)
253 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
254 transformCases(expression.cases);
259 public Expression transform(EPlaceholder expression) {
260 expression.expression = expression.expression.accept(this);
265 public Expression transform(EPreLet expression) {
266 for(LetStatement statement : expression.assignments) {
267 statement.pattern = statement.pattern.accept(this);
268 statement.value = statement.value.accept(this);
270 expression.in = expression.in.accept(this);
275 public Expression transform(EPreRuleset expression) {
276 for(RuleStatement rule : expression.statements) {
277 rule.body = rule.body.accept(this);
278 rule.head = rule.head.accept(this);
280 expression.in = expression.in.accept(this);
285 public Expression transform(ECoveringBranchPoint expression) {
286 expression.expression = expression.expression.accept(this);
291 public Expression transform(ERange expression) {
292 expression.from = expression.from.accept(this);
293 expression.to = expression.to.accept(this);
298 public Expression transform(ERealLiteral expression) {
303 public Expression transform(ERecord expression) {
304 for(FieldAssignment field : expression.fields)
305 if(field.value != null)
306 field.value = field.value.accept(this);
311 public Expression transform(ERuleset expression) {
312 for(ERuleset.DatalogRule rule : expression.rules) {
313 rule.body = rule.body.accept(this);
314 for(int i=0;i<rule.headParameters.length;++i)
315 rule.headParameters[i] = rule.headParameters[i].accept(this);
317 expression.in = expression.in.accept(this);
322 public Expression transform(ESelect expression) {
323 expression.query = expression.query.accept(this);
324 expression.expression = expression.expression.accept(this);
329 public Expression transform(ESimpleLambda expression) {
330 expression.value = expression.value.accept(this);
335 public Expression transform(ESimpleLet expression) {
336 expression.value = expression.value.accept(this);
337 expression.in = expression.in.accept(this);
342 public Expression transform(EStringLiteral expression) {
347 public Expression transform(ETransformation expression) {
348 expression.seed = expression.seed.accept(this);
353 public Expression transform(ETypeAnnotation expression) {
354 expression.value = expression.value.accept(this);
359 public Expression transform(EVar expression) {
364 public Expression transform(EVariable expression) {
369 public Expression transform(EWhen expression) {
370 expression.query = expression.query.accept(this);
371 expression.action = expression.action.accept(this);
376 public Expression transform(GuardedExpressionGroup expression) {
377 for(GuardedExpression ge : expression.expressions) {
378 for(int i=0;i<ge.guards.length;++i)
379 ge.guards[i] = ge.guards[i].accept(this);
380 ge.value = ge.value.accept(this);
386 public ListQualifier transform(ListAssignment qualifier) {
387 qualifier.pattern = qualifier.pattern.accept(this);
388 qualifier.value = qualifier.value.accept(this);
393 public ListQualifier transform(ListGenerator qualifier) {
394 qualifier.pattern = qualifier.pattern.accept(this);
395 qualifier.value = qualifier.value.accept(this);
400 public ListQualifier transform(ListGuard qualifier) {
401 qualifier.condition = qualifier.condition.accept(this);
406 public ListQualifier transform(ListSeq qualifier) {
407 qualifier.a = qualifier.a.accept(this);
408 qualifier.b = qualifier.b.accept(this);
413 public ListQualifier transform(ListThen qualifier) {
414 qualifier.left = qualifier.left.accept(this);
415 qualifier.transformer = qualifier.transformer.accept(this);
416 if(qualifier.by != null)
417 qualifier.by = qualifier.by.accept(this);
422 public Query transform(QAlternative query) {
423 for(int i=0;i<query.queries.length;++i)
424 query.queries[i] = query.queries[i].accept(this);
429 public Query transform(QAtom query) {
430 for(int i=0;i<query.parameters.length;++i)
431 query.parameters[i] = query.parameters[i].accept(this);
436 public Query transform(QConjunction query) {
437 for(int i=0;i<query.queries.length;++i)
438 query.queries[i] = query.queries[i].accept(this);
443 public Query transform(QDisjunction query) {
444 for(int i=0;i<query.queries.length;++i)
445 query.queries[i] = query.queries[i].accept(this);
450 public Query transform(QExists query) {
451 query.query = query.query.accept(this);
456 public Query transform(QIf query) {
457 query.condition = query.condition.accept(this);
458 query.thenQuery = query.thenQuery.accept(this);
459 query.elseQuery = query.elseQuery.accept(this);
464 public Query transform(QMapping query) {
465 for(int i=0;i<query.parameters.length;++i)
466 query.parameters[i] = query.parameters[i].accept(this);
471 public Query transform(QNegation query) {
472 query.query = query.query.accept(this);
477 public Query transform(QPreBinds query) {
478 query.left = query.left.accept(this);
479 query.right = query.right.accept(this);
484 public Query transform(QPreEquals query) {
485 query.left = query.left.accept(this);
486 query.right = query.right.accept(this);
491 public Query transform(QPreExists query) {
492 query.query = query.query.accept(this);
497 public Query transform(QPreGuard query) {
498 query.guard = query.guard.accept(this);
503 public Expression transform(EEquations expression) {
504 for(Equation equation : expression.equations)
505 equation.accept(this);
510 public void visit(EqBasic equation) {
511 equation.left = equation.left.accept(this);
512 equation.right = equation.right.accept(this);
516 public void visit(EqGuard equation) {
517 equation.guard = equation.guard.accept(this);