]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java
2c70bd90742ef6562d8c9f141bd705838264dc82
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EBlock.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
7 import org.simantics.scl.compiler.elaboration.chr.translation.CHRTranslation;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
10 import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
11 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
12 import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement;
13 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
14 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
15 import org.simantics.scl.compiler.elaboration.expressions.block.Statement;
16 import org.simantics.scl.compiler.elaboration.expressions.block.StatementGroup;
17 import org.simantics.scl.compiler.errors.Locations;
18
19 public class EBlock extends ASTExpression {
20
21     ArrayList<Statement> statements = new ArrayList<Statement>();
22     boolean monadic;
23     
24     public EBlock() {
25     }
26
27     public void addStatement(Statement statement) {
28         statements.add(statement);
29     }
30     
31     public void setMonadic(boolean monadic) {
32         this.monadic = monadic;
33     }
34     
35     public ArrayList<Statement> getStatements() {
36         return statements;
37     }
38     
39     public Statement getFirst() {
40         return statements.get(0);
41     }
42     
43     public Statement getLast() {
44         return statements.get(statements.size()-1);
45     }
46
47     @Override
48     public Expression resolve(TranslationContext context) {
49         if(statements.isEmpty()) {
50             context.getErrorLog().log(location, "Block should not be empty.");
51             return new EError(location);
52         }
53         int i = statements.size()-1;
54         Statement last = statements.get(i);
55         if(!(last instanceof GuardStatement)) {
56             context.getErrorLog().log(last.location, "Block should end with an expression");
57             return new EError(location);
58         }
59
60         Expression in = ((GuardStatement)last).value;
61         while(--i >= 0) {
62             Statement cur = statements.get(i);
63             StatementGroup group = cur.getStatementGroup();
64             if(group == null)
65                 in = cur.toExpression(context, monadic, in);
66             else {
67                 int endId = i+1;
68                 while(i>0 && statements.get(i-1).getStatementGroup() == group)
69                     --i;
70                 switch(group) {
71                 case LetFunction:
72                     in = extractLet(i, endId, in);
73                     break;
74                 case Rule:
75                     in = extractRules(i, endId, in);
76                     break;
77                 case CHR:
78                     in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
79                     break;
80                 }
81             }
82         }
83         return in.resolve(context);
84     }
85
86     private Expression extractRules(int begin, int end, Expression in) {
87         return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
88     }
89     
90     private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) {
91         CHRRuleset ruleset = new CHRRuleset();
92         ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
93         for(int i=begin;i<end;++i) {
94             Statement statement = statements.get(i);
95             if(statement instanceof CHRStatement)
96                 ruleset.addRule(CHRTranslation.convertCHRStatement(context, (CHRStatement)statement));
97             else if(statement instanceof ConstraintStatement)
98                 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
99             else if(statement instanceof IncludeStatement)
100                 ruleset.includes.add((IncludeStatement)statement);
101             else
102                 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
103         }
104         return ruleset;
105     }
106
107     public CHRRuleset extractCHRRules(TranslationContext context) {
108         return extractCHRRules(context, 0, statements.size());
109     }
110     
111     @SuppressWarnings("unchecked")
112     private Expression extractLet(int begin, int end, Expression in) {
113         return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
114     }
115
116     public static Expression create(ArrayList<Expression> statements) {
117         EBlock block = new EBlock();
118         for(Expression statement : statements)
119             block.addStatement(new GuardStatement(statement));
120         return block;
121     }
122
123     @Override
124     public void setLocationDeep(long loc) {
125         if(location == Locations.NO_LOCATION) {
126             location = loc;
127             for(Statement statement : statements)
128                 statement.setLocationDeep(loc);
129         }
130     }
131     
132     @Override
133     public Expression accept(ExpressionTransformer transformer) {
134         return transformer.transform(this);
135     }
136
137     @Override
138     public int getSyntacticFunctionArity() {
139         if(monadic)
140             return 0;
141         Statement lastStatement = statements.get(statements.size()-1);
142         if(!(lastStatement instanceof GuardStatement))
143             return 0;
144         return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
145     }
146     
147     @Override
148     public void accept(ExpressionVisitor visitor) {
149         visitor.visit(this);
150     }
151 }