1
0
forked from alex-eg/sex

refuse an integer with a leading 0

017 is 17 to Scheme and 15 to C, so whoever reads the Sex and whoever
reads the C would each take it for something else.
This commit is contained in:
2026-10-19 14:48:43 +03:00
parent 8d6d1855e2
commit 9505b4f0a0
2 changed files with 15 additions and 0 deletions

View File

@@ -195,6 +195,12 @@
(define (finish-atom s)
(let ((digits (if (substring-index "_" s) (or (ungrouped s (or (c-radix s) 10)) s) s)))
;; 017 is 17 to Scheme and 15 to C: whoever reads the Sex and whoever
;; reads the C would each take it for something else
(when (irregex-match "-?0[0-9]+[uUlL]*" digits)
(error
"a leading 0 is octal to C; write the number without it, or #o for octal"
s))
(cond
((string=? s ".") dot-token)
((or (string->number digits) (radix-number digits))

View File

@@ -74,6 +74,15 @@
"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")
;; 017 is 17 to Scheme and 15 to C, so it means one thing to whoever
;; reads the Sex and another to whoever reads the C
(test "a leading 0 is refused"
"a leading 0 is octal to C; write the number without it, or #o for octal"
(read-error "017"))
(test "with a suffix as well" #t (string? (read-error "017u")))
(test "or grouped" #t (string? (read-error "0_17")))
(reader-test '(0 0.5 15) "0 00.5 #o17")
;; 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"