Skip to content

Commit aa41078

Browse files
authored
Merge pull request #203 from JuliaSymbolics/s/symbolify-dict
symbolify with dict
2 parents ab204a4 + 9608788 commit aa41078

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/code.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ import SymbolicUtils: @matchable, Sym, Term, istree, operation, arguments
1111
##== state management ==##
1212

1313
struct NameState
14-
symbolify::Set
14+
symbolify::Dict{Any, Symbol}
15+
end
16+
NameState() = NameState(Dict{Any, Symbol}())
17+
function union_symbolify!(n, ts)
18+
for t in ts
19+
n[t] = Symbol(string(t))
20+
end
1521
end
16-
NameState() = NameState(Set{Any}())
1722

1823
struct LazyState
1924
ref::Ref{Any}
@@ -100,7 +105,12 @@ function toexpr(O, st)
100105
return toexpr(Term{Any}(inv, [ex]), st)
101106
else
102107
return toexpr(Term{Any}(^, [Term{Any}(inv, [ex]), -args[2]]), st)
103-
end elseif op === (SymbolicUtils.ifelse) return :($(toexpr(args[1], st)) ? $(toexpr(args[2], st)) : $(toexpr(args[3], st))) elseif op isa Sym && O in st.symbolify return Symbol(string(O)) end
108+
end
109+
elseif op === (SymbolicUtils.ifelse)
110+
return :($(toexpr(args[1], st)) ? $(toexpr(args[2], st)) : $(toexpr(args[3], st)))
111+
elseif op isa Sym && haskey(st.symbolify, O)
112+
return st.symbolify[O]
113+
end
104114
return Expr(:call, toexpr(op, st), map(x->toexpr(x, st), args)...)
105115
end
106116

@@ -174,7 +184,7 @@ function toexpr(l::Let, st)
174184
end
175185

176186
funkyargs = get_symbolify(map(lhs, dargs))
177-
union!(st.symbolify, funkyargs)
187+
union_symbolify!(st.symbolify, funkyargs)
178188

179189
Expr(:let,
180190
Expr(:block, map(p->toexpr(p, st), dargs)...),
@@ -241,7 +251,7 @@ toexpr_kw(f, st) = Expr(:kw, toexpr(f, st).args...)
241251

242252
function toexpr(f::Func, st)
243253
funkyargs = get_symbolify(vcat(f.args, map(lhs, f.kwargs)))
244-
union!(st.symbolify, funkyargs)
254+
union_symbolify!(st.symbolify, funkyargs)
245255
dargs = filter(x->x isa DestructuredArgs, f.args)
246256
if !isempty(dargs)
247257
body = Let(dargs, f.body)

test/code.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
1919
@test toexpr(x(t)+y(t)) == :($(+)(x(t), y(t)))
2020
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)(x(t), y(t), x($(+)(1, t))))
2121
s = LazyState()
22-
push!(s.symbolify, x(t))
23-
push!(s.symbolify, y(t))
22+
Code.union_symbolify!(s.symbolify, [x(t), y(t)])
2423
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)(var"x(t)", var"y(t)", x($(+)(1, t))))
2524

2625
ex = :(let a = 3, b = $(+)(1,a)

0 commit comments

Comments
 (0)