jenkins-bot has submitted this change and it was merged.

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/Interpreter.php
M includes/Runtime.php
M tests/phpunit/includes/InterpreterTest.php
4 files changed, 162 insertions(+), 61 deletions(-)

Approvals:
  Pastakhov: Verified; Looks good to me, approved
  jenkins-bot: Verified



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/Interpreter.php b/includes/Interpreter.php
index 3fd83c1..b8d5d37 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -89,6 +89,12 @@
                                case '^':
                                case T_SL: // <<
                                case T_SR: // >>
+                               case '~':
+                               case T_INT_CAST: // (int)
+                               case T_DOUBLE_CAST: // (double)
+                               case T_STRING_CAST: // (string)
+                               case T_ARRAY_CAST: // (array)
+                               case T_BOOL_CAST: // (bool)
                                                $runtime->addOperator( $id );
                                        break;
                                case T_INC: // ++
@@ -264,18 +270,24 @@
                                case T_SL: // <<
                                case T_SR: // >>
                                        $expected = array(
-                                               T_CONSTANT_ENCAPSED_STRING,
-                                               T_ENCAPSED_AND_WHITESPACE,
-                                               T_LNUMBER,
-                                               T_DNUMBER,
-                                               T_VARIABLE,
-                                               T_INC,
-                                               T_DEC,
-                                               T_CURLY_OPEN,
+                                               T_CONSTANT_ENCAPSED_STRING, // 
"foo" or 'bar'
+                                               T_ENCAPSED_AND_WHITESPACE, // " 
$a"
+                                               T_LNUMBER, // 123, 012, 0x1ac
+                                               T_DNUMBER, // 0.12
+                                               T_VARIABLE, // $foo
+                                               T_INC, // ++
+                                               T_DEC, // --
+                                               T_CURLY_OPEN, // {
                                                '"',
                                                '-',
                                                '+',
                                                '(',
+                                               '~',
+                                               T_INT_CAST, // (int)
+                                               T_DOUBLE_CAST, // (double)
+                                               T_STRING_CAST, // (string)
+                                               T_ARRAY_CAST, // (array)
+                                               T_BOOL_CAST, // (bool)
                                                );
                                        break;
                                case T_CURLY_OPEN:
@@ -327,6 +339,13 @@
                                        case T_DNUMBER:
                                                $debug[] = '<span 
style="color:#FF00FF" title="'. token_name($id) . '">' . 
htmlspecialchars($text) . '</span>';
                                                break;
+                                       case T_INT_CAST: // (int)
+                                       case T_DOUBLE_CAST: // (double)
+                                       case T_STRING_CAST: // (string)
+                                       case T_ARRAY_CAST: // (array)
+                                       case T_BOOL_CAST: // (bool)
+                                               $debug[] = '<span 
style="color:#0000E6" title="'. token_name($id) . '">' . 
htmlspecialchars($text) . '</span>';
+                                               break;
                                        case T_ECHO:
                                        case T_VARIABLE:
                                                break;
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 36199e7..0804481 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;
                }
-\MWDebug::log("lastParam = $this->lastParam, lastOperator = 
$this->lastOperator, mathMemory = " . var_export($this->mathMemory, true) );
+               $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]);
-               }
+//\MWDebug::log("function separateParams()");
+               $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,18 +190,34 @@
                        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;
                }
-\MWDebug::log( "function doOperation('$operator', '$param') @ 
'$this->lastParam'");
+//\MWDebug::log( "function doOperation('$operator', '$param') @ 
'$this->lastParam'");
        }
 
        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..fd99a6b 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,53 @@
                                );
        }
 
+       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'
+                               );
+       }
+
+       public function testRun_echo_type_1() {
+               $this->assertEquals(
+                               Interpreter::run('echo (bool)10;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_type_2() {
+               $this->assertEquals(
+                               Interpreter::run('echo (bool)-10;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_type_3() {
+               $this->assertEquals(
+                               Interpreter::run('echo -(bool)10;'),
+                               '-1'
+                               );
+       }
+       public function testRun_echo_type_4() {
+               $this->assertEquals(
+                               Interpreter::run('echo (bool)0;'),
+                               ''
+                               );
+       }
+       public function testRun_echo_type_5() {
+               $this->assertEquals(
+                               Interpreter::run('echo -(int)-5.5;'),
+                               '5'
+                               );
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id8980a6e1d2a82813dd4fbbe03f25f74fb42076e
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Foxway
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>
Gerrit-Reviewer: Pastakhov <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to