Skip to content
Snippets Groups Projects
Commit 95fa0f99 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add operator precedence table

parent a001b533
No related branches found
No related tags found
1 merge request!145git subrepo clone git@gitlab.com:OlMon/org-themes.git packages/org-themes
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment