bartblast

bartblast

Creator of Hologram

Suppose we’ve got the following module:

defmodule TestModule do
  defmacro test_macro(param1, param2, param3) do
    quote do
      param1 + param2 + unqoute(param3)
    end
  end
end

How can I get the AST that would be generated by the given macro?

Let’s say that param1 = 1, param2 = 2, param3 = 3
Given those params I’d like to get the following AST:

{:+, [context: Elixir, import: Kernel],
 [
   {:+, [context: Elixir, import: Kernel],
    [{:param1, [], Elixir}, {:param2, [], Elixir}]},
   3
 ]}

I tried to solve this problem with Macro.expand/2 and Code.eval_quoted/3 but wasn’t able to.

Can anyone let me know how to do this or give some hints?

Showing Posts 1 to 10

kip

kip

ex_cldr Core Team

Macros are just functions that take AST as parameters and are expected to return AST. Generally AST is created using quote blocks. But you can quote any code whether in a macro of not. So you to get the AST of the code you show, it would be a matter of:

defmodule TestModule do
  defmacro test_macro(param1, param2, param3) do
    quote do
      param1 + param2 + unqoute(param3)
    end
  end
end

Note that:

  1. In this case param1 and param2 in the quote block have nothing to do with the parameters passed to the macro. They are variable references that would need to be referenceable in the calling site.
  2. unquote will dereference the parameter param3. If your intent was to get the AST for unquote(param3) then change the quote do block to be quote unquote: false do
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Do note that due to macro hygene, simply having param1 as a bound variable at the call site will actually not work without opting out of hygene with var!

@bartblast you can see the AST by putting an IO.inspect after your quote do block

defmodule TestModule do
  defmacro test_macro(param1, param2, param3) do
    param1 + param2 + unqoute(param3)
  end
  |> IO.inspect
end
bartblast

bartblast OP

Creator of Hologram

@kip, @benwilson512
Sorry, I forgot about wrapping the code inside the macro with quote, but that was my intent (I edited the module code).

I need to get the AST programmatically without touching the current code.
So, I’d like to implement the following function that can be called anywhere:

defmodule Helpers do
  def get_macro_generated_ast(module, function, args) do
    …
  end
end

And calling this function like this:
Helpers.get_macro_generated_ast(TestModule, :test_macro, [1, 2, 3])
Would return the AST which i specified in my first post.

hauleth

hauleth

What you are looking for is Macro.expand_once/2 or Macro.expand/2 if you want to expand it fully. Just beware, that it is no safer than Code.eval_quoted/3, so do not use it on untrusted input.

bartblast

bartblast OP

Creator of Hologram

@hauleth
That’s what I did initially, but there are 2 problems with this:

Before running:

Macro.expand((quote do: TestModule.test_macro(1, 2, 3)), __ENV__)

I need to explicitely require the TestModule.

the result AST contains an “unquote” expression instead of a value:

{:+, [context: TestModule, import: Kernel],
 [
   {:+, [context: TestModule, import: Kernel],
    [
      {:param1, [counter: -576460752303422365], TestModule},
      {:param2, [counter: -576460752303422365], TestModule}
    ]},
   {:unqoute, [], [{:param3, [counter: -576460752303422365], TestModule}]}
 ]}
hauleth

hauleth

Yes, because require mean “ensure that given module is compiled”. Module must be compiled to be able to use macros from it.

bartblast

bartblast OP

Creator of Hologram

Ouch… it looks like it’s not possible to do with the standard lib, then. I’ll have to develop some custom solution for this. Thank you all for help!

olafura

olafura

You can always use my library:
https://github.com/olafura/beam_to_ex
https://github.com/olafura/beam_to_ex_ast

test.ex

defmodule TestModule do
  defmacro test_macro(param1, param2, param3) do
    p1_and_p2 = param1 + param2
    quote do
      unquote(p1_and_p2) + unquote(param3)
    end
  end

  def testing do
    test_macro(1, 2, 3)
  end
end

elixirc text.ex
mix run run.exs --beam Elixir.TestModule.beam

defmodule TestModule do
  def(MACRO-test_macro(&("CALLER"), _param1, _param2, _param3)) do
    _p1_and_p2 = _param1 + _param2
    {:+, [context: TestModule, import: Kernel], [_p1_and_p2, _param3]}
  end
  def testing() do
    3 + 3
  end
end
hauleth

hauleth

What exactly you want?

bartblast

bartblast OP

Creator of Hologram

Do you mean what I need this code for?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews