WIP: serialization/deserialization of datum in VM
Some checks failed
per-push tests / build (push) Failing after 3m26s
per-push tests / test-frontend (push) Has been skipped
per-push tests / timed-decomposer-parse (push) Has been skipped
per-push tests / test-utility (push) Has been skipped
per-push tests / test-backend (push) Has been skipped
Some checks failed
per-push tests / build (push) Failing after 3m26s
per-push tests / test-frontend (push) Has been skipped
per-push tests / timed-decomposer-parse (push) Has been skipped
per-push tests / test-utility (push) Has been skipped
per-push tests / test-backend (push) Has been skipped
Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
parent
0f85292e6f
commit
1c19bbec4e
2 changed files with 98 additions and 0 deletions
|
|
@ -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<Vec<u8>> for Number {
|
||||
fn into(self) -> Vec<u8> {
|
||||
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<Self, Self::Error> {
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue