Re-implement `guard'

* module/ice-9/exceptions.scm (guard): Add guard definition that
  re-propagates from original continuation, runs consequents in tail
  position in guard continuation, and doesn't rewind the stack.
* module/srfi/srfi-34.scm:
* module/rnrs/exceptions.scm (guard): Re-export from (ice-9
  exceptions).
This commit is contained in:
Andy Wingo 2020-01-10 21:42:26 +01:00
commit 8068994ba8
3 changed files with 74 additions and 62 deletions

View file

@ -1,6 +1,6 @@
;;; exceptions.scm --- The R6RS exceptions library
;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
;; Copyright (C) 2010, 2011, 2013, 2020 Free Software Foundation, Inc.
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
@ -23,20 +23,4 @@
(rnrs control (6))
(rnrs conditions (6))
(rename (ice-9 exceptions)
(raise-exception raise)))
(define-syntax guard0
(syntax-rules ()
((_ (variable cond-clause ...) . body)
(call/cc (lambda (continuation)
(with-exception-handler
(lambda (variable)
(continuation (cond cond-clause ...)))
(lambda () . body)))))))
(define-syntax guard
(syntax-rules (else)
((_ (variable cond-clause ... . ((else else-clause ...))) . body)
(guard0 (variable cond-clause ... (else else-clause ...)) . body))
((_ (variable cond-clause ...) . body)
(guard0 (variable cond-clause ... (else (raise variable))) . body)))))
(raise-exception raise))))