* (ikarus writer) is now ok.

This commit is contained in:
Abdulaziz Ghuloum 2007-05-05 20:40:23 -04:00
parent b425bc58cb
commit 7aa407b6cd
3 changed files with 52 additions and 20 deletions

Binary file not shown.

View File

@ -1,13 +1,21 @@
(library (ikarus writer) (library (ikarus writer)
(export) (export write display format printf print-error error-handler
(import (scheme)) error)
(import
(only (scheme) $fixnum->char $char->fixnum $char= $char<=
$fx= $fx>= $fx+ $fxadd1 $car $cdr $forward-ptr?
$unbound-object?
$string-length $string-ref)
(except (ikarus) write display format printf print-error
error-handler error))
(define char-table ; first nonprintable chars (define char-table ; first nonprintable chars
'#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel" "bs" "tab" "newline" '#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel" "bs" "tab" "newline"
"vt" "ff" "return" "so" "si" "dle" "dc1" "dc2" "dc3" "dc4" "nak" "vt" "ff" "return" "so" "si" "dle" "dc1" "dc2" "dc3" "dc4" "nak"
"syn" "etb" "can" "em" "sub" "esc" "fs" "gs" "rs" "us" "space")) "syn" "etb" "can" "em" "sub" "esc" "fs" "gs" "rs" "us" "space"))
(define write-character (define write-character
(lambda (x p m) (lambda (x p m)
(if m (if m
@ -28,6 +36,7 @@
(write-char #\x p) (write-char #\x p)
(write-fixnum i p)])) (write-fixnum i p)]))
(write-char x p)))) (write-char x p))))
(define write-list (define write-list
(lambda (x p m h i) (lambda (x p m h i)
(cond (cond
@ -43,6 +52,7 @@
(write-char #\. p) (write-char #\. p)
(write-char #\space p) (write-char #\space p)
(writer x p m h i)]))) (writer x p m h i)])))
(define write-vector (define write-vector
(lambda (x p m h i) (lambda (x p m h i)
(write-char #\# p) (write-char #\# p)
@ -62,6 +72,7 @@
[else i])]) [else i])])
(write-char #\) p) (write-char #\) p)
i)))) i))))
(define write-record (define write-record
(lambda (x p m h i) (lambda (x p m h i)
(write-char #\# p) (write-char #\# p)
@ -77,32 +88,40 @@
(write-char #\space p) (write-char #\space p)
(f (fxadd1 idx) (f (fxadd1 idx)
(writer (record-ref x idx) p m h i))])))))) (writer (record-ref x idx) p m h i))]))))))
(define initial? (define initial?
(lambda (c) (lambda (c)
(or (letter? c) (special-initial? c)))) (or (letter? c) (special-initial? c))))
(define letter? (define letter?
(lambda (c) (lambda (c)
(or (and ($char<= #\a c) ($char<= c #\z)) (or (and ($char<= #\a c) ($char<= c #\z))
(and ($char<= #\A c) ($char<= c #\Z))))) (and ($char<= #\A c) ($char<= c #\Z)))))
(define digit? (define digit?
(lambda (c) (lambda (c)
(and ($char<= #\0 c) ($char<= c #\9)))) (and ($char<= #\0 c) ($char<= c #\9))))
(define special-initial? (define special-initial?
(lambda (x) (lambda (x)
(memq x '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~)))) (memq x '(#\! #\$ #\% #\& #\* #\/ #\: #\< #\= #\> #\? #\^ #\_ #\~))))
(define subsequent? (define subsequent?
(lambda (x) (lambda (x)
(or (initial? x) (or (initial? x)
(digit? x) (digit? x)
(special-subsequent? x)))) (special-subsequent? x))))
(define special-subsequent? (define special-subsequent?
(lambda (x) (lambda (x)
(memq x '(#\+ #\- #\. #\@)))) (memq x '(#\+ #\- #\. #\@))))
(define subsequent*? (define subsequent*?
(lambda (str i n) (lambda (str i n)
(or ($fx= i n) (or ($fx= i n)
(and (subsequent? ($string-ref str i)) (and (subsequent? ($string-ref str i))
(subsequent*? str ($fxadd1 i) n))))) (subsequent*? str ($fxadd1 i) n)))))
(define valid-symbol-string? (define valid-symbol-string?
(lambda (str) (lambda (str)
(define normal-symbol-string? (define normal-symbol-string?
@ -133,11 +152,13 @@
(write-char #\\ p)) (write-char #\\ p))
(write-char c p)) (write-char c p))
(write-symbol-esc-loop x ($fxadd1 i) n p)))) (write-symbol-esc-loop x ($fxadd1 i) n p))))
(define write-symbol-esc (define write-symbol-esc
(lambda (x p) (lambda (x p)
(write-char #\| p) (write-char #\| p)
(write-symbol-esc-loop x 0 ($string-length x) p) (write-symbol-esc-loop x 0 ($string-length x) p)
(write-char #\| p))) (write-char #\| p)))
(define write-symbol (define write-symbol
(lambda (x p m) (lambda (x p m)
(let ([str (symbol->string x)]) (let ([str (symbol->string x)])
@ -146,6 +167,7 @@
(write-char* str p) (write-char* str p)
(write-symbol-esc str p)) (write-symbol-esc str p))
(write-char* str p))))) (write-char* str p)))))
(define write-gensym (define write-gensym
(lambda (x p m h i) (lambda (x p m h i)
(cond (cond
@ -173,6 +195,7 @@
[else [else
(write-symbol x p m) (write-symbol x p m)
i]))) i])))
(define write-string-escape (define write-string-escape
(lambda (x p) (lambda (x p)
(define loop (define loop
@ -198,11 +221,13 @@
(write-char #\" p) (write-char #\" p)
(loop x 0 (string-length x) p) (loop x 0 (string-length x) p)
(write-char #\" p))) (write-char #\" p)))
(define write-string (define write-string
(lambda (x p m) (lambda (x p m)
(if m (if m
(write-string-escape x p) (write-string-escape x p)
(write-char* x p)))) (write-char* x p))))
(define write-fixnum (define write-fixnum
(lambda (x p) (lambda (x p)
(define loop (define loop
@ -222,6 +247,7 @@
(write-char* "536870912" p) (write-char* "536870912" p)
(loop (fx- 0 x) p))] (loop (fx- 0 x) p))]
[else (loop x p)]))) [else (loop x p)])))
(define write-char* (define write-char*
(lambda (x p) (lambda (x p)
(define loop (define loop
@ -230,6 +256,7 @@
(write-char (string-ref x i) p) (write-char (string-ref x i) p)
(loop x (fxadd1 i) n p)))) (loop x (fxadd1 i) n p))))
(loop x 0 (string-length x) p))) (loop x 0 (string-length x) p)))
(define macro (define macro
(lambda (x) (lambda (x)
(define macro-forms (define macro-forms
@ -244,6 +271,7 @@
(and (pair? d) (and (pair? d)
(null? ($cdr d)))) (null? ($cdr d))))
(assq ($car x) macro-forms)))) (assq ($car x) macro-forms))))
(define write-pair (define write-pair
(lambda (x p m h i) (lambda (x p m h i)
(write-char #\( p) (write-char #\( p)
@ -251,16 +279,19 @@
(let ([i (write-list (cdr x) p m h i)]) (let ([i (write-list (cdr x) p m h i)])
(write-char #\) p) (write-char #\) p)
i)))) i))))
(define write-ref (define write-ref
(lambda (n p) (lambda (n p)
(write-char #\# p) (write-char #\# p)
(write-fixnum (fx- -1 n) p) (write-fixnum (fx- -1 n) p)
(write-char #\# p))) (write-char #\# p)))
(define write-mark (define write-mark
(lambda (n p) (lambda (n p)
(write-char #\# p) (write-char #\# p)
(write-fixnum (fx- -1 n) p) (write-fixnum (fx- -1 n) p)
(write-char #\= p))) (write-char #\= p)))
(define write-shareable (define write-shareable
(lambda (x p m h i k) (lambda (x p m h i k)
(cond (cond
@ -278,6 +309,7 @@
(write-mark i p) (write-mark i p)
(k x p m h i))]))] (k x p m h i))]))]
[else (k x p m h i)]))) [else (k x p m h i)])))
(define writer (define writer
(lambda (x p m h i) (lambda (x p m h i)
(cond (cond
@ -419,13 +451,13 @@
(hasher x h) (hasher x h)
(writer x p #t h 0)) (writer x p #t h 0))
(flush-output-port p)) (flush-output-port p))
;;;
(define (display-to-port x p) (define (display-to-port x p)
(let ([h (make-hash-table)]) (let ([h (make-hash-table)])
(hasher x h) (hasher x h)
(writer x p #f h 0)) (writer x p #f h 0))
(flush-output-port p)) (flush-output-port p))
;;;
(define formatter (define formatter
(lambda (who p fmt args) (lambda (who p fmt args)
(let f ([i 0] [args args]) (let f ([i 0] [args args])
@ -468,8 +500,7 @@
(unless (string? fmt) (unless (string? fmt)
(error 'fprintf "~s is not a string" fmt)) (error 'fprintf "~s is not a string" fmt))
(formatter 'fprintf port fmt args))) (formatter 'fprintf port fmt args)))
(define display-error (define display-error
(lambda (errname who fmt args) (lambda (errname who fmt args)
(unless (string? fmt) (unless (string? fmt)
@ -481,8 +512,8 @@
(formatter 'print-error p fmt args) (formatter 'print-error p fmt args)
(write-char #\. p) (write-char #\. p)
(newline p)))) (newline p))))
;;;
(primitive-set! 'format (define format
(lambda (fmt . args) (lambda (fmt . args)
(unless (string? fmt) (unless (string? fmt)
(error 'format "~s is not a string" fmt)) (error 'format "~s is not a string" fmt))
@ -490,35 +521,37 @@
(formatter 'format p fmt args) (formatter 'format p fmt args)
(get-output-string p)))) (get-output-string p))))
(primitive-set! 'printf (define printf
(lambda (fmt . args) (lambda (fmt . args)
(unless (string? fmt) (unless (string? fmt)
(error 'printf "~s is not a string" fmt)) (error 'printf "~s is not a string" fmt))
(formatter 'printf (current-output-port) fmt args))) (formatter 'printf (current-output-port) fmt args)))
(primitive-set! 'fprintf fprintf) (define write
(primitive-set! 'print-graph print-graph)
(primitive-set! 'write
(case-lambda (case-lambda
[(x) (write-to-port x (current-output-port))] [(x) (write-to-port x (current-output-port))]
[(x p) [(x p)
(unless (output-port? p) (unless (output-port? p)
(error 'write "~s is not an output port" p)) (error 'write "~s is not an output port" p))
(write-to-port x p)])) (write-to-port x p)]))
(primitive-set! 'display
(define display
(case-lambda (case-lambda
[(x) (display-to-port x (current-output-port))] [(x) (display-to-port x (current-output-port))]
[(x p) [(x p)
(unless (output-port? p) (unless (output-port? p)
(error 'display "~s is not an output port" p)) (error 'display "~s is not an output port" p))
(display-to-port x p)])) (display-to-port x p)]))
(primitive-set! 'print-error
(define print-error
(lambda (who fmt . args) (lambda (who fmt . args)
(display-error "Error" who fmt args))) (display-error "Error" who fmt args)))
(primitive-set! 'warning
(define warning
(lambda (who fmt . args) (lambda (who fmt . args)
(display-error "Warning" who fmt args))) (display-error "Warning" who fmt args)))
(primitive-set! 'error-handler
(define error-handler
(make-parameter (make-parameter
(lambda args (lambda args
(apply print-error args) (apply print-error args)
@ -528,7 +561,8 @@
(if (procedure? x) (if (procedure? x)
x x
(error 'error-handler "~s is not a procedure" x))))) (error 'error-handler "~s is not a procedure" x)))))
(primitive-set! 'error
(define error
(lambda args (lambda args
(apply (error-handler) args)))) (apply (error-handler) args))))

View File

@ -42,17 +42,15 @@
"ikarus.command-line.ss" "ikarus.command-line.ss"
"ikarus.core.ss" "ikarus.core.ss"
"ikarus.io-ports.ss" "ikarus.io-ports.ss"
"ikarus.io-primitives.unsafe.ss" "ikarus.io-primitives.unsafe.ss"
"ikarus.io-primitives.ss" "ikarus.io-primitives.ss"
"ikarus.io.input-files.ss" "ikarus.io.input-files.ss"
"ikarus.io.output-files.ss" "ikarus.io.output-files.ss"
"ikarus.io.output-strings.ss" "ikarus.io.output-strings.ss"
"ikarus.hash-tables.ss" "ikarus.hash-tables.ss"
"ikarus.writer.ss"
"libwriter.ss"
"libtokenizer.ss" "libtokenizer.ss"
"libassembler.ss" "libassembler.ss"
"libintelasm.ss" "libintelasm.ss"