1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
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;
21 public class EBlock extends ASTExpression {
23 ArrayList<Statement> statements = new ArrayList<Statement>();
29 public void addStatement(Statement statement) {
30 statements.add(statement);
33 public void setMonadic(boolean monadic) {
34 this.monadic = monadic;
37 public ArrayList<Statement> getStatements() {
41 public Statement getFirst() {
42 return statements.get(0);
45 public Statement getLast() {
46 return statements.get(statements.size()-1);
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);
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);
62 Expression in = ((GuardStatement)last).value;
64 Statement cur = statements.get(i);
65 StatementGroup group = cur.getStatementGroup();
67 in = cur.toExpression(context, monadic, in);
70 while(i>0 && statements.get(i-1).getStatementGroup() == group)
74 in = extractLet(i, endId, in);
77 in = extractRules(i, endId, in);
80 in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
85 return in.resolve(context);
88 private Expression extractRules(int begin, int end, Expression in) {
89 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
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)));
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);
108 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
113 public CHRRuleset extractCHRRules(TranslationContext context) {
114 return extractCHRRules(context, 0, statements.size());
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);
122 public static Expression create(ArrayList<Expression> statements) {
123 EBlock block = new EBlock();
124 for(Expression statement : statements)
125 block.addStatement(new GuardStatement(statement));
130 public void setLocationDeep(long loc) {
131 if(location == Locations.NO_LOCATION) {
133 for(Statement statement : statements)
134 statement.setLocationDeep(loc);
139 public Expression accept(ExpressionTransformer transformer) {
140 return transformer.transform(this);
144 public int getSyntacticFunctionArity() {
147 Statement lastStatement = statements.get(statements.size()-1);
148 if(!(lastStatement instanceof GuardStatement))
150 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
154 public void accept(ExpressionVisitor visitor) {