Skip to content

Commit

Permalink
Merge pull request #23 from SciML/format
Browse files Browse the repository at this point in the history
format SciML Style
  • Loading branch information
ChrisRackauckas authored Jun 23, 2022
2 parents 95c9265 + cf5bfce commit 4c936c1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 57 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "sciml"
42 changes: 42 additions & 0 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: format-check

on:
push:
branches:
- 'master'
- 'release-'
tags: '*'
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}

- uses: actions/checkout@v1
- name: Install JuliaFormatter and format
# This will use the latest version by default but you can set the version like so:
#
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
26 changes: 15 additions & 11 deletions src/MuladdMacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,30 @@ end
Determine whether `ex` is a call of operation `op` with at least two arguments.
"""
iscall(ex::Expr, op) =
ex.head == :call && length(ex.args) > 2 && ex.args[1] == op
iscall(ex::Expr, op) = ex.head == :call && length(ex.args) > 2 && ex.args[1] == op
iscall(ex, op) = false

"""
isdotcall(ex)
Determine whether `ex` is a dot call.
"""
isdotcall(ex::Expr) =
function isdotcall(ex::Expr)
(ex.head == :. && length(ex.args) == 2 && Meta.isexpr(ex.args[2], :tuple)) ||
(ex.head == :call && !isempty(ex.args) && startswith(string(ex.args[1]), '.'))
(ex.head == :call && !isempty(ex.args) && startswith(string(ex.args[1]), '.'))
end
isdotcall(ex) = false

"""
isdotcall(ex, 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) && length(ex.args[2].args) > 1) ||
(ex.head == :call && length(ex.args) > 2 && ex.args[1] == Symbol('.', op))
function isdotcall(ex::Expr, 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))
end
isdotcall(ex, op) = false

"""
Expand Down Expand Up @@ -176,7 +178,8 @@ function args(ex::Expr)
return ex.args[2:end]
end

if ex.head == :. && length(ex.args) == 2 && Meta.isexpr(ex.args[2], :tuple) && !isempty(ex.args[2].args)
if ex.head == :. && length(ex.args) == 2 && Meta.isexpr(ex.args[2], :tuple) &&
!isempty(ex.args[2].args)
return ex.args[2].args
end

Expand All @@ -191,10 +194,11 @@ arguments to one expression if possible.
"""
function splitargs(ex)
if ex.head == :call && length(ex.args) > 2
x = ex.args[2:end-1]
x = ex.args[2:(end - 1)]
y = ex.args[end]
elseif ex.head == :. && length(ex.args) == 2 && Meta.isexpr(ex.args[2], :tuple) && length(ex.args[2].args) > 1
x = ex.args[2].args[1:end-1]
elseif ex.head == :. && length(ex.args) == 2 && Meta.isexpr(ex.args[2], :tuple) &&
length(ex.args[2].args) > 1
x = ex.args[2].args[1:(end - 1)]
y = ex.args[2].args[end]
else
error("cannot split arguments")
Expand Down
101 changes: 55 additions & 46 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,52 @@ using MuladdMacro, Test
# Basic expressions
@testset "Basic expressions" begin
@testset "Summation" begin
@test @macroexpand(@muladd a*b+c) == :($(Base.muladd)(a, b, c))
@test @macroexpand(@muladd c+a*b) == :($(Base.muladd)(a, b, c))
@test @macroexpand(@muladd b*a+c) == :($(Base.muladd)(b, a, c))
@test @macroexpand(@muladd c+b*a) == :($(Base.muladd)(b, a, c))
@test @macroexpand(@muladd a * b + c) == :($(Base.muladd)(a, b, c))
@test @macroexpand(@muladd c + a * b) == :($(Base.muladd)(a, b, c))
@test @macroexpand(@muladd b * a + c) == :($(Base.muladd)(b, a, c))
@test @macroexpand(@muladd c + b * a) == :($(Base.muladd)(b, a, c))
end

@testset "Subtraction" begin
@test @macroexpand(@muladd a*b-c) == :($(Base.muladd)(a, b, -c))
@test @macroexpand(@muladd a-b*c) == :($(Base.muladd)(-b, c, a))
@test @macroexpand(@muladd b*a-c) == :($(Base.muladd)(b, a, -c))
@test @macroexpand(@muladd a-c*b) == :($(Base.muladd)(-c, b, a))
@test @macroexpand(@muladd a * b - c) == :($(Base.muladd)(a, b, -c))
@test @macroexpand(@muladd a - b * c) == :($(Base.muladd)(-b, c, a))
@test @macroexpand(@muladd b * a - c) == :($(Base.muladd)(b, a, -c))
@test @macroexpand(@muladd a - c * b) == :($(Base.muladd)(-c, b, a))
end
end

# Additional factors
@testset "Additional factors" begin
@testset "Summation" begin
@test @macroexpand(@muladd a*b*c+d) == :($(Base.muladd)(a*b, c, d))
@test @macroexpand(@muladd a*b*c*d+e) == :($(Base.muladd)(a*b*c, d, e))
@test @macroexpand(@muladd a * b * c + d) == :($(Base.muladd)(a * b, c, d))
@test @macroexpand(@muladd a * b * c * d + e) == :($(Base.muladd)(a * b * c, d, e))
end

