[pypy-svn] r12925 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Tue May 31 15:36:56 CEST 2005


Author: arigo
Date: Tue May 31 15:36:56 2005
New Revision: 12925

Added:
   pypy/dist/pypy/rpython/rstr.py   (contents, props changed)
   pypy/dist/pypy/rpython/test/test_rstr.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rptr.py
   pypy/dist/pypy/rpython/rtyper.py
Log:
- First try at implementing strings.
- is_true(), eq(), ne() operations on pointers.


Modified: pypy/dist/pypy/rpython/rptr.py
==============================================================================
--- pypy/dist/pypy/rpython/rptr.py	(original)
+++ pypy/dist/pypy/rpython/rptr.py	Tue May 31 15:36:56 2005
@@ -1,6 +1,6 @@
 from pypy.annotation.pairtype import pair, pairtype
 from pypy.annotation.model import SomePtr, SomeInteger
-from pypy.rpython.lltype import ContainerType, Void, Signed
+from pypy.rpython.lltype import ContainerType, Void, Signed, Bool
 
 
 class __extend__(SomePtr):
@@ -31,6 +31,10 @@
         return hop.genop('getarraysize', vlist,
                          resulttype = hop.s_result.lowleveltype())
 
+    def rtype_is_true(s_ptr, hop):
+        vlist = hop.inputargs(s_ptr)
+        return hop.genop('ptr_nonzero', vlist, resulttype=Bool)
+
 
 class __extend__(pairtype(SomePtr, SomeInteger)):
 
@@ -38,3 +42,14 @@
         vlist = hop.inputargs(s_ptr, Signed)
         return hop.genop('getarrayitem', vlist,
                          resulttype = hop.s_result.lowleveltype())
+
+
+class __extend__(pairtype(SomePtr, SomePtr)):
+
+    def rtype_eq(_, hop):
+        vlist = hop.inputargs(SomePtr(), SomePtr())
+        return hop.genop('ptr_eq', vlist, resulttype=Bool)
+
+    def rtype_ne(_, hop):
+        vlist = hop.inputargs(SomePtr(), SomePtr())
+        return hop.genop('ptr_ne', vlist, resulttype=Bool)

Added: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rstr.py	Tue May 31 15:36:56 2005
@@ -0,0 +1,104 @@
+from pypy.annotation.pairtype import pair, pairtype
+from pypy.annotation.model import SomeString, SomeChar, SomeInteger, SomeObject
+from pypy.rpython.lltype import *
+
+# ____________________________________________________________
+#
+#  Concrete implementation of RPython strings:
+#
+#    struct str {
+#        hash: Unsigned
+#        chars: array {
+#            item {
+#                Char ch
+#            }
+#        }
+#    }
+
+STR = GcStruct('str', ('hash',  Unsigned),
+                      ('chars', Array(('ch', Char))))
+STRPTR = GcPtr(STR)
+
+
+class __extend__(SomeString):
+
+    def lowleveltype(self):
+        return STRPTR
+
+    def rtype_len(_, hop):
+        v_str, = hop.inputargs(SomeString())
+        return hop.gendirectcall(ll_strlen, v_str)
+
+    def rtype_is_true(s_str, hop):
+        if s_str.can_be_None:
+            v_str, = hop.inputargs(SomeString())
+            return hop.gendirectcall(ll_str_is_true, v_str)
+        else:
+            # defaults to checking the length
+            return SomeObject.rtype_is_true(s_str, hop)
+
+
+class __extend__(pairtype(SomeString, SomeInteger)):
+
+    def rtype_getitem((_, s_int), hop):
+        v_str, v_index = hop.inputargs(SomeString(), Signed)
+        if s_int.nonneg:
+            llfn = ll_stritem_nonneg
+        else:
+            llfn = ll_stritem
+        return hop.gendirectcall(llfn, v_str, v_index)
+
+
+class __extend__(SomeChar):
+
+    def lowleveltype(self):
+        return Char
+
+    def rtype_len(_, hop):
+        return hop.inputconst(Signed, 1)
+
+    def rtype_is_true(s_chr, hop):
+        assert not s_chr.can_be_None
+        return hop.inputconst(Bool, True)
+
+
+class __extend__(pairtype(SomeChar, SomeString)):
+
+    def rtype_convert_from_to((s_chr, s_str), v, llops):
+        return hop.gendirectcall(ll_chr2str, v)
+
+
+class __extend__(pairtype(SomeString, SomeString)):
+
+    def rtype_convert_from_to((s_str1, s_str2), v, llops):
+        # converting between SomeString(can_be_None=False)
+        #                and SomeString(can_be_None=True)
+        assert s_str1.__class__ is s_str2.__class__ is SomeString
+        return v
+
+
+# ____________________________________________________________
+#
+#  Low-level methods.  These can be run for testing, but are meant to
+#  be direct_call'ed from rtyped flow graphs, which means that they will
+#  get flowed and annotated, mostly with SomePtr.
+
+def ll_strlen(s):
+    return len(s.chars)
+
+def ll_stritem_nonneg(s, i):
+    return s.chars[i].ch
+
+def ll_stritem(s, i):
+    if i<0:
+        i += len(s.chars)
+    return s.chars[i].ch
+
+def ll_str_is_true(s):
+    # check if a string is True, allowing for None
+    return bool(s) and len(s.chars) != 0
+
+def ll_chr2str(ch):
+    s = malloc(STR, 1)
+    s.chars[0].ch = ch
+    return s

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Tue May 31 15:36:56 2005
@@ -361,4 +361,4 @@
 # _______________________________________________________________________
 # this has the side-effect of registering the unary and binary operations
 from pypy.rpython import robject, rlist, rptr, rbuiltin, rint, rbool, rfloat
-from pypy.rpython import rpbc
+from pypy.rpython import rpbc, rstr

Added: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Tue May 31 15:36:56 2005
@@ -0,0 +1,33 @@
+from pypy.translator.translator import Translator
+from pypy.rpython.lltype import *
+from pypy.rpython.rtyper import RPythonTyper
+
+
+def test_simple():
+    def dummyfn(i):
+        s = 'hello'
+        return s[i]
+
+    t = Translator(dummyfn)
+    t.annotate([int])
+    typer = RPythonTyper(t.annotator)
+    typer.specialize()
+    #t.view()
+    t.checkgraphs()
+
+
+def test_nonzero():
+    def dummyfn(i, s):
+        if i < 0:
+            s = None
+        if i > -2:
+            return bool(s)
+        else:
+            return False
+
+    t = Translator(dummyfn)
+    t.annotate([int, str])
+    typer = RPythonTyper(t.annotator)
+    typer.specialize()
+    t.view()
+    t.checkgraphs()



More information about the Pypy-commit mailing list