Skip to content

Commit

Permalink
save code
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtayl committed Mar 14, 2019
1 parent 0845619 commit 0efb33f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
struct CallAndCode{Call}
call::Call
code::Expr
end
(it::CallAndCode)(args...; kwargs...) = it.call(args...; kwargs...)
substitute_underscores!(dictionary, body) = body
substitute_underscores!(dictionary, body::Symbol) =
if all(isequal('_'), string(body))
Expand All @@ -23,19 +28,26 @@ function substitute_underscores!(dictionary, body::Expr)
body.args
)...)
end
anonymous(location, body) = body
function anonymous(location, body::Expr)
anonymous(location, body; inline = false) = body
function anonymous(location, body::Expr; inline = false)
dictionary = Dict{Symbol, Symbol}()
new_body = substitute_underscores!(dictionary, body)
Expr(:function,
code = Expr(:function,
Expr(:call, Symbol(string("(@_ ", body, ")")), Generator(
pair -> pair.second,
sort!(collect(dictionary), by = first)
)...),
Expr(:block, location, new_body)
)
if inline
pushfirst!(code.args[2].args, Expr(:meta, :inline))
end
Expr(:call,
CallAndCode,
code,
quot(code)
)
end

"""
macro _(body)
Expand All @@ -58,7 +70,7 @@ export @_

function chain(location, body)
if @capture body head_ |> tail_
Expr(:call, :($Base.@inline($(anonymous(location, tail)))), chain(location, head))
Expr(:call, anonymous(location, tail, inline = true), chain(location, head))
else
body
end
Expand Down

0 comments on commit 0efb33f

Please sign in to comment.