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;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class ETransformation extends SimplifiableExpression {
21 private static final Logger LOGGER = LoggerFactory.getLogger(ETransformation.class);
23 public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
25 public final String name;
28 public ETransformation(String name, Query seed) {
34 protected void updateType() throws MatchException {
39 public Expression inferType(TypingContext context) {
40 context.declareEffect(location, Types.PROC);
41 seed.checkType(context);
42 return compile(context);
45 private Expression compile(TypingContext context) {
46 ArrayList<TransformationRule> rules = new ArrayList<TransformationRule>();
47 context.getEnvironment().collectRules(rules);
48 Collections.sort(rules, new Comparator<TransformationRule>() {
50 public int compare(TransformationRule o1, TransformationRule o2) {
51 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
56 TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);
58 for(TransformationRule rule : rules)
61 Expression expression = tb.compileRules();
63 if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
64 LOGGER.info("{}", expression);
69 public Expression resolve(TranslationContext context) {
70 seed = seed.resolve(context);
75 public void setLocationDeep(long loc) {
76 if(location == Locations.NO_LOCATION) {
78 seed.setLocationDeep(loc);
83 public void accept(ExpressionVisitor visitor) {
88 public Expression accept(ExpressionTransformer transformer) {
89 return transformer.transform(this);