Skip to content

Commit 4f1540b

Browse files
committed
Add nanmath as an option
1 parent 1189564 commit 4f1540b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/code.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const NaNMathFuns = (
112112
sqrt,
113113
)
114114
function function_to_expr(op, O, st)
115-
op in NaNMathFuns || return nothing
115+
(get(st.rewrites, :nanmath, false) && op in NaNMathFuns) || return nothing
116116
name = nameof(op)
117117
fun = GlobalRef(NaNMath, name)
118118
args = map(Base.Fix2(toexpr, st), arguments(O))

test/code.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using SparseArrays
88
using LinearAlgebra
99

1010
test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linenums!(b))
11+
nanmath_st = Code.NameState()
12+
nanmath_st.rewrites[:nanmath] = true
1113

1214
@testset "Code" begin
1315
@syms a b c d e p q t x(t) y(t) z(t)
@@ -84,11 +86,18 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
8486
end)
8587
@test toexpr(SetArray(true, a, [x(t), AtIndex(9, b), c])).head == :macrocall
8688

89+
f = GlobalRef(NaNMath, :sin)
8790
test_repr(toexpr(LiteralExpr(:(let x=1, y=2
88-
$(NaNMath.sin(a+b))
91+
$(sin(a+b))
92+
end)), nanmath_st),
93+
:(let x = 1, y = 2
94+
$(f)($(+)(a, b))
95+
end))
96+
test_repr(toexpr(LiteralExpr(:(let x=1, y=2
97+
$(sin(a+b))
8998
end))),
9099
:(let x = 1, y = 2
91-
$(NaNMath.sin)($(+)(a, b))
100+
$(sin)($(+)(a, b))
92101
end))
93102

94103
test_repr(toexpr(MakeArray([a,b,a+b], :arr)),
@@ -191,7 +200,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
191200
@test f(1) == 1
192201
@test f(2) == 2
193202

194-
f = eval(toexpr(Func([a, b], [], sqrt(a - b))))
203+
f = eval(toexpr(Func([a, b], [], sqrt(a - b)), nanmath_st))
195204
@test isnan(f(0, 10))
196205
@test f(10, 2) sqrt(8)
197206
end

0 commit comments

Comments
 (0)