Add operation mappings and parsing
parent
40e92fdb47
commit
c887f53b24
@ -0,0 +1,29 @@
|
||||
use charred::tapemachine::CharTapeMachine;
|
||||
use crate::tokens::{Token, Operation};
|
||||
use crate::tokens::constants::operations::{G_PLUS, G_MINUS, G_CDOT, G_AST, G_STAR, G_SLASH, G_BACKSLASH, G_TIMES, G_DIV, G_LTIMES, G_RTIMES, G_BOWTIE, G_CIRC, G_OPLUS, G_ODOT, G_SUM, G_PROD, G_WEDGE, G_BIDWEDGE, G_VEE, G_BIGVEE, G_CAP, G_BIGCAP, G_CUP, G_BIGCUP};
|
||||
use std::collections::HashMap;
|
||||
use crate::tokens::mappings::get_operation_mapping;
|
||||
|
||||
pub struct Tokenizer {
|
||||
ctm: CharTapeMachine,
|
||||
tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
impl Tokenizer {
|
||||
pub fn new(text: String) -> Self {
|
||||
Self {
|
||||
ctm: CharTapeMachine::new(text.chars().collect()),
|
||||
tokens: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_operation(&mut self) -> Option<Operation> {
|
||||
lazy_static! { static ref OPERATION_MAPPING: HashMap<&'static[&'static str], Operation> = get_operation_mapping();}
|
||||
for key in OPERATION_MAPPING.keys() {
|
||||
if self.ctm.check_any_str_sequence(*key) {
|
||||
return Some(OPERATION_MAPPING[key].clone())
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
use std::collections::HashMap;
|
||||
use crate::tokens::Operation;
|
||||
use crate::tokens::constants::operations::*;
|
||||
|
||||
pub fn get_operation_mapping() -> HashMap<&'static[&'static str], Operation> {
|
||||
hashmap! {
|
||||
G_PLUS => Operation::Plus,
|
||||
G_MINUS => Operation::Minus,
|
||||
G_CDOT => Operation::CDot,
|
||||
G_AST => Operation::Ast,
|
||||
G_STAR => Operation::Star,
|
||||
G_SLASH => Operation::Slash,
|
||||
G_BACKSLASH => Operation::Backslash,
|
||||
G_TIMES => Operation::Times,
|
||||
G_DIV => Operation::Div,
|
||||
G_LTIMES => Operation::LTimes,
|
||||
G_RTIMES => Operation::RTimes,
|
||||
G_BOWTIE => Operation::Bowtie,
|
||||
G_CIRC => Operation::Circ,
|
||||
G_OPLUS => Operation::OPlus,
|
||||
G_OTIMES => Operation::OTimes,
|
||||
G_ODOT => Operation::ODot,
|
||||
G_SUM => Operation::Sum,
|
||||
G_PROD => Operation::Prod,
|
||||
G_WEDGE => Operation::Wedge,
|
||||
G_BIDWEDGE => Operation::BidWedge,
|
||||
G_VEE => Operation::Vee,
|
||||
G_BIGVEE => Operation::BigVee,
|
||||
G_CAP => Operation::Cap,
|
||||
G_BIGCAP => Operation::BigCap,
|
||||
G_CUP => Operation::Cup,
|
||||
G_BIGCUP => Operation::BigCup,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue