import java.util.ArrayList;
import java.util.List;
-import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.elaboration.chr.CHRRule;
import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
+import org.simantics.scl.compiler.elaboration.chr.ast.CHRQueryTranslationMode;
import org.simantics.scl.compiler.elaboration.chr.translation.CHRTranslation;
import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
+import org.simantics.scl.compiler.elaboration.expressions.block.BlockType;
import org.simantics.scl.compiler.elaboration.expressions.block.CHRStatement;
import org.simantics.scl.compiler.elaboration.expressions.block.ConstraintStatement;
import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
public class EBlock extends ASTExpression {
- ArrayList<Statement> statements = new ArrayList<Statement>();
- boolean monadic;
+ public ArrayList<Statement> statements = new ArrayList<Statement>();
+ BlockType blockType = BlockType.Normal;
public EBlock() {
+ this.blockType = blockType;
+ }
+
+ public void setBlockType(BlockType blockType) {
+ this.blockType = blockType;
}
public void addStatement(Statement statement) {
statements.add(statement);
}
- public void setMonadic(boolean monadic) {
- this.monadic = monadic;
- }
-
public ArrayList<Statement> getStatements() {
return statements;
}
@Override
public Expression resolve(TranslationContext context) {
- if(statements.isEmpty())
- throw new InternalCompilerError();
+ if(statements.isEmpty()) {
+ context.getErrorLog().log(location, "Block should not be empty.");
+ return new EError(location);
+ }
int i = statements.size()-1;
Statement last = statements.get(i);
if(!(last instanceof GuardStatement)) {
Statement cur = statements.get(i);
StatementGroup group = cur.getStatementGroup();
if(group == null)
- in = cur.toExpression(context, monadic, in);
+ in = cur.toExpression(context, blockType, in);
else {
int endId = i+1;
while(i>0 && statements.get(i-1).getStatementGroup() == group)
case Rule:
in = extractRules(i, endId, in);
break;
- case CHR:
- in = new ECHRRuleset(extractCHRRules(context, i, endId), in);
+ case CHR: {
+ CHRRuleset ruleset = extractCHRRules(context, i, endId);
+ long location = Locations.combine(ruleset.location, in.location);
+ in = new ECHRRuleset(ruleset, in);
+ in.location = location;
break;
}
+ }
}
}
return in.resolve(context);
ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location);
for(int i=begin;i<end;++i) {
Statement statement = statements.get(i);
- if(statement instanceof CHRStatement)
- ruleset.addRule(CHRTranslation.convertCHRStatement(context, (CHRStatement)statement));
+ if(statement instanceof CHRStatement) {
+ CHRStatement chrStatement = (CHRStatement)statement;
+ ruleset.addRule(new CHRRule(chrStatement.location,
+ chrStatement.head.translate(context, CHRQueryTranslationMode.RULE_HEAD),
+ chrStatement.body.translate(context, CHRQueryTranslationMode.RULE_BODY)));
+ }
else if(statement instanceof ConstraintStatement)
- ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
+ ruleset.addConstraint(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
else if(statement instanceof IncludeStatement)
ruleset.includes.add((IncludeStatement)statement);
else
@Override
public int getSyntacticFunctionArity() {
- if(monadic)
+ if(blockType != BlockType.Normal)
return 0;
Statement lastStatement = statements.get(statements.size()-1);
if(!(lastStatement instanceof GuardStatement))