Pastakhov has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/58271


Change subject: add types and inverting operators (version 0.0.6)
......................................................................

add types and inverting operators (version 0.0.6)

examples:
 $a = ~5; // -6
 $a = (string)5; // '5'
 $a = (int)5.5; // 5

Change-Id: Id8980a6e1d2a82813dd4fbbe03f25f74fb42076e
---
M Foxway.php
M includes/Runtime.php
M tests/phpunit/includes/InterpreterTest.php
3 files changed, 102 insertions(+), 50 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway 
refs/changes/71/58271/1

diff --git a/Foxway.php b/Foxway.php
index e2e49dc..ba8c493 100644
--- a/Foxway.php
+++ b/Foxway.php
@@ -15,7 +15,7 @@
        die( 'This file is an extension to MediaWiki and thus not a valid entry 
point.' );
 }
 
-define( 'Foxway_VERSION' , '0.0.5' );
+define( 'Foxway_VERSION' , '0.0.6' );
 
 // Register this extension on Special:Version
 $wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 36199e7..f197c2f 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -15,7 +15,6 @@
        private $lastParam = null;
        private $listParams = array();
        private $lastOperator = false;
-       private $lastNegative = null;
        private $variableOperator = false;
        private $mathMemory = array();
 
@@ -24,10 +23,13 @@
 
        // @see http://www.php.net/manual/ru/language.operators.precedence.php
        private $operatorsPrecedence = array(
+               //                      (int)                   (float)         
(string)                (array)         (bool)
+               array('~', T_INT_CAST, T_DOUBLE_CAST, T_STRING_CAST, 
T_ARRAY_CAST, T_BOOL_CAST),
                array('!'),
                array('*', '/', '%'),
                array('+', '-', '.'),
-               array('<<', '>>'),
+               //              <<      >>
+               array(T_SL, T_SR),
                array('<', '<=', '>', '>='),
                array('==', '!=', '===', '!==', '<>'),
                array('&'),
@@ -50,7 +52,7 @@
        }
 
        private function pushStack() {
-               $this->stack[] = array($this->lastCommand, $this->lastDebug, 
$this->listParams, $this->lastOperator, $this->lastNegative, 
$this->variableOperator, $this->mathMemory);
+               $this->stack[] = array($this->lastCommand, $this->lastDebug, 
$this->listParams, $this->lastOperator, $this->variableOperator, 
$this->mathMemory);
                $this->resetRegisters();
        }
 
@@ -58,7 +60,7 @@
                if( count($this->stack) == 0 ) {
                        $this->resetRegisters();
                } else {
-                       list($this->lastCommand, $this->lastDebug, 
$this->listParams, $this->lastOperator, $this->lastNegative, 
$this->variableOperator, $this->mathMemory) = array_pop($this->stack);
+                       list($this->lastCommand, $this->lastDebug, 
$this->listParams, $this->lastOperator, $this->variableOperator, 
$this->mathMemory) = array_pop($this->stack);
                }
        }
 
@@ -68,7 +70,6 @@
                $this->lastParam = null;
                $this->listParams = array();
                $this->lastOperator = false;
-               $this->lastNegative = null;
                $this->variableOperator = false;
                $this->mathMemory = array();
        }
