srfi/34: work with else frmo racket or mzscheme

This commit is contained in:
Matthew Flatt 2014-01-31 06:57:35 -07:00
parent 9eacbec35e
commit b6218097d2
4 changed files with 54 additions and 15 deletions

View File

@ -707,6 +707,10 @@ This SRFI's syntax is part of Racket's default reader.
(raise #f "raise")
)]
An @racket[else] is recognized as either the one from
@racketmodname[racket/base] or as an identifier with the symbolic form
@racket['else] and no binding.
@; ----------------------------------------
@srfi[35]{Conditions}

View File

@ -1,7 +1,10 @@
#lang racket/base
(require (for-syntax racket/base
syntax/stx))
;; SRFI 34 for PLT Scheme
;; Zhu Chongkai, April 2005
;; <mrmathematica@yahoo.com>
(module exception mzscheme
(provide with-exception-handler
guard
@ -12,14 +15,18 @@
((_ handler thunk)
(with-handlers (((lambda (exn) #t) handler)) (thunk)))))
(define-syntax guard
(syntax-rules (else)
((_ (var clause ... (else de ...)) e1 e2 ...)
(define-syntax (guard stx)
(syntax-case stx ()
((_ (var clause ... (els de ...)) e1 e2 ...)
(and (identifier? #'els)
(module-or-top-identifier=? #'els #'else))
(syntax/loc stx
(with-handlers (((lambda (exn) #t)
(lambda (var) (cond clause ...
(else de ...)))))
e1 e2 ...))
e1 e2 ...)))
((_ (var clause ...) e1 e2 ...)
(syntax/loc stx
(with-handlers (((lambda (exn) #t)
(lambda (var) (cond clause ...
(else (raise var))))))

View File

@ -0,0 +1,18 @@
#lang racket/base
(require srfi/34)
(guard (condition
(else
(display "condition: ")
(write condition)
(newline)
'exception))
(+ 1 (raise 'an-error)))
(guard (condition
(#t ; <- not an identifier
(display "condition: ")
(write condition)
(newline)
'exception))
(+ 1 (raise 'an-error)))

View File

@ -0,0 +1,10 @@
#lang mzscheme
(require srfi/34)
(guard (condition
(else
(display "condition: ")
(write condition)
(newline)
'exception))
(+ 1 (raise 'an-error)))