group a number's digits with _
C23 groups them with ', which in Sex is quote, so 1'000 read as 1 and (quote 0); _ between two digits is dropped instead, and anywhere else stays part of a name.
This commit is contained in:
62
reader.scm
62
reader.scm
@@ -20,6 +20,7 @@
|
||||
(chicken module)
|
||||
(chicken pathname)
|
||||
(chicken platform) ; software-version, machine-type
|
||||
(only (chicken string) substring-index)
|
||||
(only srfi-1 every any) ; srfi-1 also has an append-reverse
|
||||
utils)
|
||||
|
||||
@@ -192,13 +193,26 @@
|
||||
(begin (get-ch port) (loop (cons c chars)))))))
|
||||
|
||||
(define (finish-atom s)
|
||||
(cond
|
||||
((string=? s ".") dot-token)
|
||||
((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))))
|
||||
(let ((digits (if (substring-index "_" s) (or (ungrouped s (or (c-radix s) 10)) s) s)))
|
||||
(cond
|
||||
((string=? s ".") dot-token)
|
||||
((or (string->number digits) (radix-number digits))
|
||||
=> (lambda (n) (c-number n s)))
|
||||
((suffixed-number digits s) => identity)
|
||||
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||
(else (string->symbol s)))))
|
||||
|
||||
;;; 1_000_000, 0xFF_FF: `_' between two digits groups them, the way
|
||||
;;; C23's ' does -- which in Sex is quote. S without them, or #f when a
|
||||
;;; `_' stands anywhere else, where it is part of a name
|
||||
(define (ungrouped s radix)
|
||||
(let ((digit? (lambda (c) (string->number (string c) radix))))
|
||||
(let loop ((cs (string->list s)) (previous #f) (acc (list)))
|
||||
(cond ((null? cs) (list->string (reverse acc)))
|
||||
((char=? (car cs) #\_)
|
||||
(and previous (digit? previous) (pair? (cdr cs)) (digit? (cadr cs))
|
||||
(loop (cdr cs) (car cs) acc)))
|
||||
(else (loop (cdr cs) (car cs) (cons (car cs) acc)))))))
|
||||
|
||||
;;; 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
|
||||
@@ -246,9 +260,9 @@
|
||||
(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))
|
||||
(define (suffixed-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
|
||||
(literal (and (> (string-length body) 0)
|
||||
(or (char-numeric? (string-ref body 0))
|
||||
@@ -288,20 +302,24 @@
|
||||
;;; 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)
|
||||
(let* ((negative? (and (> (string-length s) 0) (char=? #\- (string-ref s 0))))
|
||||
(digits (if negative? (substring s 1) s)))
|
||||
(and (> (string-length digits) 2)
|
||||
(char=? #\0 (string-ref digits 0))
|
||||
(let ((radix (case (string-ref digits 1)
|
||||
((#\x #\X) 16)
|
||||
((#\b #\B) 2)
|
||||
(else #f)))
|
||||
(body (substring digits 2)))
|
||||
(and radix
|
||||
(not (memv (string-ref body 0) '(#\+ #\-)))
|
||||
(let ((radix (c-radix s)))
|
||||
(and radix
|
||||
(let* ((negative? (char=? #\- (string-ref s 0)))
|
||||
(body (substring s (if negative? 3 2))))
|
||||
(and (not (memv (string-ref body 0) '(#\+ #\-)))
|
||||
(let ((n (string->number body radix)))
|
||||
(and (exact-integer? n) (if negative? (- n) n))))))))
|
||||
|
||||
;;; 16 for 0xE0 and 2 for 0b101, a minus aside; #f for any other spelling
|
||||
(define (c-radix s)
|
||||
(let ((i (if (and (> (string-length s) 0) (char=? #\- (string-ref s 0))) 1 0)))
|
||||
(and (> (string-length s) (+ i 2))
|
||||
(char=? #\0 (string-ref s i))
|
||||
(case (string-ref s (+ i 1))
|
||||
((#\x #\X) 16)
|
||||
((#\b #\B) 2)
|
||||
(else #f)))))
|
||||
|
||||
;;; -INFINITY is (- INFINITY), as -1 is minus one. No name begins with
|
||||
;;; a dash, and an operator that does goes on with a non-letter: -> --
|
||||
(define (negated-name? s)
|
||||
@@ -379,7 +397,7 @@
|
||||
;;; #x80, #b101, #o10
|
||||
(define (read-radix port radix)
|
||||
(let* ((digits (read-atom-string port))
|
||||
(n (string->number digits radix)))
|
||||
(n (string->number (or (ungrouped digits radix) digits) radix)))
|
||||
(if (exact-integer? n)
|
||||
n
|
||||
(error "Malformed number" digits))))
|
||||
|
||||
@@ -827,6 +827,8 @@ whitespace aside, THEN?"
|
||||
(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 "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"
|
||||
(emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;"))
|
||||
(test-assert "a number too wide for long long is unsigned, and says so"
|
||||
|
||||
@@ -61,6 +61,12 @@
|
||||
;; ...and only a number: a prefix with nothing after it is a name
|
||||
(reader-test '(0x 0xg) "0x 0xg")
|
||||
|
||||
;; `_' between two digits groups them, the way C23's ' does, which in
|
||||
;; Sex is quote. Anywhere else it is part of a name
|
||||
(reader-test '(1000000 65535 165 1000.5 -1000 100000.0 32768)
|
||||
"1_000_000 0xFF_FF 0b1010_0101 1_000.5 -1_000 1e0_5 #x80_00")
|
||||
(reader-test '(_1 1_ 1__0 0x_FF 0xF_p) "_1 1_ 1__0 0x_FF 0xF_p")
|
||||
|
||||
;; Scheme reads numbers C has no literal for, and they reached C as
|
||||
;; written: 1/2 is integer division there, and +inf.0 no number at all
|
||||
(test "a ratio is not a C number"
|
||||
|
||||
Reference in New Issue
Block a user