summaryrefslogtreecommitdiff
path: root/modules/language
diff options
context:
space:
mode:
Diffstat (limited to 'modules/language')
-rw-r--r--modules/language/python/compile.scm54
-rw-r--r--modules/language/python/dir.scm91
-rw-r--r--modules/language/python/list.scm103
-rw-r--r--modules/language/python/string.scm7
4 files changed, 239 insertions, 16 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm
index fdc6309..766c718 100644
--- a/modules/language/python/compile.scm
+++ b/modules/language/python/compile.scm
@@ -17,6 +17,7 @@
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define-inlinable (C x) `(@@ (language python compile) ,x))
+(define-inlinable (N x) `(@@ (language python numeric) ,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))
@@ -216,17 +217,48 @@
(define fasthash
(mkfast
+ ;; General
+ ((__init__) (O 'init))
+ ((__getattr__) (O 'getattr))
+ ((__setattr__) (O 'setattr))
+ ((__delattr__) (O 'delattr))
+ ((__ne__) (O 'ne))
+ ((__eq__) (O 'equal?))
+ ((__repr__) (O 'repr))
+
+ ;;iterators
+ ((__iter__) (F 'wrap-in))
+ ((__next__) (F 'next))
+ ((__send__) (Y 'send))
+ ((__exception__) (Y 'sendException))
+ ((__close__) (Y 'sendClose))
+
+ ;; Numerics
+ ((__add__ ) (N '+))
+ ((__radd__) (N 'r+))
+ ((__mul__ ) (N '+))
+ ((__rmul__) (N 'r*))
+ ((__le__ ) (N '<))
+ ((__lt__ ) (N '<=))
+ ((__ge__ ) (N '>))
+ ((__gt__ ) (N '>=))
+
;; Lists
- ((append) (L 'pylist-append!))
- ((count) (L 'pylist-count))
- ((extend) (L 'pylist-extend!))
- ((index) (L 'pylist-index))
- ((pop) (L 'pylist-pop!))
- ((insert) (L 'pylist-insert!))
- ((remove) (L 'pylist-remove!))
- ((reverse) (L 'pylist-reverse!))
- ((sort) (L 'pylist-sort!))
-
+ ((append) (L 'pylist-append!))
+ ((count) (L 'pylist-count))
+ ((extend) (L 'pylist-extend!))
+ ((index) (L 'pylist-index))
+ ((pop) (L 'pylist-pop!))
+ ((insert) (L 'pylist-insert!))
+ ((remove) (L 'pylist-remove!))
+ ((reverse) (L 'pylist-reverse!))
+ ((sort) (L 'pylist-sort!))
+ ((__len__) (L 'len))
+ ((__contains__) (L 'in))
+ ((__delitem__) (L 'pylist-delete!))
+ ((__delslice__) (L 'pylist-delslice))
+ ((__setitem__) (L 'pylist-set!))
+
;; String
((format) (S 'py-format ))
((capitalize) (S 'py-capitalize))
@@ -280,7 +312,7 @@
((clear) (Di 'py-clear))))
(define (fastfkn x) (hash-ref fasthash x))
-
+
(define (get-kwarg vs arg)
(let lp ((arg arg) (l '()) (kw '()))
(match arg
diff --git a/modules/language/python/dir.scm b/modules/language/python/dir.scm
new file mode 100644
index 0000000..ecd85cf
--- /dev/null
+++ b/modules/language/python/dir.scm
@@ -0,0 +1,91 @@
+(define-module (language python dir)
+ #:use-module (language python list)
+ #:use-module (language python for)
+ #:use-module (language python dict)
+ #:use-module (language python string)
+ #:use-module (oop goops)
+ #:use-module (ice-9 vlist)
+ #:use-module (oop pf-objects)
+ #:export (dir))
+
+(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
+
+(define-method (dir) (pylist))
+
+(define (get-from-class c f)
+ (let lp ((c c))
+ (hash-for-each f c)
+ (let lpp ((pl (ref c '__parents__)))
+ (if (pair? pl)
+ (begin
+ (lp (car pl))
+ (lpp (cdr pl)))))))
+
+(define (get-from-class-f c f)
+ (let lp ((c c))
+ (vhash-fold f 0 c)
+ (let lpp ((pl (ref c '__parents__)))
+ (if (pair? pl)
+ (begin
+ (lp (car pl))
+ (lpp (cdr pl)))))))
+
+(define-method (dir (o <p>))
+ (if (pyclass? o)
+ (aif it (ref o '__dir__)
+ (it)
+ (aif it (ref o '__dict__)
+ (let ((l (pylist)))
+ (for ((k v : it)) ()
+ (pylist-append! l k))
+ l)
+ (let* ((h (make-hash-table))
+ (c (ref o '__class__))
+ (l (pylist))
+ (f (lambda (k v) (pylist-append! h k #t))))
+ (hash-for-each f o)
+ (get-from-class c f)
+ (hash-for-each (lambda (k v) (pylist-append! l k)) h)
+ (pylist-sort! l)
+ l)))
+ (let* ((h (make-hash-table))
+ (c o)
+ (l '())
+ (f (lambda (k v) (pylist-append! h k #t))))
+ (get-from-class c f)
+ (hash-for-each (lambda (k v) (set! l (cons k l))) h)
+ (to-pylist (map symbol->string (sort l <))))))
+
+(define-method (dir (o <pf>))
+ (if (pyclass? o)
+ (aif it (ref o '__dir__)
+ (it)
+ (aif it (ref o '__dict__)
+ (let ((l (pylist)))
+ (for ((k v : it)) ()
+ (pylist-append! l k))
+ l)
+ (let* ((h (make-hash-table))
+ (c (ref o '__class__))
+ (l (pylist))
+ (f (lambda (k v s) (pylist-append! h k #t))))
+ (vhash-fold f 0 o)
+ (get-from-class-f c f)
+ (hash-for-each (lambda (k v) (pylist-append! l k)) h)
+ (pylist-sort! l)
+ l)))
+ (let* ((h (make-hash-table))
+ (c o)
+ (l '())
+ (f (lambda (k v s) (pylist-append! h k #t))))
+ (get-from-class-f c f)
+ (hash-for-each (lambda (k v) (set! l (cons k l))) h)
+ (to-pylist (map symbol->string (sort l <))))))
+
+(define-method (dir (o <py-list> )) (pylist-listing))
+(define-method (dir (o <hashtable> )) pyhash-listing)
+(define-method (dir (o <py-hashtable>)) pyhash-listing)
+(define-method (dir (o <string> )) string-listing)
+
+
+
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm
index f74e2b1..532868b 100644
--- a/modules/language/python/list.scm
+++ b/modules/language/python/list.scm
@@ -13,7 +13,10 @@
pylist-slice pylist-subset! pylist-reverse!
pylist-pop! pylist-count pylist-extend! len in
pylist-insert! pylist-remove! pylist-sort!
- pylist-index pylist-null pylist-delete!))
+ pylist-index pylist-null pylist-delete!
+ pylist pylist-listing))
+
+(define scm-list list)
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
@@ -162,8 +165,9 @@
(define-method (pylist-slice (o <string>) n1 n2 n3)
(list->string
- (to-list
- (pylist-slice (to-pylist o) n1 n2 n3))))
+ (map (lambda (x) (string-ref x 0))
+ (to-list
+ (pylist-slice (to-pylist o) n1 n2 n3)))))
(define-method (pylist-slice (o <pair>) n1 n2 n3)
(to-list
@@ -462,7 +466,7 @@
(if (< i (string-length str))
(let ((ret (string-ref str i)))
(slot-set! o 'i (+ i 1))
- (list->string (list ret)))
+ (list->string (scm-list ret)))
(throw StopIteration))))
(define-method (next (o <py-list-iter>))
@@ -633,3 +637,94 @@
(define-method (in x (o <p>))
((ref o '__contains__) x))
+
+(define-syntax-rule (defgen (op o1 o2) code ...)
+ (begin
+ (define-method (op (o1 <py-list>) (o2 <py-list>)) code ...)
+ (define-method (op (o1 <pair>) (o2 <pair> )) code ...)
+ (define-method (op (o1 <vector>) (o2 <vector>)) code ...)))
+
+(defgen (< o1 o2)
+ (let ((n1 (len o1))
+ (n2 (len o2)))
+ (for ((x1 : o1) (x2 : o2)) ()
+ (if (< x1 x2)
+ (break #t))
+ #:final
+ (< n1 n2))))
+
+(defgen (<= o1 o2)
+ (let ((n1 (len o1))
+ (n2 (len o2)))
+ (for ((x1 : o1) (x2 : o2)) ()
+ (if (< x1 x2)
+ (break #t))
+ #:final
+ (<= n1 n2))))
+
+(defgen (> o1 o2)
+ (let ((n1 (len o1))
+ (n2 (len o2)))
+ (for ((x1 : o1) (x2 : o2)) ()
+ (if (> x1 x2)
+ (break #t))
+ #:final
+ (> n1 n2))))
+
+(defgen (>= o1 o2)
+ (let ((n1 (len o1))
+ (n2 (len o2)))
+ (for ((x1 : o1) (x2 : o2)) ()
+ (if (> x1 x2)
+ (break #t))
+ #:final
+ (>= n1 n2))))
+
+(define-python-class list (<py-list>)
+ (define __init__
+ (lambda (self . x)
+ (slot-set! self 'vec (make-vector 30))
+ (slot-set! self 'n 0)
+ (for-each (lambda (x) (pylist-append! self x)) x))))
+
+(define pylist list)
+
+
+(define (pylist-listing)
+ (let ((l
+ (to-pylist
+ (map symbol->string
+ '(append
+ count
+ extend
+ index
+ pop
+ insert
+ remove
+ reverse
+ sort
+ __init__
+ __le__
+ __lt__
+ __gt__
+ __ge__
+ __ne__
+ __eq__
+ __len__
+ __init__
+ __add__
+ __mul__
+ __rmul__
+ __radd__
+ __repr__
+ __containes__
+ __getattr__
+ __setattr__
+ __delattr__
+ __delitem__
+ __setitem__
+ __iter__
+ )))))
+
+ (pylist-sort! l)
+ l))
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm
index 22c8b88..8eb84aa 100644
--- a/modules/language/python/string.scm
+++ b/modules/language/python/string.scm
@@ -441,7 +441,12 @@
(if (= k n)
w
(pylist-slice w 0 k 1))))))
-
+
+(define-method (< (s1 <string>) (s2 <string>)) (string-ci< s1 s2))
+(define-method (<= (s1 <string>) (s2 <string>)) (string-ci<= s1 s2))
+(define-method (> (s1 <string>) (s2 <string>)) (string-ci> s1 s2))
+(define-method (>= (s1 <string>) (s2 <string>)) (string-ci>= s1 s2))
+
(define-py (py-zfill zfill s width)
(let* ((n (len s))
(w (pk (pylist-slice s 0 n 1))))