1 (define-module (oop pf-objects
)
2 #:use-module
(oop goops
)
3 #:use-module
(ice-9 vlist
)
4 #:use-module
(ice-9 match
)
5 #:use-module
(system base message
)
6 #:use-module
(language python guilemod
)
7 #:use-module
(ice-9 pretty-print
)
8 #:use-module
(logic guile-log persistance
)
10 #:export
(set ref make-p
<p
> <py
> <pf
> <pyf
> <property
>
11 call with copy fset fcall put put
!
12 pcall pcall
! get fset-x pyclass?
13 def-p-class mk-p-class make-p-class mk-p-class2
14 define-python-class define-python-class-noname
16 object-method class-method static-method
17 py-super-mac py-super py-equal?
18 *class
* *self
* pyobject? pytype?
19 type object pylist-set
! pylist-ref tr
20 resolve-method-g rawref rawset py-dict
24 Python object system is basically syntactic suger otop of a hashmap and one
25 this project is inspired by the python object system and what it measn when
26 one in stead of hasmaps use functional hashmaps. We use vhashes
, but those have a drawback in that those are not thread safe. But it is a small effort to work
27 with assocs or tree like functional hashmaps in stead.
29 The hashmap works like an assoc e.g. we will define new values by
'consing
' a
30 new binding on the list and when the assoc take up too much space it will be
31 reshaped and all extra bindings will be removed.
33 The datastructure is functional but the objects mutate. So one need to
34 explicitly tell it to not update etc.
37 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
41 (let ((h (slot-ref o
'h
)))
42 (hash-for-each (lambda (k v
)
43 (if (member k
'(__name__ __qualname__
))
49 (let lp
((l (ref o
'__mro__
'())))
53 (if (hash-table?
(slot-ref cl
'h
))
54 (hash-for-each (lambda (k v
)
55 (if (member k
'(__name__ __qualname__
))
65 (define fail
(cons 'fail
'()))
67 (define-syntax-rule (kif it p x y
)
73 (define-method (pylist-set! (o <hashtable
>) key val
)
74 (hash-set! o key val
))
76 (define-method (pylist-ref (o <hashtable
>) key
)
77 (kif it
(hash-ref o key fail
)
79 (error "IndexError")))
81 (define (is-acl? a b
) (member a
(cons b
(class-subclasses b
))))
83 (define-class <p
> (<applicable-struct
> <object
>) h
)
84 (define-class <pf
> (<p
>) size n
) ; the pf object consist of a functional
85 ; hashmap it's size and number of live
87 (define-class <py
> (<p
>))
88 (define-class <pyf
> (<pf
>))
90 (define-class <property
> () get set del
)
96 (name-object <property
>)
98 (define-method (ref (o <procedure
>) key . l
)
99 (aif it
(procedure-property o key
)
105 (define-method (rawref (o <procedure
>) key . l
)
106 (aif it
(procedure-property o key
)
112 (define-method (set (o <procedure
>) key val
)
113 (set-procedure-property! o key val
))
115 (define-method (rawset (o <procedure
>) key val
)
116 (set-procedure-property! o key val
))
118 (define-method (find-in-class (klass <pf
>) key fail
)
119 (let ((r (vhash-assoc key
(slot-ref klass
'h
))))
124 (define-syntax-rule (find-in-class-and-parents klass key fail-
)
125 (aif parents
(find-in-class klass
'__mro__
#f
)
126 (let lp
((parents parents
))
128 (kif r
(find-in-class (car parents
) key fail
)
132 (kif r
(find-in-class klass key fail
)
137 (ficap klass key fail
) (find-in-class-and-parents klass key fail
))
139 (define (mk-getter-object f
)
142 (lambda x
(apply f x
))
145 (lambda x
(apply f obj x
))
148 (lambda x
(apply f obj x
))
151 (define (mk-getter-class f
)
153 (lambda x
(apply f cls x
))))
155 (define (class-method f
)
156 (set f
'__get__
(mk-getter-class f
))
159 (define (object-method f
)
160 (set f
'__get__
(mk-getter-object f
))
163 (define (static-method f
)
168 (define (resolve-method-g g pattern
)
169 (define (mmatch p pp
)
173 (((p . ps
) .
(pp . pps
))
176 (if (or (eq? p pp
) (is-a? p pp
))
177 (cons p
(mmatch ps pps
))
185 (let lp
((x x
) (y y
))
187 (((x . xs
) .
(y . ys
))
193 (let lp
((ms (generic-function-methods g
)))
196 (p (method-specializers m
))
197 (f (method-generic-function m
)))
198 (aif it
(mmatch p pattern
)
199 (cons (cons it f
) (lp (cdr ms
)))
204 (cdr (car (sort l q
<)))))
206 (define (resolve-method-o o pattern
)
207 (resolve-method-g (class-of o
) pattern
))
209 (define (hashforeach a b
) (values))
212 (define (new-class0 meta name parents dict . kw
)
213 (let* ((goops (pylist-ref dict
'__goops__
))
214 (p (kwclass->class kw meta
))
216 (slot-set! class
'procedure
218 (create-object class x
)))
220 (if (hash-table? dict
)
222 (lambda (k v
) k
(set class k v
))
225 (lambda (k v
) k
(set class k v
))
228 (let ((mro (ref class
'__mro__
)))
231 (aif it
(ref p
'__zub_classes__
)
232 (hash-set! it class
#t
)
235 (aif it
(ref p
'__init_subclass__
)
236 (apply it class p
#f kw
)
238 (set class
'__mro__
(cons class
(find-in-class-and-parents
239 class
'__mro__
'())))
242 (define (new-class meta name parents dict kw
)
243 (aif it
(and meta
(ficap meta
'__new__
#f
))
244 (apply it meta name parents dict kw
)
245 (apply new-class0 meta name parents dict kw
)))
247 (define (type- meta name parents dict keys
)
248 (let ((class (new-class meta name parents dict keys
)))
249 (aif it
(and meta
(find-in-class-and-parents meta
'__init__
#f
))
250 (it class name parents dict keys
)
255 (define (the-create-object class x
)
256 (let* ((meta (ref class
'__class__
))
257 (goops (ref class
'__goops__
))
258 (obj (aif it
(ficap class
'__new__
#f
)
260 (make-object class meta goops
))))
262 (aif it
(ref obj
'__init__
)
266 (slot-set! obj
'procedure
268 (aif it
(ref obj
'__call__
)
270 (error "not a callable object"))))
274 (define (create-object class x
)
276 (apply type-call class x
)
277 (let ((meta (find-in-class class
'__class__
#f
)))
278 (with-fluids ((*make-class
* #t
))
279 (aif it
(ficap meta
'__call__
#f
)
281 (the-create-object class x
))))))
288 (ref obj
'__class__
'None
))
289 ((meta name bases dict . keys
)
290 (type- meta name bases dict keys
)))
292 (the-create-object class l
))))
294 (define (get-dict self name parents
)
295 (aif it
(and self
(ficap self
'__prepare__
#f
))
296 (it self name parents
)
299 (define (create-class meta name parents gen-methods keys
)
300 (let ((dict (gen-methods (get-dict meta name parents
))))
301 (aif it
(ref meta
'__class__
)
302 (aif it
(find-in-class it
'__call__
#f
)
303 (apply it meta name parents dict keys
)
304 (type- meta name parents dict keys
))
305 (type- meta name parents dict keys
))))
307 (define (make-object class meta goops
)
308 (let ((obj (make-p goops
)))
309 (set obj
'__class__ class
)
312 ;; Make an empty pf object
314 (let ((r (make <x
>)))
317 (slot-set! r
'h vlist-null
)
318 (slot-set! r
'size
0)
321 (slot-set! r
'h
(make-hash-table)))
323 (error "make-p in pf-objects need a <p> or <pf> derived class got ~a"
328 (define-syntax-rule (hif it
(k h
) x y
)
329 (let ((a (vhash-assq k h
)))
335 (define-syntax-rule (cif (it h
) (k cl
) x y
)
336 (let* ((h (slot-ref cl
'h
))
337 (a (vhash-assq k h
)))
343 (define-inlinable (gox obj it
)
344 (let ((class (fluid-ref *location
*)))
345 (aif f
(rawref it
'__get__
)
349 (define-inlinable (gokx obj class it
)
350 (aif f
(rawref it
'__get__
)
354 (define *location
* (make-fluid #f
))
355 (define-syntax-rule (mrefx x key l
)
367 (begin (fluid-set! *location
* p
) it
)
372 (begin (fluid-set! *location
* x
) it
)
373 (hif cl
('__class__ h
)
375 (begin (fluid-set! *location
* cl
) it
)
377 (let ((r (parents p
)))
384 (define-method (find-in-class (klass <p
>) key fail
)
385 (hash-ref (slot-ref klass
'h
) key fail
))
387 (define-syntax-rule (mrefx klass key l
)
389 (define (end) (if (pair? l
) (car l
) #f
))
390 (fluid-set! *location
* klass
)
391 (kif it
(find-in-class-and-parents klass key fail
)
393 (aif klass
(find-in-class klass
'__class__
#f
)
395 (fluid-set! *location
* klass
)
396 (kif it
(find-in-class-and-parents klass key fail
)
401 (define not-implemented
(cons 'not
'implemeneted
))
403 (define-syntax-rule (mrefx-py x key l
)
405 (let* ((f (aif it
(or (mrefx xx
'__getattribute__
'())
406 (mrefx xx
'__getattr__
'()))
409 (if (or (not f
) (eq? f not-implemented
))
410 (gox xx
(mrefx xx key l
))
412 (lambda () (gox xx
(f xx key
)))
413 (lambda z
(if (pair? l
) (car l
) #f
)))))))
415 (define-syntax-rule (mref x key l
)
419 (define-syntax-rule (mref-py x key l
)
421 (let ((res (mrefx-py xx key l
)))
424 (define-method (ref x key . l
)
427 (apply ref NoneObj key l
))
433 (define-method (ref (x <pf
> ) key . l
) (mref x key l
))
434 (define-method (ref (x <p
> ) key . l
) (mref x key l
))
435 (define-method (ref (x <pyf
>) key . l
) (mref-py x key l
))
436 (define-method (ref (x <py
> ) key . l
) (mref-py x key l
))
438 (define-method (rawref x key . l
) (if (pair? l
) (car l
) #f
))
439 (define-method (rawref (x <pf
> ) key . l
) (mref x key l
))
440 (define-method (rawref (x <p
> ) key . l
) (mref x key l
))
443 (define-method (set (f <procedure
>) key val
)
444 (set-procedure-property! f key val
))
446 (define-method (ref (f <procedure
>) key . l
)
447 (aif it
(assoc key
(procedure-properties f
))
449 (if (pair? l
) (car l
) #f
)))
452 ;; the reshape function that will create a fresh new pf object with less size
453 ;; this is an expensive operation and will only be done when we now there is
454 ;; a lot to gain essentially tho complexity is as in the number of set
456 (let ((h (slot-ref x
'h
))
457 (m (make-hash-table))
459 (define h2
(vhash-fold (lambda (k v s
)
460 (if (hash-ref m k
#f
)
465 (vhash-consq k v s
))))
469 (slot-set! x
'size n
)
473 ;; on object x add a binding that key -> val
474 (define-method (mset (x <pf
>) key val
)
475 (let ((h (slot-ref x
'h
))
476 (s (slot-ref x
'size
))
478 (slot-set! x
'size
(+ 1 s
))
479 (let ((r (vhash-assoc key h
)))
481 (slot-set! x
'n
(+ n
1)))
482 (slot-set! x
'h
(vhash-cons key val h
))
487 (define (pkh h
) (hash-for-each (lambda x
(pk x
)) h
) h
)
489 (define-method (mset (x <p
>) key val
)
491 (hash-set! (slot-ref x
'h
) key val
)
494 (define *make-class
* (make-fluid #f
))
495 (define (mc?
) (not (fluid-ref *make-class
*)))
497 (define-syntax-rule (mset-py x key val
)
499 (v (mref xx key
(list fail
))))
501 (let* ((g (mrefx xx
'__fset__
'(#t
)))
504 (aif it
(rawref xx
'__setattr__
)
506 (rawset xx
'__fset__ it
)
510 (rawset xx
'__fset__ it
))
514 (if (or (eq? f not-implemented
) (not f
))
517 (lambda () (f key val
))
518 (lambda q
(mset xx key val
)))))
520 (aif it
(ref v
'__class__
)
521 (aif it
(ref it
'__set__
)
524 (mset xx key val
)))))
526 (define-syntax-rule (mklam (mset a ...
) val
)
529 (define-method (set (x <pf
>) key val
) (mklam (mset x key
) val
))
530 (define-method (set (x <p
>) key val
) (mklam (mset x key
) val
))
531 (define-method (set (x <pyf
>) key val
) (mklam (mset-py x key
) val
))
532 (define-method (set (x <py
>) key val
) (mklam (mset-py x key
) val
))
534 (define-method (rawset (x <pf
>) key val
) (mklam (mset x key
) val
))
535 (define-method (rawset (x <p
>) key val
) (mklam (mset x key
) val
))
537 ;; mref will reference the value of the key in the object x, an extra default
538 ;; parameter will tell what the fail object is else #f if fail
539 ;; if there is no found binding in the object search the class and
540 ;; the super classes for a binding
542 ;; call a function as a value of key in x with the object otself as a first
543 ;; parameter, this is pythonic object semantics
544 (define-syntax-rule (mk-call mcall mref
)
545 (define-syntax-rule (mcall x key l
)
546 (apply (mref x key
'()) l
)))
549 (mk-call mcall-py mref-py
)
551 (define-method (call (x <pf
>) key . l
) (mcall x key l
))
552 (define-method (call (x <p
>) key . l
) (mcall x key l
))
553 (define-method (call (x <pyf
>) key . l
) (mcall-py x key l
))
554 (define-method (call (x <py
>) key . l
) (mcall-py x key l
))
557 ;; make a copy of a pf object
558 (define-syntax-rule (mcopy x
)
559 (let ((r (make-p (ref x
'__goops__
))))
560 (slot-set! r
'h
(slot-ref x
'h
))
561 (slot-set! r
'size
(slot-ref x
'size
))
562 (slot-set! r
'n
(slot-ref x
'n
))
565 (define-syntax-rule (mcopy- x
)
566 (let* ((r (make-p (ref x
'__goops__
)))
568 (hash-for-each (lambda (k v
) (hash-set! h k v
)) (slot-ref x
'h
))
571 (define-method (copy (x <pf
>)) (mcopy x
))
572 (define-method (copy (x <p
> )) (mcopy- x
))
574 ;; make a copy of a pf object
575 (define-syntax-rule (mtr r x
)
577 (slot-set! r
'h
(slot-ref x
'h
))
578 (slot-set! r
'size
(slot-ref x
'size
))
579 (slot-set! r
'n
(slot-ref x
'n
))
582 (define-syntax-rule (mtr- r x
)
584 (slot-set! r
'h
(slot-ref x
'h
))
588 (define-method (tr (r <pf
>) (x <pf
>)) (mtr r x
))
589 (define-method (tr (r <p
> ) (x <p
> )) (mtr- r x
))
592 ;; with will execute thunk and restor x to it's initial state after it has
593 ;; finished note that this is a cheap operatoin because we use a functional
595 (define-syntax-rule (mwith x thunk
)
596 (let ((old (mcopy x
)))
598 (slot-set! x
'h
(slot-ref old
'h
))
599 (slot-set! x
'size
(slot-ref old
'size
))
600 (slot-set! x
'n
(slot-ref old
'n
))
603 (define-syntax-rule (mwith- x thunk
)
604 (let ((old (mcopy- x
)))
606 (slot-set! x
'h
(slot-ref old
'h
))
611 ;; a functional set will return a new object with the added binding and keep
613 (define-method (fset (x <pf
>) key val
)
618 (define-method (fset (x <p
>) key val
)
619 (let ((x (mcopy- x
)))
623 (define (fset-x obj l val
)
624 (let lp
((obj obj
) (l l
) (r '()))
627 (let lp
((v val
) (r r
))
629 (lp (fset (caar r
) (cdar r
) v
) (cdr r
))
632 (lp (ref obj k
#f
) l
(cons (cons obj k
) r
))))))
638 ;; a functional call will keep x untouched and return (values fknval newx)
639 ;; e.g. we get both the value of the call and the new version of x with
640 ;; perhaps new bindings added
641 (define-method (fcall (x <pf
>) key . l
)
644 (if (eq?
(slot-ref x
'h
) (slot-ref y
'h
))
648 (define-method (fcall (x <p
>) key . l
)
650 (values (mcall x key l
)
653 ;; this shows how we can override addition in a pythonic way
655 ;; lets define get put pcall etc so that we can refer to an object like
656 ;; e.g. (put x.y.z 1) (pcall x.y 1)
658 (define-syntax-rule (cross x k f set
)
659 (call-with-values (lambda () f
)
663 (values r
(set x k y
))))))
665 (define-syntax-rule (cross! x k f _
) f
)
669 ((_ cross set setx f
(key) (val ...
))
670 (setx f key val ...
))
671 ((_ cross set setx f
(k . l
) val
)
672 (cross f k
(mku cross set setx
(ref f k
) l val
) set
))))
674 (define-syntax-rule (mkk pset setx set cross
)
679 (let* ((to (lambda (x)
680 (datum->syntax
#'f
(string->symbol x
))))
681 (l (string-split (symbol->string
(syntax->datum
#'f
)) #\.
)))
682 (with-syntax (((a (... ...
)) (map (lambda (x) #`'#,(to x
))
685 #'(mku cross setx set h
(a (... ...
)) (val (... ...
))))))))))
687 (mkk put fset fset cross
)
688 (mkk put
! set set cross
!)
689 (mkk pcall
! call fset cross
!)
690 (mkk pcall fcall fset cross
)
691 (mkk get ref fset cross
!)
693 ;; it's good to have a null object so we don't need to construct it all the
694 ;; time because it is functional we can get away with this.
695 (define null
(make-p <pf
>))
697 (define (filter-parents l
)
700 (if (is-a?
(car l
) <p
>)
701 (cons (car l
) (lp (cdr l
)))
705 (define (kw->class kw meta
)
706 (if (memq #:functional kw
)
709 (if (or (not meta
) (is-a? meta
<pyf
>) (is-a? meta
<py
>))
713 (if (or (is-a? meta
<pyf
>) (is-a? meta
<pf
>))
729 (define (defaulter d
)
731 (aif it
(ref d
'__goops__
)
738 (define (kwclass->class kw default
)
739 (if (memq #:functionalClass kw
)
740 (if (memq #:fastClass kw
)
742 (if (memq #:pyClass kw
)
744 (if (or (is-a? default
<py
>) (is-a? default
<pyf
>))
747 (if (memq #:mutatingClass kw
)
748 (if (memq #:fastClass kw
)
750 (if (memq #:pyClass kw
)
752 (if (or (is-a? default
<py
>) (is-a? default
<pyf
>))
755 (if (memq #:fastClass kw
)
756 (if (or (is-a? default
<pf
>) (is-a? default
<pyf
>))
759 (if (memq #:pyClass kw
)
760 (if (or (is-a? default
<pf
>) (is-a? default
<pyf
>))
763 (defaulter default
))))))
769 ((name supers.kw methods
)
770 (make-p-class name
"" supers.kw methods
))
771 ((name doc supers.kw methods
)
772 (define s.kw supers.kw
)
773 (define kw
(cdr s.kw
))
774 (define supers
(car s.kw
))
775 (define goopses
(map (lambda (sups)
776 (aif it
(ref sups
'__goops__
#f
)
781 (define parents
(let ((p (filter-parents supers
)))
784 (define cparents
(if (null? parents
)
790 (define meta
(aif it
(memq #:metaclass kw
)
794 (let* ((p (car cparents
))
795 (m (ref p
'__class__
))
796 (mro (reverse (ref m
'__mro__
'()))))
797 (let lp
((l (cdr cparents
))
802 (meta (ref p
'__class__
))
803 (mro (ref meta
'__mro__
'())))
804 (let lp2
((max max
) (mr (reverse mro
)))
805 (if (and (pair? max
) (pair? mr
))
806 (if (eq?
(car max
) (car mr
))
807 (lp2 (cdr max
) (cdr mr
))
809 "need a common lead for meta"))
811 (if (< (length mro
) (length min
))
813 (lp (cdr l
) max min
))
814 (lp (cdr l
) mro min
)))))
815 (car (reverse min
))))))))
817 (define goops
(make-class (append goopses
818 (list (kw->class kw meta
)))
821 (define (make-module)
822 (let ((l (module-name (current-module))))
823 (if (and (>= (length l
) 3)
824 (equal?
(list-ref l
0) 'language
)
825 (equal?
(list-ref l
1) 'python
)
826 (equal?
(list-ref l
2) 'module
))
828 (map symbol-
>string
(cdddr l
))
832 (define (gen-methods dict
)
833 (define (filt-bases x
)
838 (cons y
(lp (cdr x
)))
843 (pylist-set! dict
'__goops__ goops
)
844 (pylist-set! dict
'__class__ meta
)
845 (pylist-set! dict
'__zub_classes__
(make-weak-key-hash-table))
846 (pylist-set! dict
'__module__
(make-module))
847 (pylist-set! dict
'__bases__
(filt-bases parents
))
848 (pylist-set! dict
'__fget__
#t
)
849 (pylist-set! dict
'__fset__
#t
)
850 (pylist-set! dict
'__name__ name
)
851 (pylist-set! dict
'__qualname__ name
)
852 (pylist-set! dict
'__class__ meta
)
853 (pylist-set! dict
'__mro__
(get-mro cparents
))
854 (pylist-set! dict
'__doc__ doc
)
857 (let ((cl (with-fluids ((*make-class
* #t
))
858 (create-class meta name parents gen-methods kw
))))
860 (aif it
(ref meta
'__init_subclass__
)
861 (let lp
((ps cparents
))
863 (let ((super (car ps
)))
872 ;; Let's make an object essentially just move a reference
874 ;; the make class and defclass syntactic sugar
876 (define-syntax make-up
877 (syntax-rules (lambda case-lambda lambda
* letrec letrec
*)
879 (object-method (lambda . l
)))
880 ((_ (case-lambda . l
))
881 (object-method (case-lambda . l
)))
883 (object-method (lambda* . l
)))
885 (object-method (letrec . l
)))
887 (object-method (letrec* . l
)))
890 (define-syntax mk-p-class
893 ((_ name parents
(ddef dname dval
) ...
)
894 #'(mk-p-class name parents
"" (ddef dname dval
) ...
))
895 ((_ name parents doc
(ddef dname dval
) ...
)
896 (with-syntax (((ddname ...
)
903 (syntax->datum
#'name
))
906 (syntax->datum dn
))))))
908 (nname (datum->syntax
913 (syntax->datum
#'name
))
915 (%add-to-warn-list
(syntax->datum
#'nname
))
916 (map (lambda (x) (%add-to-warn-list
(syntax->datum x
)))
920 (letruc ((dname (make-up dval
)) ...
)
922 (make-p-class 'name doc
925 (pylist-set! dict
'dname dname
)
929 (module-define! (current-module) 'ddname dname
)
930 (name-object ddname
))
934 (module-define! (current-module) 'nname
(ref name
'__goops__
))
939 (define-syntax mk-p-class2
942 ((_ name parents
((ddef dname dval
) ...
) body
)
943 #'(mk-p-class2 name parents
"" ((ddef dname dval
) ...
) body
))
944 ((_ name parents doc
((ddef dname dval
) ...
) body
)
945 (with-syntax (((ddname ...
)
952 (syntax->datum
#'name
))
955 (syntax->datum dn
))))))
957 (nname (datum->syntax
962 (syntax->datum
#'name
))
964 (%add-to-warn-list
(syntax->datum
#'nname
))
965 (map (lambda (x) (%add-to-warn-list
(syntax->datum x
)))
969 (letruc ((dname (make-up dval
)) ...
)
972 (make-p-class 'name doc
975 (pylist-set! dict
'dname dname
)
979 (module-define! (current-module) 'ddname dname
)
980 (name-object ddname
))
983 (module-define! (current-module) 'nname
(ref name
'__goops__
))
988 (define-syntax mk-p-class-noname
991 ((_ name parents
(ddef dname dval
) ...
)
992 #'(mk-p-class-noname name parents
"" (ddef dname dval
) ...
))
993 ((_ name parents doc
(ddef dname dval
) ...
)
996 (letruc ((dname dval
) ...
)
997 (make-p-class 'name doc
1000 (pylist-set! dict
'dname dname
)
1005 (define-syntax-rule (def-p-class name . l
)
1006 (define name
(mk-p-class name . l
)))
1008 (define (get-class o
)
1013 (error "not a pyclass"))))
1015 (define (get-type o
)
1029 (define p
(if (pyclass? o
) "C" (if (pyobject? o
) "O" "T")))
1030 (define port
(if (pair? l
) (car l
) #t
))
1032 (aif it
(if (pyclass? o
)
1039 p
(get-type o
) (it))
1042 p
(get-type o
) (ref o
'__name__
'Annonymous
)))))
1044 (define-method (write (o <p
>) . l
) (print o l
))
1045 (define-method (display (o <p
>) . l
) (print o l
))
1047 (define (arglist->pkw l
)
1048 (let lp
((l l
) (r '()))
1052 (cons (reverse r
) l
)
1053 (lp (cdr l
) (cons x r
))))
1054 (cons (reverse r
) '()))))
1056 (define-syntax-rule (define-python-class name
(parents ...
) code ...
)
1058 (syntax-parameterize ((*class
* (lambda (x) #'name
)))
1059 (mk-p-class name
(arglist->pkw
(list parents ...
)) code ...
))))
1061 (define-syntax-rule (define-python-class-noname name
(parents ...
) code ...
)
1063 (syntax-parameterize ((*class
* (lambda (x) #'name
)))
1064 (mk-p-class-noname name
(arglist->pkw
(list parents ...
))
1068 (define-syntax make-python-class
1071 ((_ name
(parents ...
) code ...
)
1072 #'(let* ((cl (mk-p-class name
1073 (arglist->pkw
(list parents ...
))
1077 (define type-goops
#f
)
1079 (if (not type-goops
) (set! type-goops
(ref type
'__goops__
)))
1081 (aif it
(find-in-class x
'__goops__
#f
)
1085 (member it
(class-subclasses type-goops
)))
1090 (define (pyobject? x
) (eq?
(kind x
) 'object
))
1091 (define (pyclass? x
) (eq?
(kind x
) 'class
))
1092 (define (pytype? x
) (eq?
(kind x
) 'type
))
1094 (define (mark-fkn tag f
)
1095 (set-procedure-property! f
'py-special tag
)
1098 (define-syntax-parameter
1099 *class
* (lambda (x) (error "*class* not parameterized")))
1100 (define-syntax-parameter
1101 *self
* (lambda (x) (error "*class* not parameterized")))
1103 (define *super
* (list 'super
))
1105 (define (not-a-super) 'not-a-super
)
1106 (define (py-super class obj
)
1107 (define (make cl parents
)
1110 (if (or (pyclass? obj
) (pytype? obj
))
1112 (let ((c (make-p <py
>))
1114 (set c
'__class__ type
)
1115 (set c
'__mro__
(cons c parents
))
1116 (set c
'__getattribute__
(lambda (self key . l
)
1117 (aif it
(ficap c key
#f
)
1121 (error "no attribute"))))
1122 (set c
'__name__
"**super**")
1123 (set o
'__class__ c
)
1128 (let lp
((l (ref (if (or (pytype? obj
) (pyclass? obj
))
1130 (ref obj
'__class__
))
1133 (if (eq? class
(car l
))
1144 (define-syntax py-super-mac
1147 (py-super *class
* *self
*))
1149 (py-super class self
))))
1152 (pretty-print (syntax->datum x
))
1155 (define-syntax letruc
1158 ((_ ((x v
) ...
) code ...
)
1159 (let lp
((a #'(x ...
)) (b #'(v ...
)) (u '()))
1162 (s (syntax->datum x
)))
1163 (let lp2
((a2 (cdr a
)) (b2 (cdr b
)) (a3 '()) (b3 '())
1166 (if (eq?
(syntax->datum a2
) s
)
1167 (lp2 (cdr a2
) (cdr b2
) a3 b3
(cons (car b2
) r
))
1168 (lp2 (cdr a2
) (cdr b2
)
1172 (lp (reverse a3
) (reverse b3
)
1174 (list x
#`(let* #,(map (lambda (v) (list x v
))
1177 #`(letrec #,(reverse u
) code ...
)))))))
1182 (define-method (py-init (o <p
>) . l
)
1183 (apply (ref o
'__init__
) l
))
1189 ((root hist
) (vector root hist
))))
1191 (define (geth t
) (vector-ref t
1))
1192 (define (getr t
) (vector-ref t
0))
1193 (define (tree-ref t
) (car (getr t
)))
1199 (dive (car r
) (cons (cdr r
) h
))
1205 (up (car h
) (cdr h
))
1212 (let ((r (getr tree
)) (h (geth tree
)))
1219 (dive x
(cons (cdr r
) h
))
1222 (up (car h
) (cdr h
))
1226 (up (car h
) (cdr h
))
1229 (define (class-to-tree cl
) (cons cl
(map class-to-tree
(ref cl
'__bases__
))))
1231 (define (find-tree o tree
)
1233 (let ((x (tree-ref tree
)))
1236 (find-tree o
(nxt tree
))))
1239 (define (linearize x
)
1243 (append (linearize (car x
)) (linearize (cdr x
))))
1246 (define (get-mro parents
)
1250 (get-mro0 (map class-to-tree parents
)))))
1252 (define (get-mro0 parents
)
1253 (define tree
(mk-tree parents
))
1254 (let lp
((tree tree
) (r '()))
1256 (let ((x (tree-ref tree
))
1263 (define-method (py-equal?
(x <p
>) y
)
1264 (aif it
(ref x
'__eq__
)
1268 (define-method (py-equal? y
(x <p
>))
1269 (aif it
(ref x
'__eq__
)
1273 (define-method (py-equal? x y
) ((@ (guile) equal?
) x y
))
1275 (define (equal? x y
) (or (eq? x y
) (py-equal? x y
)))
1277 (define (subclasses self
)
1278 (aif it
(ref self
'__zubclasses__
)
1279 (let ((h (make-hash-table)))
1281 (let lp
((l (hash-fold
1291 (hash-fold (lambda (k v s
) (cons k s
)) '() h
))
1295 (make-python-class type
()
1296 (define __new__ new-class0
)
1297 (define __init_subclass__
(lambda x
(values)))
1298 (define ___zub_classes__
(make-weak-key-hash-table))
1299 (define __subclasses__ subclasses
)
1300 (define __call__ type-call
)
1301 (define mro
(lambda (self) (ref self
'__mro__
)))))
1303 (set type
'__class__ type
)
1305 (set! object
(make-python-class object
()
1306 (define __init__
(lambda x
(values)))
1307 (define __subclasses__ subclasses
)
1308 (define __weakref__
(lambda (self) self
))))
1312 (name-object object
)
1314 (define-method (py-class (o <p
>))
1315 (aif it
(ref o
'__class__
)
1320 (define-method (py-dict (o <p
>))
1321 (aif it
(ref o
'__dict__
)
1325 (define-python-class NoneObj
()