Use prewalk to inject value into AST
This commit is contained in:
@@ -1,24 +1,8 @@
|
|||||||
defmodule ASTMangler do
|
defmodule ASTMangler do
|
||||||
def expand([], _), do: []
|
def expand(ast, replacement) do
|
||||||
def expand({fun, context, args}, replacement) do
|
Macro.prewalk(ast, fn(node) -> update(node, replacement) end)
|
||||||
new_args = replace(args, replacement)
|
|
||||||
{fun, context, new_args}
|
|
||||||
end
|
end
|
||||||
def expand([do: thing], replacement) do
|
|
||||||
[do: expand(thing, replacement)]
|
|
||||||
end
|
|
||||||
def expand(n, _), do: n
|
|
||||||
|
|
||||||
def replace(nil, _), do: nil
|
def update(:__, replacement), do: replacement
|
||||||
def replace(args, b) do
|
def update(node, _), do: node
|
||||||
args
|
|
||||||
|> Enum.find_index(fn(x) -> x == :__ end)
|
|
||||||
|> replace(args, b)
|
|
||||||
end
|
|
||||||
def replace(nil, ast, b) when is_list(ast) do
|
|
||||||
Enum.map(ast, fn(element) -> expand(element, b) end)
|
|
||||||
end
|
|
||||||
def replace(index, list, b) when is_integer(index) do
|
|
||||||
List.update_at(list, index, fn(_)-> b end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user