diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 24d8b05..532e118 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -406,12 +406,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(); @@ -1140,6 +1148,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)]), @@ -1158,8 +1170,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"]