3 ;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (language elisp runtime macros)
22 #:use-module (language elisp runtime))
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.
29 (built-in-macro lambda
31 `(function (lambda ,@cdr))))
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.
38 (lambda (form1 . rest)
39 (let ((temp (gensym)))
40 `(lexical-let ((,temp ,form1))
45 (lambda (form1 form2 . rest)
46 `(progn ,form1 (prog1 ,form2 ,@rest))))
48 ;;; Define the conditionals when and unless as macros.
51 (lambda (condition . thens)
52 `(if ,condition (progn ,@thens) nil)))
54 (built-in-macro unless
55 (lambda (condition . elses)
56 `(if ,condition nil (progn ,@elses))))
58 ;;; Impement the cond form as nested if's. A special case is a
59 ;;; (condition) subform, in which case we need to return the condition
60 ;;; itself if it is true and thus save it in a local variable before
65 (let iterate ((tail clauses))
68 (let ((cur (car tail))
69 (rest (iterate (cdr tail))))
71 ((prim or (not (list? cur)) (null? cur))
72 (macro-error "invalid clause in cond" cur))
75 `(lexical-let ((,var ,(car cur)))
84 ;;; The `and' and `or' forms can also be easily defined with macros.
91 (let iterate ((x x) (tail args))
95 ,(iterate (car tail) (cdr tail))
103 (let iterate ((x x) (tail args))
106 (let ((var (gensym)))
107 `(lexical-let ((,var ,x))
110 ,(iterate (car tail) (cdr tail))))))))))
112 ;;; Define the dotimes and dolist iteration macros.
114 (built-in-macro dotimes
115 (lambda (args . body)
120 (macro-error "invalid dotimes arguments" args)
121 (let ((var (car args))
123 (if (not (symbol? var))
124 (macro-error "expected symbol as dotimes variable"))
126 (while ((guile-primitive <) ,var ,count)
128 (setq ,var ((guile-primitive 1+) ,var)))
129 ,@(if (= (length args) 3)
133 (built-in-macro dolist
134 (lambda (args . body)
139 (macro-error "invalid dolist arguments" args)
140 (let ((var (car args))
141 (iter-list (cadr args))
143 (if (not (symbol? var))
144 (macro-error "expected symbol as dolist variable")
146 (lexical-let ((,tailvar ,iter-list))
147 (while ((guile-primitive not)
148 ((guile-primitive null?) ,tailvar))
149 (setq ,var ((guile-primitive car) ,tailvar))
151 (setq ,tailvar ((guile-primitive cdr) ,tailvar)))
152 ,@(if (= (length args) 3)
156 ;;; Exception handling. unwind-protect and catch are implemented as
157 ;;; macros (throw is a built-in function).
159 ;;; catch and throw can mainly be implemented directly using Guile's
160 ;;; primitives for exceptions, the only difficulty is that the keys used
161 ;;; within Guile must be symbols, while elisp allows any value and
162 ;;; checks for matches using eq (eq?). We handle this by using always #t
163 ;;; as key for the Guile primitives and check for matches inside the
164 ;;; handler; if the elisp keys are not eq?, we rethrow the exception.
166 (built-in-macro catch
169 (macro-error "catch with empty body"))
170 (let ((tagsym (gensym)))
171 `(lexical-let ((,tagsym ,tag))
172 ((guile-primitive catch)
175 ,(let* ((dummy-key (gensym))
178 (arglist `(,dummy-key ,elisp-key ,value)))
179 `(with-always-lexical
182 (if (eq ,elisp-key ,tagsym)
184 ((guile-primitive throw) ,dummy-key ,elisp-key
187 ;;; unwind-protect is just some weaker construct as dynamic-wind, so
188 ;;; straight-forward to implement.
190 (built-in-macro unwind-protect
191 (lambda (body . clean-ups)
192 (if (null? clean-ups)
193 (macro-error "unwind-protect without cleanup code"))
194 `((guile-primitive dynamic-wind)
197 (lambda () ,@clean-ups))))
199 ;;; Pop off the first element from a list or push one to it.
203 `(prog1 (car ,list-name)
204 (setq ,list-name (cdr ,list-name)))))
207 (lambda (new-el list-name)
208 `(setq ,list-name (cons ,new-el ,list-name))))