You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
987 B
Rust
45 lines
987 B
Rust
/// This contains enum for list of Tokens, and handles Operator precedence rules.
|
|
|
|
// List of valid tokens that can be constructed from arithmetic expression by Tokenizer
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
pub enum Token {
|
|
Add,
|
|
Subtract,
|
|
Multiply,
|
|
Divide,
|
|
Caret,
|
|
LeftParen,
|
|
RightParen,
|
|
Num(f64),
|
|
EOF,
|
|
}
|
|
|
|
// Order of operators as per operator precedence rules (low to high)
|
|
|
|
#[derive(Debug, PartialEq, PartialOrd)]
|
|
/// Defines all the OperPrec levels, from lowest to highest.
|
|
pub enum OperPrec {
|
|
DefaultZero,
|
|
AddSub,
|
|
MulDiv,
|
|
Power,
|
|
Negative,
|
|
}
|
|
|
|
// This contains methods to retrieve operator precedence for a given arithmetic operator
|
|
|
|
impl Token {
|
|
pub fn get_oper_prec(&self) -> OperPrec {
|
|
use self::OperPrec::*;
|
|
use self::Token::*;
|
|
match *self {
|
|
Add | Subtract => AddSub,
|
|
Multiply | Divide => MulDiv,
|
|
Caret => Power,
|
|
|
|
_ => DefaultZero,
|
|
}
|
|
}
|
|
}
|