write.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ;;; write.scm -- write Joy expressions.
  2. ;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
  3. ;;;
  4. ;;; Joy is free software; you can redistribute it and/or modify it under
  5. ;;; the terms of the GNU General Public License as published by the Free
  6. ;;; Software Foundation; either version 3 of the License, or (at your
  7. ;;; option) any later version.
  8. ;;;
  9. ;;; Joy is distributed in the hope that it will be useful, but WITHOUT
  10. ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. ;;; License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with Joy. If not, see <http://www.gnu.org/licenses/>.
  16. (define-module (language joy write)
  17. #:use-module (srfi srfi-1)
  18. #:use-module (ice-9 match)
  19. #:export (write-joy))
  20. (define* (write-joy expression #:optional (port (current-output-port)))
  21. "Write Joy expression EXPRESSION to port PORT."
  22. (define (print v)
  23. (match v
  24. ((? null? x)
  25. (display "[]"))
  26. ((? char? x)
  27. (display "'") (write-char x))
  28. (((? char? x) ...)
  29. (display "\"")
  30. (for-each write-char x)
  31. (display "\""))
  32. ((x xs ...)
  33. (display "[")
  34. (print x)
  35. (for-each (lambda (x)
  36. (display " ")
  37. (print x))
  38. xs)
  39. (display "]"))
  40. (else
  41. (display v))))
  42. (with-output-to-port port
  43. (lambda ()
  44. (print expression)
  45. (newline))))