rfc:chaining_comparison

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:chaining_comparison [2016/12/22 16:22] bp1222rfc:chaining_comparison [2016/12/22 17:42] bp1222
Line 181: Line 181:
 var_dump(1 < 2 == 3); var_dump(1 < 2 == 3);
 /* /*
- * 1 < 2 == 3 := true == 3 := true+ (1 < 2== 3 := true == 3 := true
  */  */
  
Line 191: Line 191:
 var_dump(1 < 2 == (3 == 4)); var_dump(1 < 2 == (3 == 4));
 /* /*
- * 1 < 2 == (3 == 4) := true == false := false+ (1 < 2== (3 == 4) := true == false := false
  */  */
 </file> </file>
Line 205: Line 205:
 var_dump(1 < 2 == 3); var_dump(1 < 2 == 3);
 /* /*
- * 1 < 2 == 3 := true == 3 := true+ (1 < 2== 3 := true == 3 := true
  */  */
  
 var_dump(1 < 2 == 3 == 4); var_dump(1 < 2 == 3 == 4);
 /* /*
- * 1 < 2 == 3 == 4 := (true == 3) == 4 := true == 4 := true+ ((1 < 2== 3== 4 := (true == 3) == 4 := true == 4 := true
  */  */
  
 var_dump(1 < 2 == (3 == 4)); var_dump(1 < 2 == (3 == 4));
 /* /*
- * 1 < 2 == (3 == 4) := true == false := false+ (1 < 2== (3 == 4) := true == false := false
  */  */
 </file> </file>
Line 229: Line 229:
 var_dump(1 < 2 == 3); var_dump(1 < 2 == 3);
 /* /*
- * 1 < 2 == 3 := true == 3 := true+ (1 < 2== 3 := true == 3 := true
  */  */
  
 var_dump(1 < 2 == 3 == 4); var_dump(1 < 2 == 3 == 4);
 /* /*
- * 1 < 2 == 3 == 4 := (true == 3) == 4 := true == 4 := true+ ((1 < 2== 3== 4 := (true == 3) == 4 := true == 4 := true
  */  */
  
 var_dump(1 < 2 == (3 == 4)); var_dump(1 < 2 == (3 == 4));
 /* /*
- * 1 < 2 == (3 == 4) := true == false := false+ (1 < 2== (3 == 4) := true == false := false
  */  */
 </file> </file>
  
 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 ''if()'' statements.  However, we do process in a left-to-right manor.  So the first thing would require us to ensure the left most side evaluates to true, and if it wasn't ''1'' but rather ''$a++'', we'd want to ensure to get that left nodes potential opcodes to execute before comparing the right hand side.  Since we are chaining, we'd want to evaluate the right, then return the left node of it to be evaluated against the top's left node.  This, odd syntax is why I didn't implement a right-recursive chaining of comparison operations. 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 ''if()'' statements.  However, we do process in a left-to-right manor.  So the first thing would require us to ensure the left most side evaluates to true, and if it wasn't ''1'' but rather ''$a++'', we'd want to ensure to get that left nodes potential opcodes to execute before comparing the right hand side.  Since we are chaining, we'd want to evaluate the right, then return the left node of it to be evaluated against the top's left node.  This, odd syntax is why I didn't implement a right-recursive chaining of comparison operations.
 +
 +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)
 +</file>
 +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.  We could, for equality operations denote if they were in-fact a right node-continuation of a chain, thus would allow them to evaluate to either the left node, or false.
 +
 +
 +As we can see right-recursive comparison operations do have numerous caveats and oddities.  For these reasons we didn't implement it, and generally are on the side of forbidding right-recursive comparison operations.
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 255: Line 273:
  
 Implementation #2: comparisons and equality evaluated together: https://github.com/php/php-src/compare/master...bp1222:multi-compare-equal-prec 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.
rfc/chaining_comparison.txt · Last modified: 2021/03/27 14:58 by ilutov