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 collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
31 ruleset.collectRefs(allRefs, refs);
32 in.collectRefs(allRefs, refs);
35 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
36 ruleset.collectVars(allVars, vars);
37 in.collectVars(allVars, vars);
40 public void forVariables(VariableProcedure procedure) {
41 ruleset.forVariables(procedure);
42 in.forVariables(procedure);
45 protected void updateType() throws MatchException {
46 setType(in.getType());
49 public IVal toVal(CompilationContext context, CodeWriter w) {
50 ruleset.generateCode(w);
51 return in.toVal(context, w);
54 public void collectFreeVariables(THashSet<Variable> vars) {
55 ruleset.collectFreeVariables(vars);
56 in.collectFreeVariables(vars);
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.");
64 context.currentRuleset = ruleset;
67 context.pushCHRConstraintFrame();
68 ruleset.resolve(context);
69 in = in.resolve(context);
70 context.popCHRConstraintFrame(ruleset.constraints);
73 context.currentRuleset = null;
78 public void setLocationDeep(long loc) {
79 if(location == Locations.NO_LOCATION) {
81 ruleset.setLocationDeep(loc);
82 in.setLocationDeep(loc);
86 public void collectEffects(THashSet<Type> effects) {
87 ruleset.collectEffects(effects);
88 in.collectEffects(effects);
91 public void accept(ExpressionVisitor visitor) {
96 public Expression inferType(TypingContext context) {
97 ruleset.checkType(context);
98 in = in.inferType(context);
103 public Expression simplify(SimplificationContext context) {
104 ruleset.simplify(context);
105 ruleset.compile(context);
106 in = in.simplify(context);
111 public Expression accept(ExpressionTransformer transformer) {
112 return transformer.transform(this);
116 public IExpression toIExpression(ExpressionInterpretationContext context) {
117 throw new UnsupportedOperationException();