1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-30 06:50:31 +02:00

make-record-type slight refactor

* test-suite/tests/records.test ("records"): Add tests for printers.

* module/ice-9/boot-9.scm (make-record-type): Refactor the code that
  makes the default printer.
This commit is contained in:
Andy Wingo 2010-04-19 19:33:57 +02:00
parent 22457d5730
commit 51797cec09
2 changed files with 49 additions and 32 deletions

View file

@ -649,16 +649,10 @@ If there is no handler at all, Guile prints an error and then exits."
(and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
(define (make-record-type type-name fields . opt) (define (make-record-type type-name fields . opt)
(let ((printer-fn (and (pair? opt) (car opt)))) (define (default-record-printer s p)
(let ((struct (make-struct record-type-vtable 0
(make-struct-layout
(apply string-append
(map (lambda (f) "pw") fields)))
(or printer-fn
(lambda (s p)
(display "#<" p) (display "#<" p)
(display type-name p) (display (record-type-name (record-type-descriptor s)) p)
(let loop ((fields fields) (let loop ((fields (record-type-fields (record-type-descriptor s)))
(off 0)) (off 0))
(cond (cond
((not (null? fields)) ((not (null? fields))
@ -667,7 +661,14 @@ If there is no handler at all, Guile prints an error and then exits."
(display ": " p) (display ": " p)
(display (struct-ref s off) p) (display (struct-ref s off) p)
(loop (cdr fields) (+ 1 off))))) (loop (cdr fields) (+ 1 off)))))
(display ">" p))) (display ">" p))
(let ((struct (make-struct record-type-vtable 0
(make-struct-layout
(apply string-append
(map (lambda (f) "pw") fields)))
(or (and (pair? opt) (car opt))
default-record-printer)
type-name type-name
(copy-tree fields)))) (copy-tree fields))))
;; Temporary solution: Associate a name to the record type descriptor ;; Temporary solution: Associate a name to the record type descriptor
@ -675,7 +676,7 @@ If there is no handler at all, Guile prints an error and then exits."
(set-struct-vtable-name! struct (if (symbol? type-name) (set-struct-vtable-name! struct (if (symbol? type-name)
type-name type-name
(string->symbol type-name))) (string->symbol type-name)))
struct))) struct))
(define (record-type-name obj) (define (record-type-name obj)
(if (record-type? obj) (if (record-type? obj)

View file

@ -1,6 +1,6 @@
;;;; records.test --- Test suite for Guile's records. -*- mode: scheme; coding: utf-8 -*- ;;;; records.test --- Test suite for Guile's records. -*- mode: scheme; coding: utf-8 -*-
;;;; ;;;;
;;;; Copyright (C) 2009 Free Software Foundation, Inc. ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
;;;; ;;;;
;;;; This library is free software; you can redistribute it and/or ;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public ;;;; modify it under the terms of the GNU Lesser General Public
@ -17,10 +17,13 @@
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-records) (define-module (test-records)
#:use-module (ice-9 format)
#:use-module (test-suite lib)) #:use-module (test-suite lib))
;; ascii names and symbols ;; ascii names and symbols, custom printer
(define rtd-foo (make-record-type "foo" '(x y))) (define rtd-foo (make-record-type "foo" '(x y)
(lambda (s p)
(display "#<it is a foo>" p))))
(define make-foo (record-constructor rtd-foo)) (define make-foo (record-constructor rtd-foo))
(define foo? (record-predicate rtd-foo)) (define foo? (record-predicate rtd-foo))
(define get-foo-x (record-accessor rtd-foo 'x)) (define get-foo-x (record-accessor rtd-foo 'x))
@ -28,7 +31,7 @@
(define set-foo-x! (record-modifier rtd-foo 'x)) (define set-foo-x! (record-modifier rtd-foo 'x))
(define set-foo-y! (record-modifier rtd-foo 'y)) (define set-foo-y! (record-modifier rtd-foo 'y))
;; non-Latin-1 names and symbols ;; non-Latin-1 names and symbols, default printer
(define rtd-fŏŏ (make-record-type "fŏŏ" '(x ȳ))) (define rtd-fŏŏ (make-record-type "fŏŏ" '(x ȳ)))
(define make-fŏŏ (record-constructor rtd-fŏŏ)) (define make-fŏŏ (record-constructor rtd-fŏŏ))
(define fŏŏ? (record-predicate rtd-fŏŏ)) (define fŏŏ? (record-predicate rtd-fŏŏ))
@ -71,4 +74,17 @@
(string=? "foo" (record-type-name rtd-foo))) (string=? "foo" (record-type-name rtd-foo)))
(pass-if "fŏŏ" (pass-if "fŏŏ"
(string=? "fŏŏ" (record-type-name rtd-fŏŏ))))) (string=? "fŏŏ" (record-type-name rtd-fŏŏ))))
(with-test-prefix "printer"
(pass-if "foo"
(string=? "#<it is a foo>"
(with-output-to-string
(lambda () (display (make-foo 1 2))))))
(pass-if "fŏŏ"
(with-locale "en_US.utf8"
(string-prefix? "#<fŏŏ"
(with-output-to-string
(lambda () (display (make-fŏŏ 1 2)))))))))