@@ -84,60 +85,69 @@
        }
 
        public function addParam( $param ) {
-\MWDebug::log( "function addParam( '$param' ) @ lastOperator=" . 
var_export($this->lastOperator, true) . " lastNegative=" . 
var_export($this->lastNegative, true));
-               if( $this->lastNegative === true) {
-                       $param = -$param;
-                       $this->lastNegative = null;
-               }
-
+\MWDebug::log( "function addParam( '$param' ) @ lastOperator=" . 
var_export($this->lastOperator, true) );
                if( $this->lastOperator ) {
                        $precedence = $this->getOperatorPrecedence( 
$this->lastOperator );
                        $this->mathMemory[$precedence] = 
array($this->lastOperator, $this->lastParam);
-                       $this->lastParam = $this->lastNegative === true ? 
-$param : $param;
-                       $this->lastNegative = null;
                        $this->lastOperator = false;
-               } else {
-                       if( !is_null($this->lastParam) ) {
-                               $this->listParams[] = $this->lastParam;
-                       }
-                       $this->lastParam = $param;
+               } elseif( !is_null($this->lastParam) ) {
+                       $this->listParams[] = $this->lastParam;
                }
+               $this->lastParam = $param;
+               $this->doMath(0);
 \MWDebug::log("lastParam = $this->lastParam, lastOperator = 
$this->lastOperator, mathMemory = " . var_export($this->mathMemory, true) );
        }
 
        public function separateParams() {
 \MWDebug::log("function separateParams()");
-               while( count($this->mathMemory) != 0 ) {
-                       $mathMemory = array_pop($this->mathMemory);
-                       $this->doOperation($mathMemory[0], $mathMemory[1]);
-               }
+               $this->doMath();
                $this->listParams[] = $this->lastParam;
                $this->lastParam = null;
        }
 
        public function addOperator( $operator ) {
-\MWDebug::log("function addOperator( $operator ) @ lastOperator = 
'$this->lastOperator', lastParam = '$this->lastParam'" );
-               // For negative operator
-               if( $this->lastOperator || is_null($this->lastParam) ) {
-                       if( $operator == '-' ) {
-                               //                                        false 
or null
-                               $this->lastNegative = !$this->lastNegative ? 
true : false;
-\MWDebug::log( "lastNegative = " . var_export($this->lastNegative, true) );
+               $precedence = $this->getOperatorPrecedence( $operator );
+\MWDebug::log("function addOperator( $operator ) @ lastOperator = '" . 
var_export($this->lastOperator, true) . "', lastParam = '" . 
var_export($this->lastParam, true) . "', precedence = '$precedence'" );
+               //                                              For negative 
operator
+               if( $precedence == 0 || $this->lastOperator || 
is_null($this->lastParam) ) {
+                       switch ($operator) {
+                               case '+':
+                                       break; // ignore this
+                               case '-':
+                               case '~':
+                               case T_INT_CAST:
+                               case T_DOUBLE_CAST:
+                               case T_STRING_CAST:
+                               case T_ARRAY_CAST:
+                               case T_BOOL_CAST:
+                                       if( !isset($this->mathMemory[0]) ) {
+                                               $this->mathMemory[0] = array();
+                                       }
+                                       $this->mathMemory[0][] = $operator;
+                                       break;
+                               default:
+                                       \MWDebug::log( __METHOD__ . " unknown 
operator '$operator'" );
+                                       break;
                        }
                } else {
                        //doOperation for higher precedence
-                       $precedence = $this->getOperatorPrecedence( $operator );
-                       if( $precedence == 0 && !is_null($this->lastParam) ) {
-                               $this->doOperation($operator);
-                       } else {
-                               for($n = 0; $n <= $precedence; $n++) {
-                                       if( isset($this->mathMemory[$n]) ) {
-                                               
$this->doOperation($this->mathMemory[$n][0], $this->mathMemory[$n][1]);
-                                               unset($this->mathMemory[$n]);
-                                       }
-                               }
-                       }
+                       $this->doMath($precedence);
                        $this->lastOperator = $operator;
+               }
+       }
+
+       private function doMath( $precedence = 12 ) { //12 = 
count($operatorsPrecedence)-1
+               if( isset($this->mathMemory[0]) ) {
+                       while( $mathZerroMemory = 
array_pop($this->mathMemory[0]) ) {
+                               $this->doOperation($mathZerroMemory);
+                       }
+                       unset($this->mathMemory[0]);
+               }
+               for($n = 1; $n <= $precedence; $n++) {
+                       if( isset($this->mathMemory[$n]) ) {
+                               $this->doOperation($this->mathMemory[$n][0], 
$this->mathMemory[$n][1]);
+                               unset($this->mathMemory[$n]);
+                       }
                }
        }
 
@@ -150,7 +160,11 @@
                                $this->lastParam += $param;
                                break;
                        case '-':
-                               $this->lastParam = $param - $this->lastParam;
+                               if( is_null($param) ) { // Negation
+                                       $this->lastParam = -$this->lastParam;
+                               } else { // Subtraction
+                                       $this->lastParam = $param - 
$this->lastParam;
+                               }
                                break;
                        case '*':
                                $this->lastParam *= $param;
@@ -176,6 +190,24 @@
                        case T_SR: // >>
                                $this->lastParam = $param >> $this->lastParam;
                                break;
+                       case '~':
+                               $this->lastParam = ~$this->lastParam;
+                               break;
+                       case T_INT_CAST:
+                               $this->lastParam = (integer) $this->lastParam;
+                               break;
+                       case T_DOUBLE_CAST:
+                               $this->lastParam = (float) $this->lastParam;
+                               break;
+                       case T_STRING_CAST:
+                               $this->lastParam = (string) $this->lastParam;
+                               break;
+                       case T_ARRAY_CAST:
+                               $this->lastParam = (array) $this->lastParam;
+                               break;
+                       case T_BOOL_CAST:
+                               $this->lastParam = (bool) $this->lastParam;
+                               break;
                        default:
                                \MWDebug::log( __METHOD__ . " unknown operator 
'$operator'" );
                                break;
@@ -184,10 +216,8 @@
        }
 
        public function getCommandResult( &$debug ) {
-               while( count($this->mathMemory) != 0 ) {
-                       $mathMemory = array_pop($this->mathMemory);
-                       $this->doOperation($mathMemory[0], $mathMemory[1]);
-               }
+               \MWDebug::log('function getCommandResult()');
+               $this->doMath();
                $this->listParams[] = $this->lastParam;
                $return = null;
 
@@ -294,10 +324,7 @@
        }
 
        public function parenthesesClose() {
-               while( count($this->mathMemory) != 0 ) {
-                       $mathMemory = array_pop($this->mathMemory);
-                       $this->doOperation($mathMemory[0], $mathMemory[1]);
-               }
+               $this->doMath();
                $this->popStack();
        }
 }
diff --git a/tests/phpunit/includes/InterpreterTest.php 
b/tests/phpunit/includes/InterpreterTest.php
index 887eb87..b71ca83 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -316,6 +316,12 @@
                                '3'
                                );
        }
+       public function testRun_echo_math_RightShift_3() {
+               $this->assertEquals(
+                               Interpreter::run('echo -123 >> 2 + 3;'),
+                               '-4'
+                               );
+       }
 
        public function testRun_echo_math_Increment_1() {
                $this->assertEquals(
@@ -373,4 +379,23 @@
                                );
        }
 
+       public function testRun_echo_inverting_1() {
+               $this->assertEquals(
+                               Interpreter::run('echo ~10;'),
+                               '-11'
+                               );
+       }
+       public function testRun_echo_inverting_2() {
+               $this->assertEquals(
+                               Interpreter::run('echo ~-10;'),
+                               '9'
+                               );
+       }
+       public function testRun_echo_inverting_3() {
+               $this->assertEquals(
+                               Interpreter::run('echo -~10;'),
+                               '11'
+                               );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/58271
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id8980a6e1d2a82813dd4fbbe03f25f74fb42076e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Foxway
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to