From ac7d9a507124df0e67b7d234d4e3ee519f968947 Mon Sep 17 00:00:00 2001 From: Stefan Israelsson Tampe Date: Mon, 2 Oct 2017 21:04:37 +0200 Subject: more numerology --- modules/language/python/compile.scm | 37 ++++++++++++----- modules/language/python/number.scm | 79 ++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 11 deletions(-) (limited to 'modules') diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm index 4de9ac6..a8fdeac 100644 --- a/modules/language/python/compile.scm +++ b/modules/language/python/compile.scm @@ -266,6 +266,22 @@ ((__invert__) (N 'py-lognot)) ((__int__) (N 'mk-int)) ((__float__) (N 'mk-float)) + ((__lshift__) (N 'py-lshift)) + ((__rshift__) (N 'py-rshift)) + ((__rlshift__) (N 'py-rlshift)) + ((__rrshift__) (N 'py-rrshift)) + ((as_integer_ratio) (N 'py-as-integer-ratio)) + ((conjugate) (N 'py-conjugate)) + ((fromhex) (N 'py-fromhex)) + ((hex) (N 'py-hex)) + ((imag) (N 'py-imag)) + ((is_integer) (N 'py-is-integer)) + ((real) (N 'py-real)) + ((__mod__) (N 'py-mod)) + ((__rmod__) (N 'py-rmod)) + ((__floordiv__) (N 'py-floordiv)) + ((__rfloordiv__)(N 'py-rfloordiv)) + ((__hex__) (N 'hex)) ;; Lists ((append) (L 'pylist-append!)) @@ -537,35 +553,38 @@ (#:+ ((_ . l) (cons '+ (map (g vs exp) l)))) + (#:- ((_ . l) (cons '- (map (g vs exp) l)))) + (#:* ((_ . l) (cons '* (map (g vs exp) l)))) + (#:/ ((_ . l) (cons (N 'py-/) (map (g vs exp) l)))) (#:% ((_ . l) - (cons 'modulo (map (g vs exp) l)))) + (cons (N 'py-mod) (map (g vs exp) l)))) (#:// ((_ . l) - (cons 'floor-quotient (map (g vs exp) l)))) - + (cons (N 'py-floordiv) (map (g vs exp) l)))) + (#:<< ((_ . l) - (cons (C '<<) (map (g vs exp) l)))) + (cons (N 'py-lshift) (map (g vs exp) l)))) (#:>> ((_ . l) - (cons (C '>>) (map (g vs exp) l)))) + (cons (N 'py-rshift) (map (g vs exp) l)))) (#:u~ ((_ x) - (list 'lognot (exp vs x)))) + (list (N 'py-lognot) (exp vs x)))) (#:u- ((_ x) @@ -577,15 +596,15 @@ (#:band ((_ . l) - (cons 'logand (map (g vs exp) l)))) + (cons (N 'py-logand) (map (g vs exp) l)))) (#:bxor ((_ . l) - (cons 'logxor (map (g vs exp) l)))) + (cons (N 'py-logxor) (map (g vs exp) l)))) (#:bor ((_ . l) - (cons 'logior (map (g vs exp) l)))) + (cons (N 'py-logior) (map (g vs exp) l)))) (#:not ((_ x) diff --git a/modules/language/python/number.scm b/modules/language/python/number.scm index cb4de5e..03b066c 100644 --- a/modules/language/python/number.scm +++ b/modules/language/python/number.scm @@ -6,8 +6,12 @@ #:use-module (language python try) #:use-module (language python exceptions) #:export (py-int py-float py-/ py-logand py-logior py-logxor py-abs + py-lshift py-rshift py-mod py-floordiv - py-divmod pyfloat-listing pyint-listing)) + py-divmod pyfloat-listing pyint-listing + py-as-integer-ratio py-conjugate py-fromhex py-hex py-imag + py-is-integer py-real hex))))) + (p)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -60,6 +64,14 @@ (mk-biop2 b0 rexpt expt __pow__ __rpow__) (b0 equal?) +(define-method (py-lshift (o1 ) (o2 )) + (ash o1 o2)) +(define-method (py-rshift (o1 ) (o2 )) + (ash o1 (- o2))) + +(mk-biop i0 py-rlshift py-lshift __lshift__ __rlshift__) +(mk-biop i0 py-rrshift py-rshift __rshift__ __rrshift__) + (define-method (py-logand (o1 ) (o2 )) (logand o1 o2)) (define-method (py-logior (o1 ) (o2 )) @@ -83,9 +95,12 @@ (define-method (py-divmod (o1 ) (o2 )) (values (floor-quotient o1 o2) - (modulo o1 o2))) + (floor-remainder o1 o2))) +(define-method (py-flordiv (o1 ) (o2 )) + (floor-quotient o1 o2)) +(mk-biop2 b0 py-rflordiv py-flordiv __floordiv__ __rfloordiv__) (mk-biop2 b0 py-rdivmod py-divmod __divmod__ __rdivmod__) (mk-biop2 b0 py-r/ py-/ __truediv__ __rtruediv__) @@ -93,6 +108,14 @@ (mk-biop2 i0 py-rlogior py-logior __or__ __ror__) (mk-biop2 i0 py-rlogxor py-logxor __xor__ __rxor__) +(define-method (py-mod (o1 ) (o2 )) + (modulo o1 o2)) +(define-method (py-mod (o1 ) (o2 )) + (floor-remainder o1 o2)) + +(mk-biop2 io py-rmod py-mod __mod__ __rmod__) + + (define-method (py-abs (o )) (abs o)) (define-method (py-floor (o1 )) o1) (define-method (py-floor (o1 )) ) @@ -120,6 +143,58 @@ (mk-unop u0 py-floor __floor__ ) (mk-unop i0 py-lognot __invert__) +(define-method (py-bit-length (i )) + (logcount i)) + +(define-method (py-conjugate (i )) + (make-rectangular (real-part i) (- (complex-part i)))) +(define-method (py-conjugate (i )) i) + +(define-method (py-imag (i )) (complex-part i)) +(define-method (py-imag (i )) i) + +(define-method (py-real (i )) (real-part i)) +(define-method (py-real (i )) i) + +(define-method (py-denominator ( o)) 0) +(define-method (py-denominator ( o)) + (denominator (inexact->exact o))) + +(define-method (py-numerator ( o)) o) +(define-method (py-numerator ( o)) + (numerator (inexact->exact o))) + +(define-method (py-as-integer-ratio ( o)) + (list o 0)) +(define-method (py-as-integer-ratio ( o)) + (let ((r (inexact->exact o))) + (list (numerator r) (denumerator r)))) + +(define-method (py-fromhex ( o)) + (error "1.2.fromhex('0x1.ap4') is not implemented")) + +(define-method (py-hex ( o)) + (error "1.2.hex() is not implemented")) + +(define-method (py-is-integer ( o)) + (= 0 (denominator (inexact->exact o)))) +(define-method (py-is-integer ( o)) #t) + +(define-method (hex ( o)) + (+ "0x" (number->string i 16))) + +(mk-unop u0 py-conjugate conjugate) +(mk-unop u0 py-imag imag) +(mk-unop u0 py-real real) +(mk-unop u0 py-denominator denominator) +(mk-unop u0 py-numerator numerator) +(mk-unop u0 py-as-integer-ratio as_integer_ratio) +(mk-unop u0 py-fromhex fromhex) +(mk-unop u0 py-hex hex) +(mk-unop i0 hex __hex__) +(mk-unop u0 py-is-integer is_integer) + + (define-method (write (o ) . l) (apply write (slot-ref o 'x) l)) (define-method (write (o ) . l) -- cgit v1.2.3