Number-to-ByteVector-conversion #44
1 changed files with 19 additions and 1 deletions
|
|
@ -399,6 +399,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();
|
||||
|
|
@ -1102,7 +1115,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)]),
|
||||
|
||||
|
|
@ -1123,9 +1136,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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue