Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;
- ;;; Outer "driver" macro; the meat is in pmatch-expand-pattern.
- ;;;
- (define-syntax pmatch
- (syntax-rules ()
- ((pmatch value-expr (pattern . exprs) . clauses)
- (let* ((value value-expr)
- (try-next-clause
- (lambda () (pmatch value . clauses))))
- (pmatch-expand-pattern pattern
- value
- ;; success-k
- (begin . exprs)
- ;; failure-k
- (try-next-clause))))))
- (define-syntax pmatch-expand-pattern
- (lambda (stx)
- (syntax-case stx ()
- ;; Cases for constants and quoted symbols omitted, but they're trivial.
- ;; Match a pair pattern. Note that failure-k is expanded three times;
- ;; that's why pmatch encapsulates its expansion inside a thunk!
- ((pmatch-expand-pattern (head-pat . tail-pat) value success-k failure-k)
- (syntax
- (if (pair? value)
- (pmatch-expand-pattern head-pat
- (car value)
- ;; If we successfully match the head, then
- ;; the success continuation is a recursive
- ;; attempt to match the tail...
- (pmatch-expand-pattern tail-pat
- (cdr value)
- success-k
- failure-k)
- failure-k))
- failure-k))
- ;; Match an identifier pattern. Always succeeds, binds identifier
- ;; to value
- ((pmatch-expand-pattern identifier value success-k failure-k)
- (identifier? (syntax identifier))
- (syntax (let ((identifier value)) success-k)))
- )))
Advertisement
Add Comment
Please, Sign In to add comment