Skip to content

Commit dc43c63

Browse files
authored
Merge pull request #72 from philtomson/master
Option to deterministically generate unique ids instead of randomized animal names
2 parents c1defd4 + 8429a40 commit dc43c63

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/utils.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,34 @@ end
131131
isgensym(s::Symbol) = contains(string(s), "#")
132132
isgensym(s) = false
133133

134+
function gensymname(x::Symbol)
135+
m = Base.match(r"##(.+)#\d+", String(x))
136+
m == nothing || return m.captures[1]
137+
m = Base.match(r"#\d+#(.+)", String(x))
138+
m == nothing || return m.captures[1]
139+
return "x"
140+
end
141+
142+
"""
143+
gensym_ids(expr)
144+
145+
Replaces gensyms with unique ids (deterministically)
146+
"""
147+
function gensym_ids(ex)
148+
counter = 0
149+
syms = Dict{Symbol, Symbol}()
150+
prewalk(ex) do x
151+
isgensym(x) ?
152+
Base.@get!(syms, x, Symbol(gensymname(x), "_", counter+=1)) :
153+
x
154+
end
155+
end
156+
134157
"""
135158
alias_gensyms(expr)
136159
137-
Replaces gensyms with animal names. This makes gensym'd code far easier to
138-
follow.
160+
Replaces gensyms with animal names
161+
This makes gensym'd code far easier to follow.
139162
"""
140163
function alias_gensyms(ex)
141164
left = copy(animals)

0 commit comments

Comments
 (0)