From 136eb779adf298eb9e636546dcbcb00cc4b24f1f Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 9 Feb 2024 17:23:27 +0100 Subject: [PATCH] Full simplify before function calls --- numbat/src/bytecode_interpreter.rs | 2 +- numbat/src/quantity.rs | 2 +- numbat/tests/interpreter.rs | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/numbat/src/bytecode_interpreter.rs b/numbat/src/bytecode_interpreter.rs index a5d51271..7bbd7b05 100644 --- a/numbat/src/bytecode_interpreter.rs +++ b/numbat/src/bytecode_interpreter.rs @@ -142,7 +142,7 @@ impl BytecodeInterpreter { Expression::FunctionCall(_span, _full_span, name, args, _type) => { // Put all arguments on top of the stack for arg in args { - self.compile_expression(arg)?; + self.compile_expression_with_simplify(arg)?; } if let Some(idx) = self.vm.get_ffi_callable_idx(name) { diff --git a/numbat/src/quantity.rs b/numbat/src/quantity.rs index d4bbf7db..9d298bf2 100644 --- a/numbat/src/quantity.rs +++ b/numbat/src/quantity.rs @@ -59,7 +59,7 @@ impl Quantity { } pub fn convert_to(&self, target_unit: &Unit) -> Result { - if &self.unit == target_unit || self.is_zero() { + if &self.unit == target_unit { Ok(Quantity::new(self.value, target_unit.clone())) } else { // Remove common unit factors to reduce unnecessary conversion procedures diff --git a/numbat/tests/interpreter.rs b/numbat/tests/interpreter.rs index 4507e53f..a52103ce 100644 --- a/numbat/tests/interpreter.rs +++ b/numbat/tests/interpreter.rs @@ -595,3 +595,8 @@ fn test_overwrite_captured_constant() { fn test_pretty_print_prefixes() { expect_output("1 megabarn", "1 megabarn"); } + +#[test] +fn test_full_simplify_for_function_calls() { + expect_output("floor(1.2 hours / hour)", "1"); +}