X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fchr%2Fplan%2FPlanContext.java;h=0ed3f8975d9bbaae4964d8f184453fdaa744b658;hb=refs%2Fchanges%2F79%2F579%2F2;hp=d8cf92332da0a035b4ce86d126a8230dabedea53;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PlanContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PlanContext.java index d8cf92332..0ed3f8975 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PlanContext.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PlanContext.java @@ -6,10 +6,13 @@ import org.simantics.scl.compiler.compilation.CompilationContext; import org.simantics.scl.compiler.constants.IntegerConstant; import org.simantics.scl.compiler.constants.JavaComparisonOperation; import org.simantics.scl.compiler.constants.singletons.IncreaseByOne; +import org.simantics.scl.compiler.constants.singletons.JustConstant; import org.simantics.scl.compiler.constants.singletons.ListElement; import org.simantics.scl.compiler.constants.singletons.ListLength; import org.simantics.scl.compiler.elaboration.chr.CHRRuleset; import org.simantics.scl.compiler.elaboration.expressions.Variable; +import org.simantics.scl.compiler.elaboration.java.EqualsFunction; +import org.simantics.scl.compiler.internal.codegen.continuations.Branch; import org.simantics.scl.compiler.internal.codegen.continuations.ICont; import org.simantics.scl.compiler.internal.codegen.references.IVal; import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter; @@ -20,20 +23,22 @@ public abstract class PlanContext { public CompilationContext context; public CHRRuleset ruleset; public IVal storeVar; + public IVal contextVar; public ArrayList partnerFacts = new ArrayList(); public IVal currentId; - public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal storeVar) { + public PlanContext(CompilationContext context, CHRRuleset ruleset, IVal storeVar, IVal contextVar) { this.context = context; this.ruleset = ruleset; this.storeVar = storeVar; + this.contextVar = contextVar; } public abstract void nextOp(CodeWriter w); public IVal generateNewId(long location, CodeWriter w) { if(currentId == null) - currentId = w.apply(location, ruleset.readCurrentId, storeVar); + currentId = w.apply(location, ruleset.readCurrentId, contextVar); IVal result = currentId; currentId = w.apply(location, IncreaseByOne.INSTANCE, currentId); return result; @@ -59,6 +64,24 @@ public abstract class PlanContext { w.continueAs(end); } + + public void iterateMaybe(long location, CodeWriter w, Variable variable, IVal maybeValue) { + Type componentType = variable.getType(); + + CodeWriter end = w.createBlock(); + CodeWriter body = w.createBlock(componentType); + w.switch_(maybeValue, new Branch[] { + new Branch(JustConstant.INSTANCE, body.getContinuation()), + new Branch(null, end.getContinuation()) + }); + + variable.setVal(body.getParameters()[0]); + nextOp(body); + if(body.isUnfinished()) + body.jump(end.getContinuation()); + + w.continueAs(end); + } public void check(long location, CodeWriter w, IVal booleanValue) { CodeWriter end = w.createBlock(); @@ -68,4 +91,29 @@ public abstract class PlanContext { w.jump(end.getContinuation()); w.continueAs(end); } + + public void checkEquals(long location, CodeWriter w, IVal a, IVal b) { + check(location, w, w.apply(location, + EqualsFunction.INSTANCE.createSpecialization(a.getType()), + a, b)); + } + + public void checkEqualsJust(long location, CodeWriter w, IVal value, IVal maybeValue) { + Type componentType = value.getType(); + + CodeWriter end = w.createBlock(); + CodeWriter body = w.createBlock(componentType); + w.switch_(maybeValue, new Branch[] { + new Branch(JustConstant.INSTANCE, body.getContinuation()), + new Branch(null, end.getContinuation()) + }); + body.branchAwayUnless(body.apply(location, + EqualsFunction.INSTANCE.createSpecialization(componentType), + value, body.getParameters()[0]), end.getContinuation()); + nextOp(body); + if(body.isUnfinished()) + body.jump(end.getContinuation()); + + w.continueAs(end); + } }