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>
impl<'src> Program<'src, Compiled>
Sourcepub fn new_from_source(source: &'src str) -> Result<Self, ProgramError>
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();Sourcepub fn new_from_file(path: impl Into<String>) -> Result<Self, ProgramError>
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();Sourcepub fn new_from_bytecode(data: &[u8]) -> Result<Self, ProgramError>
pub fn new_from_bytecode(data: &[u8]) -> Result<Self, ProgramError>
Creates a compiled program from bytecode bytes.
Deserializes the bytecode and validates the version.
Sourcepub fn link(
self,
table: SymTable,
) -> Result<Program<'src, Linked>, ProgramError>
pub fn link( self, table: SymTable, ) -> Result<Program<'src, Linked>, ProgramError>
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();Sourcepub fn symbols(&self) -> &[SymbolMetadata]
pub fn symbols(&self) -> &[SymbolMetadata]
Returns the symbol metadata required by this program.
Source§impl<'src> Program<'src, Linked>
impl<'src> Program<'src, Linked>
Sourcepub fn symtable_mut(&mut self) -> &mut SymTable
pub fn symtable_mut(&mut self) -> &mut SymTable
Returns a mutable reference to the symbol table.
Sourcepub fn get_assembly(&self) -> String
pub fn get_assembly(&self) -> String
Returns a human-readable assembly representation of the program.
Sourcepub fn to_bytecode(&self) -> Result<Vec<u8>, ProgramError>
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.
Sourcepub fn save_bytecode_to_file(
&self,
path: impl AsRef<Path>,
) -> Result<(), ProgramError>
pub fn save_bytecode_to_file( &self, path: impl AsRef<Path>, ) -> Result<(), ProgramError>
Saves the program bytecode to a file.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more