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