]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Support break and return in functions. Fix for initialization of function local variables
authorvillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Sep 2014 07:58:59 +0000 (07:58 +0000)
committervillberg <villberg@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 15 Sep 2014 07:58:59 +0000 (07:58 +0000)
refs #5224

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30274 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForStatement.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Function.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IStatement.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/IfStatement.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/MultiStatement.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Return.java [new file with mode: 0644]
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Statement.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/StatementList.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/While.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelParser.java
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jj
fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt

diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java
new file mode 100644 (file)
index 0000000..588cfe0
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+\r
+public class Break implements IStatement {\r
+       \r
+    public Break() {\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return "break";\r
+    }\r
+    \r
+    @Override\r
+    public ReturnValue evaluate(IEnvironment environment) {\r
+       return ReturnValue.BREAK;\r
+    }\r
+    \r
+}\r
index 5dedd79ef39f9a3c23b868507cde8e83517cfa75..bd487b19449f3a9d9dbf08830604fea9fa9659b9 100644 (file)
@@ -27,23 +27,28 @@ public class ForStatement implements IStatement {
                return "for " + indices + " loop "+ statement + " end for";\r
        }\r
        \r
-       private void loop(IEnvironment environment, int i) {\r
+       private ReturnValue loop(IEnvironment environment, int i) {\r
                if(i == indices.size()) {\r
-                       statement.evaluate(environment);\r
-                       return;\r
+                       ReturnValue ret = statement.evaluate(environment);\r
+                       if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+                       else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+                       else return ReturnValue.CONTINUE;\r
                } else {\r
                        ForIndex index = indices.get(i);\r
                        Array array = (Array)index.expression.evaluate(environment);\r
                        for(Object element : array.elements()) {\r
                                environment.put(index.base.index, element);\r
-                               loop(environment, i+1);\r
+                               ReturnValue ret = loop(environment, i+1);\r
+                               if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+                               else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
                        }\r
+                       return ReturnValue.CONTINUE;\r
                }\r
        }\r
        \r
        @Override\r
-       public void evaluate(IEnvironment environment) {\r
-               loop(environment, 0);\r
+       public ReturnValue evaluate(IEnvironment environment) {\r
+               return loop(environment, 0);\r
        }\r
        \r
 }\r
index 200cf3653613fa792b54ccdf0df4ff75f11398a5..9af1f4b34a0a9102c5ae9a45af4ea6662d286000 100644 (file)
@@ -48,7 +48,12 @@ final public class Function implements Fn, IFrame {
                }\r
                for(int i=0;i<internals.size();i++) {\r
                        VariableDeclaration decl = internals.get(i);\r
-                       decl.variable.assignPlain(environment, null);\r
+                       if(decl.modification.args.length == 1) {\r
+                               Object o = decl.modification.args[0].modification.evaluate(environment);\r
+                               decl.variable.assignPlain(environment, o);\r
+                       } else {\r
+                               decl.variable.assignPlain(environment, null);\r
+                       }\r
                }\r
        }\r
        \r
index e50a09bc6b77846d8f5cd0345d5ff4bec27e87b1..efb5472f95fb2efb3640d5cdde7c4ff3b8116bba 100644 (file)
@@ -12,6 +12,10 @@ package fi.semantum.sysdyn.solver;
 \r
 public interface IStatement {\r
 \r
-       public void evaluate(IEnvironment environment);\r
+       public enum ReturnValue {\r
+               CONTINUE,BREAK,RETURN;\r
+       }\r
+       \r
+       public ReturnValue evaluate(IEnvironment environment);\r
        \r
 }\r
index 37dc2af26714582c0d0e885d8e185d90e231d900..8fb020e7e5a37fecfed662e70604583208db5636 100644 (file)
@@ -28,11 +28,11 @@ public class IfStatement implements IStatement {
     }\r
     \r
        @Override\r
