Is there a tool that can illustrate of how expressions are evaluated?

I got some help solving this question on stackoverflow, not that I intend to use complicated pipelines in every day programming.

What I realized that I’ve some learning to do in reading Elixir expressions and understanding how they are evaluated.

Is there some tool that can take an Elixir expression and print out some kind of graphic tree of how it is evaluated? If not then something in text that is easier to understand.

Does iex have a tool that can output something like that? I know Elixir syntax is considered easy to understand, but being more familiar with procedural languages where code is more explicit I need all the help in the beginning.

Is there some tool that can take an Elixir expression and print out some kind of graphic tree of how it is evaluated? If not then something in text that is easier to understand.

Can you provide an example of what you’re looking for in some
other language?

If you Google “display code parse tree” and click on Images you will see a number of pictures demonstrating the idea. It doesn’t have to be actually graphical, but should display how the expressions are broken down for evaluation.

It should break down each function into a tree showing the function at the root and the parameters (which may be other function calls) at the leaves,

You can use quote to get the AST. It’s not graphically pretty, but it is technically a tree.

iex(7)> quote do: a + b
{:+, [context: Elixir, import: Kernel], [{:a, [], Elixir}, {:b, [], Elixir}]}

It’s a 3-tuple. First element is the root function. Second element is metadata, Third element is a list of parameters (which themselves can be function calls).

2 Likes

If you Google “display code parse tree” and click on Images…

Sorry, my question was more “do you have an example program
that does this for some other language”?

I actually haven’t, but I am positive such programs exist.

What does the context mean, and what does the Elixir and the empty lists in the tuples with :a and :b mean?

In any case I got some help with this in Dogbert’s comment on https://stackoverflow.com/questions/43569863

How does the expression read?

What you see is a nested list of 3-tuples. The first elem is an identifier, the second elem is metadata, and the third elem contains the tuples from deeper in the tree.

“the context and import” items in the metadata are hints to the compiler about where to look up the symbols found in the tree. In this case we’re in the context of the Elixir programming language. From most of what I know… the metadata is not documented anywhere but by-in-large you can ignore it.

I this case you end up with a simple parse tree …

   +
 /   \
a     b

You can assume that items lower in the tree will be evaluated before those higher up. So the first evaluation is to look up the value of a, then look up the value of b, then call the function Kernel.+ with those two arguments.

1 Like

I like to read the final generated end-point code of erlang via Core. The EVM/BEAM uses a base-most syntax called Core, kind of a mini-erlang language, very easy to use and parse and all. I’m unsure how to output core from Elixir code without calling in to the low level compiling primitives and passing that output to the erlang core compiler pass (which I’ve done), but it would be nice if someone PR’s a --core option to elixirc to do that more easily. :slight_smile:

1 Like