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:
2026-10-16 14:42:20 +03:00
parent 92f676d4b5
commit 7159eeb5d2
3 changed files with 48 additions and 22 deletions

View File

@@ -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)
@@ -189,13 +190,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
@@ -243,9 +257,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))
@@ -285,20 +299,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)
@@ -376,7 +394,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))))