Lambda expressions in elisp, but not yet function calls.
[bpt/guile.git] / module / language / elisp / compile-tree-il.scm
CommitLineData
51248e6e
DK
1;;; Guile Emac Lisp
2
3;; Copyright (C) 2001 Free Software Foundation, Inc.
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
7;; the Free Software Foundation; either version 2, or (at your option)
8;; any later version.
9;;
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.
14;;
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)
23 #:use-module (language tree-il)
24 #:use-module (system base pmatch)
25 #:export (compile-tree-il))
26
27
28; Find the source properties of some parsed expression if there are any
29; associated with it.
30
31(define (location x)
32 (and (pair? x)
33 (let ((props (source-properties x)))
34 (and (not (null? props))
35 props))))
36
37
fdfb36de 38; Value to use for Elisp's nil and t.
4530432e 39
a4316739 40(define (nil-value loc) (make-const loc #f))
fdfb36de 41(define (t-value loc) (make-const loc #t))
4530432e
DK
42
43
344927c3
DK
44; Modules that contain the value and function slot bindings.
45
46(define runtime '(language elisp runtime))
47(define value-slot '(language elisp runtime value-slot))
48(define function-slot '(language elisp runtime function-slot))
49
50
50abfe76
DK
51; Build a call to a primitive procedure nicely.
52
53(define (call-primitive loc sym . args)
54 (make-application loc (make-primitive-ref loc sym) args))
55
56
57; Error reporting routine for syntax/compilation problems or build code for
58; a runtime-error output.
344927c3
DK
59
60(define (report-error loc . args)
61 (apply error args))
62
50abfe76
DK
63(define (runtime-error loc msg . args)
64 (make-application loc (make-primitive-ref loc 'error)
65 (cons (make-const loc msg) args)))
66
344927c3
DK
67
68; Generate code to ensure a fluid is there for further use of a given symbol.
69
70(define (ensure-fluid! loc sym module)
71 ; FIXME: Do this!
72 (make-void loc))
73
74
75; Generate code to reference a fluid saved variable.
76
77(define (reference-variable loc sym module)
78 (make-sequence loc
79 (list (ensure-fluid! loc sym module)
50abfe76
DK
80 (call-primitive loc 'fluid-ref
81 (make-module-ref loc module sym #t)))))
344927c3
DK
82
83
84; Reference a variable and error if the value is void.
85
86(define (reference-with-check loc sym module)
87 (let ((var (gensym)))
88 (make-let loc '(value) `(,var) `(,(reference-variable loc sym module))
89 (make-conditional loc
50abfe76
DK
90 (call-primitive loc 'eq?
91 (make-module-ref loc runtime 'void #t)
92 (make-lexical-ref loc 'value var))
93 (runtime-error loc "variable is void:" (make-const loc sym))
344927c3
DK
94 (make-lexical-ref loc 'value var)))))
95
96
97; Generate code to set a fluid saved variable.
98
99(define (set-variable! loc sym module value)
100 (make-sequence loc
101 (list (ensure-fluid! loc sym module)
50abfe76
DK
102 (call-primitive loc 'fluid-set!
103 (make-module-ref loc module sym #t)
104 value))))
344927c3
DK
105
106
3a4b8635
DK
107; Process the bindings part of a let or let* expression; that is, check for
108; correctness and bring it to the form ((sym1 . val1) (sym2 . val2) ...).
109
110(define (process-let-bindings loc bindings)
111 (map (lambda (b)
112 (if (symbol? b)
113 (cons b 'nil)
114 (if (or (not (list? b))
115 (not (= (length b) 2)))
116 (report-error loc "expected symbol or list of 2 elements in let")
117 (if (not (symbol? (car b)))
118 (report-error loc "expected symbol in let")
119 (cons (car b) (cadr b))))))
120 bindings))
121
122
50abfe76
DK
123; Split the argument list of a lambda expression into required, optional and
124; rest arguments and also check it is actually valid.
125
126(define (split-lambda-arguments loc args)
127 (let iterate ((tail args)
128 (mode 'required)
129 (required '())
130 (optional '()))
131 (cond
132
133 ((null? tail)
134 (values (reverse required) (reverse optional) #f))
135
136 ((and (eq? mode 'required)
137 (eq? (car tail) '&optional))
138 (iterate (cdr tail) 'optional required optional))
139
140 ((eq? (car tail) '&rest)
141 (if (or (null? (cdr tail))
142 (not (null? (cddr tail))))
143 (report-error loc "expected exactly one symbol after &rest")
144 (values (reverse required) (reverse optional) (cadr tail))))
145
146 (else
147 (if (not (symbol? (car tail)))
148 (report-error loc "expected symbol in argument list, got" (car tail))
149 (case mode
150 ((required) (iterate (cdr tail) mode
151 (cons (car tail) required) optional))
152 ((optional) (iterate (cdr tail) mode
153 required (cons (car tail) optional)))
154 ((else) (error "invalid mode in split-lambda-arguments" mode))))))))
155
156
157; Compile a lambda expression. Things get a little complicated because TreeIL
158; does not allow optional arguments but only one rest argument, and also the
159; rest argument should be nil instead of '() for no values given. Because of
160; this, we have to do a little preprocessing to get everything done before the
161; real body is called.
162;
163; (lambda (a &optional b &rest c) body) should become:
164; (lambda (a_ . rest_)
165; (with-fluids* (list a b c) (list a_ nil nil)
166; (lambda ()
167; (if (not (null? rest_))
168; (begin
169; (fluid-set! b (car rest_))
170; (set! rest_ (cdr rest_))
171; (if (not (null? rest_))
172; (fluid-set! c rest_))))
173; body)))
174;
175; This is formulated quite imperatively, but I think in this case that is quite
176; clear and better than creating a lot of nested let's.
177
178(define (compile-lambda loc args body)
179 (call-with-values
180 (lambda ()
181 (split-lambda-arguments loc args))
182 (lambda (required optional rest)
183 ; FIXME: Ensure fluids there!
184 (let ((required-sym (map (lambda (sym) (gensym)) required))
185 (rest-sym (if (or rest (not (null? optional))) (gensym) '())))
186 (let ((real-args (append required-sym rest-sym)))
187 (make-lambda loc
188 real-args real-args '()
189 (call-primitive loc 'with-fluids*
190 (make-application loc (make-primitive-ref loc 'list)
191 (map (lambda (sym) (make-module-ref loc value-slot sym #t))
192 (append (append required optional)
193 (if rest (list rest) '()))))
194 (make-application loc (make-primitive-ref loc 'list)
195 (append (map (lambda (sym) (make-lexical-ref loc sym sym))
196 required-sym)
197 (map (lambda (sym) (nil-value loc))
198 (if (null? rest-sym)
199 optional
200 (append optional (list rest-sym))))))
201 (make-lambda loc '() '() '()
202 (make-sequence loc
203 (cons (process-optionals loc optional rest-sym)
204 (cons (process-rest loc rest rest-sym)
205 (map compile-expr body))))))))))))
206
207; Build the code to handle setting of optional arguments that are present
208; and updating the rest list.
209(define (process-optionals loc optional rest-sym)
210 (let iterate ((tail optional))
211 (if (null? tail)
212 (make-void loc)
213 (make-conditional loc
214 (call-primitive loc 'null? (make-lexical-ref loc rest-sym rest-sym))
215 (make-void loc)
216 (make-sequence loc
217 (list (set-variable! loc (car tail) value-slot
218 (call-primitive loc 'car
219 (make-lexical-ref loc rest-sym rest-sym)))
220 (make-lexical-set loc rest-sym rest-sym
221 (call-primitive loc 'cdr
222 (make-lexical-ref loc rest-sym rest-sym)))
223 (iterate (cdr tail))))))))
224
225; This builds the code to set the rest variable to nil if it is empty.
226(define (process-rest loc rest rest-sym)
227 (let ((rest-empty (call-primitive loc 'null?
228 (make-lexical-ref loc rest-sym rest-sym))))
229 (cond
230 (rest
231 (make-conditional loc rest-empty
232 (make-void loc)
233 (set-variable! loc rest value-slot
234 (make-lexical-ref loc rest-sym rest-sym))))
235 ((not (null? rest-sym))
236 (make-conditional loc rest-empty
237 (make-void loc)
238 (runtime-error loc "too many arguments and no rest argument")))
239 (else (make-void loc)))))
240
241
51248e6e
DK
242; Compile a symbol expression. This is a variable reference or maybe some
243; special value like nil.
244
245(define (compile-symbol loc sym)
246 (case sym
247
fdfb36de 248 ((nil) (nil-value loc))
51248e6e 249
fdfb36de 250 ((t) (t-value loc))
51248e6e 251
51248e6e 252 (else
344927c3 253 (reference-with-check loc sym value-slot))))
51248e6e
DK
254
255
256; Compile a pair-expression (that is, any structure-like construct).
257
258(define (compile-pair loc expr)
259 (pmatch expr
260
261 ((progn . ,forms)
262 (make-sequence loc (map compile-expr forms)))
263
264 ((if ,condition ,ifclause)
265 (make-conditional loc (compile-expr condition)
266 (compile-expr ifclause)
4530432e 267 (nil-value loc)))
51248e6e
DK
268 ((if ,condition ,ifclause ,elseclause)
269 (make-conditional loc (compile-expr condition)
270 (compile-expr ifclause)
271 (compile-expr elseclause)))
272 ((if ,condition ,ifclause . ,elses)
273 (make-conditional loc (compile-expr condition)
274 (compile-expr ifclause)
275 (make-sequence loc (map compile-expr elses))))
276
a4316739
DK
277 ; For (cond ...) forms, a special case is a (condition) clause without
278 ; body. In this case, the value of condition itself should be returned,
279 ; and thus is saved in a local variable for testing and returning, if it
280 ; is found true.
51248e6e
DK
281 ((cond . ,clauses) (guard (and-map (lambda (el)
282 (and (list? el) (not (null? el))))
283 clauses))
284 (let iterate ((tail clauses))
285 (if (null? tail)
4530432e 286 (nil-value loc)
51248e6e 287 (let ((cur (car tail)))
a4316739
DK
288 (if (null? (cdr cur))
289 (let ((var (gensym)))
290 (make-let loc
291 '(condition) `(,var) `(,(compile-expr (car cur)))
292 (make-conditional loc
293 (make-lexical-ref loc 'condition var)
294 (make-lexical-ref loc 'condition var)
295 (iterate (cdr tail)))))
296 (make-conditional loc
297 (compile-expr (car cur))
298 (make-sequence loc (map compile-expr (cdr cur)))
299 (iterate (cdr tail))))))))
51248e6e 300
fdfb36de 301 ((and) (t-value loc))
51248e6e
DK
302 ((and . ,expressions)
303 (let iterate ((tail expressions))
304 (if (null? (cdr tail))
305 (compile-expr (car tail))
306 (make-conditional loc
307 (compile-expr (car tail))
308 (iterate (cdr tail))
4530432e 309 (nil-value loc)))))
51248e6e 310
fdfb36de
DK
311 ((or . ,expressions)
312 (let iterate ((tail expressions))
313 (if (null? tail)
314 (nil-value loc)
315 (let ((var (gensym)))
316 (make-let loc
317 '(condition) `(,var) `(,(compile-expr (car tail)))
318 (make-conditional loc
319 (make-lexical-ref loc 'condition var)
320 (make-lexical-ref loc 'condition var)
321 (iterate (cdr tail))))))))
322
344927c3
DK
323 ; Build a set form for possibly multiple values. The code is not formulated
324 ; tail recursive because it is clearer this way and large lists of symbol
325 ; expression pairs are very unlikely.
326 ((setq . ,args)
327 (make-sequence loc
328 (let iterate ((tail args))
329 (if (null? tail)
330 (list (make-void loc))
331 (let ((sym (car tail))
332 (tailtail (cdr tail)))
333 (if (not (symbol? sym))
334 (report-error loc "expected symbol in setq")
335 (if (null? tailtail)
336 (report-error loc "missing value for symbol in setq" sym)
337 (let* ((val (compile-expr (car tailtail)))
338 (op (set-variable! loc sym value-slot val)))
339 (cons op (iterate (cdr tailtail)))))))))))
340
3a4b8635
DK
341 ; Let is done with a single call to with-fluids* binding them locally to new
342 ; values.
343 ((let ,bindings . ,body) (guard (and (list? bindings)
344 (list? body)
345 (not (null? bindings))
346 (not (null? body))))
347 (let ((bind (process-let-bindings loc bindings)))
50abfe76
DK
348 (call-primitive loc 'with-fluids*
349 (make-application loc (make-primitive-ref loc 'list)
350 (map (lambda (el)
351 (make-module-ref loc value-slot (car el) #t))
352 bind))
353 (make-application loc (make-primitive-ref loc 'list)
354 (map (lambda (el)
355 (compile-expr (cdr el)))
356 bind))
357 (make-lambda loc '() '() '()
358 (make-sequence loc (map compile-expr body))))))
3a4b8635
DK
359
360 ; Let* is compiled to a cascaded set of with-fluid* for each binding in turn
361 ; so that each one already sees the preceding bindings.
362 ((let* ,bindings . ,body) (guard (and (list? bindings)
363 (list? body)
364 (not (null? bindings))
365 (not (null? body))))
366 (let ((bind (process-let-bindings loc bindings)))
367 (let iterate ((tail bind))
368 (if (null? tail)
369 (make-sequence loc (map compile-expr body))
50abfe76
DK
370 (call-primitive loc 'with-fluid*
371 (make-module-ref loc value-slot (caar tail) #t)
372 (compile-expr (cdar tail))
373 (make-lambda loc '() '() '() (iterate (cdr tail))))))))
3a4b8635 374
d221c18b
DK
375 ; A while construct is transformed into a tail-recursive loop like this:
376 ; (letrec ((iterate (lambda ()
377 ; (if condition
378 ; (begin body
379 ; (iterate))
380 ; %nil))))
381 ; (iterate))
382 ((while ,condition . ,body)
383 (let* ((itersym (gensym))
384 (compiled-body (map compile-expr body))
385 (iter-call (make-application loc
386 (make-lexical-ref loc 'iterate itersym)
387 (list)))
388 (full-body (make-sequence loc
389 (append compiled-body (list iter-call))))
390 (lambda-body (make-conditional loc
391 (compile-expr condition)
392 full-body
393 (nil-value loc)))
394 (iter-thunk (make-lambda loc '() '() '() lambda-body)))
395 (make-letrec loc '(iterate) (list itersym) (list iter-thunk)
396 iter-call)))
397
50abfe76
DK
398 ; Either (lambda ...) or (function (lambda ...)) denotes a lambda-expression
399 ; that should be compiled.
400 ((lambda ,args . ,body) (guard (not (null? body)))
401 (compile-lambda loc args body))
402 ((function (lambda ,args . ,body)) (guard (not (null? body)))
403 (compile-lambda loc args body))
404
51248e6e
DK
405 (('quote ,val)
406 (make-const loc val))
407
408 (else
344927c3 409 (report-error loc "unrecognized elisp" expr))))
51248e6e
DK
410
411
412; Compile a single expression to TreeIL.
413
414(define (compile-expr expr)
415 (let ((loc (location expr)))
416 (cond
417 ((symbol? expr)
418 (compile-symbol loc expr))
419 ((pair? expr)
420 (compile-pair loc expr))
421 (else (make-const loc expr)))))
422
423
424; Entry point for compilation to TreeIL.
425
426(define (compile-tree-il expr env opts)
427 (values
428 (compile-expr expr)
429 env
430 env))