[][src]Struct arithmetic_eval::ExecutableModule

pub struct ExecutableModule<'a, T: Grammar> { /* fields omitted */ }

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]

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

impl<'a, T> !UnwindSafe for ExecutableModule<'a, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.