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.CHRStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
17 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
18 import org.simantics.scl.compiler.elaboration.expressions.block.StatementVisitor;
19 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
20 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
21 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
22 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
23 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifierTransformer;
24 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
25 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
26 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
27 import org.simantics.scl.compiler.elaboration.query.QAlternative;
28 import org.simantics.scl.compiler.elaboration.query.QAtom;
29 import org.simantics.scl.compiler.elaboration.query.QConjunction;
30 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
31 import org.simantics.scl.compiler.elaboration.query.QExists;
32 import org.simantics.scl.compiler.elaboration.query.QIf;
33 import org.simantics.scl.compiler.elaboration.query.QMapping;
34 import org.simantics.scl.compiler.elaboration.query.QNegation;
35 import org.simantics.scl.compiler.elaboration.query.Query;
36 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
37 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
38 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
39 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
41 public class StandardExpressionTransformer implements
42 ExpressionTransformer, QueryTransformer, ListQualifierTransformer, StatementVisitor,
46 public Expression transform(EAmbiguous expression) {
51 public Expression transform(EApply expression) {
52 expression.function = expression.function.accept(this);
53 for(int i=0;i<expression.parameters.length;++i)
54 expression.parameters[i] = expression.parameters[i].accept(this);
59 public Expression transform(EApplyType expression) {
60 expression.expression = expression.expression.accept(this);
65 public Expression transform(EAsPattern expression) {
66 expression.pattern = expression.pattern.accept(this);
71 public Expression transform(EBinary expression) {
72 expression.left = expression.left.accept(this);
73 for(EBinaryRightSide rs : expression.rights)
74 rs.right = rs.right.accept(this);
79 public Expression transform(EBind expression) {
80 expression.pattern = expression.pattern.accept(this);
81 expression.value = expression.value.accept(this);
82 expression.in = expression.in.accept(this);
87 public Expression transform(EViewPattern expression) {
88 expression.expression = expression.expression.accept(this);
89 expression.pattern = expression.pattern.accept(this);
94 public Expression transform(EBlock expression) {
95 for(Statement statement : expression.statements)
96 statement.accept(this);
101 public void visit(BindStatement statement) {
102 statement.pattern = statement.pattern.accept(this);
103 statement.value = statement.value.accept(this);
107 public void visit(GuardStatement statement) {
108 statement.value = statement.value.accept(this);
112 public void visit(LetStatement statement) {
113 statement.pattern = statement.pattern.accept(this);
114 statement.value = statement.value.accept(this);
118 public void visit(RuleStatement statement) {
119 statement.head = statement.head.accept(this);
120 statement.body = statement.body.accept(this);
124 public void visit(IncludeStatement statement) {
125 statement.value = statement.value.accept(this);
128 public void transform(CHRRuleset ruleset) {
129 for(CHRRule rule : ruleset.rules) {
130 for(CHRLiteral lit : rule.head.literals)
131 for(int i=0;i<lit.parameters.length;++i)
132 lit.parameters[i] = lit.parameters[i].accept(this);
133 for(CHRLiteral lit : rule.body.literals)
134 for(int i=0;i<lit.parameters.length;++i)
135 lit.parameters[i] = lit.parameters[i].accept(this);
140 public Expression transform(ECHRRuleset expression) {
141 expression.in = expression.in.accept(this);
142 transform(expression.ruleset);
147 public Expression transform(ECHRRulesetConstructor expression) {
148 transform(expression.ruleset);
153 public Expression transform(EConstant expression) {
158 public Expression transform(EEnforce expression) {
159 expression.query = expression.query.accept(this);
164 public Expression transform(EError expression) {
169 public Expression transform(EExternalConstant expression) {
174 public Expression transform(EFieldAccess expression) {
175 expression.parent = expression.parent.accept(this);
176 if(expression.accessor instanceof ExpressionAccessor) {
177 ExpressionAccessor expAcc = (ExpressionAccessor)expression.accessor;
178 expAcc.fieldName = expAcc.fieldName.accept(this);
184 public Expression transform(EGetConstraint expression) {
189 public Expression transform(EIf expression) {
190 expression.condition = expression.condition.accept(this);
191 expression.then_ = expression.then_.accept(this);
192 if(expression.else_ != null)
193 expression.else_ = expression.else_.accept(this);
198 public Expression transform(EIntegerLiteral expression) {
202 protected void transformCases(Case[] cases) {
203 for(Case case_ : cases) {
204 for(int i=0;i<case_.patterns.length;++i)
205 case_.patterns[i] = case_.patterns[i].accept(this);
206 case_.value = case_.value.accept(this);
211 public Expression transform(ELambda expression) {
212 transformCases(expression.cases);
217 public Expression transform(ELambdaType expression) {
218 expression.value = expression.value.accept(this);
223 public Expression transform(ELet expression) {
224 expression.in = expression.in.accept(this);
225 for(Assignment assign : expression.assignments) {
226 assign.pattern = assign.pattern.accept(this);
227 assign.value = assign.value.accept(this);
233 public Expression transform(EListComprehension expression) {
234 expression.head = expression.head.accept(this);
235 expression.qualifier = expression.qualifier.accept(this);
240 public Expression transform(EListLiteral expression) {
241 for(int i=0;i<expression.components.length;++i)
242 expression.components[i] = expression.components[i].accept(this);
247 public Expression transform(ELiteral expression) {
252 public Expression transform(EMatch expression) {
253 for(int i=0;i<expression.scrutinee.length;++i)
254 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
255 transformCases(expression.cases);
260 public Expression transform(EPlaceholder expression) {
261 expression.expression = expression.expression.accept(this);
266 public Expression transform(EPreLet expression) {
267 for(LetStatement statement : expression.assignments) {
268 statement.pattern = statement.pattern.accept(this);
269 statement.value = statement.value.accept(this);
271 expression.in = expression.in.accept(this);
276 public Expression transform(EPreRuleset expression) {
277 for(RuleStatement rule : expression.statements) {
278 rule.body = rule.body.accept(this);
279 rule.head = rule.head.accept(this);
281 expression.in = expression.in.accept(this);
286 public Expression transform(ECoveringBranchPoint expression) {
287 expression.expression = expression.expression.accept(this);
292 public Expression transform(ERange expression) {
293 expression.from = expression.from.accept(this);
294 expression.to = expression.to.accept(this);
299 public Expression transform(ERealLiteral expression) {
304 public Expression transform(ERecord expression) {
305 for(FieldAssignment field : expression.fields)
306 if(field.value != null)
307 field.value = field.value.accept(this);
312 public Expression transform(ERuleset expression) {
313 for(ERuleset.DatalogRule rule : expression.rules) {
314 rule.body = rule.body.accept(this);
315 for(int i=0;i<rule.headParameters.length;++i)
316 rule.headParameters[i] = rule.headParameters[i].accept(this);
318 expression.in = expression.in.accept(this);
323 public Expression transform(ESelect expression) {
324 expression.query = expression.query.accept(this);
325 expression.expression = expression.expression.accept(this);
330 public Expression transform(ESimpleLambda expression) {
331 expression.value = expression.value.accept(this);
336 public Expression transform(ESimpleLet expression) {
337 expression.value = expression.value.accept(this);
338 expression.in = expression.in.accept(this);
343 public Expression transform(EStringLiteral expression) {
348 public Expression transform(ETransformation expression) {
349 expression.seed = expression.seed.accept(this);
354 public Expression transform(ETypeAnnotation expression) {
355 expression.value = expression.value.accept(this);
360 public Expression transform(EVar expression) {
365 public Expression transform(EVariable expression) {
370 public Expression transform(EWhen expression) {
371 expression.query = expression.query.accept(this);
372 expression.action = expression.action.accept(this);
377 public Expression transform(GuardedExpressionGroup expression) {
378 for(GuardedExpression ge : expression.expressions) {
379 for(int i=0;i<ge.guards.length;++i)
380 ge.guards[i] = ge.guards[i].accept(this);
381 ge.value = ge.value.accept(this);
387 public ListQualifier transform(ListAssignment qualifier) {
388 qualifier.pattern = qualifier.pattern.accept(this);
389 qualifier.value = qualifier.value.accept(this);
394 public ListQualifier transform(ListGenerator qualifier) {
395 qualifier.pattern = qualifier.pattern.accept(this);
396 qualifier.value = qualifier.value.accept(this);
401 public ListQualifier transform(ListGuard qualifier) {
402 qualifier.condition = qualifier.condition.accept(this);
407 public ListQualifier transform(ListSeq qualifier) {
408 qualifier.a = qualifier.a.accept(this);
409 qualifier.b = qualifier.b.accept(this);
414 public ListQualifier transform(ListThen qualifier) {
415 qualifier.left = qualifier.left.accept(this);
416 qualifier.transformer = qualifier.transformer.accept(this);
417 if(qualifier.by != null)
418 qualifier.by = qualifier.by.accept(this);
423 public Query transform(QAlternative query) {
424 for(int i=0;i<query.queries.length;++i)
425 query.queries[i] = query.queries[i].accept(this);
430 public Query transform(QAtom query) {
431 for(int i=0;i<query.parameters.length;++i)
432 query.parameters[i] = query.parameters[i].accept(this);
437 public Query transform(QConjunction query) {
438 for(int i=0;i<query.queries.length;++i)
439 query.queries[i] = query.queries[i].accept(this);
444 public Query transform(QDisjunction query) {
445 for(int i=0;i<query.queries.length;++i)
446 query.queries[i] = query.queries[i].accept(this);
451 public Query transform(QExists query) {
452 query.query = query.query.accept(this);
457 public Query transform(QIf query) {
458 query.condition = query.condition.accept(this);
459 query.thenQuery = query.thenQuery.accept(this);
460 query.elseQuery = query.elseQuery.accept(this);
465 public Query transform(QMapping query) {
466 for(int i=0;i<query.parameters.length;++i)
467 query.parameters[i] = query.parameters[i].accept(this);
472 public Query transform(QNegation query) {
473 query.query = query.query.accept(this);
478 public Query transform(QPreBinds query) {
479 query.left = query.left.accept(this);
480 query.right = query.right.accept(this);
485 public Query transform(QPreEquals query) {
486 query.left = query.left.accept(this);
487 query.right = query.right.accept(this);
492 public Query transform(QPreExists query) {
493 query.query = query.query.accept(this);
498 public Query transform(QPreGuard query) {
499 query.guard = query.guard.accept(this);
504 public Expression transform(EEquations expression) {
505 for(Equation equation : expression.equations)
506 equation.accept(this);
511 public void visit(EqBasic equation) {
512 equation.left = equation.left.accept(this);
513 equation.right = equation.right.accept(this);
517 public void visit(EqGuard equation) {
518 equation.guard = equation.guard.accept(this);
522 public void visit(CHRStatement statement) {
523 for(int i=0;i<statement.body.length;++i)
524 statement.body[i] = statement.body[i].accept(this);
525 for(int i=0;i<statement.head.length;++i)
526 statement.head[i] = statement.head[i].accept(this);