Skip to content

Commit bdb244c

Browse files
committed
rename symbolify --> rewrites
1 parent df0b150 commit bdb244c

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/code.jl

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import SymbolicUtils: @matchable, Sym, Term, istree, operation, arguments,
1515
##== state management ==##
1616

1717
struct NameState
18-
symbolify::Dict{Any, Any}
18+
rewrites::Dict{Any, Any}
1919
end
2020
NameState() = NameState(Dict{Any, Any}())
21-
function union_symbolify!(n, ts)
21+
function union_rewrites!(n, ts)
2222
for t in ts
2323
n[t] = Symbol(string(t))
2424
end
@@ -34,7 +34,7 @@ function Base.get(st::LazyState)
3434
s === nothing ? getfield(st, :ref)[] = NameState() : s
3535
end
3636

37-
@inline Base.getproperty(st::LazyState, f::Symbol) = getproperty(get(st), f)
37+
@inline Base.getproperty(st::LazyState, f::Symbol) = f==:symbolify ? getproperty(st, :rewrites) : getproperty(get(st), f)
3838

3939
##========================##
4040

@@ -105,7 +105,7 @@ toexpr(a::Assignment, st) = :($(toexpr(a.lhs, st)) = $(toexpr(a.rhs, st)))
105105
function_to_expr(op, args, st) = nothing
106106

107107
function function_to_expr(op::Union{typeof(*),typeof(+)}, O, st)
108-
out = get(st.symbolify, O, nothing)
108+
out = get(st.rewrites, O, nothing)
109109
out === nothing || return out
110110
args = map(Base.Fix2(toexpr, st), arguments(O))
111111
if length(args) >= 3 && symtype(O) <: Number
@@ -138,13 +138,13 @@ function function_to_expr(::typeof(SymbolicUtils.ifelse), O, st)
138138
:($(toexpr(args[1], st)) ? $(toexpr(args[2], st)) : $(toexpr(args[3], st)))
139139
end
140140

141-
function_to_expr(::Sym, O, st) = get(st.symbolify, O, nothing)
141+
function_to_expr(::Sym, O, st) = get(st.rewrites, O, nothing)
142142

143143
toexpr(O::Expr, st) = O
144144

145145
function substitute_name(O, st)
146-
if (issym(O) || istree(O)) && haskey(st.symbolify, O)
147-
st.symbolify[O]
146+
if (issym(O) || istree(O)) && haskey(st.rewrites, O)
147+
st.rewrites[O]
148148
else
149149
O
150150
end
@@ -193,13 +193,16 @@ components. See example in `Func` for more information.
193193
DestructuredArgs
194194

195195
toexpr(x::DestructuredArgs, st) = toexpr(x.name, st)
196-
get_symbolify(args::DestructuredArgs) = ()
197-
function get_symbolify(args::Union{AbstractArray, Tuple})
198-
cflatten(map(get_symbolify, args))
196+
get_rewrites(args::DestructuredArgs) = ()
197+
function get_rewrites(args::Union{AbstractArray, Tuple})
198+
cflatten(map(get_rewrites, args))
199199
end
200-
get_symbolify(x) = istree(x) ? (x,) : ()
200+
get_rewrites(x) = istree(x) ? (x,) : ()
201201
cflatten(x) = Iterators.flatten(x) |> collect
202202

203+
# Used in Symbolics
204+
Base.@deprecate_binding get_symbolify get_rewrites
205+
203206
function get_assignments(d::DestructuredArgs, st)
204207
name = toexpr(d, st)
205208
map(d.inds, d.elems) do i, a
@@ -237,7 +240,7 @@ function toexpr(l::Let, st)
237240
append!(assignments, get_assignments(x, st))
238241
else
239242
for a in get_assignments(x, st)
240-
st.symbolify[a.lhs] = a.rhs
243+
st.rewrites[a.lhs] = a.rhs
241244
end
242245
end
243246
elseif x isa Assignment && x.lhs isa DestructuredArgs
@@ -247,7 +250,7 @@ function toexpr(l::Let, st)
247250
else
248251
push!(assignments, x.lhs.name x.rhs)
249252
for a in get_assignments(x.lhs, st)
250-
st.symbolify[a.lhs] = a.rhs
253+
st.rewrites[a.lhs] = a.rhs
251254
end
252255
end
253256
else
@@ -258,8 +261,8 @@ function toexpr(l::Let, st)
258261
return toexpr(Let(assignments, l.body, l.let_block), st)
259262
end
260263

261-
funkyargs = get_symbolify(map(lhs, dargs))
262-
union_symbolify!(st.symbolify, funkyargs)
264+
funkyargs = get_rewrites(map(lhs, dargs))
265+
union_rewrites!(st.rewrites, funkyargs)
263266

264267
bindings = map(p->toexpr(p, st), dargs)
265268
l.let_block ? Expr(:let,
@@ -334,8 +337,8 @@ Func
334337
toexpr_kw(f, st) = Expr(:kw, toexpr(f, st).args...)
335338

336339
function toexpr(f::Func, st)
337-
funkyargs = get_symbolify(vcat(f.args, map(lhs, f.kwargs)))
338-
union_symbolify!(st.symbolify, funkyargs)
340+
funkyargs = get_rewrites(vcat(f.args, map(lhs, f.kwargs)))
341+
union_rewrites!(st.rewrites, funkyargs)
339342
dargs = filter(x->x isa DestructuredArgs, f.args)
340343
if !isempty(dargs)
341344
body = Let(dargs, f.body, false)

test/code.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
2222
@test toexpr(x(t)+y(t)) == :($(+)(x(t), y(t)))
2323
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)($(+)(x(t), y(t)), x($(+)(1, t))))
2424
s = LazyState()
25-
Code.union_symbolify!(s.symbolify, [x(t), y(t)])
25+
Code.union_rewrites!(s.rewrites, [x(t), y(t)])
2626
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)($(+)(var"x(t)", var"y(t)"), x($(+)(1, t))))
2727

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

0 commit comments

Comments
 (0)