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

267 lines
9.0 KiB
Racket

9 years ago
#lang racket/base
8 years ago
(require
8 years ago
racket/function
8 years ago
(for-syntax racket/base
8 years ago
syntax/parse
8 years ago
br/private/syntax-flatten
syntax/define))
8 years ago
(provide (all-defined-out)
8 years ago
(for-syntax with-shared-id))
9 years ago
8 years ago
(module+ test
(require rackunit))
9 years ago
8 years ago
(define-syntax (define+provide stx)
(with-syntax ([(id lambda-exp)
(let-values ([(id-stx body-exp-stx)
(normalize-definition stx (datum->syntax stx 'λ) #t #t)])
(list id-stx body-exp-stx))])
#'(begin
(provide id)
(define id lambda-exp))))
(begin-for-syntax
(define (upcased-and-capitalized? sym)
(define str (symbol->string sym))
8 years ago
(and (equal? (string-upcase str) str)
(let ([first-letter (substring str 0 1)])
(or (and (string->number first-letter) #t) ; leading digit OK
(not (equal? (string-downcase first-letter) first-letter))))))
8 years ago
(define (generate-literals pats)
8 years ago
;; generate literals for any symbols that are not ... or _
8 years ago
(define pattern-arg-prefixer "_")
(for*/list ([pat-arg (in-list (syntax-flatten pats))]
[pat-datum (in-value (syntax->datum pat-arg))]
#:when (and (symbol? pat-datum)
8 years ago
(not (member pat-datum '(... _))) ; exempted from literality
(not (upcased-and-capitalized? pat-datum))))
8 years ago
pat-arg)))
(begin-for-syntax
;; expose the caller context within br:define macros with syntax parameter
(require (for-syntax racket/base) racket/stxparam)
(provide caller-stx)
(define-syntax-parameter caller-stx (λ(stx) (error 'caller-stx-not-parameterized))))
8 years ago
(define-syntax (define-cases stx)
9 years ago
(syntax-parse stx
#:literals (syntax)
8 years ago
[(_ id:id)
(raise-syntax-error 'define-cases "no cases given" (syntax->datum #'id))]
[(_ id:id [(_ . pat-args:expr) . body:expr] ...)
#'(define id
9 years ago
(case-lambda
9 years ago
[pat-args . body] ...
8 years ago
[rest-pat (apply raise-arity-error 'id (normalize-arity (map length '(pat-args ...))) rest-pat)]))]
8 years ago
[else (raise-syntax-error
'define-cases
"no matching case for calling pattern"
(syntax->datum stx))]))
9 years ago
9 years ago
(module+ test
8 years ago
(define-cases f
[(_ arg) (add1 arg)]
8 years ago
[(_ arg1 arg2) (+ arg1 arg2)]
[(_ . any) 'boing])
9 years ago
(check-equal? (f 42) 43)
8 years ago
(check-equal? (f 42 5) 47)
(check-equal? (f 42 5 'zonk) 'boing)
(define-cases f-one-arg
[(_ arg) (add1 arg)])
(check-exn exn:fail:contract:arity? (λ _ (f-one-arg 1 2 3))))
9 years ago
8 years ago
(define-syntax-rule (debug-define-macro (ID . PAT-ARGS) BODY)
(define-macro (ID . PAT-ARGS)
8 years ago
#`(begin
(for-each displayln
(list
8 years ago
(format "input pattern = #'~a" '#,'(ID . PAT-ARGS))
(format "output pattern = #'~a" (cadr '#,'BODY))
(format "invoked as = ~a" (syntax->datum #'(ID . PAT-ARGS)))
(format "expanded as = ~a" '#,(syntax->datum BODY))
(format "evaluated as = ~a" #,BODY)))
#,BODY)))
9 years ago
9 years ago
(module+ test
(require rackunit racket/port)
(parameterize ([current-output-port (open-output-nowhere)])
(check-equal? (let ()
8 years ago
(debug-define-macro (foo X Y Z)
#'(apply + (list X Y Z)))
(foo 1 2 3)) 6)
(check-equal? (let ()
8 years ago
(debug-define-macro (foo X ...) #'(apply * (list X ...)))
(foo 10 11 12)) 1320)))
9 years ago
(begin-for-syntax
(begin-for-syntax
(require (for-syntax racket/base))
8 years ago
(define-syntax-rule (make-shared-syntax-macro caller-stx)
#'(syntax-rules stx
[(_ form)
#'(datum->syntax caller-stx (if (syntax? form)
(syntax-e form)
form))]))))
8 years ago
8 years ago
(module+ test
(define-macro (dirty-maker ARG)
(with-syntax ([dirty-bar (datum->syntax caller-stx 'dirty-bar)])
#'(define dirty-bar (* ARG 2))))
(dirty-maker 42)
(check-equal? dirty-bar 84))
9 years ago
(begin-for-syntax
9 years ago
(define-syntax-rule (with-shared-id (id ...) . body)
(with-syntax ([id (datum->syntax caller-stx 'id)] ...)
8 years ago
. body)))
9 years ago
8 years ago
;; `syntax-parse` classes shared by `define-macro` and `define-macro-cases`
(begin-for-syntax
(require syntax/parse)
8 years ago
(define-syntax-class syntaxed-id
8 years ago
#:literals (syntax quasisyntax)
8 years ago
#:description "id in syntaxed form"
8 years ago
(pattern ([~or syntax quasisyntax] name:id)))
8 years ago
(define-syntax-class syntaxed-thing
8 years ago
#:literals (syntax quasisyntax)
8 years ago
#:description "some datum in syntaxed form"
8 years ago
(pattern ([~or syntax quasisyntax] thing:expr)))
(define-syntax-class else-clause
#:literals (else)
(pattern [else . body:expr]))
8 years ago
(define-syntax-class transformer-func
#:literals (lambda λ)
8 years ago
(pattern ([~or lambda λ] (arg:id) . body:expr))))
(define-syntax (define-macro stx)
8 years ago
(syntax-parse stx
8 years ago
[(_ id:id stxed-id:syntaxed-id)
#'(define-syntax id (make-rename-transformer stxed-id))]
8 years ago
[(_ id:id func:transformer-func)
#'(define-syntax id func)]
8 years ago
[(_ id:id func-id:id)
#'(define-syntax id func-id)]
8 years ago
[(_ id:id stxed-thing:syntaxed-thing)
#'(define-macro id (λ (stx) stxed-thing))]
[(_ (id:id . patargs:expr) . body:expr)
#'(define-macro-cases id [(id . patargs) (begin . body)])]
[else (raise-syntax-error
'define-macro
"no matching case for calling pattern"
(syntax->datum stx))]))
8 years ago
8 years ago
(define-syntax (define-macro-cases stx)
8 years ago
(syntax-parse stx
8 years ago
[(_ id:id)
8 years ago
(raise-syntax-error 'define-macro-cases "no cases given" (syntax->datum #'id))]
8 years ago
[(_ id:id leading-pat:expr ... else-pat:else-clause trailing-pat0:expr trailing-pat:expr ...)
(raise-syntax-error 'define-macro-cases "`else` clause must be last" (syntax->datum #'id))]
[(_ id:id (pat:expr . result-exprs:expr) ... else-clause:else-clause)
(with-syntax ([LITERALS (generate-literals #'(pat ...))])
#'(define-macro id
8 years ago
(λ (stx)
(define result
8 years ago
(syntax-parameterize ([caller-stx (make-rename-transformer #'stx)])
(syntax-case stx LITERALS
8 years ago
[pat . result-exprs] ...
else-clause)))
8 years ago
(if (syntax? result)
result
8 years ago
(datum->syntax #'id result)))))]
[(_ id:id pat-clause:expr ...) ; macro without `else` clause will reach this branch
#'(define-macro-cases id
pat-clause ...
[else (raise-syntax-error
'id
"no matching case for calling pattern"
(syntax->datum caller-stx))])]
[else (raise-syntax-error
'define-macro-cases
"no matching case for calling pattern"
(syntax->datum stx))]))
9 years ago
(module+ test
8 years ago
(define-macro plus (λ(stx) #'+))
(check-equal? (plus 42) +)
(define-macro plusser #'plus)
(check-equal? (plusser 42) +)
(check-equal? plusser +)
8 years ago
(define-macro (times [nested ARG]) #`(* ARG ARG))
8 years ago
(check-equal? (times [nested 10]) 100)
(define-macro timeser #'times)
(check-equal? (timeser [nested 12]) 144)
8 years ago
(define-macro fortytwo #`42)
8 years ago
(check-equal? fortytwo 42)
(check-equal? (let ()
(define-macro (foo X)
(with-syntax ([zam +])
#'(zam X X))) (foo 42)) 84)
(begin
(define-macro (redefine ID) #'(define ID 42))
(redefine zoombar)
(check-equal? zoombar 42))
;; use caller-stx parameter to introduce identifier unhygienically
(define-macro (zam ARG1 ARG2 ARG3)
(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
(define-macro (add X) #'(+ X X))
9 years ago
(check-equal? (add 5) 10)
(define-macro (add-b 9X) #'(+ 9X 9X))
(check-equal? (add-b 5) 10)
8 years ago
(define-macro-cases add-again [(_ X) #'(+ X X)])
9 years ago
(check-equal? (add-again 5) 10)
8 years ago
(define-macro-cases add-3rd [(_ X) #'(+ X X)])
9 years ago
(check-equal? (add-3rd 5) 10)
8 years ago
(define-macro add-4th #'add-3rd)
(check-equal? (add-4th 5) 10)
(define foo-val 'got-foo-val)
(define (foo-func) 'got-foo-func)
(define-macro-cases op
[(_ "+") #''got-plus]
8 years ago
[(_ 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)
(define-macro-cases elseop
8 years ago
[(_ ARG) #''got-arg]
8 years ago
[else #''got-else])
(check-equal? (elseop "+") 'got-arg)
(check-equal? (elseop "+" 42) 'got-else)
8 years ago
(check-exn exn:fail:syntax? (λ _ (expand-once #'(define-macro-cases no-cases))))
(check-exn exn:fail:syntax? (λ _ (expand-once #'(define-macro-cases badelseop
8 years ago
[else #''got-else]
[(_ _arg) #''got-arg]))))
(define-macro-cases no-else-macro
[(_ ARG) #''got-arg])
(check-exn exn:fail:syntax? (λ _ (expand-once #'(no-else-macro 'arg1 'arg2)))))