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 protected void updateType() throws MatchException {
41 setType(in.getType());
44 public IVal toVal(CompilationContext context, CodeWriter w) {
45 ruleset.generateCode(w);
46 return in.toVal(context, w);
49 public void collectFreeVariables(THashSet<Variable> vars) {
50 ruleset.collectFreeVariables(vars);
51 in.collectFreeVariables(vars);
54 public Expression resolve(TranslationContext context) {
55 if(context.currentRuleset != null) {
56 context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
59 context.currentRuleset = ruleset;
62 context.pushCHRConstraintFrame();
63 ruleset.resolve(context);
64 in = in.resolve(context);
65 context.popCHRConstraintFrame(ruleset.constraints);
68 context.currentRuleset = null;
73 public void setLocationDeep(long loc) {
74 if(location == Locations.NO_LOCATION) {
76 ruleset.setLocationDeep(loc);
77 in.setLocationDeep(loc);
81 public void collectEffects(THashSet<Type> effects) {
82 ruleset.collectEffects(effects);
83 in.collectEffects(effects);
86 public void accept(ExpressionVisitor visitor) {
91 public Expression inferType(TypingContext context) {
92 ruleset.checkType(context);
93 in = in.inferType(context);
98 public Expression simplify(SimplificationContext context) {
99 ruleset.simplify(context);
100 ruleset.compile(context);
101 in = in.simplify(context);
106 public Expression accept(ExpressionTransformer transformer) {
107 return transformer.transform(this);
111 public IExpression toIExpression(ExpressionInterpretationContext context) {
112 throw new UnsupportedOperationException();