View Source ExPression (ex_pression v0.3.2)

Eval user input expressions in your Elixir project.

Summary

Functions

Evaluate expression.

Evaluate expression given in AST format.

Parse expression in string format into AST format.

Types

Functions

Link to this function

eval(expression_str, opts \\ [])

View Source
@spec eval(binary(), Keyword.t()) :: {:ok, any()} | {:error, ExPression.Error.t()}

Evaluate expression.

Options

  • :bindings - map variable names and values.
  • :functions_module - module with functions that will be accessible from expressions.

Examples

iex> eval("1 + 0.5")
{:ok, 1.5}
iex> eval("div(x, y)", bindings: %{"x" => 5, "y" => 2}, functions_module: Kernel)
{:ok, 2}
iex> eval(~s/{"1": "en", "2": "fr"}[str(int_code)]/, bindings: %{"int_code" => 1})
{:ok, "en"}
iex> eval("not true or false or 1 == 1")
{:ok, true}
iex> eval("exit(self())")
{:error, %ExPression.Error{name: "UndefinedFunctionError", message: "Function 'self/0' was referenced, but was not defined", data: %{function: :self}}}
Link to this function

eval_ast(ast, opts \\ [])

View Source
@spec eval_ast(ast(), Keyword.t()) :: {:ok, any()} | {:error, ExPression.Error.t()}

Evaluate expression given in AST format.

@spec parse(binary()) :: {:ok, ast()} | {:error, ExPression.Error.t()}

Parse expression in string format into AST format.

This can be used for optimizations: to parse expression once and evaluate AST many times.