Pastakhov has uploaded a new change for review.

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


Change subject: add comparison operators (version 0.0.7)
......................................................................

add comparison operators (version 0.0.7)

Change-Id: I0f5f30e0652d28df6d695f5bf2c6f14aedd6353b
---
M Foxway.php
M includes/Interpreter.php
M includes/Runtime.php
M tests/phpunit/includes/InterpreterTest.php
4 files changed, 159 insertions(+), 7 deletions(-)


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

diff --git a/Foxway.php b/Foxway.php
index ba8c493..ca305de 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.6' );
+define( 'Foxway_VERSION' , '0.0.7' );
 
 // Register this extension on Special:Version
 $wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Interpreter.php b/includes/Interpreter.php
index b8d5d37..b9abcb6 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -16,6 +16,30 @@
                T_DOC_COMMENT,
        );
 
+       private static $arrayOperators = array(
+               ';',
+               '.',
+               '+',
+               '-',
+               '*',
+               '/',
+               '%',
+               '&',
+               '|',
+               '^',
+               T_SL,                                           // <<
+               T_SR,                                           // >>
+               T_ENCAPSED_AND_WHITESPACE,      // " $a"
+               '<',
+               '>',
+               T_IS_SMALLER_OR_EQUAL,          // <=
+               T_IS_GREATER_OR_EQUAL,          // >=
+               T_IS_EQUAL,                                     // ==
+               T_IS_NOT_EQUAL,                         // !=
+               T_IS_IDENTICAL,                         // ===
+               T_IS_NOT_IDENTICAL,                     // !==
+               );
+
        public static function run($source, $is_debug = false) {
                $tokens = token_get_all("<?php $source ?>");
                $return = "";
@@ -95,6 +119,14 @@
                                case T_STRING_CAST: // (string)
                                case T_ARRAY_CAST: // (array)
                                case T_BOOL_CAST: // (bool)
+                               case '<':
+                               case '>':
+                               case T_IS_SMALLER_OR_EQUAL: // <=
+                               case T_IS_GREATER_OR_EQUAL: // >=
+                               case T_IS_EQUAL: // ==
+                               case T_IS_NOT_EQUAL: // !=
+                               case T_IS_IDENTICAL: // ===
+                               case T_IS_NOT_IDENTICAL: // !==
                                                $runtime->addOperator( $id );
                                        break;
                                case T_INC: // ++
@@ -110,7 +142,7 @@
                                                        $variableValue--;
                                                }
                                                
$runtime->setVariableValue($variableName, $variableValue);
-                                               $expected = array( ';', '.', 
'+', '-', '*', '/', '%', '&', '|', '^', T_SL, T_SR ); // same as for case 
T_LNUMBER:
+                                               $expected = 
self::$arrayOperators;
                                                if($expectListParams){
                                                        $expected[] = ',';
                                                }
@@ -135,7 +167,7 @@
                                case T_DNUMBER:
                                        $runtime->addParam( (float)$text );
                                        break;
-                               case T_ENCAPSED_AND_WHITESPACE:
+                               case T_ENCAPSED_AND_WHITESPACE: // " $a"
                                        if( $expectQuotesClose ) {
                                                $runtime->addOperator('.');
                                        }
