Skip to content

Commit 2b9af0c

Browse files
committed
support more atomic rewriting
1 parent bd9d670 commit 2b9af0c

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
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/pict.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
(provide with-unquote-rewriter
168168
with-compound-rewriter
169169
with-compound-rewriters
170-
with-atomic-rewriter)
170+
with-atomic-rewriter
171+
with-atomic-rewriters)
171172

172173
(provide/contract
173174
[set-arrow-pict! (-> symbol? (-> pict?) void?)]

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,39 @@
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+
[(? symbol?) (list (list atom transformer))]
97+
[(? string?) (list (list (format "“~a”" atom) transformer)
98+
(list (format "~v" atom) transformer))]
99+
[#t (list (list "#t" transformer)
100+
(list "#T" transformer)
101+
(list "#true" transformer))]
102+
[#f (list (list "#f" transformer)
103+
(list "#F" transformer)
104+
(list "#false" transformer))]
105+
[(? number?) (list (list (number->string atom) transformer))]))
106+
91107
(define-syntax-rule
92108
(with-atomic-rewriter name rewriter body)
93109
(with-atomic-rewriters ([name rewriter]) body))
94110
(define-syntax (with-atomic-rewriters stx)
95111
(syntax-parse stx
96112
[(_ ([name transformer] ...) e:expr)
97113
#:declare name
98-
(expr/c #'symbol?
114+
(expr/c #'(or/c symbol? string? boolean? number?)
99115
#:name "atomic-rewriter name")
100116
#:declare transformer
101117
(expr/c #'(or/c (-> pict?) string?)
102118
#:name "atomic-rewriter rewrite")
103119
#`(parameterize ([atomic-rewrite-table
104-
(append (list (list name.c transformer.c) ...)
105-
(atomic-rewrite-table))])
120+
(apply append
121+
(generate-atom-entries name.c transformer.c)
122+
...
123+
(list (atomic-rewrite-table)))])
106124
e)]))
107125

108126
;; compound-rewrite-table : (listof lw) -> (listof (union lw pict string))
@@ -803,7 +821,8 @@
803821
(string=? "#:" (substring atom 0 2))))
804822
(list (make-string-token col span atom (paren-style)))]
805823
[(string? atom)
806-
(list (make-string-token col span atom (default-style)))]
824+
(list (or (rewrite-atomic col span atom literal-style)
825+
(make-string-token col span atom (default-style))))]
807826
[else (error 'atom->tokens "unk ~s" atom)]))
808827

809828
(define (rewrite-atomic col span e get-style)
@@ -818,7 +837,7 @@
818837
[(assoc e (atomic-rewrite-table))
819838
=>
820839
(λ (m)
821-
(when (eq? (cadr m) e)
840+
(when (equal? (cadr m) e)
822841
(error 'apply-rewrites "rewritten version of ~s is still ~s" e e))
823842
(let ([p (cadr m)])
824843
(if (procedure? p)

0 commit comments

Comments
 (0)