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