1 (define-module (language python module python
)
2 #:use-module
(oop goops
)
3 #:use-module
(ice-9 match
)
4 #:use-module
(ice-9 readline
)
5 #:use-module
((oop pf-objects
) #:select
6 (<p
> <property
> class-method static-method ref
7 py-super-mac type object pylist-ref define-python-class
9 #:use-module
(language python exceptions
)
10 #:use-module
((language python module string
) #:select
())
11 #:use-module
((language python module io
) #:select
(open))
12 #:use-module
(language python def
)
13 #:use-module
(language python for
)
14 #:use-module
(language python try
)
15 #:use-module
(language python yield
)
16 #:use-module
(language python list
)
17 #:use-module
(language python dict
)
18 #:use-module
(language python set
)
19 #:use-module
(language python compile
)
20 #:use-module
(language python string
)
21 #:use-module
(language python bytes
)
22 #:use-module
(language python set
)
23 #:use-module
(language python number
)
24 #:use-module
(language python dir
)
25 #:use-module
(language python hash
)
26 #:use-module
(language python property
)
27 #:use-module
(language python range
)
28 #:use-module
(language python tuple
)
29 #:use-module
(language python eval
)
30 #:use-module
(language python bool
)
32 #:replace
(list abs min max hash round format
)
34 #:re-export
(StopIteration GeneratorExit RuntimeError
35 Exception ValueError TypeError
36 IndexError KeyError AttributeError
37 send sendException next
38 GeneratorExit sendClose RuntimeError
40 len dir next dict None property range
41 tuple bytes bytearray eval locals globals
42 compile exec type object open
45 #:export
(print repr complex float int str
46 set all any bin callable reversed
47 chr classmethod staticmethod objectmethod
48 divmod enumerate filter
49 getattr hasattr setattr hex isinstance issubclass
50 iter map sum id input oct ord pow super
52 ClassMethod StaticMethod Funcobj
))
54 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
58 (() ((@ (guile) format
) #t
"~%"))
59 ((x) ((@ (guile) format
) #t
"~s~%" x
))
60 (l ((@ (guile) format
) #t
"~s~%" l
))))
62 (define (repr x
) ((@ (guile) format
) #f
"~a" x
))
65 (define complex py-complex
)
66 (define float py-float
)
68 (define round py-round
)
73 (define divmod py-divmod
)
74 (define format py-format
)
78 (define-method (callable x
) #f
)
79 (define-method (callable (x <procedure
> )) #t
)
80 (define-method (callable (x <procedure-class
> )) #t
)
81 (define-method (callable (x <applicable
> )) #t
)
82 (define-method (callable (x <primitive-generic
>)) #t
)
83 (define-method (callable (x <p
>))
86 (define chr integer-
>char
)
88 (define objectmethod object-method
)
89 (define classmethod class-method
)
90 (define staticmethod static-method
)
95 (for ((x : l
)) ((i 0))
106 (define miss
((@ (guile) list
) 'miss
))
108 (define* (getattr a b
#:optional
(k miss
))
109 (let ((r (ref a
(if (string? b
) (string->symbol b
) b
) k
)))
111 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
114 (define (setattr a k v
)
115 (set a
(if (string? k
) (string->symbol k
) k
) v
))
117 (define (hasattr a b
)
118 (let ((r (ref a
(symbol->string b
) miss
)))
121 (define-method (issubclass x y
) #f
)
122 (define-method (issubclass (sub <p
>) (cls <p
>))
123 (aif it
(ref cls
'__subclasscheck__
)
127 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
)))))
129 (define-method (isinstance x y
)
133 (or (isinstance x
(car y
))
134 (isinstance x
(cdr y
)))
137 (define-method (isinstance (i <integer
>) y
)
138 (if (issubclass y int
)
141 (or (isinstance i
(car y
))
142 (isinstance i
(cdr y
)))
145 (define-method (isinstance (i <real
>) y
)
146 (if (issubclass y float
)
149 (or (isinstance i
(car y
))
150 (isinstance i
(cdr y
)))
153 (define-method (isinstance (i <pair
>) y
)
154 (if (issubclass y tuple
)
157 (or (isinstance i
(car y
))
158 (isinstance i
(cdr y
)))
161 (define-method (isinstance (i <string
>) y
)
162 (if (issubclass y str
)
165 (or (isinstance i
(car y
))
166 (isinstance i
(cdr y
)))
169 (define-method (isinstance (i <bytevector
>) y
)
170 (if (issubclass y bytes
)
173 (or (isinstance i
(car y
))
174 (isinstance i
(cdr y
)))
178 (define-method (isinstance o
(cl <p
>))
184 (define-method (isinstance (o <p
>) (cl <p
>))
186 ((ref cl
'__instancecheck__
) =>
191 (isinstance o
(car cl
))
192 (isinstance o
(cdr cl
))))
194 (is-a? o
(ref cl
'__goops__
)))))
200 ((o) (aif it
(wrap-in o
)
202 (aif get
(ref o
'__getitem__
)
208 (raise TypeError
"not iterable" o
))))
224 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
225 #'(for ((x : a
) ...
) ((l '()))
227 #:final
(py-list (reverse l
))))))))
229 (define* (sum i
#:optional
(start 0))
230 (for ((x : i
)) ((s start
))
236 (define (id x
) (object-address x
))
239 ((@ (guile) format
) #t str
)
244 (def (py-min (* l
) (= key idx
) (= default miss
))
248 (for ((x : it
)) ((s miss
) (b miss
))
257 (if (eq? default miss
)
259 "min does not work for zero length list")
262 (_ (lp ((@ (guile) list
) l
))))))
264 (def (py-max (* l
) (= key idx
) (= default miss
))
268 (for ((x : it
)) ((s miss
) (b miss
))
277 (if (eq? default miss
)
279 "min does not work for zero length list")
282 (_ (lp ((@ (guile) list
) l
))))))
284 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
285 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
292 (py-mod (expt x y
) z
))))
294 (define-syntax-rule (super . l
) (py-super-mac . l
))
299 (define reversed py-reversed
)
300 (define (key-id x
) x
)
301 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
302 (define l
(to-pylist '()))
303 (for ((x : it
)) () (pylist-append! l x
))
304 (pylist-sort! l
#:key key
#:reverse reverse
)
308 (let ((l ((@ (guile) map
) wrap-in l
)))
312 (let lp2
((l l
) (r '()))
314 (call-with-values (lambda () (next (car l
)))
316 (lp2 (cdr l
) (append (reverse z
) r
))))
321 (define-python-class ClassMethod
())
322 (define-python-class StaticMethod
())
323 (define-python-class Funcobj
())