summaryrefslogtreecommitdiffstats
path: root/bytecode/src/value.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-02-04 16:46:20 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-02-04 16:46:20 -0800
commit17face3633686e374aa0859271ccf462a48e60aa (patch)
tree6a5482717ef1b8194ccf347a6d8af06d664902cd /bytecode/src/value.rs
parentd940187fd8720e0ab3c00e5a6a7ae8f181c7752d (diff)
downloadsml-17face3633686e374aa0859271ccf462a48e60aa.tar.zst
Rewrite the bytecode interpreter in Rust.
Diffstat (limited to 'bytecode/src/value.rs')
-rw-r--r--bytecode/src/value.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/bytecode/src/value.rs b/bytecode/src/value.rs
new file mode 100644
index 0000000..fbaa128
--- /dev/null
+++ b/bytecode/src/value.rs
@@ -0,0 +1,31 @@
+#[derive(Clone, Copy)]
+pub struct Value(pub u64);
+
+impl Value {
+ pub fn repr(self) -> u64 {
+ let Value(v) = self;
+ v
+ }
+
+ pub fn to_int(self) -> Option<i64> {
+ if self.repr() & 1 == 0 {
+ return None;
+ }
+ Some(self.repr() as i64 >> 1)
+ }
+
+ pub fn from_int(i: i64) -> Value {
+ Value((i as u64) << 1 | 1)
+ }
+
+ pub fn to_pointer(self) -> Option<usize> {
+ if self.repr() == 0 || self.repr() & 7 != 0 {
+ return None;
+ }
+ Some(usize::try_from(self.repr() >> 3).expect("using 32 bits in 2024 LULW"))
+ }
+
+ pub fn from_pointer(p: usize) -> Value {
+ Value(u64::try_from(p).expect("a usize should always fit in a u64... right?") << 3)
+ }
+}