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::{num, Program, SymTable};

// 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", num!(5), false).unwrap();
let mut linked = program.link(table).unwrap();

// Execute
assert_eq!(linked.execute().unwrap(), num!(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();

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§

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

Source

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

Executes the program and returns the result.

Source

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

Returns a mutable reference to the symbol table.

Source

pub fn get_assembly(&self) -> String

Returns a human-readable assembly representation of the program.

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.