diff --git a/js/test_cases/helpers_eq/main.dust b/js/test_cases/helpers_eq/main.dust index aad3f75..777fa89 100644 --- a/js/test_cases/helpers_eq/main.dust +++ b/js/test_cases/helpers_eq/main.dust @@ -27,3 +27,7 @@ Do objects with different paths referencing the same variable match?{~n} {#int renamed=some_obj} {@eq key=some_obj value=renamed}some_obj equals renamed{:else}some_obj does not equal renamed{/eq}{~n} {/int} + +Floating point equality{~n} +======================={~n} +{@eq key=int value=7.0}int is equal to 7.0{:else}int is not equal to 7.0{/eq} diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 015a070..d1ddf96 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -119,6 +119,7 @@ pub struct Partial<'a> { pub enum OwnedLiteral { LString(String), LPositiveInteger(u64), + LFloat(f64), } #[derive(Debug, PartialEq)] diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index e7d13aa..130f84a 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -137,6 +137,7 @@ impl Truthiness for OwnedLiteral { match self { OwnedLiteral::LString(text) => !text.is_empty(), OwnedLiteral::LPositiveInteger(_num) => true, + OwnedLiteral::LFloat(_num) => true, } } } @@ -146,6 +147,7 @@ impl Renderable for OwnedLiteral { match self { OwnedLiteral::LString(text) => Ok(text.clone()), OwnedLiteral::LPositiveInteger(num) => Ok(num.to_string()), + OwnedLiteral::LFloat(num) => Ok(num.to_string()), } } } @@ -180,10 +182,25 @@ impl CompareContextElement for OwnedLiteral { (OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => { self_text == &other_num.to_string() } + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LString(other_text)) => { + &self_num.to_string() == other_text + } + (OwnedLiteral::LString(self_text), OwnedLiteral::LFloat(other_num)) => { + self_text == &other_num.to_string() + } ( OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LPositiveInteger(other_num), ) => self_num == other_num, + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => { + self_num == other_num + } + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LPositiveInteger(other_num)) => { + *self_num == (*other_num as f64) + } + (OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LFloat(other_num)) => { + (*self_num as f64) == *other_num + } }, } } @@ -212,10 +229,25 @@ impl CompareContextElement for OwnedLiteral { (OwnedLiteral::LString(self_text), OwnedLiteral::LPositiveInteger(other_num)) => { self_text.partial_cmp(&other_num.to_string()) } + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LString(other_text)) => { + self_num.to_string().partial_cmp(other_text) + } + (OwnedLiteral::LString(self_text), OwnedLiteral::LFloat(other_num)) => { + self_text.partial_cmp(&other_num.to_string()) + } ( OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LPositiveInteger(other_num), ) => self_num.partial_cmp(other_num), + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LFloat(other_num)) => { + self_num.partial_cmp(other_num) + } + (OwnedLiteral::LPositiveInteger(self_num), OwnedLiteral::LFloat(other_num)) => { + (*self_num as f64).partial_cmp(other_num) + } + (OwnedLiteral::LFloat(self_num), OwnedLiteral::LPositiveInteger(other_num)) => { + self_num.partial_cmp(&(*other_num as f64)) + } }, } }