and-let-star.scm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;;; and-let-star.scm --- and-let* syntactic form (SRFI-2) for Guile
  2. ;;;;
  3. ;;;; Copyright (C) 1999, 2001, 2004, 2006, 2013,
  4. ;;;; 2015 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (ice-9 and-let-star)
  20. :export-syntax (and-let*))
  21. (define-syntax %and-let*
  22. (lambda (form)
  23. (syntax-case form ()
  24. ;; Handle zero-clauses special-case.
  25. ((_ orig-form () . body)
  26. #'(begin #t . body))
  27. ;; Reduce clauses down to one regardless of body.
  28. ((_ orig-form ((var expr) rest . rest*) . body)
  29. (identifier? #'var)
  30. #'(let ((var expr))
  31. (and var (%and-let* orig-form (rest . rest*) . body))))
  32. ((_ orig-form ((expr) rest . rest*) . body)
  33. #'(and expr (%and-let* orig-form (rest . rest*) . body)))
  34. ((_ orig-form (var rest . rest*) . body)
  35. (identifier? #'var)
  36. #'(and var (%and-let* orig-form (rest . rest*) . body)))
  37. ;; Handle 1-clause cases without a body.
  38. ((_ orig-form ((var expr)))
  39. (identifier? #'var)
  40. #'expr)
  41. ((_ orig-form ((expr)))
  42. #'expr)
  43. ((_ orig-form (var))
  44. (identifier? #'var)
  45. #'var)
  46. ;; Handle 1-clause cases with a body.
  47. ((_ orig-form ((var expr)) . body)
  48. (identifier? #'var)
  49. #'(let ((var expr))
  50. (and var (begin . body))))
  51. ((_ orig-form ((expr)) . body)
  52. #'(and expr (begin . body)))
  53. ((_ orig-form (var) . body)
  54. (identifier? #'var)
  55. #'(and var (begin . body)))
  56. ;; Handle bad clauses.
  57. ((_ orig-form (bad-clause . rest) . body)
  58. (syntax-violation 'and-let* "Bad clause" #'orig-form #'bad-clause)))))
  59. (define-syntax and-let*
  60. (lambda (form)
  61. (syntax-case form ()
  62. ((_ (c ...) body ...)
  63. #`(%and-let* #,form (c ...) body ...)))))
  64. (cond-expand-provide (current-module) '(srfi-2))