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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (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?