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.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
10 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
11 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
13 import org.simantics.scl.compiler.errors.Locations;
15 public class EBlock extends ASTExpression {
17 LinkedList<Statement> statements = new LinkedList<Statement>();
23 public void addStatement(Statement statement) {
24 statements.add(statement);
27 public void setMonadic(boolean monadic) {
28 this.monadic = monadic;
31 public LinkedList<Statement> getStatements() {
36 public Expression resolve(TranslationContext context) {
37 if(statements.isEmpty()) {
38 context.getErrorLog().log(location, "Block must contain at least one statement.");
39 return new EError(location);
41 int i = statements.size()-1;
42 Statement last = statements.get(i);
43 if(!(last instanceof GuardStatement)) {
44 context.getErrorLog().log(last.location, "Block should end with an expression");
45 return new EError(location);
48 Expression in = ((GuardStatement)last).value;
50 Statement cur = statements.get(i);
51 if(cur instanceof RuleStatement) {
53 while(i>0 && statements.get(i-1) instanceof RuleStatement)
55 in = extractRules(i, endId, in);
57 else if(cur instanceof LetStatement && ((LetStatement)cur).pattern.isFunctionPattern()) {
59 while(i>0 && (cur = statements.get(i-1)) instanceof LetStatement &&
60 ((LetStatement)cur).pattern.isFunctionPattern())
62 in = extractLet(i, endId, in);
65 in = cur.toExpression(context, monadic, in);
67 return in.resolve(context);
70 private Expression extractRules(int begin, int end, Expression in) {
71 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
74 @SuppressWarnings("unchecked")
75 private Expression extractLet(int begin, int end, Expression in) {
76 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
79 public static Expression create(ArrayList<Expression> statements) {
80 EBlock block = new EBlock();
81 for(Expression statement : statements)
82 block.addStatement(new GuardStatement(statement));
87 public void setLocationDeep(long loc) {
88 if(location == Locations.NO_LOCATION) {
90 for(Statement statement : statements)
91 statement.setLocationDeep(loc);
96 public Expression accept(ExpressionTransformer transformer) {
97 return transformer.transform(this);