3 ;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (language elisp runtime)
22 #:use-module (ice-9 format)
23 #:use-module ((system base compile)
42 set-symbol-default-value!
52 set-lexical-binding-mode
58 #:export-syntax (defspecial prim))
60 ;;; This module provides runtime support for the Elisp front-end.
62 ;;; Values for t and nil. (FIXME remove this abstraction)
64 (define nil-value #nil)
68 (define make-lisp-string identity)
69 (define lisp-string? string?)
71 ;;; Modules for the binding slots.
72 ;;; Note: Naming those value-slot and/or function-slot clashes with the
73 ;;; submodules of these names!
75 (define value-slot-module (resolve-module '(elisp-symbols)))
77 (define function-slot-module (resolve-module '(elisp-functions)))
79 (define plist-slot-module (resolve-module '(elisp-plists)))
84 ;;; Routines for access to elisp dynamically bound symbols. This is
85 ;;; used for runtime access using functions like symbol-value or set,
86 ;;; where the symbol accessed might not be known at compile-time. These
87 ;;; always access the dynamic binding and can not be used for the
90 (define lexical-binding #t)
92 (define (lexical-binding?)
95 (define (set-lexical-binding-mode x)
96 (set! lexical-binding x))
98 (define unbound (make-symbol "unbound"))
100 (define dynamic? vector?)
101 (define (make-dynamic)
102 (vector #f 4 0 0 unbound))
103 (define (dynamic-ref x)
105 (define (dynamic-set! x v)
107 (define (dynamic-unset! x)
108 (vector-set! x 4 unbound))
109 (define (dynamic-bound? x)
110 (not (eq? (vector-ref x 4) unbound)))
111 (define (dynamic-bind x v thunk)
112 (let ((old (vector-ref x 4)))
114 (lambda () (vector-set! x 4 v))
116 (lambda () (vector-set! x 4 old)))))
118 (define-inlinable (ensure-present! module sym thunk)
119 (or (module-local-variable module sym)
120 (let ((variable (make-variable (thunk))))
121 (module-add! module sym variable)
124 (define-inlinable (ensure-desc! module sym)
125 (ensure-present! module
128 (let ((x (make-dynamic)))
129 (vector-set! x 0 sym)
132 (define-inlinable (schemify symbol)
138 (define (symbol-name symbol)
139 (symbol->string (schemify symbol)))
141 (define (symbol-desc symbol)
142 (let ((symbol (schemify symbol)))
143 (let ((module value-slot-module))
144 (variable-ref (ensure-desc! module symbol)))))
146 (define (ensure-dynamic! sym)
147 (vector-set! (symbol-desc sym) 3 1))
149 (define (symbol-dynamic symbol)
150 (ensure-dynamic! symbol)
151 (symbol-desc symbol))
153 (define (symbol-value symbol)
154 (dynamic-ref (symbol-desc symbol)))
156 (define (set-symbol-value! symbol value)
157 (dynamic-set! (symbol-desc symbol) value)
160 (define (symbol-function symbol)
161 (set! symbol (schemify symbol))
162 (ensure-present! function-slot-module symbol (lambda () #nil))
163 (let ((module function-slot-module))
164 (module-ref module symbol)))
166 (define (set-symbol-function! symbol value)
167 (set! symbol (schemify symbol))
168 (ensure-present! function-slot-module symbol (lambda () #nil))
169 (let ((module function-slot-module))
170 (module-define! module symbol value)
171 (module-export! module (list symbol)))
174 (define (symbol-plist symbol)
175 (set! symbol (schemify symbol))
176 (ensure-present! plist-slot-module symbol (lambda () #nil))
177 (let ((module plist-slot-module))
178 (module-ref module symbol)))
180 (define (set-symbol-plist! symbol value)
181 (set! symbol (schemify symbol))
182 (ensure-present! plist-slot-module symbol (lambda () #nil))
183 (let ((module plist-slot-module))
184 (module-define! module symbol value)
185 (module-export! module (list symbol)))
188 (define (symbol-bound? symbol)
189 (set! symbol (schemify symbol))
191 (module-bound? value-slot-module symbol)
192 (let ((var (module-variable value-slot-module
194 (and (variable-bound? var)
195 (if (dynamic? (variable-ref var))
196 (dynamic-bound? (variable-ref var))
199 (define symbol-default-bound? symbol-bound?)
201 (define symbol-default-value symbol-value)
203 (define set-symbol-default-value! set-symbol-value!)
205 (define (symbol-fbound? symbol)
206 (set! symbol (schemify symbol))
208 (module-bound? function-slot-module symbol)
210 (module-variable function-slot-module symbol))
211 (variable-ref (module-variable function-slot-module symbol))))
213 (define (bind-symbol symbol value thunk)
214 (dynamic-bind (symbol-desc symbol) value thunk))
216 (define (makunbound! symbol)
217 (if (module-bound? value-slot-module symbol)
218 (let ((var (module-variable value-slot-module
220 (if (and (variable-bound? var) (dynamic? (variable-ref var)))
221 (dynamic-unset! (variable-ref var))
222 (variable-unset! var))))
225 (define (fmakunbound! symbol)
226 (if (module-bound? function-slot-module symbol)
227 (variable-unset! (module-variable function-slot-module symbol)))
230 (define (special? sym)
231 (eqv? (vector-ref (symbol-desc sym) 3) 1))
233 (define (proclaim-special! sym)
234 (vector-set! (symbol-desc sym) 3 1)
237 (define (emacs! ref set boundp dref dset dboundp bind)
238 (set! symbol-value ref)
239 (set! set-symbol-value! set)
240 (set! symbol-bound? boundp)
241 (set! symbol-default-value dref)
242 (set! set-symbol-default-value! dset)
243 (set! symbol-default-bound? dboundp)
244 (set! bind-symbol bind)
245 (set! lexical-binding? (lambda () (symbol-value 'lexical-binding)))
246 (set! set-lexical-binding-mode (lambda (x) (set-symbol-value! 'lexical-binding x))))
248 (define (eval-elisp form)
249 (compile form #:from 'elisp #:to 'value))
251 (set-symbol-value! nil_ #nil)
252 (set-symbol-value! t_ #t)
254 (define (make-string s) s)
256 ;;; Define a predefined macro for use in the function-slot module.
258 (define (make-id template-id . data)
259 (let ((append-symbols
262 (apply string-append (map symbol->string symbols))))))
263 (datum->syntax template-id
266 ((if (identifier? datum)
272 (define-syntax defspecial
275 ((_ name args body ...)
276 (with-syntax ((scheme-name (make-id #'name 'compile- #'name)))
277 #'(define scheme-name
278 (cons 'special-operator (lambda args body ...))))))))