]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ETransformation.java
(refs #7375) Replaced collectFreeVariables method by a visitor
[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 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 public class ETransformation extends SimplifiableExpression {
21     public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
22     
23     public final String name;
24     public Query seed;
25     
26     public ETransformation(String name, Query seed) {
27         this.name = name;
28         this.seed = seed;
29     }
30
31     @Override
32     public void collectVars(TObjectIntHashMap<Variable> allVars,
33             TIntHashSet vars) {
34         seed.collectVars(allVars, vars);
35     }
36
37     @Override
38     protected void updateType() throws MatchException {
39         setType(Types.UNIT);
40     }
41     
42     @Override
43     public Expression inferType(TypingContext context) {
44         context.declareEffect(location, Types.PROC);
45         seed.checkType(context);
46         return compile(context);
47     }
48
49     private Expression compile(TypingContext context) {
50         ArrayList<TransformationRule> rules = new ArrayList<TransformationRule>(); 
51         context.getEnvironment().collectRules(rules);
52         Collections.sort(rules, new Comparator<TransformationRule>() {
53             @Override
54             public int compare(TransformationRule o1, TransformationRule o2) {
55                 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
56             }
57         });
58        
59         // Translation
60         TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);
61         tb.handleSeed(seed);
62         for(TransformationRule rule : rules)
63             if(!rule.isAbstract)
64                 tb.handleRule(rule);
65         Expression expression = tb.compileRules();
66         
67         if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
68             System.out.println(expression);
69         return expression;
70     }
71
72     @Override
73     public Expression resolve(TranslationContext context) {
74         seed = seed.resolve(context);
75         return this;
76     }
77
78     @Override
79     public void setLocationDeep(long loc) {
80         if(location == Locations.NO_LOCATION) {
81             location = loc;
82             seed.setLocationDeep(loc);
83         }
84     }
85
86     @Override
87     public void accept(ExpressionVisitor visitor) {
88         visitor.visit(this);
89     }
90     
91     @Override
92     public Expression accept(ExpressionTransformer transformer) {
93         return transformer.transform(this);
94     }
95
96 }