From cf5bfce2035483e210b9b4f5b0d1c1ec0aa2b32d Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Thu, 23 Jun 2022 15:31:23 -0400 Subject: [PATCH] format SciML Style --- .JuliaFormatter.toml | 1 + .github/workflows/FormatCheck.yml | 42 +++++++++++++ src/MuladdMacro.jl | 26 ++++---- test/runtests.jl | 101 ++++++++++++++++-------------- 4 files changed, 113 insertions(+), 57 deletions(-) create mode 100644 .JuliaFormatter.toml create mode 100644 .github/workflows/FormatCheck.yml diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..453925c --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1 @@ +style = "sciml" \ No newline at end of file diff --git a/.github/workflows/FormatCheck.yml b/.github/workflows/FormatCheck.yml new file mode 100644 index 0000000..2a3517a --- /dev/null +++ b/.github/workflows/FormatCheck.yml @@ -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' diff --git a/src/MuladdMacro.jl b/src/MuladdMacro.jl index 292b971..e3ca3e4 100644 --- a/src/MuladdMacro.jl +++ b/src/MuladdMacro.jl @@ -105,8 +105,7 @@ 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 """ @@ -114,9 +113,10 @@ iscall(ex, op) = false 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 """ @@ -124,9 +124,11 @@ isdotcall(ex) = false 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 """ @@ -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 @@ -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") diff --git a/test/runtests.jl b/test/runtests.jl index d7551b8..7961ca4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -56,32 +57,32 @@ 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 @@ -89,10 +90,18 @@ 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