1 package org.simantics.scl.compiler.elaboration.expressions;
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;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
19 public class ECHRRuleset extends Expression {
23 public ECHRRuleset(CHRRuleset ruleset, Expression in) {
24 this.ruleset = ruleset;
29 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
30 ruleset.collectVars(allVars, vars);
31 in.collectVars(allVars, vars);
35 protected void updateType() throws MatchException {
36 setType(in.getType());
40 public IVal toVal(CompilationContext context, CodeWriter w) {
41 ruleset.generateCode(w);
42 return in.toVal(context, w);
46 public void collectFreeVariables(THashSet<Variable> vars) {
47 ruleset.collectFreeVariables(vars);
48 in.collectFreeVariables(vars);
52 public Expression resolve(TranslationContext context) {
53 if(context.currentRuleset != null) {
54 context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
57 context.currentRuleset = ruleset;
60 context.pushCHRConstraintFrame();
61 ruleset.resolve(context);
62 in = in.resolve(context);
63 context.popCHRConstraintFrame(ruleset.constraints);
66 context.currentRuleset = null;
71 public void setLocationDeep(long loc) {
72 if(location == Locations.NO_LOCATION) {
74 ruleset.setLocationDeep(loc);
75 in.setLocationDeep(loc);
80 public void accept(ExpressionVisitor visitor) {
85 public Expression inferType(TypingContext context) {
86 ruleset.checkType(context);
87 in = in.inferType(context);
92 public Expression simplify(SimplificationContext context) {
93 ruleset.simplify(context);
94 ruleset.compile(context);
95 in = in.simplify(context);
100 public Expression accept(ExpressionTransformer transformer) {
101 return transformer.transform(this);
105 public IExpression toIExpression(ExpressionInterpretationContext context) {
106 throw new UnsupportedOperationException();