ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - narrowtux/abacus: Parses and evaluates mathematical expressions in Elixir. Inspired by math.js
Parses and evaluates mathematical expressions in Elixir. Inspired by math.js - narrowtux/abacus
Visit Site

GitHub - narrowtux/abacus: Parses and evaluates mathematical expressions in Elixir. Inspired by math.js

GitHub - narrowtux/abacus: Parses and evaluates mathematical expressions in Elixir. Inspired by math.js

Abacus Build status

A simple and safe script language for the BEAM.

https://hex.pm/packages/abacus

Language

This language was written to be so simple anyone that has seen mathematical terms before can write Abacus code.

For example, adding a and b is as simple as a + b.

It was also designed to be as safe as possible, allowing you to expose the compiler to a web frontend.

Simplicity

A lot of features make this language easier to use and less confusing than regular Elixir (to laymen) with a whitelist:

  • All common math operators are available as binary operators
  • No string/atom confusion when
    • accessing maps or keyword lists
    • comparing 2 variables
  • Since it is using the Elixir AST, a lot of the advantages apply:
    • Functional
    • Immutable
    • Everything is an expression (even if statements)
    • Last expression returns

Safety

Several measures have been or can be taken to allow safe execution of untrusted code.

  • Scripts don't have access to Erlang/Elixir modules and their functions
  • For variables, atoms are reused across different scripts
    • Variables will be renamed into :var0, var1, ..., varn where n is the amount of distinct variable names in a script
    • Variable amounts can be limited so bad actors can't DoS your service by creating scripts with more variables than the VM atom limit
  • You can move execution off to a dedicated process to
    • Prevent endless loops (set a timeout)
    • Prevent heavy memory usage (set max_heap_size: %{size: 1_000_000, kill: true} in process options)
  • If you use a worker pool you can even prevent DoS attacks that spawn thousands of parallel executions in the allowed timeout (note: phoenix already uses a worker pool so you don't have to spawn your own if you execute abacus expression as a response to a HTTP call)

Performance

While one-off executions can be evaluated using interpretation (through Code.eval_quoted), the real power comes from the Abacus parser returning valid Elixir AST.

This means that the AST can be compiled to BEAM bytecode for performant execution. The performance should be the same as normal Elixir code.

Use cases

  • Write complex filters
  • As a language for map/reduce operations of all kinds
  • For user-supplied export scripts
  • Language for rule-engines (if this then that, authorization rules)

Usage

Abacus.eval(expression, scope)

Parses and evaluates the given expression. Variables can be supplied as a map of binaries in the scope.

Examples

iex> Abacus.eval("1 + 1")
{:ok, 2}
iex> Abacus.eval("sin(floor(3 + 2.5)) / 3")
{:ok, -0.3196414248877128}
iex> Abacus.eval("a * b", %{"a" => 10, "b" => 10})
{:ok, 100}

Abacus.parse(expression)

Parses the expression and returns the syntax tree.

The syntax tree is in quoted elixir. This means that you can use this to compile it into BEAM bytecode for performant execution.

Language features

An incomplete list of supported and planned features

  • basic operators (+, -, *, /, ^)
  • factorial (!)
  • bitwise operators (|, &, >>, <<, ~, |^ (xor))
  • boolean operators (&&, ||, not)
  • comparison operators (==, !=, >, <, >=, <=)
  • grouping terms with parantheses
  • ternary if operator (condition ? if_true : if_false)
  • different data types:
    • integers
    • floats
    • strings
    • boolean (true and false)
    • nulls (nil)
  • support for functions
    • trigonometric functions(sin, cos, tan)
    • floating point manipulation(ceil, floor, round)
  • lambdas ((x, y) => {x + y})
  • do blocks ({statement \n statement})
  • variable assignments
  • enumerable API (map, reduce, filter, ...)
  • [-] literals
    • integers
    • floats
    • strings
    • lists
    • maps
  • support for variables

Installation

  1. Add abacus to your list of dependencies in mix.exs:
def deps do
  [{:abacus, "~> 0.4.2"}]
end
  1. Ensure abacus is started before your application:
def application do
  [applications: [:abacus]]
end

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory