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.equation.EqBasic;
8 import org.simantics.scl.compiler.elaboration.equation.EqGuard;
9 import org.simantics.scl.compiler.elaboration.equation.Equation;
10 import org.simantics.scl.compiler.elaboration.equation.EquationVisitor;
11 import org.simantics.scl.compiler.elaboration.expressions.accessor.ExpressionAccessor;
12 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
17 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
18 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
19 import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor;
20 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
21 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
22 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
23 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
24 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierTransformer;
25 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
26 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
27 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
28 import org.simantics.scl.compiler.elaboration.query.QAlternative;
29 import org.simantics.scl.compiler.elaboration.query.QAtom;
30 import org.simantics.scl.compiler.elaboration.query.QConjunction;
31 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
32 import org.simantics.scl.compiler.elaboration.query.QExists;
33 import org.simantics.scl.compiler.elaboration.query.QIf;
34 import org.simantics.scl.compiler.elaboration.query.QMapping;
35 import org.simantics.scl.compiler.elaboration.query.QNegation;
36 import org.simantics.scl.compiler.elaboration.query.Query;
37 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
38 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
39 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
40 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
42 public class StandardExpressionTransformer implements
43 ExpressionTransformer, QueryTransformer, ListQualifierTransformer, StatementVisitor,
47 public Expression transform(EAmbiguous expression) {
48 if(expression.resolvedExpression != null)
49 expression.resolvedExpression = expression.resolvedExpression.accept(this);
54 public Expression transform(EApply expression) {
55 expression.function = expression.function.accept(this);
56 for(int i=0;i<expression.parameters.length;++i)
57 expression.parameters[i] = expression.parameters[i].accept(this);
62 public Expression transform(EApplyType expression) {
63 expression.expression = expression.expression.accept(this);
68 public Expression transform(EAsPattern expression) {
69 expression.pattern = expression.pattern.accept(this);
74 public Expression transform(EBinary expression) {
75 expression.left = expression.left.accept(this);
76 for(EBinaryRightSide rs : expression.rights)
77 rs.right = rs.right.accept(this);
82 public Expression transform(EBind expression) {
83 expression.pattern = expression.pattern.accept(this);
84 expression.value = expression.value.accept(this);
85 expression.in = expression.in.accept(this);
90 public Expression transform(EViewPattern expression) {
91 expression.expression = expression.expression.accept(this);
92 expression.pattern = expression.pattern.accept(this);
97 public Expression transform(EBlock expression) {
98 for(Statement statement : expression.statements)
99 statement.accept(this);
104 public void visit(BindStatement statement) {
105 statement.pattern = statement.pattern.accept(this);
106 statement.value = statement.value.accept(this);
110 public void visit(GuardStatement statement) {
111 statement.value = statement.value.accept(this);
115 public void visit(LetStatement statement) {
116 statement.pattern = statement.pattern.accept(this);
117 statement.value = statement.value.accept(this);
121 public void visit(RuleStatement statement) {
122 statement.head = statement.head.accept(this);
123 statement.body = statement.body.accept(this);
127 public void visit(IncludeStatement statement) {
128 statement.value = statement.value.accept(this);
131 public void transform(CHRQuery query) {
132 for(CHRLiteral lit : query.literals)
133 for(int i=0;i<lit.parameters.length;++i)
134 lit.parameters[i] = lit.parameters[i].accept(this);
137 public void transform(CHRRuleset ruleset) {
138 for(CHRRule rule : ruleset.rules) {
139 transform(rule.head);
140 transform(rule.body);
145 public Expression transform(ECHRRuleset expression) {
146 expression.in = expression.in.accept(this);
147 transform(expression.ruleset);
152 public Expression transform(ECHRSelect expression) {
153 expression.expression = expression.expression.accept(this);
154 transform(expression.query);
159 public Expression transform(ECHRRulesetConstructor expression) {
160 transform(expression.ruleset);
165 public Expression transform(EConstant expression) {
170 public Expression transform(EEnforce expression) {
171 expression.query = expression.query.accept(this);
176 public Expression transform(EError expression) {
181 public Expression transform(EExternalConstant expression) {
186 public Expression transform(EFieldAccess expression) {
187 expression.parent = expression.parent.accept(this);
188 if(expression.accessor instanceof ExpressionAccessor) {
189 ExpressionAccessor expAcc = (ExpressionAccessor)expression.accessor;
190 expAcc.fieldName = expAcc.fieldName.accept(this);
196 public Expression transform(EGetConstraint expression) {
201 public Expression transform(EIf expression) {
202 expression.condition = expression.condition.accept(this);
203 expression.then_ = expression.then_.accept(this);
204 if(expression.else_ != null)
205 expression.else_ = expression.else_.accept(this);
210 public Expression transform(EIntegerLiteral expression) {
214 protected void transformCases(Case[] cases) {
215 for(Case case_ : cases) {
216 for(int i=0;i<case_.patterns.length;++i)
217 case_.patterns[i] = case_.patterns[i].accept(this);
218 case_.value = case_.value.accept(this);
223 public Expression transform(ELambda expression) {
224 transformCases(expression.cases);
229 public Expression transform(ELambdaType expression) {
230 expression.value = expression.value.accept(this);
235 public Expression transform(ELet expression) {
236 expression.in = expression.in.accept(this);
237 for(Assignment assign : expression.assignments) {
238 assign.pattern = assign.pattern.accept(this);
239 assign.value = assign.value.accept(this);
245 public Expression transform(EListComprehension expression) {
246 expression.head = expression.head.accept(this);
247 expression.qualifier = expression.qualifier.accept(this);
252 public Expression transform(EListLiteral expression) {
253 for(int i=0;i<expression.components.length;++i)
254 expression.components[i] = expression.components[i].accept(this);
259 public Expression transform(ELiteral expression) {
264 public Expression transform(EMatch expression) {
265 for(int i=0;i<expression.scrutinee.length;++i)
266 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
267 transformCases(expression.cases);
272 public Expression transform(EPlaceholder expression) {
273 expression.expression = expression.expression.accept(this);
278 public Expression transform(EPreLet expression) {
279 for(LetStatement statement : expression.assignments) {
280 statement.pattern = statement.pattern.accept(this);
281 statement.value = statement.value.accept(this);
283 expression.in = expression.in.accept(this);
288 public Expression transform(EPreRuleset expression) {
289 for(RuleStatement rule : expression.statements) {
290 rule.body = rule.body.accept(this);
291 rule.head = rule.head.accept(this);
293 expression.in = expression.in.accept(this);
298 public Expression transform(ECoveringBranchPoint expression) {
299 expression.expression = expression.expression.accept(this);
304 public Expression transform(ERange expression) {
305 expression.from = expression.from.accept(this);
306 expression.to = expression.to.accept(this);
311 public Expression transform(ERealLiteral expression) {
316 public Expression transform(ERecord expression) {
317 for(FieldAssignment field : expression.fields)
318 if(field.value != null)
319 field.value = field.value.accept(this);
324 public Expression transform(ERuleset expression) {
325 for(ERuleset.DatalogRule rule : expression.rules) {
326 rule.body = rule.body.accept(this);
327 for(int i=0;i<rule.headParameters.length;++i)
328 rule.headParameters[i] = rule.headParameters[i].accept(this);
330 expression.in = expression.in.accept(this);
335 public Expression transform(ESelect expression) {
336 expression.query = expression.query.accept(this);
337 expression.expression = expression.expression.accept(this);
342 public Expression transform(ESimpleLambda expression) {
343 expression.value = expression.value.accept(this);
348 public Expression transform(ESimpleLet expression) {
349 expression.value = expression.value.accept(this);
350 expression.in = expression.in.accept(this);
355 public Expression transform(EStringLiteral expression) {
360 public Expression transform(ETransformation expression) {
361 expression.seed = expression.seed.accept(this);
366 public Expression transform(ETypeAnnotation expression) {
367 expression.value = expression.value.accept(this);
372 public Expression transform(EVar expression) {
377 public Expression transform(EVariable expression) {
382 public Expression transform(EWhen expression) {
383 expression.query = expression.query.accept(this);
384 expression.action = expression.action.accept(this);
389 public Expression transform(GuardedExpressionGroup expression) {
390 for(GuardedExpression ge : expression.expressions) {
391 for(int i=0;i<ge.guards.length;++i)
392 ge.guards[i] = ge.guards[i].accept(this);
393 ge.value = ge.value.accept(this);
399 public ListQualifier transform(ListAssignment qualifier) {
400 qualifier.pattern = qualifier.pattern.accept(this);
401 qualifier.value = qualifier.value.accept(this);
406 public ListQualifier transform(ListGenerator qualifier) {
407 qualifier.pattern = qualifier.pattern.accept(this);
408 qualifier.value = qualifier.value.accept(this);
413 public ListQualifier transform(ListGuard qualifier) {
414 qualifier.condition = qualifier.condition.accept(this);
419 public ListQualifier transform(ListSeq qualifier) {
420 qualifier.a = qualifier.a.accept(this);
421 qualifier.b = qualifier.b.accept(this);
426 public ListQualifier transform(ListThen qualifier) {
427 qualifier.left = qualifier.left.accept(this);
428 qualifier.transformer = qualifier.transformer.accept(this);
429 if(qualifier.by != null)
430 qualifier.by = qualifier.by.accept(this);
435 public Query transform(QAlternative query) {
436 for(int i=0;i<query.queries.length;++i)
437 query.queries[i] = query.queries[i].accept(this);
442 public Query transform(QAtom query) {
443 for(int i=0;i<query.parameters.length;++i)
444 query.parameters[i] = query.parameters[i].accept(this);
449 public Query transform(QConjunction query) {
450 for(int i=0;i<query.queries.length;++i)
451 query.queries[i] = query.queries[i].accept(this);
456 public Query transform(QDisjunction query) {
457 for(int i=0;i<query.queries.length;++i)
458 query.queries[i] = query.queries[i].accept(this);
463 public Query transform(QExists query) {
464 query.query = query.query.accept(this);
469 public Query transform(QIf query) {
470 query.condition = query.condition.accept(this);
471 query.thenQuery = query.thenQuery.accept(this);
472 query.elseQuery = query.elseQuery.accept(this);
477 public Query transform(QMapping query) {
478 for(int i=0;i<query.parameters.length;++i)
479 query.parameters[i] = query.parameters[i].accept(this);
484 public Query transform(QNegation query) {
485 query.query = query.query.accept(this);
490 public Query transform(QPreBinds query) {
491 query.left = query.left.accept(this);
492 query.right = query.right.accept(this);
497 public Query transform(QPreEquals query) {
498 query.left = query.left.accept(this);
499 query.right = query.right.accept(this);
504 public Query transform(QPreExists query) {
505 query.query = query.query.accept(this);
510 public Query transform(QPreGuard query) {
511 query.guard = query.guard.accept(this);
516 public Expression transform(EEquations expression) {
517 for(Equation equation : expression.equations)
518 equation.accept(this);
523 public void visit(EqBasic equation) {
524 equation.left = equation.left.accept(this);
525 equation.right = equation.right.accept(this);
529 public void visit(EqGuard equation) {
530 equation.guard = equation.guard.accept(this);
534 public void visit(CHRStatement statement) {
535 for(int i=0;i<statement.body.length;++i)
536 statement.body[i] = statement.body[i].accept(this);
537 for(int i=0;i<statement.head.length;++i)
538 statement.head[i] = statement.head[i].accept(this);