[pypy-svn] r37323 - in pypy/dist/pypy/lang/prolog: builtin builtin/test interpreter interpreter/test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Jan 25 13:18:02 CET 2007


Author: cfbolz
Date: Thu Jan 25 13:17:59 2007
New Revision: 37323

Modified:
   pypy/dist/pypy/lang/prolog/builtin/atomconstruction.py
   pypy/dist/pypy/lang/prolog/builtin/formatting.py
   pypy/dist/pypy/lang/prolog/builtin/test/test_formatting.py
   pypy/dist/pypy/lang/prolog/interpreter/engine.py
   pypy/dist/pypy/lang/prolog/interpreter/test/test_builtin.py
Log:
changes that I had sitting in my working copy for a long while. mostly small
cleanups


Modified: pypy/dist/pypy/lang/prolog/builtin/atomconstruction.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/builtin/atomconstruction.py	(original)
+++ pypy/dist/pypy/lang/prolog/builtin/atomconstruction.py	Thu Jan 25 13:17:59 2007
@@ -70,21 +70,34 @@
         if startbefore < 0:
             startbefore = 0
             stopbefore = len(s) + 1
+    oldstate = engine.frame.branch()
     if not isinstance(sub, term.Var):
         s1 = helper.unwrap_atom(sub)
         if len(s1) >= stoplength or len(s1) < startlength:
             raise error.UnificationFailed()
-        i = s.find(s1)
-        if not startbefore <= i < stopbefore:
-            raise error.UnificationFailed()
-        if not startlength <= len(s1) < stoplength:
-            raise error.UnificationFailed()
+        start = startbefore
+        while True:
+            try:
+                try:
+                    b = s.find(s1, start, stopbefore + len(s1)) # XXX -1?
+                    if b == -1:
+                        break
+                    start = b + 1
+                    before.unify(term.Number(b), engine.frame)
+                    after.unify(term.Number(len(s) - len(s1) - b), engine.frame)
+                    length.unify(term.Number(len(s1)), engine.frame)
+                    return continuation.call(engine)
+                except:
+                    engine.frame.revert(oldstate)
+                    raise
+            except error.UnificationFailed:
+                pass
+        raise error.UnificationFailed()
     if isinstance(after, term.Var):
         for b in range(startbefore, stopbefore):
             for l in range(startlength, stoplength):
                 if l + b > len(s):
                     continue
-                oldstate = engine.frame.branch()
                 try:
                     try:
                         before.unify(term.Number(b), engine.frame)
@@ -104,7 +117,6 @@
             assert b >= 0
             if l + b > len(s):
                 continue
-            oldstate = engine.frame.branch()
             try:
                 try:
                     before.unify(term.Number(b), engine.frame)

Modified: pypy/dist/pypy/lang/prolog/builtin/formatting.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/builtin/formatting.py	(original)
+++ pypy/dist/pypy/lang/prolog/builtin/formatting.py	Thu Jan 25 13:17:59 2007
@@ -61,8 +61,8 @@
         if self.quoted:
             try:
                 tokens = parsing.lexer.tokenize(s)
-                if (len(tokens) == 1 and tokens[0][0] == 'ATOM' and
-                    tokens[0][1] == s):
+                if (len(tokens) == 1 and tokens[0].name == 'ATOM' and
+                    tokens[0].source == s):
                     return s
             except LexerError:
                 pass

Modified: pypy/dist/pypy/lang/prolog/builtin/test/test_formatting.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/builtin/test/test_formatting.py	(original)
+++ pypy/dist/pypy/lang/prolog/builtin/test/test_formatting.py	Thu Jan 25 13:17:59 2007
@@ -19,3 +19,14 @@
     assert f.format(t) == "1+3*2"
     t = parse_query_term("'*'(1, *(3, 2)).")
     assert f.format(t) == "1*(3*2)"
+
+def test_atom_formatting():
+    f = formatting.TermFormatter(Engine(), quoted=False, ignore_ops=False)
+    t = parse_query_term("'abc def'.")
+    assert f.format(t) == "abc def"
+    f = formatting.TermFormatter(Engine(), quoted=True, ignore_ops=False)
+    t = parse_query_term("'abc def'.")
+    assert f.format(t) == "'abc def'"
+    t = parse_query_term("abc.")
+    assert f.format(t) == "abc"
+

Modified: pypy/dist/pypy/lang/prolog/interpreter/engine.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/interpreter/engine.py	(original)
+++ pypy/dist/pypy/lang/prolog/interpreter/engine.py	Thu Jan 25 13:17:59 2007
@@ -83,6 +83,11 @@
     def maxvar(self):
         return self.needed_vars
 
+    def newvar(self):
+        result = Var(self.maxvar)
+        self.extend(1)
+        return result
+
 class Engine(object):
     def __init__(self):
         self.frame = Frame()

Modified: pypy/dist/pypy/lang/prolog/interpreter/test/test_builtin.py
==============================================================================
--- pypy/dist/pypy/lang/prolog/interpreter/test/test_builtin.py	(original)
+++ pypy/dist/pypy/lang/prolog/interpreter/test/test_builtin.py	Thu Jan 25 13:17:59 2007
@@ -211,6 +211,7 @@
     assert_false("float(12).")
     assert_true("number(123).")
     assert_true("number(42.42).")
+    assert_false("number(abc).")
     assert_false("integer(a).")
     assert_false("integer(X).")
     assert_true("var(X).")
@@ -320,6 +321,9 @@
 
 def test_sub_atom():
     assert_true("sub_atom(abc, B, L, A, bc), B=1, L=2, A=0.")
+    assert_false("sub_atom(abc, B, 1, A, bc).")
+    assert_true("sub_atom(abcabcabc, 3, 3, A, abc), A=3.")
+    assert_true("sub_atom(abcabcabc, B, L, 3, abc), B=3, L=3.")
 
 def test_findall():
     assert_true("findall(X, (X = a; X = b; X = c), L), L = [a, b, c].")



More information about the Pypy-commit mailing list