tabulon
A high-performance, JIT-compiled expression evaluation engine for Rust, built on Cranelift.
tabulon parses simple arithmetic and boolean expressions and compiles them to native machine code at runtime. It is designed for applications like game servers, configuration scripts, or anywhere you need to safely and repeatedly evaluate user-provided expressions with maximum performance.
Features
- High-Performance JIT Compilation: Expressions are compiled to native machine code for near-native performance, powered by Cranelift.
- Rich Operator Support: Full support for arithmetic (
+,-,*,/), comparison (==,!=,<,<=,>,>=), and logical (&&,||) operators. - Efficient & Safe:
- Short-circuiting for
if(...),&&, and||operators avoids unnecessary computation. - AST-level optimizations like constant folding are performed automatically.
- All execution is sandboxed within the JIT engine.
- Short-circuiting for
- Extensible: Register your own custom Rust functions to be called from within an expression.
- Flexible: Use custom resolvers to map variables to your application's specific data structures.
Quick Start
Use the engine to compile and evaluate an expression:
use Tabula;
Custom Functions
You can easily register your own Rust functions to be used in expressions.
use ;
// Use the `#[function]` attribute to make a function discoverable.
Built-in Functions and Operators
tabulon supports a rich set of built-in operators and functions.
Operators
| Category | Operators | Description |
|---|---|---|
| Arithmetic | +, -, *, / |
Addition, Subtraction, Multiplication, Division. |
| Comparison | ==, !=, >, >=, <, <= |
Equal, Not Equal, Greater Than, Greater/Equal, Less Than, Less/Equal. |
| Logical | && (and), || (or) |
Evaluates logical AND and OR. Both operators are short-circuiting. |
| Unary | - |
Negates a value (e.g., -x). |
Built-in Functions & Constructs
Boolean logic in tabulon follows the convention where 1.0 is true and 0.0 is false.
| Name | Signature | Description |
|---|---|---|
| if | if(condition, then_expr, else_expr) |
If condition evaluates to true (1.0), returns then_expr, otherwise returns else_expr. This is short-circuiting. |
| ifs | ifs(cond1, then1, cond2, then2, ..., else_val) |
Evaluates multiple conditions in order. Returns the value corresponding to the first true condition. If all are false, returns else_val. |
| min | min(a, b) |
Returns the smaller of the two numbers a and b. |
| max | max(a, b) |
Returns the larger of the two numbers a and b. |
Examples
use Tabula;
let mut engine = new;
// Using the `if` function
let expr_if = engine.compile.unwrap;
let health = 75.0;
assert_eq!;
// Using `ifs` for multiple conditions
let expr_ifs = engine.compile.unwrap;
let score = 70.0;
assert_eq!;
// Using `min` and `max`
let expr_minmax = engine.compile.unwrap;
let a = 50.0;
let b = 120.0;
assert_eq!;
Performance
tabulon is designed for speed. By compiling expressions down to a few simple machine instructions, it can evaluate them orders of magnitude faster than a tree-walking interpreter. Benchmarks are included in the repository (cargo bench).
License
This project is licensed under the MIT License.