pub struct Tabula<K = String, R = IdentityResolver> { /* private fields */ }Expand description
The main JIT compilation and evaluation engine.
Tabula is the entry point for parsing, compiling, and evaluating expressions.
It holds the JIT compiler context, registered functions, and a variable resolver.
The type parameters K and R define the variable key type and the resolver logic,
respectively. By default, it uses String keys.
§Examples
use tabulon::Tabula;
let mut engine = Tabula::new();
let expr = engine.compile("a + b").unwrap();
let result = expr.eval(&[10.0, 20.0]).unwrap();
assert_eq!(result, 30.0);Implementations§
Source§impl Tabula<String, IdentityResolver>
impl Tabula<String, IdentityResolver>
Source§impl<K, R> Tabula<K, R>
impl<K, R> Tabula<K, R>
Sourcepub fn with_resolver(resolver: R) -> Self
pub fn with_resolver(resolver: R) -> Self
Creates a new Tabula engine with a custom variable resolver.
This allows you to define custom logic for mapping variable names from expression strings
to a key type K of your choice (e.g., a u64, an enum, etc.).
§Examples
use tabulon::{Tabula, VarResolver, VarResolveError};
// A resolver that maps "strength" to key 1 and "dexterity" to key 2.
struct MyResolver;
impl VarResolver<u32> for MyResolver {
fn resolve(&self, ident: &str) -> Result<u32, VarResolveError> {
match ident {
"strength" => Ok(1),
"dexterity" => Ok(2),
_ => Err(VarResolveError::Unknown(ident.to_string())),
}
}
}
let mut engine = Tabula::with_resolver(MyResolver);
let expr = engine.compile("strength * 2").unwrap();
assert_eq!(expr.vars(), &[1]);Sourcepub fn register_nullary(&mut self, name: &str, f: Fn0) -> Result<(), JitError>
pub fn register_nullary(&mut self, name: &str, f: Fn0) -> Result<(), JitError>
Registers a nullary (0-argument) function with the engine.
Functions must be registered before any expressions are compiled.
Sourcepub fn register_unary(&mut self, name: &str, f: Fn1) -> Result<(), JitError>
pub fn register_unary(&mut self, name: &str, f: Fn1) -> Result<(), JitError>
Registers a unary (1-argument) function with the engine.
Sourcepub fn register_binary(&mut self, name: &str, f: Fn2) -> Result<(), JitError>
pub fn register_binary(&mut self, name: &str, f: Fn2) -> Result<(), JitError>
Registers a binary (2-argument) function with the engine.
§Examples
use tabulon::Tabula;
fn my_pow(base: f64, exp: f64) -> f64 {
base.powf(exp)
}
let mut engine = Tabula::new();
// The function must be registered before compiling any expressions.
engine.register_binary("my_pow", my_pow).unwrap();
let expr = engine.compile("my_pow(a, 3)").unwrap();
let result = expr.eval(&[2.0]).unwrap();
assert_eq!(result, 8.0);Sourcepub fn register_ternary(&mut self, name: &str, f: Fn3) -> Result<(), JitError>
pub fn register_ternary(&mut self, name: &str, f: Fn3) -> Result<(), JitError>
Registers a ternary (3-argument) function with the engine.
Sourcepub fn compile(&mut self, expr: &str) -> Result<CompiledExpr<K>, JitError>
pub fn compile(&mut self, expr: &str) -> Result<CompiledExpr<K>, JitError>
Compiles an expression string into an executable CompiledExpr.
This method parses, optimizes, and JIT-compiles the expression to native machine code.
The returned CompiledExpr owns the compiled code and the variable map.
Evaluation is performed by passing a slice of f64 values.
§Errors
Returns a JitError if parsing, resolution, or compilation fails.
Sourcepub fn compile_ref(
&mut self,
expr: &str,
) -> Result<CompiledExprRef<K>, JitError>
pub fn compile_ref( &mut self, expr: &str, ) -> Result<CompiledExprRef<K>, JitError>
Compiles an expression string into an executable CompiledExprRef.
This is similar to compile, but is designed for evaluation via pointers/references.
The returned CompiledExprRef is tied to the lifetime of the Tabula engine.
Evaluation is performed by passing a slice of &f64 or *const f64.
§Errors
Returns a JitError if parsing, resolution, or compilation fails.
Sourcepub fn free_memory(&mut self)
pub fn free_memory(&mut self)
Frees all JIT-allocated memory for compiled expressions and resets the JIT module.
After calling this, any previously created CompiledExpr or CompiledExprRef instances
become invalid and attempting to use them will result in an JitError::Invalidated error.
This is useful for reclaiming memory in long-running applications.
§Examples
use tabulon::{Tabula, JitError};
let mut engine = Tabula::new();
let expr = engine.compile("a + 1").unwrap();
assert_eq!(expr.eval(&[5.0]).unwrap(), 6.0);
// Free all compiled code
engine.free_memory();
// Evaluating the old expression now returns an error
match expr.eval(&[5.0]) {
Err(JitError::Invalidated) => { /* This is expected */ },
_ => panic!("Expected an Invalidated error"),
}Sourcepub fn clear_registered_functions(&mut self)
pub fn clear_registered_functions(&mut self)
Clears the custom function registry.
This allows re-registering a different set of functions before compiling new expressions.
Note: This should typically be called after free_memory if expressions have already been compiled.