From 54a0e32fe1897aa9f16e3be8eae46cb11e1f61cc Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Sat, 29 Nov 2025 23:24:06 -0700 Subject: [PATCH] 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 2ce21d8..3f8318d 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -399,6 +399,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(); @@ -1102,7 +1115,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)]), @@ -1123,9 +1136,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());