]> gerrit.simantics Code Review - simantics/platform.git/blob
3ebbb03cf51bf78d5a52676683d6110ba109f8d1
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.chr.plan;
2
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.constants.BooleanConstant;
5 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
6 import org.simantics.scl.compiler.elaboration.expressions.Expression;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.internal.codegen.references.IVal;
9 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
10
11 public class AccessFactOp extends PlanOp {
12
13     Expression inputFact;
14     CHRConstraint constraint;
15     Variable[] variables;
16     boolean killAfterMatch;
17     
18     public AccessFactOp(long location, Expression inputFact, CHRConstraint constraint, Variable[] variables,
19             boolean killAfterMatch) {
20         super(location);
21         this.inputFact = inputFact;
22         this.constraint = constraint;
23         this.variables = variables;
24         this.killAfterMatch = killAfterMatch;
25     }
26
27     @Override
28     public void toString(StringBuilder b) {
29         b.append("ACCESS  ");
30         if(killAfterMatch)
31             b.append("- ");
32         b.append(constraint);
33         for(Variable variable : variables)
34             b.append(' ').append(variable);
35         b.append(" = ").append(inputFact);
36     }
37
38     @Override
39     public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
40         IVal inputVal = inputFact.toVal(context.environment, w);
41         for(int i=0;i<variables.length;++i)
42             variables[i].setVal(w.apply(location, constraint.accessComponent(i), inputVal));
43         IVal activeId = w.apply(location, constraint.accessId, inputVal);
44         CodeWriter end = constraint.mayBeRemoved() ? w.createBlock() : null;
45         planContext.partnerFacts.add(new PartnerFact(true, activeId, constraint, inputVal, constraint.mayBeRemoved(), killAfterMatch, null, null, end == null ? null : end.getContinuation()));
46         planContext.nextOp(w);
47         if(end != null)
48             end.return_(BooleanConstant.FALSE);
49     }
50
51 }