Add Remainder vdbe oppcode

This commit is contained in:
PThorpe92 2024-12-27 15:38:07 -05:00
parent 3bc554f27c
commit f08d62b446
No known key found for this signature in database
GPG key ID: 66DB3FBACBDD05CC
3 changed files with 119 additions and 0 deletions

View file

@ -693,6 +693,13 @@ pub fn translate_expr(
dest: target_register,
});
}
ast::Operator::Modulus => {
program.emit_insn(Insn::Remainder {
lhs: e1_reg,
rhs: e2_reg,
dest: target_register,
});
}
ast::Operator::BitwiseAnd => {
program.emit_insn(Insn::BitAnd {
lhs: e1_reg,

View file

@ -82,6 +82,15 @@ pub fn insn_to_str(
0,
format!("r[{}]=~r[{}]", dest, reg),
),
Insn::Remainder { lhs, rhs, dest } => (
"Modulus",
*lhs as i32,
*rhs as i32,
*dest as i32,
OwnedValue::build_text(Rc::new("".to_string())),
0,
format!("r[{}]=r[{}]%r[{}]", dest, lhs, rhs),
),
Insn::Null { dest, dest_end } => (
"Null",
0,

View file

@ -119,6 +119,12 @@ pub enum Insn {
reg: usize,
dest: usize,
},
// Divide lhs by rhs and place the remainder in dest register.
Remainder {
lhs: usize,
rhs: usize,
dest: usize,
},
// Jump to the instruction at address P1, P2, or P3 depending on whether in the most recent Compare instruction the P1 vector was less than, equal to, or greater than the P2 vector, respectively.
Jump {
target_pc_lt: BranchOffset,
@ -1224,6 +1230,103 @@ impl Program {
}
state.pc += 1;
}
Insn::Remainder { lhs, rhs, dest } => {
let lhs = *lhs;
let rhs = *rhs;
let dest = *dest;
state.registers[dest] = match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Null, _)
| (_, OwnedValue::Null)
| (_, OwnedValue::Integer(0))
| (_, OwnedValue::Float(0.0)) => OwnedValue::Null,
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
OwnedValue::Integer(lhs % rhs)
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
OwnedValue::Float(((*lhs as i64) % (*rhs as i64)) as f64)
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
OwnedValue::Float(((*lhs as i64) % rhs) as f64)
}
(OwnedValue::Integer(lhs), OwnedValue::Float(rhs)) => {
OwnedValue::Float((lhs % *rhs as i64) as f64)
}
(lhs, OwnedValue::Agg(agg_rhs)) => match lhs {
OwnedValue::Agg(agg_lhs) => {
let acc = agg_lhs.final_value();
let acc2 = agg_rhs.final_value();
match (acc, acc2) {
(_, OwnedValue::Integer(0))
| (_, OwnedValue::Float(0.0))
| (_, OwnedValue::Null)
| (OwnedValue::Null, _) => OwnedValue::Null,
(OwnedValue::Integer(l), OwnedValue::Integer(r)) => {
OwnedValue::Integer(l % r)
}
(OwnedValue::Float(lh_f), OwnedValue::Float(rh_f)) => {
OwnedValue::Float(((*lh_f as i64) % (*rh_f as i64)) as f64)
}
(OwnedValue::Integer(lh_i), OwnedValue::Float(rh_f)) => {
OwnedValue::Float((lh_i % (*rh_f as i64)) as f64)
}
_ => {
todo!("{:?} {:?}", acc, acc2);
}
}
}
OwnedValue::Integer(lh_i) => match agg_rhs.final_value() {
OwnedValue::Null => OwnedValue::Null,
OwnedValue::Float(rh_f) => {
OwnedValue::Float((lh_i % (*rh_f as i64)) as f64)
}
OwnedValue::Integer(rh_i) => OwnedValue::Integer(lh_i % rh_i),
_ => {
todo!("{:?}", agg_rhs);
}
},
OwnedValue::Float(lh_f) => match agg_rhs.final_value() {
OwnedValue::Null => OwnedValue::Null,
OwnedValue::Float(rh_f) => {
OwnedValue::Float(((*lh_f as i64) % (*rh_f as i64)) as f64)
}
OwnedValue::Integer(rh_i) => {
OwnedValue::Float(((*lh_f as i64) % rh_i) as f64)
}
_ => {
todo!("{:?}", agg_rhs);
}
},
_ => todo!("{:?}", rhs),
},
(OwnedValue::Agg(aggctx), rhs) => match rhs {
OwnedValue::Integer(rh_i) => match aggctx.final_value() {
OwnedValue::Null => OwnedValue::Null,
OwnedValue::Float(lh_f) => {
OwnedValue::Float(((*lh_f as i64) % rh_i) as f64)
}
OwnedValue::Integer(lh_i) => OwnedValue::Integer(lh_i % rh_i),
_ => {
todo!("{:?}", aggctx);
}
},
OwnedValue::Float(rh_f) => match aggctx.final_value() {
OwnedValue::Null => OwnedValue::Null,
OwnedValue::Float(lh_f) => {
OwnedValue::Float(((*lh_f as i64) % (*rh_f as i64)) as f64)
}
OwnedValue::Integer(lh_i) => {
OwnedValue::Float((lh_i % (*rh_f as i64)) as f64)
}
_ => {
todo!("{:?}", aggctx);
}
},
_ => todo!("{:?}", rhs),
},
_ => todo!("{:?} {:?}", state.registers[lhs], state.registers[rhs]),
};
state.pc += 1;
}
Insn::Null { dest, dest_end } => {
if let Some(dest_end) = dest_end {
for i in *dest..=*dest_end {