boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

049-format.scm (920B)


      1 ; format(template-bv arg ...) -> bv. Substitutes ~a (display), ~s
      2 ; (write), ~d (decimal), ~~ (literal '~'), ~% (newline).
      3 ;
      4 ; Templates are bytevectors; we build them from byte lists since the
      5 ; reader doesn't have string literals yet.
      6 (define (bv-from bs)
      7   (let ((bv (make-bytevector (length bs) 0)))
      8     (let loop ((i 0) (xs bs))
      9       (if (null? xs) bv
     10           (begin (bytevector-u8-set! bv i (car xs))
     11                  (loop (+ i 1) (cdr xs)))))))
     12 
     13 ; "n=~a~%"  ->  "n=42\n"
     14 (display (format (bv-from '(110 61 126 97 126 37)) 42))
     15 
     16 ; "list=~s~%"  ->  "list=(1 2 3)\n"
     17 (display (format (bv-from '(108 105 115 116 61 126 115 126 37)) '(1 2 3)))
     18 
     19 ; "tilde=~~~%"  ->  "tilde=~\n"
     20 (display (format (bv-from '(116 105 108 100 101 61 126 126 126 37))))
     21 
     22 ; "~d times ~d = ~d~%"  ->  "3 times 4 = 12\n"
     23 (display (format (bv-from '(126 100 32 116 105 109 101 115 32 126 100 32 61 32 126 100 126 37)) 3 4 12))
     24 
     25 (sys-exit 0)