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 public 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 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;
89 return in.resolve(context);
92 private Expression extractRules(int begin, int end, Expression in) {
93 return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
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)));
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);
112 context.getErrorLog().log(statement.location, "Invalid CHR statement.");
117 public CHRRuleset extractCHRRules(TranslationContext context) {
118 return extractCHRRules(context, 0, statements.size());
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);
126 public static Expression create(ArrayList<Expression> statements) {
127 EBlock block = new EBlock();
128 for(Expression statement : statements)
129 block.addStatement(new GuardStatement(statement));
134 public void setLocationDeep(long loc) {
135 if(location == Locations.NO_LOCATION) {
137 for(Statement statement : statements)
138 statement.setLocationDeep(loc);
143 public Expression accept(ExpressionTransformer transformer) {
144 return transformer.transform(this);
148 public int getSyntacticFunctionArity() {
151 Statement lastStatement = statements.get(statements.size()-1);
152 if(!(lastStatement instanceof GuardStatement))
154 return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity();
158 public void accept(ExpressionVisitor visitor) {