spec.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ;;; Bytecode
  2. ;; Copyright (C) 2013, 2023 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (language bytecode spec)
  18. #:use-module (system base language)
  19. #:use-module (system vm loader)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (ice-9 binary-ports)
  22. #:use-module (srfi srfi-71)
  23. #:export (bytecode))
  24. (define (bytecode->value x e opts)
  25. (let ((thunk (load-thunk-from-memory
  26. (if (bytevector? x)
  27. x
  28. (let ((port get-bv (open-bytevector-output-port)))
  29. (x port)
  30. (close-port port)
  31. (get-bv))))))
  32. (if (eq? e (current-module))
  33. ;; save a cons in this case
  34. (values (thunk) e e)
  35. (save-module-excursion
  36. (lambda ()
  37. (set-current-module e)
  38. (values (thunk) e e))))))
  39. (define-language bytecode
  40. #:title "Bytecode"
  41. #:compilers `((value . ,bytecode->value))
  42. #:printer (lambda (bytecode port)
  43. (if (bytevector? bytecode)
  44. (put-bytevector port bytecode)
  45. (bytecode port)))
  46. #:reader get-bytevector-all
  47. #:for-humans? #f)