Skip to content

Commit 851aefd

Browse files
committed
Fix minor bugs and tests
1 parent dd9573e commit 851aefd

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/code.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ toexpr(a::Assignment, st) = :($(toexpr(a.lhs, st)) = $(toexpr(a.rhs, st)))
100100
function_to_expr(op, args, st) = nothing
101101

102102
function function_to_expr(op::Union{typeof(*),typeof(+)}, O, st)
103-
args = map(toexpr, arguments(O))
103+
out = get(st.symbolify, O, nothing)
104+
out === nothing || return out
105+
args = map(Base.Fix2(toexpr, st), arguments(O))
104106
if length(args) >= 3 && symtype(O) <: Number
105107
x, xs = Iterators.peel(args)
106108
foldl(xs, init=x) do a, b

test/code.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
1313
@test toexpr(Assignment(a, b)) == :(a = b)
1414
@test toexpr(a b) == :(a = b)
1515
@test toexpr(a+b) == :($(+)(a, b))
16+
@test toexpr(a*b*c*d*e) == :($(*)($(*)($(*)($(*)(a, b), c), d), e))
17+
@test toexpr(a+b+c+d+e) == :($(+)($(+)($(+)($(+)(a, b), c), d), e))
1618
@test toexpr(a+b) == :($(+)(a, b))
1719
@test toexpr(a^b) == :($(^)(a, b))
1820
@test toexpr(a^2) == :($(^)(a, 2))
1921
@test toexpr(a^-2) == :($(^)($(inv)(a), 2))
2022
@test toexpr(x(t)+y(t)) == :($(+)(x(t), y(t)))
21-
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)(x(t), y(t), x($(+)(1, t))))
23+
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)($(+)(x(t), y(t)), x($(+)(1, t))))
2224
s = LazyState()
2325
Code.union_symbolify!(s.symbolify, [x(t), y(t)])
24-
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)(var"x(t)", var"y(t)", x($(+)(1, t))))
26+
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)($(+)(var"x(t)", var"y(t)"), x($(+)(1, t))))
2527

2628
ex = :(let a = 3, b = $(+)(1,a)
2729
$(+)(a, b)
@@ -35,14 +37,14 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
3537

3638
test_repr(toexpr(Func([x(t), x],[b a+2, y(t) b], x(t)+x(t+1)+b+y(t))),
3739
:(function (var"x(t)", x; b = $(+)(2, a), var"y(t)" = b)
38-
$(+)(b, var"x(t)", var"y(t)", x($(+)(1, t)))
40+
$(+)($(+)($(+)(b, var"x(t)"), var"y(t)"), x($(+)(1, t)))
3941
end))
4042
test_repr(toexpr(Func([DestructuredArgs([x, x(t)], :state),
4143
DestructuredArgs((a, b), :params)], [],
4244
x(t+1) + x(t) + a + b)),
4345
:(function (state, params)
4446
let x = state[1], var"x(t)" = state[2], a = params[1], b = params[2]
45-
$(+)(a, b, var"x(t)", x($(+)(1, t)))
47+
$(+)($(+)($(+)(a, b), var"x(t)"), x($(+)(1, t)))
4648
end
4749
end))
4850

@@ -81,7 +83,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
8183
:(let foo = Any[3, 3, [1, 4]],
8284
var"x(t)" = foo[1], b = foo[2], c = foo[3],
8385
p = c[1], q = c[2]
84-
$(+)(a, b, c, var"x(t)")
86+
$(+)($(+)($(+)(a, b), c), var"x(t)")
8587
end))
8688

8789
test_repr(toexpr(Func([DestructuredArgs([a,b],c,inds=[:a, :b])], [],
@@ -166,4 +168,3 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
166168
@test f(2) == 2
167169
end
168170
end
169-

0 commit comments

Comments
 (0)