Tabula

Struct Tabula 

Source
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>

Source

pub fn new() -> Self

Creates a new Tabula engine with the default configuration.

The default engine uses String keys for variables and resolves them to themselves.

Source§

impl<K, R> Tabula<K, R>
where K: Clone + Eq + Hash + Send + Sync + 'static, R: VarResolver<K>,

Source

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]);
Source

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.

Source

pub fn register_unary(&mut self, name: &str, f: Fn1) -> Result<(), JitError>

Registers a unary (1-argument) function with the engine.

Source

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);
Source

pub fn register_ternary(&mut self, name: &str, f: Fn3) -> Result<(), JitError>

Registers a ternary (3-argument) function with the engine.

Source

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.

Source

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.

Source

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"),
}
Source

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.

Trait Implementations§

Source§

impl Default for Tabula<String, IdentityResolver>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K = String, R = IdentityResolver> !Freeze for Tabula<K, R>

§

impl<K = String, R = IdentityResolver> !RefUnwindSafe for Tabula<K, R>

§

impl<K = String, R = IdentityResolver> !Send for Tabula<K, R>

§

impl<K = String, R = IdentityResolver> !Sync for Tabula<K, R>

§

impl<K, R> Unpin for Tabula<K, R>
where R: Unpin, K: Unpin,

§

impl<K = String, R = IdentityResolver> !UnwindSafe for Tabula<K, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.