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