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.equation.EqBasic;
6 import org.simantics.scl.compiler.elaboration.equation.EqGuard;
7 import org.simantics.scl.compiler.elaboration.equation.Equation;
8 import org.simantics.scl.compiler.elaboration.equation.EquationVisitor;
9 import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor;
10 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
11 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor;
16 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
17 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
18 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
19 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
20 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierTransformer;
21 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
22 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
23 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
24 import org.simantics.scl.compiler.elaboration.query.QAlternative;
25 import org.simantics.scl.compiler.elaboration.query.QAtom;
26 import org.simantics.scl.compiler.elaboration.query.QConjunction;
27 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
28 import org.simantics.scl.compiler.elaboration.query.QExists;
29 import org.simantics.scl.compiler.elaboration.query.QIf;
30 import org.simantics.scl.compiler.elaboration.query.QMapping;
31 import org.simantics.scl.compiler.elaboration.query.QNegation;
32 import org.simantics.scl.compiler.elaboration.query.Query;
33 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
34 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
35 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
36 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
38 public class StandardExpressionTransformer implements
39 ExpressionTransformer, QueryTransformer, ListQualifierTransformer, StatementVisitor,
43 public Expression transform(EAmbiguous expression) {
48 public Expression transform(EApply expression) {
49 expression.function = expression.function.accept(this);
50 for(int i=0;i<expression.parameters.length;++i)
51 expression.parameters[i] = expression.parameters[i].accept(this);
56 public Expression transform(EApplyType expression) {
57 expression.expression = expression.expression.accept(this);
62 public Expression transform(EAsPattern expression) {
63 expression.pattern = expression.pattern.accept(this);
68 public Expression transform(EBinary expression) {
69 expression.left = expression.left.accept(this);
70 for(EBinaryRightSide rs : expression.rights)
71 rs.right = rs.right.accept(this);
76 public Expression transform(EBind expression) {
77 expression.pattern = expression.pattern.accept(this);
78 expression.value = expression.value.accept(this);
79 expression.in = expression.in.accept(this);
84 public Expression transform(EViewPattern expression) {
85 expression.expression = expression.expression.accept(this);
86 expression.pattern = expression.pattern.accept(this);
91 public Expression transform(EBlock expression) {
92 for(Statement statement : expression.statements)
93 statement.accept(this);
98 public void visit(BindStatement statement) {
99 statement.pattern = statement.pattern.accept(this);
100 statement.value = statement.value.accept(this);
104 public void visit(GuardStatement statement) {
105 statement.value = statement.value.accept(this);
109 public void visit(LetStatement statement) {
110 statement.pattern = statement.pattern.accept(this);
111 statement.value = statement.value.accept(this);
115 public void visit(RuleStatement statement) {
116 statement.head = statement.head.accept(this);
117 statement.body = statement.body.accept(this);
121 public Expression transform(ECHRRuleset expression) {
122 expression.in = expression.in.accept(this);
123 for(CHRRule rule : expression.ruleset.rules) {
124 for(CHRLiteral lit : rule.head.literals)
125 for(int i=0;i<lit.parameters.length;++i)
126 lit.parameters[i] = lit.parameters[i].accept(this);
127 for(CHRLiteral lit : rule.body.literals)
128 for(int i=0;i<lit.parameters.length;++i)
129 lit.parameters[i] = lit.parameters[i].accept(this);
135 public Expression transform(EConstant expression) {
140 public Expression transform(EEnforce expression) {
141 expression.query = expression.query.accept(this);
146 public Expression transform(EError expression) {
151 public Expression transform(EExternalConstant expression) {
156 public Expression transform(EFieldAccess expression) {
157 expression.parent = expression.parent.accept(this);
158 if(expression.accessor instanceof ExpressionAccessor) {
159 ExpressionAccessor expAcc = (ExpressionAccessor)expression.accessor;
160 expAcc.fieldName = expAcc.fieldName.accept(this);
166 public Expression transform(EGetConstraint expression) {
171 public Expression transform(EIf expression) {
172 expression.condition = expression.condition.accept(this);
173 expression.then_ = expression.then_.accept(this);
174 if(expression.else_ != null)
175 expression.else_ = expression.else_.accept(this);
180 public Expression transform(EIntegerLiteral expression) {
184 protected void transformCases(Case[] cases) {
185 for(Case case_ : cases) {
186 for(int i=0;i<case_.patterns.length;++i)
187 case_.patterns[i] = case_.patterns[i].accept(this);
188 case_.value = case_.value.accept(this);
193 public Expression transform(ELambda expression) {
194 transformCases(expression.cases);
199 public Expression transform(ELambdaType expression) {
200 expression.value = expression.value.accept(this);
205 public Expression transform(ELet expression) {
206 expression.in = expression.in.accept(this);
207 for(Assignment assign : expression.assignments) {
208 assign.pattern = assign.pattern.accept(this);
209 assign.value = assign.value.accept(this);
215 public Expression transform(EListComprehension expression) {
216 expression.head = expression.head.accept(this);
217 expression.qualifier = expression.qualifier.accept(this);
222 public Expression transform(EListLiteral expression) {
223 for(int i=0;i<expression.components.length;++i)
224 expression.components[i] = expression.components[i].accept(this);
229 public Expression transform(ELiteral expression) {
234 public Expression transform(EMatch expression) {
235 for(int i=0;i<expression.scrutinee.length;++i)
236 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
237 transformCases(expression.cases);
242 public Expression transform(EPlaceholder expression) {
243 expression.expression = expression.expression.accept(this);
248 public Expression transform(EPreLet expression) {
249 for(LetStatement statement : expression.assignments) {
250 statement.pattern = statement.pattern.accept(this);
251 statement.value = statement.value.accept(this);
253 expression.in = expression.in.accept(this);
258 public Expression transform(EPreRuleset expression) {
259 for(RuleStatement rule : expression.statements) {
260 rule.body = rule.body.accept(this);
261 rule.head = rule.head.accept(this);
263 expression.in = expression.in.accept(this);
268 public Expression transform(ECoveringBranchPoint expression) {
269 expression.expression = expression.expression.accept(this);
274 public Expression transform(ERange expression) {
275 expression.from = expression.from.accept(this);
276 expression.to = expression.to.accept(this);
281 public Expression transform(ERealLiteral expression) {
286 public Expression transform(ERecord expression) {
287 for(FieldAssignment field : expression.fields)
288 if(field.value != null)
289 field.value = field.value.accept(this);
294 public Expression transform(ERuleset expression) {
295 for(ERuleset.DatalogRule rule : expression.rules) {
296 rule.body = rule.body.accept(this);
297 for(int i=0;i<rule.headParameters.length;++i)
298 rule.headParameters[i] = rule.headParameters[i].accept(this);
300 expression.in = expression.in.accept(this);
305 public Expression transform(ESelect expression) {
306 expression.query = expression.query.accept(this);
307 expression.expression = expression.expression.accept(this);
312 public Expression transform(ESimpleLambda expression) {
313 expression.value = expression.value.accept(this);
318 public Expression transform(ESimpleLet expression) {
319 expression.value = expression.value.accept(this);
320 expression.in = expression.in.accept(this);
325 public Expression transform(EStringLiteral expression) {
330 public Expression transform(ETransformation expression) {
331 expression.seed = expression.seed.accept(this);
336 public Expression transform(ETypeAnnotation expression) {
337 expression.value = expression.value.accept(this);
342 public Expression transform(EVar expression) {
347 public Expression transform(EVariable expression) {
352 public Expression transform(EWhen expression) {
353 expression.query = expression.query.accept(this);
354 expression.action = expression.action.accept(this);
359 public Expression transform(GuardedExpressionGroup expression) {
360 for(GuardedExpression ge : expression.expressions) {
361 for(int i=0;i<ge.guards.length;++i)
362 ge.guards[i] = ge.guards[i].accept(this);
363 ge.value = ge.value.accept(this);
369 public ListQualifier transform(ListAssignment qualifier) {
370 qualifier.pattern = qualifier.pattern.accept(this);
371 qualifier.value = qualifier.value.accept(this);
376 public ListQualifier transform(ListGenerator qualifier) {
377 qualifier.pattern = qualifier.pattern.accept(this);
378 qualifier.value = qualifier.value.accept(this);
383 public ListQualifier transform(ListGuard qualifier) {
384 qualifier.condition = qualifier.condition.accept(this);
389 public ListQualifier transform(ListSeq qualifier) {
390 qualifier.a = qualifier.a.accept(this);
391 qualifier.b = qualifier.b.accept(this);
396 public ListQualifier transform(ListThen qualifier) {
397 qualifier.left = qualifier.left.accept(this);
398 qualifier.transformer = qualifier.transformer.accept(this);
399 if(qualifier.by != null)
400 qualifier.by = qualifier.by.accept(this);
405 public Query transform(QAlternative query) {
406 for(int i=0;i<query.queries.length;++i)
407 query.queries[i] = query.queries[i].accept(this);
412 public Query transform(QAtom query) {
413 for(int i=0;i<query.parameters.length;++i)
414 query.parameters[i] = query.parameters[i].accept(this);
419 public Query transform(QConjunction query) {
420 for(int i=0;i<query.queries.length;++i)
421 query.queries[i] = query.queries[i].accept(this);
426 public Query transform(QDisjunction query) {
427 for(int i=0;i<query.queries.length;++i)
428 query.queries[i] = query.queries[i].accept(this);
433 public Query transform(QExists query) {
434 query.query = query.query.accept(this);
439 public Query transform(QIf query) {
440 query.condition = query.condition.accept(this);
441 query.thenQuery = query.thenQuery.accept(this);
442 query.elseQuery = query.elseQuery.accept(this);
447 public Query transform(QMapping query) {
448 for(int i=0;i<query.parameters.length;++i)
449 query.parameters[i] = query.parameters[i].accept(this);
454 public Query transform(QNegation query) {
455 query.query = query.query.accept(this);
460 public Query transform(QPreBinds query) {
461 query.left = query.left.accept(this);
462 query.right = query.right.accept(this);
467 public Query transform(QPreEquals query) {
468 query.left = query.left.accept(this);
469 query.right = query.right.accept(this);
474 public Query transform(QPreExists query) {
475 query.query = query.query.accept(this);
480 public Query transform(QPreGuard query) {
481 query.guard = query.guard.accept(this);
486 public Expression transform(EEquations expression) {
487 for(Equation equation : expression.equations)
488 equation.accept(this);
493 public void visit(EqBasic equation) {
494 equation.left = equation.left.accept(this);
495 equation.right = equation.right.accept(this);
499 public void visit(EqGuard equation) {
500 equation.guard = equation.guard.accept(this);