write number literals the way C reads them
A C suffix was no number to string->number, so 1u and 1.5f became symbols, and C got them inside the |pipes| CHICKEN writes those with. They read now into a literal that prints its C spelling and is typed by its suffix: an integer in decimal, so 0b101u needs no C23, and a float in the digits it was written with, since through a double 0.1f could round to another float and 0.1L would lose what it has over one. Scheme also reads numbers C has none of. 1/2 reached C as integer division and +inf.0 as no C at all; those, and integers past 64 bits, are read errors now. An integer is typed by size as well, so 4000000000 is no longer an int, and one past long long gets the u C would otherwise only warn about.
This commit is contained in:
97
reader.scm
97
reader.scm
@@ -16,6 +16,7 @@
|
||||
scheme
|
||||
(scheme base) ; make-parameter
|
||||
(chicken base)
|
||||
(chicken irregex)
|
||||
(chicken module)
|
||||
(chicken pathname)
|
||||
(chicken platform) ; software-version, machine-type
|
||||
@@ -27,7 +28,11 @@
|
||||
read-raw-forms
|
||||
|
||||
current-features
|
||||
platform-features)
|
||||
platform-features
|
||||
|
||||
c-literal?
|
||||
c-literal-value
|
||||
c-literal-suffix)
|
||||
|
||||
;;; Sentinels for structural tokens
|
||||
(define close-paren (list '%close-paren))
|
||||
@@ -186,11 +191,97 @@
|
||||
(define (finish-atom s)
|
||||
(cond
|
||||
((string=? s ".") dot-token)
|
||||
((string->number s) => identity)
|
||||
((radix-number s) => identity)
|
||||
((or (string->number s) (radix-number s))
|
||||
=> (lambda (n) (c-number n s)))
|
||||
((suffixed-number s) => identity)
|
||||
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||
(else (string->symbol s))))
|
||||
|
||||
;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400,
|
||||
;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide
|
||||
;;; for long long is unsigned to C, which says so only by a warning
|
||||
;;; unless it is written with a `u'; so it is.
|
||||
(define +llong-min+ (- (expt 2 63)))
|
||||
(define +llong-max+ (- (expt 2 63) 1))
|
||||
(define +ullong-max+ (- (expt 2 64) 1))
|
||||
|
||||
(define (c-number n s)
|
||||
(cond ((exact-integer? n)
|
||||
(cond ((> (abs n) +ullong-max+)
|
||||
(error "too large for any C integer type" s))
|
||||
((> n +llong-max+) (make-c-literal n "u" #f))
|
||||
((< n +llong-min+) (list '- (make-c-literal (- n) "u" #f)))
|
||||
(else n)))
|
||||
((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.
|
||||
;;;
|
||||
;;; 0b101u -> 5u in decimal, so C23's 0b never reaches C11
|
||||
;;; 0.1f -> 0.1f in the digits written: through a double, 0.1f
|
||||
;;; could round to another float, and 0.1L would
|
||||
;;; lose all it has over one
|
||||
;;; -1u -> (- 1u) which is what C reads it as too
|
||||
(define-record-type <c-literal>
|
||||
(make-c-literal value suffix digits)
|
||||
c-literal?
|
||||
(value c-literal-value)
|
||||
(suffix c-literal-suffix)
|
||||
(digits c-literal-digits))
|
||||
|
||||
(set-record-printer! <c-literal>
|
||||
(lambda (literal port)
|
||||
(display (or (c-literal-digits literal)
|
||||
(number->string (c-literal-value literal)))
|
||||
port)
|
||||
(display (c-literal-suffix literal) port)))
|
||||
|
||||
(define suffixed-integer
|
||||
(irregex "(0[xX][0-9a-fA-F]+|0[bB][01]+|[0-9]+)([uU](?:ll|LL|l|L)?|(?:ll|LL|l|L)[uU]?)"))
|
||||
|
||||
(define suffixed-float
|
||||
(irregex "((?:[0-9]+\\.[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)([fFlL])"))
|
||||
|
||||
(define (suffixed-number s)
|
||||
(let* ((negative? (and (> (string-length s) 1) (char=? #\- (string-ref s 0))))
|
||||
(body (if negative? (substring s 1) s))
|
||||
;; each spelling begins with a digit or a point, and few names do
|
||||
(literal (and (> (string-length body) 0)
|
||||
(or (char-numeric? (string-ref body 0))
|
||||
(char=? #\. (string-ref body 0)))
|
||||
(or (suffixed-integer-literal body s)
|
||||
(suffixed-float-literal body s)))))
|
||||
(and literal (if negative? (list '- literal) literal))))
|
||||
|
||||
(define (suffixed-integer-literal body s)
|
||||
(let ((m (irregex-match suffixed-integer body)))
|
||||
(and m
|
||||
(let* ((digits (irregex-match-substring m 1))
|
||||
(suffix (irregex-match-substring m 2))
|
||||
(n (or (radix-number digits) (string->number digits)))
|
||||
(widest (if (irregex-search "[uU]" suffix) +ullong-max+ +llong-max+)))
|
||||
(when (> n widest)
|
||||
(error "too large for its suffix" s))
|
||||
(make-c-literal n suffix #f)))))
|
||||
|
||||
(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)))))
|
||||
|
||||
;;; 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)))
|
||||
|
||||
;;; 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
|
||||
(define (radix-number s)
|
||||
|
||||
Reference in New Issue
Block a user