From 32d4c8c43ea1d9bc5f28bb2857579c5ef9b685d5 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 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());