From 080d630d2e920c4b1fafb32c918b05258aff8e54 Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Sat, 29 Nov 2025 20:28:34 -0700 Subject: [PATCH 1/7] Add instruction definition for number to bytevector conversion --- hyphae/vm.toml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hyphae/vm.toml b/hyphae/vm.toml index b445a25..c33aecf 100644 --- a/hyphae/vm.toml +++ b/hyphae/vm.toml @@ -641,6 +641,20 @@ character byte. Requires mutable access to input address. """ +[[instructions]] +name = "ntobv" +args = ["src"] +output = "" +description = """ +The ntobv instruction accepts a single number input. This operand is overwritten +by a new number datum that represents the inexact form of the source number. + +The inexact form is a normalization of fraction or scientific notation datum to +float datum casted to ByteVector datum. + +Requires mutable access to input address. +""" + [[instructions]] name = "ntoc" args = ["src"] From 32d4c8c43ea1d9bc5f28bb2857579c5ef9b685d5 Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Sat, 29 Nov 2025 23:24:06 -0700 Subject: [PATCH 2/7] Wrote number to bytevector conversion and test --- hyphae/src/vm.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 9372d16..9b2628e 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -391,6 +391,19 @@ impl VM { } }), + i::NTOBV => access!(&instr.1[0], { + if let Datum::Number(snum) = **access!(&instr.1[0]) { + let n = snum.make_inexact(); + if !snum.is_exact() || n.0.fract() != 0.0 || + n.0 > u8::MAX.into() || n.0 < 0.0 { + e!("input to NTOBV cannot cleanly convert"); + } + Datum::ByteVector(n.0.to_bits().to_be_bytes().to_vec()).into() + } else { + e!("illegal argument to NTOBV instruction"); + } + }), + i::NTOC => access!(&instr.1[0], { if let Datum::Number(snum) = **access!(&instr.1[0]) { let n = snum.make_inexact(); @@ -1094,7 +1107,7 @@ mod tests { Instruction(i::DUPL, vec![Operand(Address::Stack, 0), Operand(Address::Expr, 0)]), - // create a strunct and push to stack + // create a struct and push to stack Instruction(i::CTOS, vec![Operand(Address::Expr, 0)]), Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), @@ -1115,9 +1128,14 @@ mod tests { Instruction(i::NTOE, vec![Operand(Address::Expr, 0)]), Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), + // convert to ByteVector and push to stack + Instruction(i::NTOBV, vec![Operand(Address::Expr, 0)]), + Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), + // create a char and push to stack Instruction(i::NTOC, vec![Operand(Address::Expr, 0)]), Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), + ])).with_stack({ let mut i = StackStack::new(); i.push_current_stack(Datum::Char(b'a').into()); From c25a57a92f2f72c3eec96bd6be936819ee1034fc Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Sun, 30 Nov 2025 00:51:18 -0700 Subject: [PATCH 3/7] updated NTOBV and test --- hyphae/src/vm.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 9b2628e..e1d5756 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -398,7 +398,7 @@ impl VM { n.0 > u8::MAX.into() || n.0 < 0.0 { e!("input to NTOBV cannot cleanly convert"); } - Datum::ByteVector(n.0.to_bits().to_be_bytes().to_vec()).into() + Datum::ByteVector(n.0.to_bits().to_ne_bytes().to_vec()).into() } else { e!("illegal argument to NTOBV instruction"); } @@ -1103,6 +1103,7 @@ mod tests { #[test] fn isa_conversions() { let mut vm = VM::from(Program(vec![], vec![ + // load from stack into expr Instruction(i::DUPL, vec![Operand(Address::Stack, 0), Operand(Address::Expr, 0)]), @@ -1112,7 +1113,6 @@ mod tests { Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), // reset expr to original char - Instruction(i::DUPL, vec![Operand(Address::Stack, 1), Operand(Address::Expr, 0)]), @@ -1150,7 +1150,8 @@ mod tests { (2, Datum::Number(Number::Flt(Float(97.0))).into()), (3, Datum::Number(Number::Fra(Fraction(97, 1))).into()), (4, Datum::String(vec![b'a']).into()), - (5, Datum::Char(b'a').into()), + (5, Datum::Number(Number::Fra(Fraction(97, 1))).into()), + (6, Datum::Char(b'a').into()), ], syms: vec![], errr: None, From 37b21935ccaa317614e1a8b39411321204555582 Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Wed, 3 Dec 2025 20:26:52 -0700 Subject: [PATCH 4/7] WIP: Adding BVTON instruction to convert from ByteVector to Number --- hyphae/src/vm.rs | 19 ++++++++++++++++--- hyphae/vm.toml | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index e1d5756..8b23a34 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -398,12 +398,20 @@ impl VM { n.0 > u8::MAX.into() || n.0 < 0.0 { e!("input to NTOBV cannot cleanly convert"); } - Datum::ByteVector(n.0.to_bits().to_ne_bytes().to_vec()).into() + Datum::ByteVector(n.0.to_ne_bytes().to_vec()).into() } else { e!("illegal argument to NTOBV instruction"); } }), + i::BVTON => access!(&instr.1[0], { + if let Datum::ByteVector(sbv) = **access!(&instr.1[0]) { + Datum::Number(Number::Fra(Fraction(f64::from_ne_bytes(sbv) as isize, 1))).into() + } else { + e!("illegal argument to BVTON instruction"); + } + }), + i::NTOC => access!(&instr.1[0], { if let Datum::Number(snum) = **access!(&instr.1[0]) { let n = snum.make_inexact(); @@ -1132,6 +1140,10 @@ mod tests { Instruction(i::NTOBV, vec![Operand(Address::Expr, 0)]), Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), + // convert back to inexact and push to stack + Instruction(i::BVTON, vec![Operand(Address::Expr, 0)]), + Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), + // create a char and push to stack Instruction(i::NTOC, vec![Operand(Address::Expr, 0)]), Instruction(i::PUSH, vec![Operand(Address::Expr, 0)]), @@ -1150,8 +1162,9 @@ mod tests { (2, Datum::Number(Number::Flt(Float(97.0))).into()), (3, Datum::Number(Number::Fra(Fraction(97, 1))).into()), (4, Datum::String(vec![b'a']).into()), - (5, Datum::Number(Number::Fra(Fraction(97, 1))).into()), - (6, Datum::Char(b'a').into()), + (5, Datum::ByteVector(vec![0, 0, 0, 0, 0, 64, 88, 64]).into()), + (6, Datum::Number(Number::Fra(Fraction(97, 1))).into()), + (7, Datum::Char(b'a').into()), ], syms: vec![], errr: None, diff --git a/hyphae/vm.toml b/hyphae/vm.toml index c33aecf..9b2d5d5 100644 --- a/hyphae/vm.toml +++ b/hyphae/vm.toml @@ -655,6 +655,20 @@ float datum casted to ByteVector datum. Requires mutable access to input address. """ +[[instructions]] +name = "bvton" +args = ["src"] +output = "" +description = """ +The bvton instruction accepts a byte vector input. This operand is overwritten +by a new number datum that represents the inexact form of the source byte vector. + +The normalized fraction or scientific notation data represented in a +ByteVector datum is casted to an inexact floating point number. + +Requires mutable access to input address. +""" + [[instructions]] name = "ntoc" args = ["src"] From 8195d746143392c75f92cd9954fd89c19968b7f0 Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Thu, 4 Dec 2025 11:14:07 -0700 Subject: [PATCH 5/7] another attempt to make BVTON --- hyphae/src/vm.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 8b23a34..dcb7277 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -16,7 +16,7 @@ */ -use organelle::{Fraction, Number, Numeric}; +use organelle::{Float, Fraction, Number, Numeric}; use crate::hmap::QuickMap; use crate::stackstack::StackStack; @@ -406,7 +406,9 @@ impl VM { i::BVTON => access!(&instr.1[0], { if let Datum::ByteVector(sbv) = **access!(&instr.1[0]) { - Datum::Number(Number::Fra(Fraction(f64::from_ne_bytes(sbv) as isize, 1))).into() + let sf64 = f64::from_ne_bytes(sbv.try_into().unwrap()); + Ok::(Datum::Number(Number::Flt(Float(sf64)))); + //Datum::Number(Number::Fra(Fraction(sf64 as isize, 1))).into() } else { e!("illegal argument to BVTON instruction"); } From c46f9577dd351f16e27d6fd5e1c722fce5ed056a Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Thu, 4 Dec 2025 12:40:27 -0700 Subject: [PATCH 6/7] updated BVTON --- hyphae/src/vm.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index dcb7277..21f1336 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -405,10 +405,9 @@ impl VM { }), i::BVTON => access!(&instr.1[0], { - if let Datum::ByteVector(sbv) = **access!(&instr.1[0]) { - let sf64 = f64::from_ne_bytes(sbv.try_into().unwrap()); - Ok::(Datum::Number(Number::Flt(Float(sf64)))); - //Datum::Number(Number::Fra(Fraction(sf64 as isize, 1))).into() + if let Datum::ByteVector(sbv) = &**access!(&instr.1[0]) { + let sf64 = f64::from_ne_bytes(sbv.clone().try_into().unwrap()); + Datum::Number(Number::Flt(Float(sf64))).into() } else { e!("illegal argument to BVTON instruction"); } From d759937b116b593830395f74c0d3e7224521e7fa Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Thu, 4 Dec 2025 12:57:53 -0700 Subject: [PATCH 7/7] fixed BVTON test --- hyphae/src/vm.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 21f1336..77b85ad 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -1158,14 +1158,14 @@ mod tests { let case = TestResult{ expr: None, stack: vec![ - (0, Datum::Char(b'a').into()), - (1, Datum::Number(Number::Fra(Fraction(97, 1))).into()), - (2, Datum::Number(Number::Flt(Float(97.0))).into()), - (3, Datum::Number(Number::Fra(Fraction(97, 1))).into()), - (4, Datum::String(vec![b'a']).into()), - (5, Datum::ByteVector(vec![0, 0, 0, 0, 0, 64, 88, 64]).into()), - (6, Datum::Number(Number::Fra(Fraction(97, 1))).into()), - (7, Datum::Char(b'a').into()), + (0, Datum::Char(b'a').into()), // NTOC + (1, Datum::Number(Number::Flt(Float(97.0))).into()), // BVTON + (2, Datum::ByteVector(vec![0, 0, 0, 0, 0, 64, 88, 64]).into()), // NTOBV + (3, Datum::Number(Number::Fra(Fraction(97, 1))).into()), // NTOE + (4, Datum::Number(Number::Flt(Float(97.0))).into()), // NTOI + (5, Datum::Number(Number::Fra(Fraction(97, 1))).into()), // CTON + (6, Datum::String(vec![b'a']).into()), // CTOS + (7, Datum::Char(b'a').into()), // DUPL ], syms: vec![], errr: None,