Handle reverse ranges

This is really ugly and should be refactored.
This commit is contained in:
Arthur Targaryen 2021-10-09 01:18:17 +02:00
parent 8783cf0138
commit d3bc096d47
2 changed files with 39 additions and 11 deletions

View file

@ -1020,17 +1020,40 @@ impl Value {
match (self, rhs) { match (self, rhs) {
(lhs, Value::Range { val: rhs, .. }) => Ok(Value::Bool { (lhs, Value::Range { val: rhs, .. }) => Ok(Value::Bool {
val: lhs val: if rhs
.gte(Span::unknown(), &rhs.from) .incr
.gt(
Span::unknown(),
&Value::Int {
val: 0,
span: Span::unknown(),
},
)
.map_or(false, |v| v.is_true()) .map_or(false, |v| v.is_true())
&& match rhs.inclusion { {
RangeInclusion::Inclusive => lhs lhs.gte(Span::unknown(), &rhs.from)
.lte(Span::unknown(), &rhs.to) .map_or(false, |v| v.is_true())
.map_or(false, |v| v.is_true()), && match rhs.inclusion {
RangeInclusion::RightExclusive => lhs RangeInclusion::Inclusive => lhs
.lt(Span::unknown(), &rhs.to) .lte(Span::unknown(), &rhs.to)
.map_or(false, |v| v.is_true()), .map_or(false, |v| v.is_true()),
}, RangeInclusion::RightExclusive => lhs
.lt(Span::unknown(), &rhs.to)
.map_or(false, |v| v.is_true()),
}
} else {
lhs.lte(Span::unknown(), &rhs.from)
.map_or(false, |v| v.is_true())
&& match rhs.inclusion {
RangeInclusion::Inclusive => lhs
.gte(Span::unknown(), &rhs.to)
.map_or(false, |v| v.is_true()),
RangeInclusion::RightExclusive => lhs
.gt(Span::unknown(), &rhs.to)
.map_or(false, |v| v.is_true()),
}
},
span, span,
}), }),
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool { (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {

View file

@ -590,10 +590,15 @@ fn non_string_in_string() -> TestResult {
} }
#[test] #[test]
fn int_in_range() -> TestResult { fn int_in_inc_range() -> TestResult {
run_test(r#"1 in -4..9.42"#, "true") run_test(r#"1 in -4..9.42"#, "true")
} }
#[test]
fn int_in_dec_range() -> TestResult {
run_test(r#"1 in 9.42..-4"#, "true")
}
#[test] #[test]
fn int_in_exclusive_range() -> TestResult { fn int_in_exclusive_range() -> TestResult {
run_test(r#"3 in 0..<3"#, "false") run_test(r#"3 in 0..<3"#, "false")