860ce5e7ef5f4a412e805187e01204873778fb28
[bpt/guile.git] / module / ice-9 / and-let-star.scm
1 ;;;; and-let-star.scm --- and-let* syntactic form (SRFI-2) for Guile
2 ;;;;
3 ;;;; Copyright (C) 1999, 2001, 2004, 2006, 2013 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 (define-module (ice-9 and-let-star)
20 :export-syntax (and-let*))
21
22 (define-syntax %and-let*
23 (lambda (form)
24 (syntax-case form ()
25 ((_ orig-form ())
26 #'#t)
27 ((_ orig-form () body bodies ...)
28 #'(begin body bodies ...))
29 ((_ orig-form ((var exp) c ...) body ...)
30 (identifier? #'var)
31 #'(let ((var exp))
32 (if var
33 (%and-let* orig-form (c ...) body ...)
34 #f)))
35 ((_ orig-form ((exp) c ...) body ...)
36 #'(and exp (%and-let* orig-form (c ...) body ...)))
37 ((_ orig-form (var c ...) body ...)
38 (identifier? #'var)
39 #'(and var (%and-let* orig-form (c ...) body ...)))
40 ((_ orig-form (bad-clause c ...) body ...)
41 (syntax-violation 'and-let* "Bad clause" #'orig-form #'bad-clause)))))
42
43 (define-syntax and-let*
44 (lambda (form)
45 (syntax-case form ()
46 ((_ (c ...) body ...)
47 #`(%and-let* #,form (c ...) body ...)))))
48
49 (cond-expand-provide (current-module) '(srfi-2))