-       public void evaluate(IEnvironment environment) {\r
+       public ReturnValue evaluate(IEnvironment environment) {\r
                if((Boolean)exp.evaluate(environment)) {\r
-                       t.evaluate(environment);\r
+                       return t.evaluate(environment);\r
                } else {\r
-                       e.evaluate(environment);\r
+                       return e.evaluate(environment);\r
                }\r
        }\r
     \r
index ff330375b3f89752e99f7c4a80be10dc0fe9af01..f7c5083fb9cf484ca653d0b62dc63a808e08dd8c 100644 (file)
@@ -30,12 +30,13 @@ public class MultiStatement implements IStatement {
     }\r
     \r
     @Override\r
-    public void evaluate(IEnvironment environment) {\r
+    public ReturnValue evaluate(IEnvironment environment) {\r
        @SuppressWarnings("unchecked")\r
        ArrayList<Object> values = (ArrayList<Object>)environment.getSystem().evaluateFunction(environment, fn.base.name, args);\r
        for(int i=0;i<targets.size();i++) {\r
                targets.get(i).assignPlain(environment, values.get(i));\r
        }\r
+       return ReturnValue.CONTINUE;\r
     }\r
     \r
 }\r
index 95d0116602b3a5e6a4ae6759988b4af6dde9b57f..6560e2d148da85fe4658b615a23b68a7ea1eea8e 100644 (file)
@@ -153,7 +153,15 @@ public class Parser {
 //                                     | when_statement() )\r
 //                             comment() \r
                        \r
-                       if(n.jjtGetNumChildren() == 1) {\r
+                       if(n.jjtGetNumChildren() == 0) {\r
+                               if("break".equals(n.op)) {\r
+                                       return new Break();\r
+                               } else if ("return".equals(n.op)) {\r
+                                       return new Return();\r
+                               } else {\r
+                                       throw new IllegalStateException();\r
+                               }\r
+                       } else if(n.jjtGetNumChildren() == 1) {\r
                                return (IStatement)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
                        } else if (n.jjtGetNumChildren() == 2) {\r
                                SimpleNode n0 = (SimpleNode)n.jjtGetChild(1);\r
diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Return.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Return.java
new file mode 100644 (file)
index 0000000..023ae53
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+\r
+public class Return implements IStatement {\r
+       \r
+    public Return() {\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+       return "return";\r
+    }\r
+    \r
+    @Override\r
+    public ReturnValue evaluate(IEnvironment environment) {\r
+       return ReturnValue.RETURN;\r
+    }\r
+    \r
+}\r
index a3dcd40f80c5c81174479238f4f0d58613e43fe4..381192f8e57b60d1f1e6ec5f64f5ae5b813c9eb2 100644 (file)
@@ -28,9 +28,10 @@ public class Statement implements IStatement {
     }\r
     \r
     @Override\r
-    public void evaluate(IEnvironment environment) {\r
+    public ReturnValue evaluate(IEnvironment environment) {\r
                Object value = expression.evaluate(environment);\r
                target.assign(environment, subscripts, value);\r
+               return ReturnValue.CONTINUE;\r
     }\r
     \r
 }\r
index b8c17f88b931c0d6116f3490b3edad96edd2632a..a7df2cd3ff5e16fa209e3c85da1e717a67b3b54b 100644 (file)
@@ -26,8 +26,12 @@ public class StatementList implements IStatement {
     }\r
     \r
     @Override\r
-    public void evaluate(IEnvironment environment) {\r
-       for(IStatement stm : statements) stm.evaluate(environment);\r
+    public ReturnValue evaluate(IEnvironment environment) {\r
+       for(IStatement stm : statements) {\r
+               ReturnValue ret = stm.evaluate(environment);\r
+               if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+       }\r
+       return ReturnValue.CONTINUE;\r
     }\r
     \r
 }\r
index 47e94c86e6bd517644a5256bae0346dbe35ce2aa..61af2515bf18e35785892a56cecfda9234db9328 100644 (file)
@@ -26,9 +26,13 @@ public class While implements IStatement {
        }\r
        \r
        @Override\r
-       public void evaluate(IEnvironment environment) {\r
-               while ((Boolean)expression.evaluate(environment))\r
-                       statement.evaluate(environment);\r
+       public ReturnValue evaluate(IEnvironment environment) {\r
+               while ((Boolean)expression.evaluate(environment)) {\r
+                       ReturnValue ret = statement.evaluate(environment);\r
+                       if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+                       else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+               }\r
+               return ReturnValue.CONTINUE;\r
        }\r
        \r
 }\r
index 944551684d2d0a1bc33ced4a14dc7a84d00f4107..bcd1d520d1cc1c2385e85fc0839513c8274fa2c0 100644 (file)
@@ -2193,9 +2193,11 @@ public class ModelParser/*@bgen(jjtree)*/implements ModelParserTreeConstants, Mo
         break;\r
       case 29:\r
         jj_consume_token(29);\r
+                            jjtn000.op = "break";\r
         break;\r
       case 18:\r
         jj_consume_token(18);\r
+                             jjtn000.op = "return";\r
         break;\r
       case 31:\r
         if_statement();\r
index 0af3f17b2991c69b13575ea7c6b02b78d8fec676..3f9084834011988743fbcda3566674c6d10aaa8d 100644 (file)
@@ -1454,8 +1454,8 @@ void statement() : {/*@bgen(jjtree) statement */
 //     comment\r
        ( component_reference() ( ":=" expression() | function_call_args() )\r
                | "(" output_expression_list() ")" ":=" component_reference() function_call_args()\r
-               | "break"\r
-               | "return"\r
+               | "break" { jjtn000.op = "break";}\r
+               | "return" { jjtn000.op = "return";}\r
                | if_statement()\r
                | for_statement()\r
                | while_statement()\r
index 7c4de3d30facd179525eced6d149fe5a9a28859b..b4231401e04f5489713f52e9a3117b34fa1f0216 100644 (file)
@@ -445,8 +445,8 @@ void statement() : {
 //     comment\r
        ( component_reference() ( ":=" expression() | function_call_args() )\r
                | "(" output_expression_list() ")" ":=" component_reference() function_call_args()\r
-               | "break"\r
-               | "return"\r
+               | "break" { jjtThis.op = "break";}\r
+               | "return" { jjtThis.op = "return";}\r
                | if_statement()\r
                | for_statement()\r
                | while_statement()\r