[−][src]Struct prec::Climber
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
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]
rules: Vec<Rule<Op>>,
handler: fn(_: To, _: Op, _: To, _: &Ctx) -> Result<To, Err>
) -> Self
Construtor for a new climber.
Rules with the same [precedence level][1] are separated by a |
character.
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.
// 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]
Ctx: RefUnwindSafe,
Op: RefUnwindSafe,
Re: RefUnwindSafe,
To: RefUnwindSafe,
impl<Op, To, Re, Err, Ctx> Send for Climber<Op, To, Re, Err, Ctx> where
Ctx: Send,
Op: Send,
Re: Send,
To: Send,
[src]
Ctx: Send,
Op: Send,
Re: Send,
To: Send,
impl<Op, To, Re, Err, Ctx> Sync for Climber<Op, To, Re, Err, Ctx> where
Ctx: Sync,
Op: Sync,
Re: Sync,
To: Sync,
[src]
Ctx: Sync,
Op: Sync,
Re: Sync,
To: Sync,
impl<Op, To, Re, Err, Ctx> Unpin for Climber<Op, To, Re, Err, Ctx> where
Ctx: Unpin,
Op: Unpin,
Re: Unpin,
To: Unpin,
[src]
Ctx: Unpin,
Op: Unpin,
Re: Unpin,
To: Unpin,
impl<Op, To, Re, Err, Ctx> UnwindSafe for Climber<Op, To, Re, Err, Ctx> where
Ctx: UnwindSafe,
Op: UnwindSafe,
Re: UnwindSafe,
To: UnwindSafe,
[src]
Ctx: UnwindSafe,
Op: UnwindSafe,
Re: UnwindSafe,
To: UnwindSafe,
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,
pub 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.
pub 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>,