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?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
kip
Macros are just functions that take AST as parameters and are expected to return AST. Generally AST is created using
quoteblocks. But you canquoteany code whether in a macro of not. So you to get the AST of the code you show, it would be a matter of:Note that:
param1andparam2in thequoteblock 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.unquotewill dereference the parameterparam3. If your intent was to get the AST forunquote(param3)then change thequote doblock to bequote unquote: false dobenwilson512
Do note that due to macro hygene, simply having
param1as a bound variable at the call site will actually not work without opting out of hygene withvar!@bartblast you can see the AST by putting an IO.inspect after your
quote doblockbartblast
@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:
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
What you are looking for is
Macro.expand_once/2orMacro.expand/2if you want to expand it fully. Just beware, that it is no safer thanCode.eval_quoted/3, so do not use it on untrusted input.bartblast
@hauleth
That’s what I did initially, but there are 2 problems with this:
Before running:
I need to explicitely require the TestModule.
the result AST contains an “unquote” expression instead of a value:
hauleth
Yes, because
requiremean “ensure that given module is compiled”. Module must be compiled to be able to use macros from it.bartblast
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
You can always use my library:
https://github.com/olafura/beam_to_ex
https://github.com/olafura/beam_to_ex_ast
test.ex
elixirc text.exmix run run.exs --beam Elixir.TestModule.beamhauleth
What exactly you want?
bartblast
Do you mean what I need this code for?