From f3d9c95daf541ac04e9be3f9713d82b009a966c2 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 16:06:14 +0300 Subject: [PATCH] read hex and binary literals 0x80 was not a number to string->number, so it became a symbol the printer escaped as |0x80|, and #x was rejected outright. Both spellings now read as the integer, which the C writes in decimal. Fixes #47. --- reader.scm | 33 ++++++++++++++++++++++++++++++-- tests/reader.scm | 7 +++++++ tests/sex-programs/operators.sex | 7 ++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/reader.scm b/reader.scm index d1dd55f..42ec90b 100644 --- a/reader.scm +++ b/reader.scm @@ -179,9 +179,27 @@ (cond ((string=? s ".") dot-token) ((string->number s) => identity) + ((radix-number s) => identity) ((negated-name? s) (list '- (string->symbol (substring s 1)))) (else (string->symbol s)))) +;;; 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 ((n (string->number body radix))) + (and (exact-integer? n) (if negative? (- n) n)))))))) + ;;; -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) @@ -190,8 +208,8 @@ (let ((c (string-ref s 1))) (or (char-alphabetic? c) (char=? c #\_))))) -;;; #-dispatch: booleans, characters, vectors, block/datum comments, -;;; feature expressions +;;; #-dispatch: booleans, characters, vectors, numbers in a radix, +;;; block/datum comments, feature expressions (define (read-hash port) (let ((c (get-ch port))) (cond @@ -200,6 +218,9 @@ ((or (char=? c #\f) (char=? c #\F)) (read-bool port #f)) ((char=? c #\\) (read-char-lit port)) ((char=? c #\() (list->vector (read-list port close-paren))) + ((memv c '(#\x #\X)) (read-radix port 16)) + ((memv c '(#\b #\B)) (read-radix port 2)) + ((memv c '(#\o #\O)) (read-radix port 8)) ((char=? c #\|) (skip-block-comment port 1) (next-token port)) ((char=? c #\;) (read-datum port) (next-token port)) ; datum comment ((char=? c #\+) (read-conditional port #t)) @@ -253,6 +274,14 @@ ((datum-token? guarded) (next-token port)) (else guarded)))) +;;; #x80, #b101, #o10 +(define (read-radix port radix) + (let* ((digits (read-atom-string port)) + (n (string->number digits radix))) + (if (exact-integer? n) + n + (error "Malformed number" digits)))) + ;;; #t / #true / #f / #false. `val' is the boolean; consume any ;;; trailing name characters and validate (define (read-bool port val) diff --git a/tests/reader.scm b/tests/reader.scm index 23627f7..a77ca0a 100644 --- a/tests/reader.scm +++ b/tests/reader.scm @@ -47,6 +47,13 @@ (reader-test '(-1 -1.5) "-1 -1.5") (reader-test '(- -> ->> -- -=) "- -> ->> -- -=") + ;; a number in another radix, spelled the way C does or the way + ;; Scheme does; either is an ordinary integer from here on + (reader-test '(224 224 5 5 -16) "0xE0 0Xe0 0b101 0B101 -0x10") + (reader-test '(128 128 5 8 -16) "#x80 #X80 #b101 #o10 #x-10") + ;; ...and only a number: a prefix with nothing after it is a name + (reader-test '(0x 0xg) "0x 0xg") + ;; `;' comments are preserved as (comment "...") forms (reader-test '((comment " hi")) "; hi") (reader-test '((comment ";; Prototypes")) ";;; Prototypes") diff --git a/tests/sex-programs/operators.sex b/tests/sex-programs/operators.sex index bd730bc..b708b83 100644 --- a/tests/sex-programs/operators.sex +++ b/tests/sex-programs/operators.sex @@ -7,7 +7,8 @@ "unsigned wins either way: 4294967295 4294967295" "toplevel: 1 2.5 hi 12" "closure in place: 5" - "a type is not a call: 7") + "a type is not a call: 7" + "radix literals: 224 5 128") (return 0) ;;; The walk types an expression by its head, and the heads it had a @@ -87,4 +88,8 @@ (var f (closure ((int)) int) (make-adder 1)) (var fp (fn ((f int) (g int)) int) area) (printf "a type is not a call: %d\n" (f 6)) + + ;; a mask reads best in the radix it is a mask in + (var lead int 0xE5) + (printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80) (return 0))