typing.py compiles and runs

parent 392a09a9
......@@ -138,11 +138,8 @@
(slot-ref o 'hash))
(define-method (len (o <hashtable>))
(pk '============)
(hash-fold (lambda l
(pk '>>>>>>> l)
(apply (lambda ll
(pk '>>> ll)
(+ (caddr ll) 1))
l))
0 o))
......@@ -467,11 +464,9 @@
(define update
(lam (o (* L) (** K))
(pk 'update-py o L)
(match L
((L)
(for ((k v : L)) ()
(pk k)
(pylist-set! o k v)))
(_ #f))
(for ((k v : K)) ()
......
......@@ -120,11 +120,10 @@
(define miss (slask-it ((@ (guile) list) 'miss)))
(define* (getattr a b #:optional (k miss))
(pk 'getattr b k)
(let ((r (pk 'getattr 2 (ref a (if (string? b) (string->symbol b) b) miss))))
(if (pk 'getattr 3 (eq? r miss))
(if (pk 'getattr 4 (eq? k miss))
(if (pk 'getattr 5 (equal? b "__module__"))
(let ((r (ref a (if (string? b) (string->symbol b) b) miss)))
(if (eq? r miss)
(if (eq? k miss)
(if (equal? b "__module__")
"<builtin>"
(raise AttributeError
"object/class ~a is missing attribute ~a" a b))
......@@ -266,7 +265,7 @@
(isinstance i (cdr y)))
(is-a? i y))))
(define s (str ""))
(define s (str "" #f))
(define-method (isinstance (i <string>) (y <p>))
(cond
((ref y '__instancecheck__) =>
......
......@@ -59,7 +59,6 @@
(cons name l)
l)
#:final (reverse l))))))
(pk 'abc_o_a (object-address cls))
(for ((base : bases)) ()
(for ((name : (ref base '__abstractmethods__ '()))) ()
(let ((value (rawref cls name None)))
......
......@@ -606,7 +606,7 @@
(set! verbose xx)
xx)
xx)))
(pk 'namedtyple typename field_names)
(let ((seen (py-set)))
(if (string? field_names)
(set! field_names
......@@ -660,12 +660,10 @@
(py-add! seen name))
(set! field_names (map string->symbol (to-list field_names)))
(pk 'namedtyple typename field_names)
(make-p-class (string->symbol typename) '(())
(lambda (dict)
(pk 'collections 1)
(pylist-set! dict '_fields (map symbol->string field_names))
(pk 'collections 2)
(pylist-set! dict '__init__
(object-method
(eval (v `(lam
......@@ -676,21 +674,18 @@
,@(map (lambda (key) `(set self ',key ,key))
(reverse field_names))))
mod)))
(pk 'collections 3)
(pylist-set! dict '__getitem__
(object-method
(lambda (self i)
(if (number? i)
(ref self (list-ref field_names i))
(ref self (scm-sym i))))))
(pk 'collections 4)
(pylist-set! dict '__setitem__
(object-method
(lambda (self i val)
(if (number? i)
(set self (list-ref field_names i) val)
(set self (scm-sym i) val)))))
(pk 'collections 5)
(pylist-set! dict '__repr__
(object-method
(lambda (self . l)
......@@ -704,7 +699,6 @@
(car l)
(cdr l))))))
(pk 'collections 6)
(if (eq? module None)
(set! module (module-name (current-module)))
(if (string? (scm-str module))
......@@ -713,7 +707,6 @@
(map scm-sym
(string-split module #\.))))))
(pk 'collections 7)
(if verbose (pretty-print verbose))))))
(define UserDict dict)
......
......@@ -71,7 +71,6 @@
(setattr wrapper attr None))))
(for ((attr : updated)) ()
(pk 'attr attr)
(py-update (getattr wrapper attr) (getattr wrapped attr (dict))))
(set wrapper '__wrapped__ wrapped)
......@@ -481,7 +480,7 @@
(let ((result (py-apply user_function (* args) (** kwds))))
(with ((lock))
(cond
((in key cache) (pk 0.1) (values))
((in key cache) (values))
(full
(let ((oldroot root))
(list-set! oldroot KEY key)
......
......@@ -360,6 +360,8 @@
(define (compile x)
(match x
(#t
f-true)
((#:or x)
(compile x))
((#:or . l)
......
......@@ -149,7 +149,9 @@
(define pretty (make-fluid #f))
(define (parse-reg str)
(with-fluids ((*whitespace* ws))
(parse str (f-seq ee f-eof))))
(if (equal? str "")
#t
(parse str (f-seq ee f-eof)))))
(define e-matcher ee)
......
This diff is collapsed.
......@@ -70,8 +70,7 @@
(+ n 1)
#:final n))
(define-syntax-rule (mk set make-py-hashtable)
(define-python-class set (<set>)
(define-python-class set (object <set>)
(define __init__
(case-lambda
((self)
......@@ -312,10 +311,253 @@
(lambda (yield)
(for ((k v : (slot-ref self 'dict))) ()
(yield k)
(values)))))))))
(values))))))))
(define-python-class weak-set (object <set>)
(define __init__
(case-lambda
((self)
(slot-set! self 'dict (make-py-weak-key-hashtable)))
((self x)
(let ((d (make-py-weak-key-hashtable)))
(slot-set! self 'dict d)
(if (eq? x '())
(values)
(for ((y : x)) ()
(pylist-set! d y #t)))))))
(define __bool__
(lambda (self)
(bool (slot-ref self 'dict))))
(define pop
(lambda (self)
(call-with-values (lambda () (pylist-pop! (slot-ref self 'dict)))
(lambda (k v) k))))
(define add
(lambda (self k)
(pylist-set! (slot-ref self 'dict) k #t)))
(define copy
(lambda (self)
(let ((dict (py-copy (slot-ref self 'dict))))
(set dict))))
(define difference
(lambda (self . l)
(let* ((d (slot-ref self 'dict))
(r (py-copy d)))
(let lp ((l l))
(if (pair? l)
(begin
(for ((x : (car l))) ()
(when (in x d)
(pylist-delete! r x)))
(lp (cdr l)))))
(set r))))
(define difference_update
(lambda (self . l)
(let* ((r (slot-ref self 'dict)))
(let lp ((l l))
(if (pair? l)
(begin
(for ((x : (car l))) ()
(when (in x r)
(pylist-delete! r x)))
(lp (cdr l)))))
(values))))
(define discard
(lambda (self . l)
(let* ((r (slot-ref self 'dict)))
(let lp ((l l))
(if (pair? l)
(begin
(pylist-delete! r (car l))
(lp (cdr l))))))))
(define intersection
(lambda (self . l)
(let* ((d (slot-ref self 'dict))
(r (py-copy d)))
(let lp ((l l))
(if (pair? l)
(let ((y (car l)))
(for ((k v : r)) ((dels '()))
(if (not (__contains__ y k))
(cons k dels)
dels)
#:final
(let lp ((dels dels))
(if (pair? dels)
(begin
(pylist-delete! r (car dels))
(lp (cdr dels))))))
(lp (cdr l)))))
(set r))))
(define intersection_update
(lambda (self . l)
(let* ((r (slot-ref self 'dict)))
(let lp ((l l))
(if (pair? l)
(let ((y (car l)))
(for ((k v : r)) ((dels '()))
(if (not (__contains__ y k))
(cons k dels)
dels)
#:final
(let lp ((dels dels))
(if (pair? dels)
(begin
(pylist-delete! r (car dels))
(lp (cdr dels))))))
(lp (cdr l))))))))
(define isdisjoint
(lambda (self x)
(let* ((r (slot-ref self 'dict))
(n1 (len r))
(n2 (len x)))
(if (< n2 n1)
(let ((xx x))
(set! x r)
(set! r xx)))
(for ((k v : r)) ()
(if (in k x)
(break #f))
#:final
#t))))
(define issubset
(lambda (self x)
(let* ((r (slot-ref self 'dict)))
(for ((k v : r)) ()
(if (not (__contains__ x k))
(break #f))
#:final
#t))))
(define issuperset
(lambda (self x)
(let* ((r (slot-ref self 'dict)))
(for ((x v : r)) ()
(if (not (in x r))
(break #f))
#:final
#t))))
(define remove
(lambda (self x)
(let* ((r (slot-ref self 'dict)))
(if (not (in x r))
(raise KeyError "missing key in set at remove")
(pylist-delete! r x)))))
(define symmetric_difference
(lambda (self x)
(union (difference self x) (difference x self))))
(define symmetric_difference_update
(lambda (self x)
(difference_update self x)
(update self (difference x self))))
(define union
(lambda (self . l)
(let* ((d (slot-ref self 'dict))
(r (py-copy d)))
(let lp ((l l))
(if (pair? l)
(begin
(for ((k : (car l))) ()
(pylist-set! r k #t))
(lp (cdr l)))
(set r))))))
(define update
(lambda (self . l)
(let* ((r (slot-ref self 'dict)))
(let lp ((l l))
(if (pair? l)
(begin
(for ((k v : (car l))) ()
(pylist-set! r k #t))
(lp (cdr l)))
(values))))))
(define __repr__
(lambda (self)
(let* ((r (py-keys (slot-ref self 'dict)))
(n (len r))
(l (to-list r)))
(cond
((= n 0)
(format #f "set([])"))
(else
(format #f "set([~a~{, ~a~}])" (car l) (cdr l)))))))
(define __contains__
(lambda (self x)
(let* ((d (slot-ref self 'dict))
(t (slot-ref d 't)))
(not (eq? miss (py-hash-ref t x miss))))))
(define __and__
(lambda (self op)
(intersection self op)))
(define __or__
(lambda (self op)
(union self op)))
(define __sub__
(lambda (self op)
(difference self op)))
(define __xor__
(lambda (self op)
(symmetric_difference self op)))
(define __lt__
(lambda (self other)
(for ((k : self)) ()
(if (in k other)
(values)
(break #f))
#:final #t)))
(define __le__
(lambda (self other)
(and (not (__eq__ self other))
(__le__ self other))))
(define __ge__
(lambda (self other)
(not (__lt__ other self))))
(define __gt__
(lambda (self other)
(not (__le__ other self))))
(define __eq__
(lambda (self x)
(and
(__lt__ self x)
(__lt__ x self))))
(define __iter__
(lambda (self)
((make-generator ()
(lambda (yield)
(for ((k v : (slot-ref self 'dict))) ()
(yield k)
(values))))))))
(mk set make-py-hashtable)
(mk weak-set make-py-weak-key-hashtable)
(define py-set set)
(define-python-class frozenset (set))
......@@ -646,8 +646,16 @@
(define __new__
(lambda x
(apply __init__ x)))
(case-lambda
((cls x)
(__init__ cls x))
((cls x y)
(if y
(__init__ cls x y)
(let* ((o ((rawref object '__new__) cls))
(s (__init__ o x)))
(slot-set! o 'str s)
o)))))
(define __newobj__
(lambda (cls value)
......
......@@ -449,7 +449,6 @@ explicitly tell it to not update etc.
(lp (cdr x))))
'())))
(pk 'pfspecials parents)
(pylist-set! dict '__goops__ goops)
(pylist-set! dict '__zub_classes__ (make-weak-key-hash-table))
(pylist-set! dict '__module__ (make-module))
......@@ -487,7 +486,6 @@ explicitly tell it to not update etc.
(set! parents (to-list parents))
(set! name (if (symbol? name) name (string->symbol name)))
(pk 'pfnew parents)
(let* ((raw? #f)
(goops (catch #t
......@@ -495,9 +493,9 @@ explicitly tell it to not update etc.
(lambda x
(set! raw? #t)
(get-goops meta name parents kw))))
(p (pk 'type-2-p (kwclass->class kw meta)))
(p (kwclass->class kw meta))
(class (make-p p)))
(pk 'newcl0-o_a (object-address class))
(slot-set! class 'procedure
(lambda x
(create-object class x)))
......@@ -527,7 +525,6 @@ explicitly tell it to not update etc.
(if raw?
(begin
(pk 'pfraw parents)
(add-specials rawset class name goops parents meta
(catch #t
(lambda () (pylist-ref kw "doc"))
......@@ -540,8 +537,8 @@ explicitly tell it to not update etc.
(let ((mro (add-default class
(catch #t
(lambda () (pk 1 (pylist-ref dict '__mro__)))
(lambda x (pk 2 (get-mros parents)))))))
(lambda () (pylist-ref dict '__mro__))
(lambda x (get-mros parents))))))
(rawset class '__mro__ mro))
(catch #t
......@@ -554,7 +551,6 @@ explicitly tell it to not update etc.
class)))
(define (new-class0 meta . l)
(pk 'new-class0 meta l)
(match l
((name parents dict . kw)
(type-2 meta name parents dict kw))
......@@ -569,11 +565,8 @@ explicitly tell it to not update etc.
(apply new-class0 meta name parents dict kw)))
(define (type- meta name parents dict keys)
(pk 'type- meta name)
(let ((class (new-class meta name parents dict keys)))
(pk 'type- 1)
(aif it (pk 'teyp- 2
(and meta (find-in-class-and-parents meta '__init__ #f)))
(aif it (and meta (find-in-class-and-parents meta '__init__ #f))
(it class name parents dict keys)
#f)
......@@ -636,7 +629,6 @@ explicitly tell it to not update etc.
(define type-call
(lambda (class . l)
(pk 'type-call class l)
(if (pytype? class)
(aif it (ficap class '__new__ #f)
(apply it class l)
......@@ -667,11 +659,9 @@ explicitly tell it to not update etc.
(make-hash-table)))
(define (create-class meta name parents gen-methods keys)
(pk 'create-class meta name)
(let ((dict (gen-methods (pk 'create-class 2 (get-dict meta name parents)))))
(pk 'create-class 1 meta)
(aif it (and meta (pk '__class (find-in-class-raw meta '__class__ #f)))
(aif it (pk 2 '__call__ (ficap-raw it '__call__ #f))
(let ((dict (gen-methods (get-dict meta name parents))))
(aif it (and meta (find-in-class-raw meta '__class__ #f))
(aif it (ficap-raw it '__call__ #f)
(apply it meta name parents dict keys)
(type- meta name parents dict keys))
(type- meta name parents dict keys))))
......@@ -872,7 +862,7 @@ explicitly tell it to not update etc.
(reshape x))
(values))))
(name-object mset)
(define (pkh h) (hash-for-each (lambda x (pk x)) h) h)
(define-method (mset (x <p>) key val)
......@@ -1133,8 +1123,8 @@ explicitly tell it to not update etc.
(define kw (cdr s.kw))
(define supers (car s.kw))
(define parents (filter-parents supers))
(define cparents (pk 'cparents (get-cparents supers)))
(define meta (pk 'meta (aif it (memq #:metaclass kw)
(define cparents (get-cparents supers))
(define meta (aif it (memq #:metaclass kw)
(cadr it)
(if (null? cparents)
type
......@@ -1155,21 +1145,17 @@ explicitly tell it to not update etc.
(error "TypeError for meta")))
(lp l m mro))
(lp l m mro)))
(() m))))))))
(() m)))))))
(define goops (get-goops meta name supers kw))
(define (gen-methods dict)
(pk 'gen-methods 1)
(methods dict)
(pk 'gen-methods 2)
(add-specials pylist-set! dict name goops supers meta doc)
dict)
(pk 'pf-objects 12)
(let ((cl (with-fluids ((*make-class* #t))
(create-class meta name parents gen-methods kw))))
(pk 'pf-objects 13)
(aif it (ref meta '__init_subclass__)
(let lp ((ps cparents))
(if (pair? ps)
......@@ -1771,24 +1757,18 @@ explicitly tell it to not update etc.
(next-method)))
(define (get-mro parents)
(define (p a x)
(if (and #f (> (length parents) 1))
(pk a '------------------------------- x))
x)
(define (find-comp a b) (lambda (x) (or (a x) (b x))))
(define (find l)
(let ((m (make-hash-table))
(f #t))
(lambda (x)
(p 'search x)
(if f
(begin
(set! f #f)
(let lp ((l l))
(if (pair? l)
(begin
(p 'found (car l))
(car l)
(hashq-set! m (car l) #t)
(or #f
(let ((l (rawref (car l) '__mro__ '())))
......@@ -1799,8 +1779,7 @@ explicitly tell it to not update etc.
#f)))
(hashq-ref m x #f))
(hashq-ref m x #f)))))
(p 'parents parents)
(p 'res
(if (pair? parents)
(let lp ((x (car parents)) (l (cdr parents)) (r (find (cdr parents)))
(ru (lambda (x) #f)))
......@@ -1822,7 +1801,7 @@ explicitly tell it to not update etc.
(if (pair? l)
(lp (car l) (cdr l) (find (cdr l)) ru)
'())))))
'())))
'()))
(define-method (py-equal? (x <p>) y)
(aif it (ref x '__eq__)
......@@ -1908,8 +1887,7 @@ explicitly tell it to not update etc.
(define subclasscheck
(lambda (self c)
(pk self c)
(aif it (pk (rawref c '__mro__ #f))
(aif it (rawref c '__mro__ #f)
(memq self it)
#f)))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment