fix return in while/for expression 53/15453/1
Antoine ELIAS [Mon, 3 Nov 2014 09:02:04 +0000 (10:02 +0100)]
Change-Id: I6c4816e1b6f08f63f6fddbb3547e51f398cebdf1

scilab/modules/ast/src/cpp/ast/runvisitor.cpp
scilab/modules/ast/tests/unit_tests/return.dia.ref [new file with mode: 0644]
scilab/modules/ast/tests/unit_tests/return.tst [new file with mode: 0644]

index 45911ff..65ee037 100644 (file)
@@ -324,7 +324,7 @@ void RunVisitorT<T>::visitprivate(const WhileExp  &e)
     //allow return operation
     if (e.isReturnable())
     {
-        (&e.getBody())->isReturnable();
+        const_cast<Exp*>(&(e.getBody()))->setReturnable();
     }
 
     //condition
@@ -378,7 +378,7 @@ void RunVisitorT<T>::visitprivate(const ForExp  &e)
     //allow return operation
     if (e.isReturnable())
     {
-        e.getBody().isReturnable();
+        const_cast<Exp&>(e.getBody()).setReturnable();
     }
 
     if (pIT->isImplicitList())
diff --git a/scilab/modules/ast/tests/unit_tests/return.dia.ref b/scilab/modules/ast/tests/unit_tests/return.dia.ref
new file mode 100644 (file)
index 0000000..3fba965
--- /dev/null
@@ -0,0 +1,55 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+//
+//
+// <-- JVM NOT MANDATORY -->
+function v = while_return()
+    v = 0;
+    while v == 0
+        v = 1;
+        return
+        v = 2;
+    end
+endfunction
+function v = while_if_return()
+    v = 0;
+    flag = 0;
+    while v == 0
+        if v == 0 then
+            v = 1;
+            return
+            v = 2;
+        end
+    end
+    v = 3;
+endfunction
+function v = for_return()
+    v = 0;
+    for i = 1:1
+        v = 1;
+        return
+        v = 2;
+    end
+endfunction
+function v = for_if_return()
+    v = 0;
+    flag = 0;
+    for i = 1:1
+        if v == 0 then
+            v = 1;
+            return
+            v = 2;
+        end
+    end
+    v = 3;
+endfunction
+assert_checkequal(while_return(), 1);
+assert_checkequal(while_if_return(), 1);
+assert_checkequal(for_return(), 1);
+assert_checkequal(for_if_return(), 1);
diff --git a/scilab/modules/ast/tests/unit_tests/return.tst b/scilab/modules/ast/tests/unit_tests/return.tst
new file mode 100644 (file)
index 0000000..2254b71
--- /dev/null
@@ -0,0 +1,61 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+//
+//
+
+// <-- JVM NOT MANDATORY -->
+
+function v = while_return()
+    v = 0;
+    while v == 0
+        v = 1;
+        return
+        v = 2;
+    end
+endfunction
+
+function v = while_if_return()
+    v = 0;
+    flag = 0;
+    while v == 0
+        if v == 0 then
+            v = 1;
+            return
+            v = 2;
+        end
+    end
+    v = 3;
+endfunction
+
+function v = for_return()
+    v = 0;
+    for i = 1:1
+        v = 1;
+        return
+        v = 2;
+    end
+endfunction
+
+function v = for_if_return()
+    v = 0;
+    flag = 0;
+    for i = 1:1
+        if v == 0 then
+            v = 1;
+            return
+            v = 2;
+        end
+    end
+    v = 3;
+endfunction
+
+assert_checkequal(while_return(), 1);
+assert_checkequal(while_if_return(), 1);
+assert_checkequal(for_return(), 1);
+assert_checkequal(for_if_return(), 1);