Skip to content

Commit

Permalink
macroexpand
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtayl committed Nov 30, 2018
1 parent c91a4f7 commit 46fd93b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Nameless.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ substitute_underscores!(dictionary, body::Symbol) =
substitute_underscores!(dictionary, body::Expr) =
if body.head == :quote
body
elseif @capture body @_ args__
body
else
Expr(body.head,
map(body -> substitute_underscores!(dictionary, body), body.args)
Expand All @@ -35,7 +33,7 @@ substitute_underscores!(dictionary, body::Expr) =

string_length(something) = something |> String |> length

function unname_simple(body, line, file)
function unname_simple(body::Expr, line, file)
dictionary = Dict{Symbol, Symbol}()
new_body = substitute_underscores!(dictionary, body)
sorted_dictionary = sort(
Expand All @@ -49,10 +47,15 @@ function unname_simple(body, line, file)
)
end

function unname(body, line, file)

function unname(body::Expr, line, file)
Expr(:call, Nameless, unname_simple(body, line, file), quot(body))
end

function unname(body, line, file)
unname(Expr(:block, LineNumberNode(line, file), body), line, file)
end

export @_
"""
macro _(body::Expr)
Expand All @@ -74,8 +77,8 @@ julia> @_(_ + 1).expression
:(_ + 1)
```
"""
macro _(body::Expr)
unname(body, @__LINE__, @__FILE__) |> esc
macro _(body)
unname(macroexpand(@__MODULE__, body), @__LINE__, @__FILE__) |> esc
end

chain(body, line, file) =
Expand All @@ -99,5 +102,5 @@ julia> @> 0 |> _ + 1 |> _ - 1
```
"""
macro >(body)
chain(body, @__LINE__, @__FILE__) |> esc
chain(macroexpand(@__MODULE__, body), @__LINE__, @__FILE__) |> esc
end

0 comments on commit 46fd93b

Please sign in to comment.