From 8f9a6267a1d434af70ef7f18d0d87fd34ada9103 Mon Sep 17 00:00:00 2001 From: jimb Date: Fri, 6 May 2005 22:54:37 +0000 Subject: [PATCH] * pprint.scm, cos-pprint.scm: Add documentation. * pprint.scm (pprint): Don't wipe out elide-table after each call. --- cgen/ChangeLog | 4 +++ cgen/cos-pprint.scm | 10 ++++++ cgen/pprint.scm | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/cgen/ChangeLog b/cgen/ChangeLog index 38e4054666..b6f522758b 100644 --- a/cgen/ChangeLog +++ b/cgen/ChangeLog @@ -1,5 +1,9 @@ 2005-05-06 Jim Blandy + * pprint.scm, cos-pprint.scm: Add documentation. + + * pprint.scm (pprint): Don't wipe out elide-table after each call. + * pprint.scm, cos-pprint.scm: New files. 2005-04-04 Nick Clifton diff --git a/cgen/cos-pprint.scm b/cgen/cos-pprint.scm index ae39e95b9f..12321e1ed7 100644 --- a/cgen/cos-pprint.scm +++ b/cgen/cos-pprint.scm @@ -3,6 +3,16 @@ ;;;; This file is part of CGEN. ;;;; See file COPYING.CGEN for details. +;;; To use this with pprint.scm: +;;; +;;; (load "pprint.scm") +;;; (load "cos-pprint.scm") +;;; +;;; You must load this file second, so it can redefine the ELIDE? and +;;; ELIDED-NAME hooks. +;;; +;;; See the documentation in pprint.scm for details. + (define (elide? obj) (or (object? obj) (class? obj))) diff --git a/cgen/pprint.scm b/cgen/pprint.scm index d5a4d9ea63..bfd1a434d6 100644 --- a/cgen/pprint.scm +++ b/cgen/pprint.scm @@ -3,6 +3,94 @@ ;;;; This file is part of CGEN. ;;;; See file COPYING.CGEN for details. +;;; This file defines a printing function PPRINT, and some hooks to +;;; let you print certain kind of objects in a summary way, and get at +;;; their full values later. + +;;; PPRINT is a printer for Scheme objects that prints lists or +;;; vectors that contain shared structure or cycles and prints them in +;;; a finite, legible way. +;;; +;;; Ordinary values print in the usual way: +;;; +;;; guile> (pprint '(1 #(2 3) 4)) +;;; (1 #(2 3) 4) +;;; +;;; Values can share structure: +;;; +;;; guile> (let* ((c (list 1 2)) +;;; (d (list c c))) +;;; (write d) +;;; (newline)) +;;; ((1 2) (1 2)) +;;; +;;; In that list, the two instances of (1 2) are actually the same object; +;;; the top-level list refers to the same object twice. +;;; +;;; Printing that structure with PPRINT shows the sharing: +;;; +;;; guile> (let* ((c (list 1 2)) +;;; (d (list c c))) +;;; (pprint d)) +;;; (#0=(1 2) #0#) +;;; +;;; Here the "#0=" before the list (1 2) labels it with the number +;;; zero. Then, the "#0#" as the second element of the top-level list +;;; indicates that the object appears here, too, referring to it by +;;; its label. +;;; +;;; If you have several objects that appear more than once, they each +;;; get a separate label: +;;; +;;; guile> (let* ((a (list 1 2)) +;;; (b (list 3 4)) +;;; (c (list a b a b))) +;;; (pprint c)) +;;; (#0=(1 2) #1=(3 4) #0# #1#) +;;; +;;; Cyclic values just share structure with themselves: +;;; +;;; guile> (let* ((a (list 1 #f))) +;;; (set-cdr! a a) +;;; (pprint a)) +;;; #0=(1 . #0#) +;;; +;;; +;;; PPRINT also consults the function ELIDE? and ELIDED-NAME to see +;;; whether it should print a value in a summary form. You can +;;; re-define those functions to customize PPRINT's behavior; +;;; cos-pprint.scm defines them to handle COS objects and classes +;;; nicely. +;;; +;;; (ELIDE? OBJ) should return true if OBJ should be elided. +;;; (ELIDED-NAME OBJ) should return a (non-cyclic!) object to be used +;;; as OBJ's abbreviated form. +;;; +;;; PPRINT prints an elided object as a list ($ N NAME), where NAME is +;;; the value returned by ELIDED-NAME to stand for the object, and N +;;; is a number; each elided object gets its own number. You can refer +;;; to the elided object number N as ($ N). +;;; +;;; For example, if you've loaded CGEN, pprint.scm, and cos-pprint.scm +;;; (you must load cos-pprint.scm *after* loading pprint.scm), you can +;;; print a list containing the and classes: +;;; +;;; guile> (pprint (list )) +;;; (($ 1 (class )) ($ 2 (class ))) +;;; guile> (class-name ($ 1)) +;;; +;;; guile> (class-name ($ 2)) +;;; +;;; +;;; As a special case, PPRINT never elides the object that was passed +;;; to it directly. So you can look inside an elided object by doing +;;; just that: +;;; +;;; guile> (pprint ($ 2)) +;;; #0=#("class" () ((name #:unbound #f . 0) ... +;;; + + ;;; A list of elided objects, larger numbers first, and the number of ;;; the first element. (define elide-table '()) @@ -119,7 +207,6 @@ (begin (display " ") (print-tail tail))))))) - (set! elide-table (make-hash-table 4093)) (print original-obj) (newline))) -- 2.11.0