Packages

C compiler built with Elixir. This library will only allow you to compile basic C programs.

Current section

Files

Jump to
exc lib helpers ast_traveler.ex
Raw

lib/helpers/ast_traveler.ex

defmodule Helpers.ASTTraveler do
@moduledoc """
Travels through an Abstract Syntax Tree.
"""
@doc """
Returns a string that results from the traveling of the Abstract Syntax Tree.
## Specs
```root``` : abstract syntax tree. An example of an AST is the output from `Parser.parse/2`.
```num``` : number of iteration; when calling this function, its value should always be 0.
"""
def travel(root, num) do
if num == 0, do: "{#{root.tag} => {#{decide(root)}}}"
Enum.map(root.children, fn x -> if x.children == [], do: "\n" <> String.duplicate("\t", num + 1) <> "{#{x.tag} => #{x.token.expression}}", else: "\n" <> String.duplicate("\t", num + 1) <> "{#{x.tag} => #{decide(x)}}" <> "#{travel(x, num + 1)}" end)
end
defp decide(node) do
if node.token == nil, do: "", else: node.token.expression
end
end