marick

marick

Where in the Elixir source does unquoting happen?

I’d like to read the code that actually implements the action of unquote. It seems like it should be somewhere in the Erlang source, in elixir_parser.yrl, elixir_compile.erl, elixir_expand.erl, or elixir_erl_pass.erl, but I can’t find it.

It probably doesn’t help that I don’t know Erlang.

Marked As Solved

dorgan

dorgan

It seems unquote is not a function itself, but rather a special call handled here:

https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_quote.erl

In essence, quote takes the ast and handles calls(as in ast call) to unquote differently depending if the :unquote option is true or false

Also Liked

marick

marick

Thanks. I now think I understand. For people interested, I offer the following. Let me know if I’ve gotten things wrong.

Given this: quote do: inspect(1 + a), compilation works as follows.

Quote processing is handled by do_quote. The syntax tree for the body of the quote looks like this:

{:inspect,
 [context: MacroExamples.ExpansionViewer, import: Kernel],
 [{:unquote, [],
  [
    {:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}]}]}

That matches a different do_quote, whose signature is: do_quote({Name, Meta, Args}. In Erlang, variables are uppercased. There are guards that check that Name is an atom and Args is a list. There’s a bit of rearrangement that ends with a call to do_quote_tuple, which looks like this:

do_quote_tuple(Left, Meta, Right, Q, E) ->
  TLeft = do_quote(Left, Q, E),
  TRight = do_quote(Right, Q, E),
  {'{}', [], [TLeft, meta(Meta, Q), TRight]}.

Left is :inspect and Right is:

[
  {:unquote, [],
   [
    {:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}]}]

There’s a recursive descent into the list, then the do_quote on line 288 matches the single element:

do_quote({unquote, _Meta, [Expr]}, #elixir_quote{unquote=true}, _) ->
  Expr;

Here unquote is the way Erlang spells atoms. In an Elixir function, the signature would be do_quote({:unquote, _meta, [expr]}

All this version of do_quote does is strip and return the single argument:

{:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}

… which may puzzle you as it did me. But let’s see how that three-tuple is used. Recall that it was calculated as part of this:

do_quote_tuple(Left, Meta, Right, Q, E) ->
  TLeft = do_quote(Left, Q, E),
  TRight = do_quote(Right, Q, E),
  {'{}', [], [TLeft, meta(Meta, Q), TRight]}.

The interesting bit that the first three arguments came from a three-tuple of the form {:inspect, meta, [{:unquote, ..., ...}]}

Because of the last line, that’s transformed into:

{:{}, [], 
 [
   :inspect, [...],
   [{:+, [...], [1, {:a, [], MacroExamples.ExpansionViewer}]}}]]}

That is:

  1. Because of the quote, the original {:inspect, ..., ...} three-tuple was not quoted verbatim, but rather converted into a different three-tuple, with the shape {:{}, ..., ...}. The compiler interprets that shape as meaning "emit code to call this particular function. (Hold that thought.)
  2. Because of the unquote, the original {:+, ..., ...} three-tuple was quoted verbatim: was left as an instruction to the compiler to create a function call.

It’s important to remember that the structure we’ve been building is destined to be compiled and then executed. The compiler is going to take that tree and turn it into a linear set of virtual machine (“BEAM”) instructions. Assuming a pretty simple virtual machine, that set of instructions will look something like this:

  1. From {:a, [], MacroExamples.ExpansionViewer}, the compiler produces instructions that say “Look up the value for a in the compilation context and push it on the stack.”
  2. The enclosing {:+, [import: Kernel], [1, {...above...}]} produces: “use the function :+ in Kernel and apply it to 1 and the top element on the stack. Put the result on top of the stack.”
  3. The enclosing {:{}, ..., [:inspect, ..metadata.., [...above...]]} says “create a three-tuple with the first element :inspect, the second ..metadata.., and the top of the stack.”

I extracted my example from this code:

defmodule MacroExamples.Inspect do
  a = 10
  IO.inspect(quote do: inspect(1 + a))

Therefore, the result of compiling and executing the quote expression is {:inspect, ..., 6}. Which, since it’s a three-tuple in “here’s a function call, dear compiler” format, could be compiled again, had it been a return value from a defmacro.

“I’ve taught you much, my little droogies.” … Maybe?

Last Post!

marick

marick

Thanks. I now think I understand. For people interested, I offer the following. Let me know if I’ve gotten things wrong.

Given this: quote do: inspect(1 + a), compilation works as follows.

Quote processing is handled by do_quote. The syntax tree for the body of the quote looks like this:

{:inspect,
 [context: MacroExamples.ExpansionViewer, import: Kernel],
 [{:unquote, [],
  [
    {:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}]}]}

That matches a different do_quote, whose signature is: do_quote({Name, Meta, Args}. In Erlang, variables are uppercased. There are guards that check that Name is an atom and Args is a list. There’s a bit of rearrangement that ends with a call to do_quote_tuple, which looks like this:

do_quote_tuple(Left, Meta, Right, Q, E) ->
  TLeft = do_quote(Left, Q, E),
  TRight = do_quote(Right, Q, E),
  {'{}', [], [TLeft, meta(Meta, Q), TRight]}.

Left is :inspect and Right is:

[
  {:unquote, [],
   [
    {:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}]}]

There’s a recursive descent into the list, then the do_quote on line 288 matches the single element:

do_quote({unquote, _Meta, [Expr]}, #elixir_quote{unquote=true}, _) ->
  Expr;

Here unquote is the way Erlang spells atoms. In an Elixir function, the signature would be do_quote({:unquote, _meta, [expr]}

All this version of do_quote does is strip and return the single argument:

{:+, [context: MacroExamples.ExpansionViewer, import: Kernel],
      [1, {:a, [], MacroExamples.ExpansionViewer}]}

… which may puzzle you as it did me. But let’s see how that three-tuple is used. Recall that it was calculated as part of this:

do_quote_tuple(Left, Meta, Right, Q, E) ->
  TLeft = do_quote(Left, Q, E),
  TRight = do_quote(Right, Q, E),
  {'{}', [], [TLeft, meta(Meta, Q), TRight]}.

The interesting bit that the first three arguments came from a three-tuple of the form {:inspect, meta, [{:unquote, ..., ...}]}

Because of the last line, that’s transformed into:

{:{}, [], 
 [
   :inspect, [...],
   [{:+, [...], [1, {:a, [], MacroExamples.ExpansionViewer}]}}]]}

That is:

  1. Because of the quote, the original {:inspect, ..., ...} three-tuple was not quoted verbatim, but rather converted into a different three-tuple, with the shape {:{}, ..., ...}. The compiler interprets that shape as meaning "emit code to call this particular function. (Hold that thought.)
  2. Because of the unquote, the original {:+, ..., ...} three-tuple was quoted verbatim: was left as an instruction to the compiler to create a function call.

It’s important to remember that the structure we’ve been building is destined to be compiled and then executed. The compiler is going to take that tree and turn it into a linear set of virtual machine (“BEAM”) instructions. Assuming a pretty simple virtual machine, that set of instructions will look something like this:

  1. From {:a, [], MacroExamples.ExpansionViewer}, the compiler produces instructions that say “Look up the value for a in the compilation context and push it on the stack.”
  2. The enclosing {:+, [import: Kernel], [1, {...above...}]} produces: “use the function :+ in Kernel and apply it to 1 and the top element on the stack. Put the result on top of the stack.”
  3. The enclosing {:{}, ..., [:inspect, ..metadata.., [...above...]]} says “create a three-tuple with the first element :inspect, the second ..metadata.., and the top of the stack.”

I extracted my example from this code:

defmodule MacroExamples.Inspect do
  a = 10
  IO.inspect(quote do: inspect(1 + a))

Therefore, the result of compiling and executing the quote expression is {:inspect, ..., 6}. Which, since it’s a three-tuple in “here’s a function call, dear compiler” format, could be compiled again, had it been a return value from a defmacro.

“I’ve taught you much, my little droogies.” … Maybe?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement