capitalize nonliterals

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

@ -1,9 +1,19 @@
#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))
;; 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
(define-syntax (br:define-cases stx)
(define-syntax-class syntaxed-id
@ -29,14 +39,15 @@
;; syntax matcher
[(_ top-id:syntaxed-id [(syntax pat) body ...] ...+)
(with-syntax ([(LITERAL ...) (generate-literals #'(pat ...))])
#'(define-syntax top-id.name (λ (stx)
(define result
(syntax-case stx ()
(syntax-case stx (LITERAL ...)
[pat body ...] ...
[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))
(datum->syntax stx result)
result)))]
result))))]
;; function matcher
[(_ top-id:id [(_ pat-arg ... . rest-arg) body ...] ...)
@ -51,7 +62,7 @@
(define (foo-func) 'got-foo-func)
(br:define-cases #'op
[#'(_ "+") #''got-plus]
[#'(_ arg) #''got-something-else]
[#'(_ ARG) #''got-something-else]
[#'(_) #'(foo-func)]
[#'_ #'foo-val])
@ -118,16 +129,16 @@
(br:define #'plusser #'plus)
(check-equal? (plusser 42) +)
(check-equal? plusser +)
(br:define #'(times arg) #'(* arg arg))
(br:define #'(times ARG) #'(* ARG ARG))
(check-equal? (times 10) 100)
(br:define #'timeser #'times)
(check-equal? (timeser 12) 144)
(br:define #'fortytwo #'42)
(check-equal? fortytwo 42)
(check-equal? (let ()
(br:define #'(foo x)
(br:define #'(foo X)
(with-syntax ([zam +])
#'(zam x x))) (foo 42)) 84)
#'(zam X X))) (foo 42)) 84)
;; todo: error from define not trapped by check-exn
#;(check-exn exn:fail:syntax? (λ _ (br:define (#'times stx stx2) #'*)))
(begin

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

Loading…
Cancel
Save