Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/error/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ impl<NumericTypes: EvalexprNumericTypes> fmt::Display for EvalexprError<NumericT
"Expected a Value::Tuple of length {}, but got {:?}.",
expected_length, actual
),
ExpectedSameLengthTuples { expected, actual } => write!(
f,
"Expected a the same length for tuples {:?} and {:?}.",
expected, actual
),
ExpectedRangedLengthTuple {
expected_length,
actual,
Expand Down
16 changes: 16 additions & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ pub enum EvalexprError<NumericTypes: EvalexprNumericTypes = DefaultNumericTypes>
actual: Value<NumericTypes>,
},

/// A tuple value of a certain length was expected.
ExpectedSameLengthTuples {
/// The tuple with expected length.
expected: Value<NumericTypes>,
/// The actual tuple with unexpected length.
actual: Value<NumericTypes>,
},

/// A tuple value of a certain length range was expected.
ExpectedRangedLengthTuple {
/// The expected length range.
Expand Down Expand Up @@ -328,6 +336,14 @@ impl<NumericTypes: EvalexprNumericTypes> EvalexprError<NumericTypes> {
}
}

/// Constructs `EvalexprError::ExpectedSameLengthTuples {expected, actual}`.
pub fn expected_same_len_tuples(
expected: Value<NumericTypes>,
actual: Value<NumericTypes>,
) -> Self {
EvalexprError::ExpectedSameLengthTuples { expected, actual }
}

/// Constructs `EvalexprError::ExpectedFixedLenTuple{expected_len, actual}`.
pub fn expected_ranged_len_tuple(
expected_len: RangeInclusive<usize>,
Expand Down
31 changes: 31 additions & 0 deletions src/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,37 @@ impl<NumericTypes: EvalexprNumericTypes> Operator<NumericTypes> {
context: &C,
) -> EvalexprResultValue<NumericTypes> {
use crate::operator::Operator::*;

// Check if we can do piecewise operations on tuples
match self {
Add | Sub | Neg | Mul | Div | And | Or | Not => match arguments {
[Value::Tuple(a_elements), Value::Tuple(b_elements)] => {
if a_elements.len() != b_elements.len() {
return Err(EvalexprError::expected_same_len_tuples(
arguments[0].clone(),
arguments[1].clone(),
));
}

let mut computed = Vec::with_capacity(a_elements.len());
for (a, b) in a_elements.iter().zip(b_elements) {
computed.push(self.eval(&[a.clone(), b.clone()], context)?);
}

return Ok(Value::Tuple(computed));
},
[Value::Tuple(elements)] => {
let mut computed = Vec::with_capacity(elements.len());
for element in elements {
computed.push(self.eval(std::slice::from_ref(element), context)?);
}
return Ok(Value::Tuple(computed));
},
_ => {},
},
_ => {},
}

match self {
RootNode => {
if let Some(first) = arguments.first() {
Expand Down
40 changes: 40 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,46 @@ fn test_tuple_definitions() {
);
}

#[test]
fn test_tuple_operations() {
assert_eq!(
eval("!(false, true, true)"),
Ok(Value::Tuple(vec![
Value::Boolean(true),
Value::Boolean(false),
Value::Boolean(false),
]))
);
assert_eq!(
eval("(1, 2, 3) + (4, 5, 6)"),
Ok(Value::Tuple(vec![
Value::from_int(5),
Value::from_int(7),
Value::from_int(9),
]))
);
assert_eq!(
eval("(6, 5, 4) - (1, 2, 3)"),
Ok(Value::Tuple(vec![
Value::from_int(5),
Value::from_int(3),
Value::from_int(1),
]))
);

assert_eq!(
eval("(1, 2, 3) + (4, 5)"),
Err(EvalexprError::ExpectedSameLengthTuples {
expected: Value::Tuple(vec![
Value::from_int(1),
Value::from_int(2),
Value::from_int(3),
]),
actual: Value::Tuple(vec![Value::from_int(4), Value::from_int(5),])
})
);
}

#[test]
fn test_implicit_context() {
assert_eq!(
Expand Down