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

arigo at codespeak.net arigo at codespeak.net
Mon Jun 6 14:20:27 CEST 2005


Author: arigo
Date: Mon Jun  6 14:20:24 2005
New Revision: 13099

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/rrange.py
   pypy/dist/pypy/rpython/test/test_rlist.py
   pypy/dist/pypy/rpython/test/test_rrange.py
Log:
Fixed list iterators and copied them to make range iterators.


Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Mon Jun  6 14:20:24 2005
@@ -140,8 +140,8 @@
         citerptr = hop.inputconst(Void, self.lowleveltype)
         return hop.gendirectcall(ll_listiter, citerptr, v_lst)
 
-    def next(self, hop):
-        v_iter = hop.inputargs(self)
+    def rtype_next(self, hop):
+        v_iter, = hop.inputargs(self)
         return hop.gendirectcall(ll_listnext, v_iter)
 
 def ll_listiter(ITERPTR, lst):
@@ -156,4 +156,4 @@
     if index >= len(l.items):
         raise StopIteration
     iter.index = index + 1
-    return l.items[index]
+    return l.items[index].item

Modified: pypy/dist/pypy/rpython/rrange.py
==============================================================================
--- pypy/dist/pypy/rpython/rrange.py	(original)
+++ pypy/dist/pypy/rpython/rrange.py	Mon Jun  6 14:20:24 2005
@@ -13,10 +13,11 @@
 #    }
 
 RANGE = GcStruct("range", ("start", Signed), ("stop", Signed))
+RANGEITER = GcStruct("range", ("next", Signed), ("stop", Signed))
 
 
 class RangeRepr(Repr):
-    lowlevelrepr = GcPtr(RANGE)
+    lowleveltype = GcPtr(RANGE)
 
     def __init__(self, step):
         self.step = step
@@ -27,7 +28,7 @@
         return hop.gendirectcall(ll_rangelen, v_rng, cstep)
 
     def make_iterator_repr(self):
-        return riter.RangeIteratorRepr(self)
+        return RangeIteratorRepr(self)
 
 class __extend__(pairtype(RangeRepr, IntegerRepr)):
 
@@ -95,3 +96,47 @@
         raise TyperError("range() result used as a normal list: "
                          "XXX not implemented")
         #return hop.gendirectcall(ll_range2list, vstart, vstop, vstep)
+
+# ____________________________________________________________
+#
+#  Iteration.
+
+class RangeIteratorRepr(Repr):
+    lowleveltype = GcPtr(RANGEITER)
+
+    def __init__(self, r_rng):
+        self.r_rng = r_rng
+
+    def newiter(self, hop):
+        v_rng, = hop.inputargs(self.r_rng)
+        citerptr = hop.inputconst(Void, self.lowleveltype)
+        return hop.gendirectcall(ll_rangeiter, citerptr, v_rng)
+
+    def rtype_next(self, hop):
+        v_iter, = hop.inputargs(self)
+        cstep = hop.inputconst(Signed, self.r_rng.step)
+        if self.r_rng.step > 0:
+            llfn = ll_rangenext_up
+        else:
+            llfn = ll_rangenext_down
+        return hop.gendirectcall(llfn, v_iter, cstep)
+
+def ll_rangeiter(ITERPTR, rng):
+    iter = malloc(ITERPTR.TO)
+    iter.next = rng.start
+    iter.stop = rng.stop
+    return iter
+
+def ll_rangenext_up(iter, step):
+    next = iter.next
+    if next >= iter.stop:
+        raise StopIteration
+    iter.next = next + step
+    return next
+
+def ll_rangenext_down(iter, step):
+    next = iter.next
+    if next <= iter.stop:
+        raise StopIteration
+    iter.next = next + step
+    return next

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Mon Jun  6 14:20:24 2005
@@ -21,57 +21,41 @@
 
 # ____________________________________________________________
 
-def test_simple():
-    def dummyfn():
-        l = [10,20,30]
-        return l[2]
-
-    t = Translator(dummyfn)
-    t.annotate([])
+def rtype(fn, argtypes=[]):
+    t = Translator(fn)
+    t.annotate(argtypes)
     typer = RPythonTyper(t.annotator)
     typer.specialize()
     #t.view()
     t.checkgraphs()
+    return t
 
 
+def test_simple():
+    def dummyfn():
+        l = [10,20,30]
+        return l[2]
+    rtype(dummyfn)
+
 def test_append():
     def dummyfn():
         l = []
         l.append(5)
         l.append(6)
         return l[0]
-
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    #t.view()
-    t.checkgraphs()
-
+    rtype(dummyfn)
 
 def test_len():
     def dummyfn():
         l = [5,10]
         return len(l)
+    rtype(dummyfn)
 
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    #t.view()
-    t.checkgraphs()
-
-
-def DONT_YET_test_range():
-    def dummyfn(N):
+def test_iterate():
+    def dummyfn():
         total = 0
-        for i in range(N):
-            total += i
+        for x in [1,3,5,7,9]:
+            total += x
         return total
+    rtype(dummyfn)
 
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    t.view()
-    t.checkgraphs()

Modified: pypy/dist/pypy/rpython/test/test_rrange.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rrange.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rrange.py	Mon Jun  6 14:20:24 2005
@@ -1,3 +1,5 @@
+from pypy.translator.translator import Translator
+from pypy.rpython.rtyper import RPythonTyper
 from pypy.rpython.rrange import *
 
 def test_rlist_range():
@@ -17,3 +19,23 @@
         for stop in (-8, 0, 4, 8, 25):
             for step in (1, 2, 3, -1, -2):
                 test1(start, stop, step)
+
+# ____________________________________________________________
+
+def rtype(fn, argtypes=[]):
+    t = Translator(fn)
+    t.annotate(argtypes)
+    typer = RPythonTyper(t.annotator)
+    typer.specialize()
+    #t.view()
+    t.checkgraphs()
+    return t
+
+
+def test_range():
+    def dummyfn(N):
+        total = 0
+        for i in range(N):
+            total += i
+        return total
+    rtype(dummyfn, [int])



More information about the Pypy-commit mailing list