Is there a resource that would help me understand how defmacro can be defined as a macro via defmacro?

I’m trying to better understand how compilation and the AST works. I’m starting to grasp the whole “compiling elixir is the same thing as running elixir” concept but my brain is starting to warp trying to understand how Kernel.defmacro/2 works i.e

  defmacro defmacro(call, expr \\ nil) do
    define(:defmacro, call, expr, __CALLER__)
  end

i.e defmacro defmacro

This feels like turtles all the way down. Where’s “bottom” so to speak? Is this some detail of how the language is bootstrapped?

I don’t expect someone to take the time to explain everything I might need to know to grok this, but is there an existing resource that does? Would reading Chris’ Metaprogramming in Elixir book help me grasp this?

1 Like

Indeed, its the bootstrapping…

Lets take a look at the top of the file that defines the Kernel module:

As you can see it indeed starts with importing the Kernel module, except all those macros and function it uses later on.

Then it imports the :elixir_bootstrap module, which again contains the actual defintion of, eg. defmacro in erlang.

or the define function called in the Kernel.defmacro implementation:

5 Likes

Thanks so much!