blob: e9504962833ea21fcfd176eba6915521520ad042 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
(define-module (language python module _python)
#:use-module (oop goops)
#:use-module (ice-9 match)
#:use-module (ice-9 readline)
#:use-module ((oop pf-objects) #:select
(<p> <property> class-method static-method ref (set . pf-set)
py-super-mac type object pylist-ref define-python-class
object-method py-dict))
#:use-module (language python exceptions )
#:use-module ((language python module string ) #:select ())
#:use-module ((language python module io ) #:select (open))
#:use-module (language python def )
#:use-module (language python for )
#:use-module (language python try )
#:use-module (language python yield )
#:use-module (language python module )
#:use-module (language python list )
#:use-module (language python dict )
#:use-module (language python set )
#:use-module (language python string )
#:use-module (language python bytes )
#:use-module (language python set )
#:use-module (language python number )
#:use-module (language python dir )
#:use-module (language python hash )
#:use-module (language python property )
#:use-module (language python range )
#:use-module (language python tuple )
#:use-module (language python eval )
#:use-module (language python bool )
#:replace (list abs min max hash round format map filter)
#:re-export (StopIteration GeneratorExit RuntimeError
Exception ValueError TypeError
IndexError KeyError AttributeError
send sendException next
GeneratorExit sendClose RuntimeError
SyntaxError bool compile
len dir next dict None property range
tuple bytes bytearray eval locals globals
exec type object open __import__ frozenset
Warning BytesWarning DeprecationWarning
py-list
)
#:export (print repr complex float int str
set all any bin callable reversed
chr classmethod staticmethod objectmethod
divmod enumerate
getattr hasattr setattr hex isinstance issubclass
iter sum id input oct ord pow super
sorted zip vars slice))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define (vars x)
(for ((k v : x)) ((l '()))
(cons (cons k v) l)
#:final
(dict l)))
(define (repr x) ((@ (guile) format) #f "~a" x))
(define abs py-abs)
(define str pystring)
(define complex py-complex)
(define float py-float)
(define int py-int)
(define round py-round)
(define set py-set)
(define all py-all)
(define any py-any)
(define bin py-bin)
(define divmod py-divmod)
(define format py-format)
(define hash py-hash)
(define hex py-hex)
(define-method (callable x ) #f)
(define-method (callable (x <procedure> )) #t)
(define-method (callable (x <procedure-class> )) #t)
(define-method (callable (x <applicable> )) #t)
(define-method (callable (x <primitive-generic>)) #t)
(define-method (callable (x <p>))
(ref x '__call__))
(define chr integer->char)
(define objectmethod object-method)
(define classmethod class-method)
(define staticmethod static-method)
(def (enumerate l (= start 0))
((make-generator ()
(lambda (yield)
(for ((x : l)) ((i start))
(yield i x)
(+ i 1))))))
(define (filter f l)
((make-generator ()
(lambda (yield)
(for ((x : l)) ()
(if (f x)
(yield x)))))))
(define miss ((@ (guile) list) 'miss))
(define* (getattr a b #:optional (k miss))
(let ((r (ref a (if (string? b) (string->symbol b) b) k)))
(if (eq? r miss)
(raise AttributeError "object/class ~a is missing attribute ~a" a b)
r)))
(define (setattr a k v)
(pf-set a (if (string? k) (string->symbol k) k) v))
(define (hasattr a b)
(let ((r (ref a (if (string? b) (string->symbol b) b) miss)))
(not (eq? r miss))))
(define-method (issubclass x y) #f)
(define-method (issubclass (sub <p>) (cls <p>))
(aif it (ref cls '__subclasscheck__)
(it sub)
(if (eq? sub cls)
#t
(if (memq cls (ref sub '__mro__))
#t
#f))))
(define-method (isinstance x y)
(cond
((null? y)
#f)
((pair? y)
(or (isinstance x (car y))
(isinstance x (cdr y))))
(else
(catch #t
(lambda () (is-a? x y))
(lambda x #f)))))
(define-method (isinstance (i <integer>) (y <p>))
(cond
((ref y '__instancecheck__) =>
(lambda (it)
(it i)))
(else
(next-method))))
(define-method (isinstance (i <integer>) y)
(if (issubclass y int)
#t
(if (pair? y)
(or (isinstance i (car y))
(isinstance i (cdr y)))
(is-a? i y))))
(define-method (isinstance (i <real>) (y <p>))
(cond
((ref y '__instancecheck__) =>
(lambda (it)
(it i)))
(else
(next-method))))
(define-method (isinstance (i <real>) y)
(if (issubclass y float)
#t
(if (pair? y)
(or (isinstance i (car y))
(isinstance i (cdr y)))
(is-a? i y))))
(define-method (isinstance (i <pair>) (y <p>))
(cond
((ref y '__instancecheck__) =>
(lambda (it)
(it i)))
(else
(next-method))))
(define-method (isinstance (i <pair>) y)
(if (issubclass y tuple)
#t
(if (pair? y)
(or (isinstance i (car y))
(isinstance i (cdr y)))
(is-a? i y))))
(define-method (isinstance (i <string>) (y <p>))
(cond
((ref y '__instancecheck__) =>
(lambda (it)
(it i)))
(else
(next-method))))
(define-method (isinstance (i <string>) y)
(if (issubclass y str)
#t
(if (pair? y)
(or (isinstance i (car y))
(isinstance i (cdr y)))
(is-a? i y))))
(define-method (isinstance (i <bytevector>) (y <p>))
(cond
((ref y '__instancecheck__) =>
(lambda (it)
(it i)))
(else
(next-method))))
(define-method (isinstance (i <bytevector>) y)
(if (issubclass y bytes)
#t
(if (pair? y)
(or (isinstance i (car y))
(isinstance i (cdr y)))
(is-a? i y))))
(define-method (isinstance o (cl <p>))
(cond
((eq? cl py-list)
(is-a? o <py-list>))
(else #f)))
(define-method (isinstance (o <p>) (cl <p>))
(cond
((ref cl '__instancecheck__) =>
(lambda (it)
(it o)))
((pair? cl)
(or
(isinstance o (car cl))
(isinstance o (cdr cl))))
(else
(is-a? o (ref cl '__goops__)))))
(define iter
(case-lambda
((o) (aif it (wrap-in o)
it
(aif get (ref o '__getitem__)
(make-generator iter
(lambda (yield)
(for () ((i 0))
(yield (get i))
(+ i 1))))
(raise TypeError "not iterable" o))))
((f sent)
(make-generator iter
(lambda (yield)
(for () ()
(let ((r (f)))
(if (equal? r sent)
(break)
(yield r)))))))))
(define-syntax map
(lambda (x)
(syntax-case x ()
((map f a ...)
(with-syntax (((x ...) (generate-temporaries #'(a ...))))
#'(for ((x : a) ...) ((l '()))
(cons (f x ...) l)
#:final (py-list (reverse l))))))))
(define* (sum i #:optional (start 0))
(for ((x : i)) ((s start))
(+ s x)
#:final
s))
(define (id x) (object-address x))
(define (input str)
((@ (guile) format) #t str)
(readline))
(define (idx x) x)
(define-syntax min
(lambda (x)
(syntax-case x ()
((_ x y)
#'(if (and (number? x) (number? y))
(if (<= x y) x y)
(py-min- x y)))
((_ l ...)
#'(if (and (number? l) ...)
((@ (guile) min) l ...)
(py-min- l ...)))
(_ #'py-min-))))
(def (py-min- (* l) (= key idx) (= default miss))
(let lp ((l l))
(match l
((it)
(for ((x : it)) ((s miss) (b miss))
(if (eq? s miss)
(values (key x) x)
(let ((k (key x)))
(if (< k s)
(values k x)
(values s b))))
#:final
(if (eq? b miss)
(if (eq? default miss)
(raise ValueError
"min does not work for zero length list")
default)
b)))
(_ (lp ((@ (guile) list) l))))))
(define-syntax max
(lambda (x)
(syntax-case x ()
((_ x y)
#'(if (and (number? x) (number? y))
(if (>= x y) x y)
(py-max- x y)))
((_ l ...)
#'(if (and (number? l) ...)
((@ (guile) max) l ...)
(py-max- l ...)))
(_ #'py-max-))))
(def (py-max- (* l) (= key idx) (= default miss))
(let lp ((l l))
(match l
((it)
(for ((x : it)) ((s miss) (b miss))
(if (eq? s miss)
(values (key x) x)
(let ((k (key x)))
(if (> k s)
(values k x)
(values s b))))
#:final
(if (eq? b miss)
(if (eq? default miss)
(raise ValueError
"min does not work for zero length list")
default)
b)))
(_ (lp ((@ (guile) list) l))))))
(define (oct x) (+ "0o" (number->string (py-index x) 8)))
(define (ord x) (char->integer (string-ref (pylist-ref x 0) 0)))
(define pow
(case-lambda
((x y)
(expt x y))
((x y z)
(if (and (number? y) (integer? y) (number? x) (integer? x) (number? z) (integer? z))
(modulo-expt x y z)
(modulo (expt x y) z)))))
(define-syntax-rule (super . l) (py-super-mac . l))
(define list pylist)
(define reversed py-reversed)
(define (key-id x) x)
(define* (sorted it #:key (key key-id) (reverse #f))
(define l (to-pylist '()))
(for ((x : it)) () (pylist-append! l x))
(pylist-sort! l #:key key #:reverse reverse)
l)
(define (zip . l)
(let ((l ((@ (guile) map) wrap-in l)))
((make-generator ()
(lambda (yield)
(let lp ()
(let lp2 ((l l) (r '()))
(if (pair? l)
(call-with-values (lambda () (next (car l)))
(lambda z
(lp2 (cdr l) (append (reverse z) r))))
(begin
(yield (reverse r))
(lp))))))))))
(define print
(lam ((= file #f) (* l))
(if file
(if (port? file)
#t
(set! file (ref file '_port)))
(set! file (current-output-port)))
(with-output-to-port file
(lambda ()
(apply
(case-lambda
(() ((@ (guile) display) "\n"))
((x) ((@ (guile) display) x ) (print))
(l ((@ (guile) display) l ) (print)))
l)))))
(define-syntax-rule (mk cl cls ? tp)
(begin
(set! (@@ (oop pf-objects) cl) cls)
(set! (@@ (oop pf-objects) ? ) (lambda (x) (isinstance x tp)))))
(mk int-cls int int? int)
(mk tuple-cls tuple tuple? tuple)
(mk string-cls str str? str)
(mk bytes-cls bytes bytes? bytes)
(mk list-cls list list? list)
(mk float-cls float float? float)
(define-python-class slice ()
(define __init__
(lam (self x (= y None) (= z None))
(pf-set self 'x x)
(pf-set self 'y y)
(pf-set self 'z z))))
(set! (@@ (language python module) m?)
(lambda (x) (isinstance x (@@ (language python module) Module))))
|