Merge branch 'define-record-writer'
This commit is contained in:
commit
13eb47046e
|
@ -6,6 +6,7 @@ list(APPEND PICLIB_SCHEME_LIBS
|
|||
|
||||
${PROJECT_SOURCE_DIR}/piclib/scheme/base.scm
|
||||
|
||||
${PROJECT_SOURCE_DIR}/piclib/picrin/record.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/picrin/array.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/picrin/dictionary.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/picrin/test.scm
|
||||
|
|
|
@ -89,19 +89,17 @@
|
|||
(define (array-for-each proc ary)
|
||||
(for-each proc (array->list ary)))
|
||||
|
||||
(define (print-array array)
|
||||
(define-record-writer (<array> array)
|
||||
(call-with-port (open-output-string)
|
||||
(lambda (port)
|
||||
(display "#.(array" port)
|
||||
(array-for-each
|
||||
(lambda (obj)
|
||||
(display " " port)
|
||||
(write obj port))
|
||||
array)
|
||||
(display ")" port)
|
||||
(get-output-string port))))
|
||||
|
||||
(record-set! <array> 'writer print-array)
|
||||
(lambda (port)
|
||||
(display "#.(array" port)
|
||||
(array-for-each
|
||||
(lambda (obj)
|
||||
(display " " port)
|
||||
(write obj port))
|
||||
array)
|
||||
(display ")" port)
|
||||
(get-output-string port))))
|
||||
|
||||
(export make-array
|
||||
array
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
(define-library (picrin record)
|
||||
(import (scheme base))
|
||||
|
||||
(define (define-record-writer* record-type writer)
|
||||
(record-set! record-type 'writer writer))
|
||||
|
||||
(define-syntax define-record-writer
|
||||
(syntax-rules ()
|
||||
((_ (type obj) body ...)
|
||||
(define-record-writer* type
|
||||
(lambda (obj)
|
||||
body ...)))
|
||||
((_ type writer)
|
||||
(define-record-writer* type
|
||||
writer))))
|
||||
|
||||
(export define-record-writer))
|
Loading…
Reference in New Issue