summaryrefslogtreecommitdiff
path: root/modules/language/python
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-09 21:40:34 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-09 21:40:34 +0200
commite4938284cef9e4048528839d29e5930b0e1928f4 (patch)
tree3453cd642afb4586c9445c948971f3321076148b /modules/language/python
parentc3010a227bf6ee70d74fa47d57ffa641e830f4f9 (diff)
reversed
Diffstat (limited to 'modules/language/python')
-rw-r--r--modules/language/python/for.scm5
-rw-r--r--modules/language/python/list.scm103
-rw-r--r--modules/language/python/module/python.scm6
3 files changed, 90 insertions, 24 deletions
diff --git a/modules/language/python/for.scm b/modules/language/python/for.scm
index f9cdd1e..cbbbbea 100644
--- a/modules/language/python/for.scm
+++ b/modules/language/python/for.scm
@@ -101,11 +101,6 @@
(define-method (next (l <p>))
((ref l '__next__)))
-(define-method (wrap-in (x <p>))
- (aif it (ref x '__iter__ #f)
- (it)
- x))
-
(define-method (wrap-in x)
(cond
((pair? x)
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm
index 36d15e9..b0111c1 100644
--- a/modules/language/python/list.scm
+++ b/modules/language/python/list.scm
@@ -16,8 +16,8 @@
pylist-pop! pylist-count pylist-extend! len in
pylist-insert! pylist-remove! pylist-sort!
pylist-index pylist-null pylist-delete!
- pylist pylist-listing
- py-all py-any))
+ pylist pylist-listing py-reversed
+ py-all py-any py-reversed))
(define scm-list list)
@@ -40,7 +40,6 @@
(slot-set! o 'vec (make-vector 0))
(slot-set! o 'n 0)
o))
-
(define-method (py-hash (o <py-list>))
(let ((n (min complexity (slot-ref o 'n)))
@@ -145,7 +144,7 @@
(defpair (pylist-set! o n val)
(list-set! o n val))
-(defpair (pylist-set! o n val)
+(define-method (pylist-set! (o <vector>) n val)
(vector-set! o n val))
(define-method (pylist-set! (o <p>) n val)
@@ -180,6 +179,7 @@
(to-list
(pylist-slice (to-pylist o) n1 n2 n3)))))
+
(defpair (pylist-slice o n1 n2 n3)
(to-list
(pylist-slice (to-pylist o) n1 n2 n3)))
@@ -472,14 +472,16 @@
(define-method (equal? o1 (o2 <py-list>)) #f)
-(define-class <py-list-iter> (<py-list>) i)
-(define-class <string-iter> () str i)
+(define-class <py-seq-iter> () o i n d)
+(define-class <py-list-iter> (<py-list>) i d)
+(define-class <string-iter> () str i d)
;;WRAP-IN
(define-method (wrap-in (s <string>))
(let ((out (make <string-iter>)))
(slot-set! out 'str s)
(slot-set! out 'i 0)
+ (slot-set! out 'd 1)
out))
(define-method (wrap-in (o <py-list>))
@@ -487,29 +489,95 @@
(slot-set! out 'n (slot-ref o 'n ))
(slot-set! out 'vec (slot-ref o 'vec))
(slot-set! out 'i 0)
+ (slot-set! out 'd 1)
+ out))
+
+(define-method (py-reversed (o <py-list>))
+ (let ((out (make <py-list-iter>)))
+ (slot-set! out 'i (- (slot-ref o 'n) 1))
+ (slot-set! out 'vec (slot-ref o 'vec))
+ (slot-set! out 'n 0)
+ (slot-set! out 'd -1)
out))
+(define-method (py-reversed (s <string>))
+ (let ((out (make <string-iter>)))
+ (slot-set! out 'str s)
+ (slot-set! out 'i 0)
+ (slot-set! out 'd -1)
+ out))
+
+(define-method (py-reversed (o <p>))
+ (aif it (ref o '__reversed__)
+ it
+ (let ((a (ref o '__getitem__))
+ (n (ref o '__len__)))
+ (if (and a n)
+ (let ((ret (make <py-seq-iter>)))
+ (slot-set! ret 'o a)
+ (slot-set! ret 'i (n))
+ (slot-set! ret 'n -1)
+ (slot-set! ret 'd -1))
+ (next-method)))))
+
+(define-method (wrap-in (o <p>))
+ (aif it (ref o '__iter__)
+ it
+ (let ((a (ref o '__getitem__)))
+ (if a
+ (let ((ret (make <py-seq-iter>)))
+ (slot-set! ret 'o a)
+ (slot-set! ret 'i 0)
+ (slot-set! ret 'n -1)
+ (slot-set! ret 'd 1))
+ (next-method)))))
+
+
(define-method (wrap-in (o <py-list-iter>)) o)
(define-method (wrap-in (o <string-iter> )) o)
+(define-method (wrap-in (o <py-seq-iter> )) o)
+
;;NEXT
+(define-method (next (o <py-seq-iter>))
+ (let ((i (slot-ref o 'i))
+ (d (slot-ref o 'd))
+ (a (slot-ref o 'a)))
+ (let ((r (a i)))
+ (slot-set! o 'i (+ i d))
+ r)))
+
(define-method (next (o <string-iter>))
(let ((i (slot-ref o 'i ))
+ (d (slot-ref o 'd))
(str (slot-ref o 'str)))
- (if (< i (string-length str))
- (let ((ret (string-ref str i)))
- (slot-set! o 'i (+ i 1))
- (list->string (scm-list ret)))
- (throw StopIteration))))
+ (if (> d 0)
+ (if (< i (string-length str))
+ (let ((ret (string-ref str i)))
+ (slot-set! o 'i (+ i d))
+ (list->string (scm-list ret)))
+ (throw StopIteration))
+ (if (>= i 0)
+ (let ((ret (string-ref str i)))
+ (slot-set! o 'i (+ i d))
+ (list->string (scm-list ret)))
+ (throw StopIteration)))))
(define-method (next (o <py-list-iter>))
(let ((i (slot-ref o 'i ))
+ (d (slot-ref o 'd))
(n (slot-ref o 'n ))
(vec (slot-ref o 'vec)))
- (if (< i n)
- (let ((ret (vector-ref vec i)))
- (slot-set! o 'i (+ i 1))
- ret)
- (throw StopIteration))))
+ (if (> d 0)
+ (if (< i n)
+ (let ((ret (vector-ref vec i)))
+ (slot-set! o 'i (+ i 1))
+ ret)
+ (throw StopIteration))
+ (if (>= i n)
+ (let ((ret (vector-ref vec i)))
+ (slot-set! o 'i (- i 1))
+ ret)
+ (throw StopIteration)))))
;;INSERT
(define-method (pylist-insert! (o <py-list>) i val)
@@ -645,7 +713,8 @@
(next-method)))
-#:len
+;; len
+
(defpair (len l) (length l))
(define-method (len (v <vector>)) (vector-length v))
diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm
index c391907..0dab709 100644
--- a/modules/language/python/module/python.scm
+++ b/modules/language/python/module/python.scm
@@ -27,9 +27,10 @@
#:re-export (Exception StopIteration send sendException next
GeneratorExit sendClose RuntimeError
len dir next dict None property range
- tuple)
+ tuple
+ )
#:export (print repr complex float int round
- set all any bin callable
+ set all any bin callable reversed
chr classmethod staticmethod
divmod enumerate filter format
getattr hasattr hex isinstance
@@ -200,3 +201,4 @@
(define min py-min)
(define max py-max)
(define list pylist)
+(define reversed py-reversed)