describe.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1998, 1999, 2001, 2006, 2008, 2009 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. ;;;; This software is a derivative work of other copyrighted softwares; the
  19. ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
  20. ;;;;
  21. ;;;; This file is based upon describe.stklos from the STk distribution by
  22. ;;;; Erick Gallesio <eg@unice.fr>.
  23. ;;;;
  24. (define-module (oop goops describe)
  25. :use-module (oop goops)
  26. :use-module (ice-9 session)
  27. :use-module (ice-9 format)
  28. :export (describe)) ; Export the describe generic function
  29. ;;;
  30. ;;; describe for simple objects
  31. ;;;
  32. (define-method (describe (x <top>))
  33. (format #t "~s is " x)
  34. (cond
  35. ((integer? x) (format #t "an integer"))
  36. ((real? x) (format #t "a real"))
  37. ((complex? x) (format #t "a complex number"))
  38. ((null? x) (format #t "an empty list"))
  39. ((boolean? x) (format #t "a boolean value (~s)" (if x 'true 'false)))
  40. ((char? x) (format #t "a character, ascii value is ~s"
  41. (char->integer x)))
  42. ((symbol? x) (format #t "a symbol"))
  43. ((list? x) (format #t "a list"))
  44. ((pair? x) (if (pair? (cdr x))
  45. (format #t "an improper list")
  46. (format #t "a pair")))
  47. ((string? x) (if (eqv? x "")
  48. (format #t "an empty string")
  49. (format #t "a string of length ~s" (string-length x))))
  50. ((vector? x) (if (eqv? x '#())
  51. (format #t "an empty vector")
  52. (format #t "a vector of length ~s" (vector-length x))))
  53. ((eof-object? x) (format #t "the end-of-file object"))
  54. (else (format #t "an unknown object (~s)" x)))
  55. (format #t ".~%")
  56. *unspecified*)
  57. (define-method (describe (x <procedure>))
  58. (let ((name (procedure-name x)))
  59. (if name
  60. (format #t "`~s'" name)
  61. (display x))
  62. (display " is ")
  63. (display (if name #\a "an anonymous"))
  64. (display " procedure")
  65. (display " with ")
  66. (arity x)))
  67. ;;;
  68. ;;; describe for GOOPS instances
  69. ;;;
  70. (define (safe-class-name class)
  71. (if (slot-bound? class 'name)
  72. (class-name class)
  73. class))
  74. (define-method (describe (x <object>))
  75. (format #t "~S is an instance of class ~A~%"
  76. x (safe-class-name (class-of x)))
  77. ;; print all the instance slots
  78. (format #t "Slots are: ~%")
  79. (for-each (lambda (slot)
  80. (let ((name (slot-definition-name slot)))
  81. (format #t " ~S = ~A~%"
  82. name
  83. (if (slot-bound? x name)
  84. (format #f "~S" (slot-ref x name))
  85. "#<unbound>"))))
  86. (class-slots (class-of x)))
  87. *unspecified*)
  88. ;;;
  89. ;;; Describe for classes
  90. ;;;
  91. (define-method (describe (x <class>))
  92. (format #t "~S is a class. It's an instance of ~A~%"
  93. (safe-class-name x) (safe-class-name (class-of x)))
  94. ;; Super classes
  95. (format #t "Superclasses are:~%")
  96. (for-each (lambda (class) (format #t " ~A~%" (safe-class-name class)))
  97. (class-direct-supers x))
  98. ;; Direct slots
  99. (let ((slots (class-direct-slots x)))
  100. (if (null? slots)
  101. (format #t "(No direct slot)~%")
  102. (begin
  103. (format #t "Directs slots are:~%")
  104. (for-each (lambda (s)
  105. (format #t " ~A~%" (slot-definition-name s)))
  106. slots))))
  107. ;; Direct subclasses
  108. (let ((classes (class-direct-subclasses x)))
  109. (if (null? classes)
  110. (format #t "(No direct subclass)~%")
  111. (begin
  112. (format #t "Directs subclasses are:~%")
  113. (for-each (lambda (s)
  114. (format #t " ~A~%" (safe-class-name s)))
  115. classes))))
  116. ;; CPL
  117. (format #t "Class Precedence List is:~%")
  118. (for-each (lambda (s) (format #t " ~A~%" (safe-class-name s)))
  119. (class-precedence-list x))
  120. ;; Direct Methods
  121. (let ((methods (class-direct-methods x)))
  122. (if (null? methods)
  123. (format #t "(No direct method)~%")
  124. (begin
  125. (format #t "Class direct methods are:~%")
  126. (for-each describe methods))))
  127. ; (format #t "~%Field Initializers ~% ")
  128. ; (write (slot-ref x 'initializers)) (newline)
  129. ; (format #t "~%Getters and Setters~% ")
  130. ; (write (slot-ref x 'getters-n-setters)) (newline)
  131. )
  132. ;;;
  133. ;;; Describe for generic functions
  134. ;;;
  135. (define-method (describe (x <generic>))
  136. (let ((name (generic-function-name x))
  137. (methods (generic-function-methods x)))
  138. ;; Title
  139. (format #t "~S is a generic function. It's an instance of ~A.~%"
  140. name (safe-class-name (class-of x)))
  141. ;; Methods
  142. (if (null? methods)
  143. (format #t "(No method defined for ~S)~%" name)
  144. (begin
  145. (format #t "Methods defined for ~S~%" name)
  146. (for-each (lambda (x) (describe x #t)) methods)))))
  147. ;;;
  148. ;;; Describe for methods
  149. ;;;
  150. (define-method (describe (x <method>) . omit-generic)
  151. (letrec ((print-args (lambda (args)
  152. ;; take care of dotted arg lists
  153. (cond ((null? args) (newline))
  154. ((pair? args)
  155. (display #\space)
  156. (display (safe-class-name (car args)))
  157. (print-args (cdr args)))
  158. (else
  159. (display #\space)
  160. (display (safe-class-name args))
  161. (newline))))))
  162. ;; Title
  163. (format #t " Method ~A~%" x)
  164. ;; Associated generic
  165. (if (null? omit-generic)
  166. (let ((gf (method-generic-function x)))
  167. (if gf
  168. (format #t "\t Generic: ~A~%" (generic-function-name gf))
  169. (format #t "\t(No generic)~%"))))
  170. ;; GF specializers
  171. (format #t "\tSpecializers:")
  172. (print-args (method-specializers x))))
  173. (provide 'describe)