1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import java.util.ArrayList;
\r
4 import java.util.LinkedList;
\r
5 import java.util.List;
\r
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
9 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
\r
10 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
\r
11 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
\r
12 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
\r
13 import org.simantics.scl.compiler.errors.Locations;
\r
15 public class EBlock extends ASTExpression {
\r
17 LinkedList<Statement> statements = new LinkedList<Statement>();
\r
23 public void addStatement(Statement statement) {
\r
24 statements.add(statement);
\r
27 public void setMonadic(boolean monadic) {
\r
28 this.monadic = monadic;
\r
31 public LinkedList<Statement> getStatements() {
\r
36 public Expression resolve(TranslationContext context) {
\r
37 if(statements.isEmpty())
\r
38 throw new InternalCompilerError();
\r
39 int i = statements.size()-1;
\r
40 Statement last = statements.get(i);
\r
41 if(!(last instanceof GuardStatement)) {
\r
42 context.getErrorLog().log(last.location, "Block should end with an expression");
\r
43 return new EError(location);
\r
46 Expression in = ((GuardStatement)last).value;
\r
48 Statement cur = statements.get(i);
\r
49 if(cur instanceof RuleStatement) {
\r
51 while(i>0 && statements.get(i-1) instanceof RuleStatement)
\r
53 in = extractRules(i, endId, in);
\r
55 else if(cur instanceof LetStatement && ((LetStatement)cur).pattern.isFunctionPattern()) {
\r
57 while(i>0 && (cur = statements.get(i-1)) instanceof LetStatement &&
\r
58 ((LetStatement)cur).pattern.isFunctionPattern())
\r
60 in = extractLet(i, endId, in);
\r
63 in = cur.toExpression(context, monadic, in);
\r
65 return in.resolve(context);
\r
68 private Expression extractRules(int begin, int end, Expression in) {
\r
69 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
\r
72 @SuppressWarnings("unchecked")
\r
73 private Expression extractLet(int begin, int end, Expression in) {
\r
74 return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
\r
77 public static Expression create(ArrayList<Expression> statements) {
\r
78 EBlock block = new EBlock();
\r
79 for(Expression statement : statements)
\r
80 block.addStatement(new GuardStatement(statement));
\r
85 public void setLocationDeep(long loc) {
\r
86 if(location == Locations.NO_LOCATION) {
\r
88 for(Statement statement : statements)
\r
89 statement.setLocationDeep(loc);
\r
94 public Expression accept(ExpressionTransformer transformer) {
\r
95 return transformer.transform(this);
\r