@@ -159,7 +191,7 @@
                                                if( $expectCurlyClose ) {
                                                        $expected = array( '}' 
);
                                                } else {
-                                                       $expected = array( ';', 
'.', '+', '-', '*', '/', '%', '&', '|', '^', T_SL, T_SR, 
T_ENCAPSED_AND_WHITESPACE );
+                                                       $expected = 
self::$arrayOperators;
                                                        if( $variableName !== 
false ){
                                                                $expected[] = 
T_INC;
                                                                $expected[] = 
T_DEC;
@@ -234,7 +266,7 @@
                                case T_CONSTANT_ENCAPSED_STRING:
                                case T_LNUMBER:
                                case T_DNUMBER:
-                                       $expected = array( ';', '.', '+', '-', 
'*', '/', '%', '&', '|', '^', T_SL, T_SR ); // same as for case T_INC:
+                                       $expected = self::$arrayOperators;
                                        if($expectListParams){
                                                $expected[] = ',';
                                        }
@@ -269,6 +301,14 @@
                                case '^':
                                case T_SL: // <<
                                case T_SR: // >>
+                               case '<':
+                               case '>':
+                               case T_IS_SMALLER_OR_EQUAL: // <=
+                               case T_IS_GREATER_OR_EQUAL: // >=
+                               case T_IS_EQUAL: // ==
+                               case T_IS_NOT_EQUAL: // !=
+                               case T_IS_IDENTICAL: // ===
+                               case T_IS_NOT_IDENTICAL: // !==
                                        $expected = array(
                                                T_CONSTANT_ENCAPSED_STRING, // 
"foo" or 'bar'
                                                T_ENCAPSED_AND_WHITESPACE, // " 
$a"
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 0804481..3ac793a 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -30,8 +30,10 @@
                array('+', '-', '.'),
                //              <<      >>
                array(T_SL, T_SR),
-               array('<', '<=', '>', '>='),
-               array('==', '!=', '===', '!==', '<>'),
+               //                                              <=              
                                >=
+               array('<', '>', T_IS_SMALLER_OR_EQUAL, T_IS_GREATER_OR_EQUAL),
+               //              ==                              !=              
                ===                             !==
+               array(T_IS_EQUAL, T_IS_NOT_EQUAL, T_IS_IDENTICAL, 
T_IS_NOT_IDENTICAL),
                array('&'),
                array('^'),
                array('|'),
@@ -208,6 +210,30 @@
                        case T_BOOL_CAST:
                                $this->lastParam = (bool) $this->lastParam;
                                break;
+                       case '<':
+                               $this->lastParam = $param < $this->lastParam;
+                               break;
+                       case '>':
+                               $this->lastParam = $param > $this->lastParam;
+                               break;
+                       case T_IS_SMALLER_OR_EQUAL: // <=
+                               $this->lastParam = $param <= $this->lastParam;
+                               break;
+                       case T_IS_GREATER_OR_EQUAL: // >=
+                               $this->lastParam = $param >= $this->lastParam;
+                               break;
+                       case T_IS_EQUAL: // ==
+                               $this->lastParam = $param == $this->lastParam;
+                               break;
+                       case T_IS_NOT_EQUAL: // !=
+                               $this->lastParam = $param != $this->lastParam;
+                               break;
+                       case T_IS_IDENTICAL: // ===
+                               $this->lastParam = $param === $this->lastParam;
+                               break;
+                       case T_IS_NOT_IDENTICAL: // !==
+                               $this->lastParam = $param !== $this->lastParam;
+                               break;
                        default:
                                \MWDebug::log( __METHOD__ . " unknown operator 
'$operator'" );
                                break;
diff --git a/tests/phpunit/includes/InterpreterTest.php 
b/tests/phpunit/includes/InterpreterTest.php
index fd99a6b..921b960 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -428,4 +428,90 @@
                                '5'
                                );
        }
+
+       public function testRun_echo_compare_1() {
+               $this->assertEquals(
+                               Interpreter::run('echo 5 == 5;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_2() {
+               $this->assertEquals(
+                               Interpreter::run('echo 5 == 3+2;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_3() {
+               $this->assertEquals(
+                               Interpreter::run('echo -3 + 8 == 3 + 2;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_4() {
+               $this->assertEquals(
+                               Interpreter::run('echo -3 * -8 > 3 + 8;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_5() {
+               $this->assertEquals(
+                               Interpreter::run('echo -3 * 8 < 3 + 8;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_6() {
+               $this->assertEquals(
+                               Interpreter::run('echo 3 === (int)"3";'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_7() {
+               $this->assertEquals(
+                               Interpreter::run('echo 0 == "a";'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_8() {
+               $this->assertEquals(
+                               Interpreter::run('echo "1" == "01";'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_9() {
+               $this->assertEquals(
+                               Interpreter::run('echo "10" == "1e1";'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_10() {
+               $this->assertEquals(
+                               Interpreter::run('echo 100 == "1e2";'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_11() {
+               $this->assertEquals(
+                               Interpreter::run('$foo = 4; echo $foo != 
$foo*2;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_12() {
+               $this->assertEquals(
+                               Interpreter::run('echo $foo <= $foo*2;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_13() {
+               $this->assertEquals(
+                               Interpreter::run('echo $foo*4 >= $foo*2;'),
+                               '1'
+                               );
+       }
+       public function testRun_echo_compare_14() {
+               $this->assertEquals(
+                               Interpreter::run('echo 5 !== (string)5;'),
+                               '1'
+                               );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0f5f30e0652d28df6d695f5bf2c6f14aedd6353b
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