aboutsummaryrefslogtreecommitdiffstats
path: root/bytecode/src/stack.rs
blob: 66d3f7c0706cf0d4d4dd338f6e6e9ee385889a59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct Stack {
    v: Vec<u64>,
}

impl Stack {
    fn pop(&mut self) -> Result<u64, String> {
        self.v.pop().ok_or(String::from("stack underflow"))
    }

    fn pop_int(&mut self) -> Result<i64, String> {
        Ok(self.pop()? as i64 >> 1)
    }

    fn push_int(&mut self, i: i64) {
        self.v.push((i as u64) << 1 | 1);
    }

    fn pop_pointer(&mut self) -> Result<Pointer, String> {
        Ok(Pointer::from_bytes(self.pop()?))
    }

    fn push_pointer(&mut self, p: Pointer) {
        self.v.push(p.bytes());
    }

    fn pop_usize(&mut self) -> Result<usize, String> {
        Ok(usize::try_from(self.pop()?).unwrap() >> 1)
    }

    fn push_usize(&mut self, p: usize) {
        self.v.push(u64::try_from(p).unwrap() << 1 | 1);
    }
}