🐛 bug: impl OP_ADDI

main
_ 2023-09-30 09:57:51 -05:00
parent 81743108c3
commit 1943f0a40b
3 changed files with 22 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#[derive (Debug, PartialEq)]
pub enum Instruction {
Add (u8, u8, u8),
AddI (u8, u8, i8),
Call (u8, u8, u8),
Closure (u8, u32),

View File

@ -71,6 +71,11 @@ fn i_sb (buf: [u8; 4]) -> Option <i8> {
i8::try_from (i32::try_from (b).ok ()? - 127).ok ()
}
fn i_sc (buf: [u8; 4]) -> Option <i8> {
let c = buf [3];
i8::try_from (i32::try_from (c).ok ()? - 127).ok ()
}
pub fn parse_inst (buf: [u8; 4]) -> Option <Inst>
{
let opcode = buf [0] & 0x7f;
@ -79,6 +84,7 @@ pub fn parse_inst (buf: [u8; 4]) -> Option <Inst>
let b = buf [2];
let ax = a as u32 + ((b as u32) << 8);
let c = buf [3];
let s_c = c - 0x80;
let bx =
(((buf [1] >> 7) as u32) << 0) |
((buf [2] as u32) << 1) |
@ -104,6 +110,7 @@ pub fn parse_inst (buf: [u8; 4]) -> Option <Inst>
0x11 => Inst::SetI (a, b, c, k),
0x12 => Inst::SetField (a, b, c, k),
0x13 => Inst::NewTable (a),
0x15 => Inst::AddI (a, b, i_sc (buf)?),
0x18 => Inst::MulK (a, b, c),
0x19 => Inst::ModK (a, b, c),
0x22 => Inst::Add (a, b, c),

View File

@ -178,6 +178,20 @@ impl <'a> State <'a> {
*self.reg_mut (*a) = x;
},
Instruction::AddI (a, b, s_c) => {
let v_b = self.reg (*b);
let x = if let Some (v_b) = v_b.as_int ()
{
Value::from (v_b + *s_c as i64)
}
else {
let v_b = v_b.as_float ().unwrap_or_else (|| panic! ("{v_b}"));
Value::from (v_b + f64::from (*s_c))
};
*self.reg_mut (*a) = x;
},
Instruction::Call (a, b, _c) => {
let b = usize::from (*b);