MLStyle.jl
MLStyle.jl copied to clipboard
GlobalRef pattern is not defined for Expr match
e.g
@match stmt begin
Expr(:call, GlobalRef(Base, :getfield), SlotNumber(2), QuoteNode(:variables)) => break
Expr(:call, getfield, SlotNumber(2), QuoteNode(:variables)) => break
_ => nothing
end
it would also be nice to have the following things supported for Expr match, e.g Core.GotoIfNot Core.GotoNode, Core.SSAValue, Core.SlotNumber since they are valid items in lowered AST.
This can be achieved by defining active patterns:
julia> @active GlobalRef(x) begin
(x.mod, x.name)
end
julia> @match GlobalRef(Base, :getfield) begin
GlobalRef(a, b) => (a, b)
end
(Base, :getfield)
julia> SlotNumber = Core.SlotNumber
julia> @active SlotNumber(x) begin
Some(x.id) # must match
end
julia> @match Core.SlotNumber(1) begin
Core.SlotNumber(x) => x
end
1
I end up implementing the following
# MLStyle patches
@active GlobalRef(x) begin
if x isa GlobalRef
(x.mod, x.name)
else
nothing
end
end
@active Argument(x) begin
if x isa Argument
Some(x.n)
else
nothing
end
end
@active SSAValue(x) begin
if x isa SSAValue
Some(x.id)
else
nothing
end
end
@active SlotNumber(x) begin
if x isa SlotNumber
Some(x.id)
else
nothing
end
end
but I'm wondering if we can define these in MLStyle? If I define these in my package, I will end up pirating MLStyle and Julia Core. I'm not sure where to put them in MLStyle.
Sorry for missing this, I'd like to add them into https://github.com/thautwarm/MLStyle.jl/blob/master/src/Pervasives.jl
It seems I can't use @active in that file, I don't know how to implement them without @active, perhaps I'll just define them in Expronicon.jl instead.
After considering it again I guess these patterns would be better to get separated from MLStyle base. MLStyle is to provide infrastructure, but implementing patterns for stdlib types might be a big task comparing to the pattern compilation itself.