rfc:chaining_comparison

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:chaining_comparison [2016/12/22 17:42]
bp1222
rfc:chaining_comparison [2017/09/22 13:28] (current)
Line 8: Line 8:
  
 ===== Introduction ===== ===== Introduction =====
-This RFC proposes a syntax change to allow the chaining together of comparison and equality operations ''​[==,​ !=, !==, ===, <, <=, >, >​=]'' ​to allow arbitrary comparisons.  The initial request that spawned this RFC[1] was initially only for interval checking. ​ Discussion on the thread expanded the scope of the request to go from strictly interval checking to allowing more arbitrary number of comparisons. ​ It evolved from there to expand to a majority of the comparison operations.+This RFC proposes a syntax change to allow arbitrary ​chaining together of comparison and equality operations ''​[==,​ !=, !==, ===, <, <=, >, >​=]''​. ​ The initial request that spawned this RFC[1] was initially only for interval checking. ​ Discussion on the thread expanded the scope of the request to go from strictly interval checking to allowing more arbitrary number of comparisons. ​ It evolved from there to expand to a majority of the comparison operations.  The primary benefit to this proposal would be to make for more readable code when doing numerous comparisons between a single variable.
  
 <file php> <file php>
Line 22: Line 22:
  
 /* /*
- * To allow this to be functionally ​the same+ * To be functionally ​equivalent to this syntax
  */  */
 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,​ and execution. Proposals herein will contain a dump of relevant AST (php-ast) nodes and OPCodes (vld) to better visualize the compilation,​ and execution.
 +
 ==== Comparison Chaining ==== ==== Comparison Chaining ====
-The proposal creates a new AST operation type ''​ZEND_AST_COMPARE_OP''​ which will be compiled in a left-recursive ​required ​manor.  ​In doing this compilation we ensure short cutting of righter operations if the left sides have evaluated to false. ​ To accomplish this we introduce a new means of emitting an operation, by noting where a ''​JMPZ_EX''​ may need to exist (see implementations for ''​zend_emit_op_at''​). ​ This will shift operations that may have been emitted by compiling the right side of this AST compare to allow jumping over them if the left side of the operation is evaluated to false. ​ I believe this means is necessary because we can't just shortcut if the left operation is false, ''​false < $a++''​ should still evaluate the right part of the expression. ​ We should only inject the JMPZ_EX ops, IF, the left child is a chained ''​ZEND_AST_COMPARE_OP''​. ​ The proposal also changes the associativity of the equality, and comparison, operations to being left associative.+The proposal creates a new AST operation type ''​ZEND_AST_COMPARE_OP''​ which will be compiled in a left-recursive manor.  ​
  
 <file php> <file php>
Line 117: Line 118:
  */  */
 </​file>​ </​file>​
 +
 +==== False Short Circuiting ====
 +In doing this compilation we can ensure short cutting of righter operations if the left sides have evaluated to false. ​ To accomplish this we introduce a new means of emitting an operation, by noting where a ''​JMPZ_EX''​ may need to exist (see implementations for ''​zend_emit_op_at''​). ​ This will shift operations that may have been emitted by compiling the right side of this AST compare to allow jumping over them if the left side of the operation is evaluated to false. ​ I believe this means is necessary because we can't just shortcut if the left operation is false, ''​false < $a++''​ should still evaluate the right part of the expression. ​ We should only inject the JMPZ_EX ops, IF, the left child is a chained ''​ZEND_AST_COMPARE_OP''​. ​ The proposal also changes the associativity of the equality, and comparison, operations to being left associative.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 270: Line 274:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Implementation ​#1: comparisons evaluated before equality: https://​github.com/​php/​php-src/​compare/​master...bp1222:​multi-compare +Working ​Implementation:​ comparisons evaluated before equality: https://​github.com/​php/​php-src/​compare/​master...bp1222:​multi-compare
- +
-Implementation #2: comparisons and equality evaluated together: https://​github.com/​php/​php-src/​compare/​master...bp1222:​multi-compare-equal-prec +
- +
-(Note: Implementation #2 branched from #1 significantly back in time as a psuedo example. ​ I have for better/​worse kept squashing implementation #1, which is significantly more fleshed out as a possible PR implementation)+
  
 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.txt · Last modified: 2017/09/22 13:28 (external edit)