;;; Describe objects ;; Copyright (C) 2001 Free Software Foundation, Inc. ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Code: (define-module (system repl describe) :use-module (oop goops) :use-module (ice-9 regex) :use-module (ice-9 format) :use-module (ice-9 and-let-star) :export (describe)) (define-method (describe (symbol )) (format #t "`~s' is " symbol) (if (not (defined? symbol)) (display "not defined in the current module.\n") (describe-object (module-ref (current-module) symbol)))) ;;; ;;; Display functions ;;; (define (safe-class-name class) (if (slot-bound? class 'name) (class-name class) class)) (define-method (display-class class . args) (let* ((name (safe-class-name class)) (desc (if (pair? args) (car args) name))) (if (eq? *describe-format* 'tag) (format #t "@class{~a}{~a}" name desc) (format #t "~a" desc)))) (define-public (display-list title list) (if title (begin (display title) (display ":\n\n"))) (if (null? list) (display "(not defined)\n") (for-each display-summary list))) (define-public (display-slot-list title instance list) (if title (begin (display title) (display ":\n\n"))) (if (null? list) (display "(not defined)\n") (for-each (lambda (slot) (let ((name (slot-definition-name slot))) (display "Slot: ") (display name) (if (and instance (slot-bound? instance name)) (begin (display " = ") (display (slot-ref instance name)))) (newline))) list))) (define-public (display-file location) (display "Defined in ") (if (eq? *describe-format* 'tag) (format #t "@location{~a}.\n" location) (format #t "`~a'.\n" location))) (define-public (format-documentation doc) (with-current-buffer (make-buffer #:text doc) (lambda () (let ((regexp (make-regexp "@([a-z]*)(\\{([^\}]*)\\})?"))) (do-while (match (re-search-forward regexp)) (let ((key (string->symbol (match:substring match 1))) (value (match:substring match 3))) (case key ((deffnx) (delete-region! (match:start match) (begin (forward-line) (point)))) ((var) (replace-match! match 0 (string-upcase value))) ((code) (replace-match! match 0 (string-append "`" value "'"))))))) (display (string (current-buffer))) (newline)))) ;;; ;;; Top ;;; (define description-table (list (cons "a boolean") (cons "an empty list") (cons "an integer") (cons "a real number") (cons "a complex number") (cons "a character") (cons "a symbol") (cons "a keyword") (cons "a promise") (cons "a hook") (cons "a fluid") (cons "a stack") (cons "a variable") (cons "a regexp object") (cons "a module object") (cons "an unknown object"))) (define-generic describe-object) (export describe-object) (define-method (describe-object (obj )) (display-type obj) (display-location obj) (newline) (display-value obj) (newline) (display-documentation obj)) (define-generic display-object) (define-generic display-summary) (define-generic display-type) (define-generic display-value) (define-generic display-location) (define-generic display-description) (define-generic display-documentation) (export display-object display-summary display-type display-value display-location display-description display-documentation) (define-method (display-object (obj )) (write obj)) (define-method (display-summary (obj )) (display "Value: ") (display-object obj) (newline)) (define-method (display-type (obj )) (cond ((eof-object? obj) (display "the end-of-file object")) ((unspecified? obj) (display "unspecified")) (else (let ((class (class-of obj))) (display-class class (or (assq-ref description-table class) (safe-class-name class)))))) (display ".\n")) (define-method (display-value (obj )) (if (not (unspecified? obj)) (begin (display-object obj) (newline)))) (define-method (display-location (obj )) *unspecified*) (define-method (display-description (obj )) (let* ((doc (with-output-to-string (lambda () (display-documentation obj)))) (index (string-index doc #\newline))) (display (make-shared-substring doc 0 (1+ index))))) (define-method (display-documentation (obj )) (display "Not documented.\n")) ;;; ;;; Pairs ;;; (define-method (display-type (obj )) (cond ((list? obj) (display-class "a list")) ((pair? (cdr obj)) (display "an improper list")) (else (display-class "a pair"))) (display ".\n")) ;;; ;;; Strings ;;; (define-method (display-type (obj )) (if (read-only-string? 'obj) (display "a read-only string") (display-class "a string")) (display ".\n")) ;;; ;;; Procedures ;;; (define-method (display-object (obj )) (cond ((closure? obj) ;; Construct output from the source. (display "(") (display (procedure-name obj)) (let ((args (cadr (procedure-source obj)))) (cond ((null? args) (display ")")) ((pair? args) (let ((str (with-output-to-string (lambda () (display args))))) (format #t " ~a" (string-upcase! (substring str 1))))) (else (format #t " . ~a)" (string-upcase! (symbol->string args))))))) (else ;; Primitive procedure. Let's lookup the dictionary. (and-let* ((entry (lookup-procedure obj))) (let ((name (entry-property entry 'name)) (print-arg (lambda (arg) (display " ") (display (string-upcase (symbol->string arg)))))) (display "(") (display name) (and-let* ((args (entry-property entry 'args))) (for-each print-arg args)) (and-let* ((opts (entry-property entry 'opts))) (display " &optional") (for-each print-arg opts)) (and-let* ((rest (entry-property entry 'rest))) (display " &rest") (print-arg rest)) (display ")")))))) (define-method (display-summary (obj )) (display "Procedure: ") (display-object obj) (newline) (display " ") (display-description obj)) (define-method (display-type (obj )) (cond ((and (thunk? obj) (not (procedure-name obj))) (display "a thunk")) ((closure? obj) (display-class "a procedure")) ((procedure-with-setter? obj) (display-class "a procedure with setter")) ((not (struct? obj)) (display "a primitive procedure")) (else (display-class "a procedure"))) (display ".\n")) (define-method (display-location (obj )) (and-let* ((entry (lookup-procedure obj))) (display-file (entry-file entry)))) (define-method (display-documentation (obj )) (cond ((cond ((closure? obj) (procedure-documentation obj)) ((lookup-procedure obj) => entry-text) (else #f)) => format-documentation) (else (next-method)))) ;;; ;;; Classes ;;; (define-method (describe-object (obj )) (display-type obj) (display-location obj) (newline) (display-documentation obj) (newline) (display-value obj)) (define-method (display-summary (obj )) (display "Class: ") (display-class obj) (newline) (display " ") (display-description obj)) (define-method (display-type (obj )) (display-class "a class") (if (not (eq? (class-of obj) )) (begin (display " of ") (display-class (class-of obj)))) (display ".\n")) (define-method (display-value (obj )) (display-list "Class precedence list" (class-precedence-list obj)) (newline) (display-list "Direct superclasses" (class-direct-supers obj)) (newline) (display-list "Direct subclasses" (class-direct-subclasses obj)) (newline) (display-slot-list "Direct slots" #f (class-direct-slots obj)) (newline) (display-list "Direct methods" (class-direct-methods obj))) ;;; ;;; Instances ;;; (define-method (display-type (obj )) (display-class "an instance") (display " of class ") (display-class (class-of obj)) (display ".\n")) (define-method (display-value (obj )) (display-slot-list #f obj (class-slots (class-of obj)))) ;;; ;;; Generic functions ;;; (define-method (display-type (obj )) (display-class "a generic function") (display " of class ") (display-class (class-of obj)) (display ".\n")) (define-method (display-value (obj )) (display-list #f (generic-function-methods obj))) ;;; ;;; Methods ;;; (define-method (display-object (obj )) (display "(") (let ((gf (method-generic-function obj))) (display (if gf (generic-function-name gf) "#"))) (let loop ((args (method-specializers obj))) (cond ((null? args)) ((pair? args) (display " ") (display-class (car args)) (loop (cdr args))) (else (display " . ") (display-class args)))) (display ")")) (define-method (display-summary (obj )) (display "Method: ") (display-object obj) (newline) (display " ") (display-description obj)) (define-method (display-type (obj )) (display-class "a method") (display " of class ") (display-class (class-of obj)) (display ".\n")) (define-method (display-documentation (obj )) (let ((doc (procedure-documentation (method-procedure obj)))) (if doc (format-documentation doc) (next-method))))