eval
[software/python-on-guile.git] / modules / language / python / module / python.scm
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 refq
7 py-super-mac))
8 #:use-module (language python exceptions )
9 #:use-module (language python def )
10 #:use-module (language python for )
11 #:use-module (language python try )
12 #:use-module (language python yield )
13 #:use-module (language python list )
14 #:use-module (language python dict )
15 #:use-module (language python set )
16 #:use-module (language python compile )
17 #:use-module (language python string )
18 #:use-module (language python bytes )
19 #:use-module (language python set )
20 #:use-module (language python number )
21 #:use-module (language python dir )
22 #:use-module (language python hash )
23 #:use-module (language python property )
24 #:use-module (language python range )
25 #:use-module (language python tuple )
26 #:use-module (language python eval )
27
28 #:replace (list abs min max hash round)
29
30 #:re-export (StopIteration GeneratorExit RuntimeError
31 Exception ValueError TypeError
32 IndexError KeyError AttributeError
33 send sendException next
34 GeneratorExit sendClose RuntimeError
35 SyntaxError
36 len dir next dict None property range
37 tuple bytes bytearray eval locals globals
38 compile exec
39 )
40
41 #:export (print repr complex float int
42 set all any bin callable reversed
43 chr classmethod staticmethod
44 divmod enumerate filter format
45 getattr hasattr hex isinstance
46 iter map sum id input oct ord pow super
47 sorted zip))
48
49 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
50
51 (define print
52 (case-lambda
53 (() (format #t "~%"))
54 ((x) (format #t "~s~%" x))
55 (l (format #t "~s~%" l))))
56
57 (define (repr x) (format #f "~a" x))
58 (define abs py-abs)
59 (define string pystring)
60 (define complex py-complex)
61 (define float py-float)
62 (define int py-int)
63 (define round py-round)
64 (define set py-set)
65 (define all py-all)
66 (define any py-any)
67 (define bin py-bin)
68 (define divmod py-divmod)
69 (define format py-format)
70 (define hash py-hash)
71 (define hex py-hex)
72
73 (define-method (callable x ) #f)
74 (define-method (callable (x <procedure> )) #t)
75 (define-method (callable (x <procedure-class> )) #t)
76 (define-method (callable (x <applicable> )) #t)
77 (define-method (callable (x <primitive-generic>)) #t)
78 (define-method (callable (x <p>))
79 (refq x '__call__))
80
81 (define chr integer->char)
82
83 (define classmethod class-method)
84 (define staticmethod static-method)
85
86 (define (enumerate l)
87 (make-generator enumerate
88 (lambda (yield)
89 (for ((x : l)) ((i 0))
90 (yield i x)
91 (+ i 1)))))
92
93 (define (filter f l)
94 (make-generator enumerate
95 (lambda (yield)
96 (for ((x : l)) ()
97 (if (f x)
98 (yield x))))))
99
100 (define miss ((@ (guile) list) 'miss))
101
102 (define* (getattr a b #:optional (k miss))
103 (let ((r (refq a (symbol->string b) k)))
104 (if (eq? r miss)
105 (raise AttributeError "object/class ~a is missing attribute ~a" a b)
106 r)))
107
108 (define (hasattr a b)
109 (let ((r (refq a (symbol->string b) miss)))
110 (not (eq? r miss))))
111
112 (define (isinstance o cl)
113 (if (pair? cl)
114 (or
115 (isinstance o (car cl))
116 (isinstance o (cdr cl)))
117 (is-a? o cl)))
118
119 (define iter
120 (case-lambda
121 ((o) (aif it (wrap-in o)
122 it
123 (aif get (refq o '__getitem__)
124 (make-generator iter
125 (lambda (yield)
126 (for () ((i 0))
127 (yield (get i))
128 (+ i 1))))
129 (raise TypeError "not iterable" o))))
130 ((f sent)
131 (make-generator iter
132 (lambda (yield)
133 (for () ()
134 (let ((r (f)))
135 (if (equal? r sent)
136 (break)
137 (yield r)))))))))
138
139
140
141 (define-syntax map
142 (lambda (x)
143 (syntax-case x ()
144 ((map f a ...)
145 (with-syntax (((x ...) (generate-temporaries #'(a ...))))
146 #'(make-generator map
147 (lambda (yield)
148 (for ((x : a) ...) () (yield (f x ...))))))))))
149
150 (define* (sum i #:optional (start 0))
151 (for ((x : i)) ((s start))
152 (+ s x)
153 #:final
154 s))
155
156
157 (define (id x) (object-address x))
158
159 (define (input str)
160 (format #t str)
161 (readline))
162
163 (define (idx x) x)
164
165 (def (py-min (* l) (= key idx) (= default miss))
166 (let lp ((l l))
167 (match l
168 ((it)
169 (for ((x : it)) ((s default) (b default))
170 (if (eq? s miss)
171 (values (key x) x)
172 (let ((k (key x)))
173 (if (< k s)
174 (values k x)
175 (values s b))))
176 #:final
177 (if (eq? b miss)
178 (raise ValueError "min does not work for zero length list")
179 b)))
180 (_ (lp ((@ (guile) list) l))))))
181
182 (def (py-max (* l) (= key idx) (= default miss))
183 (let lp ((l l))
184 (match l
185 ((it)
186 (for ((x : it)) ((s default) (b default))
187 (if (eq? default miss)
188 (values (key x) x)
189 (let ((k (key x)))
190 (if (> k s)
191 (values k x)
192 (values s b))))
193 #:final
194 (if (eq? b miss)
195 (raise ValueError "min does not work for zero length list")
196 b)))
197 (_ (lp ((@ (guile) list) l))))))
198
199 (define (oct x) (+ "0o" (number->string (py-index x) 8)))
200 (define (ord x) (char->integer (string-ref (pylist-ref x 0) 0)))
201
202 (define pow
203 (case-lambda
204 ((x y)
205 (expt x y))
206 ((x y z)
207 (py-mod (expt x y) z))))
208
209 (define-syntax-rule (super . l) (py-super-mac . l))
210
211 (define min py-min)
212 (define max py-max)
213 (define list pylist)
214 (define reversed py-reversed)
215 (define (key-id x) x)
216 (define* (sorted it #:key (key key-id) (reverse #f))
217 (define l (to-pylist '()))
218 (for ((x : it)) () (pylist-append! l x))
219 (pylist-sort! l #:key key #:reverse reverse)
220 l)
221
222 (define (zip . l)
223 (let ((l ((@ (guile) map) wrap-in l)))
224 ((make-generator ()
225 (lambda (yield)
226 (let lp ()
227 (let lp2 ((l l) (r '()))
228 (if (pair? l)
229 (call-with-values (lambda () (next (car l)))
230 (lambda z
231 (lp2 (cdr l) (append (reverse z) r))))
232 (begin
233 (yield (reverse r))
234 (lp))))))))))
235
236
237