diff --git a/doc/userdoc.org b/doc/userdoc.org index c07aa8249c14826176999316224a461d68ab1119..7ba7b1d3a166a818b2fb45917bc211f6e77e858b 100644 --- a/doc/userdoc.org +++ b/doc/userdoc.org @@ -1540,7 +1540,72 @@ they follow a few rules. \end{equation*} #+end_src -*** TODO High-level types<<high-level-types>> +**** Operator precedence and associativity + +To avoid confusions, the operators precedence in ~pugs~ language follow +the same rules as in ~C++~. + +This is summarized in the following table, where ~a~ and ~b~ denotes two +expressions. +| Precedence | Operator | +|------------+----------| +| 1 | ~a++~ | +| | ~a--~ | +| | ~a[]~ | +|------------+----------| +| 2 | ~++a~ | +| | ~--a~ | +| | ~not a~ | +|------------+----------| +| 3 | ~a*b~ | +| | ~a/b~ | +|------------+----------| +| 4 | ~a+b~ | +| | ~a-b~ | +|------------+----------| +| 5 | ~a<<b~ | +| | ~a>>b~ | +|------------+----------| +| 6 | ~a<b~ | +| | ~a<=b~ | +| | ~a>b~ | +| | ~a>=b~ | +|------------+----------| +| 7 | ~a==b~ | +| | ~a!=b~ | +|------------+----------| +| 8 | ~a=b~ | +| | ~a+=b~ | +| | ~a-=b~ | +| | ~a*=b~ | +| | ~a/=b~ | + +As already said, we forbid some constructions to try to avoid spurious +constructions. By construction associativity of many operators makes +no sense in ~pugs~ language (affectations and increment/decrement +operators for instance). + +For all other operators, the associativity rule is *left to right*. Thus +the following code +#+BEGIN_SRC pugs :exports source + - 1 + 3 - 4 + 2; +#+END_SRC +is equivalent to +#+BEGIN_SRC pugs :exports source + (((- 1) + 3) - 4) + 2; +#+END_SRC + +Obviously ~pugs~ allows the use of parenthesis to write +expressions. It is enough to give a simple example. +#+NAME: parenthesis-precedence +#+BEGIN_SRC pugs :exports both :results output + cout << " 2 + 3 * 4 = " << 2 + 3 * 4 << "\n"; + cout << "(2 + 3) * 4 = " << (2 + 3) * 4 << "\n"; +#+END_SRC +the output is +#+RESULTS: parenthesis-precedence + +*** High-level types<<high-level-types>> *** TODO Lists