changeset 78:a7cb4615acbc

Fixed a bug in the control flow graph for while-loops.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Fri, 29 May 2009 10:48:36 +0200
parents 261228c35658
children 8462026d1e19
files pysmcl/flow.py pysmcl/test/unit/test_flow.py
diffstat 2 files changed, 41 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/flow.py	Wed May 27 15:20:23 2009 +0200
+++ b/pysmcl/flow.py	Fri May 29 10:48:36 2009 +0200
@@ -101,7 +101,7 @@
                 # TODO, does not express the condition being evaluated twice
                 # We have to rewrite the expression to make it right.
                 before = current
-                out_of_while = self.flow_body(stm.body, current)
+                out_of_while = self.flow_body(stm.body, set([stm]))
                 self.make_edge(out_of_while, stm)
                 current = before | out_of_while | self.to_loop_exit
                 self.to_loop_exit = set()
--- a/pysmcl/test/unit/test_flow.py	Wed May 27 15:20:23 2009 +0200
+++ b/pysmcl/test/unit/test_flow.py	Fri May 29 10:48:36 2009 +0200
@@ -44,6 +44,46 @@
 """
         self.assertEquals(output.getvalue(), expected_result)
 
+    def test_while(self):
+        p = 7
+        prog = parse(
+"""
+def f(x):
+    x=3
+    y=1
+    while(x>0):
+        y=y+1
+        x=x-1
+    y
+""")
+        flow = Flow()
+        flow.flow(prog.body[0])
+        entry = flow.to_dot(prog.body[0])
+        output = StringIO.StringIO()
+        Graph(entry).to_dot(output)
+        expected_result = \
+"""digraph G {
+  in -> 7
+  in [shape = plaintext, label=""]
+    7 [label="def f(x):    x = 3    y = 1    while(x > 0):        y = y + 1        x = x - 1    y"]
+    7->6;
+    6 [label="x = 3"]
+    6->8;
+    8 [label="y = 1"]
+    8->9;
+    8->10;
+    10 [label="y"]
+    9 [label="while(x > 0):    y = y + 1    x = x - 1"]
+    9->11;
+    11 [label="y = y + 1"]
+    11->12;
+    12 [label="x = x - 1"]
+    12->9;
+    12->10;
+}
+"""
+        self.assertEquals(output.getvalue(), expected_result)
+
 
 if __name__ == '__main__':
     unittest.main()