Email list hosting service & mailing list manager


Reference implementation Lassi Kortela 24 Apr 2019 13:40 UTC

Are there canonical implementations of the Scheme reader algorithm for
some of the reports (R5RS, R6RS, R7RS)?

I don't know what to post as the reference implementation of underscores
in the SRFI.

The code below would constitute the core of it -- a procedure to read a
group of digits, possibly with underscores in between. This kind of
subroutine would be called by the main number reader whenever it comes
across a "one or more digits" part of the number.

The below code is missing case folding of hex digits but that's simple
enough.

----------------------------------------------------------------------

(define digits "0123456789abcdef")

(define (read-char-limit? chars limit)
   (let ((ch (peek-char)))
     (let loop ((i 0))
       (cond ((>= i limit) #f)
             ((eqv? ch (string-ref chars i)) (read-char) i)
             (else (loop (+ i 1)))))))

(define (read-char? chars)
   (read-char-limit? chars (string-length chars)))

(define (read-digits? radix)
   (let loop ((was-digit? #f) (value #f))
     (if (read-char? "_")
         (cond (was-digit? (loop #f value))
               (value (error "More than one consecutive underscore"))
               (else (error "Underscore before digits")))
         (let ((digit (read-char-limit? digits radix)))
           (cond (digit (loop #t (+ digit (* radix (or value 0)))))
                 ((and value (not was-digit?)) (error "Underscore after
digits"))
                 (else value))))))

----------------------------------------------------------------------