fastn_type/evalexpr/tree/
mod.rs

1use fastn_type::evalexpr::{
2    token::Token,
3    value::{TupleType, EMPTY_VALUE},
4    Context, ContextWithMutableVariables, EmptyType, FloatType, HashMapContext, IntType,
5};
6
7use fastn_type::evalexpr::{
8    error::{EvalexprError, EvalexprResult},
9    operator::*,
10    value::Value,
11};
12use std::mem;
13
14// Exclude display module from coverage, as it prints not well-defined prefix notation.
15mod display;
16mod iter;
17
18/// A node in the operator tree.
19/// The operator tree is created by the crate-level `build_operator_tree` method.
20/// It can be evaluated for a given context with the `Node::eval` method.
21///
22/// The advantage of constructing the operator tree separately from the actual evaluation is that it can be evaluated arbitrarily often with different contexts.
23///
24/// # Examples
25///
26/// ```rust
27/// use fastn_type::evalexpr::*;
28///
29/// let mut context = HashMapContext::new();
30/// context.set_value("alpha".into(), 2.into()).unwrap(); // Do proper error handling here
31/// let node = build_operator_tree("1 + alpha").unwrap(); // Do proper error handling here
32/// assert_eq!(node.eval_with_context(&context), Ok(Value::from(3)));
33/// ```
34///
35#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)]
36pub struct ExprNode {
37    operator: Operator,
38    children: Vec<ExprNode>,
39}
40
41impl ExprNode {
42    /// Return a node object
43    pub fn new(operator: Operator) -> Self {
44        Self {
45            children: Vec::new(),
46            operator,
47        }
48    }
49
50    /// Adds the children in node
51    pub fn add_children(self, children: Vec<ExprNode>) -> Self {
52        let mut new_children = self.children;
53        new_children.extend(children);
54        Self {
55            children: new_children,
56            operator: self.operator,
57        }
58    }
59
60    fn root_node() -> Self {
61        Self::new(Operator::RootNode)
62    }
63
64    /// Returns an iterator over all identifiers in this expression.
65    /// Each occurrence of an identifier is returned separately.
66    ///
67    /// # Examples
68    ///
69    /// ```rust
70    /// use fastn_type::evalexpr::*;
71    ///
72    /// let tree = build_operator_tree("a + b + c * f()").unwrap(); // Do proper error handling here
73    /// let mut iter = tree.iter_identifiers();
74    /// assert_eq!(iter.next(), Some("a"));
75    /// assert_eq!(iter.next(), Some("b"));
76    /// assert_eq!(iter.next(), Some("c"));
77    /// assert_eq!(iter.next(), Some("f"));
78    /// assert_eq!(iter.next(), None);
79    /// ```
80    pub fn iter_identifiers(&self) -> impl Iterator<Item = &str> {
81        self.iter().filter_map(|node| match node.operator() {
82            Operator::VariableIdentifierWrite { identifier }
83            | Operator::VariableIdentifierRead { identifier }
84            | Operator::FunctionIdentifier { identifier } => Some(identifier.as_str()),
85            _ => None,
86        })
87    }
88
89    /// Returns an iterator over all variable identifiers in this expression.
90    /// Each occurrence of a variable identifier is returned separately.
91    ///
92    /// # Examples
93    ///
94    /// ```rust
95    /// use fastn_type::evalexpr::*;
96    ///
97    /// let tree = build_operator_tree("a + f(b + c)").unwrap(); // Do proper error handling here
98    /// let mut iter = tree.iter_variable_identifiers();
99    /// assert_eq!(iter.next(), Some("a"));
100    /// assert_eq!(iter.next(), Some("b"));
101    /// assert_eq!(iter.next(), Some("c"));
102    /// assert_eq!(iter.next(), None);
103    /// ```
104    pub fn iter_variable_identifiers(&self) -> impl Iterator<Item = &str> {
105        self.iter().filter_map(|node| match node.operator() {
106            Operator::VariableIdentifierWrite { identifier }
107            | Operator::VariableIdentifierRead { identifier } => Some(identifier.as_str()),
108            _ => None,
109        })
110    }
111
112    /// Returns an iterator over all read variable identifiers in this expression.
113    /// Each occurrence of a variable identifier is returned separately.
114    ///
115    /// # Examples
116    ///
117    /// ```rust
118    /// use fastn_type::evalexpr::*;
119    ///
120    /// let tree = build_operator_tree("d = a + f(b + c)").unwrap(); // Do proper error handling here
121    /// let mut iter = tree.iter_read_variable_identifiers();
122    /// assert_eq!(iter.next(), Some("a"));
123    /// assert_eq!(iter.next(), Some("b"));
124    /// assert_eq!(iter.next(), Some("c"));
125    /// assert_eq!(iter.next(), None);
126    /// ```
127    pub fn iter_read_variable_identifiers(&self) -> impl Iterator<Item = &str> {
128        self.iter().filter_map(|node| match node.operator() {
129            Operator::VariableIdentifierRead { identifier } => Some(identifier.as_str()),
130            _ => None,
131        })
132    }
133
134    /// Returns an iterator over all write variable identifiers in this expression.
135    /// Each occurrence of a variable identifier is returned separately.
136    ///
137    /// # Examples
138    ///
139    /// ```rust
140    /// use fastn_type::evalexpr::*;
141    ///
142    /// let tree = build_operator_tree("d = a + f(b + c)").unwrap(); // Do proper error handling here
143    /// let mut iter = tree.iter_write_variable_identifiers();
144    /// assert_eq!(iter.next(), Some("d"));
145    /// assert_eq!(iter.next(), None);
146    /// ```
147    pub fn iter_write_variable_identifiers(&self) -> impl Iterator<Item = &str> {
148        self.iter().filter_map(|node| match node.operator() {
149            Operator::VariableIdentifierWrite { identifier } => Some(identifier.as_str()),
150            _ => None,
151        })
152    }
153
154    /// Returns an iterator over all function identifiers in this expression.
155    /// Each occurrence of a function identifier is returned separately.
156    ///
157    /// # Examples
158    ///
159    /// ```rust
160    /// use fastn_type::evalexpr::*;
161    ///
162    /// let tree = build_operator_tree("a + f(b + c)").unwrap(); // Do proper error handling here
163    /// let mut iter = tree.iter_function_identifiers();
164    /// assert_eq!(iter.next(), Some("f"));
165    /// assert_eq!(iter.next(), None);
166    /// ```
167    pub fn iter_function_identifiers(&self) -> impl Iterator<Item = &str> {
168        self.iter().filter_map(|node| match node.operator() {
169            Operator::FunctionIdentifier { identifier } => Some(identifier.as_str()),
170            _ => None,
171        })
172    }
173
174    /// Evaluates the operator tree rooted at this node with the given context.
175    ///
176    /// Fails, if one of the operators in the expression tree fails.
177    pub fn eval_with_context<C: Context>(&self, context: &C) -> EvalexprResult<Value> {
178        let mut arguments = Vec::new();
179        for child in self.children() {
180            arguments.push(child.eval_with_context(context)?);
181        }
182        self.operator().eval(&arguments, context)
183    }
184
185    /// Evaluates the operator tree rooted at this node with the given mutable context.
186    ///
187    /// Fails, if one of the operators in the expression tree fails.
188    pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
189        &self,
190        context: &mut C,
191    ) -> EvalexprResult<Value> {
192        let mut arguments = Vec::new();
193        for child in self.children() {
194            arguments.push(child.eval_with_context_mut(context)?);
195        }
196        self.operator().eval_mut(&arguments, context)
197    }
198
199    /// Evaluates the operator tree rooted at this node.
200    ///
201    /// Fails, if one of the operators in the expression tree fails.
202    pub fn eval(&self) -> EvalexprResult<Value> {
203        self.eval_with_context_mut(&mut HashMapContext::new())
204    }
205
206    /// Evaluates the operator tree rooted at this node into a string with an the given context.
207    ///
208    /// Fails, if one of the operators in the expression tree fails.
209    pub fn eval_string_with_context<C: Context>(&self, context: &C) -> EvalexprResult<String> {
210        match self.eval_with_context(context) {
211            Ok(Value::String(string)) => Ok(string),
212            Ok(value) => Err(EvalexprError::expected_string(value)),
213            Err(error) => Err(error),
214        }
215    }
216
217    /// Evaluates the operator tree rooted at this node into a float with an the given context.
218    ///
219    /// Fails, if one of the operators in the expression tree fails.
220    pub fn eval_float_with_context<C: Context>(&self, context: &C) -> EvalexprResult<FloatType> {
221        match self.eval_with_context(context) {
222            Ok(Value::Float(float)) => Ok(float),
223            Ok(value) => Err(EvalexprError::expected_float(value)),
224            Err(error) => Err(error),
225        }
226    }
227
228    /// Evaluates the operator tree rooted at this node into an integer with an the given context.
229    ///
230    /// Fails, if one of the operators in the expression tree fails.
231    pub fn eval_int_with_context<C: Context>(&self, context: &C) -> EvalexprResult<IntType> {
232        match self.eval_with_context(context) {
233            Ok(Value::Int(int)) => Ok(int),
234            Ok(value) => Err(EvalexprError::expected_int(value)),
235            Err(error) => Err(error),
236        }
237    }
238
239    /// Evaluates the operator tree rooted at this node into a float with an the given context.
240    /// If the result of the expression is an integer, it is silently converted into a float.
241    ///
242    /// Fails, if one of the operators in the expression tree fails.
243    pub fn eval_number_with_context<C: Context>(&self, context: &C) -> EvalexprResult<FloatType> {
244        match self.eval_with_context(context) {
245            Ok(Value::Int(int)) => Ok(int as FloatType),
246            Ok(Value::Float(float)) => Ok(float),
247            Ok(value) => Err(EvalexprError::expected_number(value)),
248            Err(error) => Err(error),
249        }
250    }
251
252    /// Evaluates the operator tree rooted at this node into a boolean with an the given context.
253    ///
254    /// Fails, if one of the operators in the expression tree fails.
255    pub fn eval_boolean_with_context<C: Context>(&self, context: &C) -> EvalexprResult<bool> {
256        match self.eval_with_context(context) {
257            Ok(Value::Boolean(boolean)) => Ok(boolean),
258            Ok(value) => Err(EvalexprError::expected_boolean(value)),
259            Err(error) => Err(error),
260        }
261    }
262
263    /// Evaluates the operator tree rooted at this node into a tuple with an the given context.
264    ///
265    /// Fails, if one of the operators in the expression tree fails.
266    pub fn eval_tuple_with_context<C: Context>(&self, context: &C) -> EvalexprResult<TupleType> {
267        match self.eval_with_context(context) {
268            Ok(Value::Tuple(tuple)) => Ok(tuple),
269            Ok(value) => Err(EvalexprError::expected_tuple(value)),
270            Err(error) => Err(error),
271        }
272    }
273
274    /// Evaluates the operator tree rooted at this node into an empty value with an the given context.
275    ///
276    /// Fails, if one of the operators in the expression tree fails.
277    pub fn eval_empty_with_context<C: Context>(&self, context: &C) -> EvalexprResult<EmptyType> {
278        match self.eval_with_context(context) {
279            Ok(Value::Empty) => Ok(EMPTY_VALUE),
280            Ok(value) => Err(EvalexprError::expected_empty(value)),
281            Err(error) => Err(error),
282        }
283    }
284
285    /// Evaluates the operator tree rooted at this node into a string with an the given mutable context.
286    ///
287    /// Fails, if one of the operators in the expression tree fails.
288    pub fn eval_string_with_context_mut<C: ContextWithMutableVariables>(
289        &self,
290        context: &mut C,
291    ) -> EvalexprResult<String> {
292        match self.eval_with_context_mut(context) {
293            Ok(Value::String(string)) => Ok(string),
294            Ok(value) => Err(EvalexprError::expected_string(value)),
295            Err(error) => Err(error),
296        }
297    }
298
299    /// Evaluates the operator tree rooted at this node into a float with an the given mutable context.
300    ///
301    /// Fails, if one of the operators in the expression tree fails.
302    pub fn eval_float_with_context_mut<C: ContextWithMutableVariables>(
303        &self,
304        context: &mut C,
305    ) -> EvalexprResult<FloatType> {
306        match self.eval_with_context_mut(context) {
307            Ok(Value::Float(float)) => Ok(float),
308            Ok(value) => Err(EvalexprError::expected_float(value)),
309            Err(error) => Err(error),
310        }
311    }
312
313    /// Evaluates the operator tree rooted at this node into an integer with an the given mutable context.
314    ///
315    /// Fails, if one of the operators in the expression tree fails.
316    pub fn eval_int_with_context_mut<C: ContextWithMutableVariables>(
317        &self,
318        context: &mut C,
319    ) -> EvalexprResult<IntType> {
320        match self.eval_with_context_mut(context) {
321            Ok(Value::Int(int)) => Ok(int),
322            Ok(value) => Err(EvalexprError::expected_int(value)),
323            Err(error) => Err(error),
324        }
325    }
326
327    /// Evaluates the operator tree rooted at this node into a float with an the given mutable context.
328    /// If the result of the expression is an integer, it is silently converted into a float.
329    ///
330    /// Fails, if one of the operators in the expression tree fails.
331    pub fn eval_number_with_context_mut<C: ContextWithMutableVariables>(
332        &self,
333        context: &mut C,
334    ) -> EvalexprResult<FloatType> {
335        match self.eval_with_context_mut(context) {
336            Ok(Value::Int(int)) => Ok(int as FloatType),
337            Ok(Value::Float(float)) => Ok(float),
338            Ok(value) => Err(EvalexprError::expected_number(value)),
339            Err(error) => Err(error),
340        }
341    }
342
343    /// Evaluates the operator tree rooted at this node into a boolean with an the given mutable context.
344    ///
345    /// Fails, if one of the operators in the expression tree fails.
346    pub fn eval_boolean_with_context_mut<C: ContextWithMutableVariables>(
347        &self,
348        context: &mut C,
349    ) -> EvalexprResult<bool> {
350        match self.eval_with_context_mut(context) {
351            Ok(Value::Boolean(boolean)) => Ok(boolean),
352            Ok(value) => Err(EvalexprError::expected_boolean(value)),
353            Err(error) => Err(error),
354        }
355    }
356
357    /// Evaluates the operator tree rooted at this node into a tuple with an the given mutable context.
358    ///
359    /// Fails, if one of the operators in the expression tree fails.
360    pub fn eval_tuple_with_context_mut<C: ContextWithMutableVariables>(
361        &self,
362        context: &mut C,
363    ) -> EvalexprResult<TupleType> {
364        match self.eval_with_context_mut(context) {
365            Ok(Value::Tuple(tuple)) => Ok(tuple),
366            Ok(value) => Err(EvalexprError::expected_tuple(value)),
367            Err(error) => Err(error),
368        }
369    }
370
371    /// Evaluates the operator tree rooted at this node into an empty value with an the given mutable context.
372    ///
373    /// Fails, if one of the operators in the expression tree fails.
374    pub fn eval_empty_with_context_mut<C: ContextWithMutableVariables>(
375        &self,
376        context: &mut C,
377    ) -> EvalexprResult<EmptyType> {
378        match self.eval_with_context_mut(context) {
379            Ok(Value::Empty) => Ok(EMPTY_VALUE),
380            Ok(value) => Err(EvalexprError::expected_empty(value)),
381            Err(error) => Err(error),
382        }
383    }
384
385    /// Evaluates the operator tree rooted at this node into a string.
386    ///
387    /// Fails, if one of the operators in the expression tree fails.
388    pub fn eval_string(&self) -> EvalexprResult<String> {
389        self.eval_string_with_context_mut(&mut HashMapContext::new())
390    }
391
392    /// Evaluates the operator tree rooted at this node into a float.
393    ///
394    /// Fails, if one of the operators in the expression tree fails.
395    pub fn eval_float(&self) -> EvalexprResult<FloatType> {
396        self.eval_float_with_context_mut(&mut HashMapContext::new())
397    }
398
399    /// Evaluates the operator tree rooted at this node into an integer.
400    ///
401    /// Fails, if one of the operators in the expression tree fails.
402    pub fn eval_int(&self) -> EvalexprResult<IntType> {
403        self.eval_int_with_context_mut(&mut HashMapContext::new())
404    }
405
406    /// Evaluates the operator tree rooted at this node into a float.
407    /// If the result of the expression is an integer, it is silently converted into a float.
408    ///
409    /// Fails, if one of the operators in the expression tree fails.
410    pub fn eval_number(&self) -> EvalexprResult<FloatType> {
411        self.eval_number_with_context_mut(&mut HashMapContext::new())
412    }
413
414    /// Evaluates the operator tree rooted at this node into a boolean.
415    ///
416    /// Fails, if one of the operators in the expression tree fails.
417    pub fn eval_boolean(&self) -> EvalexprResult<bool> {
418        self.eval_boolean_with_context_mut(&mut HashMapContext::new())
419    }
420
421    /// Evaluates the operator tree rooted at this node into a tuple.
422    ///
423    /// Fails, if one of the operators in the expression tree fails.
424    pub fn eval_tuple(&self) -> EvalexprResult<TupleType> {
425        self.eval_tuple_with_context_mut(&mut HashMapContext::new())
426    }
427
428    /// Evaluates the operator tree rooted at this node into an empty value.
429    ///
430    /// Fails, if one of the operators in the expression tree fails.
431    pub fn eval_empty(&self) -> EvalexprResult<EmptyType> {
432        self.eval_empty_with_context_mut(&mut HashMapContext::new())
433    }
434
435    /// Returns the children of this node as a slice.
436    pub fn children(&self) -> &[ExprNode] {
437        &self.children
438    }
439
440    /// Returns the children of this node as a mutable slice.
441    pub fn mut_children(&mut self) -> &mut [ExprNode] {
442        &mut self.children
443    }
444
445    /// Returns the operator associated with this node.
446    pub fn operator(&self) -> &Operator {
447        &self.operator
448    }
449
450    /// Returns a mutable reference to the vector containing the children of this node.
451    ///
452    /// WARNING: Writing to this might have unexpected results, as some operators require certain amounts and types of arguments.
453    pub fn children_mut(&mut self) -> &mut Vec<ExprNode> {
454        &mut self.children
455    }
456
457    /// Returns a mutable reference to the operator associated with this node.
458    ///
459    /// WARNING: Writing to this might have unexpected results, as some operators require different amounts and types of arguments.
460    pub fn operator_mut(&mut self) -> &mut Operator {
461        &mut self.operator
462    }
463
464    fn has_enough_children(&self) -> bool {
465        Some(self.children().len()) == self.operator().max_argument_amount()
466    }
467
468    fn has_too_many_children(&self) -> bool {
469        if let Some(max_argument_amount) = self.operator().max_argument_amount() {
470            self.children().len() > max_argument_amount
471        } else {
472            false
473        }
474    }
475
476    fn insert_back_prioritized(
477        &mut self,
478        node: ExprNode,
479        is_root_node: bool,
480    ) -> EvalexprResult<()> {
481        // println!("Inserting {:?} into {:?}", node.operator, self.operator());
482        if self.operator().precedence() < node.operator().precedence() || is_root_node
483            // Right-to-left chaining
484            || (self.operator().precedence() == node.operator().precedence() && !self.operator().is_left_to_right() && !node.operator().is_left_to_right())
485        {
486            if self.operator().is_leaf() {
487                Err(EvalexprError::AppendedToLeafNode)
488            } else if self.has_enough_children() {
489                // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child
490                let last_child_operator = self.children.last().unwrap().operator();
491
492                if last_child_operator.precedence()
493                    < node.operator().precedence()
494                    // Right-to-left chaining
495                    || (last_child_operator.precedence()
496                    == node.operator().precedence() && !last_child_operator.is_left_to_right() && !node.operator().is_left_to_right())
497                {
498                    // println!("Recursing into {:?}", self.children.last().unwrap().operator());
499                    // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child
500                    self.children
501                        .last_mut()
502                        .unwrap()
503                        .insert_back_prioritized(node, false)
504                } else {
505                    // println!("Rotating");
506                    if node.operator().is_leaf() {
507                        return Err(EvalexprError::AppendedToLeafNode);
508                    }
509
510                    // Unwrap cannot fail because is_leaf being false and has_enough_children being true implies that the operator wants and has at least one child
511                    let last_child = self.children.pop().unwrap();
512                    // Root nodes have at most one child
513                    // TODO I am not sure if this is the correct error
514                    if self.operator() == &Operator::RootNode && !self.children().is_empty() {
515                        return Err(EvalexprError::MissingOperatorOutsideOfBrace);
516                    }
517                    // Do not insert root nodes into root nodes.
518                    // TODO I am not sure if this is the correct error
519                    if self.operator() == &Operator::RootNode
520                        && node.operator() == &Operator::RootNode
521                    {
522                        return Err(EvalexprError::MissingOperatorOutsideOfBrace);
523                    }
524                    self.children.push(node);
525                    let node = self.children.last_mut().unwrap();
526
527                    // Root nodes have at most one child
528                    // TODO I am not sure if this is the correct error
529                    if node.operator() == &Operator::RootNode && !node.children().is_empty() {
530                        return Err(EvalexprError::MissingOperatorOutsideOfBrace);
531                    }
532                    // Do not insert root nodes into root nodes.
533                    // TODO I am not sure if this is the correct error
534                    if node.operator() == &Operator::RootNode
535                        && last_child.operator() == &Operator::RootNode
536                    {
537                        return Err(EvalexprError::MissingOperatorOutsideOfBrace);
538                    }
539                    node.children.push(last_child);
540                    Ok(())
541                }
542            } else {
543                // println!("Inserting as specified");
544                self.children.push(node);
545                Ok(())
546            }
547        } else {
548            Err(EvalexprError::PrecedenceViolation)
549        }
550    }
551}
552
553fn collapse_root_stack_to(
554    root_stack: &mut Vec<ExprNode>,
555    mut root: ExprNode,
556    collapse_goal: &ExprNode,
557) -> EvalexprResult<ExprNode> {
558    loop {
559        if let Some(mut potential_higher_root) = root_stack.pop() {
560            // TODO I'm not sure about this >, as I have no example for different sequence operators with the same precedence
561            if potential_higher_root.operator().precedence() > collapse_goal.operator().precedence()
562            {
563                potential_higher_root.children.push(root);
564                root = potential_higher_root;
565            } else {
566                root_stack.push(potential_higher_root);
567                break;
568            }
569        } else {
570            // This is the only way the topmost root node could have been removed
571            return Err(EvalexprError::UnmatchedRBrace);
572        }
573    }
574
575    Ok(root)
576}
577
578fn collapse_all_sequences(root_stack: &mut Vec<ExprNode>) -> EvalexprResult<()> {
579    // println!("Collapsing all sequences");
580    // println!("Initial root stack is: {:?}", root_stack);
581    let mut root = if let Some(root) = root_stack.pop() {
582        root
583    } else {
584        return Err(EvalexprError::UnmatchedRBrace);
585    };
586
587    loop {
588        // println!("Root is: {:?}", root);
589        if root.operator() == &Operator::RootNode {
590            // This should fire if parsing something like `4(5)`
591            if root.has_too_many_children() {
592                return Err(EvalexprError::MissingOperatorOutsideOfBrace);
593            }
594
595            root_stack.push(root);
596            break;
597        }
598
599        if let Some(mut potential_higher_root) = root_stack.pop() {
600            if root.operator().is_sequence() {
601                potential_higher_root.children.push(root);
602                root = potential_higher_root;
603            } else {
604                // This should fire if parsing something like `4(5)`
605                if root.has_too_many_children() {
606                    return Err(EvalexprError::MissingOperatorOutsideOfBrace);
607                }
608
609                root_stack.push(potential_higher_root);
610                root_stack.push(root);
611                break;
612            }
613        } else {
614            // This is the only way the topmost root node could have been removed
615            return Err(EvalexprError::UnmatchedRBrace);
616        }
617    }
618
619    // println!("Root stack after collapsing all sequences is: {:?}", root_stack);
620    Ok(())
621}
622
623pub(crate) fn tokens_to_operator_tree(tokens: Vec<Token>) -> EvalexprResult<ExprNode> {
624    let mut root_stack = vec![ExprNode::root_node()];
625    let mut last_token_is_rightsided_value = false;
626    let mut token_iter = tokens.iter().peekable();
627
628    while let Some(token) = token_iter.next().cloned() {
629        let next = token_iter.peek().cloned();
630
631        let node = match token.clone() {
632            Token::Plus => Some(ExprNode::new(Operator::Add)),
633            Token::Minus => {
634                if last_token_is_rightsided_value {
635                    Some(ExprNode::new(Operator::Sub))
636                } else {
637                    Some(ExprNode::new(Operator::Neg))
638                }
639            }
640            Token::Star => Some(ExprNode::new(Operator::Mul)),
641            Token::Slash => Some(ExprNode::new(Operator::Div)),
642            Token::Percent => Some(ExprNode::new(Operator::Mod)),
643            Token::Hat => Some(ExprNode::new(Operator::Exp)),
644
645            Token::Eq => Some(ExprNode::new(Operator::Eq)),
646            Token::Neq => Some(ExprNode::new(Operator::Neq)),
647            Token::Gt => Some(ExprNode::new(Operator::Gt)),
648            Token::Lt => Some(ExprNode::new(Operator::Lt)),
649            Token::Geq => Some(ExprNode::new(Operator::Geq)),
650            Token::Leq => Some(ExprNode::new(Operator::Leq)),
651            Token::And => Some(ExprNode::new(Operator::And)),
652            Token::Or => Some(ExprNode::new(Operator::Or)),
653            Token::Not => Some(ExprNode::new(Operator::Not)),
654
655            Token::LBrace => {
656                root_stack.push(ExprNode::root_node());
657                None
658            }
659            Token::RBrace => {
660                if root_stack.len() <= 1 {
661                    return Err(EvalexprError::UnmatchedRBrace);
662                } else {
663                    collapse_all_sequences(&mut root_stack)?;
664                    root_stack.pop()
665                }
666            }
667
668            Token::Assign => Some(ExprNode::new(Operator::Assign)),
669            Token::PlusAssign => Some(ExprNode::new(Operator::AddAssign)),
670            Token::MinusAssign => Some(ExprNode::new(Operator::SubAssign)),
671            Token::StarAssign => Some(ExprNode::new(Operator::MulAssign)),
672            Token::SlashAssign => Some(ExprNode::new(Operator::DivAssign)),
673            Token::PercentAssign => Some(ExprNode::new(Operator::ModAssign)),
674            Token::HatAssign => Some(ExprNode::new(Operator::ExpAssign)),
675            Token::AndAssign => Some(ExprNode::new(Operator::AndAssign)),
676            Token::OrAssign => Some(ExprNode::new(Operator::OrAssign)),
677
678            Token::Comma => Some(ExprNode::new(Operator::Tuple)),
679            Token::Semicolon => Some(ExprNode::new(Operator::Chain)),
680
681            Token::Identifier(identifier) => {
682                let mut result = Some(ExprNode::new(Operator::variable_identifier_read(
683                    identifier.clone(),
684                )));
685                if let Some(next) = next {
686                    if next.is_assignment() {
687                        result = Some(ExprNode::new(Operator::variable_identifier_write(
688                            identifier.clone(),
689                        )));
690                    } else if next.is_leftsided_value() {
691                        result = Some(ExprNode::new(Operator::function_identifier(identifier)));
692                    }
693                }
694                result
695            }
696            Token::Float(float) => Some(ExprNode::new(Operator::value(Value::Float(float)))),
697            Token::Int(int) => Some(ExprNode::new(Operator::value(Value::Int(int)))),
698            Token::Boolean(boolean) => {
699                Some(ExprNode::new(Operator::value(Value::Boolean(boolean))))
700            }
701            Token::String(string) => Some(ExprNode::new(Operator::value(Value::String(string)))),
702        };
703
704        if let Some(mut node) = node {
705            // Need to pop and then repush here, because Rust 1.33.0 cannot release the mutable borrow of root_stack before the end of this complete if-statement
706            if let Some(mut root) = root_stack.pop() {
707                if node.operator().is_sequence() {
708                    // println!("Found a sequence operator");
709                    // println!("Stack before sequence operation: {:?}, {:?}", root_stack, root);
710                    // If root.operator() and node.operator() are of the same variant, ...
711                    if mem::discriminant(root.operator()) == mem::discriminant(node.operator()) {
712                        // ... we create a new root node for the next expression in the sequence
713                        root.children.push(ExprNode::root_node());
714                        root_stack.push(root);
715                    } else if root.operator() == &Operator::RootNode {
716                        // If the current root is an actual root node, we start a new sequence
717                        node.children.push(root);
718                        node.children.push(ExprNode::root_node());
719                        root_stack.push(ExprNode::root_node());
720                        root_stack.push(node);
721                    } else {
722                        // Otherwise, we combine the sequences based on their precedences
723                        // TODO I'm not sure about this <, as I have no example for different sequence operators with the same precedence
724                        if root.operator().precedence() < node.operator().precedence() {
725                            // If the new sequence has a higher precedence, it is part of the last element of the current root sequence
726                            if let Some(last_root_child) = root.children.pop() {
727                                node.children.push(last_root_child);
728                                node.children.push(ExprNode::root_node());
729                                root_stack.push(root);
730                                root_stack.push(node);
731                            } else {
732                                // Once a sequence has been pushed on top of the stack, it also gets a child
733                                unreachable!()
734                            }
735                        } else {
736                            // If the new sequence doesn't have a higher precedence, then all sequences with a higher precedence are collapsed below this one
737                            root = collapse_root_stack_to(&mut root_stack, root, &node)?;
738                            node.children.push(root);
739                            root_stack.push(node);
740                        }
741                    }
742                // println!("Stack after sequence operation: {:?}", root_stack);
743                } else if root.operator().is_sequence() {
744                    if let Some(mut last_root_child) = root.children.pop() {
745                        last_root_child.insert_back_prioritized(node, true)?;
746                        root.children.push(last_root_child);
747                        root_stack.push(root);
748                    } else {
749                        // Once a sequence has been pushed on top of the stack, it also gets a child
750                        unreachable!()
751                    }
752                } else {
753                    root.insert_back_prioritized(node, true)?;
754                    root_stack.push(root);
755                }
756            } else {
757                return Err(EvalexprError::UnmatchedRBrace);
758            }
759        }
760
761        last_token_is_rightsided_value = token.is_rightsided_value();
762    }
763
764    // In the end, all sequences are implicitly terminated
765    collapse_all_sequences(&mut root_stack)?;
766
767    if root_stack.len() > 1 {
768        Err(EvalexprError::UnmatchedLBrace)
769    } else if let Some(root) = root_stack.pop() {
770        Ok(root)
771    } else {
772        Err(EvalexprError::UnmatchedRBrace)
773    }
774}