Struct Environment

Source
pub struct Environment<'a> { /* private fields */ }
Expand description

Struct containing custom environment setup for expr evaluation (e.g. custom function definitions)

Example:

use expr::{Context, Environment, Value};
let mut env = Environment::new();
let ctx = Context::default();
env.add_function("add", |c| {
  let mut sum = 0;
    for arg in c.args {
      if let Value::Number(n) = arg {
        sum += n;
       } else {
         panic!("Invalid argument: {arg:?}");
       }
    }
  Ok(sum.into())
});
assert_eq!(env.eval("add(1, 2, 3)", &ctx).unwrap().to_string(), "6");

Implementations§

Source§

impl Environment<'_>

Source

pub fn eval_operator( &self, ctx: &Context, operator: Operator, left: Node, right: Node, ) -> Result<Value>

Source§

impl Environment<'_>

Source

pub fn eval_unary_operator( &self, ctx: &Context, operator: UnaryOperator, node: Node, ) -> Result<Value>

Source§

impl Environment<'_>

Source

pub fn eval_postfix_operator( &self, ctx: &Context, operator: PostfixOperator, node: Node, ) -> Result<Value>

Source§

impl<'a> Environment<'a>

Source

pub fn new() -> Self

Create a new environment with default set of functions

Source

pub fn add_function<F>(&mut self, name: &str, f: F)
where F: Fn(ExprCall<'_, '_>) -> Result<Value> + 'a + Sync + Send,

Add a function for expr programs to call

Example:

use expr::{Context, Environment, Value};
let mut env = Environment::new();
let ctx = Context::default();
env.add_function("add", |c| {
  let mut sum = 0;
    for arg in c.args {
      if let Value::Number(n) = arg {
        sum += n;
       } else {
         panic!("Invalid argument: {arg:?}");
       }
    }
  Ok(sum.into())
});
assert_eq!(env.eval("add(1, 2, 3)", &ctx).unwrap().to_string(), "6");
Source

pub fn run(&self, program: Program, ctx: &Context) -> Result<Value>

Run a compiled expr program

Source

pub fn eval(&self, code: &str, ctx: &Context) -> Result<Value>

Compile and run an expr program in one step

Example:

use std::collections::HashMap;
use expr::{Context, Environment};
let env = Environment::new();
let ctx = Context::default();
assert_eq!(env.eval("1 + 2", &ctx).unwrap().to_string(), "3");
Source

pub fn eval_expr(&self, ctx: &Context, node: Node) -> Result<Value>

Source§

impl Environment<'_>

Source

pub fn eval_func( &self, ctx: &Context, ident: String, args: Vec<Value>, predicate: Option<Program>, ) -> Result<Value>

Trait Implementations§

Source§

impl Debug for Environment<'_>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for Environment<'a>

Source§

fn default() -> Environment<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Environment<'a>

§

impl<'a> !RefUnwindSafe for Environment<'a>

§

impl<'a> Send for Environment<'a>

§

impl<'a> Sync for Environment<'a>

§

impl<'a> Unpin for Environment<'a>

§

impl<'a> !UnwindSafe for Environment<'a>

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.