inline `generate-let' and `generate-let*'
[bpt/guile.git] / module / language / elisp / compile-tree-il.scm
CommitLineData
dfbc6e9d 1;;; Guile Emacs Lisp
51248e6e 2
54e53aa4 3;; Copyright (C) 2009, 2010 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
BT
62(define lexical-binding (make-fluid))
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)
103 (make-application loc (make-primitive-ref loc sym) args))
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
c983a199
BT
111;;; Generate code to ensure a global symbol is there for further use of
112;;; a given symbol. In general during the compilation, those needed are
113;;; only tracked with the bindings data structure. Afterwards, however,
114;;; for all those needed symbols the globals are really generated with
115;;; this routine.
344927c3 116
1b1195f2 117(define (generate-ensure-global loc sym module)
f4e5e411
BT
118 (make-application loc
119 (make-module-ref loc runtime 'ensure-fluid! #t)
120 (list (make-const loc module)
121 (make-const loc sym))))
f28de791 122
2ce5e740
BT
123(define (ensuring-globals loc bindings body)
124 (make-sequence
125 loc
d5da7661
BT
126 `(,@(map-globals (fluid-ref bindings)
127 (lambda (mod sym)
128 (generate-ensure-global loc sym mod)))
2ce5e740
BT
129 ,body)))
130
c983a199
BT
131;;; Build a construct that establishes dynamic bindings for certain
132;;; variables. We may want to choose between binding with fluids and
133;;; with-fluids* and using just ordinary module symbols and
134;;; setting/reverting their values with a dynamic-wind.
1b1195f2
DK
135
136(define (let-dynamic loc syms module vals body)
f4e5e411
BT
137 (call-primitive
138 loc
139 'with-fluids*
140 (make-application loc
141 (make-primitive-ref loc 'list)
142 (map (lambda (sym)
143 (make-module-ref loc module sym #t))
144 syms))
145 (make-application loc (make-primitive-ref loc 'list) vals)
146 (make-lambda loc
147 '()
148 (make-lambda-case #f '() #f #f #f '() '() body #f))))
1b1195f2 149
c983a199
BT
150;;; Handle access to a variable (reference/setting) correctly depending
151;;; on whether it is currently lexically or dynamically bound. lexical
152;;; access is done only for references to the value-slot module!
a6a5cf03 153
c6920dc8
BT
154(define (access-variable loc
155 sym
156 module
157 handle-global
158 handle-lexical
159 handle-dynamic)
a6a5cf03 160 (let ((lexical (get-lexical-binding (fluid-ref bindings-data) sym)))
c6920dc8
BT
161 (cond
162 (lexical (handle-lexical lexical))
163 ((equal? module function-slot) (handle-global))
164 (else (handle-dynamic)))))
a6a5cf03 165
c983a199
BT
166;;; Generate code to reference a variable. For references in the
167;;; value-slot module, we may want to generate a lexical reference
168;;; instead if the variable has a lexical binding.
344927c3 169
a90d9c85 170(define (reference-variable loc sym module)
f4e5e411
BT
171 (access-variable
172 loc
173 sym
174 module
c6920dc8 175 (lambda () (make-module-ref loc module sym #t))
f4e5e411
BT
176 (lambda (lexical) (make-lexical-ref loc lexical lexical))
177 (lambda ()
d5da7661 178 (mark-global! (fluid-ref bindings-data) sym module)
f4e5e411
BT
179 (call-primitive loc
180 'fluid-ref
181 (make-module-ref loc module sym #t)))))
344927c3 182
c983a199
BT
183;;; Generate code to set a variable. Just as with reference-variable, in
184;;; case of a reference to value-slot, we want to generate a lexical set
185;;; when the variable has a lexical binding.
344927c3 186
a90d9c85 187(define (set-variable! loc sym module value)
f4e5e411
BT
188 (access-variable
189 loc
190 sym
191 module
c6920dc8
BT
192 (lambda ()
193 (make-application
194 loc
85bc6238
BT
195 (make-module-ref loc runtime 'set-symbol-function! #t) ;++ fix
196 (list (make-const loc sym) value)))
f4e5e411
BT
197 (lambda (lexical) (make-lexical-set loc lexical lexical value))
198 (lambda ()
d5da7661 199 (mark-global! (fluid-ref bindings-data) sym module)
f4e5e411
BT
200 (call-primitive loc
201 'fluid-set!
202 (make-module-ref loc module sym #t)
203 value))))
344927c3 204
f6e0a4a6 205(define (bind-lexically? sym module decls)
9083c48d 206 (or (eq? module function-slot)
f6e0a4a6
BT
207 (let ((decl (assq-ref decls sym)))
208 (and (equal? module value-slot)
209 (or
210 (eq? decl 'lexical)
211 (and
212 (fluid-ref lexical-binding)
213 (not (global? (fluid-ref bindings-data) sym module))))))))
a6a5cf03 214
0e5b7e74
BT
215(define (parse-let-binding loc binding)
216 (pmatch binding
217 ((unquote var)
218 (guard (symbol? var))
219 (cons var #nil))
220 ((,var)
221 (guard (symbol? var))
222 (cons var #nil))
223 ((,var ,val)
224 (guard (symbol? var))
225 (cons var val))
226 (else
227 (report-error loc "malformed variable binding" binding))))
228
229(define (parse-flet-binding loc binding)
230 (pmatch binding
231 ((,var ,args . ,body)
232 (guard (symbol? var))
233 (cons var `(function (lambda ,args ,@body))))
234 (else
235 (report-error loc "malformed function binding" binding))))
236
805b8211
BT
237(define (parse-declaration expr)
238 (pmatch expr
239 ((lexical . ,vars)
240 (map (cut cons <> 'lexical) vars))
805b8211
BT
241 (else
242 '())))
243
244(define (parse-body-1 body lambda?)
245 (let loop ((lst body)
246 (decls '())
247 (intspec #f)
248 (doc #f))
249 (pmatch lst
250 (((declare . ,x) . ,tail)
251 (loop tail (append-reverse x decls) intspec doc))
252 (((interactive . ,x) . ,tail)
253 (guard lambda? (not intspec))
254 (loop tail decls x doc))
255 ((,x . ,tail)
256 (guard lambda? (string? x) (not doc) (not (null? tail)))
257 (loop tail decls intspec x))
258 (else
259 (values (append-map parse-declaration decls)
260 intspec
261 doc
262 lst)))))
263
264(define (parse-lambda-body body)
265 (parse-body-1 body #t))
266
267(define (parse-body body)
268 (receive (decls intspec doc body) (parse-body-1 body #f)
269 (values decls body)))
270
16318179
BT
271;;; Partition the argument list of a lambda expression into required,
272;;; optional and rest arguments.
273
274(define (parse-lambda-list lst)
275 (define (%match lst null optional rest symbol)
276 (pmatch lst
277 (() (null))
278 ((&optional . ,tail) (optional tail))
279 ((&rest . ,tail) (rest tail))
280 ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
281 (else (fail))))
282 (define (return rreq ropt rest)
283 (values #t (reverse rreq) (reverse ropt) rest))
284 (define (fail)
285 (values #f #f #f #f))
286 (define (parse-req lst rreq)
287 (%match lst
288 (lambda () (return rreq '() #f))
289 (lambda (tail) (parse-opt tail rreq '()))
290 (lambda (tail) (parse-rest tail rreq '()))
291 (lambda (arg tail) (parse-req tail (cons arg rreq)))))
292 (define (parse-opt lst rreq ropt)
293 (%match lst
294 (lambda () (return rreq ropt #f))
295 (lambda (tail) (fail))
296 (lambda (tail) (parse-rest tail rreq ropt))
297 (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
298 (define (parse-rest lst rreq ropt)
299 (%match lst
300 (lambda () (fail))
301 (lambda (tail) (fail))
302 (lambda (tail) (fail))
303 (lambda (arg tail) (parse-post-rest tail rreq ropt arg))))
304 (define (parse-post-rest lst rreq ropt rest)
305 (%match lst
306 (lambda () (return rreq ropt rest))
307 (lambda () (fail))
308 (lambda () (fail))
309 (lambda (arg tail) (fail))))
310 (parse-req lst '()))
311
312(define (make-simple-lambda loc meta req opt init rest vars body)
313 (make-lambda loc
314 meta
315 (make-lambda-case #f req opt rest #f init vars body #f)))
50abfe76 316
d9806be1 317(define (compile-lambda loc meta args body)
16318179
BT
318 (receive (valid? req-ids opt-ids rest-id)
319 (parse-lambda-list args)
320 (if valid?
321 (let* ((all-ids (append req-ids
322 opt-ids
323 (or (and=> rest-id list) '())))
324 (all-vars (map (lambda (ignore) (gensym)) all-ids)))
805b8211
BT
325 (let*-values (((decls intspec doc forms)
326 (parse-lambda-body body))
327 ((lexical dynamic)
328 (partition
f6e0a4a6 329 (compose (cut bind-lexically? <> value-slot decls)
805b8211
BT
330 car)
331 (map list all-ids all-vars)))
332 ((lexical-ids lexical-vars) (unzip2 lexical))
333 ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
334 (with-dynamic-bindings
335 (fluid-ref bindings-data)
336 dynamic-ids
337 (lambda ()
338 (with-lexical-bindings
339 (fluid-ref bindings-data)
340 lexical-ids
341 lexical-vars
342 (lambda ()
343 (let* ((tree-il (compile-expr `(progn ,@forms)))
344 (full-body
345 (if (null? dynamic)
346 tree-il
347 (let-dynamic loc
348 dynamic-ids
349 value-slot
350 (map (cut make-lexical-ref
351 loc
352 <>
353 <>)
354 dynamic-ids
355 dynamic-vars)
356 tree-il))))
357 (make-simple-lambda loc
358 meta
359 req-ids
360 opt-ids
361 (map (const (nil-value loc))
362 opt-ids)
363 rest-id
364 all-vars
365 full-body))))))))
16318179 366 (report-error "invalid function" `(lambda ,args ,@body)))))
50abfe76 367
c983a199
BT
368;;; Handle the common part of defconst and defvar, that is, checking for
369;;; a correct doc string and arguments as well as maybe in the future
370;;; handling the docstring somehow.
de9f26b5
DK
371
372(define (handle-var-def loc sym doc)
373 (cond
f4e5e411
BT
374 ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
375 ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
376 ((and (not (null? doc)) (not (string? (car doc))))
377 (report-error loc "expected string as third argument of defvar, got"
378 (car doc)))
379 ;; TODO: Handle doc string if present.
380 (else #t)))
de9f26b5 381
44ae163d 382;;; Handle macro and special operator bindings.
74c009da 383
35724ee1 384(define (find-operator name type)
8295b7c4 385 (and
35724ee1
BT
386 (symbol? name)
387 (module-defined? (resolve-interface function-slot) name)
388 (let ((op (module-ref (resolve-module function-slot) name)))
44ae163d
BT
389 (if (and (pair? op) (eq? (car op) type))
390 (cdr op)
391 #f))))
74c009da 392
c983a199 393;;; See if a (backquoted) expression contains any unquotes.
9b5ff6a6
DK
394
395(define (contains-unquotes? expr)
396 (if (pair? expr)
f4e5e411
BT
397 (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
398 #t
399 (or (contains-unquotes? (car expr))
400 (contains-unquotes? (cdr expr))))
401 #f))
9b5ff6a6 402
c983a199
BT
403;;; Process a backquoted expression by building up the needed
404;;; cons/append calls. For splicing, it is assumed that the expression
405;;; spliced in evaluates to a list. The emacs manual does not really
406;;; state either it has to or what to do if it does not, but Scheme
407;;; explicitly forbids it and this seems reasonable also for elisp.
9b5ff6a6
DK
408
409(define (unquote-cell? expr)
410 (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
abcf4a9e 411
9b5ff6a6
DK
412(define (unquote-splicing-cell? expr)
413 (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
414
a90d9c85 415(define (process-backquote loc expr)
9b5ff6a6 416 (if (contains-unquotes? expr)
f4e5e411
BT
417 (if (pair? expr)
418 (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
419 (compile-expr (cadr expr))
420 (let* ((head (car expr))
421 (processed-tail (process-backquote loc (cdr expr)))
422 (head-is-list-2 (and (list? head)
423 (= (length head) 2)))
424 (head-unquote (and head-is-list-2
425 (unquote? (car head))))
426 (head-unquote-splicing (and head-is-list-2
427 (unquote-splicing?
428 (car head)))))
429 (if head-unquote-splicing
430 (call-primitive loc
431 'append
432 (compile-expr (cadr head))
433 processed-tail)
434 (call-primitive loc 'cons
435 (if head-unquote
436 (compile-expr (cadr head))
437 (process-backquote loc head))
438 processed-tail))))
439 (report-error loc
440 "non-pair expression contains unquotes"
441 expr))
442 (make-const loc expr)))
9b5ff6a6 443
44ae163d 444;;; Special operators
abcf4a9e 445
44ae163d 446(defspecial progn (loc args)
d5ac6923
BT
447 (make-sequence loc
448 (if (null? args)
449 (list (nil-value loc))
450 (map compile-expr args))))
abcf4a9e 451
80687f2e
BT
452(defspecial eval-when-compile (loc args)
453 (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
454
44ae163d
BT
455(defspecial if (loc args)
456 (pmatch args
457 ((,cond ,then . ,else)
30439aa8
BT
458 (make-conditional
459 loc
460 (call-primitive loc 'not
461 (call-primitive loc 'nil? (compile-expr cond)))
462 (compile-expr then)
d5ac6923 463 (compile-expr `(progn ,@else))))))
44ae163d
BT
464
465(defspecial defconst (loc args)
466 (pmatch args
467 ((,sym ,value . ,doc)
de9f26b5 468 (if (handle-var-def loc sym doc)
f4e5e411
BT
469 (make-sequence loc
470 (list (set-variable! loc
471 sym
472 value-slot
473 (compile-expr value))
44ae163d 474 (make-const loc sym)))))))
de9f26b5 475
44ae163d
BT
476(defspecial defvar (loc args)
477 (pmatch args
478 ((,sym) (make-const loc sym))
479 ((,sym ,value . ,doc)
de9f26b5 480 (if (handle-var-def loc sym doc)
f4e5e411
BT
481 (make-sequence
482 loc
3f70b2dc
BT
483 (list
484 (make-conditional
485 loc
486 (make-conditional
487 loc
488 (call-primitive
489 loc
490 'module-bound?
491 (call-primitive loc
492 'resolve-interface
493 (make-const loc value-slot))
494 (make-const loc sym))
495 (call-primitive loc
496 'fluid-bound?
497 (make-module-ref loc value-slot sym #t))
498 (make-const loc #f))
499 (make-void loc)
500 (set-variable! loc sym value-slot (compile-expr value)))
501 (make-const loc sym)))))))
44ae163d
BT
502
503(defspecial setq (loc args)
f5742cf0
BT
504 (define (car* x) (if (null? x) '() (car x)))
505 (define (cdr* x) (if (null? x) '() (cdr x)))
506 (define (cadr* x) (car* (cdr* x)))
507 (define (cddr* x) (cdr* (cdr* x)))
44ae163d
BT
508 (make-sequence
509 loc
f5742cf0
BT
510 (let loop ((args args) (last (nil-value loc)))
511 (if (null? args)
512 (list last)
513 (let ((sym (car args))
514 (val (compile-expr (cadr* args))))
515 (if (not (symbol? sym))
516 (report-error loc "expected symbol in setq")
517 (cons
518 (set-variable! loc sym value-slot val)
519 (loop (cddr* args)
520 (reference-variable loc sym value-slot)))))))))
521
44ae163d
BT
522(defspecial let (loc args)
523 (pmatch args
c64c51eb
BT
524 ((,varlist . ,body)
525 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
526 (receive (decls forms) (parse-body body)
527 (receive (lexical dynamic)
528 (partition
529 (compose (cut bind-lexically? <> value-slot decls)
530 car)
531 bindings)
532 (for-each (lambda (sym)
533 (mark-global! (fluid-ref bindings-data)
534 sym
535 value-slot))
536 (map car dynamic))
537 (let ((make-values (lambda (for)
538 (map (lambda (el) (compile-expr (cdr el)))
539 for)))
540 (make-body (lambda () (compile-expr `(progn ,@forms)))))
541 (if (null? lexical)
542 (let-dynamic loc (map car dynamic) value-slot
543 (make-values dynamic) (make-body))
544 (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
545 (dynamic-syms (map (lambda (el) (gensym)) dynamic))
546 (all-syms (append lexical-syms dynamic-syms))
547 (vals (append (make-values lexical)
548 (make-values dynamic))))
549 (make-let loc
550 all-syms
551 all-syms
552 vals
553 (with-lexical-bindings
554 (fluid-ref bindings-data)
555 (map car lexical)
556 lexical-syms
557 (lambda ()
558 (if (null? dynamic)
559 (make-body)
560 (let-dynamic loc
561 (map car dynamic)
562 value-slot
563 (map
564 (lambda (sym)
565 (make-lexical-ref loc
566 sym
567 sym))
568 dynamic-syms)
569 (make-body)))))))))))))))
570
571(defspecial let* (loc args)
572 (pmatch args
573 ((,varlist . ,body)
574 (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
575 (receive (decls forms) (parse-body body)
576 (for-each (lambda (sym)
577 (if (not (bind-lexically? sym value-slot decls))
578 (mark-global! (fluid-ref bindings-data)
579 sym
580 value-slot)))
581 (map car bindings))
582 (let iterate ((tail bindings))
583 (if (null? tail)
584 (compile-expr `(progn ,@forms))
585 (let ((sym (caar tail))
586 (value (compile-expr (cdar tail))))
587 (if (bind-lexically? sym value-slot decls)
588 (let ((target (gensym)))
589 (make-let loc
590 `(,target)
591 `(,target)
592 `(,value)
593 (with-lexical-bindings
594 (fluid-ref bindings-data)
595 `(,sym)
596 `(,target)
597 (lambda () (iterate (cdr tail))))))
598 (let-dynamic loc
599 `(,(caar tail))
600 value-slot
601 `(,value)
602 (iterate (cdr tail))))))))))))
44ae163d 603
44ae163d
BT
604(defspecial flet (loc args)
605 (pmatch args
606 ((,bindings . ,body)
6bb004c4
BT
607 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
608 (receive (decls forms) (parse-body body)
609 (let ((names (map car names+vals))
610 (vals (map cdr names+vals))
611 (gensyms (map (lambda (x) (gensym)) names+vals)))
612 (with-lexical-bindings
613 (fluid-ref bindings-data)
614 names
615 gensyms
616 (lambda ()
617 (make-let loc
618 names
619 gensyms
620 (map compile-expr vals)
621 (compile-expr `(progn ,@forms)))))))))))
44ae163d 622
1c2f9636
BT
623(defspecial labels (loc args)
624 (pmatch args
625 ((,bindings . ,body)
626 (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
627 (receive (decls forms) (parse-body body)
628 (let ((names (map car names+vals))
629 (vals (map cdr names+vals))
630 (gensyms (map (lambda (x) (gensym)) names+vals)))
631 (with-lexical-bindings
632 (fluid-ref bindings-data)
633 names
634 gensyms
635 (lambda ()
636 (make-letrec #f
637 loc
638 names
639 gensyms
640 (map compile-expr vals)
641 (compile-expr `(progn ,@forms)))))))))))
642
44ae163d
BT
643;;; guile-ref allows building TreeIL's module references from within
644;;; elisp as a way to access data within the Guile universe. The module
645;;; and symbol referenced are static values, just like (@ module symbol)
646;;; does!
647
648(defspecial guile-ref (loc args)
649 (pmatch args
650 ((,module ,sym) (guard (and (list? module) (symbol? sym)))
651 (make-module-ref loc module sym #t))))
652
653;;; guile-primitive allows to create primitive references, which are
654;;; still a little faster.
655
656(defspecial guile-primitive (loc args)
657 (pmatch args
658 ((,sym)
659 (make-primitive-ref loc sym))))
660
44ae163d
BT
661(defspecial function (loc args)
662 (pmatch args
663 (((lambda ,args . ,body))
d9806be1 664 (compile-lambda loc '() args body))
67cb2c27 665 ((,sym) (guard (symbol? sym))
3f70b2dc 666 (reference-variable loc sym function-slot))))
de9f26b5 667
44ae163d
BT
668(defspecial defmacro (loc args)
669 (pmatch args
670 ((,name ,args . ,body)
74c009da 671 (if (not (symbol? name))
f4e5e411 672 (report-error loc "expected symbol as macro name" name)
2ce5e740
BT
673 (let* ((tree-il
674 (make-sequence
675 loc
676 (list
677 (set-variable!
678 loc
679 name
680 function-slot
681 (make-application
682 loc
683 (make-module-ref loc '(guile) 'cons #t)
684 (list (make-const loc 'macro)
d9806be1
BT
685 (compile-lambda loc
686 `((name . ,name))
687 args
688 body))))
2ce5e740
BT
689 (make-const loc name)))))
690 (compile (ensuring-globals loc bindings-data tree-il)
691 #:from 'tree-il
692 #:to 'value)
44ae163d 693 tree-il)))))
abcf4a9e 694
44ae163d
BT
695(defspecial defun (loc args)
696 (pmatch args
697 ((,name ,args . ,body)
698 (if (not (symbol? name))
699 (report-error loc "expected symbol as function name" name)
700 (make-sequence loc
701 (list (set-variable! loc
702 name
703 function-slot
704 (compile-lambda loc
d9806be1 705 `((name . ,name))
44ae163d
BT
706 args
707 body))
708 (make-const loc name)))))))
abcf4a9e 709
0dbfdeef 710(defspecial #{`}# (loc args)
44ae163d
BT
711 (pmatch args
712 ((,val)
713 (process-backquote loc val))))
1e018f6c 714
44ae163d
BT
715(defspecial quote (loc args)
716 (pmatch args
717 ((,val)
718 (make-const loc val))))
abcf4a9e 719
d273b826
BT
720(defspecial %funcall (loc args)
721 (pmatch args
722 ((,function . ,arguments)
723 (make-application loc
724 (compile-expr function)
725 (map compile-expr arguments)))))
726
03e00c5c
BT
727(defspecial %set-lexical-binding-mode (loc args)
728 (pmatch args
729 ((,val)
730 (fluid-set! lexical-binding val)
731 (make-void loc))))
732
44ae163d 733;;; Compile a compound expression to Tree-IL.
74c009da 734
44ae163d
BT
735(define (compile-pair loc expr)
736 (let ((operator (car expr))
737 (arguments (cdr expr)))
738 (cond
739 ((find-operator operator 'special-operator)
740 => (lambda (special-operator-function)
741 (special-operator-function loc arguments)))
742 ((find-operator operator 'macro)
743 => (lambda (macro-function)
744 (compile-expr (apply macro-function arguments))))
745 (else
b05ca4ab 746 (compile-expr `(%funcall (function ,operator) ,@arguments))))))
abcf4a9e 747
44ae163d
BT
748;;; Compile a symbol expression. This is a variable reference or maybe
749;;; some special value like nil.
cef997e8 750
44ae163d
BT
751(define (compile-symbol loc sym)
752 (case sym
753 ((nil) (nil-value loc))
754 ((t) (t-value loc))
3f70b2dc 755 (else (reference-variable loc sym value-slot))))
51248e6e 756
c983a199 757;;; Compile a single expression to TreeIL.
51248e6e 758
a90d9c85 759(define (compile-expr expr)
51248e6e
DK
760 (let ((loc (location expr)))
761 (cond
f4e5e411
BT
762 ((symbol? expr)
763 (compile-symbol loc expr))
764 ((pair? expr)
765 (compile-pair loc expr))
766 (else (make-const loc expr)))))
51248e6e 767
c983a199
BT
768;;; Process the compiler options.
769;;; FIXME: Why is '(()) passed as options by the REPL?
a0899974 770
c808c926
DK
771(define (valid-symbol-list-arg? value)
772 (or (eq? value 'all)
773 (and (list? value) (and-map symbol? value))))
774
a0899974
DK
775(define (process-options! opt)
776 (if (and (not (null? opt))
777 (not (equal? opt '(()))))
f4e5e411
BT
778 (if (null? (cdr opt))
779 (report-error #f "Invalid compiler options" opt)
780 (let ((key (car opt))
781 (value (cadr opt)))
782 (case key
38299196
BT
783 ((#:warnings) ; ignore
784 #f)
f4e5e411
BT
785 (else (report-error #f
786 "Invalid compiler option"
787 key)))))))
a0899974 788
c983a199
BT
789;;; Entry point for compilation to TreeIL. This creates the bindings
790;;; data structure, and after compiling the main expression we need to
791;;; make sure all globals for symbols used during the compilation are
792;;; created using the generate-ensure-global function.
51248e6e
DK
793
794(define (compile-tree-il expr env opts)
795 (values
e5a361d1 796 (with-fluids ((bindings-data (make-bindings)))
f4e5e411 797 (process-options! opts)
2ce5e740 798 (let ((compiled (compile-expr expr)))
30439aa8 799 (ensuring-globals (location expr) bindings-data compiled)))
f4e5e411
BT
800 env
801 env))