Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ function bindinglet(bs, body)
return ex
end

ensurequoted(x) = x
ensurequoted(x::Union{Expr, Symbol}) = Expr(:quote, x)

function makeclause(pat, yes, els = nothing)
bs = allbindings(pat)
pat = subtb(subor(pat))
pat = ensurequoted(subtb(subor(pat)))
quote
env = trymatch($(Expr(:quote, pat)), ex)
env = trymatch($(pat), ex)
if env != nothing
$(bindinglet(bs, esc(yes)))
else
Expand Down
4 changes: 2 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ istb(s::Symbol) = !(endswith(string(s), "_") || endswith(string(s), "_str")) &&
tbname(s::Symbol) = Symbol(split(string(s), "_")[1])
tbname(s::TypeBind) = s.name

totype(s::Symbol) = string(s)[1] in 'A':'Z' ? s : Expr(:quote, s)
totype(s::Symbol) = string(s)[1] in 'A':'Z' ? eval(s) : s

function tbnew(s::Symbol)
istb(s) || return s
ts = map(Symbol, split(string(s), "_"))
name = shift!(ts)
ts = map(totype, ts)
Expr(:$, :(MacroTools.TypeBind($(Expr(:quote, name)), Set{Any}([$(ts...)]))))
MacroTools.TypeBind(name, Set{Any}([ts...]))
end

match_inner(b::TypeBind, ex, env) =
Expand Down
1 change: 1 addition & 0 deletions src/union.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ subor(s) = s
subor(s::Symbol) = s
subor(s::Expr) = isor(s) ? subor(ornew(s)) : Expr(s.head, map(subor, s.args)...)
subor(s::OrBind) = OrBind(subor(s.pat1), subor(s.pat2))
subtb(s::OrBind) = OrBind(subtb(s.pat1), subtb(s.pat2))
44 changes: 44 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,48 @@ let
@test isexpr(x, :kw)
end

let
ex = :(x[1])
@test @match(ex, begin
v_ref => v
end) == ex

@test @match(ex, begin
v_call => v
end) === nothing

@test @match(:(x(1)), begin
v_call => v
end) == :(x(1))

@test @match(:(x(1)), begin
v_ref => v
end) === nothing

@test @match(ex, begin
(v_ref |
(v_ref <= ub_)) => (v, ub)
end) == (ex, nothing)

@test @match(:(x <= 2), begin
(v_ref |
(v_ref <= ub_)) => (v, ub)
end) == nothing

@test @match(:(x(3) <= 2), begin
(v_call |
(v_ref <= ub_)) => (v, ub)
end) == (:(x(3) <= 2), nothing) # only the first pattern matches

@test @match(:(x(3) <= 2), begin
((v_ref <= ub_) |
v_call ) => (v, ub)
end) == (:(x(3) <= 2), nothing)

@test @match(:(x[1] <= 2), begin
(v_ref |
(v_ref <= ub_)) => (v, ub)
end) == (:(x[1]), :(2))
end

include("destruct.jl")