Skip to content

Commit 5748a74

Browse files
committed
Simplify guard form using call-in-continuation
Chez Scheme 10's new call-in-continuation procedure allows implementing guard in a simpler and likely more efficient way.
1 parent ae31756 commit 5748a74

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

s/exceptions.ss

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
;;; exceptions.ss
22
;;; Copyright 1984-2017 Cisco Systems, Inc.
3-
;;;
3+
;;;
44
;;; Licensed under the Apache License, Version 2.0 (the "License");
55
;;; you may not use this file except in compliance with the License.
66
;;; You may obtain a copy of the License at
7-
;;;
7+
;;;
88
;;; http://www.apache.org/licenses/LICENSE-2.0
9-
;;;
9+
;;;
1010
;;; Unless required by applicable law or agreed to in writing, software
1111
;;; distributed under the License is distributed on an "AS IS" BASIS,
1212
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -94,7 +94,7 @@ TODO:
9494
[(message-condition? c)
9595
(let ([irritants (if (irritants-condition? c) (condition-irritants c) '())])
9696
(case (and (list? irritants) (length irritants))
97-
[(0)
97+
[(0)
9898
($report-string op
9999
(and prefix? (if (warning-only? c) "warning" "exception"))
100100
(and (who-condition? c) (condition-who c))
@@ -263,37 +263,27 @@ TODO:
263263
(set-who! $guard
264264
(lambda (supply-else? guards body)
265265
(if supply-else?
266-
((call/cc
267-
(lambda (kouter)
268-
(let ([original-handler-stack ($current-handler-stack)])
269-
(with-exception-handler
270-
(lambda (arg)
271-
((call/cc
266+
(call/cc
267+
(lambda (kouter)
268+
(let ([original-handler-stack ($current-handler-stack)])
269+
(with-exception-handler
270+
(lambda (arg)
271+
(call/cc
272272
(lambda (kinner)
273-
(kouter
273+
(call-in-continuation kouter
274274
(lambda ()
275275
(guards arg
276276
(lambda ()
277-
(kinner
277+
(call-in-continuation kinner
278278
(lambda ()
279279
(parameterize ([$current-handler-stack original-handler-stack])
280-
(raise-continuable arg))))))))))))
281-
(lambda ()
282-
(call-with-values
283-
body
284-
(case-lambda
285-
[(x) (lambda () x)]
286-
[vals (lambda () (apply values vals))]))))))))
287-
((call/cc
288-
(lambda (k)
289-
(with-exception-handler
290-
(lambda (arg) (k (lambda () (guards arg))))
291-
(lambda ()
292-
(call-with-values
293-
body
294-
(case-lambda
295-
[(x) (lambda () x)]
296-
[vals (lambda () (apply values vals))]))))))))))
280+
(raise-continuable arg)))))))))))
281+
body))))
282+
(call/cc
283+
(lambda (k)
284+
(with-exception-handler
285+
(lambda (arg) (call-in-continuation k (lambda () (guards arg))))
286+
body))))))
297287
)
298288

299289
(define-syntax guard
@@ -471,7 +461,7 @@ TODO:
471461
;;; defining its child types, even though the system is compiled with
472462
;;; (eval-syntax-expanders-when) not including compile.
473463
(begin
474-
(let-syntax ([a (syntax-rules ()
464+
(let-syntax ([a (syntax-rules ()
475465
[(_ &condition) ; leave only &condition visible
476466
(define-record-type (&condition make-simple-condition simple-condition?)
477467
(nongenerative #{&condition oyb459ue1fphfx4-a}))])])
@@ -706,7 +696,7 @@ TODO:
706696
(for-each
707697
(lambda (m) (unless (string? m) ($oops who "~s is not a string" m)))
708698
messages)
709-
(error-help #f who #f
699+
(error-help #f who #f
710700
(if (null? messages) "invalid syntax" (apply string-append messages))
711701
#f (make-syntax-violation form #f))))
712702

0 commit comments

Comments
 (0)