]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ETransformation.java
b96907ea3b9e4b3b69907f0cd53b766e6d8d07e2
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ETransformation.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6
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
17 public class ETransformation extends SimplifiableExpression {
18     public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
19     
20     public final String name;
21     public Query seed;
22     
23     public ETransformation(String name, Query seed) {
24         this.name = name;
25         this.seed = seed;
26     }
27
28     @Override
29     protected void updateType() throws MatchException {
30         setType(Types.UNIT);
31     }
32     
33     @Override
34     public Expression inferType(TypingContext context) {
35         context.declareEffect(location, Types.PROC);
36         seed.checkType(context);
37         return compile(context);
38     }
39
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>() {
44             @Override
45             public int compare(TransformationRule o1, TransformationRule o2) {
46                 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
47             }
48         });
49        
50         // Translation
51         TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);
52         tb.handleSeed(seed);
53         for(TransformationRule rule : rules)
54             if(!rule.isAbstract)
55                 tb.handleRule(rule);
56         Expression expression = tb.compileRules();
57         
58         if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
59             System.out.println(expression);
60         return expression;
61     }
62
63     @Override
64     public Expression resolve(TranslationContext context) {
65         seed = seed.resolve(context);
66         return this;
67     }
68
69     @Override
70     public void setLocationDeep(long loc) {
71         if(location == Locations.NO_LOCATION) {
72             location = loc;
73             seed.setLocationDeep(loc);
74         }
75     }
76
77     @Override
78     public void accept(ExpressionVisitor visitor) {
79         visitor.visit(this);
80     }
81     
82     @Override
83     public Expression accept(ExpressionTransformer transformer) {
84         return transformer.transform(this);
85     }
86
87 }