PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/pps.sc

https://bitbucket.org/bunny351/ezd
Scala | 120 lines | 110 code | 10 blank | 0 comment | 4 complexity | 9e622012841899bd7b3b6cb76e659ff6 MD5 | raw file
  1. ;;; ezd - easy drawing for X11 displays.
  2. ;;;
  3. ;;; Structure pretty-printer.
  4. ;* Copyright 1990-1993 Digital Equipment Corporation
  5. ;* All Rights Reserved
  6. ;*
  7. ;* Permission to use, copy, and modify this software and its documentation is
  8. ;* hereby granted only under the following terms and conditions. Both the
  9. ;* above copyright notice and this permission notice must appear in all copies
  10. ;* of the software, derivative works or modified versions, and any portions
  11. ;* thereof, and both notices must appear in supporting documentation.
  12. ;*
  13. ;* Users of this software agree to the terms and conditions set forth herein,
  14. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  15. ;* right and license under any changes, enhancements or extensions made to the
  16. ;* core functions of the software, including but not limited to those affording
  17. ;* compatibility with other hardware or software environments, but excluding
  18. ;* applications which incorporate this software. Users further agree to use
  19. ;* their best efforts to return to Digital any such changes, enhancements or
  20. ;* extensions that they make and inform Digital of noteworthy uses of this
  21. ;* software. Correspondence should be provided to Digital at:
  22. ;*
  23. ;* Director of Licensing
  24. ;* Western Research Laboratory
  25. ;* Digital Equipment Corporation
  26. ;* 250 University Avenue
  27. ;* Palo Alto, California 94301
  28. ;*
  29. ;* This software may be distributed (but not offered for sale or transferred
  30. ;* for compensation) to third parties, provided such third parties agree to
  31. ;* abide by the terms and conditions of this notice.
  32. ;*
  33. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  34. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  35. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  36. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  37. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  38. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  39. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  40. ;* SOFTWARE.
  41. (module pps)
  42. ;;; (PPS form [ output ]) pretty-prints the form on the current output port,
  43. ;;; another port, or to a file depending upon the value of "output".
  44. (define (PPS form . output)
  45. (let ((form (if (number? form) (vector-ref object-cache form) form)))
  46. (cond ((null? output)
  47. (pp1 form (current-output-port) #f))
  48. ((output-port? (car output))
  49. (pp1 form (car output) #f))
  50. (else
  51. (let ((port (open-output-file (car output))))
  52. (pp1 form port #f)
  53. (close-output-port port))))
  54. #t))
  55. (define (PP1 form port cache-structs)
  56. (let* ((indent (write-count port))
  57. (left (print-in form (- (write-width port) indent))))
  58. (cond ((negative? left)
  59. (cond ((pair? form)
  60. (display "(" port)
  61. (pp1 (car form) port #t)
  62. (do ((tab (make-string (+ indent 2) #\space))
  63. (x (cdr form) (cdr x)))
  64. ((not (pair? x))
  65. (when x
  66. (newline port)
  67. (display tab port)
  68. (display ". " port)
  69. (pp1 x port #t))
  70. (display ")" port))
  71. (newline port)
  72. (display tab port)
  73. (pp1 (car x) port #t)))
  74. ((vector? form)
  75. (display "#" port)
  76. (if (not cache-structs)
  77. (pp1 (vector->list form) port #t)
  78. (let ((x object-cache-index))
  79. (vector-set! object-cache x form)
  80. (set! object-cache-index
  81. (remainder (+ object-cache-index 1)
  82. (vector-length object-cache)))
  83. (pp1 (cons (vector-ref form 0) x) port #t))))
  84. (else (write form port))))
  85. (else (write form port)))))
  86. ;;; Objects are cached in OBJECT-CACHE and OBJECT-CACHE-INDEX is the index of
  87. ;;; the next free entry.
  88. (define OBJECT-CACHE (make-vector 40))
  89. (define OBJECT-CACHE-INDEX 0)
  90. ;;; PRINT-IN is used to decide if a form can be printed in line-length
  91. ;;; characters. If it can, then it will return:
  92. ;;; line-length - # characters needed
  93. ;;; otherwise it will return a negative number.
  94. (define (PRINT-IN form line-length)
  95. (cond ((negative? line-length) line-length)
  96. ((pair? form)
  97. (cond ((null? (cdr form)) ;;; End of list
  98. (- (print-in (car form) (- line-length 1)) 1))
  99. ((pair? (cdr form)) ;;; Continued list
  100. (print-in (cdr form) (print-in (car form)
  101. (- line-length 1))))
  102. (else ;;; Dotted pair
  103. (print-in (cdr form)
  104. (print-in (car form) (- line-length 5))))))
  105. ((vector? form) ;;; Vector is 1 longer than its list
  106. (print-in (vector->list form) (- line-length 1)))
  107. (else ;;; Print to a string port and measure
  108. (let ((port (open-output-string)))
  109. (write form port)
  110. (- line-length (string-length (get-output-string port)))))))