]> gerrit.simantics Code Review - simantics/platform.git/blob
b7f943c07f47009c2ed7596dbcbdee9a95c49449
[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     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                     in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
81                     break;
82                 }
83             }
84         }
85         return in.resolve(context);
86     }
87
88     private Expression extractRules(int begin, int end, Expression in) {
89         return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
90     }
91     
92     private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) {
93         CHRRuleset ruleset = new CHRRuleset();
94         ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
95         for(int i=begin;i<end;++i) {
96             Statement statement = statements.get(i);
97             if(statement instanceof CHRStatement) {
98                 CHRStatement chrStatement = (CHRStatement)statement;
99                 ruleset.addRule(new CHRRule(chrStatement.location,
100                         chrStatement.head.translate(context, CHRQueryTranslationMode.RULE_HEAD),
101                         chrStatement.body.translate(context, CHRQueryTranslationMode.RULE_BODY)));
102             }
103             else if(statement instanceof ConstraintStatement)
104                 ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
105             else if(statement instanceof IncludeStatement)
106                 ruleset.includes.add((IncludeStatement)statement);
107             else
108                 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
109         }
110         return ruleset;
111     }
112
113     public CHRRuleset extractCHRRules(TranslationContext context) {
114         return extractCHRRules(context, 0, statements.size());
115     }
116     
117     @SuppressWarnings("unchecked")
118     private Expression extractLet(int begin, int end, Expression in) {
119         return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
120     }
121
122     public static Expression create(ArrayList<Expression> statements) {
123         EBlock block = new EBlock();
124         for(Expression statement : statements)
125             block.addStatement(new GuardStatement(statement));
126         return block;
127     }
128
129     @Override
130     public void setLocationDeep(long loc) {
131         if(location == Locations.NO_LOCATION) {
132             location = loc;
133             for(Statement statement : statements)
134                 statement.setLocationDeep(loc);
135         }
136     }
137     
138     @Override
139     public Expression accept(ExpressionTransformer transformer) {
140         return transformer.transform(this);
141     }
142
143     @Override
144     public int getSyntacticFunctionArity() {
145         if(monadic)
146             return 0;
147         Statement lastStatement = statements.get(statements.size()-1);
148         if(!(lastStatement instanceof GuardStatement))
149             return 0;
150         return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
151     }
152     
153     @Override
154     public void accept(ExpressionVisitor visitor) {
155         visitor.visit(this);
156     }
157 }