Skip to content

Commit 394c083

Browse files
authored
Merge pull request #238 from JuliaSymbolics/myb/extend
Add `function_to_expr` to make `toexpr` of calls extendible
2 parents 4efc408 + ce2f2f1 commit 394c083

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.9.1"
4+
version = "0.9.2"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"

src/code.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,38 @@ Base.convert(::Type{Assignment}, p::Pair) = Assignment(pair[1], pair[2])
9696

9797
toexpr(a::Assignment, st) = :($(toexpr(a.lhs, st)) = $(toexpr(a.rhs, st)))
9898

99-
function toexpr(O, st)
100-
!istree(O) && return O
101-
op = operation(O)
99+
function_to_expr(op, args, st) = nothing
100+
101+
function function_to_expr(::typeof(^), O, st)
102102
args = arguments(O)
103-
if op === (^) && length(args) == 2 && args[2] isa Number && args[2] < 0
103+
if length(args) == 2 && args[2] isa Number && args[2] < 0
104104
ex = args[1]
105105
if args[2] == -1
106106
return toexpr(Term{Any}(inv, [ex]), st)
107107
else
108108
return toexpr(Term{Any}(^, [Term{Any}(inv, [ex]), -args[2]]), st)
109109
end
110-
elseif op === (SymbolicUtils.ifelse)
111-
return :($(toexpr(args[1], st)) ? $(toexpr(args[2], st)) : $(toexpr(args[3], st)))
112-
elseif op isa Sym && haskey(st.symbolify, O)
113-
return st.symbolify[O]
114110
end
115-
return Expr(:call, toexpr(op, st), map(x->toexpr(x, st), args)...)
111+
return nothing
112+
end
113+
114+
function function_to_expr(::typeof(SymbolicUtils.ifelse), O, st)
115+
args = arguments(O)
116+
:($(toexpr(args[1], st)) ? $(toexpr(args[2], st)) : $(toexpr(args[3], st)))
117+
end
118+
119+
function_to_expr(::Sym, O, st) = get(st.symbolify, O, nothing)
120+
121+
function toexpr(O, st)
122+
!istree(O) && return O
123+
op = operation(O)
124+
expr′ = function_to_expr(op, O, st)
125+
if expr′ !== nothing
126+
return expr′
127+
else
128+
args = arguments(O)
129+
return Expr(:call, toexpr(op, st), map(x->toexpr(x, st), args)...)
130+
end
116131
end
117132

118133
# Call elements of vector arguments by their name.

0 commit comments

Comments
 (0)