ExSel v0.0.1 ExSel

Simple runtime expression language for elixir.

Variables

A variable should start with a lowercase letter and is followed by mixed case letters, numbers or underscore true and false are reserved symbols

# valid:
a , myVar , my_Var, var1

# invalid:
Aaaa , true, false, _myVar, my-var, 9var

Arithmetic expressions:

The supported operators: + - / *

Additionally grouping is supported to override normal precedence rules.

# examples:
(1 + 2) * 5 / 4 - 2
var1 * 9 - varB

Boolean expressions:

The supported comparison operators: > >= < <= == !=

The supported logic operators: && || !

The supported boolean consts: true false

Additionally grouping is supported to override normal precedence rules.

# examples:
true
varA > (1 + 2) || varB == true
var1 * 9 - varB == varC

Link to this section Summary

Functions

Parses the given arithmetic expression into an ExSel.Ast

Parses the given boolean expression into an ExSel.Ast

Evaluates the expression ast

Link to this section Functions

Link to this function aexpr(expression)
aexpr(binary()) :: {:ok, ExSel.Ast.t()} | {:error, term()}

Parses the given arithmetic expression into an ExSel.Ast

Example

iex> ExSel.aexpr("8 + 2 / 4")
{:ok, {:+, [8, {:/, [2, 4]}]}}
Link to this function bexpr(expression)
bexpr(binary()) :: {:ok, ExSel.Ast.t()} | {:error, term()}

Parses the given boolean expression into an ExSel.Ast

Example

iex> ExSel.bexpr("8 + 2 > 4")
{:ok, {:>, [{:+, [8, 2]}, 4]}}

Evaluates the expression ast.

First build an ast using aexpr/1 or bexpr/1. To specify variables that should be available during evaluation pass a map with their values.

Example

iex> {:ok, ast} = ExSel.aexpr("varA + varB / 4")
iex> my_vars = %{"varA" => 8, "varB" => 2}
iex> ExSel.eval!(ast, my_vars)
8.5