Number-to-ByteVector-conversion #44

Merged
affine merged 7 commits from Number-to-ByteVector-conversion into main 2025-12-04 12:09:45 -08:00
2 changed files with 30 additions and 3 deletions
Showing only changes of commit 333beba0bb - Show all commits

View file

@ -406,12 +406,20 @@ impl VM {
n.0 > u8::MAX.into() || n.0 < 0.0 { n.0 > u8::MAX.into() || n.0 < 0.0 {
e!("input to NTOBV cannot cleanly convert"); 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 { } else {
e!("illegal argument to NTOBV instruction"); 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], { i::NTOC => access!(&instr.1[0], {
if let Datum::Number(snum) = **access!(&instr.1[0]) { if let Datum::Number(snum) = **access!(&instr.1[0]) {
let n = snum.make_inexact(); let n = snum.make_inexact();
@ -1140,6 +1148,10 @@ mod tests {
Instruction(i::NTOBV, vec![Operand(Address::Expr, 0)]), Instruction(i::NTOBV, vec![Operand(Address::Expr, 0)]),
Instruction(i::PUSH, 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 // create a char and push to stack
Instruction(i::NTOC, vec![Operand(Address::Expr, 0)]), Instruction(i::NTOC, vec![Operand(Address::Expr, 0)]),
Instruction(i::PUSH, 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()), (2, Datum::Number(Number::Flt(Float(97.0))).into()),
(3, Datum::Number(Number::Fra(Fraction(97, 1))).into()), (3, Datum::Number(Number::Fra(Fraction(97, 1))).into()),
(4, Datum::String(vec![b'a']).into()), (4, Datum::String(vec![b'a']).into()),
(5, Datum::Number(Number::Fra(Fraction(97, 1))).into()), (5, Datum::ByteVector(vec![0, 0, 0, 0, 0, 64, 88, 64]).into()),
(6, Datum::Char(b'a').into()), (6, Datum::Number(Number::Fra(Fraction(97, 1))).into()),
(7, Datum::Char(b'a').into()),
], ],
syms: vec![], syms: vec![],
errr: None, errr: None,

View file

@ -655,6 +655,20 @@ float datum casted to ByteVector datum.
Requires mutable access to input address. 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]] [[instructions]]
name = "ntoc" name = "ntoc"
args = ["src"] args = ["src"]