lib_ruby_parser/
stack_state.rs

1#[derive(Clone, Default)]
2pub struct StackState {
3    name: &'static str,
4    stack: usize,
5}
6
7impl StackState {
8    pub(crate) fn new(name: &'static str) -> Self {
9        Self { name, stack: 0 }
10    }
11
12    pub(crate) fn is_empty(&self) -> bool {
13        self.stack == 0
14    }
15
16    pub fn push(&mut self, bit: bool) {
17        let bit_value = if bit { 1 } else { 0 };
18        self.stack = (self.stack << 1) | bit_value
19    }
20
21    pub fn pop(&mut self) {
22        self.stack >>= 1;
23    }
24
25    pub(crate) fn is_active(&self) -> bool {
26        (self.stack & 1) == 1
27    }
28}
29
30impl std::fmt::Debug for StackState {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        f.write_str(&format!("[{:b} <= {}]", self.stack, self.name))
33    }
34}