View Source ExPression (ex_pression v0.2.0)

Define and evaluate expressions in runtime in your Elixir project.

Features

  • JSON support - expressions support all JSON types with it's stanrad syntax
  • Python-like operators and standard functions
  • Extend expressions by providing Elixir module with functions that you want to use.
  • Safe evaluation without acces to other Elixir modules.

Summary

Functions

Evalate expression in string or 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()}

Evalate expression in string or AST format.

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()}
@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.