ft(interpreter): generalize binary operations

This commit is contained in:
2025-06-10 10:59:35 +09:00
parent 2b37884a60
commit 35fefb7625
3 changed files with 110 additions and 27 deletions

View File

@@ -7,7 +7,7 @@ use crate::register::SegmentRegister;
use crate::{disasm::DisasmError, register::Register};
use core::fmt;
use std::ops::{Add, Sub};
use std::ops::{Add, Div, Mul, Sub};
pub type Byte = u8; // b
pub type IByte = i8; // used for displacements of memory access
@@ -88,6 +88,44 @@ impl Sub for ImmediateOperand {
}
}
impl Mul for ImmediateOperand {
type Output = Self;
fn mul(self, other: Self) -> Self {
match self {
ImmediateOperand::Byte(lhsb) => match other {
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_mul(rhsb)),
_ => panic!("Cannot multiply Byte with Word"),
},
ImmediateOperand::Word(lhsw) => match other {
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_mul(rhsw)),
ImmediateOperand::Byte(rhsb) => {
ImmediateOperand::Word(lhsw.wrapping_mul(rhsb as Word))
}
},
}
}
}
impl Div for ImmediateOperand {
type Output = Self;
fn div(self, other: Self) -> Self {
match self {
ImmediateOperand::Byte(lhsb) => match other {
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_div(rhsb)),
_ => panic!("Cannot divide Byte with Word"),
},
ImmediateOperand::Word(lhsw) => match other {
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_div(rhsw)),
ImmediateOperand::Byte(rhsb) => {
ImmediateOperand::Word(lhsw.wrapping_div(rhsb as Word))
}
},
}
}
}
impl fmt::Display for ImmediateOperand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {