@@ -650,3 +650,59 @@ int = zero / ( digit1-9 *digit )
650
650
minus = %x 2D ; -
651
651
plus = %x 2B ; +
652
652
zero = %x 30 ; 0
653
+
654
+ expression =/ arithmetic-expression ; ; ## Arithmetic Expressions
655
+
656
+ arithmetic-expression =/ " +" expression ; + %x43
657
+ arithmetic-expression =/ ( " -" / " –" ) expression ; - %x45 – %x2212
658
+
659
+ arithmetic-expression = expression " %" expression ; % %x37
660
+ arithmetic-expression =/ expression ( " *" / " ×" ) expression ; * %x42 × %xD7
661
+ arithmetic-expression =/ expression " +" expression ; + %x43
662
+ arithmetic-expression =/ expression ( " -" / " –" ) expression ; - %x45 – %x2212
663
+ arithmetic-expression =/ expression ( " /" / " ÷" ) expression ; / %x47 ÷
664
+
665
+ ; ; An `arithmetic-expression` enables simple computations using the four basic operations,
666
+ ; ; as well as the modulo and integer-division operations.
667
+ ; ;
668
+ ; ; To support arithmetic operations, the following operators are available:
669
+ ; ;
670
+ ; ; - `+` addition operator
671
+ ; ; - `-` subtraction operator
672
+ ; ; - `*` multiplication operator
673
+ ; ; - `/` division operator
674
+ ; ; - `%` modulo operator
675
+ ; ; - `//` integer division operator
676
+ ; ;
677
+ ; ; Proper mathematical operators are also supported using the following UNICODE characters:
678
+ ; ;
679
+ ; ; - `–` (U+2212 MINUS SIGN)
680
+ ; ; - `÷` (U+00F7 DIVISION SIGN)
681
+ ; ; - `×` (U+00D7 MULTIPLY SIGN)
682
+ ; ;
683
+ ; ; Arithmetic operations adhere to the usual precedence rules, from lowest to highest:
684
+ ; ;
685
+ ; ; - `-` subtraction operator and `+` addition operator
686
+ ; ; - `/` division, `*` multiplication, `%` modulo and `//` integer division operators
687
+ ; ;
688
+ ; ; In the absence of parentheses, operators of the same level of precedence are evaluated from left to right.
689
+ ; ; Arithmetic operators have higher precedence than comparison operators and lower precedence than the `.` "dot" `sub-expression` token separator.
690
+ ; ;
691
+ ; ; ## Examples
692
+ ; ;
693
+ ; ; ```
694
+ ; ; search(a + b, {"a": 1, "b": 2}) -> 3
695
+ ; ; search(a - b, {"a": 1, "b": 2}) -> -1
696
+ ; ; search(a * b, {"a": 2, "b": 4}) -> 8
697
+ ; ; search(a / b, {"a": 2, "b": 3}) -> 0.666666666666667
698
+ ; ; search(a % b, {"a": 10, "b": 3} -> 1
699
+ ; ; search(a // b, {"a": 10, "b": 3} -> -3
700
+ ; ; search(a.b + cd, {"a": {"b": 1}, "c": {"d": 2}}) -> 3
701
+ ; ; ```
702
+ ; ;
703
+ ; ; Since `arithmetic-expression` is not valid on the right-hand-side of a `sub-expression`, a `pipe-expression` can be used instead:
704
+ ; ;
705
+ ; ; ```
706
+ ; ; search({ab: a.b, cd: c.d} | ab + cd, {"a": {"b": 1}, "c": {"d": 2}}) -> 3
707
+ ; ; ```
708
+ ; ;
0 commit comments