From 0e6b429aa4be6ab5eced49db6f721891460e99b5 Mon Sep 17 00:00:00 2001 From: Grzegorz Dziadkiewicz Date: Tue, 15 Mar 2016 15:08:56 +0100 Subject: [PATCH] * Added TravisCI and AppVeyor configs * Fixed LineNumberNodes removal * Updated REQUIRE * Updated tests to new FactCheck syntax * Updated README.md --- .travis.yml | 15 +++++++++++++++ README.md | 30 ++++++++++++++---------------- appveyor.yml | 29 +++++++++++++++++++++++++++++ src/ExpressionUtils.jl | 16 +++++++++------- REQUIRE => test/REQUIRE | 0 test/runtests.jl | 34 ++++++++++++++++------------------ 6 files changed, 83 insertions(+), 41 deletions(-) create mode 100644 .travis.yml create mode 100644 appveyor.yml rename REQUIRE => test/REQUIRE (100%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..40aa044 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +# Documentation: http://docs.travis-ci.com/user/languages/julia/ +language: julia +os: + - linux + - osx +julia: + - release + - nightly + +notifications: + email: true +# uncomment the following lines to override the default test script +#script: +# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi +# - julia -e 'Pkg.clone(pwd()); Pkg.build("TestRunner"); Pkg.test("TestRunner"; coverage=true)' diff --git a/README.md b/README.md index 0479009..549727d 100644 --- a/README.md +++ b/README.md @@ -28,18 +28,19 @@ julia> b = quote end # => quote # none, line 2: # let x = 1, y = 2, z = 3 # line 3: -# +(x,y,z) +# x + y + z # end # end -julia> isline(ex) = isa(ex, Expr) && ex.head == :line -# methods for generic function isline -isline(ex) at none:1 +julia> remove_line_nodes(node::LineNumberNode) = ExpressionUtils.Remove +# remove_line_nodes (generic function with 1 method) +julia> remove_line_nodes(ex) = ex +# remove_line_nodes (generic function with 2 methods) -julia> walk(ex -> isline(ex) ? ExpressionUtils.Remove : ex, b) +julia> walk(remove_line_nodes, b) # => quote # let x = 1, y = 2, z = 3 -# +(x,y,z) +# x + y + z # end # end ``` @@ -50,7 +51,7 @@ Syntax rewriting! ```.jl julia> ex = quote - let x=1, y=2, z=3 + let x, y, z bar x + y y + z @@ -65,24 +66,21 @@ julia> template = quote end julia> out = quote - function _funname_(; _UNSPLAT_bindings_) + function _funname_( _UNSPLAT_bindings_) _UNSPLAT_body_ end end julia> fnexpr = expr_replace(ex, template, out) -# => :(function bar($(Expr(:parameters, :(x = 1), :(y = 2), :(z = 3)))) -# +(x,y) -# +(y,z) +# => :(function bar(x, y, z) +# x + y +# y + z # end) julia> eval(fnexpr) +# bar (generic function with 1 method) -julia> bar() -# methods for generic function bar -bar() - -julia> bar() +julia> bar(1, 2, 3) # => 5 ``` diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..4cd9e57 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,29 @@ +environment: + matrix: + - JULIAVERSION: "julialang/bin/winnt/x86/0.4/julia-0.4-latest-win32.exe" + - JULIAVERSION: "julialang/bin/winnt/x64/0.4/julia-0.4-latest-win64.exe" + - JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe" + - JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe" + +notifications: + - provider: Email + on_build_success: false + on_build_failure: true + on_build_status_changed: true + +install: +# Download most recent Julia Windows binary + - ps: (new-object net.webclient).DownloadFile( + $("http://s3.amazonaws.com/"+$env:JULIAVERSION), + "C:\projects\julia-binary.exe") +# Run installer silently, output to C:\projects\julia + - C:\projects\julia-binary.exe /S /D=C:\projects\julia + +build_script: +# Need to convert from shallow to complete for Pkg.clone to work + - IF EXIST .git\shallow (git fetch --unshallow) + - C:\projects\julia\bin\julia -e "versioninfo(); + Pkg.clone(pwd(), \"ExpressionUtils\"); Pkg.build(\"ExpressionUtils\")" + +test_script: + - C:\projects\julia\bin\julia --check-bounds=yes -e "Pkg.test(\"ExpressionUtils\")" diff --git a/src/ExpressionUtils.jl b/src/ExpressionUtils.jl index 82511ce..f5111ce 100644 --- a/src/ExpressionUtils.jl +++ b/src/ExpressionUtils.jl @@ -33,10 +33,13 @@ remove_quote_block(val) = val remove_quote_block(ex::Expr) = ex.head == :block && length(ex.args) == 2 ? ex.args[2] : ex -remove_line_ann(ex::Expr) = - walk((e) -> isa(e, Expr) && e.head == :line ? Remove : e, ex) +function remove_line_ann(ex::Expr) + remove_line_nodes(node::LineNumberNode) = Remove + remove_line_nodes(e) = e + walk(remove_line_nodes, ex) +end -symbeginswith(sym, s) = beginswith(string(sym), s) +symbeginswith(sym, s) = startswith(string(sym), s) issplat(sym) = isa(sym, Symbol) && symbeginswith(sym, "_SPLAT_") isunsplat(sym) = isa(sym, Symbol) && symbeginswith(sym, "_UNSPLAT_") isesc(sym) = isa(sym, Symbol) && symbeginswith(sym, "_ESC_") @@ -81,11 +84,11 @@ expr_bindings(ex, template) = function unsplat(ex::Expr, bindings::Dict) r = (args, e) -> isunsplat(e) ? - vcat(args, get(bindings, removeprefix(e), {e})) : + vcat(args, get(bindings, removeprefix(e), [e])) : push!(args, get(bindings, removeprefix(e), e)) ex = copy(ex) - ex.args = reduce(r, {}, ex.args) + ex.args = reduce(r, [], ex.args) ex end @@ -101,10 +104,9 @@ end function expr_replace(ex, template, out) ex, template, out = map((e) -> remove_line_ann(remove_quote_block(e)), - {ex, template, out}) + [ex, template, out]) bindings = expr_bindings(ex, template) - expr_bind(remove_quote_block(out), bindings) end diff --git a/REQUIRE b/test/REQUIRE similarity index 100% rename from REQUIRE rename to test/REQUIRE diff --git a/test/runtests.jl b/test/runtests.jl index dbb7465..e90820d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,13 +1,13 @@ using FactCheck using ExpressionUtils -@facts "Expression destructuring" begin +facts("Expression destructuring") do - @fact "expr_bindings" begin + context("expr_bindings") do - expr_bindings(:(x = 5), :(x = _val_)) => {:_val_ => 5} - expr_bindings(:(x = 5), :(_sym_ = 5)) => {:_sym_ => :x} - expr_bindings(:(x = 5), :_ex_) => {:_ex_ => :(x = 5)} + @fact expr_bindings(:(x = 5), :(x = _val_)) --> Dict(:_val_ => 5) + @fact expr_bindings(:(x = 5), :(_sym_ = 5)) --> Dict(:_sym_ => :x) + @fact expr_bindings(:(x = 5), :_ex_) --> Dict(:_ex_ => :(x = 5)) splatex = Expr(:let, :_body_, :_SPLAT_bindings_) ex = :(let x=1, y=2, z=3 @@ -17,17 +17,16 @@ using ExpressionUtils bindings = expr_bindings(ex, splatex) - haskey(bindings, :_body_) => true - haskey(bindings, :_bindings_) => true - bindings[:_body_] => body -> isa(body, Expr) && body.head == :block - bindings[:_bindings_] => b -> isa(b, Array) && length(b) == 3 - + @fact haskey(bindings, :_body_) --> true + @fact haskey(bindings, :_bindings_) --> true + @fact bindings[:_body_] --> body -> isa(body, Expr) && body.head == :block + @fact bindings[:_bindings_] --> b -> isa(b, Array) && length(b) == 3 end - @fact "expr_replace" begin + context("expr_replace") do ex = quote - let x=1, y=2, z=3 + let x, y, z bar x + y y + z @@ -42,17 +41,16 @@ using ExpressionUtils end out = quote - function _funname_(; _UNSPLAT_bindings_) + function _funname_( _UNSPLAT_bindings_) _UNSPLAT_body_ end end foofun = expr_replace(ex, template, out) - - foofun.head => :function - eval(foofun)() => 5 - bar() => 5 - + eval(foofun) + @fact foofun.head --> :function + @fact eval(foofun)(1,2,3) --> 5 + @fact bar(1,2,3) --> 5 end end