home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / guile / 1.6 / ice-9 / pretty-print.scm < prev    next >
Encoding:
Text File  |  2006-06-19  |  9.5 KB  |  280 lines

  1. ;;;; -*-scheme-*-
  2. ;;;;
  3. ;;;;     Copyright (C) 2001 Free Software Foundation, Inc.
  4. ;;;; 
  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.
  9. ;;;; 
  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.
  14. ;;;; 
  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., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 USA
  19. ;;;;
  20. ;;;; As a special exception, the Free Software Foundation gives permission
  21. ;;;; for additional uses of the text contained in its release of GUILE.
  22. ;;;;
  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.
  28. ;;;;
  29. ;;;; This exception does not however invalidate any other reasons why
  30. ;;;; the executable file might be covered by the GNU General Public License.
  31. ;;;;
  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.
  39. ;;;;
  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.
  43. ;;;; 
  44. (define-module (ice-9 pretty-print)
  45.   :export (pretty-print))
  46.  
  47. ;; From SLIB.
  48.  
  49. ;;"genwrite.scm" generic write used by pretty-print and truncated-print.
  50. ;; Copyright (c) 1991, Marc Feeley
  51. ;; Author: Marc Feeley (feeley@iro.umontreal.ca)
  52. ;; Distribution restrictions: none
  53.  
  54. (define genwrite:newline-str (make-string 1 #\newline))
  55.  
  56. (define (generic-write obj display? width output)
  57.  
  58.   (define (read-macro? l)
  59.     (define (length1? l) (and (pair? l) (null? (cdr l))))
  60.     (let ((head (car l)) (tail (cdr l)))
  61.       (case head
  62.         ((quote quasiquote unquote unquote-splicing) (length1? tail))
  63.         (else                                        #f))))
  64.  
  65.   (define (read-macro-body l)
  66.     (cadr l))
  67.  
  68.   (define (read-macro-prefix l)
  69.     (let ((head (car l)) (tail (cdr l)))
  70.       (case head
  71.         ((quote)            "'")
  72.         ((quasiquote)       "`")
  73.         ((unquote)          ",")
  74.         ((unquote-splicing) ",@"))))
  75.  
  76.   (define (out str col)
  77.     (and col (output str) (+ col (string-length str))))
  78.  
  79.   (define (wr obj col)
  80.     (cond ((and (pair? obj)
  81.         (read-macro? obj))
  82.        (wr (read-macro-body obj)
  83.            (out (read-macro-prefix obj) col)))
  84.       (else
  85.        (out (object->string obj (if display? display write)) col))))
  86.  
  87.   (define (pp obj col)
  88.  
  89.     (define (spaces n col)
  90.       (if (> n 0)
  91.         (if (> n 7)
  92.           (spaces (- n 8) (out "        " col))
  93.           (out (substring "        " 0 n) col))
  94.         col))
  95.  
  96.     (define (indent to col)
  97.       (and col
  98.            (if (< to col)
  99.              (and (out genwrite:newline-str col) (spaces to 0))
  100.              (spaces (- to col) col))))
  101.  
  102.     (define (pr obj col extra pp-pair)
  103.       (if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
  104.         (let ((result '())
  105.               (left (min (+ (- (- width col) extra) 1) max-expr-width)))
  106.           (generic-write obj display? #f
  107.             (lambda (str)
  108.               (set! result (cons str result))
  109.               (set! left (- left (string-length str)))
  110.               (> left 0)))
  111.           (if (> left 0) ; all can be printed on one line
  112.             (out (reverse-string-append result) col)
  113.             (if (pair? obj)
  114.               (pp-pair obj col extra)
  115.               (pp-list (vector->list obj) (out "#" col) extra pp-expr))))
  116.         (wr obj col)))
  117.  
  118.     (define (pp-expr expr col extra)
  119.       (if (read-macro? expr)
  120.         (pr (read-macro-body expr)
  121.             (out (read-macro-prefix expr) col)
  122.             extra
  123.             pp-expr)
  124.         (let ((head (car expr)))
  125.           (if (symbol? head)
  126.             (let ((proc (style head)))
  127.               (if proc
  128.                 (proc expr col extra)
  129.                 (if (> (string-length (symbol->string head))
  130.                        max-call-head-width)
  131.                   (pp-general expr col extra #f #f #f pp-expr)
  132.                   (pp-call expr col extra pp-expr))))
  133.             (pp-list expr col extra pp-expr)))))
  134.  
  135.     ; (head item1
  136.     ;       item2
  137.     ;       item3)
  138.     (define (pp-call expr col extra pp-item)
  139.       (let ((col* (wr (car expr) (out "(" col))))
  140.         (and col
  141.              (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))
  142.  
  143.     ; (item1
  144.     ;  item2
  145.     ;  item3)
  146.     (define (pp-list l col extra pp-item)
  147.       (let ((col (out "(" col)))
  148.         (pp-down l col col extra pp-item)))
  149.  
  150.     (define (pp-down l col1 col2 extra pp-item)
  151.       (let loop ((l l) (col col1))
  152.         (and col
  153.              (cond ((pair? l)
  154.                     (let ((rest (cdr l)))
  155.                       (let ((extra (if (null? rest) (+ extra 1) 0)))
  156.                         (loop rest
  157.                               (pr (car l) (indent col2 col) extra pp-item)))))
  158.                    ((null? l)
  159.                     (out ")" col))
  160.                    (else
  161.                     (out ")"
  162.                          (pr l
  163.                              (indent col2 (out "." (indent col2 col)))
  164.                              (+ extra 1)
  165.                              pp-item)))))))
  166.  
  167.     (define (pp-general expr col extra named? pp-1 pp-2 pp-3)
  168.  
  169.       (define (tail1 rest col1 col2 col3)
  170.         (if (and pp-1 (pair? rest))
  171.           (let* ((val1 (car rest))
  172.                  (rest (cdr rest))
  173.                  (extra (if (null? rest) (+ extra 1) 0)))
  174.             (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
  175.           (tail2 rest col1 col2 col3)))
  176.  
  177.       (define (tail2 rest col1 col2 col3)
  178.         (if (and pp-2 (pair? rest))
  179.           (let* ((val1 (car rest))
  180.                  (rest (cdr rest))
  181.                  (extra (if (null? rest) (+ extra 1) 0)))
  182.             (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
  183.           (tail3 rest col1 col2)))
  184.  
  185.       (define (tail3 rest col1 col2)
  186.         (pp-down rest col2 col1 extra pp-3))
  187.  
  188.       (let* ((head (car expr))
  189.              (rest (cdr expr))
  190.              (col* (wr head (out "(" col))))
  191.         (if (and named? (pair? rest))
  192.           (let* ((name (car rest))
  193.                  (rest (cdr rest))
  194.                  (col** (wr name (out " " col*))))
  195.             (tail1 rest (+ col indent-general) col** (+ col** 1)))
  196.           (tail1 rest (+ col indent-general) col* (+ col* 1)))))
  197.  
  198.     (define (pp-expr-list l col extra)
  199.       (pp-list l col extra pp-expr))
  200.  
  201.     (define (pp-LAMBDA expr col extra)
  202.       (pp-general expr col extra #f pp-expr-list #f pp-expr))
  203.  
  204.     (define (pp-IF expr col extra)
  205.       (pp-general expr col extra #f pp-expr #f pp-expr))
  206.  
  207.     (define (pp-COND expr col extra)
  208.       (pp-call expr col extra pp-expr-list))
  209.  
  210.     (define (pp-CASE expr col extra)
  211.       (pp-general expr col extra #f pp-expr #f pp-expr-list))
  212.  
  213.     (define (pp-AND expr col extra)
  214.       (pp-call expr col extra pp-expr))
  215.  
  216.     (define (pp-LET expr col extra)
  217.       (let* ((rest (cdr expr))
  218.              (named? (and (pair? rest) (symbol? (car rest)))))
  219.         (pp-general expr col extra named? pp-expr-list #f pp-expr)))
  220.  
  221.     (define (pp-BEGIN expr col extra)
  222.       (pp-general expr col extra #f #f #f pp-expr))
  223.  
  224.     (define (pp-DO expr col extra)
  225.       (pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))
  226.  
  227.     ; define formatting style (change these to suit your style)
  228.  
  229.     (define indent-general 2)
  230.  
  231.     (define max-call-head-width 5)
  232.  
  233.     (define max-expr-width 50)
  234.  
  235.     (define (style head)
  236.       (case head
  237.         ((lambda let* letrec define) pp-LAMBDA)
  238.         ((if set!)                   pp-IF)
  239.         ((cond)                      pp-COND)
  240.         ((case)                      pp-CASE)
  241.         ((and or)                    pp-AND)
  242.         ((let)                       pp-LET)
  243.         ((begin)                     pp-BEGIN)
  244.         ((do)                        pp-DO)
  245.         (else                        #f)))
  246.  
  247.     (pr obj col 0 pp-expr))
  248.  
  249.   (if width
  250.     (out genwrite:newline-str (pp obj 0))
  251.     (wr obj 0))
  252.   ;; Return `unspecified'
  253.   (if #f #f))
  254.  
  255. ; (reverse-string-append l) = (apply string-append (reverse l))
  256.  
  257. (define (reverse-string-append l)
  258.  
  259.   (define (rev-string-append l i)
  260.     (if (pair? l)
  261.       (let* ((str (car l))
  262.              (len (string-length str))
  263.              (result (rev-string-append (cdr l) (+ i len))))
  264.         (let loop ((j 0) (k (- (- (string-length result) i) len)))
  265.           (if (< j len)
  266.             (begin
  267.               (string-set! result k (string-ref str j))
  268.               (loop (+ j 1) (+ k 1)))
  269.             result)))
  270.       (make-string i)))
  271.  
  272.   (rev-string-append l 0))
  273.  
  274. ;"pp.scm" Pretty-Print
  275. (define (pretty-print obj . opt)
  276.   (let ((port (if (pair? opt) (car opt) (current-output-port))))
  277.     (generic-write obj #f 79
  278.            (lambda (s) (display s port) #t))))
  279.  
  280.