punify.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ;;; punify --- Display Scheme code w/o unnecessary comments / whitespace
  2. ;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program 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 software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Thien-Thi Nguyen
  19. ;;; Commentary:
  20. ;; Usage: punify FILE1 FILE2 ...
  21. ;;
  22. ;; Each file's forms are read and written to stdout.
  23. ;; The effect is to remove comments and much non-essential whitespace.
  24. ;; This is useful when installing Scheme source to space-limited media.
  25. ;;
  26. ;; Example:
  27. ;; $ wc ./punify ; ./punify ./punify | wc
  28. ;; 89 384 3031 ./punify
  29. ;; 0 42 920
  30. ;;
  31. ;; TODO: Read from stdin.
  32. ;; Handle vectors.
  33. ;; Identifier punification.
  34. ;;; Code:
  35. (define-module (scripts punify)
  36. :export (punify))
  37. (define %include-in-guild-list #f)
  38. (define %summary "Strip comments and whitespace from a Scheme file.")
  39. (define (write-punily form)
  40. (cond ((and (list? form) (not (null? form)))
  41. (let ((first (car form)))
  42. (display "(")
  43. (write-punily first)
  44. (let loop ((ls (cdr form)) (last-was-list? (list? first)))
  45. (if (null? ls)
  46. (display ")")
  47. (let* ((new-first (car ls))
  48. (this-is-list? (list? new-first)))
  49. (and (not last-was-list?)
  50. (not this-is-list?)
  51. (display " "))
  52. (write-punily new-first)
  53. (loop (cdr ls) this-is-list?))))))
  54. ((and (symbol? form)
  55. (let ((ls (string->list (symbol->string form))))
  56. (and (char=? (car ls) #\:)
  57. (not (memq #\space ls))
  58. (list->string (cdr ls)))))
  59. => (lambda (symbol-name-after-colon)
  60. (display #\:)
  61. (display symbol-name-after-colon)))
  62. (else (write form))))
  63. (define (punify-one file)
  64. (with-input-from-file file
  65. (lambda ()
  66. (let ((toke (lambda () (read (current-input-port)))))
  67. (let loop ((form (toke)))
  68. (or (eof-object? form)
  69. (begin
  70. (write-punily form)
  71. (loop (toke)))))))))
  72. (define (punify . args)
  73. (for-each punify-one args))
  74. (define main punify)
  75. ;;; punify ends here