runtime.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; Guile Emacs Lisp
  2. ;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public
  6. ;;; License as published by the Free Software Foundation; either
  7. ;;; version 3 of the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this library; if not, write to the Free Software
  16. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Code:
  18. (define-module (language elisp runtime)
  19. #:export (nil-value
  20. t-value
  21. value-slot-module
  22. function-slot-module
  23. elisp-bool
  24. ensure-fluid!
  25. symbol-fluid
  26. set-symbol-fluid!
  27. symbol-value
  28. set-symbol-value!
  29. symbol-function
  30. set-symbol-function!
  31. symbol-bound?
  32. symbol-fbound?
  33. makunbound!
  34. fmakunbound!)
  35. #:export-syntax (defspecial prim))
  36. ;;; This module provides runtime support for the Elisp front-end.
  37. ;;; Values for t and nil. (FIXME remove this abstraction)
  38. (define nil-value #nil)
  39. (define t-value #t)
  40. ;;; Modules for the binding slots.
  41. ;;; Note: Naming those value-slot and/or function-slot clashes with the
  42. ;;; submodules of these names!
  43. (define value-slot-module '(language elisp runtime value-slot))
  44. (define function-slot-module '(language elisp runtime function-slot))
  45. ;;; Routines for access to elisp dynamically bound symbols. This is
  46. ;;; used for runtime access using functions like symbol-value or set,
  47. ;;; where the symbol accessed might not be known at compile-time. These
  48. ;;; always access the dynamic binding and can not be used for the
  49. ;;; lexical!
  50. (define (ensure-fluid! module sym)
  51. (let ((intf (resolve-interface module))
  52. (resolved (resolve-module module)))
  53. (if (not (module-defined? intf sym))
  54. (let ((fluid (make-unbound-fluid)))
  55. (module-define! resolved sym fluid)
  56. (module-export! resolved `(,sym))))))
  57. (define (symbol-fluid symbol)
  58. (let ((module (resolve-module value-slot-module)))
  59. (ensure-fluid! value-slot-module symbol) ;++ implicit special proclamation
  60. (module-ref module symbol)))
  61. (define (set-symbol-fluid! symbol fluid)
  62. (let ((module (resolve-module value-slot-module)))
  63. (module-define! module symbol fluid)
  64. (module-export! module (list symbol)))
  65. fluid)
  66. (define (symbol-value symbol)
  67. (fluid-ref (symbol-fluid symbol)))
  68. (define (set-symbol-value! symbol value)
  69. (fluid-set! (symbol-fluid symbol) value)
  70. value)
  71. (define (symbol-function symbol)
  72. (let ((module (resolve-module function-slot-module)))
  73. (module-ref module symbol)))
  74. (define (set-symbol-function! symbol value)
  75. (let ((module (resolve-module function-slot-module)))
  76. (module-define! module symbol value)
  77. (module-export! module (list symbol)))
  78. value)
  79. (define (symbol-bound? symbol)
  80. (and
  81. (module-bound? (resolve-interface value-slot-module) symbol)
  82. (let ((var (module-variable (resolve-module value-slot-module)
  83. symbol)))
  84. (and (variable-bound? var)
  85. (if (fluid? (variable-ref var))
  86. (fluid-bound? (variable-ref var))
  87. #t)))))
  88. (define (symbol-fbound? symbol)
  89. (and
  90. (module-bound? (resolve-interface function-slot-module) symbol)
  91. (variable-bound?
  92. (module-variable (resolve-module function-slot-module)
  93. symbol))))
  94. (define (makunbound! symbol)
  95. (if (module-bound? (resolve-interface value-slot-module) symbol)
  96. (let ((var (module-variable (resolve-module value-slot-module)
  97. symbol)))
  98. (if (and (variable-bound? var) (fluid? (variable-ref var)))
  99. (fluid-unset! (variable-ref var))
  100. (variable-unset! var))))
  101. symbol)
  102. (define (fmakunbound! symbol)
  103. (if (module-bound? (resolve-interface function-slot-module) symbol)
  104. (variable-unset! (module-variable
  105. (resolve-module function-slot-module)
  106. symbol)))
  107. symbol)
  108. ;;; Define a predefined macro for use in the function-slot module.
  109. (define (make-id template-id . data)
  110. (let ((append-symbols
  111. (lambda (symbols)
  112. (string->symbol
  113. (apply string-append (map symbol->string symbols))))))
  114. (datum->syntax template-id
  115. (append-symbols
  116. (map (lambda (datum)
  117. ((if (identifier? datum)
  118. syntax->datum
  119. identity)
  120. datum))
  121. data)))))
  122. (define-syntax defspecial
  123. (lambda (x)
  124. (syntax-case x ()
  125. ((_ name args body ...)
  126. (with-syntax ((scheme-name (make-id #'name 'compile- #'name)))
  127. #'(define scheme-name
  128. (cons 'special-operator (lambda args body ...))))))))