Commit | Line | Data |
---|---|---|
dfbc6e9d | 1 | ;;; Guile Emacs Lisp |
51248e6e | 2 | |
7081d4f9 | 3 | ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. |
51248e6e DK |
4 | |
5 | ;; This program is free software; you can redistribute it and/or modify | |
6 | ;; it under the terms of the GNU General Public License as published by | |
e4257331 | 7 | ;; the Free Software Foundation; either version 3, or (at your option) |
51248e6e | 8 | ;; any later version. |
abcf4a9e | 9 | ;; |
51248e6e DK |
10 | ;; This program 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 | |
13 | ;; GNU General Public License for more details. | |
abcf4a9e | 14 | ;; |
51248e6e DK |
15 | ;; You should have received a copy of the GNU General Public License |
16 | ;; along with this program; see the file COPYING. If not, write to | |
17 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | ;; Boston, MA 02111-1307, USA. | |
19 | ||
20 | ;;; Code: | |
21 | ||
22 | (define-module (language elisp compile-tree-il) | |
5d221ca3 | 23 | #:use-module (language elisp bindings) |
44ae163d | 24 | #:use-module (language elisp runtime) |
51248e6e DK |
25 | #:use-module (language tree-il) |
26 | #:use-module (system base pmatch) | |
74c009da | 27 | #:use-module (system base compile) |
dfbc6e9d | 28 | #:use-module (srfi srfi-1) |
eda83f0a BT |
29 | #:use-module (srfi srfi-8) |
30 | #:use-module (srfi srfi-11) | |
31 | #:use-module (srfi srfi-26) | |
44ae163d BT |
32 | #:export (compile-tree-il |
33 | compile-progn | |
80687f2e | 34 | compile-eval-when-compile |
44ae163d BT |
35 | compile-if |
36 | compile-defconst | |
37 | compile-defvar | |
38 | compile-setq | |
39 | compile-let | |
44ae163d | 40 | compile-flet |
1c2f9636 | 41 | compile-labels |
44ae163d | 42 | compile-let* |
44ae163d BT |
43 | compile-guile-ref |
44 | compile-guile-primitive | |
44ae163d BT |
45 | compile-function |
46 | compile-defmacro | |
47 | compile-defun | |
0dbfdeef | 48 | #{compile-`}# |
03e00c5c | 49 | compile-quote |
d273b826 | 50 | compile-%funcall |
03e00c5c | 51 | compile-%set-lexical-binding-mode)) |
51248e6e | 52 | |
c983a199 BT |
53 | ;;; Certain common parameters (like the bindings data structure or |
54 | ;;; compiler options) are not always passed around but accessed using | |
55 | ;;; fluids to simulate dynamic binding (hey, this is about elisp). | |
a90d9c85 | 56 | |
c983a199 BT |
57 | ;;; The bindings data structure to keep track of symbol binding related |
58 | ;;; data. | |
abcf4a9e | 59 | |
a90d9c85 DK |
60 | (define bindings-data (make-fluid)) |
61 | ||
03e00c5c | 62 | (define lexical-binding (make-fluid)) |
a90d9c85 | 63 | |
c983a199 BT |
64 | ;;; Find the source properties of some parsed expression if there are |
65 | ;;; any associated with it. | |
51248e6e DK |
66 | |
67 | (define (location x) | |
68 | (and (pair? x) | |
69 | (let ((props (source-properties x))) | |
70 | (and (not (null? props)) | |
71 | props)))) | |
72 | ||
c983a199 | 73 | ;;; Values to use for Elisp's nil and t. |
4530432e | 74 | |
f4e5e411 BT |
75 | (define (nil-value loc) |
76 | (make-const loc (@ (language elisp runtime) nil-value))) | |
4530432e | 77 | |
f4e5e411 BT |
78 | (define (t-value loc) |
79 | (make-const loc (@ (language elisp runtime) t-value))) | |
4530432e | 80 | |
c983a199 | 81 | ;;; Modules that contain the value and function slot bindings. |
344927c3 DK |
82 | |
83 | (define runtime '(language elisp runtime)) | |
abcf4a9e | 84 | |
37099846 | 85 | (define value-slot (@ (language elisp runtime) value-slot-module)) |
344927c3 | 86 | |
abcf4a9e | 87 | (define function-slot (@ (language elisp runtime) function-slot-module)) |
344927c3 | 88 | |
c983a199 BT |
89 | ;;; The backquoting works the same as quasiquotes in Scheme, but the |
90 | ;;; forms are named differently; to make easy adaptions, we define these | |
91 | ;;; predicates checking for a symbol being the car of an | |
92 | ;;; unquote/unquote-splicing/backquote form. | |
9b5ff6a6 | 93 | |
9b5ff6a6 | 94 | (define (unquote? sym) |
0dbfdeef | 95 | (and (symbol? sym) (eq? sym '#{,}#))) |
9b5ff6a6 DK |
96 | |
97 | (define (unquote-splicing? sym) | |
0dbfdeef | 98 | (and (symbol? sym) (eq? sym '#{,@}#))) |
9b5ff6a6 | 99 | |
c983a199 | 100 | ;;; Build a call to a primitive procedure nicely. |
50abfe76 DK |
101 | |
102 | (define (call-primitive loc sym . args) | |
a881a4ae | 103 | (make-primcall loc sym args)) |
50abfe76 | 104 | |
c983a199 BT |
105 | ;;; Error reporting routine for syntax/compilation problems or build |
106 | ;;; code for a runtime-error output. | |
344927c3 DK |
107 | |
108 | (define (report-error loc . args) | |
109 | (apply error args)) | |
110 | ||
eaeda0d5 BT |
111 | (define (access-variable loc symbol handle-lexical handle-dynamic) |
112 | (cond | |
113 | ((get-lexical-binding (fluid-ref bindings-data) symbol) | |
114 | => handle-lexical) | |
115 | (else | |
116 | (handle-dynamic)))) | |
344927c3 | 117 | |
eaeda0d5 | 118 | (define (reference-variable loc symbol) |
f4e5e411 BT |
119 | (access-variable |
120 | loc | |
eaeda0d5 BT |
121 | symbol |
122 | (lambda (lexical) | |
123 | (make-lexical-ref loc lexical lexical)) | |
f4e5e411 | 124 | (lambda () |
f4e5e411 BT |
125 | (call-primitive loc |
126 | 'fluid-ref | |
eaeda0d5 | 127 | (make-module-ref loc value-slot symbol #t))))) |
344927c3 | 128 | |
66be42cb BT |
129 | (define (global? module symbol) |
130 | (module-variable module symbol)) | |
131 | ||
132 | (define (ensure-globals! loc names body) | |
eaeda0d5 BT |
133 | (if (and (every (cut global? (resolve-module value-slot) <>) names) |
134 | (every symbol-interned? names)) | |
66be42cb | 135 | body |
5ddd9645 | 136 | (list->seq |
66be42cb BT |
137 | loc |
138 | `(,@(map | |
139 | (lambda (name) | |
140 | (ensure-fluid! value-slot name) | |
5ddd9645 BT |
141 | (make-call loc |
142 | (make-module-ref loc runtime 'ensure-fluid! #t) | |
143 | (list (make-const loc value-slot) | |
144 | (make-const loc name)))) | |
66be42cb BT |
145 | names) |
146 | ,body)))) | |
147 | ||
eaeda0d5 | 148 | (define (set-variable! loc symbol value) |
f4e5e411 BT |
149 | (access-variable |
150 | loc | |
eaeda0d5 BT |
151 | symbol |
152 | (lambda (lexical) | |
153 | (make-lexical-set loc lexical lexical value)) | |
c6920dc8 | 154 | (lambda () |
eaeda0d5 | 155 | (ensure-globals! |
c6920dc8 | 156 | loc |
eaeda0d5 BT |
157 | (list symbol) |
158 | (call-primitive loc | |
159 | 'fluid-set! | |
160 | (make-module-ref loc value-slot symbol #t) | |
161 | value))))) | |
344927c3 | 162 | |
eaeda0d5 BT |
163 | (define (access-function loc symbol handle-lexical handle-global) |
164 | (cond | |
165 | ((get-function-binding (fluid-ref bindings-data) symbol) | |
166 | => handle-lexical) | |
167 | (else | |
168 | (handle-global)))) | |
344927c3 | 169 | |
eaeda0d5 BT |
170 | (define (reference-function loc symbol) |
171 | (access-function | |
172 | loc | |
173 | symbol | |
174 | (lambda (gensym) (make-lexical-ref loc symbol gensym)) | |
175 | (lambda () (make-module-ref loc function-slot symbol #t)))) | |
176 | ||
177 | (define (set-function! loc symbol value) | |
178 | (access-function | |
f4e5e411 | 179 | loc |
eaeda0d5 BT |
180 | symbol |
181 | (lambda (gensym) (make-lexical-set loc symbol gensym value)) | |
c6920dc8 | 182 | (lambda () |
7081d4f9 | 183 | (make-call |
c6920dc8 | 184 | loc |
eaeda0d5 BT |
185 | (make-module-ref loc runtime 'set-symbol-function! #t) |
186 | (list (make-const loc symbol) value))))) | |
344927c3 | 187 | |
f6e0a4a6 | 188 | (define (bind-lexically? sym module decls) |
9083c48d | 189 | (or (eq? module function-slot) |
f6e0a4a6 BT |
190 | (let ((decl (assq-ref decls sym))) |
191 | (and (equal? module value-slot) | |
192 | (or | |
193 | (eq? decl 'lexical) | |
194 | (and | |
195 | (fluid-ref lexical-binding) | |
66be42cb | 196 | (not (global? (resolve-module module) sym)))))))) |
a6a5cf03 | 197 | |
0e5b7e74 BT |
198 | (define (parse-let-binding loc binding) |
199 | (pmatch binding | |
200 | ((unquote var) | |
201 | (guard (symbol? var)) | |
202 | (cons var #nil)) | |
203 | ((,var) | |
204 | (guard (symbol? var)) | |
205 | (cons var #nil)) | |
206 | ((,var ,val) | |
207 | (guard (symbol? var)) | |
208 | (cons var val)) | |
209 | (else | |
210 | (report-error loc "malformed variable binding" binding)))) | |
211 | ||
212 | (define (parse-flet-binding loc binding) | |
213 | (pmatch binding | |
214 | ((,var ,args . ,body) | |
215 | (guard (symbol? var)) | |
216 | (cons var `(function (lambda ,args ,@body)))) | |
217 | (else | |
218 | (report-error loc "malformed function binding" binding)))) | |
219 | ||
805b8211 BT |
220 | (define (parse-declaration expr) |
221 | (pmatch expr | |
222 | ((lexical . ,vars) | |
223 | (map (cut cons <> 'lexical) vars)) | |
805b8211 BT |
224 | (else |
225 | '()))) | |
226 | ||
227 | (define (parse-body-1 body lambda?) | |
228 | (let loop ((lst body) | |
229 | (decls '()) | |
230 | (intspec #f) | |
231 | (doc #f)) | |
232 | (pmatch lst | |
233 | (((declare . ,x) . ,tail) | |
234 | (loop tail (append-reverse x decls) intspec doc)) | |
235 | (((interactive . ,x) . ,tail) | |
236 | (guard lambda? (not intspec)) | |
237 | (loop tail decls x doc)) | |
238 | ((,x . ,tail) | |
239 | (guard lambda? (string? x) (not doc) (not (null? tail))) | |
240 | (loop tail decls intspec x)) | |
241 | (else | |
242 | (values (append-map parse-declaration decls) | |
243 | intspec | |
244 | doc | |
245 | lst))))) | |
246 | ||
247 | (define (parse-lambda-body body) | |
248 | (parse-body-1 body #t)) | |
249 | ||
250 | (define (parse-body body) | |
251 | (receive (decls intspec doc body) (parse-body-1 body #f) | |
252 | (values decls body))) | |
253 | ||
16318179 BT |
254 | ;;; Partition the argument list of a lambda expression into required, |
255 | ;;; optional and rest arguments. | |
256 | ||
257 | (define (parse-lambda-list lst) | |
258 | (define (%match lst null optional rest symbol) | |
259 | (pmatch lst | |
260 | (() (null)) | |
261 | ((&optional . ,tail) (optional tail)) | |
262 | ((&rest . ,tail) (rest tail)) | |
263 | ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail)) | |
264 | (else (fail)))) | |
265 | (define (return rreq ropt rest) | |
266 | (values #t (reverse rreq) (reverse ropt) rest)) | |
267 | (define (fail) | |
268 | (values #f #f #f #f)) | |
269 | (define (parse-req lst rreq) | |
270 | (%match lst | |
271 | (lambda () (return rreq '() #f)) | |
272 | (lambda (tail) (parse-opt tail rreq '())) | |
273 | (lambda (tail) (parse-rest tail rreq '())) | |
274 | (lambda (arg tail) (parse-req tail (cons arg rreq))))) | |
275 | (define (parse-opt lst rreq ropt) | |
276 | (%match lst | |
277 | (lambda () (return rreq ropt #f)) | |
278 | (lambda (tail) (fail)) | |
279 | (lambda (tail) (parse-rest tail rreq ropt)) | |
280 | (lambda (arg tail) (parse-opt tail rreq (cons arg ropt))))) | |
281 | (define (parse-rest lst rreq ropt) | |
282 | (%match lst | |
283 | (lambda () (fail)) | |
284 | (lambda (tail) (fail)) | |
285 | (lambda (tail) (fail)) | |
286 | (lambda (arg tail) (parse-post-rest tail rreq ropt arg)))) | |
287 | (define (parse-post-rest lst rreq ropt rest) | |
288 | (%match lst | |
289 | (lambda () (return rreq ropt rest)) | |
290 | (lambda () (fail)) | |
291 | (lambda () (fail)) | |
292 | (lambda (arg tail) (fail)))) | |
293 | (parse-req lst '())) | |
294 | ||
295 | (define (make-simple-lambda loc meta req opt init rest vars body) | |
296 | (make-lambda loc | |
297 | meta | |
298 | (make-lambda-case #f req opt rest #f init vars body #f))) | |
50abfe76 | 299 | |
d9806be1 | 300 | (define (compile-lambda loc meta args body) |
16318179 BT |
301 | (receive (valid? req-ids opt-ids rest-id) |
302 | (parse-lambda-list args) | |
303 | (if valid? | |
304 | (let* ((all-ids (append req-ids | |
305 | opt-ids | |
306 | (or (and=> rest-id list) '()))) | |
307 | (all-vars (map (lambda (ignore) (gensym)) all-ids))) | |
805b8211 BT |
308 | (let*-values (((decls intspec doc forms) |
309 | (parse-lambda-body body)) | |
310 | ((lexical dynamic) | |
311 | (partition | |
f6e0a4a6 | 312 | (compose (cut bind-lexically? <> value-slot decls) |
805b8211 BT |
313 | car) |
314 | (map list all-ids all-vars))) | |
315 | ((lexical-ids lexical-vars) (unzip2 lexical)) | |
316 | ((dynamic-ids dynamic-vars) (unzip2 dynamic))) | |
317 | (with-dynamic-bindings | |
318 | (fluid-ref bindings-data) | |
319 | dynamic-ids | |
320 | (lambda () | |
321 | (with-lexical-bindings | |
322 | (fluid-ref bindings-data) | |
323 | lexical-ids | |
324 | lexical-vars | |
325 | (lambda () | |
66be42cb BT |
326 | (ensure-globals! |
327 | loc | |
328 | dynamic-ids | |
663c5875 BT |
329 | (let* ((tree-il |
330 | (compile-expr | |
331 | (if rest-id | |
332 | `(let ((,rest-id (if ,rest-id | |
333 | ,rest-id | |
334 | nil))) | |
335 | ,@forms) | |
336 | `(progn ,@forms)))) | |
66be42cb BT |
337 | (full-body |
338 | (if (null? dynamic) | |
339 | tree-il | |
340 | (make-dynlet | |
341 | loc | |
342 | (map (cut make-module-ref loc value-slot <> #t) | |
343 | dynamic-ids) | |
344 | (map (cut make-lexical-ref loc <> <>) | |
345 | dynamic-ids | |
346 | dynamic-vars) | |
347 | tree-il)))) | |
348 | (make-simple-lambda loc | |
349 | meta | |
350 | req-ids | |
351 | opt-ids | |
352 | (map (const (nil-value loc)) | |
353 | opt-ids) | |
354 | rest-id | |
355 | all-vars | |
356 | full-body))))))))) | |
16318179 | 357 | (report-error "invalid function" `(lambda ,args ,@body))))) |
50abfe76 | 358 | |
c983a199 BT |
359 | ;;; Handle the common part of defconst and defvar, that is, checking for |
360 | ;;; a correct doc string and arguments as well as maybe in the future | |
361 | ;;; handling the docstring somehow. | |
de9f26b5 DK |
362 | |
363 | (define (handle-var-def loc sym doc) | |
364 | (cond | |
f4e5e411 BT |
365 | ((not (symbol? sym)) (report-error loc "expected symbol, got" sym)) |
366 | ((> (length doc) 1) (report-error loc "too many arguments to defvar")) | |
367 | ((and (not (null? doc)) (not (string? (car doc)))) | |
368 | (report-error loc "expected string as third argument of defvar, got" | |
369 | (car doc))) | |
370 | ;; TODO: Handle doc string if present. | |
371 | (else #t))) | |
de9f26b5 | 372 | |
44ae163d | 373 | ;;; Handle macro and special operator bindings. |
74c009da | 374 | |
35724ee1 | 375 | (define (find-operator name type) |
8295b7c4 | 376 | (and |
35724ee1 BT |
377 | (symbol? name) |
378 | (module-defined? (resolve-interface function-slot) name) | |
379 | (let ((op (module-ref (resolve-module function-slot) name))) | |
44ae163d BT |
380 | (if (and (pair? op) (eq? (car op) type)) |
381 | (cdr op) | |
382 | #f)))) | |
74c009da | 383 | |
c983a199 | 384 | ;;; See if a (backquoted) expression contains any unquotes. |
9b5ff6a6 DK |
385 | |
386 | (define (contains-unquotes? expr) | |
387 | (if (pair? expr) | |
f4e5e411 BT |
388 | (if (or (unquote? (car expr)) (unquote-splicing? (car expr))) |
389 | #t | |
390 | (or (contains-unquotes? (car expr)) | |
391 | (contains-unquotes? (cdr expr)))) | |
392 | #f)) | |
9b5ff6a6 | 393 | |
c983a199 BT |
394 | ;;; Process a backquoted expression by building up the needed |
395 | ;;; cons/append calls. For splicing, it is assumed that the expression | |
396 | ;;; spliced in evaluates to a list. The emacs manual does not really | |
397 | ;;; state either it has to or what to do if it does not, but Scheme | |
398 | ;;; explicitly forbids it and this seems reasonable also for elisp. | |
9b5ff6a6 DK |
399 | |
400 | (define (unquote-cell? expr) | |
401 | (and (list? expr) (= (length expr) 2) (unquote? (car expr)))) | |
abcf4a9e | 402 | |
9b5ff6a6 DK |
403 | (define (unquote-splicing-cell? expr) |
404 | (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr)))) | |
405 | ||
a90d9c85 | 406 | (define (process-backquote loc expr) |
9b5ff6a6 | 407 | (if (contains-unquotes? expr) |
f4e5e411 BT |
408 | (if (pair? expr) |
409 | (if (or (unquote-cell? expr) (unquote-splicing-cell? expr)) | |
410 | (compile-expr (cadr expr)) | |
411 | (let* ((head (car expr)) | |
412 | (processed-tail (process-backquote loc (cdr expr))) | |
413 | (head-is-list-2 (and (list? head) | |
414 | (= (length head) 2))) | |
415 | (head-unquote (and head-is-list-2 | |
416 | (unquote? (car head)))) | |
417 | (head-unquote-splicing (and head-is-list-2 | |
418 | (unquote-splicing? | |
419 | (car head))))) | |
420 | (if head-unquote-splicing | |
421 | (call-primitive loc | |
422 | 'append | |
423 | (compile-expr (cadr head)) | |
424 | processed-tail) | |
425 | (call-primitive loc 'cons | |
426 | (if head-unquote | |
427 | (compile-expr (cadr head)) | |
428 | (process-backquote loc head)) | |
429 | processed-tail)))) | |
430 | (report-error loc | |
431 | "non-pair expression contains unquotes" | |
432 | expr)) | |
433 | (make-const loc expr))) | |
9b5ff6a6 | 434 | |
44ae163d | 435 | ;;; Special operators |
abcf4a9e | 436 | |
44ae163d | 437 | (defspecial progn (loc args) |
5ddd9645 BT |
438 | (list->seq loc |
439 | (if (null? args) | |
440 | (list (nil-value loc)) | |
441 | (map compile-expr args)))) | |
abcf4a9e | 442 | |
80687f2e BT |
443 | (defspecial eval-when-compile (loc args) |
444 | (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value))) | |
abcf4a9e | 445 | |
44ae163d BT |
446 | (defspecial if (loc args) |
447 | (pmatch args | |
448 | ((,cond ,then . ,else) | |
30439aa8 BT |
449 | (make-conditional |
450 | loc | |
451 | (call-primitive loc 'not | |
452 | (call-primitive loc 'nil? (compile-expr cond))) | |
453 | (compile-expr then) | |
d5ac6923 | 454 | (compile-expr `(progn ,@else)))))) |
44ae163d BT |
455 | |
456 | (defspecial defconst (loc args) | |
457 | (pmatch args | |
458 | ((,sym ,value . ,doc) | |
de9f26b5 | 459 | (if (handle-var-def loc sym doc) |
6fc3eae4 | 460 | (make-seq loc |
5ddd9645 BT |
461 | (set-variable! loc sym (compile-expr value)) |
462 | (make-const loc sym)))))) | |
de9f26b5 | 463 | |
44ae163d BT |
464 | (defspecial defvar (loc args) |
465 | (pmatch args | |
466 | ((,sym) (make-const loc sym)) | |
467 | ((,sym ,value . ,doc) | |
de9f26b5 | 468 | (if (handle-var-def loc sym doc) |
6fc3eae4 | 469 | (make-seq |
f4e5e411 | 470 | loc |
6fc3eae4 AW |
471 | (make-conditional |
472 | loc | |
3f70b2dc BT |
473 | (make-conditional |
474 | loc | |
6fc3eae4 | 475 | (call-primitive |
3f70b2dc | 476 | loc |
6fc3eae4 | 477 | 'module-bound? |
3f70b2dc | 478 | (call-primitive loc |
6fc3eae4 AW |
479 | 'resolve-interface |
480 | (make-const loc value-slot)) | |
481 | (make-const loc sym)) | |
482 | (call-primitive loc | |
483 | 'fluid-bound? | |
484 | (make-module-ref loc value-slot sym #t)) | |
485 | (make-const loc #f)) | |
486 | (make-void loc) | |
5ddd9645 | 487 | (set-variable! loc sym (compile-expr value))) |
6fc3eae4 | 488 | (make-const loc sym)))))) |
44ae163d BT |
489 | |
490 | (defspecial setq (loc args) | |
f5742cf0 BT |
491 | (define (car* x) (if (null? x) '() (car x))) |
492 | (define (cdr* x) (if (null? x) '() (cdr x))) | |
493 | (define (cadr* x) (car* (cdr* x))) | |
494 | (define (cddr* x) (cdr* (cdr* x))) | |
6fc3eae4 | 495 | (list->seq |
44ae163d | 496 | loc |
f5742cf0 BT |
497 | (let loop ((args args) (last (nil-value loc))) |
498 | (if (null? args) | |
499 | (list last) | |
500 | (let ((sym (car args)) | |
501 | (val (compile-expr (cadr* args)))) | |
502 | (if (not (symbol? sym)) | |
503 | (report-error loc "expected symbol in setq") | |
504 | (cons | |
eaeda0d5 | 505 | (set-variable! loc sym val) |
f5742cf0 | 506 | (loop (cddr* args) |
eaeda0d5 | 507 | (reference-variable loc sym))))))))) |
f5742cf0 | 508 | |
44ae163d BT |
509 | (defspecial let (loc args) |
510 | (pmatch args | |
c64c51eb BT |
511 | ((,varlist . ,body) |
512 | (let ((bindings (map (cut parse-let-binding loc <>) varlist))) | |
513 | (receive (decls forms) (parse-body body) | |
514 | (receive (lexical dynamic) | |
515 | (partition | |
516 | (compose (cut bind-lexically? <> value-slot decls) | |
517 | car) | |
518 | bindings) | |
c64c51eb BT |
519 | (let ((make-values (lambda (for) |
520 | (map (lambda (el) (compile-expr (cdr el))) | |
521 | for))) | |
522 | (make-body (lambda () (compile-expr `(progn ,@forms))))) | |
66be42cb BT |
523 | (ensure-globals! |
524 | loc | |
525 | (map car dynamic) | |
526 | (if (null? lexical) | |
527 | (make-dynlet loc | |
528 | (map (compose (cut make-module-ref | |
529 | loc | |
530 | value-slot | |
531 | <> | |
532 | #t) | |
533 | car) | |
534 | dynamic) | |
535 | (map (compose compile-expr cdr) | |
536 | dynamic) | |
537 | (make-body)) | |
538 | (let* ((lexical-syms (map (lambda (el) (gensym)) lexical)) | |
539 | (dynamic-syms (map (lambda (el) (gensym)) dynamic)) | |
540 | (all-syms (append lexical-syms dynamic-syms)) | |
541 | (vals (append (make-values lexical) | |
542 | (make-values dynamic)))) | |
543 | (make-let loc | |
544 | all-syms | |
545 | all-syms | |
546 | vals | |
547 | (with-lexical-bindings | |
548 | (fluid-ref bindings-data) | |
549 | (map car lexical) | |
550 | lexical-syms | |
551 | (lambda () | |
552 | (if (null? dynamic) | |
553 | (make-body) | |
554 | (make-dynlet loc | |
555 | (map | |
556 | (compose | |
557 | (cut make-module-ref | |
558 | loc | |
559 | value-slot | |
560 | <> | |
561 | #t) | |
562 | car) | |
563 | dynamic) | |
564 | (map | |
565 | (lambda (sym) | |
566 | (make-lexical-ref | |
567 | loc | |
568 | sym | |
569 | sym)) | |
570 | dynamic-syms) | |
571 | (make-body)))))))))))))))) | |
44ae163d BT |
572 | |
573 | (defspecial let* (loc args) | |
574 | (pmatch args | |
c64c51eb BT |
575 | ((,varlist . ,body) |
576 | (let ((bindings (map (cut parse-let-binding loc <>) varlist))) | |
577 | (receive (decls forms) (parse-body body) | |
c64c51eb BT |
578 | (let iterate ((tail bindings)) |
579 | (if (null? tail) | |
580 | (compile-expr `(progn ,@forms)) | |
581 | (let ((sym (caar tail)) | |
582 | (value (compile-expr (cdar tail)))) | |
583 | (if (bind-lexically? sym value-slot decls) | |
584 | (let ((target (gensym))) | |
585 | (make-let loc | |
586 | `(,target) | |
587 | `(,target) | |
588 | `(,value) | |
589 | (with-lexical-bindings | |
590 | (fluid-ref bindings-data) | |
591 | `(,sym) | |
592 | `(,target) | |
593 | (lambda () (iterate (cdr tail)))))) | |
66be42cb BT |
594 | (ensure-globals! |
595 | loc | |
596 | (list sym) | |
597 | (make-dynlet loc | |
598 | (list (make-module-ref loc value-slot sym #t)) | |
599 | (list value) | |
600 | (iterate (cdr tail))))))))))))) | |
44ae163d | 601 | |
44ae163d | 602 | (defspecial flet (loc args) |
44ae163d BT |
603 | (pmatch args |
604 | ((,bindings . ,body) | |
6bb004c4 BT |
605 | (let ((names+vals (map (cut parse-flet-binding loc <>) bindings))) |
606 | (receive (decls forms) (parse-body body) | |
607 | (let ((names (map car names+vals)) | |
608 | (vals (map cdr names+vals)) | |
609 | (gensyms (map (lambda (x) (gensym)) names+vals))) | |
eaeda0d5 | 610 | (with-function-bindings |
6bb004c4 BT |
611 | (fluid-ref bindings-data) |
612 | names | |
613 | gensyms | |
614 | (lambda () | |
615 | (make-let loc | |
616 | names | |
617 | gensyms | |
618 | (map compile-expr vals) | |
619 | (compile-expr `(progn ,@forms))))))))))) | |
44ae163d | 620 | |
1c2f9636 | 621 | (defspecial labels (loc args) |
44ae163d BT |
622 | (pmatch args |
623 | ((,bindings . ,body) | |
1c2f9636 BT |
624 | (let ((names+vals (map (cut parse-flet-binding loc <>) bindings))) |
625 | (receive (decls forms) (parse-body body) | |
626 | (let ((names (map car names+vals)) | |
627 | (vals (map cdr names+vals)) | |
628 | (gensyms (map (lambda (x) (gensym)) names+vals))) | |
eaeda0d5 | 629 | (with-function-bindings |
1c2f9636 BT |
630 | (fluid-ref bindings-data) |
631 | names | |
632 | gensyms | |
633 | (lambda () | |
634 | (make-letrec #f | |
635 | loc | |
636 | names | |
637 | gensyms | |
638 | (map compile-expr vals) | |
639 | (compile-expr `(progn ,@forms))))))))))) | |
44ae163d BT |
640 | |
641 | ;;; guile-ref allows building TreeIL's module references from within | |
642 | ;;; elisp as a way to access data within the Guile universe. The module | |
643 | ;;; and symbol referenced are static values, just like (@ module symbol) | |
644 | ;;; does! | |
645 | ||
646 | (defspecial guile-ref (loc args) | |
647 | (pmatch args | |
648 | ((,module ,sym) (guard (and (list? module) (symbol? sym))) | |
649 | (make-module-ref loc module sym #t)))) | |
650 | ||
651 | ;;; guile-primitive allows to create primitive references, which are | |
652 | ;;; still a little faster. | |
653 | ||
654 | (defspecial guile-primitive (loc args) | |
655 | (pmatch args | |
656 | ((,sym) | |
657 | (make-primitive-ref loc sym)))) | |
658 | ||
44ae163d BT |
659 | (defspecial function (loc args) |
660 | (pmatch args | |
661 | (((lambda ,args . ,body)) | |
d9806be1 | 662 | (compile-lambda loc '() args body)) |
67cb2c27 | 663 | ((,sym) (guard (symbol? sym)) |
eaeda0d5 | 664 | (reference-function loc sym)))) |
de9f26b5 | 665 | |
44ae163d BT |
666 | (defspecial defmacro (loc args) |
667 | (pmatch args | |
668 | ((,name ,args . ,body) | |
74c009da | 669 | (if (not (symbol? name)) |
f4e5e411 | 670 | (report-error loc "expected symbol as macro name" name) |
2ce5e740 | 671 | (let* ((tree-il |
6fc3eae4 | 672 | (make-seq |
2ce5e740 | 673 | loc |
5ddd9645 | 674 | (set-function! |
6fc3eae4 AW |
675 | loc |
676 | name | |
5ddd9645 | 677 | (make-call |
2ce5e740 | 678 | loc |
5ddd9645 BT |
679 | (make-module-ref loc '(guile) 'cons #t) |
680 | (list (make-const loc 'macro) | |
681 | (compile-lambda loc | |
682 | `((name . ,name)) | |
683 | args | |
684 | body)))) | |
6fc3eae4 | 685 | (make-const loc name)))) |
66be42cb | 686 | (compile tree-il #:from 'tree-il #:to 'value) |
44ae163d | 687 | tree-il))))) |
abcf4a9e | 688 | |
44ae163d BT |
689 | (defspecial defun (loc args) |
690 | (pmatch args | |
691 | ((,name ,args . ,body) | |
692 | (if (not (symbol? name)) | |
693 | (report-error loc "expected symbol as function name" name) | |
6fc3eae4 | 694 | (make-seq loc |
5ddd9645 | 695 | (set-function! loc |
6fc3eae4 | 696 | name |
6fc3eae4 | 697 | (compile-lambda loc |
5ddd9645 | 698 | `((name . ,name)) |
6fc3eae4 AW |
699 | args |
700 | body)) | |
701 | (make-const loc name)))))) | |
abcf4a9e | 702 | |
0dbfdeef | 703 | (defspecial #{`}# (loc args) |
44ae163d BT |
704 | (pmatch args |
705 | ((,val) | |
706 | (process-backquote loc val)))) | |
1e018f6c | 707 | |
44ae163d BT |
708 | (defspecial quote (loc args) |
709 | (pmatch args | |
710 | ((,val) | |
711 | (make-const loc val)))) | |
abcf4a9e | 712 | |
d273b826 BT |
713 | (defspecial %funcall (loc args) |
714 | (pmatch args | |
715 | ((,function . ,arguments) | |
5ddd9645 BT |
716 | (make-call loc |
717 | (compile-expr function) | |
718 | (map compile-expr arguments))))) | |
d273b826 | 719 | |
03e00c5c BT |
720 | (defspecial %set-lexical-binding-mode (loc args) |
721 | (pmatch args | |
722 | ((,val) | |
723 | (fluid-set! lexical-binding val) | |
724 | (make-void loc)))) | |
725 | ||
44ae163d | 726 | ;;; Compile a compound expression to Tree-IL. |
74c009da | 727 | |
44ae163d BT |
728 | (define (compile-pair loc expr) |
729 | (let ((operator (car expr)) | |
730 | (arguments (cdr expr))) | |
731 | (cond | |
732 | ((find-operator operator 'special-operator) | |
733 | => (lambda (special-operator-function) | |
734 | (special-operator-function loc arguments))) | |
735 | ((find-operator operator 'macro) | |
736 | => (lambda (macro-function) | |
737 | (compile-expr (apply macro-function arguments)))) | |
738 | (else | |
b05ca4ab | 739 | (compile-expr `(%funcall (function ,operator) ,@arguments)))))) |
abcf4a9e | 740 | |
44ae163d BT |
741 | ;;; Compile a symbol expression. This is a variable reference or maybe |
742 | ;;; some special value like nil. | |
cef997e8 | 743 | |
44ae163d BT |
744 | (define (compile-symbol loc sym) |
745 | (case sym | |
746 | ((nil) (nil-value loc)) | |
747 | ((t) (t-value loc)) | |
eaeda0d5 | 748 | (else (reference-variable loc sym)))) |
51248e6e | 749 | |
c983a199 | 750 | ;;; Compile a single expression to TreeIL. |
51248e6e | 751 | |
a90d9c85 | 752 | (define (compile-expr expr) |
51248e6e DK |
753 | (let ((loc (location expr))) |
754 | (cond | |
f4e5e411 BT |
755 | ((symbol? expr) |
756 | (compile-symbol loc expr)) | |
757 | ((pair? expr) | |
758 | (compile-pair loc expr)) | |
759 | (else (make-const loc expr))))) | |
51248e6e | 760 | |
c983a199 BT |
761 | ;;; Process the compiler options. |
762 | ;;; FIXME: Why is '(()) passed as options by the REPL? | |
a0899974 | 763 | |
c808c926 DK |
764 | (define (valid-symbol-list-arg? value) |
765 | (or (eq? value 'all) | |
766 | (and (list? value) (and-map symbol? value)))) | |
767 | ||
a0899974 DK |
768 | (define (process-options! opt) |
769 | (if (and (not (null? opt)) | |
770 | (not (equal? opt '(())))) | |
f4e5e411 BT |
771 | (if (null? (cdr opt)) |
772 | (report-error #f "Invalid compiler options" opt) | |
773 | (let ((key (car opt)) | |
774 | (value (cadr opt))) | |
775 | (case key | |
38299196 BT |
776 | ((#:warnings) ; ignore |
777 | #f) | |
f4e5e411 BT |
778 | (else (report-error #f |
779 | "Invalid compiler option" | |
780 | key))))))) | |
a0899974 | 781 | |
51248e6e DK |
782 | (define (compile-tree-il expr env opts) |
783 | (values | |
e5a361d1 | 784 | (with-fluids ((bindings-data (make-bindings))) |
f4e5e411 | 785 | (process-options! opts) |
66be42cb | 786 | (compile-expr expr)) |
f4e5e411 BT |
787 | env |
788 | env)) |