Add flattening of operator chains when building operator tree

Relates to #44
This commit is contained in:
Sebastian Schmidt 2019-04-13 20:04:36 +02:00
parent 531b7b72a0
commit d576cec9b9
4 changed files with 30 additions and 0 deletions

View File

@ -91,6 +91,15 @@ impl Operator {
}
}
/// Returns true if chains of this operator should be flattened into one operator with many arguments.
// Make this a const fn once #57563 is resolved
fn is_flatten_chains(&self) -> bool {
match self {
Operator::Chain => true,
_ => false,
}
}
/// True if this operator is a leaf, meaning it accepts no arguments.
// Make this a const fn once #57563 is resolved
pub(crate) fn is_leaf(&self) -> bool {

View File

@ -383,6 +383,9 @@ impl Node {
.last_mut()
.unwrap()
.insert_back_prioritized(node, false)
} else if self.children.last().unwrap().operator().id() == node.operator().id() && node.operator().is_flatten_chains() && !self.children.last().unwrap().has_enough_children() {
// The operators will be chained together, and the next value will be added to this nodes last child.
Ok(())
} else {
if node.operator().is_leaf() {
return Err(EvalexprError::AppendedToLeafNode);

View File

@ -194,6 +194,10 @@ impl From<Value> for EvalexprResult<Value> {
}
}
impl From<()> for Value {
fn from(_: ()) -> Self { Value::Empty }
}
#[cfg(test)]
mod tests {
use value::{TupleType, Value};

View File

@ -605,3 +605,17 @@ fn test_serde() {
assert_eq!(manual_tree.eval(), serde_tree.eval());
}
}
#[test]
fn test_tuple_definitions() {
assert_eq!(eval_empty("()"), Ok(()));
assert_eq!(eval_int("(3)"), Ok(3));
assert_eq!(eval_tuple("(3, 4)"), Ok(vec![Value::from(3), Value::from(4)]));
assert_eq!(eval_tuple("2, (5, 6)"), Ok(vec![Value::from(2), Value::from(vec![Value::from(5), Value::from(6)])]));
assert_eq!(eval_tuple("1, 2"), Ok(vec![Value::from(1), Value::from(2)]));
assert_eq!(eval_tuple("1, 2, 3, 4"), Ok(vec![Value::from(1), Value::from(2), Value::from(3), Value::from(4)]));
assert_eq!(eval_tuple("(1, 2, 3), 5, 6, (true, false, 0)"), Ok(vec![Value::from(vec![Value::from(1), Value::from(2), Value::from(3)]), Value::from(5), Value::from(6), Value::from(vec![Value::from(true), Value::from(false), Value::from(0)])]));
assert_eq!(eval_tuple("1, (2)"), Ok(vec![Value::from(1), Value::from(2)]));
assert_eq!(eval_tuple("1, ()"), Ok(vec![Value::from(1), Value::from(())]));
assert_eq!(eval_tuple("1, ((2))"), Ok(vec![Value::from(1), Value::from(2)]));
}