View Source Iteraptor.AST (iteraptor v1.14.0)

Iteraptor.AST module traverses AST, allowing map, reduce and family.

Link to this section Summary

Functions

Mapper for the AST.

Reduces the AST with an accumulator.

Link to this section Functions

Link to this function

map(input, fun, opts \\ [])

View Source
@spec map(
  binary() | {atom(), list(), any()} | list(),
  (any(), any() -> any()),
  list()
) :: any()

Mapper for the AST.

parameters

Parameters

  • input: the AST to traverse
  • fun: the function to be called on the tree element
  • opts: the options to be passed to the iteration
    • yield: :all | nil what to yield; default: nil for yielding values only.

examples

Examples

iex> bindings = [a: 1, b: 2, c: 3]
...> ":math.sin(42 * a / (3.14 * b)) > c"
...> |> Iteraptor.AST.map(fn
...>      {var, _, val} when is_atom(val) -> bindings[var]
...>      any -> any
...>    end)
{:>, [line: 1],
  [
    {{:., [line: 1], [:math, :sin]}, [line: 1],
      [
        {:/, [line: 1],
        [
          {:*, [line: 1], [42, 1]},
          {:*, [line: 1], [3.14, 2]}
        ]}
      ]},
    3
  ]}
Link to this function

reduce(input, acc, fun, opts \\ [])

View Source
@spec reduce(
  binary() | {atom(), list(), any()} | list(),
  any(),
  (any(), any() -> any()),
  list()
) :: any()

Reduces the AST with an accumulator.

parameters

Parameters

  • input: the AST to traverse
  • acc: the accumulator
  • fun: the function to be called on the tree element
  • opts: the options to be passed to the iteration
    • yield: :all | nil what to yield; default: nil for yielding values only.

examples

Examples

iex> ":math.sin(42 * a / (3.14 * b)) > c"
...> |> Iteraptor.AST.reduce([], fn
...>      {var, _, val}, acc when is_atom(val) -> [var | acc]
...>      _, acc -> acc
...>    end)
...> |> Enum.reverse()
~w|a b c|a