changeset 89:3bb5d6df094a

Made a simpler to_dot in flow.py using a depth-first recursive run-through, testcases are correct now, but nodes where numbered in wrong order, this is fixed
author Sigurd Meldgaard <stm@daimi.au.dk>
date Fri, 29 May 2009 12:13:27 +0200
parents 0fb9ae8d823d
children 73aaee69ec44
files pysmcl/flow.py pysmcl/test/unit/test_flow.py
diffstat 2 files changed, 24 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/flow.py	Fri May 29 12:08:34 2009 +0200
+++ b/pysmcl/flow.py	Fri May 29 12:13:27 2009 +0200
@@ -120,26 +120,14 @@
             printer.visit(ast_node)
             s = output.getvalue()
             return s.replace('\n', '')
-        pending_list = {}
-        worklist = [entry]
+        def depth_first(entry):
+            node = Node([], pretty(entry))
+            processed[entry] = node
+            for child in entry.children:
+                if not child in processed:
+                    depth_first(child)
+                node.out.append(processed[child])
         processed = {}
-        while len(worklist) > 0:
-            current = worklist.pop()
-            children = []
-            for n in current.children:
-                if n in processed:
-                    children.append(processed[n])
-                else:
-                    pending_list[n] = Node([], pretty(n))
-                    children.append(pending_list[n])
-                    worklist.append(n)
-
-            if current in pending_list:
-                node = pending_list[current]
-                node.out = children
-                del pending_list[current]
-            else:
-                node = Node(children, pretty(current))
-            processed[current] = node
+        depth_first(entry)
         return processed[entry]
 
--- a/pysmcl/test/unit/test_flow.py	Fri May 29 12:08:34 2009 +0200
+++ b/pysmcl/test/unit/test_flow.py	Fri May 29 12:13:27 2009 +0200
@@ -28,18 +28,18 @@
         Graph(entry).to_dot(output)
         expected_result = \
 """digraph G {
-  in -> 2
+  in -> 1
   in [shape = plaintext, label=""]
-    2 [label="def f(x):    a = 3    if(x):        a = a + 1    z = (a + 2) - a"]
-    2->1;
-    1 [label="a = 3"]
-    1->3;
+    1 [label="def f(x):    a = 3    if(x):        a = a + 1    z = (a + 2) - a"]
+    1->2;
+    2 [label="a = 3"]
+    2->3;
     3 [label="if(x):    a = a + 1"]
     3->4;
     3->5;
-    5 [label="z = (a + 2) - a"]
-    4 [label="a = a + 1"]
-    4->5;
+    5 [label="a = a + 1"]
+    5->4;
+    4 [label="z = (a + 2) - a"]
 }
 """
         self.assertEquals(output.getvalue(), expected_result)
@@ -63,23 +63,23 @@
         Graph(entry).to_dot(output)
         expected_result = \
 """digraph G {
-  in -> 7
+  in -> 6
   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;
+    6 [label="def f(x):    x = 3    y = 1    while(x > 0):        y = y + 1        x = x - 1    y"]
+    6->7;
+    7 [label="x = 3"]
+    7->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;
+    10 [label="while(x > 0):    y = y + 1    x = x - 1"]
+    10->11;
     11 [label="y = y + 1"]
     11->12;
     12 [label="x = x - 1"]
     12->9;
     12->10;
+    9 [label="y"]
 }
 """
         self.assertEquals(output.getvalue(), expected_result)