read hex floats
0x1.8p3 was no number to the reader, so it became a symbol C got inside |pipes|, and _ could not type it. It reads now as a literal written as it was: the digits of a double's own bits, which no decimal rounds to.
This commit is contained in:
51
reader.scm
51
reader.scm
@@ -198,7 +198,7 @@
|
||||
((string=? s ".") dot-token)
|
||||
((or (string->number digits) (radix-number digits))
|
||||
=> (lambda (n) (c-number n s)))
|
||||
((suffixed-number digits s) => identity)
|
||||
((spelled-number digits s) => identity)
|
||||
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||
(else (string->symbol s)))))
|
||||
|
||||
@@ -232,8 +232,8 @@
|
||||
((and (real? n) (inexact? n) (finite? n)) n)
|
||||
(else (error "C has no literal for this number" s))))
|
||||
|
||||
;;; 1u, 10L, 1ULL, 1.5f, 0.1L: a number with C's suffix, which the C is
|
||||
;;; written with.
|
||||
;;; 1u, 10L, 1ULL, 1.5f, 0.1L, 0x1.8p3: a number with C's suffix, or a
|
||||
;;; hex float, which the C is written with.
|
||||
;;;
|
||||
;;; 0b101u -> 5u in decimal, so C23's 0b never reaches C11
|
||||
;;; 0.1f -> 0.1f in the digits written: through a double, 0.1f
|
||||
@@ -260,7 +260,10 @@
|
||||
(define suffixed-float
|
||||
(irregex "((?:[0-9]+\\.[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)([fFlL])"))
|
||||
|
||||
(define (suffixed-number digits s)
|
||||
(define hex-float
|
||||
(irregex "(0[xX]([0-9a-fA-F]*)\\.?([0-9a-fA-F]*)[pP]([+-]?[0-9]+))([fFlL]?)"))
|
||||
|
||||
(define (spelled-number digits s)
|
||||
(let* ((negative? (and (> (string-length digits) 1) (char=? #\- (string-ref digits 0))))
|
||||
(body (if negative? (substring digits 1) digits))
|
||||
;; each spelling begins with a digit or a point, and few names do
|
||||
@@ -268,7 +271,8 @@
|
||||
(or (char-numeric? (string-ref body 0))
|
||||
(char=? #\. (string-ref body 0)))
|
||||
(or (suffixed-integer-literal body s)
|
||||
(suffixed-float-literal body s)))))
|
||||
(suffixed-float-literal body s)
|
||||
(hex-float-literal body s)))))
|
||||
(and literal (if negative? (list '- literal) literal))))
|
||||
|
||||
(define (suffixed-integer-literal body s)
|
||||
@@ -282,22 +286,41 @@
|
||||
(error "too large for its suffix" s))
|
||||
(make-c-literal n suffix #f)))))
|
||||
|
||||
;;; 0x1.8p3 is 0x18 sixteenths times 2^3
|
||||
(define (hex-float-literal body s)
|
||||
(let ((m (irregex-match hex-float body)))
|
||||
(and m
|
||||
(let ((whole (irregex-match-substring m 2))
|
||||
(fraction (irregex-match-substring m 3))
|
||||
(exponent (string->number (irregex-match-substring m 4)))
|
||||
(suffix (irregex-match-substring m 5)))
|
||||
(and (> (+ (string-length whole) (string-length fraction)) 0)
|
||||
(float-literal
|
||||
(if (> (abs exponent) 20000) ; past any C type
|
||||
(if (> exponent 0) +inf.0 0.)
|
||||
(exact->inexact
|
||||
(* (string->number (string-append whole fraction) 16)
|
||||
(expt 2 (- exponent (* 4 (string-length fraction)))))))
|
||||
suffix (irregex-match-substring m 1) s))))))
|
||||
|
||||
(define (suffixed-float-literal body s)
|
||||
(let ((m (irregex-match suffixed-float body)))
|
||||
(and m
|
||||
(let* ((digits (irregex-match-substring m 1))
|
||||
(suffix (irregex-match-substring m 2))
|
||||
(n (string->number digits)))
|
||||
(unless (in-range? n suffix)
|
||||
(error "C has no literal for this number" s))
|
||||
(make-c-literal n suffix digits)))))
|
||||
(let ((digits (irregex-match-substring m 1)))
|
||||
(float-literal (string->number digits) (irregex-match-substring m 2)
|
||||
digits s)))))
|
||||
|
||||
(define (float-literal n suffix digits s)
|
||||
(unless (in-range? n suffix)
|
||||
(error "C has no literal for this number" s))
|
||||
(make-c-literal n suffix digits))
|
||||
|
||||
;;; A float overflows where it would round past FLT_MAX; a long double
|
||||
;;; may be wider than any double, so whatever a double holds it does too
|
||||
(define (in-range? n suffix)
|
||||
(case (string-ref suffix 0)
|
||||
((#\f #\F) (< (abs n) (* (- 2 (expt 2. -24)) (expt 2. 127))))
|
||||
(else #t)))
|
||||
(cond ((string-ci=? suffix "f") (< (abs n) (* (- 2 (expt 2. -24)) (expt 2. 127))))
|
||||
((string-ci=? suffix "l") #t)
|
||||
(else (finite? n))))
|
||||
|
||||
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
|
||||
;;; numbers: the C written from them says 224 and 5
|
||||
|
||||
Reference in New Issue
Block a user