diff options
Diffstat (limited to 'modules/language')
-rw-r--r-- | modules/language/python/exceptions.scm | 5 | ||||
-rw-r--r-- | modules/language/python/for.scm | 2 | ||||
-rw-r--r-- | modules/language/python/list.scm | 148 |
3 files changed, 148 insertions, 7 deletions
diff --git a/modules/language/python/exceptions.scm b/modules/language/python/exceptions.scm index 5b3b5ca..00e7074 100644 --- a/modules/language/python/exceptions.scm +++ b/modules/language/python/exceptions.scm @@ -2,7 +2,7 @@ #:use-module (oop pf-objects) #:use-module (oop goops) #:export (StopIteration GeneratorExit RuntimeError - Exception + Exception ValueError IndexError)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -11,7 +11,8 @@ (define GeneratorExit 'GeneratorExit) (define RuntimeError 'RuntimeError) (define IndexError 'IndexError) - +(define ValueError 'ValueError) + (define-python-class Exception () (define __init__ (case-lambda diff --git a/modules/language/python/for.scm b/modules/language/python/for.scm index fade3f2..f9cdd1e 100644 --- a/modules/language/python/for.scm +++ b/modules/language/python/for.scm @@ -4,7 +4,7 @@ #:use-module (language python exceptions) #:use-module (oop goops) #:use-module (ice-9 control) - #:export (for break next)) + #:export (for break next wrap-in)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm index ac35e88..3976ca6 100644 --- a/modules/language/python/list.scm +++ b/modules/language/python/list.scm @@ -1,4 +1,5 @@ (define-module (language python list) + #:use-module (ice-9 match) #:use-module (oop pf-objects) #:use-module (oop goops) #:use-module (language python exceptions) @@ -8,7 +9,8 @@ #:use-module (language python exceptions) #:export (to-list pylist-ref pylist-set! pylist-append! pylist-slice pylist-subset! pylist-reverse! - pylist-pop! pylist-count)) + pylist-pop! pylist-count pylist-extend! + pylist-insert! pylist-remove! pylist-sort!)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -303,7 +305,8 @@ (vector-set! vec i (vector-ref vec k)) (vector-set! vec k swap)))))) -(define-method (pylist-reverse! (o <p>)) ((ref o 'reverse))) +(define-method (pylist-reverse! (o <p>) . l) + (apply (ref o 'reverse) l)) ;;POP! (define-method (pylist-pop! (o <py-list>)) @@ -317,7 +320,8 @@ ret) (raise IndexError "pop from empty list")))) -(define-method (pylist-pop! (o <p>)) ((ref o 'pop))) +(define-method (pylist-pop! (o <p>) . l) + (apply (ref o 'pop) l)) ;;COUNT (define-method (pylist-count (o <py-list>) q) @@ -349,4 +353,140 @@ (lp (cdr l) sum )) sum))) -(define-method (pylist-count (o <p>) q) ((ref o 'count) q)) +(define-method (pylist-count (o <p>) . l) + (apply (ref o 'count) l)) + +;; extend +(define-method (pylist-extend! (o <py-list>) iter) + (for ((x : iter)) () + (pylist-append! o x))) + +(define-method (pylist-extend! (o <p>) . l) + (apply (ref o 'extend) l)) + +;; equal? +(define-method (equal? (o1 <py-list>) (o2 <py-list>)) + (let ((n1 (slot-ref o1 'n)) + (n2 (slot-ref o2 'n)) + (vec1 (slot-ref o1 'vec)) + (vec2 (slot-ref o2 'vec))) + (and + (equal? n1 n2) + (let lp ((i 0)) + (if (< i n1) + (and (equal? (vector-ref vec1 i) (vector-ref vec2 i)) + (lp (+ i 1))) + #t))))) + +(define-method (equal? (o1 <py-list>) o2) #f) +(define-method (equal? o1 (o2 <py-list>)) #f) + + +(define-class <py-list-iter> (<py-list>) i) +(define-class <string-iter> () str i) + +;;WRAP-IN +(define-method (wrap-in (s <string>)) + (let ((out (make <string-iter>))) + (slot-set! out 'str s) + (slot-set! out 'i 0) + out)) + +(define-method (wrap-in (o <py-list>)) + (let ((out (make <py-list-iter>))) + (slot-set! out 'n (slot-ref o 'n )) + (slot-set! out 'vec (slot-ref o 'vec)) + (slot-set! out 'i 0) + out)) + +(define-method (wrap-in (o <py-list-iter>)) o) +(define-method (wrap-in (o <string-iter> )) o) +;;NEXT +(define-method (next (o <string-iter>)) + (let ((i (slot-ref o 'i )) + (str (slot-ref o 'str))) + (if (< i (string-length str)) + (let ((ret (string-ref str i))) + (slot-set! o 'i (+ i 1)) + (list->string (list ret))) + (throw StopIteration)))) + +(define-method (next (o <py-list-iter>)) + (let ((i (slot-ref o 'i )) + (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)))) + +;;INSERT +(define-method (pylist-insert! (o <py-list>) i val) + (let* ((vec (slot-ref o 'vec)) + (n (slot-ref o 'n)) + (i (if (< i 0) (+ n i) i))) + (if (and (>= i 0) (<= i n)) + (let lp ((v val) (i i)) + (if (< i n) + (let ((swap (vector-ref vec i))) + (vector-set! vec i v) + (lp swap (+ i 1))) + (pylist-append! o v))) + (raise IndexError "Wrong index in insert")))) + +(define-method (pylist-insert! (o <p>) . l) (apply (ref o 'insert) l)) + + +;;REMOVE +(define-method (pylist-remove! (o <py-list>) val) + (let ((n (slot-ref o 'n )) + (vec (slot-ref o 'vec))) + (let lp ((i 0)) + (if (< i n) + (let ((r (vector-ref vec i))) + (if (equal? r val) + (pylist-subset! o i (+ i 1) 1 '()) + (lp (+ i 1)))) + (raise ValueError "list removal has no element to remove"))))) + +(define-method (pylist-remove! (o <p>) . l) (apply (ref o 'remove) l)) + +;; SORT! +(define-method (pylist-sort! (o <py-list>) ) + (let lp ((l (sort (to-list o) <)) (i 0)) + (if (pair? l) + (begin + (pylist-set! o i (car l)) + (lp (cdr l) (+ i 1)))))) + +(define-method (pylist-sort! (o <py-list>) . l) (apply (ref o 'sort) l)) + +;; INDEX +(define-method (pylist-index (o <py-list>) val . l) + (let* ((n (slot-ref o 'n )) + (vec (slot-ref o 'vec)) + (f (lambda (m) (if (< m 0) (+ m n) m)))) + (call-with-values + (lambda () + (match l + (() + (values 0 n)) + ((x) + (values (f x) n)) + ((x y) + (values (f x) (f y))))) + (lambda (n1 n2) + (if (and (>= n1 0) (>= n2 0) (< n1 n) (<= n2 n)) + (let lp ((i n1)) + (if (< i n2) + (let ((r (vector-ref vec i))) + (if (equal? r val) + i + (lp (+ i 1)))) + (raise ValueError "could not find value in index fkn"))) + (raise IndexError "index out of scop in index fkn")))))) + +(define-method (pylist-index (o <py>) . l) (apply (ref o 'index) l)) + + |