Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from gdziadkiewicz/Test
Browse files Browse the repository at this point in the history
Resurrection
  • Loading branch information
gdziadkiewicz committed Mar 15, 2016
2 parents 0e93c7e + 0e6b429 commit d5cd47e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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)'
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,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
```
Expand All @@ -54,7 +55,7 @@ Syntax rewriting!

```.jl
julia> ex = quote
let x=1, y=2, z=3
let x, y, z
bar
x + y
y + z
Expand All @@ -69,24 +70,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
```

Expand Down
29 changes: 29 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -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\")"
16 changes: 9 additions & 7 deletions src/ExpressionUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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_")
Expand Down Expand Up @@ -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

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

Expand Down
File renamed without changes.
34 changes: 16 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit d5cd47e

Please sign in to comment.