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

arigo at codespeak.net arigo at codespeak.net
Sat May 14 13:50:40 CEST 2005


Author: arigo
Date: Sat May 14 13:50:40 2005
New Revision: 12263

Modified:
   pypy/dist/pypy/rpython/lltypes.py
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
   pypy/dist/pypy/rpython/typer.py
Log:
list.append() and supporting code.


Modified: pypy/dist/pypy/rpython/lltypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypes.py	Sat May 14 13:50:40 2005
@@ -194,7 +194,11 @@
     def _example(self):
         o = self.TO._container_example()
         return _ptr(self, o)
-        
+
+    def withflags(self, **flags):
+        newflags = self.flags.copy()
+        newflags.update(flags)
+        return _PtrType(self.TO, **newflags)
 
 def GcPtr(TO, **flags):
     return _PtrType(TO, gc=True, **flags)

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Sat May 14 13:50:40 2005
@@ -16,27 +16,30 @@
 
     def define(self, typer):
         self.ITEM = typer.annotation2concretetype(self.s_item)
-        self.LIST.become(Struct("list",
-                                ("items", GcPtr(Array(('item', self.ITEM))))))
+        LISTPTR = self.LISTPTR
+        LIST    = self.LIST
+        ITEM    = self.ITEM
+        LIST.become(Struct("list",
+                           ("items", GcPtr(Array(('item', ITEM))))))
 
         def getitem(l, i):
             return l.items[i].item
 
         typer['getitem', self.s_list, SomeInteger()] = (
-            getitem, self.LISTPTR, Signed, self.ITEM)
+               getitem,  LISTPTR,     Signed,       ITEM)
 
-    ##    def append(l, newitem):
-    ##        length = len(l.items)
-    ##        newitems = malloc(List_typ.items.TO, length+1)
-    ##        i = 0
-    ##        while i<length:
-    ##          newitems[i].item = l.items[i].item
-    ##          i += 1
-    ##        newitems[length].item = newitem
-    ##        l.items = newitems
-
-    ##    Registry['getattr', ...
+        def append(l, newitem):
+            length = len(l.items)
+            newitems = malloc(LIST.items.TO, length+1)
+            i = 0
+            while i<length:
+                newitems[i].item = l.items[i].item
+                i += 1
+            newitems[length].item = newitem
+            l.items = newitems
 
+        typer.registermethod(('append', self.s_list, self.s_item),
+                             ( append,  LISTPTR,     ITEM,       Void))
 
 
 def substitute_newlist(typer, op):

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	Sat May 14 13:50:40 2005
@@ -14,3 +14,18 @@
     typer.specialize()
     #t.view()
     assert "did not crash"
+
+
+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()
+    assert "did not crash"

Modified: pypy/dist/pypy/rpython/typer.py
==============================================================================
--- pypy/dist/pypy/rpython/typer.py	(original)
+++ pypy/dist/pypy/rpython/typer.py	Sat May 14 13:50:40 2005
@@ -45,6 +45,32 @@
         # items inserted last have higher priority
         patternlist.insert(0, (pattern[1:], substitution))
 
+    def registermethod(self, pattern, substitution):
+        # method calls are decomposed in two operations that must be
+        # handled separately:
+        #
+        #  v1 = getattr(self, 'method_name') --> v1 = cast_flags(self)
+        #  v2 = simple_call(v1, ...)         --> v2 = simple_call(meth, v1, ...)
+        #
+        # where 'v1' becomes a pointer with the (method='method_name') flag.
+        # It points to 'self', but the flag modifies its meaning to
+        # "pointer to the method 'method_name' of self" instead of just
+        # "pointer to self".
+        #
+        method_name = pattern[0]
+        s_self      = pattern[1]
+        method      = substitution[0]
+        SELFPTR     = substitution[1]
+        METHODPTR   = SELFPTR.withflags(method=method_name)
+        s_method_name = self.annotator.bookkeeper.immutablevalue(method_name)
+
+        self['getattr',    s_self,  s_method_name] = (
+             'cast_flags', SELFPTR,     None,     METHODPTR)
+
+        s_method = s_self.find_method(method_name)
+        self[('simple_call', s_method) + pattern[2:]] = (
+               method,       SELFPTR)  + substitution[2:]
+
     def maketype(self, cls, s_annotation):
         try:
             return self.typecache[cls, s_annotation]



More information about the Pypy-commit mailing list