1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
7 import org.simantics.scl.compiler.elaboration.chr.translation.CHRTranslation;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
10 import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
11 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.StatementGroup;
17 import org.simantics.scl.compiler.errors.Locations;
19 public class EBlock extends ASTExpression {
21 ArrayList<Statement> statements = new ArrayList<Statement>();
27 public void addStatement(Statement statement) {
28 statements.add(statement);
31 public void setMonadic(boolean monadic) {
32 this.monadic = monadic;
35 public ArrayList<Statement> getStatements() {
39 public Statement getFirst() {
40 return statements.get(0);
43 public Statement getLast() {
44 return statements.get(statements.size()-1);
48 public Expression resolve(TranslationContext context) {
49 if(statements.isEmpty()) {
50 context.getErrorLog().log(location, "Block should not be empty.");
51 return new EError(location);
53 int i = statements.size()-1;
54 Statement last = statements.get(i);
55 if(!(last instanceof GuardStatement)) {
56 context.getErrorLog().log(last.location, "Block should end with an expression");
57 return new EError(location);
60 Expression in = ((GuardStatement)last).value;
62 Statement cur = statements.get(i);
63 StatementGroup group = cur.getStatementGroup();
65 in = cur.toExpression(context, monadic, in);
68 while(i>0 && statements.get(i-1).getStatementGroup() == group)
72 in = extractLet(i, endId, in);
75 in = extractRules(i, endId, in);
78 in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
83 return in.resolve(context);
86 private Expression extractRules(int begin, int end, Expression in) {
87 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
90 private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) {
91 CHRRuleset ruleset = new CHRRuleset();
92 ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
93 for(int i=begin;i<end;++i) {
94 Statement statement = statements.get(i);
95 if(statement instanceof CHRStatement)
96 ruleset.addRule(CHRTranslation.convertCHRStatement(context, (CHRStatement)statement));
97 else if(statement instanceof ConstraintStatement)
98 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
99 else if(statement instanceof IncludeStatement)
100 ruleset.includes.add((IncludeStatement)statement);
102 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
107 public CHRRuleset extractCHRRules(TranslationContext context) {
108 return extractCHRRules(context, 0, statements.size());
111 @SuppressWarnings("unchecked")
112 private Expression extractLet(int begin, int end, Expression in) {
113 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
116 public static Expression create(ArrayList<Expression> statements) {
117 EBlock block = new EBlock();
118 for(Expression statement : statements)
119 block.addStatement(new GuardStatement(statement));
124 public void setLocationDeep(long loc) {
125 if(location == Locations.NO_LOCATION) {
127 for(Statement statement : statements)
128 statement.setLocationDeep(loc);
133 public Expression accept(ExpressionTransformer transformer) {
134 return transformer.transform(this);
138 public int getSyntacticFunctionArity() {
141 Statement lastStatement = statements.get(statements.size()-1);
142 if(!(lastStatement instanceof GuardStatement))
144 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
148 public void accept(ExpressionVisitor visitor) {