Skip to content

Commit

Permalink
Only consider functions with at least two arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion committed Jul 22, 2018
1 parent 1a69913 commit 17a7b3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/MuladdMacro.jl
Expand Up @@ -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

"""
Expand All @@ -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

"""
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 17a7b3e

Please sign in to comment.