capitalize nonliterals

dev-elider-3
Matthew Butterick 9 years ago
parent 7164c4afec
commit d768d518bb

@ -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

@ -104,26 +104,26 @@
(define (basic:or . args) (cond->int (ormap true? args))) (define (basic:or . args) (cond->int (ormap true? args)))
(define-cases #'expr (define-cases #'expr
[#'(_ COMP-EXPR "AND" EXPR) #'(basic:and COMP-EXPR EXPR)] [#'(_ COMP-EXPR "AND" SUBEXPR) #'(basic:and COMP-EXPR SUBEXPR)]
[#'(_ COMP-EXPR "OR" EXPR) #'(basic:or COMP-EXPR EXPR)] [#'(_ COMP-EXPR "OR" SUBEXPR) #'(basic:or COMP-EXPR SUBEXPR)]
[#'(_ COMP-EXPR) #'COMP-EXPR]) [#'(_ COMP-EXPR) #'COMP-EXPR])
(define-cases #'comp-expr (define-cases #'comp-expr
[#'(_ LEXPR "=" REXPR) #'(comp-expr LEXPR "equal?" REXPR)] ; special case because = is overloaded [#'(_ LEXPR "=" REXPR) #'(comp-expr LEXPR "equal?" REXPR)] ; special case because = is overloaded
[#'(_ LEXPR op REXPR) (inject-syntax ([#'OP (string->symbol (syntax->datum #'op))]) [#'(_ LEXPR op REXPR) (inject-syntax ([#'OP (string->symbol (syntax->datum #'op))])
#'(cond->int (OP LEXPR REXPR)))] #'(cond->int (OP LEXPR REXPR)))]
[#'(_ expr) #'expr]) [#'(_ ARG) #'ARG])
(define <> (compose1 not equal?)) (define <> (compose1 not equal?))
(define-cases #'sum (define-cases #'sum
[#'(_ term "+" sum) #'(+ term sum)] [#'(_ TERM "+" SUM) #'(+ TERM SUM)]
[#'(_ term "-" sum) #'(- term sum)] [#'(_ TERM "-" SUM) #'(- TERM SUM)]
[#'(_ term) #'term]) [#'(_ TERM) #'TERM])
(define-cases #'product (define-cases #'product
[#'(_ factor "*" product) #'(* factor product)] [#'(_ FACTOR "*" PRODUCT) #'(* FACTOR PRODUCT)]
[#'(_ factor "/" product) #'(/ factor product)] [#'(_ FACTOR "/" PRODUCT) #'(/ FACTOR PRODUCT)]
[#'(_ factor) #'factor]) [#'(_ FACTOR) #'FACTOR])
(define print-list list) (define print-list list)
@ -136,7 +136,7 @@
[(list print-list-item ...) (for-each displayln print-list-item)])) [(list print-list-item ...) (for-each displayln print-list-item)]))
(define (TAB num) (make-string num #\space)) (define (TAB num) (make-string num #\space))
(define #'(INT EXPR ...) #'(inexact->exact (truncate (expr EXPR ...)))) (define #'(INT ARG ...) #'(inexact->exact (truncate (expr ARG ...))))
(define (SIN num) (sin num)) (define (SIN num) (sin num))
(define (ABS num) (inexact->exact (abs num))) (define (ABS num) (inexact->exact (abs num)))
(define (RND num) (* (random) num)) (define (RND num) (* (random) num))

Loading…
Cancel
Save