From ae524a07e2e3f26736a140cc5845b7ac843a7f1c Mon Sep 17 00:00:00 2001 From: Pere Diaz Bou Date: Thu, 4 Jul 2024 17:19:05 +0200 Subject: [PATCH] core: Insn::Real support Signed-off-by: Pere Diaz Bou --- core/translate.rs | 17 +++++++++++++---- core/vdbe.rs | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/core/translate.rs b/core/translate.rs index adc7c64bd..30dd60094 100644 --- a/core/translate.rs +++ b/core/translate.rs @@ -384,10 +384,19 @@ fn translate_expr( ast::Expr::Like { .. } => todo!(), ast::Expr::Literal(lit) => match lit { ast::Literal::Numeric(val) => { - program.emit_insn(Insn::Integer { - value: val.parse().unwrap(), - dest: target_register, - }); + let maybe_int = val.parse::(); + if maybe_int.is_ok() { + program.emit_insn(Insn::Integer { + value: maybe_int.unwrap(), + dest: target_register, + }); + } else { + // must be a float + program.emit_insn(Insn::Real { + value: val.parse().unwrap(), + dest: target_register, + }); + } target_register } ast::Literal::String(s) => { diff --git a/core/vdbe.rs b/core/vdbe.rs index bee20d31e..075c62197 100644 --- a/core/vdbe.rs +++ b/core/vdbe.rs @@ -83,6 +83,12 @@ pub enum Insn { dest: usize, }, + // Write a float value into a register + Real { + value: f64, + dest: usize, + }, + // Write a string value into a register. String8 { value: String, @@ -328,6 +334,10 @@ impl Program { state.registers[*dest] = OwnedValue::Integer(*value); state.pc += 1; } + Insn::Real { value, dest } => { + state.registers[*dest] = OwnedValue::Float(*value); + state.pc += 1; + } Insn::String8 { value, dest } => { state.registers[*dest] = OwnedValue::Text(Rc::new(value.into())); state.pc += 1; @@ -449,6 +459,7 @@ fn print_insn(addr: usize, insn: &Insn) { enum IntValue { Int(i64), Usize(usize), + Float(f64), } impl fmt::Display for IntValue { @@ -456,6 +467,7 @@ impl fmt::Display for IntValue { match self { IntValue::Int(i) => f.pad(i.to_string().as_str()), IntValue::Usize(i) => f.pad(i.to_string().as_str()), + IntValue::Float(float_val) => write!(f, "{}", float_val), } } } @@ -603,6 +615,15 @@ fn insn_to_str(addr: usize, insn: &Insn) -> String { IntValue::Usize(0), "".to_string(), ), + Insn::Real { value, dest } => ( + "Real", + IntValue::Usize(*dest), + IntValue::Float(*value), + IntValue::Usize(0), + "", + IntValue::Usize(0), + "".to_string(), + ), Insn::String8 { value, dest } => ( "String8", IntValue::Usize(*dest),