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.
beautiful-racket/beautiful-racket-lib/br/define.rkt

315 lines
13 KiB
Racket

8 years ago
#lang racket/base
(require (for-syntax racket/list racket/base syntax/parse br/syntax racket/syntax syntax/datum syntax/strip-context racket/string) sugar/define)
8 years ago
(provide (all-defined-out))
8 years ago
;; everything is prefixed br: whether it needs it or not so it can be stripped by #lang br
8 years ago
(define-for-syntax (upcased? str) (equal? (string-upcase str) str))
(define-for-syntax (generate-literals pats)
;; generate literals for any symbols that are not ... or _ or _underscore-prefixed
8 years ago
(define pattern-arg-prefixer "_")
(for/list ([pat-arg (in-list (syntax-flatten pats))]
#:when (let ([pat-datum (syntax->datum pat-arg)])
(and (symbol? pat-datum)
(not (member pat-datum '(... _ else))) ; exempted from literality
(not (string-prefix? (symbol->string pat-datum) pattern-arg-prefixer))
(not (upcased? (symbol->string pat-datum))))))
8 years ago
pat-arg))
;; expose the caller context within br:define macros with syntax parameter
(begin-for-syntax
(require (for-syntax racket/base) racket/stxparam)
(provide caller-stx shared-syntax)
(define-syntax-parameter caller-stx (λ(stx) (error 'caller-stx-not-parameterized)))
(define-syntax-parameter shared-syntax (λ(stx) (error 'shared-syntax-not-parameterized))))
8 years ago
(define-syntax (br:define-cases stx)
(define-syntax-class syntaxed-id
#:literals (syntax)
#:description "id in syntaxed form"
(pattern (syntax name:id)))
(define-syntax-class syntaxed-thing
#:literals (syntax)
#:description "some datum in syntaxed form"
(pattern (syntax thing:expr)))
(syntax-parse stx
#:literals (syntax)
;; defective for syntax or function
[(_ top-id)
(raise-syntax-error 'define-cases "no cases given" (syntax->datum #'top-id))]
;; defective for syntax
8 years ago
[(_ (sid:syntaxed-id . _) . _) ; (define (#'f1 stx) expr ...)
8 years ago
(raise-syntax-error 'define-cases "definition of a syntax transformer must use lambda notation, because otherwise it's too easy to confuse the compile-time shape and the run-time shape" (syntax->datum #'sid.name))]
;; syntax matcher
[(_ top-id:syntaxed-id . patexprs)
;; todo: rephrase this check as a syntax-parse pattern above
(let ([all-but-last-pat-datums (map syntax->datum (syntax->list (syntax-case #'patexprs ()
[((pat result) ... last-one) #'(pat ...)])))])
(when (member 'else all-but-last-pat-datums)
(raise-syntax-error 'define-cases "else case must be last" (syntax->datum #'top-id.name))))
8 years ago
(with-syntax* ([((pat . result-exprs) ... else-result-exprs)
8 years ago
(syntax-parse #'patexprs
#:literals (syntax else)
;; syntax notation on pattern is optional
[(((~or (syntax pat) pat) result-expr) ... (else . else-result-exprs))
8 years ago
#'((pat result-expr) ... else-result-exprs)]
8 years ago
[(((~or (syntax pat) pat) result-expr) ...)
8 years ago
#'((pat result-expr) ... (list (raise-syntax-error 'define-cases (format "no matching case for syntax pattern ~v" (syntax->datum stx)) (syntax->datum #'top-id.name))))])]
8 years ago
[LITERALS (generate-literals #'(pat ...))])
#'(define-syntax top-id.name (λ (stx)
(define result
8 years ago
(syntax-case stx LITERALS
[pat (syntax-parameterize ([caller-stx (make-rename-transformer #'stx)])
(syntax-parameterize ([shared-syntax (make-shared-syntax-macro caller-stx)])
8 years ago
. result-exprs))] ...
[else . else-result-exprs]))
8 years ago
(if (syntax? result)
result
(datum->syntax #'top-id.name result)))))]
8 years ago
;; function matcher
8 years ago
[(_ top-id:id [(_ . pat-args) . body] ...)
8 years ago
#'(define top-id
(case-lambda
8 years ago
[pat-args . body] ...
8 years ago
[else (raise-syntax-error 'define-cases "no matching case for argument pattern" (object-name top-id))]))]))
8 years ago
8 years ago
(module+ test
8 years ago
(require rackunit)
(define foo-val 'got-foo-val)
(define (foo-func) 'got-foo-func)
(br:define-cases #'op
8 years ago
[(_ "+") #''got-plus]
[(_ _ARG) #''got-something-else]
8 years ago
[#'(_) #'(foo-func)]
[#'_ #'foo-val])
(check-equal? (op "+") 'got-plus)
(check-equal? (op 42) 'got-something-else)
(check-equal? (op) 'got-foo-func)
(check-equal? op 'got-foo-val)
(br:define-cases #'elseop
[#'(_ _arg) #''got-arg]
[else #''got-else])
(check-equal? (elseop "+") 'got-arg)
(check-equal? (elseop "+" 42) 'got-else)
(check-exn exn:fail:syntax? (λ _ (expand-once #'(br:define-cases #'badelseop
[else #''got-else]
[#'(_ _arg) #''got-arg]))))
8 years ago
(br:define-cases f
[(_ arg) (add1 arg)]
[(_ arg1 arg2) (+ arg1 arg2)])
(check-equal? (f 42) 43)
8 years ago
(check-equal? (f 42 5) 47)
8 years ago
8 years ago
(check-exn exn:fail:syntax? (λ _ (expand-once #'(br:define-cases (#'times stx stx2) #'*)))))
8 years ago
8 years ago
8 years ago
(define-syntax (br:define stx)
8 years ago
;;todo: share syntax classes
8 years ago
(define-syntax-class syntaxed-id
#:literals (syntax)
#:description "id in syntaxed form"
(pattern (syntax name:id)))
8 years ago
8 years ago
(define-syntax-class syntaxed-thing
#:literals (syntax)
#:description "some datum in syntaxed form"
(pattern (syntax thing:expr)))
(syntax-parse stx
#:literals (syntax)
8 years ago
;; syntax
8 years ago
[(_ (syntax (id . pat-args)) . body) ; (define #'(foo arg) #'(+ arg arg))
8 years ago
#'(br:define-cases (syntax id) [(syntax (_ . pat-args)) (begin . body)])]
8 years ago
[(_ sid:syntaxed-id sid2:syntaxed-id) ; (define #'f1 #'f2)
#'(define-syntax sid.name (make-rename-transformer sid2))]
8 years ago
[(_ (syntax id) (syntax thing)) ; (define #'f1 #'42)
#'(br:define-cases (syntax id) [#'_ (syntax thing)])]
8 years ago
8 years ago
[(_ (sid:syntaxed-id stx-arg ...) . exprs) ; (define (#'f1 stx) expr ...)
(raise-syntax-error 'define "definition of a syntax transformer must use lambda notation, because otherwise it's too easy to confuse the compile-time shape and the run-time shape" (syntax->datum #'sid.name))]
8 years ago
8 years ago
[(_ sid:syntaxed-id (λ (stx-arg ...) . exprs)) ; (define #'f1 (λ(stx) expr ...)
8 years ago
#:fail-when (not (= (length (syntax->datum #'(stx-arg ...))) 1))
(raise-syntax-error 'define "did not get exactly one argument for macro" (syntax->datum #'(stx-arg ...)))
8 years ago
(with-syntax ([(first-stx-arg other ...) #'(stx-arg ...)])
#'(define-syntax (sid.name first-stx-arg) . exprs))]
8 years ago
8 years ago
[(_ . args) #'(define . args)]))
8 years ago
(module+ test
(require rackunit)
(br:define #'plus (λ(stx) #'+))
(check-equal? (plus 42) +)
8 years ago
(br:define #'plusser #'plus)
8 years ago
(check-equal? (plusser 42) +)
8 years ago
(check-equal? plusser +)
8 years ago
(br:define #'(times [nested _ARG]) #'(* _ARG _ARG))
(check-equal? (times [nested 10]) 100)
8 years ago
(br:define #'timeser #'times)
8 years ago
(check-equal? (timeser [nested 12]) 144)
8 years ago
(br:define #'fortytwo #'42)
(check-equal? fortytwo 42)
8 years ago
(check-equal? (let ()
(br:define #'(foo _X)
8 years ago
(with-syntax ([zam +])
#'(zam _X _X))) (foo 42)) 84)
8 years ago
(check-exn exn:fail:syntax? (λ _ (expand-once #'(br:define (#'times stx stx2) #'*))))
8 years ago
(begin
(br:define #'(redefine _id) #'(define _id 42))
8 years ago
(redefine zoombar)
(check-equal? zoombar 42))
8 years ago
;; use caller-stx parameter to introduce identifier unhygienically
(br:define #'(zam _arg1 _arg2 _arg3)
8 years ago
(with-syntax ([dz (datum->syntax caller-stx 'dirty-zam)])
#`(define dz 'got-dirty-zam)))
(zam 'this 'that 42)
(check-equal? dirty-zam 'got-dirty-zam))
8 years ago
8 years ago
(define-syntax-rule (br:debug-define (syntax (id . pat-args)) body-exp)
(br:define #'(id . pat-args)
#`(begin
(for-each displayln
(list
8 years ago
(format "input pattern = #'~a" '#,'(id . pat-args))
(format "output pattern = #'~a" (cadr '#,'body-exp))
8 years ago
(format "invoked as = ~a" (syntax->datum #'(id . pat-args)))
(format "expanded as = ~a" '#,(syntax->datum body-exp))
(format "evaluated as = ~a" #,body-exp)))
#,body-exp)))
8 years ago
8 years ago
(module+ test
(require rackunit racket/port)
(parameterize ([current-output-port (open-output-nowhere)])
(check-equal? (let ()
(br:debug-define #'(foo _X _Y _Z)
#'(apply + (list _X _Y _Z)))
(foo 1 2 3)) 6)
(check-equal? (let ()
(br:debug-define #'(foo _X ...) #'(apply * (list _X ...)))
(foo 10 11 12)) 1320)))
8 years ago
8 years ago
(define-syntax-rule (br:define+provide . args)
(define+provide . args))
8 years ago
(define-for-syntax (expand-macro mac)
(syntax-disarm (local-expand mac 'expression #f) #f))
(define-syntax (br:define-inverting stx)
(syntax-case stx (syntax)
8 years ago
[(_ (syntax (_id . _pat-args)) . _syntaxexprs)
#'(br:define-cases-inverting (syntax _id)
8 years ago
[(syntax (_ . _pat-args)) . _syntaxexprs])]))
(begin-for-syntax
(begin-for-syntax
(require (for-syntax racket/base))
(define-syntax (make-shared-syntax-macro stx)
(syntax-case stx ()
[(_ caller-stx)
#'(λ(stx) (syntax-case stx ()
[(_ form)
#'(datum->syntax caller-stx (if (syntax? form)
(syntax-e form)
form))]))]))))
8 years ago
(provide (for-syntax with-shared-id with-calling-site-id))
8 years ago
(begin-for-syntax
8 years ago
(define-syntax-rule (with-shared-id (id ...) . body)
8 years ago
(with-syntax ([id (shared-syntax 'id)] ...)
8 years ago
. body))
(define-syntax with-calling-site-id (make-rename-transformer #'with-shared-id)))
(define-syntax (br:define-cases-inverting stx)
(syntax-case stx (syntax)
8 years ago
[(_ (syntax _id) [(syntax _patarg) . _bodyexprs] ...)
(with-syntax ([LITERALS (generate-literals #'(_patarg ...))])
#'(define-syntax (_id stx)
(syntax-case stx ()
[(_id . rest)
8 years ago
(let ([expanded-stx (with-syntax ([expanded-macros (map expand-macro (syntax->list #'rest))])
#'(_id . expanded-macros))])
(define result
8 years ago
(syntax-case expanded-stx LITERALS
[_patarg (syntax-parameterize ([caller-stx (make-rename-transformer #'stx)])
(syntax-parameterize ([shared-syntax (make-shared-syntax-macro caller-stx)])
. _bodyexprs))] ...
[else (raise-syntax-error 'define-cases-inverting (format "no matching case for syntax pattern ~v" (syntax->datum stx)) (syntax->datum #'_id))]))
8 years ago
(if (syntax? result)
result
(datum->syntax #'_id result)))])))]))
8 years ago
(module+ test
;; an inverting macro expands its arguments.
;; so `foo` does not get `(falsy a) (falsy b) (falsy c)` as arguments,
;; but rather the result of their expansion, namely `((#f a) (#f b) (#f c))`
;; and `tree` does not get `(foo (#f a) (#f b) (#f c))` as its first argument,
;; but rather the result of its expansion, namely (a b c).
(br:define-inverting #'(tree (_id ...) _vals)
#'(let ()
(define-values (_id ...) _vals)
(list _id ...)))
(br:define-cases-inverting #'foo
[#'(_ (#f _id) ...) #'(_id ...)])
(define-syntax-rule (falsy id) (#f id))
(check-equal? (tree (foo (falsy a) (falsy b) (falsy c)) (values 1 2 3)) '(1 2 3)))
(define-syntax (br:define-macro stx)
(syntax-case stx (syntax)
8 years ago
[(_ id #'other-id) ; (define-macro id #'other-id)
#'(br:define #'id #'other-id)]
8 years ago
[(_ (id . patargs) . body)
8 years ago
#'(br:define #'(id . patargs) . body)]
8 years ago
[(_ id [pat . patbody] ...)
8 years ago
#'(br:define-cases #'id [pat . patbody] ...)]))
8 years ago
(define-syntax (br:define-macro-cases stx)
(syntax-case stx (syntax)
8 years ago
[(_ id . body)
#'(br:define-cases (syntax id) . body)]))
8 years ago
(module+ test
(br:define-macro (add _x) #'(+ _x _x))
8 years ago
(check-equal? (add 5) 10)
8 years ago
(br:define-macro-cases add-again [(_ X) #'(+ X X)])
(check-equal? (add-again 5) 10)
(br:define-macro add-3rd [(_ X) #'(+ X X)])
8 years ago
(check-equal? (add-3rd 5) 10)
(br:define-macro add-4th #'add-3rd)
(check-equal? (add-4th 5) 10))