1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.elaboration.chr.CHRRule;
7 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
8 import org.simantics.scl.compiler.elaboration.chr.translation.CHRTranslation;
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
10 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
11 import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
17 import org.simantics.scl.compiler.elaboration.expressions.block.StatementGroup;
18 import org.simantics.scl.compiler.errors.Locations;
20 public class EBlock extends ASTExpression {
22 ArrayList<Statement> statements = new ArrayList<Statement>();
28 public void addStatement(Statement statement) {
29 statements.add(statement);
32 public void setMonadic(boolean monadic) {
33 this.monadic = monadic;
36 public ArrayList<Statement> getStatements() {
40 public Statement getFirst() {
41 return statements.get(0);
44 public Statement getLast() {
45 return statements.get(statements.size()-1);
49 public Expression resolve(TranslationContext context) {
50 if(statements.isEmpty()) {
51 context.getErrorLog().log(location, "Block should not be empty.");
52 return new EError(location);
54 int i = statements.size()-1;
55 Statement last = statements.get(i);
56 if(!(last instanceof GuardStatement)) {
57 context.getErrorLog().log(last.location, "Block should end with an expression");
58 return new EError(location);
61 Expression in = ((GuardStatement)last).value;
63 Statement cur = statements.get(i);
64 StatementGroup group = cur.getStatementGroup();
66 in = cur.toExpression(context, monadic, in);
69 while(i>0 && statements.get(i-1).getStatementGroup() == group)
73 in = extractLet(i, endId, in);
76 in = extractRules(i, endId, in);
79 in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
84 return in.resolve(context);
87 private Expression extractRules(int begin, int end, Expression in) {
88 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
91 private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) {
92 CHRRuleset ruleset = new CHRRuleset();
93 ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
94 for(int i=begin;i<end;++i) {
95 Statement statement = statements.get(i);
96 if(statement instanceof CHRStatement) {
97 CHRStatement chrStatement = (CHRStatement)statement;
98 ruleset.addRule(new CHRRule(chrStatement.location, chrStatement.head.translateAsHead(context), chrStatement.body.translateAsBody(context)));
100 else if(statement instanceof ConstraintStatement)
101 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
102 else if(statement instanceof IncludeStatement)
103 ruleset.includes.add((IncludeStatement)statement);
105 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
110 public CHRRuleset extractCHRRules(TranslationContext context) {
111 return extractCHRRules(context, 0, statements.size());
114 @SuppressWarnings("unchecked")
115 private Expression extractLet(int begin, int end, Expression in) {
116 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
119 public static Expression create(ArrayList<Expression> statements) {
120 EBlock block = new EBlock();
121 for(Expression statement : statements)
122 block.addStatement(new GuardStatement(statement));
127 public void setLocationDeep(long loc) {
128 if(location == Locations.NO_LOCATION) {
130 for(Statement statement : statements)
131 statement.setLocationDeep(loc);
136 public Expression accept(ExpressionTransformer transformer) {
137 return transformer.transform(this);
141 public int getSyntacticFunctionArity() {
144 Statement lastStatement = statements.get(statements.size()-1);
145 if(!(lastStatement instanceof GuardStatement))
147 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
151 public void accept(ExpressionVisitor visitor) {