Skip to main content

Interpreter

Struct Interpreter 

Source
pub struct Interpreter { /* private fields */ }

Implementations§

Source§

impl Interpreter

Source

pub fn new() -> Self

Examples found in repository?
examples/example.rs (line 6)
5fn main() {
6    let mut interp = Interpreter::new();
7    interp.eval("x = 10").unwrap();
8    interp.eval("y = 20").unwrap();
9    let val = interp
10        .eval("[max(x + y * 2, x * 2 - y), abs(x - y), x**2]")
11        .unwrap();
12    println!("{}", val); // 50
13
14    let context = HashMap::from([("x".to_string(), Value::Number(10.0))]);
15    let val = interp.eval_with_context("x + 1", &context).unwrap();
16    println!("{}", val); // 11
17
18    let val = interp.eval_boolean("x + 1").unwrap();
19    println!("{}", val); // true
20}
Source

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

Examples found in repository?
examples/example.rs (line 7)
5fn main() {
6    let mut interp = Interpreter::new();
7    interp.eval("x = 10").unwrap();
8    interp.eval("y = 20").unwrap();
9    let val = interp
10        .eval("[max(x + y * 2, x * 2 - y), abs(x - y), x**2]")
11        .unwrap();
12    println!("{}", val); // 50
13
14    let context = HashMap::from([("x".to_string(), Value::Number(10.0))]);
15    let val = interp.eval_with_context("x + 1", &context).unwrap();
16    println!("{}", val); // 11
17
18    let val = interp.eval_boolean("x + 1").unwrap();
19    println!("{}", val); // true
20}
Source

pub fn eval_with_context( &self, code: &str, context: &HashMap<String, Value>, ) -> Result<Value, EvalError>

Examples found in repository?
examples/example.rs (line 15)
5fn main() {
6    let mut interp = Interpreter::new();
7    interp.eval("x = 10").unwrap();
8    interp.eval("y = 20").unwrap();
9    let val = interp
10        .eval("[max(x + y * 2, x * 2 - y), abs(x - y), x**2]")
11        .unwrap();
12    println!("{}", val); // 50
13
14    let context = HashMap::from([("x".to_string(), Value::Number(10.0))]);
15    let val = interp.eval_with_context("x + 1", &context).unwrap();
16    println!("{}", val); // 11
17
18    let val = interp.eval_boolean("x + 1").unwrap();
19    println!("{}", val); // true
20}
Source

pub fn eval_boolean(&self, code: &str) -> Result<bool, EvalError>

Examples found in repository?
examples/example.rs (line 18)
5fn main() {
6    let mut interp = Interpreter::new();
7    interp.eval("x = 10").unwrap();
8    interp.eval("y = 20").unwrap();
9    let val = interp
10        .eval("[max(x + y * 2, x * 2 - y), abs(x - y), x**2]")
11        .unwrap();
12    println!("{}", val); // 50
13
14    let context = HashMap::from([("x".to_string(), Value::Number(10.0))]);
15    let val = interp.eval_with_context("x + 1", &context).unwrap();
16    println!("{}", val); // 11
17
18    let val = interp.eval_boolean("x + 1").unwrap();
19    println!("{}", val); // true
20}

Auto Trait Implementations§

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.