You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(define-syntax-parameter break
(lambda (stx)
(raise-syntax-error (syntax-e stx) "can only be used inside `while`")))
(define-syntax-parse-rule (while condition body ...)
(call/ec
(λ (return)
(syntax-parameterize ([break (make-rename-transformer #'return)])
(let loop ()
(when condition
(begin body ...
(loop))))))))
This uses an escape continuation to provide the semantics of break, and leverages it using a syntax parameter so that the continuation is accessible in the lexical scope of the while body, and also so that break is a syntax error outside the while loop.
Example
(define x 5)
(while (> x 0)
(displayln x)
(set! x (sub1 x)))
(set! x 5)
(while #t
(displayln x)
(set! x (sub1 x))
(unless (> x 0)
(break)))
Licence
This code and all associated text involved in this submission is released as public domain.
The text was updated successfully, but these errors were encountered:
bennn
added a commit
to syntax-objects/syntax-parse-example
that referenced
this issue
Oct 11, 2021
Macro
This uses an escape continuation to provide the semantics of
break
, and leverages it using a syntax parameter so that the continuation is accessible in the lexical scope of the while body, and also so thatbreak
is a syntax error outside the while loop.Example
Licence
This code and all associated text involved in this submission is released as public domain.
The text was updated successfully, but these errors were encountered: