punify 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/sh
  2. # aside from this initial boilerplate, this is actually -*- scheme -*- code
  3. main='(module-ref (resolve-module '\''(scripts punify)) '\'main')'
  4. exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
  5. !#
  6. ;;; punify --- Display Scheme code w/o unnecessary comments / whitespace
  7. ;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  8. ;;
  9. ;; This program is free software; you can redistribute it and/or
  10. ;; modify it under the terms of the GNU General Public License as
  11. ;; published by the Free Software Foundation; either version 2, or
  12. ;; (at your option) any later version.
  13. ;;
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. ;; General Public License for more details.
  18. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this software; see the file COPYING. If not, write to
  21. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301 USA
  23. ;;; Author: Thien-Thi Nguyen
  24. ;;; Commentary:
  25. ;; Usage: punify FILE1 FILE2 ...
  26. ;;
  27. ;; Each file's forms are read and written to stdout.
  28. ;; The effect is to remove comments and much non-essential whitespace.
  29. ;; This is useful when installing Scheme source to space-limited media.
  30. ;;
  31. ;; Example:
  32. ;; $ wc ./punify ; ./punify ./punify | wc
  33. ;; 89 384 3031 ./punify
  34. ;; 0 42 920
  35. ;;
  36. ;; TODO: Read from stdin.
  37. ;; Handle vectors.
  38. ;; Identifier punification.
  39. ;;; Code:
  40. (define-module (scripts punify)
  41. :export (punify))
  42. (define (write-punily form)
  43. (cond ((and (list? form) (not (null? form)))
  44. (let ((first (car form)))
  45. (display "(")
  46. (write-punily first)
  47. (let loop ((ls (cdr form)) (last-was-list? (list? first)))
  48. (if (null? ls)
  49. (display ")")
  50. (let* ((new-first (car ls))
  51. (this-is-list? (list? new-first)))
  52. (and (not last-was-list?)
  53. (not this-is-list?)
  54. (display " "))
  55. (write-punily new-first)
  56. (loop (cdr ls) this-is-list?))))))
  57. ((and (symbol? form)
  58. (let ((ls (string->list (symbol->string form))))
  59. (and (char=? (car ls) #\:)
  60. (not (memq #\space ls))
  61. (list->string (cdr ls)))))
  62. => (lambda (symbol-name-after-colon)
  63. (display #\:)
  64. (display symbol-name-after-colon)))
  65. (else (write form))))
  66. (define (punify-one file)
  67. (with-input-from-file file
  68. (lambda ()
  69. (let ((toke (lambda () (read (current-input-port)))))
  70. (let loop ((form (toke)))
  71. (or (eof-object? form)
  72. (begin
  73. (write-punily form)
  74. (loop (toke)))))))))
  75. (define (punify . args)
  76. (for-each punify-one args))
  77. (define main punify)
  78. ;;; punify ends here