diff --git a/hyphae/src/heap.rs b/hyphae/src/heap.rs index 46f4aab..447ff9e 100644 --- a/hyphae/src/heap.rs +++ b/hyphae/src/heap.rs @@ -175,6 +175,19 @@ impl Clone for Datum { } } +impl Into> for Datum { + fn into(self) -> Vec { + unimplemented!("") + } +} + +impl TryFrom<&[u8]> for Datum { + type Error = &str; + fn try_from(value: &[u8]) -> Result { + unimplemented!("") + } +} + #[derive(Clone, PartialEq, Debug)] pub struct Cons(pub Option>, pub Option>); @@ -293,6 +306,18 @@ impl Index for Cons { } } +impl Into> for Cons { + fn into(self) -> Vec { + unimplemented!("") + } +} + +impl TryFrom<&[u8]> for Cons { + type Error = &str; + fn try_from(value: &[u8]) -> Result { + unimplemented!("") + } +} #[cfg(test)] mod tests { diff --git a/organelle/src/lib.rs b/organelle/src/lib.rs index cec5dc5..e43cf13 100644 --- a/organelle/src/lib.rs +++ b/organelle/src/lib.rs @@ -21,6 +21,8 @@ extern crate alloc; use alloc::string::String; use alloc::format; use alloc::fmt::Debug; +use alloc::vec; +use alloc::vec::Vec; use core::{cmp::Ordering, f64, ops::{Add, Div, Mul, Sub}, str::FromStr}; use num::{integer::{gcd}, pow::Pow}; @@ -250,6 +252,77 @@ impl FromStr for Number { } } +// this looks rushed and it is +// would rather work on organelles replacement than improve this +impl Into> for Number { + fn into(self) -> Vec { + let mut out = vec![]; + match self { + Number::Sci(num) => { + out.push(0x00); + for ele in num.0.to_be_bytes().iter() { + out.push(*ele); + } + for ele in num.1.to_be_bytes().iter() { + out.push(*ele); + } + out + }, + Number::Flt(num) => { + out.push(0x01 as u8); + for ele in num.0.to_be_bytes().iter() { + out.push(*ele); + } + out + }, + Number::Fra(num) => { + out.push(0x02); + for ele in num.0.to_be_bytes().iter() { + out.push(*ele); + } + for ele in num.0.to_be_bytes().iter() { + out.push(*ele); + } + out + }, + Number::Sym(num) => { + match num { + SymbolicNumber::Inf => out.push(0x03), + SymbolicNumber::NaN => out.push(0x04), + SymbolicNumber::NegInf => out.push(0x05), + SymbolicNumber::NegNan => out.push(0x06), + } + out + } + } + } +} + +impl TryFrom<&[u8]> for Number { + type Error = &'static str; + fn try_from(value: &[u8]) -> Result { + match value[0] { + 0x03 => Ok(Number::Sym(SymbolicNumber::Inf)), + 0x04 => Ok(Number::Sym(SymbolicNumber::NaN)), + 0x05 => Ok(Number::Sym(SymbolicNumber::NegInf)), + 0x06 => Ok(Number::Sym(SymbolicNumber::NegNan)), + 0x00 if value.len() >= (1 + 4 + (isize::BITS / 8)) as usize => { + let i: [u8; 4] = [0, 0, 0, 0]; + value[1..5].iter().zip(i.iter_mut()) + .for_each(|(a, b)| { *b = *a }); + let i = f32::from_be_bytes(i); + + let j: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; + value[6..14].iter().zip(j.iter_mut()) + .for_each(|(a, b)| { *b = *a }); + let j = isize::from_be_bytes(j); + Ok(Number::Sci(ScientificNotation(i, j))) + }, + 0x01 if + } + } +} + impl Add for Number { type Output = Number; fn add(self, rhs: Self) -> Self::Output {