Skip to content

Commit dd9573e

Browse files
committed
Unflatten * and + in toexpr
1 parent 48cc5ff commit dd9573e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/code.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export toexpr, Assignment, (←), Let, Func, DestructuredArgs, LiteralExpr,
77
SpawnFetch, Multithreaded
88

99
import ..SymbolicUtils
10-
import SymbolicUtils: @matchable, Sym, Term, istree, operation, arguments
10+
import SymbolicUtils: @matchable, Sym, Term, istree, operation, arguments,
11+
symtype
1112

1213
##== state management ==##
1314

@@ -98,6 +99,20 @@ toexpr(a::Assignment, st) = :($(toexpr(a.lhs, st)) = $(toexpr(a.rhs, st)))
9899

99100
function_to_expr(op, args, st) = nothing
100101

102+
function function_to_expr(op::Union{typeof(*),typeof(+)}, O, st)
103+
args = map(toexpr, arguments(O))
104+
if length(args) >= 3 && symtype(O) <: Number
105+
x, xs = Iterators.peel(args)
106+
foldl(xs, init=x) do a, b
107+
Expr(:call, op, a, b)
108+
end
109+
else
110+
expr = Expr(:call, op)
111+
append!(expr.args, args)
112+
expr
113+
end
114+
end
115+
101116
function function_to_expr(::typeof(^), O, st)
102117
args = arguments(O)
103118
if length(args) == 2 && args[2] isa Real && args[2] < 0

test/code.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
119119

120120
@test eval(toexpr(Let([a 1, b 2, arr @SLVector((:a, :b))(@SVector[1,2])],
121121
MakeArray([a+b,a/b], arr)))) === @SLVector((:a, :b))(@SVector [3, 1/2])
122-
122+
123123
R1 = eval(toexpr(Let([a 1, b 2, arr @MVector([1,2])],MakeArray([a,b,a+b,a/b], arr))))
124124
@test R1 == (@MVector [1, 2, 3, 1/2]) && R1 isa MVector
125125

0 commit comments

Comments
 (0)