[pypy-svn] r26819 - pypy/dist/pypy/translator/cl

dialtone at codespeak.net dialtone at codespeak.net
Fri May 5 17:22:35 CEST 2006


Author: dialtone
Date: Fri May  5 17:22:34 2006
New Revision: 26819

Modified:
   pypy/dist/pypy/translator/cl/gencl.py
   pypy/dist/pypy/translator/cl/opformatter.py
Log:
actually support dictionary iterator in a lisp-portable way, unfortunately it's currently a work-around waiting for a better solution

Modified: pypy/dist/pypy/translator/cl/gencl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/gencl.py	(original)
+++ pypy/dist/pypy/translator/cl/gencl.py	Fri May  5 17:22:34 2006
@@ -67,6 +67,26 @@
         self.structcount += 1
         return name
 
+    def declare_dict_iter(self):
+        name = 'pypy-dict-iter'
+        if name in self.declarations:
+            return self.declarations[name][0]
+        definition = """\
+(defun %s (hash)
+  (let ((current-index -1)
+        (keys (loop for keys being the hash-keys in hash collect keys)))
+    (cons (lambda ()
+            (let ((more (<= (incf current-index) (1- (length keys)))))
+              (if more
+                (let* ((key (nth current-index keys))
+                       (val (gethash key hash)))
+                  (values more key val))
+                (values nil nil nil))))
+          (lambda ()
+            (nth current-index keys)))))""" % (name)
+        self.declarations[name] = (name,  definition)
+        return name
+
     def declare_class(self, cls):
         assert isinstance(cls, Instance)
         assert not self.is_exception_instance(cls)

Modified: pypy/dist/pypy/translator/cl/opformatter.py
==============================================================================
--- pypy/dist/pypy/translator/cl/opformatter.py	(original)
+++ pypy/dist/pypy/translator/cl/opformatter.py	Fri May  5 17:22:34 2006
@@ -115,7 +115,7 @@
             code = getattr(impl, method)(*args)
             yield "(setf %s %s)" % (result, code)
         elif isinstance(cls, Dict):
-            impl = DictImpl(selfvar)
+            impl = DictImpl(selfvar, self.gen)
             code = getattr(impl, method)(*args)
             yield "(setf %s %s)" % (result, code)
         elif isinstance(cls, DictItemsIterator):
@@ -131,7 +131,7 @@
 
     def op_oogetfield(self, result, obj, _):
         fieldname = self.args[1].value
-        if isinstance(self.args[0].concretetype, Record):
+        if isinstance(self.args[0].concretetype, Record):   
             yield "(setf %s (slot-value %s '%s))" % (clrepr(result, True),
                                                    clrepr(obj, True),
                                                    clrepr(fieldname, True))
@@ -184,8 +184,9 @@
 
 class DictImpl:
 
-    def __init__(self, obj):
+    def __init__(self, obj, gen):
         self.obj = obj
+        self.gen = gen
 
     def ll_length(self):
         return "(hash-table-count %s)" % (self.obj,)
@@ -202,11 +203,8 @@
     def ll_get_items_iterator(self):
         # This is explicitly unspecified by the specification.
         # Should think of a better way to do this.
-        return """\
-(let ((temp (gensym)))
-  (setf (symbol-value temp)
-    (with-hash-table-iterator (iter %s)
-      (lambda () (iter)))) temp)""" % (self.obj,)
+        name = self.gen.declare_dict_iter()
+        return "(%s %s)" % (name, self.obj)
 
 class DictItemsIteratorImpl:
 
@@ -216,10 +214,8 @@
     def ll_go_next(self):
         return """\
 (multiple-value-bind (more key value)
-    (funcall (symbol-value %s))
-  (setf (get %s 'key) key)
-  (setf (get %s 'value) value)
-  more)""" % (self.obj, self.obj, self.obj)
+    (funcall (car %s))
+  more)""" % (self.obj,)
 
     def ll_current_key(self):
-        return "(get %s 'key)" % (self.obj,)
+        return "(funcall (cdr %s))" % (self.obj,)



More information about the Pypy-commit mailing list