From 17a7b3e03ffdf460f0ce31f6a834e3c903436bf4 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Mon, 23 Jul 2018 01:23:35 +0200 Subject: [PATCH] Only consider functions with at least two arguments --- src/MuladdMacro.jl | 10 +++++----- test/runtests.jl | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/MuladdMacro.jl b/src/MuladdMacro.jl index a526dd8..908c158 100644 --- a/src/MuladdMacro.jl +++ b/src/MuladdMacro.jl @@ -85,10 +85,10 @@ end """ iscall(ex, op) -Determine whether `ex` is a call of operation `op`. +Determine whether `ex` is a call of operation `op` with at least two arguments. """ iscall(ex::Expr, op) = - ex.head == :call && !isempty(ex.args) && ex.args[1] == op + ex.head == :call && length(ex.args) > 2 && ex.args[1] == op iscall(ex, op) = false """ @@ -104,11 +104,11 @@ isdotcall(ex) = false """ isdotcall(ex, op) -Determine whether `ex` is a dot call of operation `op`. +Determine whether `ex` is a dot call of operation `op` with at least two arguments. """ isdotcall(ex::Expr, op) = - (ex.head == :. && length(ex.args) == 2 && ex.args[1] == op && Meta.isexpr(ex.args[2], :tuple)) || - (ex.head == :call && !isempty(ex.args) && ex.args[1] == Symbol('.', op)) + (ex.head == :. && length(ex.args) == 2 && ex.args[1] == op && Meta.isexpr(ex.args[2], :tuple) && length(ex.args[2].args) > 1) || + (ex.head == :call && length(ex.args) > 2 && ex.args[1] == Symbol('.', op)) isdotcall(ex, op) = false """ diff --git a/test/runtests.jl b/test/runtests.jl index d1a4544..d7551b8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -40,12 +40,16 @@ end :($(Base.muladd)(e, f, $(Base.muladd)(c, d, a*b))) @test @macroexpand(@muladd a*(b*c+d)+e) == :($(Base.muladd)(a, $(Base.muladd)(b, c, d), e)) + + @test @macroexpand(@muladd +a) == :(+a) end @testset "Subtraction" begin @test @macroexpand(@muladd a*b-c*d) == :($(Base.muladd)(-c, d, a*b)) @test @macroexpand(@muladd a*(b*c-d)-e) == :($(Base.muladd)(a, $(Base.muladd)(b, c, -d), -e)) + + @test @macroexpand(@muladd -a) == :(-a) end end @@ -64,6 +68,8 @@ end @test @macroexpand(@muladd f.(a)*b+c) == :($(Base.muladd)(f.(a), b, c)) @test @macroexpand(@muladd a*f.(b)+c) == :($(Base.muladd)(a, f.(b), c)) @test @macroexpand(@muladd a*b+f.(c)) == :($(Base.muladd)(a, b, f.(c))) + + @test @macroexpand(@muladd .+a) == :(.+a) end @testset "Subtraction" begin @@ -76,6 +82,8 @@ end @test @macroexpand(@muladd @. a-b*c) == :($(Base.muladd).((-).(b), c, a)) @test @macroexpand(@muladd a-b.*c) == :(a-b.*c) @test @macroexpand(@muladd a.-b*c) == :(a.-b*c) + + @test @macroexpand(@muladd .-a) == :(.-a) end end