Program

Struct Program 

Source
pub struct Program<'src, State> { /* private fields */ }
Expand description

Type-state program using Rust’s type system to enforce correct usage.

§Examples

use expr_solver::{Program, SymTable};
use rust_decimal_macros::dec;

// Compile from source
let program = Program::new_from_source("x * 2 + 1").unwrap();

// Link with symbol table
let mut table = SymTable::new();
table.add_const("x", dec!(5)).unwrap();
let linked = program.link(table).unwrap();

// Execute
assert_eq!(linked.execute().unwrap(), dec!(11));

Implementations§

Source§

impl<'src> Program<'src, Compiled>

Source

pub fn new_from_source(source: &'src str) -> Result<Self, ProgramError>

Creates a compiled program from source code.

§Examples
use expr_solver::Program;

let program = Program::new_from_source("2 + 3 * 4").unwrap();
Source

pub fn new_from_file(path: impl Into<String>) -> Result<Self, ProgramError>

Creates a compiled program from a binary file.

§Examples
use expr_solver::Program;

let program = Program::new_from_file("expr.bin").unwrap();
Source

pub fn new_from_bytecode(data: &[u8]) -> Result<Self, ProgramError>

Creates a compiled program from bytecode bytes.

Deserializes the bytecode and validates the version.

Links the bytecode with a symbol table.

Validates that all required symbols are present and compatible.

§Examples
use expr_solver::{Program, SymTable};

let program = Program::new_from_source("sin(pi)").unwrap();
let linked = program.link(SymTable::stdlib()).unwrap();
Source

pub fn symbols(&self) -> &[SymbolMetadata]

Returns the symbol metadata required by this program.

Source

pub fn version(&self) -> &str

Returns the version of this program.

Source§

impl<'src> Program<'src, Linked>

Source

pub fn execute(&self) -> Result<Decimal, VmError>

Executes the program and returns the result.

Source

pub fn symtable(&self) -> &SymTable

Returns a reference to the symbol table.

Source

pub fn symtable_mut(&mut self) -> &mut SymTable

Returns a mutable reference to the symbol table.

Source

pub fn version(&self) -> &str

Returns the version of this program.

Source

pub fn get_assembly(&self) -> String

Returns a human-readable assembly representation of the program.

Source

pub fn to_bytecode(&self) -> Result<Vec<u8>, ProgramError>

Converts the program to bytecode bytes.

This involves reverse-mapping the bytecode indices back to metadata indices.

Source

pub fn save_bytecode_to_file( &self, path: impl AsRef<Path>, ) -> Result<(), ProgramError>

Saves the program bytecode to a file.

Trait Implementations§

Source§

impl<'src, State: Debug> Debug for Program<'src, State>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'src, State> Freeze for Program<'src, State>
where State: Freeze,

§

impl<'src, State> RefUnwindSafe for Program<'src, State>
where State: RefUnwindSafe,

§

impl<'src, State> Send for Program<'src, State>
where State: Send,

§

impl<'src, State> Sync for Program<'src, State>
where State: Sync,

§

impl<'src, State> Unpin for Program<'src, State>
where State: Unpin,

§

impl<'src, State> UnwindSafe for Program<'src, State>
where State: UnwindSafe,

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.