[−][src]Struct arithmetic_eval::ExecutableModule
Executable module together with its imports.
Examples
use arithmetic_parser::{grammars::F32Grammar, GrammarExt, Span}; use arithmetic_eval::{fns, Interpreter, Value}; let mut interpreter = Interpreter::new(); interpreter .insert_native_fn( "max", fns::Binary::new(|x, y| if x > y { x } else { y }), ) .insert_native_fn("fold", fns::Fold) .insert_var("INF", Value::Number(f32::INFINITY)) .insert_var("xs", Value::Tuple(vec![])); let module = "xs.fold(-INF, max)"; let module = F32Grammar::parse_statements(Span::new(module)).unwrap(); let mut module = interpreter.compile(&module).unwrap(); // With the original imports, the returned value is `-INF`. assert_eq!(module.run().unwrap(), Value::Number(f32::NEG_INFINITY)); // Imports can be changed. Let's check that `xs` is indeed an import. let imports: HashSet<_> = module.imports().map(|(name, _)| name).collect(); assert_eq!(imports, HashSet::from_iter(vec!["max", "fold", "xs", "INF"])); // Change the `xs` import and run the module again. let array = [1.0, -3.0, 2.0, 0.5].iter().copied().map(Value::Number).collect(); module.set_import("xs", Value::Tuple(array)); assert_eq!(module.run().unwrap(), Value::Number(2.0));
Implementations
impl<'a, T: Grammar> ExecutableModule<'a, T>
[src]
pub fn set_import(&mut self, name: &str, value: Value<'a, T>) -> &mut Self
[src]
Sets the value of an imported variable.
Panics
Panics if the variable with the specified name is not an import.
pub fn imports(&self) -> impl Iterator<Item = (&'a str, &Value<'a, T>)> + '_
[src]
Enumerates imports of this module together with their current values.
impl<'a, T: Grammar> ExecutableModule<'a, T> where
T::Lit: Num + Neg<Output = T::Lit> + Pow<T::Lit, Output = T::Lit>,
[src]
T::Lit: Num + Neg<Output = T::Lit> + Pow<T::Lit, Output = T::Lit>,
pub fn run(&self) -> Result<Value<'a, T>, ErrorWithBacktrace<'a>>
[src]
Runs the module with the current values of imports.
Trait Implementations
impl<'a, T: Debug + Grammar> Debug for ExecutableModule<'a, T>
[src]
Auto Trait Implementations
impl<'a, T> !RefUnwindSafe for ExecutableModule<'a, T>
impl<'a, T> !Send for ExecutableModule<'a, T>
impl<'a, T> !Sync for ExecutableModule<'a, T>
impl<'a, T> Unpin for ExecutableModule<'a, T> where
<T as Grammar>::Lit: Unpin,
<T as Grammar>::Lit: Unpin,
impl<'a, T> !UnwindSafe for ExecutableModule<'a, T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,