Skip to content

Commit 4c4ac7e

Browse files
authored
Merge pull request #21 from esl/expr-elixir-pp
Expressions elixir pp
2 parents 455b270 + 05c81d2 commit 4c4ac7e

16 files changed

+1233
-67
lines changed

lib/gradient/ast_specifier.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ defmodule Gradient.AstSpecifier do
556556
end
557557

558558
@doc """
559-
Iterate over the list in abstract code format and runs mapper on each element
559+
Iterate over the list in abstract code and runs mapper on each element.
560560
"""
561561
@spec cons_mapper(form(), [token()], options()) :: {form(), tokens()}
562562
def cons_mapper({:cons, anno, value, tail}, tokens, opts) do

lib/gradient/debug.ex

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,37 @@ defmodule Gradient.Debug do
55

66
## TODO: specify elixir_form
77
@type elixir_form() :: any()
8-
@type erlang_form() :: Gradient.SpecifyErlAst.form()
8+
@type erlang_form() :: Gradient.Types.form()
9+
10+
@doc ~S"""
11+
Translate the Elixir code to the Erlang AST.
12+
"""
13+
defmacro elixir_to_ast(do: code) do
14+
quoted_to_ast(code)
15+
end
16+
17+
defmacro elixir_to_ast(code) do
18+
quoted_to_ast(code)
19+
end
20+
21+
@doc ~S"""
22+
Translate the Elixir AST to the Erlang AST.
23+
"""
24+
@spec quoted_to_ast(elixir_form()) :: erlang_form()
25+
def quoted_to_ast(qt) do
26+
env = :elixir_env.new()
27+
{ast, _, _} = :elixir.quoted_to_erl(qt, env)
28+
Macro.escape(ast)
29+
end
930

1031
@doc ~S"""
1132
Return the Elixir AST of an Elixir module.
1233
"""
1334
@spec elixir_ast(module()) :: {:ok, [elixir_form()]}
1435
def elixir_ast(mod) do
15-
{:ok, {_, [{:debug_info, {:debug_info_v1, :elixir_erl, abstract_code}}]}} =
36+
{:ok, {_, [{:debug_info, {:debug_info_v1, :elixir_erl, abstract_code}}]}} =
1637
:beam_lib.chunks(get_beam_path_as_charlist(mod), [:debug_info])
38+
1739
{:ok, _forms} = :elixir_erl.debug_info(:elixir_v1, :module_name, abstract_code, [])
1840
end
1941

@@ -22,7 +44,8 @@ defmodule Gradient.Debug do
2244
"""
2345
@spec erlang_ast(module()) :: {:ok, [erlang_form()]}
2446
def erlang_ast(mod) do
25-
{:ok, _forms} = get_beam_path_as_charlist(mod) |> Gradient.ElixirFileUtils.get_forms_from_beam()
47+
{:ok, _forms} =
48+
get_beam_path_as_charlist(mod) |> Gradient.ElixirFileUtils.get_forms_from_beam()
2649
end
2750

2851
@doc ~S"""

0 commit comments

Comments
 (0)