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