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