Commits

Taku Miyakawa committed 9ed3a0f

"Assignment operators" section of the manual

Comments (0)

Files changed (1)

src/doc/language/syntax.rst

 
 Operator expressions
 ====================
-Operator expressions are syntax sugar for other expressions, such as :ref:`call expressions <language-syntax-call>`.
+Operator expressions are syntax sugar for other :ref:`call expressions <language-syntax-call>`.
 
 Here is the list of expressions.
 
       - ``-``, ``!``, ``-``
       - Unary
 
+Assignment operators
+--------------------
+:token:`Assignment operator expressions <assign_op>` generate
+:ref:`call expressions <language-syntax-call>`.
 
-Assignment expressions
+.. list-table::
+    :header-rows: 1
+    :widths: 10 10 35
+
+    * - Operator expression
+      - Generated expression
+      - Usual meaning
+    * - ``A = B``
+      - ``A.op_set(B)``
+      - Stores B in A, and returns A
+    * - ``A ||= B``
+      - ``A.op_logor_set { B }``
+      - Calculates the logical OR with short-circuit evaluation then stores the result in A
+    * - ``A &&= B``
+      - ``A.op_logand_set { B }``
+      - Calculates the logical AND with short-circuit evaluation then stores the result in A
+    * - ``A |= B``
+      - ``A.op_or_set(B)``
+      - Stores the bitwise OR in A
+    * - ``A &= B``
+      - ``A.op_xor_set(B)``
+      - Stores the bitwise XOR in A
+    * - ``A += B``
+      - ``A.op_add_set(B)``
+      - Stores the sum in A
+    * - ``A -= B``
+      - ``A.op_sub_set(B)``
+      - Stores the difference in A
+    * - ``A *= B``
+      - ``A.op_mul_set(B)``
+      - Stores the product in A
+    * - ``A /= B``
+      - ``A.op_div_set(B)``
+      - Stores the reult of division in A
+    * - ``A //= B``
+      - ``A.op_quo_set(B)``
+      - Stores the integral quotient in A
+    * - ``A %= B``
+      - ``A.op_rem_set(B)``
+      - Stores the positive remainder in A
+    * - ``A **= B``
+      - ``A.op_pow_set(B)``
+      - Stores the power in A
+
+Note that right-hand-side values of ``||=`` and ``&&=`` are generated as a body chunk of a function.
+Therefore, values of pseudo variables such as \\env, \\args and \\0
+differ from ones on the enclosing context.
+
+For example,
+``fun`` function of the next program intends to calculate logical OR of false and the argument,
+but it never works.
+
+::
+
+    :fun = {
+        :_bool = false
+        :_bool ||= \0
+        printline(_bool)
+    }
+    fun(true)  # => Wrong index 0 for 0..<0
+
+The reason is that ``:_bool ||= \0`` generates ``:_bool.op_logor_set { \0 }``
+and thus \\0 means the first argument of the function ``{ \0 }`` here,
+not of ``fun``.
+
+The program should be written as below.
+
+::
+
+    :fun = { (:_arg)
+        :_bool = false
+        :_bool ||= _arg
+        printline(_bool)
+    }
+    fun(true)  # => true
+
+The same caution applies to ``||`` and ``&&``,
+which are :ref:`logical operators <language-syntax-logical-operator>`.
+
+.. _language-syntax-logical-operator:
+
+Logical operators
+-----------------
+
+Relationship operators
 ----------------------
 
-Logical expressions
--------------------
+Range operators
+---------------
 
-Relationship expressions
+Bit operators
+-------------
+
+Additive operators
+------------------
+
+Multiplicative operators
 ------------------------
 
-Range expressions
------------------
-
-Bit expressions
+Power operators
 ---------------
 
-Additive expressions
---------------------
-
-Multiplicative expressions
---------------------------
-
-Power expressions
------------------
-
-Unary expressions
------------------
+Unary operators
+---------------
 
 .. _language-syntax-chunk:
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.