spec.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; Language interface for Wisp in Guile
  2. ;; Copyright (C) 2005--2014 by David A. Wheeler and Alan Manuel K. Gloria
  3. ;; Copyright (C) 2014--2023 Arne Babenhauserheide.
  4. ;; Copyright (C) 2023 Maxime Devos <maximedevos@telenet.be>
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library 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 GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;; adapted from spec.scm: https://gitorious.org/nacre/guile-sweet/?p=nacre:guile-sweet.git;a=blob;f=sweet/spec.scm;hb=ae306867e371cb4b56e00bb60a50d9a0b8353109
  19. (define-module (language wisp spec)
  20. #:use-module (language wisp)
  21. #:use-module (system base compile)
  22. #:use-module (system base language)
  23. #:use-module (language scheme compile-tree-il)
  24. #:use-module (language scheme decompile-tree-il)
  25. #:export (wisp))
  26. ;;;
  27. ;;; Language definition
  28. ;;;
  29. (define (read-one-wisp-sexp port env)
  30. ;; Allow using "# foo" as #(foo).
  31. ;; Don't use the globally-acting read-hash-extend, because this
  32. ;; doesn't make much sense in parenthese-y (non-Wisp) Scheme.
  33. ;; Instead, use fluids to temporarily add the extension.
  34. (with-fluids ((%read-hash-procedures (fluid-ref %read-hash-procedures)))
  35. (read-hash-extend #\# (lambda args #\# ))
  36. ;; Read Wisp files as UTF-8, to support non-ASCII characters.
  37. ;; TODO: would be nice to support ';; coding: whatever' lines
  38. ;; like in parenthese-y Scheme.
  39. (set-port-encoding! port "UTF-8")
  40. (if (eof-object? (peek-char port))
  41. (read-char port) ; return eof: we’re done
  42. (let ((chunk (wisp-scheme-read-chunk port)))
  43. (and (not (null? chunk)) ; <---- XXX: maybe (pair? chunk)
  44. (car chunk))))))
  45. (define-language wisp
  46. #:title "Wisp Scheme Syntax. See SRFI-119 for details"
  47. ;; . #:reader read-one-wisp-sexp
  48. #:reader read-one-wisp-sexp ; : lambda (port env) : let ((x (read-one-wisp-sexp port env))) (display x)(newline) x ;
  49. #:compilers `((tree-il . ,compile-tree-il))
  50. #:decompilers `((tree-il . ,decompile-tree-il))
  51. #:evaluator (lambda (x module) (primitive-eval x))
  52. #:printer write ; TODO: backtransform to Wisp? Use source-properties?
  53. #:make-default-environment
  54. (lambda ()
  55. ;; Ideally we'd duplicate the whole module hierarchy so that `set!',
  56. ;; `fluid-set!', etc. don't have any effect in the current environment.
  57. (let ((m (make-fresh-user-module)))
  58. ;; Provide a separate `current-reader' fluid so that
  59. ;; compile-time changes to `current-reader' are
  60. ;; limited to the current compilation unit.
  61. (module-define! m 'current-reader (make-fluid))
  62. m)))