]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Check that do block is not empty. 81/81/3
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Sep 2016 11:27:57 +0000 (14:27 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Sep 2016 11:56:40 +0000 (14:56 +0300)
A new check is needed, because parser was modified before
to allow empty blocks.

Change-Id: I7fa51c7ef25cae6a51e69f9a39378780094c0d8e

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java

index 295f3c1769f33c567424e80df9839133707e584f..ff131254081f7c4ce0a27b3bf9a18cf29fbc9195 100755 (executable)
-package org.simantics.scl.compiler.elaboration.expressions;\r
-\r
-import java.util.ArrayList;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-\r
-import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
-import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
-import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;\r
-import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;\r
-import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;\r
-import org.simantics.scl.compiler.elaboration.expressions.block.Statement;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-\r
-public class EBlock extends ASTExpression {\r
-\r
-    LinkedList<Statement> statements = new LinkedList<Statement>();\r
-    boolean monadic;\r
-    \r
-    public EBlock() {\r
-    }\r
-\r
-    public void addStatement(Statement statement) {\r
-        statements.add(statement);\r
-    }\r
-    \r
-    public void setMonadic(boolean monadic) {\r
-        this.monadic = monadic;\r
-    }\r
-    \r
-    public LinkedList<Statement> getStatements() {\r
-        return statements;\r
-    }\r
-\r
-    @Override\r
-    public Expression resolve(TranslationContext context) {\r
-        if(statements.isEmpty())\r
-            throw new InternalCompilerError();\r
-        int i = statements.size()-1;\r
-        Statement last = statements.get(i);\r
-        if(!(last instanceof GuardStatement)) {\r
-            context.getErrorLog().log(last.location, "Block should end with an expression");\r
-            return new EError(location);\r
-        }\r
-\r
-        Expression in = ((GuardStatement)last).value;\r
-        while(--i >= 0) {\r
-            Statement cur = statements.get(i);\r
-            if(cur instanceof RuleStatement) {\r
-                int endId = i+1;\r
-                while(i>0 && statements.get(i-1) instanceof RuleStatement)\r
-                    --i;\r
-                in = extractRules(i, endId, in);\r
-            }\r
-            else if(cur instanceof LetStatement && ((LetStatement)cur).pattern.isFunctionPattern()) {\r
-                int endId = i+1;\r
-                while(i>0 && (cur = statements.get(i-1)) instanceof LetStatement &&\r
-                        ((LetStatement)cur).pattern.isFunctionPattern())\r
-                    --i;\r
-                in = extractLet(i, endId, in);\r
-            }\r
-            else\r
-                in = cur.toExpression(context, monadic, in);\r
-        }\r
-        return in.resolve(context);\r
-    }\r
-\r
-    private Expression extractRules(int begin, int end, Expression in) {\r
-        return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    private Expression extractLet(int begin, int end, Expression in) {\r
-        return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);\r
-    }\r
-\r
-    public static Expression create(ArrayList<Expression> statements) {\r
-        EBlock block = new EBlock();\r
-        for(Expression statement : statements)\r
-            block.addStatement(new GuardStatement(statement));\r
-        return block;\r
-    }\r
-\r
-    @Override\r
-    public void setLocationDeep(long loc) {\r
-        if(location == Locations.NO_LOCATION) {\r
-            location = loc;\r
-            for(Statement statement : statements)\r
-                statement.setLocationDeep(loc);\r
-        }\r
-    }\r
-    \r
-    @Override\r
-    public Expression accept(ExpressionTransformer transformer) {\r
-        return transformer.transform(this);\r
-    }\r
-\r
-}\r
+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.contexts.TranslationContext;
+import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
+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;
+import org.simantics.scl.compiler.errors.Locations;
+
+public class EBlock extends ASTExpression {
+
+    LinkedList<Statement> statements = new LinkedList<Statement>();
+    boolean monadic;
+    
+    public EBlock() {
+    }
+
+    public void addStatement(Statement statement) {
+        statements.add(statement);
+    }
+    
+    public void setMonadic(boolean monadic) {
+        this.monadic = monadic;
+    }
+    
+    public LinkedList<Statement> getStatements() {
+        return statements;
+    }
+
+    @Override
+    public Expression resolve(TranslationContext context) {
+        if(statements.isEmpty()) {
+            context.getErrorLog().log(location, "Block must contain at least one statement.");
+            return new EError(location);
+        } 
+        int i = statements.size()-1;
+        Statement last = statements.get(i);
+        if(!(last instanceof GuardStatement)) {
+            context.getErrorLog().log(last.location, "Block should end with an expression");
+            return new EError(location);
+        }
+
+        Expression in = ((GuardStatement)last).value;
+        while(--i >= 0) {
+            Statement cur = statements.get(i);
+            if(cur instanceof RuleStatement) {
+                int endId = i+1;
+                while(i>0 && statements.get(i-1) instanceof RuleStatement)
+                    --i;
+                in = extractRules(i, endId, in);
+            }
+            else if(cur instanceof LetStatement && ((LetStatement)cur).pattern.isFunctionPattern()) {
+                int endId = i+1;
+                while(i>0 && (cur = statements.get(i-1)) instanceof LetStatement &&
+                        ((LetStatement)cur).pattern.isFunctionPattern())
+                    --i;
+                in = extractLet(i, endId, in);
+            }
+            else
+                in = cur.toExpression(context, monadic, in);
+        }
+        return in.resolve(context);
+    }
+
+    private Expression extractRules(int begin, int end, Expression in) {
+        return new EPreRuleset(statements.subList(begin, end).toArray(new RuleStatement[end-begin]), in);
+    }
+
+    @SuppressWarnings("unchecked")
+    private Expression extractLet(int begin, int end, Expression in) {
+        return new EPreLet((List<LetStatement>)(List<?>)statements.subList(begin, end), in);
+    }
+
+    public static Expression create(ArrayList<Expression> statements) {
+        EBlock block = new EBlock();
+        for(Expression statement : statements)
+            block.addStatement(new GuardStatement(statement));
+        return block;
+    }
+
+    @Override
+    public void setLocationDeep(long loc) {
+        if(location == Locations.NO_LOCATION) {
+            location = loc;
+            for(Statement statement : statements)
+                statement.setLocationDeep(loc);
+        }
+    }
+    
+    @Override
+    public Expression accept(ExpressionTransformer transformer) {
+        return transformer.transform(this);
+    }
+
+}