1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
;;; Guile Emacs Lisp
;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Code:
(define-module (language elisp runtime)
#:use-module (ice-9 format)
#:use-module ((system base compile)
#:select (compile))
#:export (nil-value
t-value
value-slot-module
function-slot-module
elisp-bool
ensure-dynamic!
symbol-name
symbol-value
set-symbol-value!
symbol-function
set-symbol-function!
symbol-plist
set-symbol-plist!
symbol-bound?
symbol-fbound?
symbol-default-bound?
symbol-default-value
set-symbol-default-value!
bind-symbol
makunbound!
fmakunbound!
symbol-desc
proclaim-special!
special?
emacs!
unbound
lexical-binding?
set-lexical-binding-mode
log!
eval-elisp
compile-elisp
local-eval-elisp
make-lisp-string
lisp-string?)
#:export-syntax (defspecial prim))
;;; This module provides runtime support for the Elisp front-end.
;;; Values for t and nil. (FIXME remove this abstraction)
(define nil-value #nil)
(define t-value #t)
(define make-lisp-string identity)
(define lisp-string? string?)
;;; Modules for the binding slots.
;;; Note: Naming those value-slot and/or function-slot clashes with the
;;; submodules of these names!
(define value-slot-module (define-module* '(elisp-symbols) #:pure #t))
(define function-slot-module (define-module* '(elisp-functions) #:pure #t))
(define plist-slot-module (define-module* '(elisp-plists) #:pure #t))
(define nil_ 'nil)
(define t_ 't)
;;; Routines for access to elisp dynamically bound symbols. This is
;;; used for runtime access using functions like symbol-value or set,
;;; where the symbol accessed might not be known at compile-time. These
;;; always access the dynamic binding and can not be used for the
;;; lexical!
(define lexical-binding #t)
(define (lexical-binding?)
lexical-binding)
(define (set-lexical-binding-mode x)
(set! lexical-binding x))
(define unbound (make-symbol "unbound"))
(define dynamic? vector?)
(define (make-dynamic)
(vector #f 4 0 0 unbound))
(define (dynamic-ref x)
(vector-ref x 4))
(define (dynamic-set! x v)
(vector-set! x 4 v))
(define (dynamic-unset! x)
(vector-set! x 4 unbound))
(define (dynamic-bound? x)
(not (eq? (vector-ref x 4) unbound)))
(define (dynamic-bind x v thunk)
(let ((old (vector-ref x 4)))
(dynamic-wind
(lambda () (vector-set! x 4 v))
thunk
(lambda () (vector-set! x 4 old)))))
(define-inlinable (ensure-present! module sym thunk)
(or (module-local-variable module sym)
(let ((variable (make-variable (thunk))))
(module-add! module sym variable)
variable)))
(define-inlinable (ensure-desc! module sym)
(ensure-present! module
sym
(lambda ()
(let ((x (make-dynamic)))
(vector-set! x 0 sym)
x))))
(define-inlinable (schemify symbol)
(case symbol
((#nil) nil_)
((#t) t_)
(else symbol)))
(define (symbol-name symbol)
(symbol->string (schemify symbol)))
(define (symbol-desc symbol)
(let ((symbol (schemify symbol)))
(let ((module value-slot-module))
(variable-ref (ensure-desc! module symbol)))))
(define (ensure-dynamic! sym)
(vector-set! (symbol-desc sym) 3 1))
(define (symbol-dynamic symbol)
(ensure-dynamic! symbol)
(symbol-desc symbol))
(define (symbol-value symbol)
(dynamic-ref (symbol-desc symbol)))
(define (set-symbol-value! symbol value)
(dynamic-set! (symbol-desc symbol) value)
value)
(define (symbol-function symbol)
(cond
((module-variable function-slot-module (schemify symbol))
=> variable-ref)
(else #nil)))
(define (set-symbol-function! symbol value)
(set! symbol (schemify symbol))
(ensure-present! function-slot-module symbol (lambda () #nil))
(let ((module function-slot-module))
(module-define! module symbol value)
(module-export! module (list symbol)))
value)
(define (symbol-plist symbol)
(set! symbol (schemify symbol))
(ensure-present! plist-slot-module symbol (lambda () #nil))
(let ((module plist-slot-module))
(module-ref module symbol)))
(define (set-symbol-plist! symbol value)
(set! symbol (schemify symbol))
(ensure-present! plist-slot-module symbol (lambda () #nil))
(let ((module plist-slot-module))
(module-define! module symbol value)
(module-export! module (list symbol)))
value)
(define (symbol-bound? symbol)
(set! symbol (schemify symbol))
(and
(module-bound? value-slot-module symbol)
(let ((var (module-variable value-slot-module
symbol)))
(and (variable-bound? var)
(if (dynamic? (variable-ref var))
(dynamic-bound? (variable-ref var))
#t)))))
(define symbol-default-bound? symbol-bound?)
(define symbol-default-value symbol-value)
(define set-symbol-default-value! set-symbol-value!)
(define (symbol-fbound? symbol)
(set! symbol (schemify symbol))
(and
(module-bound? function-slot-module symbol)
(variable-bound?
(module-variable function-slot-module symbol))
(variable-ref (module-variable function-slot-module symbol))))
(define (bind-symbol symbol value thunk)
(dynamic-bind (symbol-desc symbol) value thunk))
(define (makunbound! symbol)
(if (module-bound? value-slot-module symbol)
(let ((var (module-variable value-slot-module
symbol)))
(if (and (variable-bound? var) (dynamic? (variable-ref var)))
(dynamic-unset! (variable-ref var))
(variable-unset! var))))
symbol)
(define (fmakunbound! symbol)
(if (module-bound? function-slot-module symbol)
(variable-unset! (module-variable function-slot-module symbol)))
symbol)
(define (special? sym)
(eqv? (vector-ref (symbol-desc sym) 3) 1))
(define (proclaim-special! sym)
(vector-set! (symbol-desc sym) 3 1)
#nil)
(define (emacs! ref set boundp dref dset dboundp bind)
(set! symbol-value ref)
(set! set-symbol-value! set)
(set! symbol-bound? boundp)
(set! symbol-default-value dref)
(set! set-symbol-default-value! dset)
(set! symbol-default-bound? dboundp)
(set! bind-symbol bind)
(set! lexical-binding? (lambda () (symbol-value 'lexical-binding)))
(set! set-lexical-binding-mode (lambda (x) (set-symbol-value! 'lexical-binding x))))
(define (eval-elisp form)
(compile form #:from 'elisp #:to 'value))
(define (compile-elisp form)
(compile (compile form #:from 'elisp #:to 'bytecode)
#:from 'bytecode #:to 'value))
(set-symbol-value! nil_ #nil)
(set-symbol-value! t_ #t)
(define (make-string s) s)
;;; Define a predefined macro for use in the function-slot module.
(define (make-id template-id . data)
(let ((append-symbols
(lambda (symbols)
(string->symbol
(apply string-append (map symbol->string symbols))))))
(datum->syntax template-id
(append-symbols
(map (lambda (datum)
((if (identifier? datum)
syntax->datum
identity)
datum))
data)))))
(define-syntax defspecial
(lambda (x)
(syntax-case x ()
((_ name args body ...)
(with-syntax ((scheme-name (make-id #'name 'compile- #'name)))
#'(begin
(define scheme-name
(cons 'special-operator (lambda args body ...)))
(set-symbol-function! 'name scheme-name)))))))
|