Skip to content

Commit 6ef6d06

Browse files
author
Roger-luo
committed
Merge branch 'master' into add-docs
2 parents 41e4a23 + fd517a2 commit 6ef6d06

File tree

13 files changed

+226
-212
lines changed

13 files changed

+226
-212
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ have a type definition:
1313

1414
```julia
1515
ex = quote
16-
type Foo
16+
struct Foo
1717
x::Int
1818
y
1919
end
@@ -23,7 +23,7 @@ end
2323
If you know what you're doing, you can pull out the name and fields via:
2424

2525
```julia
26-
julia> if isexpr(ex.args[2], :type)
26+
julia> if isexpr(ex.args[2], :struct)
2727
(ex.args[2].args[2], ex.args[2].args[3].args)
2828
end
2929
(:Foo,{:( # line 3:),:(x::Int),:( # line 4:),:y})
@@ -40,7 +40,7 @@ Enter MacroTools:
4040
```julia
4141
julia> using MacroTools
4242

43-
julia> @capture(ex, type T_ fields__ end)
43+
julia> @capture(ex, struct T_ fields__ end)
4444
true
4545

4646
julia> T, fields
@@ -91,12 +91,12 @@ Another common use case is to catch symbol literals, e.g.
9191
9292
```julia
9393
@capture(ex,
94-
type T_Symbol
94+
struct T_Symbol
9595
fields__
9696
end)
9797
```
9898
99-
which will match e.g. `type Foo ...` but not `type Foo{V} ...`
99+
which will match e.g. `struct Foo ...` but not `struct Foo{V} ...`
100100
101101
### Unions
102102

src/MacroTools.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
__precompile__(true)
21
module MacroTools
32

43
using Compat
54
using Compat.Markdown
65
export @match, @capture
76

8-
include("match.jl")
9-
include("types.jl")
10-
include("union.jl")
11-
include("macro.jl")
7+
include("match/match.jl")
8+
include("match/types.jl")
9+
include("match/union.jl")
10+
include("match/macro.jl")
11+
1212
include("utils.jl")
1313
include("structdef.jl")
1414

@@ -17,7 +17,7 @@ include("examples/threading.jl")
1717
include("examples/forward.jl")
1818

1919
const animals = Symbol[]
20-
const animals_file = joinpath(dirname(@__FILE__), "..", "animals.txt")
20+
const animals_file = joinpath(@__DIR__, "..", "animals.txt")
2121

2222
function __init__()
2323
_animals = split(read(animals_file, String))

src/examples/forward.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ macro forward(ex, fs)
1414
@capture(ex, T_.field_) || error("Syntax: @forward T.x f, g, h")
1515
T = esc(T)
1616
fs = isexpr(fs, :tuple) ? map(esc, fs.args) : [esc(fs)]
17-
:($([:($f(x::$T, args...) = (Base.@_inline_meta; $f(x.$field, args...)))
17+
:($([:($f(x::$T, args...; kwargs...) =
18+
(Base.@_inline_meta; $f(x.$field, args...; kwargs...)))
1819
for f in fs]...);
1920
nothing)
2021
end

src/examples/threading.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro >(exs...)
2222
isexpr(ex, :block) ? thread(x, rmlines(ex).args...) :
2323
Expr(:call, ex, x)
2424

25-
thread(x, exs...) = reduce(thread, x, exs)
25+
thread(x, exs...) = Compat.reduce(thread, exs, init=x)
2626

2727
esc(thread(exs...))
2828
end
@@ -42,7 +42,7 @@ macro >>(exs...)
4242
isexpr(ex, :block) ? thread(x, rmlines(ex).args...) :
4343
Expr(:call, ex, x)
4444

45-
thread(x, exs...) = reduce(thread, x, exs)
45+
thread(x, exs...) = Compat.reduce(thread, exs, init=x)
4646

4747
esc(thread(exs...))
4848
end

src/macro.jl renamed to src/match/macro.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ macro match(ex, lines)
5151
result = quote
5252
ex = $(esc(ex))
5353
end
54-
body = foldr((clause, body) -> makeclause(clause..., body),
55-
nothing, clauses(lines))
54+
55+
@static if VERSION < v"0.7.0-"
56+
body = foldr((clause, body) -> makeclause(clause..., body),
57+
nothing, clauses(lines))
58+
else
59+
body = foldr((clause, body) -> makeclause(clause..., body),
60+
clauses(lines); init=nothing)
61+
end
62+
5663
push!(result.args, body)
5764
return result
5865
end
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/utils.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,11 @@ end
368368
flatten(ex) = postwalk(flatten1, ex)
369369

370370
function makeif(clauses, els = nothing)
371-
foldr((c, ex)->:($(c[1]) ? $(c[2]) : $ex), els, clauses)
371+
@static if VERSION < v"0.7.0-"
372+
foldr((c, ex)->:($(c[1]) ? $(c[2]) : $ex), els, clauses)
373+
else
374+
foldr((c, ex)->:($(c[1]) ? $(c[2]) : $ex), clauses; init=els)
375+
end
372376
end
373377

374378
unresolve1(x) = x
@@ -383,8 +387,7 @@ function resyntax(ex)
383387
setfield!(x_, :f_, v_) => :($x.$f = $v)
384388
getindex(x_, i__) => :($x[$(i...)])
385389
tuple(xs__) => :($(xs...),)
386-
ctranspose(x_) => :($x')
387-
transpose(x_) => :($x.')
390+
adjoint(x_) => :($x')
388391
_ => x
389392
end
390393
end

test/match.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
let
2+
x = @match :(2+3) begin
3+
(a_+b_) => (a, b)
4+
(a_-b_) => (b, a)
5+
end
6+
@test x == (2, 3)
7+
end
8+
9+
let
10+
x = @match :(2-3) begin
11+
(a_+b_) => (a, b)
12+
(a_-b_) => (b, a)
13+
end
14+
@test x == (3, 2)
15+
end
16+
17+
let
18+
x = @match :(2/3) begin
19+
(a_+b_) => (a, b)
20+
(a_-b_) => (b, a)
21+
end
22+
@test x == nothing
23+
end
24+
25+
let
26+
x = @match :(2/3) begin
27+
(a_+b_) => (a, b)
28+
(a_-b_) => (b, a)
29+
_ => :default
30+
end
31+
@test x == :default
32+
end
33+
34+
let
35+
ex = :(mutable struct Foo
36+
x::Int
37+
y
38+
end)
39+
@capture(ex, mutable struct T_ fields__ end)
40+
@test T == :Foo
41+
@test fields == [:(x::Int), :y]
42+
43+
@capture(ex, mutable struct T_ fields__ end)
44+
@test T == :Foo
45+
@test fields == [:(x::Int), :y]
46+
end
47+
48+
let
49+
ex = :(f(x))
50+
@capture(ex, f_(xs__))
51+
@test f == :f
52+
@test xs == [:x]
53+
end
54+
55+
let
56+
ex = :(f(x, y, z))
57+
@capture(ex, f_(x_, xs__))
58+
@test f == :f
59+
@test x == :x
60+
@test xs == [:y, :z]
61+
end
62+
63+
let
64+
ex = quote
65+
function foo(a, b)
66+
return a+b
67+
end
68+
end
69+
@assert @capture(shortdef(ex), f_(args__) = body_)
70+
end
71+
72+
let
73+
ex = :(a = b)
74+
@capture(ex, a_ = b_)
75+
@test (a, b) == (:a, :b)
76+
end
77+
78+
let
79+
ex = :(f(a = b))
80+
@capture(ex, f(a_ = b_))
81+
@test (a, b) == (:a, :b)
82+
@capture(ex, f(x_))
83+
@test isexpr(x, :kw)
84+
end
85+
86+
let
87+
ex = :(@foo(a,b))
88+
@capture(ex, @foo(a_,b_))
89+
@test (a, b) == (:a, :b)
90+
end

0 commit comments

Comments
 (0)