Merge pull request #67 from pereman2/fix-float-parse

This commit is contained in:
Pekka Enberg
2024-07-05 08:43:11 +03:00
committed by GitHub
2 changed files with 34 additions and 4 deletions

View File

@@ -370,10 +370,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::<i64>();
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) => {

View File

@@ -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),