You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
677 B
Racket
19 lines
677 B
Racket
#lang racket/base
|
|
(require (for-syntax racket/base syntax/parse) br/macro)
|
|
(provide (all-defined-out) (all-from-out br/macro))
|
|
|
|
(define-syntax (define-cases stx)
|
|
(syntax-parse stx
|
|
#:literals (syntax)
|
|
[(_ id:id)
|
|
(raise-syntax-error 'define-cases "no cases given" (syntax->datum #'id))]
|
|
[(_ id:id [(_ . pat-args:expr) . body:expr] ...)
|
|
#'(define id
|
|
(case-lambda
|
|
[pat-args . body] ...
|
|
[rest-pat (apply raise-arity-error 'id (normalize-arity (map length '(pat-args ...))) rest-pat)]))]
|
|
[else (raise-syntax-error
|
|
'define-cases
|
|
"no matching case for calling pattern"
|
|
(syntax->datum stx))]))
|