spec.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;; Language interface for Wisp in Guile
  2. ;;
  3. ;; -Author: Arne Babenhauserheide
  4. ; adapted from guile-sweet: https://gitorious.org/nacre/guile-sweet/source/ae306867e371cb4b56e00bb60a50d9a0b8353109:sweet/common.scm
  5. ; adapted from spec.scm: https://gitorious.org/nacre/guile-sweet/source/ae306867e371cb4b56e00bb60a50d9a0b8353109:sweet/spec.scm
  6. (define-module (language wisp spec)
  7. ; . #:use-module : wisp
  8. #:use-module (language wisp)
  9. #:use-module (system base compile)
  10. #:use-module (system base language)
  11. #:use-module (language scheme compile-tree-il)
  12. #:use-module (language scheme decompile-tree-il)
  13. #:export (wisp))
  14. ; Set locale to something which supports unicode. Required to avoid using fluids.
  15. (catch #t
  16. (lambda ()
  17. (setlocale LC_ALL ""))
  18. (lambda (key . parameters)
  19. (let ((locale-fallback "en_US.UTF-8"))
  20. (format (current-error-port)
  21. (string-join
  22. (list "Warning: setlocale LC_ALL \"\" failed with ~A: ~A"
  23. "using explicit ~A locale. Please setup your locale.\n")
  24. "\n ")
  25. key parameters locale-fallback)
  26. (setlocale LC_ALL locale-fallback))))
  27. ;;;
  28. ;;; Language definition
  29. ;;;
  30. (define wisp-pending-sexps (list))
  31. (define (read-one-wisp-sexp port env)
  32. ;; allow using "# foo" as #(foo).
  33. (read-hash-extend #\# (λ (chr port) #\#))
  34. (cond
  35. ((eof-object? (peek-char port))
  36. (read-char port )); return eof: we’re done
  37. (else
  38. (let ((chunk (wisp-scheme-read-chunk port)))
  39. (cond
  40. ((not (null? chunk))
  41. (car chunk))
  42. (else
  43. #f))))))
  44. (define-language wisp
  45. #:title "Wisp Scheme Syntax. See SRFI-119 for details. THIS IS EXPERIMENTAL, USE AT YOUR OWN RISK"
  46. ; . #:reader read-one-wisp-sexp
  47. #:reader read-one-wisp-sexp ; : lambda (port env) : let ((x (read-one-wisp-sexp port env))) (display x)(newline) x ;
  48. #:compilers `((tree-il . ,compile-tree-il))
  49. #:decompilers `((tree-il . ,decompile-tree-il))
  50. #:evaluator (lambda (x module) (primitive-eval x))
  51. #:printer write ; TODO: backtransform to wisp? Use source-properties?
  52. #:make-default-environment
  53. (lambda ()
  54. ;; Ideally we'd duplicate the whole module hierarchy so that `set!',
  55. ;; `fluid-set!', etc. don't have any effect in the current environment.
  56. (let ((m (make-fresh-user-module)))
  57. ;; Provide a separate `current-reader' fluid so that
  58. ;; compile-time changes to `current-reader' are
  59. ;; limited to the current compilation unit.
  60. (module-define! m 'current-reader (make-fluid))
  61. ;; Default to `simple-format', as is the case until
  62. ;; (ice-9 format) is loaded. This allows
  63. ;; compile-time warnings to be emitted when using
  64. ;; unsupported options.
  65. (module-set! m 'format simple-format)
  66. m)))