diff options
-rw-r--r-- | modules/language/python/compile.scm | 33 | ||||
-rw-r--r-- | modules/language/python/list.scm | 29 |
2 files changed, 55 insertions, 7 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm index 873b264..734fe60 100644 --- a/modules/language/python/compile.scm +++ b/modules/language/python/compile.scm @@ -50,12 +50,13 @@ x) (define-inlinable (C x) `(@@ (language python compile) ,x)) -(define-inlinable (Y x) `(@@ (language python yield) ,x)) -(define-inlinable (T x) `(@@ (language python try) ,x)) -(define-inlinable (F x) `(@@ (language python for) ,x)) -(define-inlinable (L x) `(@@ (language python list) ,x)) -(define-inlinable (O x) `(@@ (oop pf-objects) ,x)) -(define-inlinable (G x) `(@ (guile) ,x)) +(define-inlinable (Y x) `(@@ (language python yield) ,x)) +(define-inlinable (T x) `(@@ (language python try) ,x)) +(define-inlinable (F x) `(@@ (language python for) ,x)) +(define-inlinable (L x) `(@@ (language python list) ,x)) +(define-inlinable (A x) `(@@ (language python array) ,x)) +(define-inlinable (O x) `(@@ (oop pf-objects) ,x)) +(define-inlinable (G x) `(@ (guile) ,x)) (define (union as vs) (let lp ((as as) (vs vs)) @@ -293,6 +294,26 @@ trailer) (lp `(,e ,@(map (g vs exp) args)) trailer))) + ((#:subscripts (n #f #f)) + `(,(L 'pylist-ref) ,e ,(exp vs n))) + + ((#:subscripts (n1 n2 n3)) + (let ((w (lambda (x) (if (eq? x 'None) ''None x)))) + `(,(L 'pylist-slice) ,e + ,(w (exp vs n1)) ,(w (exp vs n2)) ,(w (exp vs n3))))) + + ((#:subscripts (n #f #f) ...) + `(,(A 'pyarray-ref) ,e (list ,@ (map (lambda (n) + (exp vs n)) + n)))) + + ((#:subscripts (n1 n2 n3) ...) + (let ((w (lambda (x) (if (eq? x 'None) ''None x)))) + `(,(A 'pyarray-slice) ,e + (list ,@(map (lambda (x y z) + `(list ,(exp vs x) ,(exp vs y) ,(exp vs z))) + n1 n2 n3))))) + (_ (error "unhandled trailer"))))))))))) (#:identifier diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm index 8f734d8..0b98b61 100644 --- a/modules/language/python/list.scm +++ b/modules/language/python/list.scm @@ -6,7 +6,8 @@ #:use-module (language python for) #:use-module (language python try) #:use-module (language python exceptions) - #:export (to-list pylist-ref pylist-set! pylist-append!)) + #:export (to-list pylist-ref pylist-set! pylist-append! + pylist-slice)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -46,7 +47,17 @@ (slot-set! o 'vec vec) o)) +(define-method (to-pylist (l <vector>)) + (to-pylist (vector->list l))) +(define-method (to-pylist l) + (if (null? l) + (let ((o (make <py-list>))) + (slot-set! o 'vec (make-vector 4)) + (slot-set! o 'n 0) + o) + (error "not able to make a pylist"))) + ;;; REF (define-method (pylist-ref (o <py-list>) n) (if (< n (slot-ref o 'n)) @@ -77,6 +88,22 @@ (define-method (pylist-set! (o <p>) n val) ((ref o '__listset__) n val)) +;;SLICE +(define-method (pylist-slice (o <py-list>) n1 n2 n3) + (let* ((n1 (if (eq? n1 'None) 0 n1)) + (n2 (if (eq? n2 'None) (slot-ref o 'n) n2)) + (n3 (if (eq? n3 'None) 1 n3)) + + (vec (slot-ref o 'vec)) + (l (let lp ((i n1)) + (if (< i n2) + (cons (vector-ref vec i) (lp (+ i n3))) + '())))) + (to-pylist l))) + +(define-method (pylist-slice o n1 n2 n3) + (pylist-slice (to-pylist o) n1 n2 n3)) + ;;APPEND (define-method (pylist-append! (o <py-list>) val) (let* ((n (slot-ref o 'n)) |