Guest User

Untitled

a guest
May 25th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. ;;;
  2. ;;; Outer "driver" macro; the meat is in pmatch-expand-pattern.
  3. ;;;
  4. (define-syntax pmatch
  5. (syntax-rules ()
  6. ((pmatch value-expr (pattern . exprs) . clauses)
  7. (let* ((value value-expr)
  8. (try-next-clause
  9. (lambda () (pmatch value . clauses))))
  10. (pmatch-expand-pattern pattern
  11. value
  12. ;; success-k
  13. (begin . exprs)
  14. ;; failure-k
  15. (try-next-clause))))))
  16.  
  17. (define-syntax pmatch-expand-pattern
  18. (lambda (stx)
  19. (syntax-case stx ()
  20.  
  21. ;; Cases for constants and quoted symbols omitted, but they're trivial.
  22.  
  23. ;; Match a pair pattern. Note that failure-k is expanded three times;
  24. ;; that's why pmatch encapsulates its expansion inside a thunk!
  25. ((pmatch-expand-pattern (head-pat . tail-pat) value success-k failure-k)
  26. (syntax
  27. (if (pair? value)
  28. (pmatch-expand-pattern head-pat
  29. (car value)
  30. ;; If we successfully match the head, then
  31. ;; the success continuation is a recursive
  32. ;; attempt to match the tail...
  33. (pmatch-expand-pattern tail-pat
  34. (cdr value)
  35. success-k
  36. failure-k)
  37. failure-k))
  38. failure-k))
  39.  
  40. ;; Match an identifier pattern. Always succeeds, binds identifier
  41. ;; to value
  42. ((pmatch-expand-pattern identifier value success-k failure-k)
  43. (identifier? (syntax identifier))
  44. (syntax (let ((identifier value)) success-k)))
  45. )))
Advertisement
Add Comment
Please, Sign In to add comment