srfi-34.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; srfi-34.scm --- Exception handling for programs
  2. ;; Copyright (C) 2003, 2006, 2008 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Neil Jerram <neil@ossau.uklinux.net>
  18. ;;; Commentary:
  19. ;; This is an implementation of SRFI-34: Exception Handling for
  20. ;; Programs. For documentation please see the SRFI-34 document; this
  21. ;; module is not yet documented at all in the Guile manual.
  22. ;;; Code:
  23. (define-module (srfi srfi-34)
  24. #:export (with-exception-handler
  25. raise)
  26. #:export-syntax (guard))
  27. (cond-expand-provide (current-module) '(srfi-34))
  28. (define throw-key 'srfi-34)
  29. (define (with-exception-handler handler thunk)
  30. "Returns the result(s) of invoking THUNK. HANDLER must be a
  31. procedure that accepts one argument. It is installed as the current
  32. exception handler for the dynamic extent (as determined by
  33. dynamic-wind) of the invocation of THUNK."
  34. (with-throw-handler throw-key
  35. thunk
  36. (lambda (key obj)
  37. (handler obj))))
  38. (define (raise obj)
  39. "Invokes the current exception handler on OBJ. The handler is
  40. called in the dynamic environment of the call to raise, except that
  41. the current exception handler is that in place for the call to
  42. with-exception-handler that installed the handler being called. The
  43. handler's continuation is otherwise unspecified."
  44. (throw throw-key obj))
  45. (define-macro (guard var+clauses . body)
  46. "Syntax: (guard (<var> <clause1> <clause2> ...) <body>)
  47. Each <clause> should have the same form as a `cond' clause.
  48. Semantics: Evaluating a guard form evaluates <body> with an exception
  49. handler that binds the raised object to <var> and within the scope of
  50. that binding evaluates the clauses as if they were the clauses of a
  51. cond expression. That implicit cond expression is evaluated with the
  52. continuation and dynamic environment of the guard expression. If
  53. every <clause>'s <test> evaluates to false and there is no else
  54. clause, then raise is re-invoked on the raised object within the
  55. dynamic environment of the original call to raise except that the
  56. current exception handler is that of the guard expression."
  57. (let ((var (car var+clauses))
  58. (clauses (cdr var+clauses)))
  59. `(catch ',throw-key
  60. (lambda ()
  61. ,@body)
  62. (lambda (key ,var)
  63. (cond ,@(if (eq? (caar (last-pair clauses)) 'else)
  64. clauses
  65. (append clauses
  66. `((else (throw key ,var))))))))))
  67. ;;; (srfi srfi-34) ends here.