From 37b21935ccaa317614e1a8b39411321204555582 Mon Sep 17 00:00:00 2001 From: Kolby Heacock Date: Wed, 3 Dec 2025 20:26:52 -0700 Subject: [PATCH] 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"]