Skip to content

Commit 3913c8f

Browse files
committed
support more atomic rewriting
1 parent bd9d670 commit 3913c8f

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

redex-doc/redex/scribblings/ref/typesetting.scrbl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,18 +1048,18 @@ The @racket[proc] must match the contract @racket[(-> lw? lw?)].
10481048
Its result should be the rewritten version version of the input.
10491049
}
10501050
1051-
@defform[(with-atomic-rewriter name-symbol
1051+
@defform[(with-atomic-rewriter atom
10521052
string-or-thunk-returning-pict
10531053
expression)]{
10541054
10551055
Extends the current set of atomic-rewriters with one
1056-
new one that rewrites the value of name-symbol to
1056+
new one that rewrites the value of atom to
10571057
@racket[string-or-pict-returning-thunk] (applied, in the case of a
10581058
thunk), during the evaluation of expression.
10591059
1060-
@racket[name-symbol] is expected to evaluate to a symbol. The value
1061-
of @racket[string-or-thunk-returning-pict] is used whenever the symbol
1062-
appears in a pattern.
1060+
@racket[atom] is expected to evaluate to a symbol, string, boolean,
1061+
or number. The value of @racket[string-or-thunk-returning-pict] is
1062+
used that atom appears in a pattern.
10631063
10641064
@ex[
10651065
(define-language lam-lang
@@ -1071,7 +1071,7 @@ appears in a pattern.
10711071
]
10721072
}
10731073
1074-
@defform[(with-atomic-rewriters ([name-symbol string-or-thunk-returning-pict] ...)
1074+
@defform[(with-atomic-rewriters ([atom string-or-thunk-returning-pict] ...)
10751075
expression)]{
10761076
Shorthand for nested @racket[with-atomic-rewriter] expressions.
10771077
@history[#:added "1.4"]}

redex-pict-lib/redex/private/core-layout.rkt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,37 @@
8888
(basic-text "..." (default-style)))))
8989
(hole "[]"))))
9090

91+
;; generate the assoc-table lookup entries to rewrite atoms
92+
;; (i.e. since internally all atom literals will be a string
93+
;; of some sort)
94+
(define (generate-atom-entries atom transformer)
95+
(match atom
96+
[(or (? symbol?) (? string?))
97+
(list (list atom transformer))]
98+
[#t (list (list "#t" transformer)
99+
(list "#T" transformer)
100+
(list "#true" transformer))]
101+
[#f (list (list "#f" transformer)
102+
(list "#F" transformer)
103+
(list "#false" transformer))]
104+
[(? number?) (list (list (number->string atom) transformer))]))
105+
91106
(define-syntax-rule
92107
(with-atomic-rewriter name rewriter body)
93108
(with-atomic-rewriters ([name rewriter]) body))
94109
(define-syntax (with-atomic-rewriters stx)
95110
(syntax-parse stx
96111
[(_ ([name transformer] ...) e:expr)
97112
#:declare name
98-
(expr/c #'symbol?
113+
(expr/c #'(or/c symbol? string? boolean? number?)
99114
#:name "atomic-rewriter name")
100115
#:declare transformer
101116
(expr/c #'(or/c (-> pict?) string?)
102117
#:name "atomic-rewriter rewrite")
103118
#`(parameterize ([atomic-rewrite-table
104-
(append (list (list name.c transformer.c) ...)
119+
(apply append
120+
(generate-atom-entries name.c transformer.c)
121+
...
105122
(atomic-rewrite-table))])
106123
e)]))
107124

@@ -803,7 +820,8 @@
803820
(string=? "#:" (substring atom 0 2))))
804821
(list (make-string-token col span atom (paren-style)))]
805822
[(string? atom)
806-
(list (make-string-token col span atom (default-style)))]
823+
(list (or (rewrite-atomic col span atom literal-style)
824+
(make-string-token col span atom (default-style))))]
807825
[else (error 'atom->tokens "unk ~s" atom)]))
808826

809827
(define (rewrite-atomic col span e get-style)
@@ -818,7 +836,7 @@
818836
[(assoc e (atomic-rewrite-table))
819837
=>
820838
(λ (m)
821-
(when (eq? (cadr m) e)
839+
(when (equal? (cadr m) e)
822840
(error 'apply-rewrites "rewritten version of ~s is still ~s" e e))
823841
(let ([p (cadr m)])
824842
(if (procedure? p)

0 commit comments

Comments
 (0)