1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
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 throw new InternalCompilerError();
52 int i = statements.size()-1;
53 Statement last = statements.get(i);
54 if(!(last instanceof GuardStatement)) {
55 context.getErrorLog().log(last.location, "Block should end with an expression");
56 return new EError(location);
59 Expression in = ((GuardStatement)last).value;
61 Statement cur = statements.get(i);
62 StatementGroup group = cur.getStatementGroup();
64 in = cur.toExpression(context, monadic, in);
67 while(i>0 && statements.get(i-1).getStatementGroup() == group)
71 in = extractLet(i, endId, in);
74 in = extractRules(i, endId, in);
77 in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
82 return in.resolve(context);
85 private Expression extractRules(int begin, int end, Expression in) {
86 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
89 private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) {
90 CHRRuleset ruleset = new CHRRuleset();
91 ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
92 for(int i=begin;i<end;++i) {
93 Statement statement = statements.get(i);
94 if(statement instanceof CHRStatement)
95 ruleset.rules.add(CHRTranslation.convertCHRStatement(context, (CHRStatement)statement));
96 else if(statement instanceof ConstraintStatement)
97 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
98 else if(statement instanceof IncludeStatement)
99 ruleset.includes.add((IncludeStatement)statement);
101 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
106 public CHRRuleset extractCHRRules(TranslationContext context) {
107 return extractCHRRules(context, 0, statements.size());
110 @SuppressWarnings("unchecked")
111 private Expression extractLet(int begin, int end, Expression in) {
112 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
115 public static Expression create(ArrayList<Expression> statements) {
116 EBlock block = new EBlock();
117 for(Expression statement : statements)
118 block.addStatement(new GuardStatement(statement));
123 public void setLocationDeep(long loc) {
124 if(location == Locations.NO_LOCATION) {
126 for(Statement statement : statements)
127 statement.setLocationDeep(loc);
132 public Expression accept(ExpressionTransformer transformer) {
133 return transformer.transform(this);
137 public int getSyntacticFunctionArity() {
140 Statement lastStatement = statements.get(statements.size()-1);
141 if(!(lastStatement instanceof GuardStatement))
143 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
147 public void accept(ExpressionVisitor visitor) {