1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.LinkedList;
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
8 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
9 import org.simantics.scl.compiler.elaboration.chr.translation.CHRTranslation;
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
11 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
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 LinkedList<Statement> statements = new LinkedList<Statement>();
28 public void addStatement(Statement statement) {
29 statements.add(statement);
32 public void setMonadic(boolean monadic) {
33 this.monadic = monadic;
36 public LinkedList<Statement> getStatements() {
41 public Expression resolve(TranslationContext context) {
42 if(statements.isEmpty())
43 throw new InternalCompilerError();
44 int i = statements.size()-1;
45 Statement last = statements.get(i);
46 if(!(last instanceof GuardStatement)) {
47 context.getErrorLog().log(last.location, "Block should end with an expression");
48 return new EError(location);
51 Expression in = ((GuardStatement)last).value;
53 Statement cur = statements.get(i);
54 StatementGroup group = cur.getStatementGroup();
56 in = cur.toExpression(context, monadic, in);
59 while(i>0 && statements.get(i-1).getStatementGroup() == group)
63 in = extractLet(i, endId, in);
66 in = extractRules(i, endId, in);
69 in = extractCHRRules(context, i, endId, in);
74 return in.resolve(context);
77 private Expression extractRules(int begin, int end, Expression in) {
78 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
81 private Expression extractCHRRules(TranslationContext context, int begin, int end, Expression in) {
82 CHRRuleset ruleset = new CHRRuleset();
83 ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
84 for(int i=begin;i<end;++i) {
85 Statement statement = statements.get(i);
86 if(statement instanceof CHRStatement)
87 ruleset.rules.add(CHRTranslation.convertCHRStatement(context, (CHRStatement)statement));
88 else if(statement instanceof ConstraintStatement)
89 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
91 throw new InternalCompilerError("Invalid CHR statement.");
93 return new ECHRRuleset(ruleset, in);
96 @SuppressWarnings("unchecked")
97 private Expression extractLet(int begin, int end, Expression in) {
98 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
101 public static Expression create(ArrayList<Expression> statements) {
102 EBlock block = new EBlock();
103 for(Expression statement : statements)
104 block.addStatement(new GuardStatement(statement));
109 public void setLocationDeep(long loc) {
110 if(location == Locations.NO_LOCATION) {
112 for(Statement statement : statements)
113 statement.setLocationDeep(loc);
118 public Expression accept(ExpressionTransformer transformer) {
119 return transformer.transform(this);
123 public int getSyntacticFunctionArity() {
126 Statement lastStatement = statements.getLast();
127 if(!(lastStatement instanceof GuardStatement))
129 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();