spec.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ;;; spec.scm -- Guile language specification for Joy's REPL.
  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. ;;; Commentary:
  17. ;;;
  18. ;;; This pseudo-language provides support for running Joy expressions
  19. ;;; interactively, by automatically applying them to a global stack.
  20. ;;;
  21. ;;; Code:
  22. (define-module (language joy-repl spec)
  23. #:use-module (system base language)
  24. #:use-module (language tree-il)
  25. #:use-module (language joy parser)
  26. #:use-module (language joy compile-tree-il)
  27. #:export (joy-repl compile-repl-tree-il))
  28. (define %joy-stack (make-parameter '()))
  29. ;;; Guile 2.2 changed the external representation for a procedure call
  30. ;;; from 'apply' to 'call'.
  31. (define call
  32. (if (and (>= (string->number (major-version)) 2)
  33. (>= (string->number (minor-version)) 2))
  34. 'call
  35. 'apply))
  36. (define (compile-repl-tree-il expr env opts)
  37. (call-with-values
  38. (lambda () (compile-tree-il* expr env opts))
  39. (lambda (op env cenv)
  40. (values
  41. (parse-tree-il
  42. `(begin
  43. (,call (@@ (language joy-repl spec) %joy-stack)
  44. (,call (toplevel apply)
  45. ,op
  46. (,call (@@ (language joy-repl spec) %joy-stack))))
  47. (,call (@@ (language joy-repl spec) %joy-stack))))
  48. env cenv))))
  49. (define-language joy-repl
  50. #:title "Joy (REPL)"
  51. #:reader (lambda (port env) (parse-joy* port #f))
  52. #:compilers `((tree-il . ,compile-repl-tree-il))
  53. #:printer write
  54. #:make-default-environment
  55. (lambda () (%joy-stack '())))