bitfyre/src/shunting_yard.rs
2025-04-12 19:14:29 -04:00

47 lines
937 B
Rust

pub type Precision = i64;
struct ShuntingYard {
input: String,
output: Vec<String>,
stack: Vec<String>,
}
impl ShuntingYard {
pub const fn new(input: String) -> Self {
Self {
input,
output: Vec::new(),
stack: Vec::new()
}
}
fn is_shift_operator(&mut self, value: char) -> bool {
match value {
'&' | '^' | '|' => {
self.stack.push(value.to_string());
true
},
'>' | '<' => {
match self.stack.last_mut() {
None => self.stack.push(value.to_string()),
Some(last) => match last.as_str() {
">" => *last = ">>".to_owned(),
"<" => *last = "<<".to_owned(),
_ => self.stack.push(value.to_string())
}
}
true
}
_ => false
}
}
pub fn eval(&mut self) {
self.output = self.input.chars()
.collect::<Vec<char>>()
.into_iter()
.filter(|char| self.is_shift_operator(*char))
.map(|char_num| char_num.to_string())
.collect::<Vec<String>>();
}
}