]> hydra-www.ietfng.org Git - dyna2/commitdiff
multiline input in doctest
authorTim Vieira <tim.f.vieira@gmail.com>
Wed, 3 Jul 2013 23:19:20 +0000 (19:19 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Wed, 3 Jul 2013 23:19:20 +0000 (19:19 -0400)
disable REPL run command.. it doesn't pass parser state along.

src/Dyna/Backend/Python/dyna-doctest.py
src/Dyna/Backend/Python/interpreter.py
src/Dyna/Backend/Python/repl.py

index 9be0c3fd59c0bce0190b4d97fec13ac85a41662b..fcc3a6c23736c6c025aefa158230950bdc13ddb0 100755 (executable)
@@ -1,8 +1,6 @@
 #!/usr/bin/env python
 import re, sys
 
-sys.path.append('src/Dyna/Backend/Python')
-
 from interpreter import Interpreter
 from repl import REPL
 from cStringIO import StringIO
@@ -12,8 +10,17 @@ from utils import red, green, yellow, strip_comments
 
 def extract(code):
     for block in re.compile('^> ', re.MULTILINE).split(code):
-        for cmd, expect in re.findall('(.*?)\n([\w\W]*)$', block):
-            yield cmd, expect
+        cmd = []
+        expect = []
+        for i, line in enumerate(block.split('\n')):
+            if line.startswith('|') or i == 0:
+                if line.startswith('|'):
+                    line = line[1:]
+                cmd.append(line)
+            else:
+                expect.append(line)
+
+        yield '\n'.join(cmd).strip(), '\n'.join(expect).strip()
 
 
 def run(code):
@@ -25,6 +32,11 @@ def run(code):
             print
             continue
         print yellow % '> %s' % cmd
+
+        if strip_comments(cmd) == '*resume*':
+            repl.cmdloop()
+            continue
+
         sys.stdout = x = StringIO()
         try:
             repl.onecmd(cmd)
@@ -32,19 +44,26 @@ def run(code):
             sys.stdout = sys.__stdout__
         got = x.getvalue().strip()
         expect = expect.strip()
+
+        if strip_comments(expect) == '*ignore*':
+            continue
+
         if strip_comments(expect) != strip_comments(got):
             print green % expect
             print red % got
-            errors.append(cmd, expect, got)
+            errors.append([cmd, expect, got])
         else:
             print x.getvalue().rstrip()
         print
 
     if not errors:
         print green % 'PASS!'
+        print
     else:
         print red % '%s errors' % len(errors)
-    print
+        print
+        sys.exit(1)
+
 
 if __name__ == '__main__':
     for filename in sys.argv[1:]:
index 6c0cc9b0f8bb6a50f4930433fd98faeb34260843..b81eb2890afb10e060df9866ca634fee5c9682e4 100644 (file)
@@ -596,8 +596,9 @@ class Interpreter(object):
             return item
         for i in new_rules:
             r = self.rules[i]
-            agg, head, evals, unifs, result = r.anf[2:]
-            r.item = rule(i, r.src, todyna([head, agg, result, evals, unifs]), r.init, r.query)
+            if hasattr(r, 'anf'):   # XXX: all rules should have ANF!
+                agg, head, evals, unifs, result = r.anf[2:]
+                r.item = rule(i, r.src, todyna([head, agg, result, evals, unifs]), r.init, r.query)
         #-----------------------------------------
 
         return self.go()
index 0a9ee1436c84342da84ff81ee71a6c9f086ae680..efe7b286696b61cf5a013834666dd2290a6fef74 100644 (file)
@@ -143,19 +143,19 @@ class REPL(cmd.Cmd, object):
 #            f.write(line)
 #        debug.main(f.name)
 
-    def do_run(self, filename):
-        """
-        Load dyna rules from `filename`.
-
-        > run examples/papa.dyna
-
-        """
-        try:
-            changed = self.interp.do(self.interp.dynac(filename))
-        except DynaCompilerError as e:
-            print e
-        else:
-            self._changed(changed)
+#    def do_run(self, filename):
+#        """
+#        Load dyna rules from `filename`.
+#
+#        > run examples/papa.dyna
+#
+#        """
+#        try:
+#            changed = self.interp.do(self.interp.dynac(filename))
+#        except DynaCompilerError as e:
+#            print e
+#        else:
+#            self._changed(changed)
 
     def _query(self, q):
 
@@ -272,16 +272,19 @@ class REPL(cmd.Cmd, object):
                 self._changed(changed)
 
     def _changed(self, changed):
+        if not changed:
+            return
+
+        changed = [x for x in changed if not x.fn.startswith('$rule/')]
+
         if not changed:
             return
 
         print
         print 'Changes'
         print '======='
-        for x, v in sorted(changed.items()):
-            if x.fn.startswith('$rule/'):
-                continue
-            print '%s = %s.' % (x, _repr(v))
+        for x in sorted(changed):
+            print '%s = %s.' % (x, _repr(x.value))
         print
 
 #    def _changed_subscriptions(self, changed):