Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F5581
Expr.php.diff
Public
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
•
bzimport
Nov 21 2014, 10:33 PM
2014-11-21 22:33:32 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
Expr.php.diff
View Options
--- Expr.php Revision 47428
+++ Expr.php Working copy
@@ -46,4 +46,7 @@
define( 'EXPR_POW', 35 );
define( 'EXPR_PI', 36 );
+define( 'EXPR_QUESTION_MARK', 37 );
+define( 'EXPR_COLON', 38 );
+define( 'EXPR_SIGN', 39 );
class ExprError extends Exception {
@@ -61,4 +64,5 @@
EXPR_POSITIVE => 10,
EXPR_EXPONENT => 10,
+ EXPR_SIGN => 9,
EXPR_SINE => 9,
EXPR_COSINE => 9,
@@ -89,4 +93,6 @@
EXPR_AND => 3,
EXPR_OR => 2,
+ EXPR_QUESTION_MARK => 1,
+ EXPR_COLON => 1,
EXPR_PI => 0,
EXPR_OPEN => -1,
@@ -127,7 +133,9 @@
EXPR_POW => '^',
EXPR_PI => 'pi',
+ EXPR_QUESTION_MARK => '?',
+ EXPR_COLON => ':',
+ EXPR_SIGN => 'sgn',
);
-
var $words = array(
'mod' => EXPR_MOD,
@@ -151,4 +159,5 @@
'ceil' => EXPR_CEIL,
'pi' => EXPR_PI,
+ 'sgn' => EXPR_SIGN,
);
@@ -163,8 +172,9 @@
$operands = array();
$operators = array();
+ $ndOperators = array();
# Unescape inequality operators
$expr = strtr( $expr, array( '<' => '<', '>' => '>',
- '−' => '-', '−' => '-' ) );
+ '−' => '-', '\xE2\x88\x92' => '-' ) );
$p = 0;
@@ -248,4 +258,5 @@
case EXPR_TRUNC:
case EXPR_CEIL:
+ case EXPR_SIGN:
if ( $expecting != 'expression' ) {
throw new ExprError( 'unexpected_operator', $word );
@@ -318,5 +329,5 @@
$lastOp = end( $operators );
while ( $lastOp && $lastOp != EXPR_OPEN ) {
- $this->doOperation( $lastOp, $operands );
+ $this->doOperation( $lastOp, $operands, $ndOperators );
array_pop( $operators );
$lastOp = end( $operators );
@@ -342,4 +353,12 @@
$op = EXPR_GREATER;
++$p;
+ } elseif ( $char == '?' ) {
+ $name = $char;
+ $op = EXPR_QUESTION_MARK;
+ ++$p;
+ } elseif ( $char == ':' ) {
+ $name = $char;
+ $op = EXPR_COLON;
+ ++$p;
} else {
throw new ExprError('unrecognised_punctuation', UtfNormal::cleanUp( $char ));
@@ -354,5 +373,5 @@
$lastOp = end( $operators );
while ( $lastOp && $this->precedence[$op] <= $this->precedence[$lastOp] ) {
- $this->doOperation( $lastOp, $operands );
+ $this->doOperation( $lastOp, $operands, $ndOperators );
array_pop( $operators );
$lastOp = end( $operators );
@@ -364,14 +383,17 @@
// Finish off the operator array
while ( $op = array_pop( $operators ) ) {
- if ( $op == EXPR_OPEN ) {
- throw new ExprError('unclosed_bracket');
+ switch ( $op ) {
+ case EXPR_OPEN: throw new ExprError('unclosed_bracket');
+ case EXPR_QUESTION_MARK: throw new ExprError('unserved_question_mark');
}
- $this->doOperation( $op, $operands );
+ $this->doOperation( $op, $operands, $ndOperators );
}
+ if ( count ( $ndOperators ) ) throw new ExprError('unserved_question_mark');
+
return implode( "<br />\n", $operands );
}
- function doOperation( $op, &$stack ) {
+ function doOperation( $op, &$stack, &$ndStack ) {
switch ( $op ) {
case EXPR_NEGATIVE:
@@ -549,4 +571,24 @@
if ( false === ($stack[] = pow($left, $right)) ) throw new ExprError('division_by_zero', $this->names[$op]);
break;
+ case EXPR_SIGN:
+ if ( count( $stack ) < 1 ) throw new ExprError('missing_operand', $this->names[$op]);
+ $arg = array_pop( $stack );
+ $stack[] = $arg ? ($arg > 0 ? 1 : -1) : 0;
+ break;
+ case EXPR_QUESTION_MARK:
+ if ( count( $stack ) < 2 ) throw new ExprError('missing_operand', $this->names[$lastOp]);
+ $right = array_pop( $stack );
+ $left = array_pop( $stack );
+ $ndStack[] = $left ? true : false;
+ $stack[] = $right; // push back for EXPR_COLON
+ break;
+ case EXPR_COLON:
+ if ( count( $ndStack ) < 1 ) throw new ExprError('missing_question_mark', $this->names[$lastOp]);
+ if ( count( $stack ) < 2 ) throw new ExprError('missing_operand', $this->names[$lastOp]);
+ $arg = array_pop( $ndStack );
+ $right = array_pop( $stack );
+ $left = array_pop( $stack );
+ $stack[] = $arg ? $left : $right;
+ break;
default:
// Should be impossible to reach here.
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5131
Default Alt Text
Expr.php.diff (3 KB)
Attached To
Mode
T20199: Provide the ternary operator (condition ? then : else)
Attached
Detach File
Event Timeline
Log In to Comment