Add mapping priorities

pull/1/head
trivernis 4 years ago
parent c887f53b24
commit efe28607b8

@ -1,8 +1,12 @@
use crate::tokens::constants::operations::{
G_AST, G_BACKSLASH, G_BIDWEDGE, G_BIGCAP, G_BIGCUP, G_BIGVEE, G_BOWTIE, G_CAP, G_CDOT, G_CIRC,
G_CUP, G_DIV, G_LTIMES, G_MINUS, G_ODOT, G_OPLUS, G_PLUS, G_PROD, G_RTIMES, G_SLASH, G_STAR,
G_SUM, G_TIMES, G_VEE, G_WEDGE,
};
use crate::tokens::mappings::get_operation_mappings;
use crate::tokens::{Operation, Token};
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,
@ -18,12 +22,17 @@ impl Tokenizer {
}
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())
lazy_static! {
static ref OPERATION_MAPPINGS: Vec<HashMap<&'static [&'static str], Operation>> =
get_operation_mappings();
}
for mapping in OPERATION_MAPPINGS.iter() {
for key in mapping.keys() {
if self.ctm.check_any_str_sequence(*key) {
return Some(mapping[key].clone());
}
}
}
None
}
}
}

@ -1,34 +1,40 @@
use std::collections::HashMap;
use crate::tokens::Operation;
use crate::tokens::constants::operations::*;
use crate::tokens::Operation;
use std::collections::HashMap;
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,
}
}
pub fn get_operation_mappings() -> Vec<HashMap<&'static [&'static str], Operation>> {
vec![
hashmap! {
G_STAR => Operation::Star,
},
hashmap! {
G_AST => Operation::Ast,
G_BOWTIE => Operation::Bowtie,
G_DIV => Operation::Div,
G_BIDWEDGE => Operation::BidWedge,
G_BIGVEE => Operation::BigVee,
G_BIGCAP => Operation::BigCap,
G_BIGCUP => Operation::BigCup,
},
hashmap! {
G_PLUS => Operation::Plus,
G_MINUS => Operation::Minus,
G_CDOT => Operation::CDot,
G_SLASH => Operation::Slash,
G_BACKSLASH => Operation::Backslash,
G_TIMES => Operation::Times,
G_LTIMES => Operation::LTimes,
G_RTIMES => Operation::RTimes,
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_VEE => Operation::Vee,
G_CAP => Operation::Cap,
G_CUP => Operation::Cup,
},
]
}

Loading…
Cancel
Save