Skip to content

Commit

Permalink
support more atomic rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwamk committed Mar 17, 2018
1 parent bd9d670 commit 3913c8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 6 additions & 6 deletions redex-doc/redex/scribblings/ref/typesetting.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -1048,18 +1048,18 @@ The @racket[proc] must match the contract @racket[(-> lw? lw?)].
Its result should be the rewritten version version of the input.
}
@defform[(with-atomic-rewriter name-symbol
@defform[(with-atomic-rewriter atom
string-or-thunk-returning-pict
expression)]{
Extends the current set of atomic-rewriters with one
new one that rewrites the value of name-symbol to
new one that rewrites the value of atom to
@racket[string-or-pict-returning-thunk] (applied, in the case of a
thunk), during the evaluation of expression.
@racket[name-symbol] is expected to evaluate to a symbol. The value
of @racket[string-or-thunk-returning-pict] is used whenever the symbol
appears in a pattern.
@racket[atom] is expected to evaluate to a symbol, string, boolean,
or number. The value of @racket[string-or-thunk-returning-pict] is
used that atom appears in a pattern.
@ex[
(define-language lam-lang
Expand All @@ -1071,7 +1071,7 @@ appears in a pattern.
]
}
@defform[(with-atomic-rewriters ([name-symbol string-or-thunk-returning-pict] ...)
@defform[(with-atomic-rewriters ([atom string-or-thunk-returning-pict] ...)
expression)]{
Shorthand for nested @racket[with-atomic-rewriter] expressions.
@history[#:added "1.4"]}
Expand Down
26 changes: 22 additions & 4 deletions redex-pict-lib/redex/private/core-layout.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,37 @@
(basic-text "..." (default-style)))))
(hole "[]"))))

;; generate the assoc-table lookup entries to rewrite atoms
;; (i.e. since internally all atom literals will be a string
;; of some sort)
(define (generate-atom-entries atom transformer)
(match atom
[(or (? symbol?) (? string?))
(list (list atom transformer))]
[#t (list (list "#t" transformer)
(list "#T" transformer)
(list "#true" transformer))]
[#f (list (list "#f" transformer)
(list "#F" transformer)
(list "#false" transformer))]
[(? number?) (list (list (number->string atom) transformer))]))

(define-syntax-rule
(with-atomic-rewriter name rewriter body)
(with-atomic-rewriters ([name rewriter]) body))
(define-syntax (with-atomic-rewriters stx)
(syntax-parse stx
[(_ ([name transformer] ...) e:expr)
#:declare name
(expr/c #'symbol?
(expr/c #'(or/c symbol? string? boolean? number?)
#:name "atomic-rewriter name")
#:declare transformer
(expr/c #'(or/c (-> pict?) string?)
#:name "atomic-rewriter rewrite")
#`(parameterize ([atomic-rewrite-table
(append (list (list name.c transformer.c) ...)
(apply append
(generate-atom-entries name.c transformer.c)
...
(atomic-rewrite-table))])
e)]))

Expand Down Expand Up @@ -803,7 +820,8 @@
(string=? "#:" (substring atom 0 2))))
(list (make-string-token col span atom (paren-style)))]
[(string? atom)
(list (make-string-token col span atom (default-style)))]
(list (or (rewrite-atomic col span atom literal-style)
(make-string-token col span atom (default-style))))]
[else (error 'atom->tokens "unk ~s" atom)]))

(define (rewrite-atomic col span e get-style)
Expand All @@ -818,7 +836,7 @@
[(assoc e (atomic-rewrite-table))
=>
(λ (m)
(when (eq? (cadr m) e)
(when (equal? (cadr m) e)
(error 'apply-rewrites "rewritten version of ~s is still ~s" e e))
(let ([p (cadr m)])
(if (procedure? p)
Expand Down

0 comments on commit 3913c8f

Please sign in to comment.