aboutsummaryrefslogtreecommitdiffstats
path: root/bytecode/src/stack.rs
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-03-03 13:48:19 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-03-03 13:48:19 -0800
commit056fbf7e7dc727d020ba54e439f2b47411c58e76 (patch)
treea85cd582c4b06932463c4754ef466216d9afc8aa /bytecode/src/stack.rs
parent1dd8b1f6eaf611cf5525065aa18f20baa1ada5f3 (diff)
downloadchromatopelma-056fbf7e7dc727d020ba54e439f2b47411c58e76.tar.zst
Start the garbage collector.
I don't think we finished it, but I wrote this code a while ago and I'm just trying to get it committed.
Diffstat (limited to 'bytecode/src/stack.rs')
-rw-r--r--bytecode/src/stack.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/bytecode/src/stack.rs b/bytecode/src/stack.rs
new file mode 100644
index 0000000..66d3f7c
--- /dev/null
+++ b/bytecode/src/stack.rs
@@ -0,0 +1,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);
+ }
+}