Skip to content

Commit 8429a40

Browse files
committed
preserve gensym names
1 parent acf5fde commit 8429a40

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/utils.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,26 @@ 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+
134142
"""
135-
gensyms_to_ids(expr)
143+
gensym_ids(expr)
136144
137145
Replaces gensyms with unique ids (deterministically)
138146
"""
139-
function gensyms_to_ids(ex)
147+
function gensym_ids(ex)
140148
counter = 0
141-
base_id = "sym_id_"
142149
syms = Dict{Symbol, Symbol}()
143150
prewalk(ex) do x
144-
if isgensym(x)
145-
repl_sym = Symbol("$base_id$counter")
146-
#tried the following, but it did not work:
147-
#repl_sym = Symbol(replace(string(x), "#", ""))
148-
counter+=1
149-
Base.@get!(syms, x, repl_sym)
150-
else
151+
isgensym(x) ?
152+
Base.@get!(syms, x, Symbol(gensymname(x), "_", counter+=1)) :
151153
x
152-
end
153-
154154
end
155155
end
156156

0 commit comments

Comments
 (0)