|
|
@ -1,9 +1,19 @@
|
|
|
|
#lang racket/base
|
|
|
|
#lang racket/base
|
|
|
|
(require (for-syntax racket/base syntax/parse racket/syntax syntax/datum syntax/strip-context) sugar/define)
|
|
|
|
(require (for-syntax racket/list racket/base syntax/parse racket/syntax syntax/datum syntax/strip-context) sugar/define)
|
|
|
|
(provide (all-defined-out))
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
|
|
|
|
;; everything is prefixed br: whether it needs it or not so it can be stripped by #lang br
|
|
|
|
;; everything is prefixed br: whether it needs it or not so it can be stripped by #lang br
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-for-syntax (generate-literals pats)
|
|
|
|
|
|
|
|
;; generate literals for any symbols that are not ... or _ or UPPERCASE
|
|
|
|
|
|
|
|
(for*/list ([pat-arg (in-list (flatten (map (λ(stx) (or (syntax->list stx) stx)) (syntax->list pats))))]
|
|
|
|
|
|
|
|
[pat-datum (in-value (syntax->datum pat-arg))]
|
|
|
|
|
|
|
|
#:when (and (symbol? pat-datum)
|
|
|
|
|
|
|
|
(not (eq? pat-datum '...)) (not (eq? pat-datum '_))
|
|
|
|
|
|
|
|
(not (let ([str (symbol->string pat-datum)])
|
|
|
|
|
|
|
|
(equal? (string-upcase str) str)))))
|
|
|
|
|
|
|
|
pat-arg))
|
|
|
|
|
|
|
|
|
|
|
|
;; todo: support `else` case
|
|
|
|
;; todo: support `else` case
|
|
|
|
(define-syntax (br:define-cases stx)
|
|
|
|
(define-syntax (br:define-cases stx)
|
|
|
|
(define-syntax-class syntaxed-id
|
|
|
|
(define-syntax-class syntaxed-id
|
|
|
@ -29,14 +39,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
;; syntax matcher
|
|
|
|
;; syntax matcher
|
|
|
|
[(_ top-id:syntaxed-id [(syntax pat) body ...] ...+)
|
|
|
|
[(_ top-id:syntaxed-id [(syntax pat) body ...] ...+)
|
|
|
|
|
|
|
|
(with-syntax ([(LITERAL ...) (generate-literals #'(pat ...))])
|
|
|
|
#'(define-syntax top-id.name (λ (stx)
|
|
|
|
#'(define-syntax top-id.name (λ (stx)
|
|
|
|
(define result
|
|
|
|
(define result
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx (LITERAL ...)
|
|
|
|
[pat body ...] ...
|
|
|
|
[pat body ...] ...
|
|
|
|
[else (raise-syntax-error 'define-cases (format "no matching case for syntax pattern `~a`" (syntax->datum stx)) (syntax->datum #'top-id.name))]))
|
|
|
|
[else (raise-syntax-error 'define-cases (format "no matching case for syntax pattern `~a`" (syntax->datum stx)) (syntax->datum #'top-id.name))]))
|
|
|
|
(if (not (syntax? result))
|
|
|
|
(if (not (syntax? result))
|
|
|
|
(datum->syntax stx result)
|
|
|
|
(datum->syntax stx result)
|
|
|
|
result)))]
|
|
|
|
result))))]
|
|
|
|
|
|
|
|
|
|
|
|
;; function matcher
|
|
|
|
;; function matcher
|
|
|
|
[(_ top-id:id [(_ pat-arg ... . rest-arg) body ...] ...)
|
|
|
|
[(_ top-id:id [(_ pat-arg ... . rest-arg) body ...] ...)
|
|
|
@ -51,7 +62,7 @@
|
|
|
|
(define (foo-func) 'got-foo-func)
|
|
|
|
(define (foo-func) 'got-foo-func)
|
|
|
|
(br:define-cases #'op
|
|
|
|
(br:define-cases #'op
|
|
|
|
[#'(_ "+") #''got-plus]
|
|
|
|
[#'(_ "+") #''got-plus]
|
|
|
|
[#'(_ arg) #''got-something-else]
|
|
|
|
[#'(_ ARG) #''got-something-else]
|
|
|
|
[#'(_) #'(foo-func)]
|
|
|
|
[#'(_) #'(foo-func)]
|
|
|
|
[#'_ #'foo-val])
|
|
|
|
[#'_ #'foo-val])
|
|
|
|
|
|
|
|
|
|
|
@ -118,16 +129,16 @@
|
|
|
|
(br:define #'plusser #'plus)
|
|
|
|
(br:define #'plusser #'plus)
|
|
|
|
(check-equal? (plusser 42) +)
|
|
|
|
(check-equal? (plusser 42) +)
|
|
|
|
(check-equal? plusser +)
|
|
|
|
(check-equal? plusser +)
|
|
|
|
(br:define #'(times arg) #'(* arg arg))
|
|
|
|
(br:define #'(times ARG) #'(* ARG ARG))
|
|
|
|
(check-equal? (times 10) 100)
|
|
|
|
(check-equal? (times 10) 100)
|
|
|
|
(br:define #'timeser #'times)
|
|
|
|
(br:define #'timeser #'times)
|
|
|
|
(check-equal? (timeser 12) 144)
|
|
|
|
(check-equal? (timeser 12) 144)
|
|
|
|
(br:define #'fortytwo #'42)
|
|
|
|
(br:define #'fortytwo #'42)
|
|
|
|
(check-equal? fortytwo 42)
|
|
|
|
(check-equal? fortytwo 42)
|
|
|
|
(check-equal? (let ()
|
|
|
|
(check-equal? (let ()
|
|
|
|
(br:define #'(foo x)
|
|
|
|
(br:define #'(foo X)
|
|
|
|
(with-syntax ([zam +])
|
|
|
|
(with-syntax ([zam +])
|
|
|
|
#'(zam x x))) (foo 42)) 84)
|
|
|
|
#'(zam X X))) (foo 42)) 84)
|
|
|
|
;; todo: error from define not trapped by check-exn
|
|
|
|
;; todo: error from define not trapped by check-exn
|
|
|
|
#;(check-exn exn:fail:syntax? (λ _ (br:define (#'times stx stx2) #'*)))
|
|
|
|
#;(check-exn exn:fail:syntax? (λ _ (br:define (#'times stx stx2) #'*)))
|
|
|
|
(begin
|
|
|
|
(begin
|
|
|
|