Wrote number to bytevector conversion and test

This commit is contained in:
Kolby Heacock 2025-11-29 23:24:06 -07:00
parent 080d630d2e
commit 32d4c8c43e

View file

@ -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], { 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();
@ -1094,7 +1107,7 @@ mod tests {
Instruction(i::DUPL, vec![Operand(Address::Stack, 0), Instruction(i::DUPL, vec![Operand(Address::Stack, 0),
Operand(Address::Expr, 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::CTOS, vec![Operand(Address::Expr, 0)]),
Instruction(i::PUSH, 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::NTOE, vec![Operand(Address::Expr, 0)]),
Instruction(i::PUSH, 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 // 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)]),
])).with_stack({ ])).with_stack({
let mut i = StackStack::new(); let mut i = StackStack::new();
i.push_current_stack(Datum::Char(b'a').into()); i.push_current_stack(Datum::Char(b'a').into());