Maui
Maui copied to clipboard
[Proposal] MathExpressionConverter boolean support
Discussed in https://github.com/CommunityToolkit/Maui/discussions/2084
Originally posted by stephenquan August 1, 2024 This is a Math Expression Converters https://github.com/CommunityToolkit/Maui/issues/71 refactor enhancement to improve the parser by (1) using a Parsing expression grammar, and (2) to support boolean operations. The grammar is now:
Expr ::= Conditional
Conditional ::= LogicalOr ( '?' LogicalOr ':' LogicalOr ) ?
LogicalOr ::= LogicalAnd ( '||' LogicalAnd ) *
LogicalAnd ::= Equality ( '&&' Equality ) *
Equality ::= Compare ( ( '==' | '!=' ) Compare ) *
Compare ::= Sum ( ( '>=' | '>' | '<=' | '<' ) Sum ) *
Sum ::= Product ( ( '+' | '-' ) Product ) *
Product ::= Power ( ( '*' | '/' ) Power ) *
Power ::= Primary ( '^' Primary ) *
Primary ::= Number
| Function
| Constant
| '(' Expr ')'
| ( '-' | '!' ) Primary
Number ::= ( '-' ) ? ( ( [0-9]+ '.' [0-9]+ ) | [0-9]+ )
Function ::= [a-z]+ ( '(' ')' | '(' Expr ( ',' Expr ) * ')' )
Constant ::= [a-z]+
Where Conditional, LogicalOr, LogicalAnd, Equality, and Compare are new. true and false boolean constants are also new. Sum, Product, Power, Primary, Number, Function, and Constant are a refactor of what was already supported by the original parser.
The implementation can be found:
- in my fork: https://github.com/stephenquan/Maui
- preview change commits: https://github.com/CommunityToolkit/Maui/compare/main...stephenquan:Maui:main
References:
- https://en.wikipedia.org/wiki/Parsing_expression_grammar
- https://en.wikipedia.org/wiki/Order_of_operations#Programming_languages