1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
7 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
9 import org.simantics.scl.compiler.elaboration.query.Query;
10 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.compiler.internal.elaboration.transformations.TransformationBuilder;
13 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 public class ETransformation extends SimplifiableExpression {
18 public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
20 public final String name;
23 public ETransformation(String name, Query seed) {
29 protected void updateType() throws MatchException {
34 public Expression inferType(TypingContext context) {
35 context.declareEffect(location, Types.PROC);
36 seed.checkType(context);
37 return compile(context);
40 private Expression compile(TypingContext context) {
41 ArrayList<TransformationRule> rules = new ArrayList<TransformationRule>();
42 context.getEnvironment().collectRules(rules);
43 Collections.sort(rules, new Comparator<TransformationRule>() {
45 public int compare(TransformationRule o1, TransformationRule o2) {
46 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
51 TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);
53 for(TransformationRule rule : rules)
56 Expression expression = tb.compileRules();
58 if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
59 System.out.println(expression);
64 public Expression resolve(TranslationContext context) {
65 seed = seed.resolve(context);
70 public void setLocationDeep(long loc) {
71 if(location == Locations.NO_LOCATION) {
73 seed.setLocationDeep(loc);
78 public void accept(ExpressionVisitor visitor) {
83 public Expression accept(ExpressionTransformer transformer) {
84 return transformer.transform(this);