]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/MatchOp.java
SCL compiler generates line numbers to bytecode
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / plan / MatchOp.java
1 package org.simantics.scl.compiler.elaboration.chr.plan;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.compilation.CompilationContext;
6 import org.simantics.scl.compiler.elaboration.expressions.Expression;
7 import org.simantics.scl.compiler.internal.codegen.references.IVal;
8 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
9 import org.simantics.scl.compiler.internal.elaboration.matching2.PatternMatchingCompiler2;
10 import org.simantics.scl.compiler.internal.elaboration.matching2.Row2;
11
12 public class MatchOp extends PlanOp {
13     public Expression scrutinee;
14     public Expression pattern;
15     
16     public MatchOp(long location, Expression scrutinee, Expression pattern) {
17         super(location);
18         this.scrutinee = scrutinee;
19         this.pattern = pattern;
20     }
21     
22     @Override
23     public void toString(StringBuilder b) {
24         b.append("MATCH ").append(pattern).append(" = ").append(scrutinee);
25     }
26
27     @Override
28     public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
29         CodeWriter body = w.createBlock();
30         CodeWriter end = w.createBlock();
31         
32         IVal[] scrutineeVals = new IVal[1];
33         scrutineeVals[0] = scrutinee.toVal(context, w);
34
35         ArrayList<Row2> rows = new ArrayList<Row2>(1);
36         rows.add(new Row2(new Expression[] {pattern}, body.getContinuation()));
37
38         PatternMatchingCompiler2.split(location, w, context, scrutineeVals, end.getContinuation(), rows);
39         
40         planContext.nextOp(body);
41         if(body.isUnfinished())
42             body.jump(location, end.getContinuation());
43         
44         w.continueAs(end);
45     }
46 }