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.Type;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class ECHRRuleset extends Expression {
24 public ECHRRuleset(CHRRuleset ruleset, Expression in) {
25 this.ruleset = ruleset;
30 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
31 ruleset.collectVars(allVars, vars);
32 in.collectVars(allVars, vars);
36 protected void updateType() throws MatchException {
37 setType(in.getType());
41 public IVal toVal(CompilationContext context, CodeWriter w) {
42 ruleset.generateCode(w);
43 return in.toVal(context, w);
47 public void collectFreeVariables(THashSet<Variable> vars) {
48 ruleset.collectFreeVariables(vars);
49 in.collectFreeVariables(vars);
53 public Expression resolve(TranslationContext context) {
54 if(context.currentRuleset != null) {
55 context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
58 context.currentRuleset = ruleset;
61 context.pushCHRConstraintFrame();
62 ruleset.resolve(context);
63 in = in.resolve(context);
64 context.popCHRConstraintFrame(ruleset.constraints);
67 context.currentRuleset = null;
72 public void setLocationDeep(long loc) {
73 if(location == Locations.NO_LOCATION) {
75 ruleset.setLocationDeep(loc);
76 in.setLocationDeep(loc);
80 public void collectEffects(THashSet<Type> effects) {
81 ruleset.collectEffects(effects);
82 in.collectEffects(effects);
85 public void accept(ExpressionVisitor visitor) {
90 public Expression inferType(TypingContext context) {
91 ruleset.checkType(context);
92 in = in.inferType(context);
97 public Expression simplify(SimplificationContext context) {
98 ruleset.simplify(context);
99 ruleset.compile(context);
100 in = in.simplify(context);
105 public Expression accept(ExpressionTransformer transformer) {
106 return transformer.transform(this);
110 public IExpression toIExpression(ExpressionInterpretationContext context) {
111 throw new UnsupportedOperationException();