6ab8852ba13ccdbadca531efd157d4cc40b9a21d
[bpt/guile.git] / module / language / elisp / runtime / macros.scm
1 ;;; Guile Emacs Lisp
2
3 ;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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
18
19 ;;; Code:
20
21 (define-module (language elisp runtime macros)
22 #:use-module (language elisp runtime))
23
24 ;;; This module contains the macro definitions of elisp symbols. In
25 ;;; contrast to the other runtime modules, those are used directly
26 ;;; during compilation, of course, so not really in runtime. But I
27 ;;; think it fits well to the others here.
28
29 (built-in-macro lambda
30 (lambda cdr
31 `(function (lambda ,@cdr))))
32
33 ;;; The prog1 and prog2 constructs can easily be defined as macros using
34 ;;; progn and some lexical-let's to save the intermediate value to
35 ;;; return at the end.
36
37 (built-in-macro prog1
38 (lambda (form1 . rest)
39 (let ((temp (gensym)))
40 `(without-void-checks (,temp)
41 (lexical-let ((,temp ,form1))
42 ,@rest
43 ,temp)))))
44
45 (built-in-macro prog2
46 (lambda (form1 form2 . rest)
47 `(progn ,form1 (prog1 ,form2 ,@rest))))
48
49 ;;; Define the conditionals when and unless as macros.
50
51 (built-in-macro when
52 (lambda (condition . thens)
53 `(if ,condition (progn ,@thens) nil)))
54
55 (built-in-macro unless
56 (lambda (condition . elses)
57 `(if ,condition nil (progn ,@elses))))
58
59 ;;; Impement the cond form as nested if's. A special case is a
60 ;;; (condition) subform, in which case we need to return the condition
61 ;;; itself if it is true and thus save it in a local variable before
62 ;;; testing it.
63
64 (built-in-macro cond
65 (lambda (. clauses)
66 (let iterate ((tail clauses))
67 (if (null? tail)
68 'nil
69 (let ((cur (car tail))
70 (rest (iterate (cdr tail))))
71 (prim cond
72 ((prim or (not (list? cur)) (null? cur))
73 (macro-error "invalid clause in cond" cur))
74 ((null? (cdr cur))
75 (let ((var (gensym)))
76 `(without-void-checks (,var)
77 (lexical-let ((,var ,(car cur)))
78 (if ,var
79 ,var
80 ,rest)))))
81 (else
82 `(if ,(car cur)
83 (progn ,@(cdr cur))
84 ,rest))))))))
85
86 ;;; The and and or forms can also be easily defined with macros.
87
88 (built-in-macro and
89 (case-lambda
90 (() 't)
91 ((x) x)
92 ((x . args)
93 (let iterate ((x x) (tail args))
94 (if (null? tail)
95 x
96 `(if ,x
97 ,(iterate (car tail) (cdr tail))
98 nil))))))
99
100 (built-in-macro or
101 (case-lambda
102 (() 'nil)
103 ((x) x)
104 ((x . args)
105 (let iterate ((x x) (tail args))
106 (if (null? tail)
107 x
108 (let ((var (gensym)))
109 `(without-void-checks
110 (,var)
111 (lexical-let ((,var ,x))
112 (if ,var
113 ,var
114 ,(iterate (car tail) (cdr tail)))))))))))
115
116 ;;; Define the dotimes and dolist iteration macros.
117
118 (built-in-macro dotimes
119 (lambda (args . body)
120 (if (prim or
121 (not (list? args))
122 (< (length args) 2)
123 (> (length args) 3))
124 (macro-error "invalid dotimes arguments" args)
125 (let ((var (car args))
126 (count (cadr args)))
127 (if (not (symbol? var))
128 (macro-error "expected symbol as dotimes variable"))
129 `(let ((,var 0))
130 (while ((guile-primitive <) ,var ,count)
131 ,@body
132 (setq ,var ((guile-primitive 1+) ,var)))
133 ,@(if (= (length args) 3)
134 (list (caddr args))
135 '()))))))
136
137 (built-in-macro dolist
138 (lambda (args . body)
139 (if (prim or
140 (not (list? args))
141 (< (length args) 2)
142 (> (length args) 3))
143 (macro-error "invalid dolist arguments" args)
144 (let ((var (car args))
145 (iter-list (cadr args))
146 (tailvar (gensym)))
147 (if (not (symbol? var))
148 (macro-error "expected symbol as dolist variable")
149 `(let (,var)
150 (without-void-checks (,tailvar)
151 (lexical-let ((,tailvar ,iter-list))
152 (while ((guile-primitive not)
153 ((guile-primitive null?) ,tailvar))
154 (setq ,var ((guile-primitive car) ,tailvar))
155 ,@body
156 (setq ,tailvar ((guile-primitive cdr) ,tailvar)))
157 ,@(if (= (length args) 3)
158 (list (caddr args))
159 '())))))))))
160
161 ;;; Exception handling. unwind-protect and catch are implemented as
162 ;;; macros (throw is a built-in function).
163
164 ;;; catch and throw can mainly be implemented directly using Guile's
165 ;;; primitives for exceptions, the only difficulty is that the keys used
166 ;;; within Guile must be symbols, while elisp allows any value and
167 ;;; checks for matches using eq (eq?). We handle this by using always #t
168 ;;; as key for the Guile primitives and check for matches inside the
169 ;;; handler; if the elisp keys are not eq?, we rethrow the exception.
170
171 (built-in-macro catch
172 (lambda (tag . body)
173 (if (null? body)
174 (macro-error "catch with empty body"))
175 (let ((tagsym (gensym)))
176 `(lexical-let ((,tagsym ,tag))
177 ((guile-primitive catch)
178 #t
179 (lambda () ,@body)
180 ,(let* ((dummy-key (gensym))
181 (elisp-key (gensym))
182 (value (gensym))
183 (arglist `(,dummy-key ,elisp-key ,value)))
184 `(with-always-lexical
185 ,arglist
186 (lambda ,arglist
187 (if (eq ,elisp-key ,tagsym)
188 ,value
189 ((guile-primitive throw) ,dummy-key ,elisp-key
190 ,value))))))))))
191
192 ;;; unwind-protect is just some weaker construct as dynamic-wind, so
193 ;;; straight-forward to implement.
194
195 (built-in-macro unwind-protect
196 (lambda (body . clean-ups)
197 (if (null? clean-ups)
198 (macro-error "unwind-protect without cleanup code"))
199 `((guile-primitive dynamic-wind)
200 (lambda () nil)
201 (lambda () ,body)
202 (lambda () ,@clean-ups))))
203
204 ;;; Pop off the first element from a list or push one to it.
205
206 (built-in-macro pop
207 (lambda (list-name)
208 `(prog1 (car ,list-name)
209 (setq ,list-name (cdr ,list-name)))))
210
211 (built-in-macro push
212 (lambda (new-el list-name)
213 `(setq ,list-name (cons ,new-el ,list-name))))