aboutsummaryrefslogtreecommitdiffstats
path: root/bytecode/src/data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bytecode/src/data.rs')
-rw-r--r--bytecode/src/data.rs115
1 files changed, 14 insertions, 101 deletions
diff --git a/bytecode/src/data.rs b/bytecode/src/data.rs
index 4331f46..d6f9bd2 100644
--- a/bytecode/src/data.rs
+++ b/bytecode/src/data.rs
@@ -1,4 +1,7 @@
+use std::fmt;
+
// The data representations of values of different types are given below.
+// - 0 represents an undefined value. Any operation with 0 will cause an error.
// - Pointers are 64 bit unsigned (positive) ints which are
// word-aligned, i.e. 0 mod 8. All types not listed below are
// allocated on the heap behind a pointer.
@@ -18,12 +21,17 @@
pub struct Pointer(pub usize);
impl Pointer {
- pub fn offset(self, i: i64) -> Self {
+ pub fn offset(self, i: usize) -> Self {
let Pointer(u) = self;
- if i > 0 {
- return Pointer(u + usize::try_from(i).unwrap());
- }
- return Pointer(u - usize::try_from(-i).unwrap());
+ Pointer(u + i)
+ }
+}
+
+impl fmt::LowerHex for Pointer {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let val = self.0;
+
+ fmt::LowerHex::fmt(&val, f)
}
}
@@ -31,14 +39,9 @@ impl Pointer {
pub struct Value(pub u64);
impl Value {
- pub fn is_nil(self) -> bool {
- let Value(stack_representation) = self;
- return stack_representation == 0;
- }
-
pub fn is_pointer(self) -> bool {
let Value(stack_representation) = self;
- return stack_representation & 0x7 == 0;
+ return stack_representation != 0 && stack_representation & 0x7 == 0;
}
pub fn from_pointer(p: Pointer) -> Self {
@@ -71,47 +74,12 @@ impl Value {
return Ok(stack_representation as i64 >> 1);
}
- pub fn is_bool(self) -> bool {
- let Value(stack_representation) = self;
- return stack_representation == 0xa || stack_representation == 0x2;
- }
-
pub fn from_bool(b: bool) -> Self {
if b {
return Value(0xa);
}
return Value(0x2);
}
-
- pub fn to_bool(self) -> Result<bool, String> {
- let Value(stack_representation) = self;
- if !self.is_bool() {
- return Err(format!("value {:x} is not a boolean", stack_representation));
- }
- return Ok(stack_representation == 0xa);
- }
-
- pub fn is_char(self) -> bool {
- let Value(stack_representation) = self;
- return stack_representation & 0x7 == 4;
- }
-
- pub fn from_char(c: char) -> Self {
- return Value(u64::from(c) << 32 | 0x4);
- }
-
- pub fn to_char(self) -> Result<char, String> {
- let Value(stack_representation) = self;
- if !self.is_char() {
- return Err(format!("value {:x} is not a char", stack_representation));
- }
- return Ok(
- char::from_u32((stack_representation >> 32) as u32).ok_or(format!(
- "value {:x} is not a valid char",
- stack_representation >> 32
- ))?,
- );
- }
}
#[cfg(test)]
@@ -119,16 +87,6 @@ mod tests {
use super::*;
#[test]
- fn is_nil() {
- assert_eq!(true, Value(0).is_nil());
- }
-
- #[test]
- fn is_not_nil() {
- assert_eq!(false, Value(1).is_nil());
- }
-
- #[test]
fn is_pointer() {
assert_eq!(true, Value(0xf8).is_pointer());
}
@@ -169,21 +127,6 @@ mod tests {
}
#[test]
- fn true_is_bool() {
- assert_eq!(true, Value(0xa).is_bool());
- }
-
- #[test]
- fn false_is_bool() {
- assert_eq!(true, Value(2).is_bool());
- }
-
- #[test]
- fn is_not_bool() {
- assert_eq!(false, Value(0).is_bool());
- }
-
- #[test]
fn true_from_bool() {
assert_eq!(Value(0xa), Value::from_bool(true));
}
@@ -192,34 +135,4 @@ mod tests {
fn false_from_bool() {
assert_eq!(Value(2), Value::from_bool(false));
}
-
- #[test]
- fn true_to_bool() {
- assert_eq!(Ok(true), Value(0xa).to_bool());
- }
-
- #[test]
- fn false_to_bool() {
- assert_eq!(Ok(false), Value(2).to_bool());
- }
-
- #[test]
- fn is_char() {
- assert_eq!(true, Value(0x6100000004).is_char());
- }
-
- #[test]
- fn is_not_char() {
- assert_eq!(false, Value(0).is_char());
- }
-
- #[test]
- fn from_char() {
- assert_eq!(Value(0x5800000004), Value::from_char('X'));
- }
-
- #[test]
- fn to_char() {
- assert_eq!(Ok('😂'), Value(0x1f60200000004).to_char());
- }
}