tim2CF

tim2CF

How to expand AST fully?

Hey!
I’m playing around with macro and I’m thinking - is it possible to completely expand quoted expression (including all private and public macro)?
Simplest example which I actually can’t fully expand:

  defmodule Hello do
    defmacrop foo, do: {:y, [], nil}
    def bar(x, foo()), do: x + y
  end

for example let’s try to use Macro.expand/2

quote do
  defmodule Hello do
    defmacrop foo, do: {:y, [], nil}
    def bar(x, foo()), do: x + y
  end
end
|> Macro.expand(__ENV__)
|> Macro.to_string 
|> Code.format_string! 
|> IO.puts

result is

:elixir_module.compile(
  Hello,
  {:__block__, [],
   [
     {:=, [],
      [
        {:result, [], Kernel},
        {:__block__, [],
         [
           {:defmacrop, [context: Elixir, import: Kernel],
            [{:foo, [context: Elixir], Elixir}, [do: {:{}, [], [:y, [], nil]}]]},
           {:def, [context: Elixir, import: Kernel],
            [
              {:bar, [context: Elixir], [{:x, [], Elixir}, {:foo, [], []}]},
              [do: {:+, [context: Elixir, import: Kernel], [{:x, [], Elixir}, {:y, [], Elixir}]}]
            ]}
         ]}
      ]},
     {{:., [], [:elixir_utils, :noop]}, [], []},
     {:result, [], Kernel}
   ]},
  [{:q, nil, :_@0, q}],
  __ENV__
)

interesting line is

{:bar, [context: Elixir], [{:x, [], Elixir}, {:foo, [], []}]},

where foo is AST of macro call, but not result of this call (should be {:y, [], Elixir})

Is there any way to expand it and get resulting quoted expression somehow?

Most Liked

OvermindDL1

OvermindDL1

That’s because it is not a macro, :foo at that point is a function invocation of something called foo that is not in scope at that point. A macro cannot be expanded unless it is in scope. :slight_smile:

Nah, expanding is fine, you just have to make sure it is all in scope first, and def blah... inside the ast itself does not bring anything into scope at that time as it needs to be in scope ‘prior’ to the expand call. :slight_smile:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement