Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/python/format.x
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
((if (= conv 111) #t (if (= conv 120) #t (= conv 88)))
(do
(def b (if (= conv 111) 8 16))
(def m (%py-fmt-base v b (= conv 88)))
(def m (%py-fmt-base (%py-index-of v) b (= conv 88)))
(def pfx
(if alt
(if (= conv 111) "0o" (if (= conv 88) "0X" "0x"))
Expand Down
12 changes: 8 additions & 4 deletions lib/python/runtime-call.x
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@
; the bigint path are stated once. The SIGN GOES BEFORE THE PREFIX:
; bin(-15) is -0b1111, not 0b-1111.
(def %py-based-str
(fn (_ v base pfx)
(fn (_ v0 base pfx)
(def v (%py-index-of v0))
(if (not (eq? (python-num-py-type (python-boolnorm v)) (lit int)))
(Err raise (lit type) "an integer is required" ())
(let ((m (%py-fmt-base (python-boolnorm v) base #f)))
Expand All @@ -693,9 +694,12 @@

(def %py-divmod
(fn (_ a b)
(if (if (python-num? a) (python-num? b) #f)
(python-tuple-new (list (python-floordiv a b) (python-mod a b)))
(%py-op-refuse "divmod()" a b))))
(match
((if (python-num? a) (python-num? b) #f)
(python-tuple-new (list (python-floordiv a b) (python-mod a b))))
((if (python-obj-is a) #t (python-obj-is b))
(%py-binop a b "__divmod__" "__rdivmod__" "divmod()"))
(#t (%py-op-refuse "divmod()" a b)))))

; A CLOSURE HAS NO PREDICATE, so its type handle is taken from one built
; here and compared -- the same trick the arithmetic seams use for float.
Expand Down
83 changes: 78 additions & 5 deletions lib/python/runtime-num.x
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,49 @@
(if (null? m) %py-NotImplemented (m y)))
%py-NotImplemented)))

; The plain operation each dunder stands for, which a plain number on the
; left of an int subclass's instance answers with: 1.5 + C(2) is float's add.
; The plain operation each dunder stands for, which a plain number on the
; left of an int subclass's instance answers with: 1.5 + C(2) is float's add.
; Each is reached by name when called, since most are defined below.
(def %py-binop-plain
(list (pair "__add__" (fn (_ x y) (python-add x y)))
(pair "__sub__" (fn (_ x y) (python-sub x y)))
(pair "__mul__" (fn (_ x y) (python-mul x y)))
(pair "__truediv__" (fn (_ x y) (python-div x y)))
(pair "__floordiv__" (fn (_ x y) (python-floordiv x y)))
(pair "__mod__" (fn (_ x y) (python-mod x y)))
(pair "__pow__" (fn (_ x y) (%py-pow x y)))
(pair "__divmod__" (fn (_ x y) (%py-divmod x y)))
(pair "__lshift__" (fn (_ x y) (%py-lshift x y)))
(pair "__rshift__" (fn (_ x y) (%py-rshift x y)))
(pair "__and__" (fn (_ x y) (%py-bitand x y)))
(pair "__or__" (fn (_ x y) (%py-bitor x y)))
(pair "__xor__" (fn (_ x y) (%py-bitxor x y)))))
; Is A a plain number whose own method goes first against the object B?
; Python asks the right operand's reflected method first only when its class
; derives from the left's type; a number beside an object carrying a number
; otherwise answers for itself.
(def %py-num-first?
(fn (_ a b)
(match
((python-obj-is a) #f)
((not (python-obj-is b)) #f)
((not (python-num? a)) #f)
((not (python-num? (python-obj-native b))) #f)
(#t (not (python-subclass? (python-obj-class b) (python-type-of a)))))))
(def %py-binop
(fn (_ a b name rname opname)
(let ((r1 (%py-side a b name)))
(let ((plain (if (%py-num-first? a b) (python-alist-find name %py-binop-plain) ())))
(let ((r1 (if (not (null? plain))
((rest plain) a (python-obj-native b))
(%py-side a b name))))
(if (not (eq? r1 %py-NotImplemented))
r1
(let ((r2 (%py-side b a rname)))
(if (not (eq? r2 %py-NotImplemented))
r2
(%py-op-refuse opname a b)))))))
(%py-op-refuse opname a b))))))))

; augmented + on a list is list.extend, which takes any iterable -- while
; plain + demands a list. Python draws that line and the corpus tests it.
Expand Down Expand Up @@ -272,7 +306,7 @@
; CPython's words.
(def %py-repeat-count
(fn (_ k)
(let ((n (python-boolnorm k)))
(let ((n (python-boolnorm (%py-index-of k))))
(if (eq? (python-num-py-type n) (lit int))
n
(Err raise (lit type)
Expand Down Expand Up @@ -397,12 +431,36 @@
(%py-binop a b "__matmul__" "__rmatmul__" "@")
(%py-op-refuse "@" a b))))

; * with an object on one side. Its __mul__ or __rmul__ answers first; when
; neither does and the other side is a sequence, the object is a repeat count
; -- 'ab' * n for an int subclass's n -- read through __index__.
(def %py-seq-operand?
(fn (_ v)
(match
((python-obj-is v) #f)
((python-str-is v) #t)
((python-list? v) #t)
((python-tuple-is v) #t)
((python-bytes-is v) #t)
(#t (python-dq-is v)))))
(def %py-mul-obj
(fn (_ a b)
(if (if (%py-seq-operand? a) #t (%py-seq-operand? b))
(let ((r1 (%py-side a b "__mul__")))
(if (not (eq? r1 %py-NotImplemented))
r1
(let ((r2 (%py-side b a "__rmul__")))
(match
((not (eq? r2 %py-NotImplemented)) r2)
((%py-seq-operand? a) (python-mul a (%py-repeat-count b)))
(#t (python-mul (%py-repeat-count a) b))))))
(%py-binop a b "__mul__" "__rmul__" "*"))))

(def python-mul
(fn (_ a b)
(match
((%py-plain-nums? a b) (* a b))
((if (python-obj-is a) #t (python-obj-is b))
(%py-binop a b "__mul__" "__rmul__" "*"))
((if (python-obj-is a) #t (python-obj-is b)) (%py-mul-obj a b))
((python-dq-is a) (python-dq-repeat a b))
((python-dq-is b) (python-dq-repeat b a))
((python-list? a)
Expand Down Expand Up @@ -1029,7 +1087,22 @@
(Err raise (lit overflow) "cannot convert float infinity to integer" ()))
(#t (Err raise (lit value) "cannot convert float NaN to integer" ())))))

; round() on an object: its own __round__ answers, and otherwise an instance
; of a class over int or float rounds the number it carries.
(def %py-round
(fn (_ . a)
(match
((if (null? a) #f (python-obj-is (first a)))
(let ((m (python-dunder (first a) "__round__")))
(match
((not (null? m)) (apply m (rest a)))
((python-num? (python-obj-native (first a)))
(apply %py-round-num (pair (python-obj-native (first a)) (rest a))))
(#t (Err raise (lit type)
(Str8 append "type " (Str8 append (python-class-name (python-obj-class (first a)))
" doesn't define __round__ method")) ())))))
(#t (apply %py-round-num a)))))
(def %py-round-num
(fn (_ . a)
(if (null? a)
(Err raise (lit type) "round() missing required argument 'number' (pos 1)" ())
Expand Down
95 changes: 85 additions & 10 deletions lib/python/runtime-py-type.x
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,89 @@
(go 0 0)))
"bit_count" (list "self") 1 #f))

; int's arithmetic, as the methods a subclass of int inherits. A plain int
; never reaches these -- the operators take machine numbers directly -- but an
; instance of `class C(int)` is an object carrying its int, and the operators
; ask an object's class. Each row works on the value the instance carries,
; and answers NotImplemented for an operand that is no number, so the other
; side's reflected method is asked and the refusal names the subclass. The
; answer is a plain int, as CPython's is.
(def %py-int-intlike?
(fn (_ v) (if (python-num? v) (eq? (python-num-py-type (python-boolnorm v)) (lit int)) #f)))
(def %py-int-op
(fn (_ op ok? flip)
(fn (_ self o)
(let ((b (python-native-of o)))
(if (ok? b)
(if flip (op b (python-native-of self)) (op (python-native-of self) b))
%py-NotImplemented)))))
(def %py-int-num-op (fn (_ op flip) (%py-int-op op python-num? flip)))
(def %py-int-bit-op (fn (_ op flip) (%py-int-op op %py-int-intlike? flip)))
(def %py-int-un-op (fn (_ op) (fn (_ self) (op (python-native-of self)))))
(def %py-int-arith-rows
(list
(pair "__add__" (%py-int-num-op python-add #f))
(pair "__radd__" (%py-int-num-op python-add #t))
(pair "__sub__" (%py-int-num-op python-sub #f))
(pair "__rsub__" (%py-int-num-op python-sub #t))
(pair "__mul__" (%py-int-num-op python-mul #f))
(pair "__rmul__" (%py-int-num-op python-mul #t))
(pair "__truediv__" (%py-int-num-op python-div #f))
(pair "__rtruediv__" (%py-int-num-op python-div #t))
(pair "__floordiv__" (%py-int-num-op python-floordiv #f))
(pair "__rfloordiv__" (%py-int-num-op python-floordiv #t))
(pair "__mod__" (%py-int-num-op python-mod #f))
(pair "__rmod__" (%py-int-num-op python-mod #t))
(pair "__divmod__" (%py-int-num-op %py-divmod #f))
(pair "__rdivmod__" (%py-int-num-op %py-divmod #t))
(pair "__pow__" (%py-int-num-op %py-pow #f))
(pair "__rpow__" (%py-int-num-op %py-pow #t))
(pair "__lshift__" (%py-int-bit-op %py-lshift #f))
(pair "__rlshift__" (%py-int-bit-op %py-lshift #t))
(pair "__rshift__" (%py-int-bit-op %py-rshift #f))
(pair "__rrshift__" (%py-int-bit-op %py-rshift #t))
(pair "__and__" (%py-int-bit-op %py-bitand #f))
(pair "__rand__" (%py-int-bit-op %py-bitand #t))
(pair "__or__" (%py-int-bit-op %py-bitor #f))
(pair "__ror__" (%py-int-bit-op %py-bitor #t))
(pair "__xor__" (%py-int-bit-op %py-bitxor #f))
(pair "__rxor__" (%py-int-bit-op %py-bitxor #t))
(pair "__neg__" (%py-int-un-op %py-neg))
(pair "__pos__" (%py-int-un-op %py-pos))
(pair "__invert__" (%py-int-un-op %py-invert))
(pair "__abs__" (%py-int-un-op %py-abs-num))
(pair "__eq__" (%py-int-num-op python-eq #f))
(pair "__lt__" (%py-int-num-op python-lt #f))
(pair "__gt__" (%py-int-num-op python-gt #f))
(pair "__le__" (%py-int-num-op python-le #f))
(pair "__ge__" (%py-int-num-op python-ge #f))
; what int(), float(), an index, bool() and hash() read off an instance
(pair "__int__" (%py-int-un-op python-boolnorm))
(pair "__index__" (%py-int-un-op python-boolnorm))
(pair "__float__" (%py-int-un-op %py-float-ctor))
(pair "__bool__" (%py-int-un-op (fn (_ n) (not (= (python-boolnorm n) 0)))))
(pair "__hash__" (%py-int-un-op %py-hash))
; an int as the rational and the complex number it also is
(pair "real" (python-desc-new (lit property) (%py-int-un-op python-boolnorm)))
(pair "imag" (python-desc-new (lit property) (fn (_ self) 0)))
(pair "numerator" (python-desc-new (lit property) (%py-int-un-op python-boolnorm)))
(pair "denominator" (python-desc-new (lit property) (fn (_ self) 1)))
(pair "conjugate" (python-sig! (%py-int-un-op python-boolnorm) "conjugate" (list "self") 1 #f))
(pair "as_integer_ratio"
(python-sig! (fn (_ self) (python-tuple-new (list (python-boolnorm (python-native-of self)) 1)))
"as_integer_ratio" (list "self") 1 #f))
(pair "is_integer" (python-sig! (fn (_ self) #t) "is_integer" (list "self") 1 #f))))

(def %py-cls-int
(python-class-new "int" python-cls-object
(list (pair "%ctor" %py-int-ctor)
(pair "to_bytes" %py-int-to-bytes)
(pair "bit_length" %py-int-bit-length)
(pair "bit_count" %py-int-bit-count)
; a classmethod, as in Python: int.from_bytes and (5).from_bytes
; both bind the class
(pair "from_bytes" (python-desc-new (lit classmethod) %py-int-from-bytes)))
(pair (pair "%ctor" %py-int-ctor)
(pair (pair "to_bytes" %py-int-to-bytes)
(pair (pair "bit_length" %py-int-bit-length)
(pair (pair "bit_count" %py-int-bit-count)
; a classmethod, as in Python: int.from_bytes and (5).from_bytes
; both bind the class
(pair (pair "from_bytes" (python-desc-new (lit classmethod) %py-int-from-bytes))
%py-int-arith-rows)))))
"int"))

; An int's attribute is a method of the int class bound to the value -- a
Expand All @@ -745,9 +819,10 @@
(Err raise (lit attribute)
(Str8 append "'int' object has no attribute '" (Str8 append name "'")) ()))
((%py-desc-is m)
(if (eq? (%py-desc-py-type m) (lit classmethod))
(%py-bound-new (%py-desc-fn m) %py-cls-int)
(%py-desc-fn m)))
(match
((eq? (%py-desc-py-type m) (lit classmethod)) (%py-bound-new (%py-desc-fn m) %py-cls-int))
((eq? (%py-desc-py-type m) (lit property)) ((%py-desc-fn m) v))
(#t (%py-desc-fn m))))
(#t (%py-bound-new m v))))))
(def %py-cls-bool
(python-class-new "bool" %py-cls-int
Expand Down
10 changes: 9 additions & 1 deletion lib/python/runtime-seq.x
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,17 @@
; A SEQUENCE'S INDEX IS AN INTEGER, and a bool is one: True indexes 1, as in
; Python. Anything else is refused in the sequence's own words -- CPython says
; "string indices" for a str and "byte indices" for bytes, and names the rest.
; What an object's __index__ answers, or the value itself: an int subclass's
; instance, or any class with __index__, stands wherever an int index does.
(def %py-index-of
(fn (_ v)
(if (python-obj-is v)
(let ((m (python-dunder v "__index__")))
(if (null? m) v (python-boolnorm (m))))
v)))
(def %py-seq-index
(fn (_ i what)
(let ((k (python-boolnorm i)))
(let ((k (python-boolnorm (%py-index-of i))))
(if (eq? (python-num-py-type k) (lit int))
k
(Err raise (lit type)
Expand Down
35 changes: 22 additions & 13 deletions lib/python/runtime-str.x
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,19 @@
; what makes `cls` the child in `Sub.method()` -- and a
; property stays the descriptor, since `C.v` in Python is the
; property object, not a value it has no instance to compute.
(if (%py-desc-is m)
(let ((f (%py-desc-fn m)) (k (%py-desc-py-type m)))
(if (eq? k (lit static))
f
(if (eq? k (lit classmethod))
(%py-bound-new f cls)
m)))
m)))))
;
; A descriptor the PROGRAM wrote is asked, as Python asks it:
; __get__(None, cls), so one that answers itself for a class
; still can, and one that computes from the class does.
(match
((%py-desc-is m)
(let ((f (%py-desc-fn m)) (k (%py-desc-py-type m)))
(match
((eq? k (lit static)) f)
((eq? k (lit classmethod)) (%py-bound-new f cls))
(#t m))))
((%py-desc-get? m) ((python-dunder m "__get__") () cls))
(#t m))))))

(python-sweep!)
; What a builtin type offers beyond the methods on its class object: the
Expand Down Expand Up @@ -592,7 +597,7 @@

(def %py-chr
(fn (_ n0)
(def n (python-boolnorm n0))
(def n (python-boolnorm (%py-index-of n0)))
(match
((not (eq? (python-num-py-type n) (lit int)))
(Err raise (lit type) "an integer is required" ()))
Expand Down Expand Up @@ -720,9 +725,13 @@

(def %py-format-spec
(fn (_ v spec)
(if (Str8 =? spec "")
(python-str v)
(do
(match
((Str8 =? spec "") (python-str v))
; an instance of a class over int or float formats the number it
; carries, as int.__format__ and float.__format__ do
((if (python-obj-is v) (python-num? (python-obj-native v)) #f)
(%py-format-spec (python-obj-native v) spec))
(#t (do
(def ps (%py-spec-parse spec))
(def fill (List ref 0 ps))
(def align (List ref 1 ps))
Expand Down Expand Up @@ -905,7 +914,7 @@
(Str8 append "Unknown format code '"
(Str8 append type
(Str8 append "' for object of type '"
(Str8 append (if (eq? py-type (lit int)) "int" "float") "'")))) ())))))))))
(Str8 append (if (eq? py-type (lit int)) "int" "float") "'")))) ()))))))))))

(python-sweep!)
; Digits grouped by k from the right with sep.
Expand Down
Loading
Loading