3 ;;;; Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
46 ;;;; This software is a derivative work of other copyrighted softwares; the
47 ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
49 ;;;; This file is based upon describe.stklos from the STk distribution by
50 ;;;; Erick Gallesio <eg@unice.fr>.
53 (define-module (oop goops describe)
54 :use-module (oop goops)
55 :use-module (ice-9 session)
56 :use-module (ice-9 format))
58 (export describe) ; Export the describe generic function
61 ;;; describe for simple objects
63 (define-method (describe (x <top>))
64 (format #t "~s is " x)
66 ((integer? x) (format #t "an integer"))
67 ((real? x) (format #t "a real"))
68 ((complex? x) (format #t "a complex number"))
69 ((null? x) (format #t "an empty list"))
70 ((boolean? x) (format #t "a boolean value (~s)" (if x 'true 'false)))
71 ((char? x) (format #t "a character, ascii value is ~s"
73 ((symbol? x) (format #t "a symbol"))
74 ((list? x) (format #t "a list"))
75 ((pair? x) (if (pair? (cdr x))
76 (format #t "an improper list")
77 (format #t "a pair")))
78 ((string? x) (if (eqv? x "")
79 (format #t "an empty string")
80 (format #t "a string of length ~s" (string-length x))))
81 ((vector? x) (if (eqv? x '#())
82 (format #t "an empty vector")
83 (format #t "a vector of length ~s" (vector-length x))))
84 ((eof-object? x) (format #t "the end-of-file object"))
85 (else (format #t "an unknown object (~s)" x)))
89 (define-method (describe (x <procedure>))
90 (let ((name (procedure-name x)))
92 (format #t "`~s'" name)
95 (display (if name #\a "an anonymous"))
96 (display (cond ((closure? x) " procedure")
97 ((not (struct? x)) " primitive procedure")
98 ((entity? x) " entity")
104 ;;; describe for GOOPS instances
106 (define (safe-class-name class)
107 (if (slot-bound? class 'name)
111 (define-method (describe (x <object>))
112 (format #t "~S is an instance of class ~A~%"
113 x (safe-class-name (class-of x)))
115 ;; print all the instance slots
116 (format #t "Slots are: ~%")
117 (for-each (lambda (slot)
118 (let ((name (slot-definition-name slot)))
119 (format #t " ~S = ~A~%"
121 (if (slot-bound? x name)
122 (format #f "~S" (slot-ref x name))
124 (class-slots (class-of x)))
128 ;;; Describe for classes
130 (define-method (describe (x <class>))
131 (format #t "~S is a class. It's an instance of ~A~%"
132 (safe-class-name x) (safe-class-name (class-of x)))
135 (format #t "Superclasses are:~%")
136 (for-each (lambda (class) (format #t " ~A~%" (safe-class-name class)))
137 (class-direct-supers x))
140 (let ((slots (class-direct-slots x)))
142 (format #t "(No direct slot)~%")
144 (format #t "Directs slots are:~%")
145 (for-each (lambda (s)
146 (format #t " ~A~%" (slot-definition-name s)))
151 (let ((classes (class-direct-subclasses x)))
153 (format #t "(No direct subclass)~%")
155 (format #t "Directs subclasses are:~%")
156 (for-each (lambda (s)
157 (format #t " ~A~%" (safe-class-name s)))
161 (format #t "Class Precedence List is:~%")
162 (for-each (lambda (s) (format #t " ~A~%" (safe-class-name s)))
163 (class-precedence-list x))
166 (let ((methods (class-direct-methods x)))
168 (format #t "(No direct method)~%")
170 (format #t "Class direct methods are:~%")
171 (for-each describe methods))))
173 ; (format #t "~%Field Initializers ~% ")
174 ; (write (slot-ref x 'initializers)) (newline)
176 ; (format #t "~%Getters and Setters~% ")
177 ; (write (slot-ref x 'getters-n-setters)) (newline)
181 ;;; Describe for generic functions
183 (define-method (describe (x <generic>))
184 (let ((name (generic-function-name x))
185 (methods (generic-function-methods x)))
187 (format #t "~S is a generic function. It's an instance of ~A.~%"
188 name (safe-class-name (class-of x)))
191 (format #t "(No method defined for ~S)~%" name)
193 (format #t "Methods defined for ~S~%" name)
194 (for-each (lambda (x) (describe x #t)) methods)))))
197 ;;; Describe for methods
199 (define-method (describe (x <method>) . omit-generic)
200 (letrec ((print-args (lambda (args)
201 ;; take care of dotted arg lists
202 (cond ((null? args) (newline))
205 (display (safe-class-name (car args)))
206 (print-args (cdr args)))
209 (display (safe-class-name args))
213 (format #t " Method ~A~%" x)
215 ;; Associated generic
216 (if (null? omit-generic)
217 (let ((gf (method-generic-function x)))
219 (format #t "\t Generic: ~A~%" (generic-function-name gf))
220 (format #t "\t(No generic)~%"))))
223 (format #t "\tSpecializers:")
224 (print-args (method-specializers x))))