[][src]Struct prec::Climber

pub struct Climber<Op: Hash + Eq + Copy, To: Token<Re, Err, Ctx> + Clone, Re, Err, Ctx = ()> {
    pub rules: HashMap<Op, (usize, Assoc)>,
    pub handler: fn(_: To, _: Op, _: To, _: &Ctx) -> Result<To, Err>,
    // some fields omitted
}

A struct containing the order of operations rules and a pointer to a handler function.

Generics

Has three generics:

Op

An enum or other value that is used to specify different rules.

Required implementations: Hash, Eq, Copy

To

A token used in expressions between operators.

Required implementations: Into, Clone

Re

A result value returned from the compute function.

Err

The error type returned in results.

Ctx

A context value made available across an entire expression while evaluating. Entirely optional, defaults to ()

Fields

rules: HashMap<Op, (usize, Assoc)>

A map of Rule s.

handler: fn(_: To, _: Op, _: To, _: &Ctx) -> Result<To, Err>

Function to handle the result of an operator between two tokens.

Arguments are:

  • Left-hand side token
  • Operator
  • Right-hand side token

Implementations

impl<Op: Hash + Eq + Copy, To: Token<Re, Err, Ctx> + Clone, Re, Err, Ctx> Climber<Op, To, Re, Err, Ctx>[src]

pub fn new(
    rules: Vec<Rule<Op>>,
    handler: fn(_: To, _: Op, _: To, _: &Ctx) -> Result<To, Err>
) -> Self
[src]

Construtor for a new climber. Rules with the same [precedence level][1] are separated by a | character.

This example is not tested
fn handler(lhs: f64, op: Op, rhs: f64, _:&()) -> Result<f64, ()> {
	Ok(match op {
		Op::Add => lhs + rhs,
		Op::Sub => lhs - rhs,
		Op::Mul => lhs * rhs,
		Op::Div => lhs / rhs,
		Op::Exp => lhs.powf(rhs)
	})
}

let climber = Climber::new(
	vec![
		Rule::new(Op::Add, Assoc::Left) | Rule::new(Op::Sub, Assoc::Right),
		Rule::new(Op::Mul, Assoc::Left) | Rule::new(Op::Div, Assoc::Right),
		Rule::new(Op::Exp, Assoc::Right)
	],
	handler
);

pub fn process(&self, expr: &Expression<Op, To>, ctx: &Ctx) -> Result<Re, Err>[src]

Process an Expression and return the resulting value.

This example is not tested
// 2 + 2 * 3
// 2 + 6
// 8
let expression = Expression::new(
	2.0f64,
	vec![
		(Op::Add, 2.0f64),
		(Op::Mul, 3.0f64)
	]
);
assert_eq!(climber.process(&expression, &()).unwrap(), 8.0f64);

Auto Trait Implementations

impl<Op, To, Re, Err, Ctx> RefUnwindSafe for Climber<Op, To, Re, Err, Ctx> where
    Ctx: RefUnwindSafe,
    Op: RefUnwindSafe,
    Re: RefUnwindSafe,
    To: RefUnwindSafe
[src]

impl<Op, To, Re, Err, Ctx> Send for Climber<Op, To, Re, Err, Ctx> where
    Ctx: Send,
    Op: Send,
    Re: Send,
    To: Send
[src]

impl<Op, To, Re, Err, Ctx> Sync for Climber<Op, To, Re, Err, Ctx> where
    Ctx: Sync,
    Op: Sync,
    Re: Sync,
    To: Sync
[src]

impl<Op, To, Re, Err, Ctx> Unpin for Climber<Op, To, Re, Err, Ctx> where
    Ctx: Unpin,
    Op: Unpin,
    Re: Unpin,
    To: Unpin
[src]

impl<Op, To, Re, Err, Ctx> UnwindSafe for Climber<Op, To, Re, Err, Ctx> where
    Ctx: UnwindSafe,
    Op: UnwindSafe,
    Re: UnwindSafe,
    To: UnwindSafe
[src]

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.