# `Unity.Interpreter`
[🔗](https://github.com/elixir-localize/unity/blob/v0.6.0/lib/unity/interpreter.ex#L1)

Evaluates ASTs produced by `Unity.Parser` by building `Localize.Unit`
structs and applying operations via `Localize.Unit.Math`.

The interpreter maintains an environment map for variable bindings
(via `let`) and a special `_` binding for the previous result.

# `env`

```elixir
@type env() :: %{required(String.t()) =&gt; Localize.Unit.t() | number()}
```

# `result`

```elixir
@type result() :: Localize.Unit.t() | number() | {:decomposed, [Localize.Unit.t()]}
```

# `eval`

```elixir
@spec eval(term(), env()) :: {:ok, result(), env()} | {:error, String.t()}
```

Evaluates a parsed AST in the given environment.

### Arguments

* `ast` - the AST node from `Unity.Parser`.

* `environment` - a map of variable bindings. Defaults to `%{}`.

### Returns

* `{:ok, result, environment}` on success, where `result` is a
  `Localize.Unit.t()` or a number, and `environment` is the updated
  variable bindings.

* `{:error, message}` on failure.

### Examples

    iex> {:ok, ast} = Unity.Parser.parse("3 meters to feet")
    iex> {:ok, result, _env} = Unity.Interpreter.eval(ast)
    iex> result.name
    "foot"

---

*Consult [api-reference.md](api-reference.md) for complete listing*
