]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java
(refs #7375) Replaced ExpressionDecorator by ExpressionTransformer
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ECHRRuleset.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 import org.simantics.scl.compiler.internal.interpreted.IExpression;
12 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
15
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 public class ECHRRuleset extends Expression {
21     CHRRuleset ruleset;
22     Expression in;
23     
24     public ECHRRuleset(CHRRuleset ruleset, Expression in) {
25         this.ruleset = ruleset;
26         this.in = in;
27     }
28     
29     @Override
30     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
31         ruleset.collectRefs(allRefs, refs);
32         in.collectRefs(allRefs, refs);
33     }
34     @Override
35     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
36         ruleset.collectVars(allVars, vars);
37         in.collectVars(allVars, vars);
38     }
39     @Override
40     public void forVariables(VariableProcedure procedure) {
41         ruleset.forVariables(procedure);
42         in.forVariables(procedure);
43     }
44     @Override
45     protected void updateType() throws MatchException {
46         setType(in.getType());
47     }
48     @Override
49     public IVal toVal(CompilationContext context, CodeWriter w) {
50         ruleset.generateCode(w);
51         return in.toVal(context, w);
52     }
53     @Override
54     public void collectFreeVariables(THashSet<Variable> vars) {
55         ruleset.collectFreeVariables(vars);
56         in.collectFreeVariables(vars);
57     }
58     @Override
59     public Expression resolve(TranslationContext context) {
60         if(context.currentRuleset != null) {
61             context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
62             return this;
63         }
64         context.currentRuleset = ruleset;
65         
66         context.pushFrame();
67         context.pushCHRConstraintFrame();
68         ruleset.resolve(context);
69         in = in.resolve(context);
70         context.popCHRConstraintFrame(ruleset.constraints);
71         context.popFrame();
72         
73         context.currentRuleset = null;
74         
75         return this;
76     }
77     @Override
78     public void setLocationDeep(long loc) {
79         if(location == Locations.NO_LOCATION) {
80             this.location = loc;
81             ruleset.setLocationDeep(loc);
82             in.setLocationDeep(loc);
83         }
84     }
85     @Override
86     public void collectEffects(THashSet<Type> effects) {
87         ruleset.collectEffects(effects);
88         in.collectEffects(effects);
89     }
90     @Override
91     public void accept(ExpressionVisitor visitor) {
92         visitor.visit(this);
93     }
94     
95     @Override
96     public Expression inferType(TypingContext context) {
97         ruleset.checkType(context);
98         in = in.inferType(context);
99         return this;
100     }
101     
102     @Override
103     public Expression simplify(SimplificationContext context) {
104         ruleset.simplify(context);
105         ruleset.compile(context);
106         in = in.simplify(context);
107         return this;
108     }
109     
110     @Override
111     public Expression accept(ExpressionTransformer transformer) {
112         return transformer.transform(this);
113     }
114     
115     @Override
116     public IExpression toIExpression(ExpressionInterpretationContext context) {
117         throw new UnsupportedOperationException();
118     }
119
120 }