@testset "Subtraction" begin
@test @macroexpand(@muladd a*b*c-d) == :($(Base.muladd)(a*b, c, -d))
@test @macroexpand(@muladd a*b*c*d-e) == :($(Base.muladd)(a*b*c, d, -e))
@test @macroexpand(@muladd a-b*c*d) == :($(Base.muladd)(-(b*c), d, a))
@test @macroexpand(@muladd a-b*c*d*e) == :($(Base.muladd)(-(b*c*d), e, a))
@test @macroexpand(@muladd a * b * c - d) == :($(Base.muladd)(a * b, c, -d))
@test @macroexpand(@muladd a * b * c * d - e) == :($(Base.muladd)(a * b * c, d, -e))
@test @macroexpand(@muladd a - b * c * d) == :($(Base.muladd)(-(b * c), d, a))
@test @macroexpand(@muladd a - b * c * d * e) ==
:($(Base.muladd)(-(b * c * d), e, a))
end
end

# Multiple multiplications
@testset "Multiple multiplications" begin
@testset "Summation" begin
@test @macroexpand(@muladd a*b+c*d) == :($(Base.muladd)(c, d, a*b))
@test @macroexpand(@muladd a*b+c*d+e*f) ==
:($(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 * b + c * d) == :($(Base.muladd)(c, d, a * b))
@test @macroexpand(@muladd a * b + c * d + e * f) ==
:($(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 * 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
Expand All @@ -56,43 +57,51 @@ end
# Dot calls
@testset "Dot calls" begin
@testset "Summation" begin
@test @macroexpand(@. @muladd a*b+c) == :($(Base.muladd).(a, b, c))
@test @macroexpand(@muladd @. a*b+c) == :($(Base.muladd).(a, b, c))
@test @macroexpand(@muladd a.*b+c) == :(a.*b+c)
@test @macroexpand(@muladd a*b.+c) == :(a*b.+c)
@test @macroexpand(@. @muladd a * b + c) == :($(Base.muladd).(a, b, c))
@test @macroexpand(@muladd @. a * b + c) == :($(Base.muladd).(a, b, c))
@test @macroexpand(@muladd a .* b + c) == :(a .* b + c)
@test @macroexpand(@muladd a * b .+ c) == :(a * b .+ c)

@test @macroexpand(@muladd .+(a.*b, c, d)) == :($(Base.muladd).(a, b, c.+d))
@test @macroexpand(@muladd @. a*b+c+d) == :($(Base.muladd).(a, b, (+).(c, d)))
@test @macroexpand(@muladd @. a*b*c+d) == :($(Base.muladd).((*).(a, b), c, d))
@test @macroexpand(@muladd .+(a .* b, c, d)) == :($(Base.muladd).(a, b, c .+ d))
@test @macroexpand(@muladd @. a * b + c + d) == :($(Base.muladd).(a, b, (+).(c, d)))
@test @macroexpand(@muladd @. a * b * c + d) == :($(Base.muladd).((*).(a, b), c, d))

@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 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
@test @macroexpand(@. @muladd a*b-c) == :($(Base.muladd).(a, b, -c))
@test @macroexpand(@muladd @. a*b-c) == :($(Base.muladd).(a, b, (-).(c)))
@test @macroexpand(@muladd a.*b-c) == :(a.*b-c)
@test @macroexpand(@muladd a*b.-c) == :(a*b.-c)
@test @macroexpand(@. @muladd a * b - c) == :($(Base.muladd).(a, b, -c))
@test @macroexpand(@muladd @. a * b - c) == :($(Base.muladd).(a, b, (-).(c)))
@test @macroexpand(@muladd a .* b - c) == :(a .* b - c)
@test @macroexpand(@muladd a * b .- c) == :(a * b .- c)

@test @macroexpand(@. @muladd a-b*c) == :($(Base.muladd).(-b, c, a))
@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 - b * c) == :($(Base.muladd).(-b, c, a))
@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

# Nested expressions
@testset "Nested expressions" begin
@test @macroexpand(@muladd f(x, y, z) = x*y+z) ==
:(f(x, y, z) = $(Base.muladd)(x, y, z))
@test @macroexpand(@muladd function f(x, y, z) x*y+z end) ==
:(function f(x, y, z) $(Base.muladd)(x, y, z) end)
@test @macroexpand(@muladd(for i in 1:n z = x*i + y end)) ==
:(for i in 1:n z = $(Base.muladd)(x, i, y) end)
@test @macroexpand(@muladd f(x, y, z) = x * y + z) ==
:(f(x, y, z) = $(Base.muladd)(x, y, z))
@test @macroexpand(@muladd function f(x, y, z)
x * y + z
end) ==
:(function f(x, y, z)
$(Base.muladd)(x, y, z)
end)
@test @macroexpand(@muladd(for i in 1:n
z = x * i + y
end)) ==
:(for i in 1:n
z = $(Base.muladd)(x, i, y)
end)
end

0 comments on commit 4c936c1

Please sign in to comment.