Boolean Expression Evaluator? #582
-
Hi, this crate looks really good, especially the name. I was wondering from the creator's perspective what the best way to go about making a VSCode when clause-like boolean expression evaluator with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You might find the example pratt parser to be very close to what you want. Despite being extremely terse, it's a fully-functioning expression evaluator. Of course, the 'proper' thing to do is to parse it into an AST and then evaluate the AST, but if you're just writing something quick, there's not necessarily a need to do that. |
Beta Was this translation helpful? Give feedback.
Parsing arithmetic operations like
+
is not really any different to parsing boolean operators like==
,&&
, etc. Each operator has precedence (indicated by the number given for each operator: see the pratt module docs for more information) and an associativity (left or right). Along with that, you provide a function that tells chumsky how it can combine the operator and operands together (usually you'd use this function to construct an AST node representing that operation, but in the example we just evaluate the result immediately).You can read the guide to see how to use chumsky in a Rust project. The guide also covers key concepts in crate, and a little parser theory.
That said, your re…