2006-11-23 19:33:45 -05:00
|
|
|
|
2007-04-29 23:00:20 -04:00
|
|
|
(library (ikarus writer)
|
2007-10-23 23:55:57 -04:00
|
|
|
(export write display format printf print-error print-unicode print-graph)
|
2007-05-05 20:40:23 -04:00
|
|
|
(import
|
2007-10-09 09:22:02 -04:00
|
|
|
(rnrs hashtables)
|
2007-05-06 17:47:36 -04:00
|
|
|
(ikarus system $chars)
|
|
|
|
(ikarus system $strings)
|
2007-05-19 22:36:52 -04:00
|
|
|
(ikarus system $vectors)
|
2007-05-06 17:47:36 -04:00
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $pairs)
|
2007-05-06 18:52:19 -04:00
|
|
|
(ikarus system $symbols)
|
2007-05-15 23:57:35 -04:00
|
|
|
(ikarus system $bytevectors)
|
2007-10-11 22:55:20 -04:00
|
|
|
(ikarus system $transcoders)
|
2007-06-17 10:20:19 -04:00
|
|
|
(only (ikarus unicode-data) unicode-printable-char?)
|
2007-10-23 23:55:57 -04:00
|
|
|
(except (ikarus) write display format printf print-error print-unicode print-graph))
|
2007-05-19 22:36:52 -04:00
|
|
|
|
2007-06-17 19:49:40 -04:00
|
|
|
(define print-unicode
|
|
|
|
(make-parameter #t))
|
2007-05-19 15:18:08 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define char-table ; first nonprintable chars
|
2007-05-19 15:18:08 -04:00
|
|
|
'#("nul" "x1" "x2" "x3" "x4" "x5" "x6" "alarm"
|
|
|
|
"backspace" "tab" "linefeed" "vtab" "page" "return" "xE" "xF"
|
|
|
|
"x10" "x11" "x12" "x13" "x14" "x15" "x16" "x17"
|
|
|
|
"x18" "x19" "x1A" "esc" "x1C" "x1D" "x1E" "x1F"
|
|
|
|
"space"))
|
|
|
|
|
|
|
|
(define write-positive-hex-fx
|
|
|
|
(lambda (n p)
|
|
|
|
(unless ($fx= n 0)
|
|
|
|
(write-positive-hex-fx ($fxsra n 4) p)
|
|
|
|
(let ([n ($fxlogand n #xF)])
|
|
|
|
(cond
|
|
|
|
[($fx<= n 9)
|
|
|
|
(write-char ($fixnum->char
|
|
|
|
($fx+ ($char->fixnum #\0) n))
|
|
|
|
p)]
|
|
|
|
[else
|
|
|
|
(write-char ($fixnum->char
|
|
|
|
($fx+ ($char->fixnum #\A) ($fx- n 10)))
|
|
|
|
p)])))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-character
|
|
|
|
(lambda (x p m)
|
|
|
|
(if m
|
|
|
|
(let ([i ($char->fixnum x)])
|
|
|
|
(write-char #\# p)
|
|
|
|
(cond
|
|
|
|
[(fx< i (vector-length char-table))
|
2006-11-23 19:40:06 -05:00
|
|
|
(write-char #\\ p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char* (vector-ref char-table i) p)]
|
|
|
|
[(fx< i 127)
|
2006-11-23 19:40:06 -05:00
|
|
|
(write-char #\\ p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char x p)]
|
|
|
|
[(fx= i 127)
|
2006-11-23 19:40:06 -05:00
|
|
|
(write-char #\\ p)
|
2007-05-19 15:18:08 -04:00
|
|
|
(write-char* "delete" p)]
|
2007-06-17 19:49:40 -04:00
|
|
|
[(and (print-unicode) (unicode-printable-char? x))
|
2007-05-19 22:36:52 -04:00
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char x p)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2007-01-20 16:52:22 -05:00
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char #\x p)
|
2007-05-19 15:18:08 -04:00
|
|
|
(write-positive-hex-fx i p)]))
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char x p))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-list
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x p m h i)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-11-23 19:48:14 -05:00
|
|
|
[(and (pair? x)
|
2007-10-09 09:22:02 -04:00
|
|
|
(or (not (hashtable-ref h x #f))
|
|
|
|
(fxzero? (hashtable-ref h x 0))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\space p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-list (cdr x) p m h
|
|
|
|
(writer (car x) p m h i))]
|
|
|
|
[(null? x) i]
|
|
|
|
[else
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\space p)
|
|
|
|
(write-char #\. p)
|
|
|
|
(write-char #\space p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(writer x p m h i)])))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-vector
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x p m h i)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\# p)
|
|
|
|
(write-char #\( p)
|
|
|
|
(let ([n (vector-length x)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([i
|
|
|
|
(cond
|
|
|
|
[(fx> n 0)
|
|
|
|
(let f ([idx 1] [i (writer (vector-ref x 0) p m h i)])
|
|
|
|
(cond
|
|
|
|
[(fx= idx n)
|
|
|
|
i]
|
|
|
|
[else
|
|
|
|
(write-char #\space p)
|
|
|
|
(f (fxadd1 idx)
|
|
|
|
(writer (vector-ref x idx) p m h i))]))]
|
|
|
|
[else i])])
|
|
|
|
(write-char #\) p)
|
|
|
|
i))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-05-15 23:57:35 -04:00
|
|
|
(define write-bytevector
|
|
|
|
(lambda (x p m h i)
|
|
|
|
(write-char #\# p)
|
|
|
|
(write-char #\v p)
|
|
|
|
(write-char #\u p)
|
|
|
|
(write-char #\8 p)
|
|
|
|
(write-char #\( p)
|
|
|
|
(let ([n ($bytevector-length x)])
|
2007-05-19 22:09:30 -04:00
|
|
|
(when (fx> n 0)
|
|
|
|
(write-fixnum ($bytevector-u8-ref x 0) p)
|
|
|
|
(let f ([idx 1] [n n] [x x] [p p])
|
|
|
|
(unless ($fx= idx n)
|
|
|
|
(write-char #\space p)
|
|
|
|
(write-fixnum ($bytevector-u8-ref x idx) p)
|
|
|
|
(f (fxadd1 idx) n x p)))))
|
|
|
|
(write-char #\) p)
|
|
|
|
i))
|
2007-05-15 23:57:35 -04:00
|
|
|
|
2007-10-12 02:59:27 -04:00
|
|
|
(define write-struct
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x p m h i)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\# p)
|
|
|
|
(write-char #\[ p)
|
2007-10-12 02:59:27 -04:00
|
|
|
(let ([i (writer (struct-name x) p m h i)])
|
|
|
|
(let ([n (struct-length x)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(let f ([idx 0] [i i])
|
|
|
|
(cond
|
|
|
|
[(fx= idx n)
|
|
|
|
(write-char #\] p)
|
|
|
|
i]
|
|
|
|
[else
|
|
|
|
(write-char #\space p)
|
|
|
|
(f (fxadd1 idx)
|
2007-10-12 02:59:27 -04:00
|
|
|
(writer (struct-ref x idx) p m h i))]))))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define initial?
|
|
|
|
(lambda (c)
|
|
|
|
(or (letter? c) (special-initial? c))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define letter?
|
|
|
|
(lambda (c)
|
|
|
|
(or (and ($char<= #\a c) ($char<= c #\z))
|
|
|
|
(and ($char<= #\A c) ($char<= c #\Z)))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define digit?
|
|
|
|
(lambda (c)
|
|
|
|
(and ($char<= #\0 c) ($char<= c #\9))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define special-initial?
|
|
|
|
(lambda (x)
|
|
|
|
(memq x '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define subsequent?
|
|
|
|
(lambda (x)
|
|
|
|
(or (initial? x)
|
|
|
|
(digit? x)
|
|
|
|
(special-subsequent? x))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define special-subsequent?
|
|
|
|
(lambda (x)
|
|
|
|
(memq x '(#\+ #\- #\. #\@))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define subsequent*?
|
|
|
|
(lambda (str i n)
|
|
|
|
(or ($fx= i n)
|
|
|
|
(and (subsequent? ($string-ref str i))
|
|
|
|
(subsequent*? str ($fxadd1 i) n)))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-05-19 17:28:03 -04:00
|
|
|
(define peculiar-symbol-string?
|
|
|
|
(lambda (str)
|
|
|
|
(let ([n (string-length str)])
|
|
|
|
(cond
|
|
|
|
[(fx= n 1)
|
|
|
|
(memq (string-ref str 0) '(#\+ #\-))]
|
|
|
|
[(fx>= n 2)
|
|
|
|
(or (and (char=? (string-ref str 0) #\-)
|
|
|
|
(char=? (string-ref str 1) #\>)
|
|
|
|
(subsequent*? str 2 n))
|
|
|
|
(string=? str "..."))]
|
|
|
|
[else #f]))))
|
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define valid-symbol-string?
|
|
|
|
(lambda (str)
|
2007-01-26 09:05:07 -05:00
|
|
|
(define normal-symbol-string?
|
|
|
|
(lambda (str)
|
|
|
|
(let ([n ($string-length str)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(and ($fx>= n 1)
|
|
|
|
(initial? ($string-ref str 0))
|
2007-01-26 09:05:07 -05:00
|
|
|
(subsequent*? str 1 n)))))
|
|
|
|
(or (normal-symbol-string? str)
|
|
|
|
(peculiar-symbol-string? str))))
|
2007-04-29 23:00:20 -04:00
|
|
|
|
2007-05-19 17:28:03 -04:00
|
|
|
(define write-symbol-bar-esc-loop
|
2006-11-23 19:33:45 -05:00
|
|
|
(lambda (x i n p)
|
|
|
|
(unless ($fx= i n)
|
2007-05-19 17:28:03 -04:00
|
|
|
(let* ([c ($string-ref x i)]
|
|
|
|
[b ($char->fixnum c)])
|
|
|
|
(cond
|
|
|
|
[($fx< b 32)
|
|
|
|
(cond
|
|
|
|
[($fx< b 7)
|
|
|
|
(write-inline-hex b p)]
|
|
|
|
[($fx< b 14)
|
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char (string-ref "abtnvfr" ($fx- b 7)) p)]
|
|
|
|
[else
|
|
|
|
(write-inline-hex b p)])]
|
|
|
|
[(memq c '(#\\ #\|))
|
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char c p)]
|
|
|
|
[($fx< b 127)
|
|
|
|
(write-char c p)]
|
|
|
|
[else
|
|
|
|
(write-inline-hex b p)]))
|
|
|
|
(write-symbol-bar-esc-loop x ($fxadd1 i) n p))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-05-19 17:28:03 -04:00
|
|
|
(define write-symbol-bar-esc
|
2006-11-23 19:33:45 -05:00
|
|
|
(lambda (x p)
|
|
|
|
(write-char #\| p)
|
2007-05-19 17:28:03 -04:00
|
|
|
(write-symbol-bar-esc-loop x 0 ($string-length x) p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\| p)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-05-19 17:28:03 -04:00
|
|
|
(define-syntax ascii-map
|
|
|
|
(lambda (x)
|
|
|
|
;;; r6rs prohibits bytevectors from being "datum"s
|
|
|
|
;;; oh well.
|
|
|
|
(syntax-case x ()
|
|
|
|
[(stx str) (string? (syntax->datum #'str))
|
|
|
|
(let ([s (syntax->datum #'str)]
|
|
|
|
[bv (make-bytevector 16 0)])
|
|
|
|
(for-each
|
|
|
|
(lambda (c)
|
|
|
|
(let ([b (char->integer c)])
|
|
|
|
(let ([i (fxlogand b 7)]
|
|
|
|
[j (fxsra b 3)])
|
|
|
|
(bytevector-u8-set! bv j
|
|
|
|
(fxlogor (bytevector-u8-ref bv j)
|
|
|
|
(fxsll 1 i))))))
|
|
|
|
(string->list s))
|
|
|
|
(with-syntax ([bv (datum->syntax #'stx bv)])
|
|
|
|
#'(quote bv)))])))
|
|
|
|
|
|
|
|
(define subsequents-map
|
|
|
|
(ascii-map
|
|
|
|
"!$%&*/:<=>?^_~+-.@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
|
|
|
|
(define initials-map
|
|
|
|
(ascii-map
|
|
|
|
"!$%&*/:<=>?^_~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
|
|
|
|
|
|
|
|
|
|
|
|
(define (in-map? byte map)
|
|
|
|
(let ([i ($fxlogand byte 7)]
|
|
|
|
[j ($fxsra byte 3)])
|
|
|
|
(and
|
|
|
|
(fx< j ($bytevector-length map))
|
|
|
|
(let ([mask ($fxsll 1 i)])
|
|
|
|
(not ($fxzero?
|
|
|
|
($fxlogand mask
|
|
|
|
($bytevector-u8-ref map j))))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (write-subsequent* str i j p)
|
|
|
|
(unless ($fx= i j)
|
|
|
|
(let* ([c ($string-ref str i)]
|
|
|
|
[b ($char->fixnum c)])
|
|
|
|
(cond
|
|
|
|
[(in-map? b subsequents-map)
|
|
|
|
(write-char c p)]
|
2007-05-19 22:36:52 -04:00
|
|
|
[($fx< b 128)
|
|
|
|
(write-inline-hex b p)]
|
|
|
|
[(unicode-printable-char? c)
|
|
|
|
(write-char c p)]
|
2007-05-19 17:28:03 -04:00
|
|
|
[else
|
2007-05-19 22:09:30 -04:00
|
|
|
(write-inline-hex b p)]))
|
|
|
|
(write-subsequent* str ($fxadd1 i) j p)))
|
2007-05-19 17:28:03 -04:00
|
|
|
|
|
|
|
(define write-symbol-hex-esc
|
|
|
|
(lambda (str p)
|
|
|
|
(let ([n ($string-length str)])
|
|
|
|
(cond
|
|
|
|
[($fx= n 0)
|
|
|
|
(write-char #\| p)
|
|
|
|
(write-char #\| p)]
|
|
|
|
[else
|
|
|
|
(let* ([c0 ($string-ref str 0)]
|
|
|
|
[b0 ($char->fixnum c0)])
|
|
|
|
(cond
|
|
|
|
[(in-map? b0 initials-map) (write-char c0 p)]
|
2007-05-19 22:36:52 -04:00
|
|
|
[($fx< b0 128) (write-inline-hex b0 p)]
|
|
|
|
[(unicode-printable-char? c0) (write-char c0 p)]
|
2007-05-19 17:28:03 -04:00
|
|
|
[else (write-inline-hex b0 p)])
|
|
|
|
(write-subsequent* str 1 n p))]))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (write-peculiar str p)
|
|
|
|
(let ([n ($string-length str)])
|
|
|
|
(cond
|
|
|
|
[($fx= n 1)
|
|
|
|
(write-char ($string-ref str 0) p)]
|
|
|
|
[(and ($fx>= n 2)
|
|
|
|
($char= ($string-ref str 0) #\-)
|
|
|
|
($char= ($string-ref str 1) #\>))
|
|
|
|
(write-char #\- p)
|
|
|
|
(write-char #\> p)
|
|
|
|
(write-subsequent* str 2 n p)]
|
|
|
|
[(string=? str "...")
|
|
|
|
(write-char #\. p)
|
|
|
|
(write-char #\. p)
|
|
|
|
(write-char #\. p)]
|
|
|
|
[else (error 'write-peculiear "BUG")])))
|
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-symbol
|
|
|
|
(lambda (x p m)
|
2007-05-19 17:28:03 -04:00
|
|
|
(write-symbol-string (symbol->string x) p m)))
|
|
|
|
|
|
|
|
(define write-symbol-string
|
|
|
|
(lambda (str p m)
|
|
|
|
(if m
|
|
|
|
(if (peculiar-symbol-string? str)
|
|
|
|
(write-peculiar str p)
|
|
|
|
(write-symbol-hex-esc str p))
|
|
|
|
(write-char* str p))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-gensym
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x p m h i)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2006-12-25 03:33:03 -05:00
|
|
|
[(and m (print-gensym)) =>
|
|
|
|
(lambda (gensym-how)
|
|
|
|
(case gensym-how
|
|
|
|
[(pretty)
|
|
|
|
(let ([str (symbol->string x)])
|
|
|
|
(write-char #\# p)
|
|
|
|
(write-char #\: p)
|
2007-05-19 17:28:03 -04:00
|
|
|
(write-symbol-string str p m))]
|
2006-12-25 03:33:03 -05:00
|
|
|
[else
|
2007-05-19 17:28:03 -04:00
|
|
|
(let ([str (symbol->string x)]
|
|
|
|
[ustr (gensym->unique-string x)])
|
2006-12-25 03:33:03 -05:00
|
|
|
(write-char #\# p)
|
|
|
|
(write-char #\{ p)
|
2007-05-19 17:28:03 -04:00
|
|
|
(write-symbol-string str p m)
|
2006-12-25 03:33:03 -05:00
|
|
|
(write-char #\space p)
|
2007-05-19 17:28:03 -04:00
|
|
|
(write-symbol-bar-esc ustr p)
|
2006-12-25 03:33:03 -05:00
|
|
|
(write-char #\} p))])
|
|
|
|
i)]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
|
|
|
(write-symbol x p m)
|
|
|
|
i])))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-05-19 17:28:03 -04:00
|
|
|
(define write-inline-hex
|
|
|
|
(lambda (b p)
|
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char #\x p)
|
|
|
|
(if ($fxzero? b)
|
|
|
|
(write-char #\0 p)
|
|
|
|
(write-positive-hex-fx b p))
|
|
|
|
(write-char #\; p)))
|
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-string-escape
|
|
|
|
(lambda (x p)
|
|
|
|
(define loop
|
|
|
|
(lambda (x i n p)
|
|
|
|
(unless (fx= i n)
|
2007-05-19 17:28:03 -04:00
|
|
|
(let* ([c (string-ref x i)]
|
|
|
|
[b ($char->fixnum c)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
2007-05-19 17:28:03 -04:00
|
|
|
[($fx< b 32)
|
|
|
|
(cond
|
|
|
|
[($fx< b 7)
|
|
|
|
(write-inline-hex b p)]
|
|
|
|
[($fx< b 14)
|
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char (string-ref "abtnvfr" ($fx- b 7)) p)]
|
|
|
|
[else
|
|
|
|
(write-inline-hex b p)])]
|
2006-11-23 19:40:06 -05:00
|
|
|
[(or ($char= #\" c) ($char= #\\ c))
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\\ p)
|
|
|
|
(write-char c p)]
|
2007-05-19 17:28:03 -04:00
|
|
|
[($fx< b 127)
|
|
|
|
(write-char c p)]
|
2007-05-19 22:36:52 -04:00
|
|
|
[(unicode-printable-char? c)
|
|
|
|
(write-char c p)]
|
2007-05-19 17:28:03 -04:00
|
|
|
[else (write-inline-hex b p)]))
|
2006-11-23 19:33:45 -05:00
|
|
|
(loop x (fxadd1 i) n p))))
|
|
|
|
(write-char #\" p)
|
|
|
|
(loop x 0 (string-length x) p)
|
|
|
|
(write-char #\" p)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-string
|
|
|
|
(lambda (x p m)
|
|
|
|
(if m
|
|
|
|
(write-string-escape x p)
|
|
|
|
(write-char* x p))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-fixnum
|
|
|
|
(lambda (x p)
|
|
|
|
(define loop
|
|
|
|
(lambda (x p)
|
|
|
|
(unless (fxzero? x)
|
|
|
|
(loop (fxquotient x 10) p)
|
|
|
|
(write-char
|
|
|
|
($fixnum->char
|
|
|
|
($fx+ (fxremainder x 10)
|
|
|
|
($char->fixnum #\0)))
|
|
|
|
p))))
|
|
|
|
(cond
|
|
|
|
[(fxzero? x) (write-char #\0 p)]
|
|
|
|
[(fx< x 0)
|
|
|
|
(write-char #\- p)
|
|
|
|
(if (fx= x -536870912)
|
|
|
|
(write-char* "536870912" p)
|
|
|
|
(loop (fx- 0 x) p))]
|
|
|
|
[else (loop x p)])))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define write-char*
|
|
|
|
(lambda (x p)
|
|
|
|
(define loop
|
|
|
|
(lambda (x i n p)
|
|
|
|
(unless (fx= i n)
|
|
|
|
(write-char (string-ref x i) p)
|
|
|
|
(loop x (fxadd1 i) n p))))
|
|
|
|
(loop x 0 (string-length x) p)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define macro
|
2007-08-30 20:30:21 -04:00
|
|
|
(lambda (x h)
|
2006-11-23 19:33:45 -05:00
|
|
|
(define macro-forms
|
2007-08-30 20:30:21 -04:00
|
|
|
'([quote . "'"]
|
|
|
|
[quasiquote . "`"]
|
|
|
|
[unquote . ","]
|
|
|
|
[unquote-splicing . ",@"]
|
|
|
|
[syntax . "#'"]
|
|
|
|
[quasisyntax . "#`"]
|
|
|
|
[unsyntax . "#,"]
|
|
|
|
[unsyntax-splicing . "#,@"]
|
2006-11-23 19:33:45 -05:00
|
|
|
[|#primitive| . "#%"]))
|
|
|
|
(and (pair? x)
|
|
|
|
(let ([d ($cdr x)])
|
|
|
|
(and (pair? d)
|
2007-08-30 20:30:21 -04:00
|
|
|
(null? ($cdr d))
|
2007-10-09 09:22:02 -04:00
|
|
|
(not (hashtable-ref h x #f))))
|
2006-11-23 19:33:45 -05:00
|
|
|
(assq ($car x) macro-forms))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define write-pair
|
|
|
|
(lambda (x p m h i)
|
2007-08-30 20:30:21 -04:00
|
|
|
(cond
|
|
|
|
[(macro x h) =>
|
|
|
|
(lambda (a)
|
|
|
|
(display (cdr a) p)
|
|
|
|
(writer (cadr x) p m h i))]
|
|
|
|
[else
|
|
|
|
(write-char #\( p)
|
|
|
|
(let ([i (writer (car x) p m h i)])
|
|
|
|
(let ([i (write-list (cdr x) p m h i)])
|
|
|
|
(write-char #\) p)
|
|
|
|
i))])))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define write-ref
|
|
|
|
(lambda (n p)
|
|
|
|
(write-char #\# p)
|
|
|
|
(write-fixnum (fx- -1 n) p)
|
|
|
|
(write-char #\# p)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define write-mark
|
|
|
|
(lambda (n p)
|
|
|
|
(write-char #\# p)
|
|
|
|
(write-fixnum (fx- -1 n) p)
|
|
|
|
(write-char #\= p)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define write-shareable
|
|
|
|
(lambda (x p m h i k)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
|
|
|
(cond
|
|
|
|
[(fx< n 0)
|
|
|
|
(write-ref n p)
|
|
|
|
i]
|
|
|
|
[(fx= n 0)
|
|
|
|
(k x p m h i)]
|
|
|
|
[else
|
|
|
|
(let ([i (fx- i 1)])
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x i)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-mark i p)
|
|
|
|
(k x p m h i))]))]
|
|
|
|
[else (k x p m h i)])))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define writer
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (x p m h i)
|
2006-11-23 19:33:45 -05:00
|
|
|
(cond
|
|
|
|
[(pair? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-shareable x p m h i write-pair)]
|
|
|
|
[(symbol? x)
|
2006-11-23 19:33:45 -05:00
|
|
|
(if (gensym? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-gensym x p m h i)
|
|
|
|
(begin (write-symbol x p m) i))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(fixnum? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-fixnum x p)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(string? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-string x p m)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(boolean? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char* (if x "#t" "#f") p)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(char? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-character x p m)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(procedure? x)
|
2007-09-04 20:38:16 -04:00
|
|
|
(cond
|
|
|
|
[(let ([name (procedure-annotation x)])
|
|
|
|
(and (symbol? name) name)) =>
|
|
|
|
(lambda (name)
|
|
|
|
(write-char* "#<procedure " p)
|
|
|
|
(write-char* (symbol->string name) p)
|
|
|
|
(write-char* ">" p))]
|
|
|
|
[else (write-char* "#<procedure>" p)])
|
2006-11-23 19:48:14 -05:00
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(output-port? x)
|
|
|
|
(write-char* "#<output-port " p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([i (writer (output-port-name x) p #t h i)])
|
|
|
|
(write-char #\> p)
|
|
|
|
i)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(input-port? x)
|
|
|
|
(write-char* "#<input-port " p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(let ([i (writer (input-port-name x) p #t h i)])
|
|
|
|
(write-char #\> p)
|
|
|
|
i)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(vector? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-shareable x p m h i write-vector)]
|
2007-05-15 23:57:35 -04:00
|
|
|
[(bytevector? x)
|
|
|
|
(write-shareable x p m h i write-bytevector)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(null? x)
|
|
|
|
(write-char #\( p)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char #\) p)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eq? x (void))
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char* "#<void>" p)
|
|
|
|
i]
|
2006-11-23 19:33:45 -05:00
|
|
|
[(eof-object? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char* "#!eof" p)
|
|
|
|
i]
|
|
|
|
[(bwp-object? x)
|
|
|
|
(write-char* "#!bwp" p)
|
|
|
|
i]
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable? x)
|
|
|
|
(write-char* "#<hashtable>" p)
|
2006-12-04 09:57:30 -05:00
|
|
|
i]
|
2007-10-12 02:59:27 -04:00
|
|
|
[(struct? x)
|
|
|
|
(let ([printer (struct-printer x)])
|
2006-11-23 19:33:45 -05:00
|
|
|
(if (procedure? printer)
|
2006-11-23 19:48:14 -05:00
|
|
|
(begin (printer x p) i)
|
2007-10-12 02:59:27 -04:00
|
|
|
(write-shareable x p m h i write-struct)))]
|
2006-12-04 09:54:28 -05:00
|
|
|
[(code? x)
|
|
|
|
(write-char* "#<code>" p)]
|
2006-11-23 19:40:06 -05:00
|
|
|
[($unbound-object? x)
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char* "#<unbound-object>" p)
|
|
|
|
i]
|
2007-05-06 18:52:19 -04:00
|
|
|
;;; [($forward-ptr? x) FIXME reinstate
|
|
|
|
;;; (write-char* "#<forward-ptr>" p)
|
|
|
|
;;; i]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(number? x)
|
|
|
|
(write-char* (number->string x) p)
|
|
|
|
i]
|
2007-10-11 22:55:20 -04:00
|
|
|
[($transcoder? x)
|
|
|
|
(write-char* "#<transcoder " p)
|
|
|
|
(let ([n ($transcoder->data x)])
|
|
|
|
(write-char* (number->string n) p))
|
|
|
|
(write-char* ">" p)]
|
2006-11-23 19:33:45 -05:00
|
|
|
[else
|
2006-11-23 19:48:14 -05:00
|
|
|
(write-char* "#<unknown>" p)
|
|
|
|
i])))
|
2007-04-29 23:00:20 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define print-graph (make-parameter #f))
|
2007-04-29 23:00:20 -04:00
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
(define (hasher x h)
|
|
|
|
(define (vec-graph x i j h)
|
|
|
|
(unless (fx= i j)
|
|
|
|
(graph (vector-ref x i) h)
|
|
|
|
(vec-graph x (fxadd1 i) j h)))
|
|
|
|
(define (vec-dynamic x i j h)
|
|
|
|
(unless (fx= i j)
|
|
|
|
(dynamic (vector-ref x i) h)
|
|
|
|
(vec-dynamic x (fxadd1 i) j h)))
|
|
|
|
(define (graph x h)
|
|
|
|
(cond
|
|
|
|
[(pair? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x (fxadd1 n)))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x 0)
|
2006-11-23 19:48:14 -05:00
|
|
|
(graph (car x) h)
|
|
|
|
(graph (cdr x) h)])]
|
|
|
|
[(vector? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x (fxadd1 n)))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x 0)
|
2006-11-23 19:48:14 -05:00
|
|
|
(vec-graph x 0 (vector-length x) h)])]
|
|
|
|
[(gensym? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x (fxadd1 n)))])]))
|
2006-11-23 19:48:14 -05:00
|
|
|
(define (dynamic x h)
|
|
|
|
(cond
|
|
|
|
[(pair? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x (fxadd1 n)))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x 0)
|
2006-11-23 19:48:14 -05:00
|
|
|
(dynamic (car x) h)
|
|
|
|
(dynamic (cdr x) h)
|
2007-10-09 09:22:02 -04:00
|
|
|
(when (and (hashtable-ref h x #f)
|
|
|
|
(fxzero? (hashtable-ref h x #f)))
|
|
|
|
(hashtable-set! h x #f))])]
|
2006-11-23 19:48:14 -05:00
|
|
|
[(vector? x)
|
|
|
|
(cond
|
2007-10-09 09:22:02 -04:00
|
|
|
[(hashtable-ref h x #f) =>
|
2006-11-23 19:48:14 -05:00
|
|
|
(lambda (n)
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x (fxadd1 n)))]
|
2006-11-23 19:48:14 -05:00
|
|
|
[else
|
2007-10-09 09:22:02 -04:00
|
|
|
(hashtable-set! h x 0)
|
2006-11-23 19:48:14 -05:00
|
|
|
(vec-dynamic x 0 (vector-length x) h)
|
2007-10-09 09:22:02 -04:00
|
|
|
(when (and (hashtable-ref h x #f)
|
|
|
|
(fxzero? (hashtable-ref h x #f)))
|
|
|
|
(hashtable-set! h x #f))])]))
|
2006-11-23 19:48:14 -05:00
|
|
|
(if (print-graph)
|
|
|
|
(graph x h)
|
|
|
|
(dynamic x h)))
|
2007-04-29 23:00:20 -04:00
|
|
|
|
|
|
|
(define (write-to-port x p)
|
2007-10-10 07:09:18 -04:00
|
|
|
(let ([h (make-eq-hashtable)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(hasher x h)
|
|
|
|
(writer x p #t h 0))
|
2006-11-23 19:40:06 -05:00
|
|
|
(flush-output-port p))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-04-29 23:00:20 -04:00
|
|
|
(define (display-to-port x p)
|
2007-10-10 07:09:18 -04:00
|
|
|
(let ([h (make-eq-hashtable)])
|
2006-11-23 19:48:14 -05:00
|
|
|
(hasher x h)
|
|
|
|
(writer x p #f h 0))
|
2006-11-23 19:40:06 -05:00
|
|
|
(flush-output-port p))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define formatter
|
|
|
|
(lambda (who p fmt args)
|
|
|
|
(let f ([i 0] [args args])
|
|
|
|
(unless (fx= i (string-length fmt))
|
|
|
|
(let ([c (string-ref fmt i)])
|
|
|
|
(cond
|
2006-11-23 19:40:06 -05:00
|
|
|
[($char= c #\~)
|
2006-11-23 19:33:45 -05:00
|
|
|
(let ([i (fxadd1 i)])
|
|
|
|
(when (fx= i (string-length fmt))
|
|
|
|
(error who "invalid ~~ at end of format string ~s" fmt))
|
|
|
|
(let ([c (string-ref fmt i)])
|
|
|
|
(cond
|
2006-11-23 19:40:06 -05:00
|
|
|
[($char= c #\~)
|
2006-11-23 19:33:45 -05:00
|
|
|
(write-char #\~ p)
|
|
|
|
(f (fxadd1 i) args)]
|
2006-12-06 18:26:37 -05:00
|
|
|
[($char= c #\%)
|
|
|
|
(write-char #\newline p)
|
|
|
|
(f (fxadd1 i) args)]
|
2006-11-23 19:40:06 -05:00
|
|
|
[($char= c #\a)
|
2006-11-23 19:33:45 -05:00
|
|
|
(when (null? args)
|
|
|
|
(error who "insufficient arguments"))
|
2007-04-29 23:00:20 -04:00
|
|
|
(display-to-port (car args) p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(f (fxadd1 i) (cdr args))]
|
2006-11-23 19:40:06 -05:00
|
|
|
[($char= c #\s)
|
2006-11-23 19:33:45 -05:00
|
|
|
(when (null? args)
|
|
|
|
(error who "insufficient arguments"))
|
2007-04-29 23:00:20 -04:00
|
|
|
(write-to-port (car args) p)
|
2006-11-23 19:33:45 -05:00
|
|
|
(f (fxadd1 i) (cdr args))]
|
|
|
|
[else
|
|
|
|
(error who "invalid sequence ~~~a" c)])))]
|
|
|
|
[else
|
|
|
|
(write-char c p)
|
2006-12-02 05:56:42 -05:00
|
|
|
(f (fxadd1 i) args)]))))
|
|
|
|
(flush-output-port p)))
|
2007-04-29 23:00:20 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define fprintf
|
|
|
|
(lambda (port fmt . args)
|
|
|
|
(unless (output-port? port)
|
|
|
|
(error 'fprintf "~s is not an output port" port))
|
|
|
|
(unless (string? fmt)
|
|
|
|
(error 'fprintf "~s is not a string" fmt))
|
|
|
|
(formatter 'fprintf port fmt args)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-01-31 19:07:28 -05:00
|
|
|
(define display-error
|
|
|
|
(lambda (errname who fmt args)
|
2006-11-23 19:33:45 -05:00
|
|
|
(unless (string? fmt)
|
|
|
|
(error 'print-error "~s is not a string" fmt))
|
|
|
|
(let ([p (standard-error-port)])
|
|
|
|
(if who
|
2007-01-31 19:07:28 -05:00
|
|
|
(fprintf p "~a in ~a: " errname who)
|
|
|
|
(fprintf p "~a: " errname))
|
2006-11-23 19:33:45 -05:00
|
|
|
(formatter 'print-error p fmt args)
|
|
|
|
(write-char #\. p)
|
|
|
|
(newline p))))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
|
|
|
(define format
|
2007-04-29 23:00:20 -04:00
|
|
|
(lambda (fmt . args)
|
|
|
|
(unless (string? fmt)
|
|
|
|
(error 'format "~s is not a string" fmt))
|
|
|
|
(let ([p (open-output-string)])
|
|
|
|
(formatter 'format p fmt args)
|
|
|
|
(get-output-string p))))
|
|
|
|
|
2007-05-05 20:40:23 -04:00
|
|
|
(define printf
|
2007-04-29 23:00:20 -04:00
|
|
|
(lambda (fmt . args)
|
|
|
|
(unless (string? fmt)
|
|
|
|
(error 'printf "~s is not a string" fmt))
|
|
|
|
(formatter 'printf (current-output-port) fmt args)))
|
|
|
|
|
2007-05-05 20:40:23 -04:00
|
|
|
(define write
|
2006-11-23 19:40:06 -05:00
|
|
|
(case-lambda
|
2007-04-29 23:00:20 -04:00
|
|
|
[(x) (write-to-port x (current-output-port))]
|
2006-11-23 19:40:06 -05:00
|
|
|
[(x p)
|
|
|
|
(unless (output-port? p)
|
|
|
|
(error 'write "~s is not an output port" p))
|
2007-04-29 23:00:20 -04:00
|
|
|
(write-to-port x p)]))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
|
|
|
(define display
|
2006-11-23 19:40:06 -05:00
|
|
|
(case-lambda
|
2007-04-29 23:00:20 -04:00
|
|
|
[(x) (display-to-port x (current-output-port))]
|
2006-11-23 19:40:06 -05:00
|
|
|
[(x p)
|
|
|
|
(unless (output-port? p)
|
|
|
|
(error 'display "~s is not an output port" p))
|
2007-04-29 23:00:20 -04:00
|
|
|
(display-to-port x p)]))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
|
|
|
(define print-error
|
2007-01-31 19:07:28 -05:00
|
|
|
(lambda (who fmt . args)
|
|
|
|
(display-error "Error" who fmt args)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
|
|
|
(define warning
|
2007-01-31 19:07:28 -05:00
|
|
|
(lambda (who fmt . args)
|
|
|
|
(display-error "Warning" who fmt args)))
|
2007-05-05 20:40:23 -04:00
|
|
|
|
2007-10-23 23:55:57 -04:00
|
|
|
)
|
2006-11-23 19:33:45 -05:00
|
|
|
|