I did this in iex
iex(1) x = quote do
...(1)> defmodule One do
...(1)> def area({:square,x}) do
...(1)> x*x
...(1)> end
...(1)>
...(1)> def area({:rectangle,x,y}) do
...(1)> x*y
...(1)> end
...(1)> end
...(1)> end
{:defmodule, [context: Elixir, import: Kernel],
[{:__aliases__, [alias: false], [:One]},
[do: {:__block__, [],
[{:def, [context: Elixir, import: Kernel],
[{:area, [context: Elixir], [square: {:x, [], Elixir}]},
[do: {:*, [context: Elixir, import: Kernel],
[{:x, [], Elixir}, {:x, [], Elixir}]}]]},
{:def, [context: Elixir, import: Kernel],
[{:area, [context: Elixir],
[{:{}, [], [:rectangle, {:x, [], Elixir}, {:y, [], Elixir}]}]},
[do: {:*, [context: Elixir, import: Kernel],
[{:x, [], Elixir}, {:y, [], Elixir}]}]]}]}]]}
But when I print it I see this
iex(2)> x |> Macro.to_string |> IO.puts
defmodule(One) do
def(area({:square, x})) do
x * x
end
def(area({:rectangle, x, y})) do
x * y
end
end
:ok
I’ve lost the white space between the definitions
and extra parentheses have been added around the def
so it prints def(area({:square,x}) do which is rather ugly
compared to def area({:square,x}) do
Is there any way of retaining the white space in the AST?
And is it easy to change the pretty printer to remove the additional
parentheses??
Cheers
/Joe