read-print: Improve declaration of integer bases.

This is a followup to c3b1cfe76b7038f4030d7d207ffc417fed9a7ead.

* guix/read-print.scm (%symbols-followed-by-octal-integers)
(%symbols-followed-by-hexadecimal-integers): Remove.
* guix/read-print.scm (%integer-forms): New variable.
(integer->string)[form-base, octal?]: New procedures.
Rewrite accordingly.
This commit is contained in:
Ludovic Courtès 2022-09-02 10:19:48 +02:00
parent 82968362ea
commit aaf7820d57
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5

@ -442,26 +442,40 @@ each line except the first one (they're assumed to be already there)."
(display (make-string indent #\space) port)
(loop tail)))))
(define %symbols-followed-by-octal-integers
;; Symbols for which the following integer must be printed as octal.
'(chmod umask mkdir mkstemp))
(define %symbols-followed-by-hexadecimal-integers
;; Likewise, for hexadecimal integers.
'(logand logior logxor lognot))
(define %integer-forms
;; Forms that take an integer as their argument, where said integer should
;; be printed in base other than decimal base.
(letrec-syntax ((vhashq (syntax-rules ()
((_) vlist-null)
((_ (key value) rest ...)
(vhash-consq key value (vhashq rest ...))))))
(vhashq
('chmod 8)
('umask 8)
('mkdir 8)
('mkstemp 8)
('logand 16)
('logior 16)
('logxor 16)
('lognot 16))))
(define (integer->string integer context)
"Render INTEGER as a string using a base suitable based on CONTEXT."
(define (form-base form)
(match (vhash-assq form %integer-forms)
(#f 10)
((_ . base) base)))
(define (octal? form)
(= 8 (form-base form)))
(define base
(match context
((head . tail)
(cond ((memq head %symbols-followed-by-octal-integers) 8)
((memq head %symbols-followed-by-hexadecimal-integers)
(if (any (cut memq <> %symbols-followed-by-octal-integers)
tail)
8
16))
(else 10)))
(match (form-base head)
(8 8)
(16 (if (any octal? tail) 8 16))
(10 10)))
(_ 10)))
(string-append (match base