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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews