com php-langspec: Fix precedence of coalesce operator: spec/00-specification-for-php.md spec/10-expressions.md spec/19-grammar.md
| From: | Nikita Popov | Date: | Tue, 19 Sep 2017 03:46:21 +0000 |
| Subject: | com php-langspec: Fix precedence of coalesce operator: spec/00-specification-for-php.md spec/10-expressions.md spec/19-grammar.md | ||
| Groups: | php.standards.cvs | ||
| Request: | Send a blank email to standards--cvs+get-386@lists.php.net to get a copy of this message | ||
Commit: 8082f0911ad18da427d2920599d06a1a0632edbc
Author: Nikita Popov <nikic@php.net> Tue, 19 Sep 2017 05:46:21 +0200
Parents: 3c6875b738786cea3ac87e90d7f781e13d66aa8f
Branches: master
Link: http://git.php.net/?p=php-langspec.git;a=commitdiff;h=8082f0911ad18da427d2920599d06a1a0632edbc
Log:
Fix precedence of coalesce operator
Fix #201.
Bugs:
https://bugs.php.net/201
Changed paths:
M spec/00-specification-for-php.md
M spec/10-expressions.md
M spec/19-grammar.md
diff --git a/spec/00-specification-for-php.md b/spec/00-specification-for-php.md index d2d2f4c..fb7fe9b 100644 --- a/spec/00-specification-for-php.md +++ b/spec/00-specification-for-php.md @@ -144,8 +144,8 @@ is distributed without any warranty. - [Bitwise Inclusive OR Operator](10-expressions.md#bitwise-inclusive-or-operator) - [Logical AND Operator (form 1)](10-expressions.md#logical-and-operator-form-1) - [Logical Inclusive OR Operator (form 1)](10-expressions.md#logical-inclusive-or-operator-form-1) - - [Conditional Operator](10-expressions.md#conditional-operator) - [Coalesce Operator](10-expressions.md#coalesce-operator) + - [Conditional Operator](10-expressions.md#conditional-operator) - [Assignment Operators](10-expressions.md#assignment-operators) - [General](10-expressions.md#general-5) - [Simple Assignment](10-expressions.md#simple-assignment) diff --git a/spec/10-expressions.md b/spec/10-expressions.md index 9bf100f..e99a4a3 100644 --- a/spec/10-expressions.md +++ b/spec/10-expressions.md @@ -2911,65 +2911,20 @@ This operator associates left-to-right. if ($month < 1 || $month > 12) ... ``` -## Conditional Operator - -**Syntax** - -<!-- GRAMMAR -conditional-expression: - logical-inc-OR-expression-1 - logical-inc-OR-expression-1 '?' expression? ':' conditional-expression ---> - -<pre> -<i id="grammar-conditional-expression">conditional-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-conditional-expression">conditional-expression</a></i> -</pre> - -**Semantics** -Given the expression `e1 ? e2 : e3`, `e1` is evaluated first and [converted to `bool`](08-conversions.md#converting-to-boolean-type) if it has another type. -If the result is `TRUE`, then and only then is `e2` evaluated, and the result and its type become the result and type of -the whole expression. Otherwise, then and only then is `e3` evaluated, and -the result and its type become the result and type of the whole -expression. There is a sequence point after the evaluation of `e1`. If `e2` -is omitted, the result and type of the whole expression is the value and -type of `e1` (before the conversion to `bool`). - -This operator associates left-to-right. - -**Examples** - -```PHP -for ($i = -5; $i <= 5; ++$i) - echo "$i is ".(($i & 1 == TRUE) ? "odd\n" : "even\n"); -// ----------------------------------------- -$a = 10 ? : "Hello"; // result is int with value 10 -$a = 0 ? : "Hello"; // result is string with value "Hello" -$i = PHP_INT_MAX; -$a = $i++ ? : "red"; // result is int with value 2147483647 (on a 32-bit - // system) even though $i is now the float 2147483648.0 -// ----------------------------------------- -$i++ ? f($i) : f(++$i); // the sequence point makes this well-defined -// ----------------------------------------- -function factorial($int) -{ - return ($int > 1) ? $int * factorial($int - 1) : $int; -} -``` - ## Coalesce Operator **Syntax** <!-- GRAMMAR coalesce-expression: - logical-inc-OR-expression-1 '??' expression + logical-inc-OR-expression-1 + logical-inc-OR-expression-1 '??' coalesce-expression --> <pre> <i id="grammar-coalesce-expression">coalesce-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-expression">expression</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> </pre> **Semantics** @@ -2979,8 +2934,8 @@ Given the expression `e1 ?? e2`, if `e1` is set and not `NULL` (i.e. TRUE for evaluated, and the result becomes the result of the whole expression. There is a sequence point after the evaluation of `e1`. -Note that the semantics of `??` is similar to `isset` so that uninitialized variables will not produce -warnings when used in `e1`. +Note that the semantics of `??` is similar to `isset` so that uninitialized variables will +not produce warnings when used in `e1`. This operator associates right-to-left. @@ -3006,6 +2961,54 @@ function foo() { var_dump(true ?? foo()); // outputs bool(true), "executed!" does not appear as it short-circuits ``` +## Conditional Operator + +**Syntax** + +<!-- GRAMMAR +conditional-expression: + coalesce-expression + conditional-expression '?' expression? ':' coalesce-expression +--> + +<pre> +<i id="grammar-conditional-expression">conditional-expression:</i> + <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> + <i><a href="#grammar-conditional-expression">conditional-expression</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> +</pre> + +**Semantics** +Given the expression `e1 ? e2 : e3`, `e1` is evaluated first and [converted to `bool`](08-conversions.md#converting-to-boolean-type) if it has another type. +If the result is `TRUE`, then and only then is `e2` evaluated, and the result and its type become the result and type of +the whole expression. Otherwise, then and only then is `e3` evaluated, and +the result and its type become the result and type of the whole +expression. There is a sequence point after the evaluation of `e1`. If `e2` +is omitted, the result and type of the whole expression is the value and +type of `e1` (before the conversion to `bool`). + +This operator associates left-to-right. + +**Examples** + +```PHP +for ($i = -5; $i <= 5; ++$i) + echo "$i is ".(($i & 1 == TRUE) ? "odd\n" : "even\n"); +// ----------------------------------------- +$a = 10 ? : "Hello"; // result is int with value 10 +$a = 0 ? : "Hello"; // result is string with value "Hello" +$i = PHP_INT_MAX; +$a = $i++ ? : "red"; // result is int with value 2147483647 (on a 32-bit + // system) even though $i is now the float 2147483648.0 +// ----------------------------------------- +$i++ ? f($i) : f(++$i); // the sequence point makes this well-defined +// ----------------------------------------- +function factorial($int) +{ + return ($int > 1) ? $int * factorial($int - 1) : $int; +} +``` + + ## Assignment Operators ### General @@ -3015,7 +3018,6 @@ var_dump(true ?? foo()); // outputs bool(true), "executed!" does not appear as i <!-- GRAMMAR assignment-expression: conditional-expression - coalesce-expression simple-assignment-expression byref-assignment-expression compound-assignment-expression @@ -3024,7 +3026,6 @@ assignment-expression: <pre> <i id="grammar-assignment-expression">assignment-expression:</i> <i><a href="#grammar-conditional-expression">conditional-expression</a></i> - <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i><a href="#grammar-simple-assignment-expression">simple-assignment-expression</a></i> <i><a href="#grammar-byref-assignment-expression">byref-assignment-expression</a></i> <i><a href="#grammar-compound-assignment-expression">compound-assignment-expression</a></i> diff --git a/spec/19-grammar.md b/spec/19-grammar.md index d3e82ba..6f17c92 100644 --- a/spec/19-grammar.md +++ b/spec/19-grammar.md @@ -654,16 +654,16 @@ The grammar notation is described in [Grammars section](09-lexical-structure.md# <i><a href="#grammar-logical-AND-expression-1">logical-AND-expression-1</a></i> <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> || <i><a href="#grammar-logical-AND-expression-1">logical-AND-expression-1</a></i> -<i id="grammar-conditional-expression">conditional-expression:</i> +<i id="grammar-coalesce-expression">coalesce-expression:</i> <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-conditional-expression">conditional-expression</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> -<i id="grammar-coalesce-expression">coalesce-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-expression">expression</a></i> +<i id="grammar-conditional-expression">conditional-expression:</i> + <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> + <i><a href="#grammar-conditional-expression">conditional-expression</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i id="grammar-assignment-expression">assignment-expression:</i> <i><a href="#grammar-conditional-expression">conditional-expression</a></i> - <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i><a href="#grammar-simple-assignment-expression">simple-assignment-expression</a></i> <i><a href="#grammar-byref-assignment-expression">byref-assignment-expression</a></i> <i><a href="#grammar-compound-assignment-expression">compound-assignment-expression</a></i>
diff --git a/spec/00-specification-for-php.md b/spec/00-specification-for-php.md index d2d2f4c..fb7fe9b 100644 --- a/spec/00-specification-for-php.md +++ b/spec/00-specification-for-php.md @@ -144,8 +144,8 @@ is distributed without any warranty. - [Bitwise Inclusive OR Operator](10-expressions.md#bitwise-inclusive-or-operator) - [Logical AND Operator (form 1)](10-expressions.md#logical-and-operator-form-1) - [Logical Inclusive OR Operator (form 1)](10-expressions.md#logical-inclusive-or-operator-form-1) - - [Conditional Operator](10-expressions.md#conditional-operator) - [Coalesce Operator](10-expressions.md#coalesce-operator) + - [Conditional Operator](10-expressions.md#conditional-operator) - [Assignment Operators](10-expressions.md#assignment-operators) - [General](10-expressions.md#general-5) - [Simple Assignment](10-expressions.md#simple-assignment) diff --git a/spec/10-expressions.md b/spec/10-expressions.md index 9bf100f..e99a4a3 100644 --- a/spec/10-expressions.md +++ b/spec/10-expressions.md @@ -2911,65 +2911,20 @@ This operator associates left-to-right. if ($month < 1 || $month > 12) ... ``` -## Conditional Operator - -**Syntax** - -<!-- GRAMMAR -conditional-expression: - logical-inc-OR-expression-1 - logical-inc-OR-expression-1 '?' expression? ':' conditional-expression ---> - -<pre> -<i id="grammar-conditional-expression">conditional-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-conditional-expression">conditional-expression</a></i> -</pre> - -**Semantics** -Given the expression `e1 ? e2 : e3`, `e1` is evaluated first and [converted to `bool`](08-conversions.md#converting-to-boolean-type) if it has another type. -If the result is `TRUE`, then and only then is `e2` evaluated, and the result and its type become the result and type of -the whole expression. Otherwise, then and only then is `e3` evaluated, and -the result and its type become the result and type of the whole -expression. There is a sequence point after the evaluation of `e1`. If `e2` -is omitted, the result and type of the whole expression is the value and -type of `e1` (before the conversion to `bool`). - -This operator associates left-to-right. - -**Examples** - -```PHP -for ($i = -5; $i <= 5; ++$i) - echo "$i is ".(($i & 1 == TRUE) ? "odd\n" : "even\n"); -// ----------------------------------------- -$a = 10 ? : "Hello"; // result is int with value 10 -$a = 0 ? : "Hello"; // result is string with value "Hello" -$i = PHP_INT_MAX; -$a = $i++ ? : "red"; // result is int with value 2147483647 (on a 32-bit - // system) even though $i is now the float 2147483648.0 -// ----------------------------------------- -$i++ ? f($i) : f(++$i); // the sequence point makes this well-defined -// ----------------------------------------- -function factorial($int) -{ - return ($int > 1) ? $int * factorial($int - 1) : $int; -} -``` - ## Coalesce Operator **Syntax** <!-- GRAMMAR coalesce-expression: - logical-inc-OR-expression-1 '??' expression + logical-inc-OR-expression-1 + logical-inc-OR-expression-1 '??' coalesce-expression --> <pre> <i id="grammar-coalesce-expression">coalesce-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-expression">expression</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> </pre> **Semantics** @@ -2979,8 +2934,8 @@ Given the expression `e1 ?? e2`, if `e1` is set and not `NULL` (i.e. TRUE for evaluated, and the result becomes the result of the whole expression. There is a sequence point after the evaluation of `e1`. -Note that the semantics of `??` is similar to `isset` so that uninitialized variables will not produce -warnings when used in `e1`. +Note that the semantics of `??` is similar to `isset` so that uninitialized variables will +not produce warnings when used in `e1`. This operator associates right-to-left. @@ -3006,6 +2961,54 @@ function foo() { var_dump(true ?? foo()); // outputs bool(true), "executed!" does not appear as it short-circuits ``` +## Conditional Operator + +**Syntax** + +<!-- GRAMMAR +conditional-expression: + coalesce-expression + conditional-expression '?' expression? ':' coalesce-expression +--> + +<pre> +<i id="grammar-conditional-expression">conditional-expression:</i> + <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> + <i><a href="#grammar-conditional-expression">conditional-expression</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> +</pre> + +**Semantics** +Given the expression `e1 ? e2 : e3`, `e1` is evaluated first and [converted to `bool`](08-conversions.md#converting-to-boolean-type) if it has another type. +If the result is `TRUE`, then and only then is `e2` evaluated, and the result and its type become the result and type of +the whole expression. Otherwise, then and only then is `e3` evaluated, and +the result and its type become the result and type of the whole +expression. There is a sequence point after the evaluation of `e1`. If `e2` +is omitted, the result and type of the whole expression is the value and +type of `e1` (before the conversion to `bool`). + +This operator associates left-to-right. + +**Examples** + +```PHP +for ($i = -5; $i <= 5; ++$i) + echo "$i is ".(($i & 1 == TRUE) ? "odd\n" : "even\n"); +// ----------------------------------------- +$a = 10 ? : "Hello"; // result is int with value 10 +$a = 0 ? : "Hello"; // result is string with value "Hello" +$i = PHP_INT_MAX; +$a = $i++ ? : "red"; // result is int with value 2147483647 (on a 32-bit + // system) even though $i is now the float 2147483648.0 +// ----------------------------------------- +$i++ ? f($i) : f(++$i); // the sequence point makes this well-defined +// ----------------------------------------- +function factorial($int) +{ + return ($int > 1) ? $int * factorial($int - 1) : $int; +} +``` + + ## Assignment Operators ### General @@ -3015,7 +3018,6 @@ var_dump(true ?? foo()); // outputs bool(true), "executed!" does not appear as i <!-- GRAMMAR assignment-expression: conditional-expression - coalesce-expression simple-assignment-expression byref-assignment-expression compound-assignment-expression @@ -3024,7 +3026,6 @@ assignment-expression: <pre> <i id="grammar-assignment-expression">assignment-expression:</i> <i><a href="#grammar-conditional-expression">conditional-expression</a></i> - <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i><a href="#grammar-simple-assignment-expression">simple-assignment-expression</a></i> <i><a href="#grammar-byref-assignment-expression">byref-assignment-expression</a></i> <i><a href="#grammar-compound-assignment-expression">compound-assignment-expression</a></i> diff --git a/spec/19-grammar.md b/spec/19-grammar.md index d3e82ba..6f17c92 100644 --- a/spec/19-grammar.md +++ b/spec/19-grammar.md @@ -654,16 +654,16 @@ The grammar notation is described in [Grammars section](09-lexical-structure.md# <i><a href="#grammar-logical-AND-expression-1">logical-AND-expression-1</a></i> <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> || <i><a href="#grammar-logical-AND-expression-1">logical-AND-expression-1</a></i> -<i id="grammar-conditional-expression">conditional-expression:</i> +<i id="grammar-coalesce-expression">coalesce-expression:</i> <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-conditional-expression">conditional-expression</a></i> + <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> -<i id="grammar-coalesce-expression">coalesce-expression:</i> - <i><a href="#grammar-logical-inc-OR-expression-1">logical-inc-OR-expression-1</a></i> ?? <i><a href="#grammar-expression">expression</a></i> +<i id="grammar-conditional-expression">conditional-expression:</i> + <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> + <i><a href="#grammar-conditional-expression">conditional-expression</a></i> ? <i><a href="#grammar-expression">expression</a></i><sub>opt</sub> : <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i id="grammar-assignment-expression">assignment-expression:</i> <i><a href="#grammar-conditional-expression">conditional-expression</a></i> - <i><a href="#grammar-coalesce-expression">coalesce-expression</a></i> <i><a href="#grammar-simple-assignment-expression">simple-assignment-expression</a></i> <i><a href="#grammar-byref-assignment-expression">byref-assignment-expression</a></i> <i><a href="#grammar-compound-assignment-expression">compound-assignment-expression</a></i>