]> hydra-www.ietfng.org Git - dyna2/commitdiff
add debug method to Rule.
authorTim Vieira <tim.f.vieira@gmail.com>
Mon, 29 Jul 2013 16:47:02 +0000 (12:47 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Mon, 29 Jul 2013 16:47:13 +0000 (12:47 -0400)
src/Dyna/Backend/Python/debug.py
src/Dyna/Backend/Python/interpreter.py
src/Dyna/Backend/Python/post/trace.py

index 2aa120aaea94d4649077df40fd006c8c5b98d9d9..715509aabdba17185eb3eea68656bb2d03cb6bef 100644 (file)
@@ -109,7 +109,8 @@ class Hypergraph(object):
 
                 # connect body variables to edge crux
                 for b in e.body:
-                    print >> f, '"%s" -> "%s" [arrowhead=none];' % (b, id(e))
+#                    print >> f, '"%s" -> "%s" [arrowhead=none];' % (b, id(e))
+                    print >> f, '"%s" -> "%s";' % (b, id(e))
 #                    print >> f, '"%s" -> "%s";' % (b, id(e))
 
                 # connect head variables to edge
index 8033c5aaedc026e2ab21c3f36c9c491826de5a4f..97c7703b745b9be8ceb72f0f6ef32a66be0460ba 100644 (file)
@@ -41,6 +41,12 @@ class Rule(object):
         c = Crux(head=None, rule=self, body=None, vs = ctx)
         return '\n'.join(indent + line for line in c.format())
 
+    def debug(self):
+        import debug
+        with file(dotdynadir / 'tmp.dyna', 'wb') as f:
+            f.write(self.src)
+        debug.main(f.name)
+
 
 # TODO: yuck, hopefully temporary measure to support pickling the Interpreter's
 # state
index bfa1a49cdd3afad7a04298ddc8c485d26c545f1b..166a0904e33e2e7c43643325f822d1ea7cc99c6c 100644 (file)
@@ -153,25 +153,30 @@ class Crux(object):
         explode = ('%s %s %s' % (self.get_function(rule.anf.head)[1:],  # drop quote on head
                                  green % rule.anf.agg,
                                  self.get_function(rule.anf.result)))
-
         if side:
             side = '    ' + ', '.join('%s %s' % ('for', x,) for x in side) + '.'
-            explode += ','
         else:
             explode += '.'
-
         lines = [explode]
-
         if side:
             lines.append(side)
-
         return lines
 
     def get_function(self, x):
+        self.visited = set()
+        return self._get_function(x)
+
+    def _get_function(self, x):
         """
         String of symbolic representation of ``x``, a variable or function, in
         this expresion graph.
         """
+
+        if x not in ('true', 'false'):
+            if x in self.visited:
+                return red % '*cycle@%s*' % x
+        self.visited.add(x)
+
         g = self.graph
         if isinstance(x, debug.Edge):
 
@@ -181,12 +186,12 @@ class Crux(object):
 
             if label == '=':
                 [b] = x.body
-                return self.get_function(b)
+                return self._get_function(b)
 
             if not x.body:  # arity 0
                 return label
 
-            fn_args = [self.get_function(y) for y in x.body]
+            fn_args = [self._get_function(y) for y in x.body]
 
             # infix
             if (not label[0].isalpha() and label[0] not in ('$','&') and len(fn_args) == 2) \
@@ -204,18 +209,18 @@ class Crux(object):
 
             # handle multiple values (can happen at unification nodes)
             if len(g.incoming[x]) > 1:
-                return ' = '.join('(%s%s)' % (self.get_function(e), (cyan % '=%s' % self.values(e.head))) for e in g.incoming[x])
+                return ' = '.join('(%s%s)' % (self._get_function(e), (cyan % '=%s' % self.values(e.head))) for e in g.incoming[x])
 
             [e] = g.incoming[x]
 
             if e.label == '=':
-                return self.get_function(e)
+                return self._get_function(e)
 
             # handle lists
-            if e.label == '& nil':
+            elif e.label == '& nil':
                 return '[]'
 
-            if e.label == '& cons':
+            elif e.label == '& cons':
                 _e = e
                 a = []
                 while e.label == '& cons':
@@ -236,14 +241,15 @@ class Crux(object):
                     # xs = 'uX'
 
                     [e] = g.incoming[xs]
-                    a.append(self.get_function(x))
+                    a.append(self._get_function(x))
 
                 if e.label == '& nil':
                     return '[%s]' % ', '.join(a)
                 else:
-                    return self.get_function(_e)          # malformed list.
+                    return self._get_function(_e)          # malformed list.
 
-            if e.label.startswith('&'):
-                return self.get_function(e)
+            elif e.label.startswith('&'):
+                return self._get_function(e)
 
-            return self.get_function(e) + (cyan % '=%s' % self.values(x))
+            else:
+                return self._get_function(e) + (cyan % '=%s' % self.values(x))