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

247 lines
8.8 KiB
Racket

8 years ago
#lang racket/base
8 years ago
(require
(for-syntax racket/list
racket/base
syntax/parse
br/syntax
racket/syntax
syntax/datum
racket/string))
(provide (all-defined-out)
(for-syntax with-shared-id with-calling-site-id))
8 years ago
8 years ago
(module+ test
(require rackunit))
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 (define-cases stx)
8 years ago
(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)
8 years ago
;; defective for function
8 years ago
[(_ top-id)
(raise-syntax-error 'define-cases "no cases given" (syntax->datum #'top-id))]
;; 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
(define-cases f
[(_ arg) (add1 arg)]
[(_ arg1 arg2) (+ arg1 arg2)])
8 years ago
(check-equal? (f 42) 43)
8 years ago
(check-equal? (f 42 5) 47))
8 years ago
8 years ago
(define-syntax-rule (debug-define-macro (id . pat-args) body-exp)
(define-macro (id . pat-args)
#`(begin
(for-each displayln
(list
(format "input pattern = #'~a" '#,'(id . pat-args))
(format "output pattern = #'~a" (cadr '#,'body-exp))
(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 ()
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)))
8 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
(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)))
8 years ago
(define-syntax (define-macro stx)
8 years ago
(define-syntax-class syntaxed-id
8 years ago
#:literals (syntax)
#:description "id in syntaxed form"
(pattern (syntax name:id)))
8 years ago
(define-syntax-class syntaxed-thing
#:literals (syntax)
#:description "some datum in syntaxed form"
(pattern (syntax thing:expr)))
8 years ago
(define-syntax-class transformer-func
#:literals (lambda λ)
(pattern ([~or lambda λ] (arg:id) . body:expr)))
8 years ago
(syntax-parse stx
#:literals (syntax)
8 years ago
[(_ id:id sid:syntaxed-id)
#'(define-syntax id (make-rename-transformer sid))]
[(_ id:id func:transformer-func)
#'(define-syntax id func)]
[(_ id:id thing:syntaxed-thing)
#'(define-syntax id (λ(stx) thing))]
[(_ (id:id . patargs) . body:expr)
#'(define-macro-cases id [(id . patargs) (begin . body)])]))
8 years ago
(define-syntax (define-macro-cases stx)
8 years ago
(define-syntax-class syntaxed-id
8 years ago
#: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)
8 years ago
[(_ id:id) ; defective for syntax
(raise-syntax-error 'define-macro-cases "no cases given" (syntax->datum #'id))]
[(_ id: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 #'id))))
(with-syntax* ([((pat . result-exprs) ... else-result-exprs)
(syntax-parse #'patexprs
#:literals (syntax else)
;; syntax notation on pattern is optional
[(((~or (syntax pat) pat) result-expr) ... (else . else-result-exprs))
#'((pat result-expr) ... else-result-exprs)]
[(((~or (syntax pat) pat) result-expr) ...)
#'((pat result-expr) ... (list (raise-syntax-error 'define-cases (format "no matching case for syntax pattern ~v" (syntax->datum stx)) (syntax->datum #'id))))])]
[LITERALS (generate-literals #'(pat ...))])
#'(define-syntax id
(λ (stx)
(define result
(syntax-case stx LITERALS
[pat (syntax-parameterize ([caller-stx (make-rename-transformer #'stx)])
(syntax-parameterize ([shared-syntax (make-shared-syntax-macro caller-stx)])
. result-exprs))] ...
[else . else-result-exprs]))
(if (syntax? result)
result
(datum->syntax #'id result)))))]))
8 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))
(check-equal? (times [nested 10]) 100)
(define-macro timeser #'times)
(check-equal? (timeser [nested 12]) 144)
(define-macro fortytwo #'42)
(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)
(define-macro (add _x) #'(+ _x _x))
8 years ago
(check-equal? (add 5) 10)
8 years ago
(define-macro-cases add-again [(_ X) #'(+ X X)])
8 years ago
(check-equal? (add-again 5) 10)
8 years ago
(define-macro-cases add-3rd [(_ X) #'(+ X X)])
8 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]
[(_ _ARG) #''got-something-else]
[(_) #'(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
[(_ _arg) #''got-arg]
[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])))))