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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Matching
```julia
using ExpressionPatterns.Matching

m1 = matcher(:(x+y))
m1 = @matcher(x+y)

m1(:(1+2)) == true
m1(:(1-2)) == false

m2 = matcher(:(f(*{args})))
m2 = @matcher(f(*{args}))

m2(:(g(1,2))) == true
m2(:(h())) == true
Expand Down Expand Up @@ -137,4 +137,4 @@ See [Language.md](./docs/Language.md) for information on the pattern language.

See [the examples](./examples/) or [the tests](./test/) for more uses.

Y'all should also check [MacroTools](https://github.com/MikeInnes/MacroTools.jl), which inspired me to remake and publicize this in the first place!
Y'all should also check [MacroTools](https://github.com/MikeInnes/MacroTools.jl), which inspired me to remake and publish this in the first place!
2 changes: 1 addition & 1 deletion src/Analyzer/Function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AnalysisState(state, mod::Module) = newstate(state.tree, state.literal, mo
# analyze function: transform expressions into patterns
#-----------------------------------------------------------------------------------

function analyze(ex, mod=current_module())
function analyze(ex, mod)
root = PatternRoot()
consts = Set{Symbol}()
analyze!(ex, newstate(root, false, mod, consts))
Expand Down
12 changes: 6 additions & 6 deletions src/Destructuring/Applications.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ DA = Applications
# Utilities
#----------------------------------------------------------------------------

function destructure_all(patterns, values, body)
destructure(:($(patterns...),), :(Expr(:tuple, $(values...))), body)
function destructure_all(patterns, mod, values, body)
destructure(:($(patterns...),), mod, :(Expr(:tuple, $(values...))), body)
end

#----------------------------------------------------------------------------
# Destructuring let
#----------------------------------------------------------------------------

macro letds(lets...)
esc(letds(lets[1:end-1], lets[end]))
esc(letds(__module__, lets[1:end-1], lets[end]))
end

function letds(lets, body)
function letds(mod, lets, body)
patterns, values = unzip(map(x->x.args, lets))
destructure_all(patterns, values, body)
destructure_all(patterns, mod, values, body)
end

#----------------------------------------------------------------------------
Expand Down Expand Up @@ -70,4 +70,4 @@ end

#----------------------------------------------------------------------------

end
end
6 changes: 3 additions & 3 deletions src/Destructuring/Function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import ...Helper: clean_code
import Base.Meta: quot
export destructure

function destructure(pattern, ex, body)
ptree = analyze(pattern).child
function destructure(pattern, mod, ex, body)
ptree = analyze(pattern, mod).child
@gensym vars
quote
$vars = $Variables()
Expand All @@ -30,4 +30,4 @@ declare(vars, name) =
:($name = $(vars)[$(quot(name))])


end
end
30 changes: 14 additions & 16 deletions src/Dispatch/Applications.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DI = ExpressionPatterns.Dispatch.Applications
const MACROMETHODS = TopMetaTable()

@macrods macromet(name(*{patterns})[label], body) begin
esc(macromet(name, patterns, label, body, current_module()))
esc(macromet(name, patterns, label, body, __module__))
end

function macromet(name, patterns, label, body, mod)
Expand All @@ -25,14 +25,13 @@ function macromet(name, patterns, label, body, mod)

code =
quote
$newmethod!($macrotable, Expr(:tuple, $patterns...), $(quot(body)), $mod, $(quot(label)))
$newmethod!($macrotable, $patterns, $(quot(body)), $mod, $(quot(label)))
end

if undefined
push!(code.args,
:(macro $name(args...)
parameters = Expr(:tuple, args...)
$getmethod($macrotable, parameters)(parameters)
$callmethod($macrotable, args, __source__, __module__)
end))
end
return code
Expand All @@ -43,21 +42,21 @@ end


@macromet macromethod(name(*{patterns})[label], body)[with_label] begin
esc(macromet(name, patterns, label, body, current_module()))
esc(macromet(name, patterns, label, body, __module__))
end

@macromet macromethod(name(*{patterns}), body)[no_label] begin
esc(macromet(name, patterns, unlabeled, body, current_module()))
esc(macromet(name, patterns, unlabeled, body, __module__))
end

# assignment, to be used as @macromethod name(patterns...)[label] = expr

@macromet macromethod(name(*{patterns})[label] = body)[eq_with_label] begin
esc(macromet(name, patterns, label, body, current_module()))
esc(macromet(name, patterns, label, body, __module__))
end

@macromet macromethod(name(*{patterns}) = body)[eq_no_label] begin
esc(macromet(name, patterns, unlabeled, body, current_module()))
esc(macromet(name, patterns, unlabeled, body, __module__))
end

# the [label] is optional in all definitions
Expand All @@ -71,18 +70,18 @@ const METAFUNCTIONS = TopMetaTable()
# normal

@macromethod metafunction(name(*{patterns})[label], body)[with_label] =
esc(metafunction(name, patterns, label, body, current_module()))
esc(metafunction(name, patterns, label, body, __module__))

@macromethod metafunction(name(*{patterns}), body)[no_label] =
esc(metafunction(name, patterns, unlabeled, body, current_module()))
esc(metafunction(name, patterns, unlabeled, body, __module__))

# assignment

@macromethod metafunction(name(*{patterns})[label] = body)[eq_with_label] =
esc(metafunction(name, patterns, label, body, current_module()))
esc(metafunction(name, patterns, label, body, __module__))

@macromethod metafunction(name(*{patterns}) = body)[eq_no_label] =
esc(metafunction(name, patterns, unlabeled, body, current_module()))
esc(metafunction(name, patterns, unlabeled, body, __module__))


function metafunction(name, patterns, label, body, mod)
Expand All @@ -92,14 +91,13 @@ function metafunction(name, patterns, label, body, mod)

code =
quote
$newmethod!($metatable, Expr(:tuple, $patterns...), $(quot(body)), $mod, $(quot(label)))
$newmethod!($metatable, $patterns, $(quot(body)), $mod, $(quot(label)))
end

if undefined
push!(code.args,
:(function $name(args...)
parameters = Expr(:tuple, args...)
$getmethod($metatable, parameters)(parameters)
:(function $name(args...; __module__=nothing, __source__=nothing)
$callmethod($metatable, args, __source__, __module__)
end))
end
return code
Expand Down
12 changes: 6 additions & 6 deletions src/Dispatch/MetaModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const MF = Dispatch.Applications.METAFUNCTIONS
# @metamodule
#-----------------------------------------------------------------------------------

@macromethod metamodule(:R{:import, *{path}}) esc(importcode(path))
@macromethod metamodule(:R{:import, *{path}}) esc(importcode(path, __module__))
@macromethod metamodule(:R{:importall, *{path}}) esc(importallcode(path))
@macromethod metamodule(:R{:export, *{names}}) esc(exportcode(names))
@macromethod metamodule(:R{:export, *{names}}) esc(exportcode(names, __module__))

@macromethod metamodule(:R{:toplevel}) nothing
@macromethod metamodule(:R{:toplevel,x,*{xs}}) esc(quote
Expand All @@ -26,22 +26,22 @@ end)
# Code generation.
#-----------------------------------------------------------------------------------

function importcode(path)
function importcode(path, mod)
name = last(path)
top = gettable(path[end])
quote
import $(path...)
import $(path[1:end-1]...).($(path[end-1]))
$(import_metatable!)($top, $(quot(name)), $(path[end-1]))
$(import_metatable!)($top, $(quot(name)), $(path[end-1]), $mod)
end
end

exportcode(names) = quote
exportcode(names, mod) = quote

eval(:(export $($names...)))
for name in $names
top = $gettable(name)
$(export_metatable!)(top, name)
$(export_metatable!)(top, name, $mod)
end
end

Expand Down
42 changes: 21 additions & 21 deletions src/Dispatch/Reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,77 +23,77 @@ gettable(typ, m) =
preparg(x::Symbol) = quot(x)
preparg(x::Vector) = :(Expr(:tuple, $x...))

prefercode(m, arg1, arg2, typ, M=current_module()) =
:($(prefermethod!)($(gettable(typ, m))($M), $(preparg(arg1)), $(preparg(arg2))))
prefercode(m, arg1, arg2, typ, M) =
:($(prefermethod!)($(gettable(typ, m))($M), $(preparg(arg1)), $(preparg(arg2)), $M))

whichcode(m, patterns, typ, M=current_module()) =
whichcode(m, patterns, typ, M) =
:($(whichmethod)($(gettable(typ, m))($M), Expr(:tuple, $(patterns)...)))

removecode(m, arg, typ, M=current_module()) =
:($(removemethod!)($(gettable(typ, m))($M), $(preparg(arg))))
removecode(m, arg, typ, M) =
:($(removemethod!)($(gettable(typ, m))($M), $(preparg(arg)), $M))

methodscode(m, typ, M=current_module()) =
methodscode(m, typ, M) =
:($(gettable(typ, m))($M).methods)

conflictscode(m, typ, M=current_module()) =
conflictscode(m, typ, M) =
:($(print_conflicts)(STDOUT, $(methodconflicts)($(gettable(typ, m))($M))))

#----------------------------------------------------------------------------
# which
#----------------------------------------------------------------------------

@macromethod whichmeta( m(*{patterns})) = whichcode(m, patterns, :fun)
@macromethod whichmeta( m(*{patterns})) = whichcode(m, patterns, :fun, __module__)
@macromethod whichmeta( M.m(*{patterns})) = whichcode(m, patterns, :fun, M)
@macromethod whichmeta( @m(*{patterns})) = whichcode(m, patterns, :macro)
@macromethod whichmeta( @m(*{patterns})) = whichcode(m, patterns, :macro, __module__)
@macromethod whichmeta(M.@m(*{patterns})) = whichcode(m, patterns, :macro, M)


#----------------------------------------------------------------------------
# preference
#----------------------------------------------------------------------------

@macromethod prefer( m(*{p1}), :L{over}, m(*{p2})) = esc(prefercode(m, p1, p2, :fun))
@macromethod prefer( m(*{p1}), :L{over}, m(*{p2})) = esc(prefercode(m, p1, p2, :fun, __module__))
@macromethod prefer( M.m(*{p1}), :L{over}, M.m(*{p2})) = esc(prefercode(m, p1, p2, :fun, M))
@macromethod prefer( @m(*{p1}), :L{over}, @m(*{p2})) = esc(prefercode(m, p1, p2, :macro))
@macromethod prefer( @m(*{p1}), :L{over}, @m(*{p2})) = esc(prefercode(m, p1, p2, :macro, __module__))
@macromethod prefer(M.@m(*{p1}), :L{over}, M.@m(*{p2})) = esc(prefercode(m, p1, p2, :macro, M))

@macromethod prefer(label1, :L{over}, label2 in m) = esc(prefercode(m ,label1, label2, :fun))
@macromethod prefer(label1, :L{over}, label2 in m) = esc(prefercode(m ,label1, label2, :fun, __module__))
@macromethod prefer(label1, :L{over}, label2 in M.m) = esc(prefercode(m ,label1, label2, :fun, M))
@macromethod prefer(label1, :L{over}, label2 in @m) = esc(prefercode(m ,label1, label2, :macro))
@macromethod prefer(label1, :L{over}, label2 in @m) = esc(prefercode(m ,label1, label2, :macro, __module__))
@macromethod prefer(label1, :L{over}, label2 in M.@m) = esc(prefercode(m ,label1, label2, :macro, M))

#----------------------------------------------------------------------------
# remove
#----------------------------------------------------------------------------

@macromethod remove( m(*{patterns})) = esc(removecode(m, patterns, :fun))
@macromethod remove( m(*{patterns})) = esc(removecode(m, patterns, :fun, __module__))
@macromethod remove( M.m(*{patterns})) = esc(removecode(m, patterns, :fun, M))
@macromethod remove( @m(*{patterns})) = esc(removecode(m, patterns, :macro))
@macromethod remove( @m(*{patterns})) = esc(removecode(m, patterns, :macro, __module__))
@macromethod remove(M.@m(*{patterns})) = esc(removecode(m, patterns, :macro, M))

@macromethod remove(label, :L{from}, m) = esc(removecode(m, label, :fun))
@macromethod remove(label, :L{from}, m) = esc(removecode(m, label, :fun, __module__))
@macromethod remove(label, :L{from}, M.m) = esc(removecode(m, label, :fun, M))
@macromethod remove(label, :L{from}, @m) = esc(removecode(m, label, :macro))
@macromethod remove(label, :L{from}, @m) = esc(removecode(m, label, :macro, __module__))
@macromethod remove(label, :L{from}, M.@m) = esc(removecode(m, label, :macro, M))


#----------------------------------------------------------------------------
# metamethods
#----------------------------------------------------------------------------

@macromethod metamethods( m) = esc(methodscode(m, :fun))
@macromethod metamethods( m) = esc(methodscode(m, :fun, __module__))
@macromethod metamethods( M.m) = esc(methodscode(m, :fun, M))
@macromethod metamethods( @m) = esc(methodscode(m, :macro))
@macromethod metamethods( @m) = esc(methodscode(m, :macro, __module__))
@macromethod metamethods(M.@m) = esc(methodscode(m, :macro, M))


#----------------------------------------------------------------------------
# metaconflicts
#----------------------------------------------------------------------------

@macromethod metaconflicts( m) = esc(conflictscode(m, :fun))
@macromethod metaconflicts( m) = esc(conflictscode(m, :fun, __module__))
@macromethod metaconflicts( M.m) = esc(conflictscode(m, :fun, M))
@macromethod metaconflicts( @m) = esc(conflictscode(m, :macro))
@macromethod metaconflicts( @m) = esc(conflictscode(m, :macro, __module__))
@macromethod metaconflicts(M.@m) = esc(conflictscode(m, :macro, M))


Expand Down
Loading