]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java
(refs #7250) CHR rules modularization (first working version)
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ECHRRulesetConstructor.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java
new file mode 100644 (file)
index 0000000..e8a5cb8
--- /dev/null
@@ -0,0 +1,105 @@
+package org.simantics.scl.compiler.elaboration.expressions;
+
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
+import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
+import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
+import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
+import org.simantics.scl.compiler.environment.Environment;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.scl.compiler.internal.codegen.references.IVal;
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
+import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
+import org.simantics.scl.compiler.internal.interpreted.IExpression;
+import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.exceptions.MatchException;
+
+import gnu.trove.map.hash.TObjectIntHashMap;
+import gnu.trove.set.hash.THashSet;
+import gnu.trove.set.hash.TIntHashSet;
+
+public class ECHRRulesetConstructor extends Expression {
+    CHRRuleset ruleset;
+    
+    public ECHRRulesetConstructor(CHRRuleset ruleset) {
+        this.ruleset = ruleset;
+    }
+    
+    @Override
+    public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
+        ruleset.collectRefs(allRefs, refs);
+    }
+    @Override
+    public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
+        ruleset.collectVars(allVars, vars);
+    }
+    @Override
+    public void forVariables(VariableProcedure procedure) {
+        ruleset.forVariables(procedure);
+    }
+    @Override
+    protected void updateType() throws MatchException {
+        throw new InternalCompilerError("Type of ECHRRulesetConstructor should be already given.");
+    }
+    @Override
+    public IVal toVal(Environment env, CodeWriter w) {
+        return ruleset.generateCode(w);
+    }
+    @Override
+    public void collectFreeVariables(THashSet<Variable> vars) {
+        ruleset.collectFreeVariables(vars);
+    }
+    @Override
+    public Expression resolve(TranslationContext context) {
+        context.pushFrame();
+        context.pushCHRConstraintFrame();
+        ruleset.resolve(context);
+        context.popCHRConstraintFrame(ruleset.constraints);
+        context.popFrame();
+        return this;
+    }
+    @Override
+    public void setLocationDeep(long loc) {
+        if(location == Locations.NO_LOCATION) {
+            this.location = loc;
+            ruleset.setLocationDeep(loc);
+        }
+    }
+    @Override
+    public Expression decorate(ExpressionDecorator decorator) {
+        return this;
+    }
+    @Override
+    public void collectEffects(THashSet<Type> effects) {
+        ruleset.collectEffects(effects);
+    }
+    @Override
+    public void accept(ExpressionVisitor visitor) {
+        visitor.visit(this);
+    }
+    
+    @Override
+    public Expression inferType(TypingContext context) {
+        ruleset.checkType(context);
+        return this;
+    }
+    
+    @Override
+    public Expression simplify(SimplificationContext context) {
+        ruleset.simplify(context);
+        ruleset.compile(context);
+        return this;
+    }
+    
+    @Override
+    public Expression accept(ExpressionTransformer transformer) {
+        return transformer.transform(this);
+    }
+    
+    @Override
+    public IExpression toIExpression(ExpressionInterpretationContext context) {
+        throw new UnsupportedOperationException();
+    }
+
+}