changeset 275:92577eba8379

Graph, flow: more meaningfull interface for making graphs Now the graph has the nodes, and all nodes in the graph are counted from 0.
author Sigurd Meldgaard <stm@daimi.au.dk>
date Thu, 11 Mar 2010 11:14:25 +0100
parents 3d30f2a64034
children ebec4ece9b58
files pysmcl/flow.py pysmcl/graph.py
diffstat 2 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/flow.py	Thu Mar 11 11:12:41 2010 +0100
+++ b/pysmcl/flow.py	Thu Mar 11 11:14:25 2010 +0100
@@ -2,7 +2,7 @@
 from copy import copy
 
 import pysmcl.ast_wrapper as ast
-from pysmcl.graph import Node
+from pysmcl.graph import Graph
 from pysmcl.pretty_print import PrettyPrinter
 
 
@@ -131,12 +131,14 @@
                 current = set([stm])
         return current
 
-    def to_dot(self, entry):
-        """Returns a pysmcl.graph.Node which is the dot node corresponding to entry.
+    def to_graph(self, entry):
+        """Returns a pysmcl.graph.Graph representing the flow-graph.
 
         entry (ast.Expr)
         """
 
+        graph = Graph()
+
         def pretty(ast_node):
             output = StringIO.StringIO()
             printer = PrettyPrinter(output)
@@ -145,7 +147,7 @@
             return s.replace('\n', '')
 
         def depth_first(entry):
-            node = Node([], pretty(entry))
+            node = graph.add_node([], pretty(entry))
             processed[entry] = node
             for child in entry.children:
                 if not child in processed:
@@ -153,5 +155,9 @@
                 node.out.append(processed[child])
         processed = {}
         depth_first(entry)
-        return processed[entry]
+        graph.entry =processed[entry]
+        return graph
 
+
+    def to_dot_string(self, entry):
+        return self.to_graph(entry).to_dot_string()
--- a/pysmcl/graph.py	Thu Mar 11 11:12:41 2010 +0100
+++ b/pysmcl/graph.py	Thu Mar 11 11:14:25 2010 +0100
@@ -3,19 +3,22 @@
 
 
 class Node:
-    count = 0
 
-    def __init__(self, out, msg = ""):
+    def __init__(self, out, n, msg = ""):
         self.out = out
-        Node.count += 1
-        self.nr = str(Node.count)
+        self.nr = str(n)
         self.msg = msg
 
 
 class Graph:
 
-    def __init__(self, entry):
-        self.entry = entry
+    def __init__(self):
+        self.count=1
+
+    def add_node(self, out, msg = ""):
+        n = Node(out, self.count, msg)
+        self.count += 1
+        return n
 
     def to_dot(self, output=sys.stdout):
         taken = set()