X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FEBlock.java;h=c21732557ba555f5ce950058e0eddaaab468182a;hp=ebe88b499aab482e79bef9baac25a544898abaa1;hb=82a87b8535628d47d9c381e1a3a2296fb67c7fd0;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java index ebe88b499..c21732557 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java @@ -1,16 +1,18 @@ package org.simantics.scl.compiler.elaboration.expressions; import java.util.ArrayList; -import java.util.LinkedList; 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; +import org.simantics.scl.compiler.elaboration.expressions.block.IncludeStatement; import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement; import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement; import org.simantics.scl.compiler.elaboration.expressions.block.Statement; @@ -19,28 +21,39 @@ import org.simantics.scl.compiler.errors.Locations; public class EBlock extends ASTExpression { - LinkedList statements = new LinkedList(); - boolean monadic; + public ArrayList statements = new ArrayList(); + 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 getStatements() { + return statements; } - public LinkedList getStatements() { - return statements; + public Statement getFirst() { + return statements.get(0); + } + + public Statement getLast() { + return statements.get(statements.size()-1); } @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)) { @@ -53,7 +66,7 @@ public class EBlock extends ASTExpression { 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) @@ -65,10 +78,14 @@ public class EBlock extends ASTExpression { case Rule: in = extractRules(i, endId, in); break; - case CHR: - in = 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); @@ -78,21 +95,31 @@ public class EBlock extends ASTExpression { return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in); } - private Expression extractCHRRules(TranslationContext context, int begin, int end, Expression in) { + private CHRRuleset extractCHRRules(TranslationContext context, int begin, int end) { CHRRuleset ruleset = new CHRRuleset(); ruleset.location = Locations.combine(statements.get(begin).location, statements.get(end-1).location); for(int i=begin;i)(List)statements.subList(begin, end), in); @@ -121,11 +148,16 @@ public class EBlock extends ASTExpression { @Override public int getSyntacticFunctionArity() { - if(monadic) + if(blockType != BlockType.Normal) return 0; - Statement lastStatement = statements.getLast(); + Statement lastStatement = statements.get(statements.size()-1); if(!(lastStatement instanceof GuardStatement)) return 0; return ((GuardStatement)lastStatement).value.getSyntacticFunctionArity(); } + + @Override + public void accept(ExpressionVisitor visitor) { + visitor.visit(this); + } }