rfc:chaining_comparison
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:chaining_comparison [2016/12/17 15:56] – bp1222 | rfc:chaining_comparison [2021/03/27 14:58] (current) – Move to inactive ilutov | ||
---|---|---|---|
Line 4: | Line 4: | ||
* Author: David Walker (dave@mudsite.com) | * Author: David Walker (dave@mudsite.com) | ||
* Author: Richard Fussenegger (php@fleshgrinder.com) | * Author: Richard Fussenegger (php@fleshgrinder.com) | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC proposes a syntax change to allow the chaining together of comparison and equality operations '' | + | This RFC proposes a syntax change to allow arbitrary |
<file php> | <file php> | ||
Line 22: | Line 22: | ||
/* | /* | ||
- | * To allow this to be functionally | + | * To be functionally |
*/ | */ | ||
if (0 < $a < 100) { | if (0 < $a < 100) { | ||
Line 31: | Line 31: | ||
===== Proposal ===== | ===== Proposal ===== | ||
Proposals herein will contain a dump of relevant AST (php-ast) nodes and OPCodes (vld) to better visualize the compilation, | Proposals herein will contain a dump of relevant AST (php-ast) nodes and OPCodes (vld) to better visualize the compilation, | ||
+ | |||
==== Comparison Chaining ==== | ==== Comparison Chaining ==== | ||
- | The proposal creates a new AST operation type '' | + | The proposal creates a new AST operation type '' |
<file php> | <file php> | ||
Line 66: | Line 67: | ||
| | ||
| | ||
- | | + | |
| | ||
- | | + | |
- | | + | |
| | ||
*/ | */ | ||
Line 111: | Line 112: | ||
* | * | ||
| | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
*/ | */ | ||
</ | </ | ||
+ | |||
+ | ==== False Short Circuiting ==== | ||
+ | In doing this compilation we can ensure short cutting of righter operations if the left sides have evaluated to false. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | No BC Breaking changes expected | + | BC Breaking changes expected |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 126: | Line 130: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | Yes, we're adding new JMPZ_EX | + | I'm unsure; |
===== Open Issues ===== | ===== Open Issues ===== | ||
Line 159: | Line 163: | ||
var_dump((1 < 2) == (3 < 4) == (5 < 6)) // bool(true) | var_dump((1 < 2) == (3 < 4) == (5 < 6)) // bool(true) | ||
</ | </ | ||
+ | |||
+ | ==== Right Recursion ==== | ||
+ | Another syntax difference that could be BC problematic is with right-recursion of the chained expression. | ||
+ | |||
+ | <file php> | ||
+ | <?php | ||
+ | var_dump(1 < (2 < 3)); | ||
+ | var_dump(1 < 2 == 3); | ||
+ | var_dump(1 < 2 == 3 == 4); | ||
+ | var_dump(1 < 2 == (3 == 4)); | ||
+ | </ | ||
+ | |||
+ | We will go over how PHP 7.1 currently would evaluate each, and then how a right-recursive chain would pan out. | ||
+ | <file php> | ||
+ | <?php | ||
+ | var_dump(1 < (2 < 3)); | ||
+ | /* | ||
+ | * 1 < (2 < 3) := 1 < true := false | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3); | ||
+ | /* | ||
+ | * (1 < 2) == 3 := true == 3 := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3 == 4); | ||
+ | /* | ||
+ | * Parse Error, unexpected == | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == (3 == 4)); | ||
+ | /* | ||
+ | * (1 < 2) == (3 == 4) := true == false := false | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | The current proposal (implemented) evaluation method. | ||
+ | <file php> | ||
+ | <?php | ||
+ | var_dump(1 < (2 < 3)); | ||
+ | /* | ||
+ | * Parse Error: No right recursion | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3); | ||
+ | /* | ||
+ | * (1 < 2) == 3 := true == 3 := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3 == 4); | ||
+ | /* | ||
+ | * ((1 < 2) == 3) == 4 := (true == 3) == 4 := true == 4 := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == (3 == 4)); | ||
+ | /* | ||
+ | * (1 < 2) == (3 == 4) := true == false := false | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | If however we permitted right recursive comparison operations we would evaluate as such: | ||
+ | <file php> | ||
+ | <?php | ||
+ | var_dump(1 < (2 < 3)); | ||
+ | /* | ||
+ | * 1 < (2 < 3) := 1 && (2 < 3) && (1 < 2) := true && true && true := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3); | ||
+ | /* | ||
+ | * (1 < 2) == 3 := true == 3 := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == 3 == 4); | ||
+ | /* | ||
+ | * ((1 < 2) == 3) == 4 := (true == 3) == 4 := true == 4 := true | ||
+ | */ | ||
+ | |||
+ | var_dump(1 < 2 == (3 == 4)); | ||
+ | /* | ||
+ | * (1 < 2) == (3 == 4) := true == false := false | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | If the first example in this last one looks a little odd, it's because it is. We do design for short-cutting of a long expression when a fault is found to prevent further execution much like you have in '' | ||
+ | |||
+ | Although allow right-recursion of equality operations does itself introduce some slightly odd syntax like: | ||
+ | <file php> | ||
+ | <?php | ||
+ | /* | ||
+ | * Right chained comparison syntax | ||
+ | */ | ||
+ | var_dump(1 < (2 == 2)); // bool(false) | ||
+ | |||
+ | /* | ||
+ | * Is Functionally identical to PHP 7.1's allowed syntax | ||
+ | */ | ||
+ | var_dump(1 < (2 <= 2)); // bool(false) | ||
+ | </ | ||
+ | Since we don't chain together the right/left node of an equality operator, this is functionally identical to PHP 7.1's allowed syntax. | ||
+ | |||
+ | |||
+ | As we can see right-recursive comparison operations do have numerous caveats and oddities. | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 167: | Line 274: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Implementation | + | Working |
- | + | ||
- | Implementation #2: comparisons and equality evaluated together: https:// | + | |
Will need eyes of those more familiar with AST/VM to review. | Will need eyes of those more familiar with AST/VM to review. | ||
- | |||
- | For changes affecting the core language, you should also provide a patch for the language specification. | ||
===== Implementation ===== | ===== Implementation ===== |
rfc/chaining_comparison.1481990209.txt.gz · Last modified: 2017/09/22 13:28 (external edit)