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)
49 set-lexical-binding-mode
55 #:export-syntax (defspecial prim))
57 ;;; This module provides runtime support for the Elisp front-end.
59 ;;; Values for t and nil. (FIXME remove this abstraction)
61 (define nil-value #nil)
65 (define make-lisp-string identity)
66 (define lisp-string? string?)
68 ;;; Modules for the binding slots.
69 ;;; Note: Naming those value-slot and/or function-slot clashes with the
70 ;;; submodules of these names!
72 (define value-slot-module (resolve-module '(elisp-symbols)))
74 (define function-slot-module (resolve-module '(elisp-functions)))
76 (define plist-slot-module (resolve-module '(elisp-plists)))
81 ;;; Routines for access to elisp dynamically bound symbols. This is
82 ;;; used for runtime access using functions like symbol-value or set,
83 ;;; where the symbol accessed might not be known at compile-time. These
84 ;;; always access the dynamic binding and can not be used for the
87 (define lexical-binding #t)
89 (define (lexical-binding?)
92 (define (set-lexical-binding-mode x)
93 (set! lexical-binding x))
95 (define unbound (make-symbol "unbound"))
97 (define dynamic? vector?)
98 (define (make-dynamic)
99 (vector #f 4 0 0 unbound))
100 (define (dynamic-ref x)
102 (define (dynamic-set! x v)
104 (define (dynamic-unset! x)
105 (vector-set! x 4 unbound))
106 (define (dynamic-bound? x)
107 (not (eq? (vector-ref x 4) unbound)))
108 (define (dynamic-bind x v thunk)
109 (let ((old (vector-ref x 4)))
111 (lambda () (vector-set! x 4 v))
113 (lambda () (vector-set! x 4 old)))))
115 (define-inlinable (ensure-present! module sym thunk)
116 (or (module-local-variable module sym)
117 (let ((variable (make-variable (thunk))))
118 (module-add! module sym variable)
121 (define-inlinable (ensure-desc! module sym)
122 (ensure-present! module
125 (let ((x (make-dynamic)))
126 (vector-set! x 0 sym)
129 (define-inlinable (schemify symbol)
135 (define (symbol-name symbol)
136 (symbol->string (schemify symbol)))
138 (define (symbol-desc symbol)
139 (let ((symbol (schemify symbol)))
140 (let ((module value-slot-module))
141 (variable-ref (ensure-desc! module symbol)))))
143 (define (ensure-dynamic! sym)
144 (vector-set! (symbol-desc sym) 3 1))
146 (define (symbol-dynamic symbol)
147 (ensure-dynamic! symbol)
148 (symbol-desc symbol))
150 (define (symbol-value symbol)
151 (dynamic-ref (symbol-desc symbol)))
153 (define (set-symbol-value! symbol value)
154 (dynamic-set! (symbol-desc symbol) value)
157 (define (symbol-function symbol)
158 (set! symbol (schemify symbol))
159 (ensure-present! function-slot-module symbol (lambda () #nil))
160 (let ((module function-slot-module))
161 (module-ref module symbol)))
163 (define (set-symbol-function! symbol value)
164 (set! symbol (schemify symbol))
165 (ensure-present! function-slot-module symbol (lambda () #nil))
166 (let ((module function-slot-module))
167 (module-define! module symbol value)
168 (module-export! module (list symbol)))
171 (define (symbol-plist symbol)
172 (set! symbol (schemify symbol))
173 (ensure-present! plist-slot-module symbol (lambda () #nil))
174 (let ((module plist-slot-module))
175 (module-ref module symbol)))
177 (define (set-symbol-plist! symbol value)
178 (set! symbol (schemify symbol))
179 (ensure-present! plist-slot-module symbol (lambda () #nil))
180 (let ((module plist-slot-module))
181 (module-define! module symbol value)
182 (module-export! module (list symbol)))
185 (define (symbol-bound? symbol)
186 (set! symbol (schemify symbol))
188 (module-bound? value-slot-module symbol)
189 (let ((var (module-variable value-slot-module
191 (and (variable-bound? var)
192 (if (dynamic? (variable-ref var))
193 (dynamic-bound? (variable-ref var))
196 (define (symbol-fbound? symbol)
197 (set! symbol (schemify symbol))
199 (module-bound? function-slot-module symbol)
201 (module-variable function-slot-module symbol))
202 (variable-ref (module-variable function-slot-module symbol))))
204 (define (bind-symbol symbol value thunk)
205 (dynamic-bind (symbol-desc symbol) value thunk))
207 (define (makunbound! symbol)
208 (if (module-bound? value-slot-module symbol)
209 (let ((var (module-variable value-slot-module
211 (if (and (variable-bound? var) (dynamic? (variable-ref var)))
212 (dynamic-unset! (variable-ref var))
213 (variable-unset! var))))
216 (define (fmakunbound! symbol)
217 (if (module-bound? function-slot-module symbol)
218 (variable-unset! (module-variable function-slot-module symbol)))
221 (define (special? sym)
222 (eqv? (vector-ref (symbol-desc sym) 3) 1))
224 (define (proclaim-special! sym)
225 (vector-set! (symbol-desc sym) 3 1)
228 (define (emacs! ref set boundp bind)
229 (set! symbol-value ref)
230 (set! set-symbol-value! set)
231 (set! symbol-bound? boundp)
232 (set! bind-symbol bind)
233 (set! lexical-binding? (lambda () (symbol-value 'lexical-binding)))
234 (set! set-lexical-binding-mode (lambda (x) (set-symbol-value! 'lexical-binding x))))
236 (define (eval-elisp form)
237 (compile form #:from 'elisp #:to 'value))
239 (set-symbol-value! nil_ #nil)
240 (set-symbol-value! t_ #t)
242 (define (make-string s) s)
244 ;;; Define a predefined macro for use in the function-slot module.
246 (define (make-id template-id . data)
247 (let ((append-symbols
250 (apply string-append (map symbol->string symbols))))))
251 (datum->syntax template-id
254 ((if (identifier? datum)
260 (define-syntax defspecial
263 ((_ name args body ...)
264 (with-syntax ((scheme-name (make-id #'name 'compile- #'name)))
265 #'(define scheme-name
266 (cons 'special-operator (lambda args body ...))))))))