forked from alex-eg/sex
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
@@ -195,7 +195,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)))))
|
||||
|
||||
@@ -229,8 +229,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
|
||||
@@ -257,7 +257,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
|
||||
@@ -265,7 +268,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)
|
||||
@@ -279,22 +283,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
|
||||
|
||||
@@ -1220,12 +1220,14 @@
|
||||
;;; 1 -> int 1u -> (unsigned int)
|
||||
;;; 4000000000 -> (long long) 10L -> long
|
||||
;;; 1.5f -> float 0.1L -> (long double)
|
||||
;;; 0x1.8p3 -> double
|
||||
(define (c-literal-type literal)
|
||||
(let ((n (c-literal-value literal))
|
||||
(suffix (map char-downcase (string->list (c-literal-suffix literal)))))
|
||||
(cond ((exact? n) (integer-literal-type n suffix))
|
||||
((memv #\f suffix) 'float)
|
||||
(else '(long double)))))
|
||||
((memv #\l suffix) '(long double))
|
||||
(else 'double))))
|
||||
|
||||
(define (integer-literal-type n suffix)
|
||||
(let ((ranks (drop (if (memv #\u suffix)
|
||||
|
||||
@@ -769,6 +769,10 @@
|
||||
(test-assert "a float keeps its digits, which a double would round"
|
||||
(emits? (in-fn "(var x (long double) 3.14159265358979323846264338327950288L)")
|
||||
"x = 3.14159265358979323846264338327950288L;"))
|
||||
(test-assert "a hex float is written as it was"
|
||||
(emits? (in-fn "(var x double 0x1.8p3)") "x = 0x1.8p3;"))
|
||||
(test-assert "suffix and all"
|
||||
(emits? (in-fn "(var x float -0x1p-2f)") "x = -0x1p-2f;"))
|
||||
(test-assert "grouped digits reach C ungrouped"
|
||||
(emits? (in-fn "(var x float 1_000.5f)") "x = 1000.5f;"))
|
||||
(test-assert "a minus is C's own"
|
||||
|
||||
@@ -76,6 +76,10 @@
|
||||
(test "nor a float" "C has no literal for this number" (read-error "1e39f"))
|
||||
(test "but a long double may be wider than a double"
|
||||
#f (read-error "1e400L"))
|
||||
(test "a hex float is held to the same ranges"
|
||||
"C has no literal for this number" (read-error "0x1p1024"))
|
||||
(test "a float's too" "C has no literal for this number" (read-error "0x1p128f"))
|
||||
(test "and a long double's" #f (read-error "0x1p1024L"))
|
||||
(test "nor an infinity" "C has no literal for this number" (read-error "-inf.0"))
|
||||
(test "nor a NaN" "C has no literal for this number" (read-error "+nan.0"))
|
||||
(test "nor a complex number" "C has no literal for this number" (read-error "1+2i"))
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
"widths: 1 1 1"
|
||||
"a float stays a float: 1"
|
||||
"radix with suffix: 255 5"
|
||||
"by magnitude: 4000000000 18446744073709551615")
|
||||
"by magnitude: 4000000000 18446744073709551615"
|
||||
"hex floats: 12 0.25 1")
|
||||
(return 0)
|
||||
|
||||
;;; A number is written the way C writes it, suffix included, and `_'
|
||||
@@ -36,4 +37,9 @@
|
||||
(var big _ 4000000000)
|
||||
(var mask _ 0xFFFFFFFFFFFFFFFF)
|
||||
(printf "by magnitude: %lld %llu\n" big mask)
|
||||
|
||||
;; a double's own digits, which no decimal has to round to
|
||||
(var twelve _ 0x1.8p3)
|
||||
(var quarter _ 0x1p-2f)
|
||||
(printf "hex floats: %g %g %d\n" twelve quarter (== (sizeof quarter) (sizeof float)))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user