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