]> gerrit.simantics Code Review - simantics/platform.git/blob
ff131254081f7c4ce0a27b3bf9a18cf29fbc9195
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4 import java.util.LinkedList;
5 import java.util.List;
6
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;
14
15 public class EBlock extends ASTExpression {
16
17     LinkedList<Statement> statements = new LinkedList<Statement>();
18     boolean monadic;
19     
20     public EBlock() {
21     }
22
23     public void addStatement(Statement statement) {
24         statements.add(statement);
25     }
26     
27     public void setMonadic(boolean monadic) {
28         this.monadic = monadic;
29     }
30     
31     public LinkedList<Statement> getStatements() {
32         return statements;
33     }
34
35     @Override
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);
40         } 
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);
46         }
47
48         Expression in = ((GuardStatement)last).value;
49         while(--i >= 0) {
50             Statement cur = statements.get(i);
51             if(cur instanceof RuleStatement) {
52                 int endId = i+1;
53                 while(i>0 && statements.get(i-1) instanceof RuleStatement)
54                     --i;
55                 in = extractRules(i, endId, in);
56             }
57             else if(cur instanceof LetStatement && ((LetStatement)cur).pattern.isFunctionPattern()) {
58                 int endId = i+1;
59                 while(i>0 && (cur = statements.get(i-1)) instanceof LetStatement &&
60                         ((LetStatement)cur).pattern.isFunctionPattern())
61                     --i;
62                 in = extractLet(i, endId, in);
63             }
64             else
65                 in = cur.toExpression(context, monadic, in);
66         }
67         return in.resolve(context);
68     }
69
70     private Expression extractRules(int begin, int end, Expression in) {
71         return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
72     }
73
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);
77     }
78
79     public static Expression create(ArrayList<Expression> statements) {
80         EBlock block = new EBlock();
81         for(Expression statement : statements)
82             block.addStatement(new GuardStatement(statement));
83         return block;
84     }
85
86     @Override
87     public void setLocationDeep(long loc) {
88         if(location == Locations.NO_LOCATION) {
89             location = loc;
90             for(Statement statement : statements)
91                 statement.setLocationDeep(loc);
92         }
93     }
94     
95     @Override
96     public Expression accept(ExpressionTransformer transformer) {
97         return transformer.transform(this);
98     }
99
100 }