r32536 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r32535‎ | r32536 | r32537 >
Date:11:59, 28 March 2008
Author:werdna
Status:old
Tags:
Comment:
Allow expr parser function to accept exponent input (i.e. 3e8). It seems silly to output in a format that we don't accept as input.
Modified paths:
  • /trunk/extensions/ParserFunctions/Expr.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/Expr.php
@@ -30,6 +30,7 @@
3131 define( 'EXPR_GREATEREQ', 19 );
3232 define( 'EXPR_NOTEQ', 20 );
3333 define( 'EXPR_ROUND', 21 );
 34+define( 'EXPR_EXPONENT', 22 );
3435
3536 class ExprError extends Exception {
3637 public function __construct($msg, $parameter = ''){
@@ -44,6 +45,7 @@
4546 var $precedence = array(
4647 EXPR_NEGATIVE => 10,
4748 EXPR_POSITIVE => 10,
 49+ EXPR_EXPONENT => 9,
4850 EXPR_NOT => 9,
4951 EXPR_TIMES => 8,
5052 EXPR_DIVIDE => 8,
@@ -60,7 +62,7 @@
6163 EXPR_AND => 3,
6264 EXPR_OR => 2,
6365 EXPR_OPEN => -1,
64 - EXPR_CLOSE => -1
 66+ EXPR_CLOSE => -1,
6567 );
6668
6769 var $names = array(
@@ -81,6 +83,7 @@
8284 EXPR_NOTEQ => '<>',
8385 EXPR_AND => 'and',
8486 EXPR_OR => 'or',
 87+ EXPR_EXPONENT => 'e',
8588 );
8689
8790
@@ -90,7 +93,8 @@
9194 'or' => EXPR_OR,
9295 'not' => EXPR_NOT,
9396 'round' => EXPR_ROUND,
94 - 'div' => EXPR_DIVIDE
 97+ 'div' => EXPR_DIVIDE,
 98+ 'e' => EXPR_EXPONENT,
9599 );
96100
97101 /**
@@ -187,7 +191,7 @@
188192 }
189193
190194 // Finally the single-character operators
191 -
 195+
192196 elseif ( $char == '+' ) {
193197 ++$p;
194198 if ( $expecting == 'expression' ) {
@@ -382,6 +386,12 @@
383387 $left = array_pop( $stack );
384388 $stack[] = ( $left != $right ) ? 1 : 0;
385389 break;
 390+ case EXPR_EXPONENT:
 391+ if ( count( $stack ) < 2 ) throw new ExprError('missing_operand', $this->names[$op]);
 392+ $right = array_pop( $stack );
 393+ $left = array_pop( $stack );
 394+ $stack[] = $left * pow(10,$right);
 395+ break;
386396 default:
387397 // Should be impossible to reach here.
388398 throw new ExprError('unknown_error');

Status & tagging log