170cb11ee82e562b68a93445ed60724e41408371
1 (define-module (language python compile
)
2 #:use-module
(ice-9 match
)
3 #:use-module
(ice-9 control
)
4 #:use-module
(oop pf-objects
)
5 #:use-module
(oop goops
)
6 #:use-module
(rnrs bytevectors
)
7 #:use-module
(language python dict
)
8 #:use-module
(language python exceptions
)
9 #:use-module
(language python yield
)
10 #:use-module
(language python for
)
11 #:use-module
(language python try
)
12 #:use-module
(language python list
)
13 #:use-module
(language python string
)
14 #:use-module
(language python bytes
)
15 #:use-module
(language python number
)
16 #:use-module
(language python def
)
17 #:use-module
(language python module
)
18 #:use-module
(language python dir
)
19 #:use-module
(language python procedure
)
20 #:use-module
(language python bool
)
21 #:use-module
((language python with
) #:select
())
22 #:use-module
(ice-9 pretty-print
)
25 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
27 (define-inlinable (C x
) `(@@ (language python compile
) ,x
))
28 (define-inlinable (N x
) `(@@ (language python number
) ,x
))
29 (define-inlinable (Y x
) `(@@ (language python yield
) ,x
))
30 (define-inlinable (T x
) `(@@ (language python try
) ,x
))
31 (define-inlinable (F x
) `(@@ (language python for
) ,x
))
32 (define-inlinable (E x
) `(@@ (language python exceptions
) ,x
))
33 (define-inlinable (L x
) `(@@ (language python list
) ,x
))
34 (define-inlinable (S x
) `(@@ (language python string
) ,x
))
35 (define-inlinable (B x
) `(@@ (language python bytes
) ,x
))
36 (define-inlinable (Se x
) `(@@ (language python set
) ,x
))
37 (define-inlinable (D x
) `(@@ (language python def
) ,x
))
38 (define-inlinable (Di x
) `(@@ (language python dict
) ,x
))
39 (define-inlinable (O x
) `(@@ (oop pf-objects
) ,x
))
40 (define-inlinable (G x
) `(@ (guile) ,x
))
41 (define-inlinable (H x
) `(@ (language python hash
) ,x
))
42 (define-inlinable (W x
) `(@ (language python with
) ,x
))
46 (define (pre) (warn "Patching guile will lead to way better experience use 'python.patch' on guile-2.2 e.g. (use-modules (language python guilemod))"))
48 (define-syntax clear-warning-data
52 (fluid-set! (@@ (system base message
) %dont-warn-list
) '()))
59 (fluid-set! (@@ (system base message
) %dont-warn-list
)
61 (fluid-ref (@@ (system base message
) %dont-warn-list
)))))
64 (define *prefixes
* (make-fluid '()))
65 (define (add-prefix id
)
68 (if (fluid-ref (@@ (system base compile
) %in-compile
))
69 (fluid-set! *prefixes
* (cons id
(fluid-ref *prefixes
*)))
71 (when (not (module-defined?
(current-module) '__prefixes__
))
72 (module-define! (current-module)
73 '__prefixes__
(make-fluid '())))
75 (let ((p (module-ref (current-module) '__prefixes__
)))
76 (fluid-set! p
(cons id
(fluid-ref p
)))))))
79 (define (is-prefix? id
)
82 (if (fluid-ref (@@ (system base compile
) %in-compile
))
83 (member id
(fluid-ref *prefixes
*))
84 (if (not (module-defined?
(current-module) '__prefixes__
))
86 (let ((p (module-ref (current-module) '__prefixes__
)))
87 (member id
(fluid-ref p
))))))
92 ((_ (f) . l
) (f . l
))))
94 (define (fold f init l
)
96 (fold f
(f (car l
) init
) (cdr l
))
100 (define port
(open-file "/home/stis/src/python-on-guile/log.txt" "a"))
101 (with-output-to-port port
103 (pretty-print (syntax->datum x
))))
108 (define port
(open-file "/home/stis/src/python-on-guile/compile.log" "a"))
109 (with-output-to-port port
110 (lambda () (pretty-print (syntax->datum x
)) x
))
115 (pretty-print (syntax->datum x
))
118 (define (gen-sel vs e item
)
121 ((#:cfor for-e in-e cont
)
122 `(,(F 'for
) ((,@(map (g vs exp
) for-e
) : ,(exp vs in-e
))) ()
123 ,(gen-sel vs cont item
)))
126 ,(gen-sel vs cont item
)))))
128 (define (union as vs
)
129 (let lp
((as as
) (vs vs
))
134 (lp as
(cons x vs
))))
139 (let lp
((as as
) (rs '()))
144 (lp as
(cons x rs
))))
148 (define (get-globals code
)
149 (let lp
((vs (glob code
'())) (rs (scope code
'())))
161 (let lp
((l l
) (vs vs
))
163 (((#:identifier v . _
) . l
)
164 (let ((s (string->symbol v
)))
167 (lp l
(cons s vs
)))))
171 (glob y
(glob x vs
)))
177 (union (list (exp '() f
)) vs
))
182 ((#:with
(l ...
) code
)
183 (scope code
(union vs
187 (cons (exp '() b
) (lp l
)))
192 (union (list (exp '() f
)) vs
))
197 ((#:import
(#:name
((ids ...
) . as
)) ...
)
198 (let lp
((ids ids
) (as as
) (vs vs
))
200 (lp (cdr ids
) (cdr as
)
203 (union vs
(list (exp '() (if as as
(car ids
)))))))
206 ((#:expr-stmt l
(#:assign u
))
207 (union (fold (lambda (x s
)
209 ((#:test
(#:power v2 v1
() . _
) . _
)
212 (union (list (exp '() v1
))
215 (union (list (exp '() v1
)) s
)))
222 (scope y
(scope x vs
)))
227 ((#:def
(#:identifier f
) . _
)
228 (union (list (string->symbol f
)) vs
))
235 ((#:import
(#:name
((ids ...
) . as
)) ...
)
236 (let lp
((ids ids
) (as as
) (vs vs
))
238 (lp (cdr ids
) (cdr as
)
241 (union vs
(list (exp '() (if as as
(car ids
)))))))
244 (defs y
(defs x vs
)))
247 (define (gen-yield f
)
254 (lambda (x) (e vs x
)))
256 (define return
(make-fluid 'error-return
))
258 (define-syntax-rule (<< x y
) (ash x y
))
259 (define-syntax-rule (>> x y
) (ash x
(- y
)))
261 (define-syntax-rule (mkfast ((a) v
) ...
)
262 (let ((h (make-hash-table)))
268 (aif it
(assoc x
`((__class__ .
,(O 'py-class
))))
275 ((__init__) (O 'py-init
))
276 ((__getattr__) (O 'getattr
))
277 ((__setattr__) (O 'setattr
))
278 ((__delattr__) (O 'delattr
))
280 ((__eq__) (O 'equal?
))
281 ((__repr__) (O 'repr
))
284 ((__iter__) (F 'wrap-in
))
285 ((__next__) (F 'next
))
286 ((__send__) (Y 'send
))
287 ((__exception__) (Y 'sendException
))
288 ((__close__) (Y 'sendClose
))
291 ((__index__) (N 'py-index
))
295 ((__radd__ ) (N 'r
+))
296 ((__rmul__ ) (N 'r
*))
297 ((__rsub__ ) (N 'r-
))
303 ((__abs__ ) (N 'py-abs
))
304 ((__pow__ ) (N 'expt
))
305 ((__rpow__ ) (N 'rexpt
))
306 ((__truediv__) (N 'py-
/))
307 ((__rtruediv__) (N 'py-r
/))
308 ((__and__) (N 'py-logand
))
309 ((__or__) (N 'py-logior
))
310 ((__xor__) (N 'py-logxor
))
311 ((__rand__) (N 'py-rlogand
))
312 ((__ror__) (N 'py-rlogior
))
313 ((__rxor__) (N 'py-rlogxor
))
314 ((__divmod__) (N 'py-divmod
))
315 ((__rdivmod__) (N 'py-rdivmod
))
316 ((__invert__) (N 'py-lognot
))
317 ((__int__) (N 'mk-int
))
318 ((__float__) (N 'mk-float
))
319 ((__lshift__) (N 'py-lshift
))
320 ((__rshift__) (N 'py-rshift
))
321 ((__rlshift__) (N 'py-rlshift
))
322 ((__rrshift__) (N 'py-rrshift
))
323 ((as_integer_ratio) (N 'py-as-integer-ratio
))
324 ((conjugate) (N 'py-conjugate
))
325 ((denominator) (N 'py-denominator
))
326 ((numerator) (N 'py-numerator
))
327 ((fromhex) (N 'py-fromhex
))
329 ((imag) (N 'py-imag
))
330 ((is_integer) (N 'py-is-integer
))
331 ((real) (N 'py-real
))
332 ((__mod__) (N 'py-mod
))
333 ((__rmod__) (N 'py-rmod
))
334 ((__floordiv__) (N 'py-floordiv
))
335 ((__rfloordiv__)(N 'py-rfloordiv
))
339 ((append) (L 'pylist-append
!))
340 ((count) (L 'pylist-count
))
341 ((extend) (L 'pylist-extend
!))
342 ((index) (L 'pylist-index
))
343 ((pop) (L 'pylist-pop
!))
344 ((insert) (L 'pylist-insert
!))
345 ((remove) (L 'pylist-remove
!))
346 ((reverse) (L 'pylist-reverse
!))
347 ((sort) (L 'pylist-sort
!))
349 ((__contains__) (L 'in
))
350 ((__delitem__) (L 'pylist-delete
!))
351 ((__delslice__) (L 'pylist-delslice
))
352 ((__setitem__) (L 'pylist-set
!))
355 ((format) (S 'py-strformat
))
356 ((format_map) (S 'py-format-map
))
357 ((capitalize) (S 'py-capitalize
))
358 ((center) (S 'py-center
))
359 ((endswith) (S 'py-endswith
))
360 ((expandtabs) (S 'py-expandtabs
))
361 ((find) (S 'py-find
))
362 ((rfind) (S 'py-rfind
))
363 ((isalnum) (S 'py-isalnum
))
364 ((isalpha) (S 'py-isalpha
))
365 ((isdigit) (S 'py-isdigit
))
366 ((islower) (S 'py-islower
))
367 ((isspace) (S 'py-isspace
))
368 ((isupper) (S 'py-isupper
))
369 ((istitle) (S 'py-istitle
))
370 ((isidentifier) (S 'py-identifier
))
371 ((join) (S 'py-join
))
372 ((ljust) (S 'py-join
))
373 ((rljust) (S 'py-rljust
))
374 ((lower) (S 'py-lower
))
375 ((upper) (S 'py-upper
))
376 ((lstrip) (S 'py-lstrip
))
377 ((rstrip) (S 'py-rstrip
))
378 ((partition) (S 'py-partition
))
379 ((replace) (S 'py-replace
))
380 ((strip) (S 'py-strip
))
381 ((title) (S 'py-title
))
382 ((rpartition) (S 'py-rpartition
))
383 ((rindex) (S 'py-rindex
))
384 ((split) (S 'py-split
))
385 ((rsplit) (S 'py-rsplit
))
386 ((splitlines) (S 'py-splitlines
))
387 ((startswith) (S 'py-startswith
))
388 ((swapcase) (S 'py-swapcase
))
389 ((translate) (S 'py-translate
))
390 ((zfill) (S 'py-zfill
))
393 ((copy) (Di 'py-copy
))
394 ((fromkeys) (Di 'py-fromkeys
))
396 ((has_key) (Di 'py-has_key
))
397 ((items) (Di 'py-items
))
398 ((iteritems) (Di 'py-iteritems
))
399 ((iterkeys) (Di 'py-iterkeys
))
400 ((itervalues) (Di 'py-itervalues
))
401 ((keys) (Di 'py-keys
))
402 ((values) (Di 'py-values
))
403 ((popitem) (Di 'py-popitem
))
404 ((setdefault) (Di 'py-setdefault
))
405 ((update) (Di 'py-update
))
406 ((clear) (Di 'py-clear
))
407 ((__hash__) (H 'py-hash
))))
410 (define (fastfkn x
) (hash-ref fasthash x
))
412 (define (get-kwarg vs arg
)
415 (((#:comp .
(and x
(_ (#:cfor . _
) . _
))) . arg2
)
416 (cons `(* ,(exp vs
`(#:tuple
,@x
))) (lp arg2
)))
418 (cons `(* ,(exp vs a
)) (lp arg
)))
420 (cons `(** ,(exp vs a
)) (lp arg
)))
422 (cons `(= ,(exp vs a
) ,(exp vs b
)) (lp arg
)))
424 (cons (exp vs x
) (lp arg
)))
428 (define (get-args_ vs arg
)
432 (cons (exp vs
(car x
))
440 (define (get-args= vs arg
)
444 (cons (list '= (exp vs
(car x
)) (exp vs v
))
453 (define (get-args* vs arg
)
457 (cons (list '* (exp vs
(car x
)))
466 (define (get-args** vs arg
)
470 (cons (list '** (exp vs
(car x
)))
479 (define (kw->li dict
)
480 (for ((k v
: dict
)) ((l '()))
481 (cons* v
(symbol->keyword
(string->symbol k
)) l
)
485 (define (arglist->pkw . l
)
486 (let lp
((l l
) (r '()))
491 (lp (cdr l
) (cons x r
))))
492 (cons (reverse l
) '()))))
494 (define (get-addings vs x
)
498 (let ((is-fkn?
(match l
500 (((#:arglist . _
) . _
)
508 (let* ((tag (exp vs x
))
511 (is-fkn?
(aif it
(and is-fkn? fast
)
512 `(#:call-obj
(lambda (e)
519 `(#:fastfkn-ref
,fast
',tag
)
520 (aif it
(fast-ref tag
)
521 `(#:fast-id
,it
',tag
)
522 `(#:identifier
',tag
))))))
525 `(#:apply
,@(get-kwarg vs args
)))
527 ((#:subscripts
(n #f
#f
))
528 `(#:vecref
,(exp vs n
)))
530 ((#:subscripts
(n1 n2 n3
))
531 (let ((w (lambda (x) (if (eq? x None
) (E 'None
) x
))))
533 ,(w (exp vs n1
)) ,(w (exp vs n2
)) ,(w (exp vs n3
)))))
535 ((#:subscripts
(n #f
#f
) ...
)
536 `(#:array-ref
,@ (map (lambda (n)
540 ((#:subscripts
(n1 n2 n3
) ...
)
541 (let ((w (lambda (x) (if (eq? x None
) (E 'None
) x
))))
543 ,@(map (lambda (x y z
)
544 `(,(exp vs x
) ,(exp vs y
) ,(exp vs z
)))
547 (_ (error "unhandled addings")))
548 (get-addings vs l
))))))
550 (define-syntax-rule (setwrap u
)
551 (call-with-values (lambda () u
)
556 (define (make-set vs op x u
)
570 ("//=" 'floor-quotient
)))
573 ((#:test
(#:power kind v addings . _
) . _
)
574 (let* ((v (exp vs v
))
575 (v.add
(if (is-prefix? v
)
576 (let ((w (symbol->string
(exp vs
(car addings
)))))
577 (cons (string-append (symbol->string v
) "." w
)
581 (addings (cdr v.add
))
582 (addings (get-addings vs addings
))
587 (let ((w (symbol->string
(exp vs
(car add
)))))
588 (cons (string-append (symbol->string v
) "." w
)
590 (cons (exp vs v
) add
)))))
593 (pa (get-addings vs pa
)))
594 (define q
(lambda (x) `',x
))
599 `(,s
/d
,v
(,(C 'setwrap
) (,(tr-op op
) ,v
,u
)))
600 `(,s
/d
,v
(,(C 'setwrap
) ,u
)))
602 `(,s
/d
,(exp vs kind
)
603 (,(C 'fset-x
) ,v
,addings
605 (,(tr-op op
) (,(C 'ref-x
) ,v
,@addings
) ,u
))))
607 `(,s
/d
,(exp vs kind
)
608 (,(C 'fset-x
) ,v
,addings
609 (,(C 'setwrap
) ,u
)))))
611 (let ((pre (if (equal? p v
)
612 (let lp
((pa pa
) (ad addings
) (r '()))
613 (if (and (pair? pa
) (pair? ad
))
614 (let ((px (car pa
)) (ax (car ad
)))
616 (lp (cdr pa
) (cdr ad
) (cons px r
))
624 `(,s
/d
,v
(,(C 'setwrap
) (,(tr-op op
) ,v
,u
)))
625 `(,s
/d
,v
(,(C 'setwrap
) ,u
)))
627 `(,(C 'set-x
) ,v
,pre
,p
,pa
,addings
629 (,(tr-op op
) (,(C 'ref-x
) ,v
,@addings
) ,u
)))
631 `(,(C 'set-x
) ,v
,pre
,p
,pa
,addings
632 (,(C 'setwrap
) ,u
))))))
636 `(,s
/d
,v
(,(C 'setwrap
)
637 (,(tr-op op
) (,(C 'ref-x
) ,v
,@addings
) ,u
)))
638 `(,s
/d
,v
(,(C 'setwrap
)
645 `(,(tr-op op
) (,(C 'ref-x
) ,v
,@addings
) ,u
)
648 (define is-class?
(make-fluid #f
))
649 (define (gen-yargs vs x
)
650 (match (pr 'yarg x
) ((#:list args
)
651 (map (g vs exp
) args
))))
653 (define inhibit-finally
#f
)
654 (define decorations
(make-fluid '()))
655 (define tagis
(make-hash-table))
656 (define-syntax-rule (gen-table x vs
(tag code ...
) ...
)
672 ((_ #f vf trailer .
**)
673 (let* ((vf (exp vs vf
))
674 (vf.tr
(if (is-prefix? vf
)
680 (symbol->string
(exp vs
(car trailer
)))))
684 (trailer (cdr vf.tr
)))
687 `(expt ,x
,(exp vs
**))
690 (let ((trailer (get-addings vs trailer
)))
691 `(,(C 'ref-x
) ,vf
,@trailer
))))))
694 ((#:identifier x . _
)
699 (fluid-set! decorations
(map (g vs exp
) l
))
708 (let* ((b (make-bytevector (length l
))))
709 (let lp
((l l
) (i 0))
712 (bytevector-u8-set! b i
(car l
))
713 (lp (cdr l
) (+ i
1)))
714 `(,(B 'bytes
) ,b
))))))
719 (cons '+ (map (g vs exp
) l
))))
723 (cons '-
(map (g vs exp
) l
))))
727 (cons '* (map (g vs exp
) l
))))
731 (cons (N 'py-
/) (map (g vs exp
) l
))))
735 (cons (N 'py-mod
) (map (g vs exp
) l
))))
739 (cons (N 'py-floordiv
) (map (g vs exp
) l
))))
743 (cons (N 'py-lshift
) (map (g vs exp
) l
))))
747 (cons (N 'py-rshift
) (map (g vs exp
) l
))))
751 (list (N 'py-lognot
) (exp vs x
))))
755 (list '-
(exp vs x
))))
759 (list '+ (exp vs x
))))
763 (cons (N 'py-logand
) (map (g vs exp
) l
))))
767 (cons (N 'py-logxor
) (map (g vs exp
) l
))))
771 (cons (N 'py-logior
) (map (g vs exp
) l
))))
775 (list 'not
(list (C 'boolit
) (exp vs x
)))))
779 (cons 'or
(map (lambda (x) (list (C 'boolit
) (exp vs x
))) x
))))
783 (cons 'and
(map (lambda (x) (list (C 'boolit
) (exp vs x
))) x
))))
790 (list 'if
(list (C 'boolit
) (exp vs e2
)) (exp vs e1
) (C 'None
)))
793 (list 'if
(list (C 'boolit
) (exp vs e2
)) (exp vs e1
) (exp vs e3
))))
796 ;;We don't delete variables
797 ((_ (#:power
#f base
() .
#f
))
800 ((_ (#:power
#f base
(l ... fin
) .
#f
))
801 (let ((add (get-addings vs l
))
802 (fin (get-addings vs
(list fin
)))
804 `(,(C 'del-x
) (,(C 'ref-x
) ,f
,@add
) ,@fin
))))
808 (let* ((l (map (lambda (x)
810 ((a b
) (list (exp vs b
) (gensym "as") (exp vs a
)))
811 ((b) (list (exp vs b
)))))
813 (vs (union vs
(let lp
((l l
))
816 (((a b c
) . l
) (cons a
(lp l
)))
821 ((a b c
) (list 'set
! a b
))
822 ((a) (list (G 'values
)))))
829 `(,(W 'with
) ,(map g l
)
835 ((_ test a
((tests . as
) ...
) . else
)
837 (,(list (C 'boolit
) (exp vs test
)) ,(exp vs a
))
838 ,@(map (lambda (p a
) (list (list (C 'boolit
) (exp vs p
))
839 (exp vs a
))) tests as
)
840 ,@(if else
`((else ,(exp vs else
))) '()))))
843 ((_ . l
) (cons 'begin
(map (g vs exp
) l
))))
846 ((_ class parents code
)
847 (with-fluids ((is-class?
#t
))
849 (let* ((decor (let ((r (fluid-ref decorations
)))
850 (fluid-set! decorations
'())
852 (class (exp vs class
))
853 (vs (union (list class
) vs
))
857 (parents (match parents
861 (get-addings vs
(list parents
))))))
863 (,(C 'class-decor
) ,decor
864 (,(C 'with-class
) ,class
868 `(,(C 'ref-x
) ,(C 'arglist-
>pkw
) ,@parents
)
869 `(,(G 'cons
) '() '()))
870 ,(map (lambda (x) `(define ,x
#f
)) ls
)
871 ,(exp vs code
))))))))))
876 ((_ (#:string _ s
)) (with-input-from-string s read
)))
879 ((_ (#:from
(() . nm
) .
#f
))
880 `(use-modules (language python module
,@(map (lambda (nm) (exp vs nm
))
882 ((_ (#:from
(() . nm
) . l
))
883 `(use-modules ((language python module
,@(map (lambda (nm) (exp vs nm
))
885 #:select
,(map (lambda (x)
890 (cons (exp vs a
) (exp vs b
)))))
894 ((_ (#:name
((ids ...
) . as
)) ...
)
898 (let ((path (map (g vs exp
) ids
)))
903 ((#:test
(#:power
#f
,as
())))
906 ((@ (language python module
) import
)
907 ((@ (language python module
) Module
)
908 ',(reverse (append '(language python module
) path
))
914 ((#:test
(#:power
#f
,(car ids
) ())))
917 ((@ (language python module
) import
)
918 ((@ (language python module
) Module
)
919 ',(append '(language python module
) path
))
920 ,(exp vs
(car ids
)))))))))))
927 (((#:power
#f
(#:identifier x . _
) () .
#f
))
929 (((#:test power . _
))
932 (#:identifier
"range" . _
)
933 ((#:arglist arglist . _
))
937 (let ((v (gensym "v"))
938 (x (string->symbol x
))
940 `(let ((,v
,(exp vs arg
)))
947 (let ((v1 (gensym "va"))
950 `(let ((,v1
,(exp vs arg1
))
951 (,v2
,(exp vs arg2
)))
958 (let ((v1 (gensym "va"))
962 `(let ((,v1
,(exp vs arg1
))
964 (,v2
,(exp vs arg3
)))
977 (error "range with step 0 not allowed"))))))
983 ((_ es in code . else
)
984 (let* ((es2 (map (g vs exp
) es
))
986 (code2 (exp vs2 code
))
987 (p (is-ec #t code2
#t
(list (C 'break
) (C 'continue
))))
988 (else2 (if else
(exp vs2 else
) #f
))
989 (in2 (map (g vs exp
) in
)))
990 (list (C 'cfor
) es2 in2 code2 else2 p
))))
995 (let ((lp (gensym "lp")))
1003 (let ((lp (gensym "lp")))
1012 ((_ x
(or #f
()) #f . fin
)
1014 `(,(T 'try
) ,(exp vs x
) #:finally
(lambda () fin
))
1018 ((_ x exc else . fin
)
1019 `(,(T 'try
) ,(exp vs x
)
1020 ,@(let lp
((exc exc
) (r (if else
(exp vs else
) '())))
1022 ((((test .
#f
) code
) . exc
)
1023 (lp exc
(cons `(#:except
,(exp vs code
)) r
)))
1026 (lp exc
(cons `(#:except
,(exp vs code
)) r
)))
1028 ((((test . as
) code
) . exc
)
1029 (let ((l (gensym "l")))
1032 `(#:except
,(exp vs test
) => (lambda (,(exp vs as
) .
,l
)
1037 ,@(if fin
`(#:finally
(lambda () ,(exp vs fin
))) '()))))
1045 `(,(T 'raise
) (,(O 'Exception
))))
1048 `(,(T 'raise
) ,(exp vs code
)))
1051 (let ((o (gensym "o"))
1054 (let ((,c
,(exp vs code
)))
1055 (let ((,o
(if (,(O 'pyclass?
) ,c
)
1058 (,(O 'set
) ,o
'__cause__
,(exp vs from
))
1064 (let ((f (gensym "f")))
1066 (fluid-set! ,(Y 'in-yield
) #t
)
1067 (let ((,f
(scm.yield
,@(gen-yargs vs args
))))
1072 (let ((f (gen-yield (exp vs f
)))
1075 (set! ,(C 'inhibit-finally
) #t
)
1076 (let ((,g
(,f
,@(gen-yargs vs args
))))
1081 (#:types-args-list . args
)
1084 (let* ((decor (let ((r (fluid-ref decorations
)))
1085 (fluid-set! decorations
'())
1087 (arg_ (get-args_ vs args
))
1088 (arg= (get-args= vs args
))
1089 (dd= (map cadr arg
=))
1090 (c?
(fluid-ref is-class?
))
1092 (y?
(is-yield f
#f code
))
1093 (r (gensym "return"))
1094 (*f
(get-args* vs args
))
1096 (**f
(get-args** vs args
))
1097 (dd** (map cadr
**f
))
1099 (vs (union dd
** (union dd
* (union dd
= (union args vs
)))))
1100 (ns (scope code vs
))
1101 (df (defs code
'()))
1105 (ls (diff (diff ns vs
) df
)))
1108 `(let-syntax ((,y
(syntax-rules ()
1110 (abort-to-prompt ,ab . args
))))
1111 (,y.f
(syntax-rules ()
1113 (abort-to-prompt ,ab . args
)))))
1116 (with-fluids ((is-class?
#f
))
1120 (,(C 'def-decor
) ,decor
1121 (,(C 'def-wrap
) ,y?
,f
,ab
1122 (,(D 'lam
) (,@arg_
,@*f
,@arg
= ,@**f
)
1123 (,(C 'with-return
) ,r
1124 ,(mk `(let ,(map (lambda (x) (list x
#f
)) ls
)
1125 (,(C 'with-self
) ,c?
,args
1126 ,(with-fluids ((return r
))
1127 (exp ns code
))))))))))
1130 (,(C 'def-decor
) ,decor
1131 (,(D 'lam
) (,@arg_
,@*f
,@arg
= ,@**f
)
1132 (,(C 'with-return
) ,r
1133 ,(mk `(let ,(map (lambda (x) (list x
#f
)) ls
)
1134 (,(C 'with-self
) ,c?
,args
1135 ,(with-fluids ((return r
))
1136 (exp ns code
))))))))))
1140 (,(C 'def-decor
) ,decor
1141 (,(C 'def-wrap
) ,y?
,f
,ab
1142 (,(D 'lam
) (,@arg_
,@*f
,@arg
= ,@**f
)
1143 (,(C 'with-return
) ,r
1144 (let ,(map (lambda (x) (list x
#f
)) ls
)
1145 (,(C 'with-self
) ,c?
,args
1146 ,(with-fluids ((return r
))
1148 (exp ns code
))))))))))
1150 (,(C 'def-decor
) ,decor
1151 (,(D 'lam
) (,@arg_
,@*f
,@arg
= ,@**f
)
1152 (,(C 'with-return
) ,r
1153 (let ,(map (lambda (x) (list x
#f
)) ls
)
1154 (,(C 'with-self
) ,c?
,args
1155 ,(with-fluids ((return r
))
1156 (exp ns code
))))))))))))))
1163 ((_ x
(and e
(#:cfor . _
)))
1164 (let ((l (gensym "l")))
1165 `(let ((,l
(,(L 'to-pylist
) '())))
1166 ,(gen-sel vs e
`(,(L 'pylist-append
!) ,l
,(exp vs x
)))
1170 (list (L 'to-pylist
) (let lp
((l l
))
1173 (((#:starexpr
#:power
#f
(#:list . l
) . _
) . _
)
1175 (((#:starexpr
#:power
#f
(#:tuple . l
) . _
) . _
)
1177 (((#:starexpr . l
) . _
)
1178 `(,(L 'to-list
) ,(exp vs l
)))
1180 `(cons ,(exp vs x
) ,(lp l
))))))))
1182 ((_ x
(and e
(#:cfor . _
)))
1183 (let ((l (gensym "l")))
1185 ,(gen-sel vs e
`(set! ,l
(cons ,(exp vs x
) ,l
)))
1192 (((#:starexpr
#:power
#f
(#:list . l
) . _
) . _
)
1194 (((#:starexpr
#:power
#f
(#:tuple . l
) . _
) . _
)
1196 (((#:starexpr . l
) . _
)
1197 `(,(L 'to-list
) ,(exp vs l
)))
1199 `(cons ,(exp vs x
) ,(lp l
)))))))
1203 (list `lambda v
(exp vs e
))))
1207 (if (> (length l
) 1)
1208 (cons 'values
(map (g vs exp
) l
))
1212 ((_ (l ...
) (#:assign
))
1213 (let ((l (map (g vs exp
) l
)))
1214 (if (= (length l
) 1)
1216 `(,(G 'values
) ,@l
))))
1221 (lambda () (match type
1230 ((= (length l
) (length u
))
1231 (if (= (length l
) 1)
1233 ,(make-set vs op
(car l
) (exp vs
(car u
)))
1236 @,(map (lambda (l u
) (make-set vs op l u
))
1241 ((and (= (length u
) 1) (not op
))
1242 (let ((vars (map (lambda (x) (gensym "v")) l
))
1246 (call-with-values (lambda () ,(exp vs
(car u
)))
1252 (apply ,f
(,(L 'to-list
) ,q
))))
1254 ,@(map (lambda (l v
) (make-set vs op l v
))
1259 ((and (= (length l
) 1) (not op
))
1261 ,(make-set vs op
(car l
) `(,(G 'list
) ,@(map (g vs exp
) u
)))
1265 ((#:test
(#:power
#f
(#:identifier v . _
) () .
#f
) #f
))
1267 (let ((s (string->symbol v
)))
1268 `(,s
/d
,s
,(exp vs l
)))))
1272 `(if (,(G 'not
) (,(G 'and
) ,@(map (lambda (x) `(,(C 'boolit
) ,(exp vs x
)))
1274 (,(C 'raise
) ,(C 'AssertionError
) ',f
,n
,m
))))
1279 `(,(fluid-ref return
) ,@(map (g vs exp
) x
))
1280 `(,(fluid-ref return
)))))
1285 `(,(Di 'make-py-hashtable
)))
1287 ((_ (#:e k . v
) (and e
(#:cfor . _
)))
1288 (let ((dict (gensym "dict")))
1289 `(let ((,dict
(,(Di 'make-py-hashtable
))))
1290 ,(gen-sel vs e
`(,(L 'pylist-set
!) ,dict
,(exp vs k
) ,(exp vs v
)))
1293 ((_ (#:e k . v
) ...
)
1294 (let ((dict (gensym "dict")))
1295 `(let ((,dict
(,(Di 'make-py-hashtable
))))
1296 ,@(map (lambda (k v
)
1297 `(,(L 'pylist-set
!) ,dict
,(exp vs k
) ,(exp vs v
)))
1301 ((_ k
(and e
(#:cfor . _
)))
1302 (let ((dict (gensym "dict")))
1303 `(let ((,dict
(,(Se 'set
))))
1304 ,(gen-sel vs e
`((,(O 'ref
) ,dict
'add
) ,(exp vs k
)))
1308 (let ((set (gensym "dict")))
1309 `(let ((,set
(,(Se 'set
))))
1311 `((,(O 'ref
) ,set
'add
) ,(exp vs k
)))
1323 ((or "<" ">" "<=" ">=")
1324 (list (G (string->symbol op
)) x y
))
1325 ("!=" (list (G 'not
) (list (O 'equal?
) x y
)))
1326 ("==" (list (O 'equal?
) x y
))
1327 ("is" (list (G 'eq?
) x y
))
1328 ("isnot" (list (G 'not
) (list (G 'eq?
) x y
)))
1329 ("in" (list (L 'in
) x y
))
1330 ("notin" (list (G 'not
) (list (L 'in
) x y
)))
1331 ("<>" (list (G 'not
) (list (O 'equal?
) x y
)))))
1332 (tr op
(exp vs x
) (exp vs y
)))))
1339 ((hash-ref tagis tag
1340 (lambda y
(warn (format #f
"not tag in tagis ~a" tag
)) x
))
1354 (define-syntax-rule (define- n x
) (define! 'n x
))
1363 (#:identifier
"module" . _
)
1364 ((#:arglist arglist
))
1366 (#:assign
)))) . rest
)
1374 `((,(G 'define-module
) (language python module
,@args
)
1375 #:use-module
(language python module python
))
1377 (define __module__
'(language python module
,@args
)))))
1380 (if (fluid-ref (@@ (system base compile
) %in-compile
))
1381 (with-fluids ((*prefixes
* '()))
1382 (if (fluid-ref (@@ (system base compile
) %in-compile
))
1384 (set! s
/d
(C 'define-
)))
1389 (let ((globs (get-globals x
)))
1392 ,(C 'clear-warning-data
)
1393 (fluid-set! (@@ (system base message
) %dont-warn-list
) '())
1394 ,@(map (lambda (s) `(,(C 'var
) ,s
)) globs
)
1395 ,@(map (g globs exp
) x
)
1396 (,(C 'export-all
)))))
1398 (if (fluid-ref (@@ (system base compile
) %in-compile
))
1400 (set! s
/d
(C 'define-
)))
1405 (let ((globs (get-globals x
)))
1408 ,(C 'clear-warning-data
)
1409 (fluid-set! (@@ (system base message
) %dont-warn-list
) '())
1410 ,@(map (lambda (s) `(,(C 'var
) ,s
)) globs
)
1411 ,@(map (g globs exp
) x
))))))
1414 (define-syntax-parameter break
1415 (lambda (x) #'(values)))
1417 (define-syntax-parameter continue
1418 (lambda (x) (error "continue must be bound")))
1420 (define (is-yield f p x
)
1422 ((#:def nm args _ code
)
1423 (is-yield f
#t code
))
1425 (eq? f
(exp '() x
)))
1437 (define-syntax-rule (with-sp ((x v
) ...
) code ...
)
1438 (syntax-parameterize ((x (lambda (y) #'v
)) ...
) code ...
))
1440 (define (is-ec ret x tail tags
)
1441 (syntax-case (pr 'is-ec x
) (begin let if define
@@)
1445 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(a ...
))
1446 (is-ec ret
#'b tail tags
)))
1448 ((let lp
((y x
) ...
) a ... b
)
1449 (symbol?
(syntax->datum
#'lp
))
1451 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(x ...
))
1452 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(a ...
))
1453 (is-ec ret
#'b tail tags
)))
1455 ((let ((y x
) ...
) a ... b
)
1458 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(x ...
))
1459 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(a ...
))
1460 (is-ec ret
#'b tail tags
)))
1465 (is-ec ret
#'p
#f tags
)
1466 (is-ec ret
#'a tail tags
)
1467 (is-ec ret
#'b tail tags
)))
1476 (is-ec ret
#'p
#f tags
)
1477 (is-ec ret
#'a tail tags
)))
1481 (if (member (pr (syntax->datum x
)) tags
)
1487 (or-map (lambda (x) (is-ec ret x
#f tags
)) #'(a ...
)))
1493 (define-syntax with-return
1495 (define (analyze ret x
)
1496 (syntax-case x
(begin let if
)
1498 #`(begin a ...
#,(analyze ret
#'b
)))
1500 (symbol?
(syntax->datum
#'lp
))
1501 #`(let lp v a ...
#,(analyze ret
#'b
)))
1503 #`(let v a ...
#,(analyze ret
#'b
)))
1505 #`(if p
#,(analyze ret
#'a
) #,(analyze ret
#'b
)))
1507 #`(if p
#,(analyze ret
#'a
)))
1509 (equal?
(syntax->datum
#'return
) (syntax->datum ret
))
1510 (if (eq?
#'(b ...
) '())
1512 #`(values a b ...
)))
1515 (define (is-ec ret x tail
)
1516 (syntax-case x
(begin let if define
@@)
1520 (or-map (lambda (x) (is-ec ret x
#f
)) #'(a ...
))
1521 (is-ec ret
#'b tail
)))
1523 ((let lp
((y x
) ...
) a ... b
)
1524 (symbol?
(syntax->datum
#'lp
))
1526 (or-map (lambda (x) (is-ec ret x
#f
)) #'(x ...
))
1527 (or-map (lambda (x) (is-ec ret x
#f
)) #'(a ...
))
1528 (is-ec ret
#'b tail
)))
1530 ((let ((y x
) ...
) a ... b
)
1533 (or-map (lambda (x) (is-ec ret x
#f
)) #'(x ...
))
1534 (or-map (lambda (x) (is-ec ret x
#f
)) #'(a ...
))
1535 (is-ec ret
#'b tail
)))
1545 (is-ec ret
#'a tail
)
1546 (is-ec ret
#'b tail
)))
1552 (is-ec ret
#'a tail
)))
1555 (equal?
(syntax->datum
#'return
) (syntax->datum ret
))
1560 (or-map (lambda (x) (is-ec ret x
#f
)) #'(a ...
)))
1568 (let ((code (analyze #'ret
#'l
)))
1569 (if (is-ec #'ret
#'l
#t
)
1570 #`(let/ec ret
#,code
)
1578 (dont-warn (syntax->datum
#'v
))
1579 #'(if (module-defined?
(current-module) 'v
)
1581 (define! 'v
#f
)))))))
1583 (define-inlinable (non? x
) (eq? x
#:nil
))
1585 (define (gentemp stx
) (datum->syntax stx
(gensym "x")))
1589 ((_ (x) (a) code
#f
#f
)
1594 (with-sp ((continue (lp (cdr l
)))
1598 (for/adv1
(x) (a) code
#f
#f
)))
1600 ((_ (x) (a) code
#f
#t
)
1606 (let/ec continue-ret
1608 (with-sp ((continue (continue-ret))
1609 (break (break-ret)))
1612 (for/adv1
(x) (a) code
#f
#t
)))
1614 ((_ (x) (a) code next
#f
)
1617 (let ((x (let lp
((l a
) (old #f
))
1620 (let/ec continue-ret
1621 (with-sp ((continue (continue-ret))
1622 (break (break-ret)))
1627 (for/adv1
(x) (a) code next
#f
)))
1629 ((_ x a code next p
)
1630 (for/adv1 x a code next p
))))
1632 (define-syntax for
/adv1
1635 ((_ (x ...
) (in) code
#f
#f
)
1636 (with-syntax ((inv (gentemp #'in
)))
1637 #'(let ((inv (wrap-in in
)))
1638 (catch StopIteration
1641 (call-with-values (lambda () (next inv
))
1643 (with-sp ((break (values))
1644 (continue (values)))
1647 (lambda z
(values))))))
1649 ((_ (x ...
) (in ...
) code
#f
#f
)
1650 (with-syntax (((inv ...
) (generate-temporaries #'(in ...
))))
1651 #'(let ((inv (wrap-in in
)) ...
)
1652 (catch StopIteration
1655 (call-with-values (lambda () (values (next inv
) ...
))
1657 (with-sp ((break (values))
1658 (continue (values)))
1661 (lambda z
(values))))))
1663 ((_ (x ...
) (in) code
#f
#t
)
1664 (with-syntax ((inv (gentemp #'in
)))
1665 #'(let ((inv (wrap-in in
)))
1668 (catch StopIteration
1670 (call-with-values (lambda () (next inv
))
1672 (let/ec continue-ret
1673 (with-sp ((break (break-ret))
1674 (continue (continue-ret)))
1677 (lambda z
(values))))))))
1679 ((_ (x ...
) (in ...
) code
#f
#t
)
1680 (with-syntax (((inv ...
) (generate-temporaries #'(in ...
))))
1681 #'(let ((inv (wrap-in in
)) ...
)
1684 (catch StopIteration
1686 (call-with-values (lambda () (values (next inv
) ...
))
1688 (let/ec continue-ret
1689 (with-sp ((break (break-ret))
1690 (continue (continue-ret)))
1693 (lambda z
(values))))))))
1695 ((_ (x ...
) in code else
#f
)
1696 #'(for-adv (x ...
) in code else
#f
))
1698 ((_ (x ...
) in code else
#t
)
1699 #'(for-adv (x ...
) in code else
#t
)))))
1702 (define-syntax for-adv
1705 (if (= (length (syntax->datum x
)) (= (length (syntax->datum y
))))
1707 ((x ...
) #'(values (next x
) ...
)))
1712 ((_ (x ...
) (in) code else p
)
1713 (with-syntax ((inv (gentemp #'in
)))
1714 (with-syntax (((xx ...
) (generate-temporaries #'(x ...
))))
1715 (if (syntax->datum
#'p
)
1716 #'(let ((inv (wrap-in in
)))
1719 (catch StopIteration
1722 (call-with-values (lambda () (next inv
))
1725 (let/ec continue-ret
1726 (with-sp ((break (break-ret))
1727 (continue (continue-ret)))
1732 #'(let ((inv (wrap-in in
)))
1735 (catch StopIteration
1738 (call-with-values (lambda () (next inv
))
1741 (with-sp ((break (break-ret))
1742 (continue (values)))
1745 (lambda e else
)))))))))
1747 ((_ (x ...
) (in ...
) code else p
)
1748 (with-syntax (((inv ...
) (generate-temporaries #'(in ...
))))
1749 (with-syntax ((get (gen #'(inv ...
) #'(x ...
)))
1750 ((xx ...
) (generate-temporaries #'(x ...
))))
1751 (if (syntax->datum
#'p
)
1752 #'(let ((inv (wrap-in in
)) ...
)
1755 (catch StopIteration
1758 (call-with-values (lambda () get
)
1761 (let/ec continue-ret
1762 (with-sp ((break (break-ret))
1763 (continue (continue-ret)))
1768 #'(let ((inv (wrap-in in
)) ...
)
1771 (catch StopIteration
1774 (call-with-values (lambda () get
)
1777 (with-sp ((break (break-ret))
1778 (continue (values)))
1781 (lambda e else
))))))))))))
1783 (define-syntax def-wrap
1787 (pr 'def-wrap
#'f
'false
)
1791 (pr 'def-wrap
#'f
'true
)
1793 (define obj
(make <yield
>))
1794 (define ab
(make-prompt-tag))
1795 (slot-set! obj
'k
#f
)
1796 (slot-set! obj
'closed
#f
)
1804 (slot-set! obj
'closed
#t
)
1805 (throw StopIteration
))
1808 (fluid-set! in-yield
#f
)
1820 (define-syntax ref-x
1825 ((_ v
(#:fastfkn-ref f _
) . l
)
1826 #'(ref-x (lambda x
(if (pyclass? v
) (apply f x
) (apply f v x
))) . l
))
1827 ((_ v
(#:fast-id f _
) . l
)
1828 #'(ref-x (f v
) . l
))
1829 ((_ v
(#:identifier x
) . l
)
1830 #'(ref-x (ref v x
) . l
))
1831 ((_ v
(#:call-obj x
) . l
)
1832 #'(ref-x (x v
) . l
))
1833 ((_ v
(#:call x ...
) . l
)
1834 #'(ref-x (v x ...
) . l
))
1835 ((_ v
(#:apply x ...
) . l
)
1836 #'(ref-x (py-apply v x ...
) . l
))
1837 ((_ v
(#:apply x ...
) . l
)
1838 #'(ref-x (py-apply v x ...
) . l
))
1839 ((_ v
(#:vecref x
) . l
)
1840 #'(ref-x (pylist-ref v x
) . l
))
1841 ((_ v
(#:vecsub . x
) . l
)
1842 #'(ref-x (pylist-slice v . x
) . l
)))))
1844 (define-syntax del-x
1846 ((_ v
(#:identifier x
))
1848 ((_ v
(#:call-obj x
))
1850 ((_ v
(#:call x ...
))
1852 ((_ v
(#:apply x ...
))
1855 (pylist-delete! v x
))
1856 ((_ v
(#:vecsub x ...
))
1857 (pylist-subset! v x ... pylist-null
))))
1859 (define-syntax set-x
1861 ((_ v
(a ... b
) val
)
1862 (set-x-2 (ref-x v a ...
) b val
))
1863 ((_ v
#f p pa a val
)
1864 (set-x p pa
(fset-x v a val
)))
1865 ((_ v pre p pa a val
)
1866 (set-c v pre a val
))
1867 ((_ v
(a ... b
) val
)
1868 (set-x-2 (ref-x v a ...
) b val
))))
1870 (define-syntax set-c
1875 (tr v
(fset-x v as val
)))
1876 ((_ v
((#:identifier a
) . as
) (b . bs
) val
)
1877 (set-c (ref v a
) as bs val
))))
1879 (define-syntax fset-x
1881 ((_ v
((#:identifier x
) ...
) val
)
1882 ((@ (oop pf-objects
) fset-x
) v
(list x ...
) val
))))
1884 (define-syntax set-x-2
1886 ((_ v
(#:fastfkn-ref f id
) val
)
1888 ((_ v
(#:fastid-ref f id
) val
)
1890 ((_ v
(#:identifier x
) val
)
1892 ((_ v
(#:vecref n
) val
)
1893 (pylist-set! v n val
))
1894 ((_ v
(#:vecsub x ...
) val
)
1895 (pylist-subset! v x ... val
))))
1898 (define-syntax class-decor
1902 (class-decor (f ...
) (r y
)))))
1904 (define-syntax def-decor
1908 (def-decor (f ...
) (r y
)))))
1910 (define-syntax with-self
1915 (syntax-parameterize ((*self
* (lambda (x) #'s
))) c
))))
1917 (define-syntax with-class
1920 (syntax-parameterize ((*class
* (lambda (x) #'s
))) c
))))
1923 (define-syntax boolit
1924 (syntax-rules (and or not
< <= > >=)
1925 ((_ (and x y
)) (and (boolit x
) (boolit y
)))
1926 ((_ (or x y
)) (or (boolit x
) (boolit y
)))
1927 ((_ (not x
)) (not (boolit x
)))
1928 ((_ (< x y
)) (< x y
))
1929 ((_ (<= x y
)) (<= x y
))
1930 ((_ (> x y
)) (> x y
))
1931 ((_ (>= x y
)) (>= x y
))
1936 (define (export-all)
1937 (define mod
(current-module))
1938 (if (module-defined? mod
'__all__
)
1939 (for ((x : (module-ref mod
'__all__
))) ()
1940 (module-export! mod
(string->symbol
(scm-str x
))))))