]> 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 collectVars method by a visitor
[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.exceptions.MatchException;
14
15 public class ECHRRuleset extends Expression {
16     public CHRRuleset ruleset;
17     public Expression in;
18     
19     public ECHRRuleset(CHRRuleset ruleset, Expression in) {
20         this.ruleset = ruleset;
21         this.in = in;
22     }
23
24     @Override
25     protected void updateType() throws MatchException {
26         setType(in.getType());
27     }
28
29     @Override
30     public IVal toVal(CompilationContext context, CodeWriter w) {
31         ruleset.generateCode(w);
32         return in.toVal(context, w);
33     }
34
35     @Override
36     public Expression resolve(TranslationContext context) {
37         if(context.currentRuleset != null) {
38             context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
39             return this;
40         }
41         context.currentRuleset = ruleset;
42         
43         context.pushFrame();
44         context.pushCHRConstraintFrame();
45         ruleset.resolve(context);
46         in = in.resolve(context);
47         context.popCHRConstraintFrame(ruleset.constraints);
48         context.popFrame();
49         
50         context.currentRuleset = null;
51         
52         return this;
53     }
54     @Override
55     public void setLocationDeep(long loc) {
56         if(location == Locations.NO_LOCATION) {
57             this.location = loc;
58             ruleset.setLocationDeep(loc);
59             in.setLocationDeep(loc);
60         }
61     }
62     
63     @Override
64     public void accept(ExpressionVisitor visitor) {
65         visitor.visit(this);
66     }
67     
68     @Override
69     public Expression inferType(TypingContext context) {
70         ruleset.checkType(context);
71         in = in.inferType(context);
72         return this;
73     }
74     
75     @Override
76     public Expression simplify(SimplificationContext context) {
77         ruleset.simplify(context);
78         ruleset.compile(context);
79         in = in.simplify(context);
80         return this;
81     }
82     
83     @Override
84     public Expression accept(ExpressionTransformer transformer) {
85         return transformer.transform(this);
86     }
87     
88     @Override
89     public IExpression toIExpression(ExpressionInterpretationContext context) {
90         throw new UnsupportedOperationException();
91     }
92
93 }