Skip to content

Commit 9285d05

Browse files
committed
Add NaNMath lowering
1 parent 3dc99d4 commit 9285d05

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/code.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Code
22

3-
using StaticArrays, LabelledArrays, SparseArrays, LinearAlgebra
3+
using StaticArrays, LabelledArrays, SparseArrays, LinearAlgebra, NaNMath
44

55
export toexpr, Assignment, (), Let, Func, DestructuredArgs, LiteralExpr,
66
SetArray, MakeArray, MakeSparseArray, MakeTuple, AtIndex,
@@ -96,7 +96,29 @@ 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_to_expr(op, args, st) = nothing
99+
const NaNMathFuns = (
100+
:sin,
101+
:cos,
102+
:tan,
103+
:asin,
104+
:acos,
105+
:acosh,
106+
:atanh,
107+
:log,
108+
:log2,
109+
:log10,
110+
:lgamma,
111+
:log1p,
112+
:sqrt,
113+
)
114+
function function_to_expr(op, args, st)
115+
(op isa Function && (name = nameof(op)) in NaNMathFuns) && return nothing
116+
fun = GlobalRef(NaNMath, name)
117+
args = map(Base.Fix2(toexpr, st), arguments(O))
118+
expr = Expr(:call, fun)
119+
expr.args = args
120+
return expr
121+
end
100122

101123
function function_to_expr(op::Union{typeof(*),typeof(+)}, O, st)
102124
out = get(st.rewrites, O, nothing)

0 commit comments

Comments
 (0)