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

Change subject: Add elseif construct (version 0.0.11)
......................................................................


Add elseif construct (version 0.0.11)

Change-Id: I980945f0029ab661399194277a74a1a9e2535347
---
M Foxway.php
M includes/Interpreter.php
M tests/phpunit/includes/InterpreterTest.php
3 files changed, 74 insertions(+), 24 deletions(-)

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



diff --git a/Foxway.php b/Foxway.php
index 5b10b48..ff83034 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.10' );
+define( 'Foxway_VERSION' , '0.0.11' );
 
 // Register this extension on Special:Version
 $wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Interpreter.php b/includes/Interpreter.php
index ab28df0..d3e3040 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -51,8 +51,8 @@
 
        public static function run($source, $is_debug = false) {
                $tokens = token_get_all("<?php $source ?>");
-               \MWDebug::log( "\$tokens WHILE " . var_export($tokens,true) );
-               
+               //\MWDebug::log( "\$tokens is " . var_export($tokens,true) );
+
                $return = "";
                $debug = array();
                $blocks = array();
@@ -80,7 +80,7 @@
                                list($id, $text, $line) = $token;
                        }
 
-                       \MWDebug::log( "$index WHILE " . 
var_export($token,true) );
+                       //\MWDebug::log( "$index WHILE " . 
var_export($token,true) );
 
                        if( $expected && in_array($id, self::$skipTokenIds) === 
false && in_array($id, $expected) === false) {
                                $id_str = is_string($id) ? "' $id '" : 
token_name($id);
@@ -271,8 +271,9 @@
                                        }
                                        break;
                                case T_ELSE:
+                               case T_ELSEIF:
                                        if( isset($blocks[$index]) ) {
-                                               $commandResult = array(T_ELSE, 
$blocks[$index][FOX_VALUE]);
+                                               $commandResult = array($id, 
$blocks[$index][FOX_VALUE]);
                                        } else {
                                                $id_str = is_string($id) ? "' 
$id '" : token_name($id);
                                                $return .= '<br><span 
class="error" title="' . __LINE__ . '">' . wfMessage( 
'foxway-php-syntax-error-unexpected', $id_str, $line )->escaped() . '</span>';
@@ -431,25 +432,35 @@
                                                                        
$expected = false;
                                                                }
                                                                $index = 
$curBlock[FOX_ENDBLOCK];
-                                                               
//\MWDebug::log( var_export($tokens[$endBlockIndex], true) );
                                                                $commandResult 
= null;
                                                                continue 2;
                                                        }
                                                        break;
+                                               case T_ELSEIF:
+                                                       if( $result ) { // chek 
for IF
+                                                               // this code 
from previus swith( $id ) case T_IF: & case T_ECHO:
+                                                               $IfIndex = 
$index;
+                                                               $expected = 
array('(');
+                                                               if($is_debug) {
+                                                                       $i = 
array_push($debug, $text)-1;
+                                                               } else {
+                                                                       $i = 
false;
+                                                               }
+                                                               
$runtime->addCommand(T_IF, $i);
+                                                               break;
+                                                       }
+                                                       // break is not 
necessary here
                                                case T_ELSE:
-                                                       if( $result ) {
-                                                               //$expected = 
false;
-                                                               // just go next
-                                                       } else {
+                                                       if( $result == false ) 
{ // for true just go next
                                                                // skip next 
statement
                                                                // find end of 
block
-                                                               if( 
!isset($blocks[$index][FOX_ENDBLOCK]) ) {
+                                                               if( 
!isset($blocks[$index][FOX_ENDIF]) ) {
                                                                        if( 
self::findIfElseIndexes($tokens, $blocks, $index, $index+1) !== true ) {
                                                                                
$return .= '<br><span class="error" title="' . __LINE__ . '">' . wfMessage( 
'foxway-php-syntax-error-unexpected', '$end', $line )->escaped() . '</span>';
                                                                                
break 2;
                                                                        }
                                                                }
-                                                               $index = 
$blocks[$index][FOX_ENDBLOCK];
+                                                               $index = 
$blocks[$index][FOX_ENDIF];
                                                                $commandResult 
= null;
                                                                //$expected = 
false;
                                                        }
@@ -622,6 +633,7 @@
                                                break;
                                        case T_ECHO:
                                        case T_IF:
+                                       case T_ELSEIF:
                                        case T_VARIABLE:
                                                break;
                                        case '?':
@@ -688,10 +700,8 @@
                                        break;
                                case T_IF:
                                        if( $nestedBlocks == 0 ) {
-                                               if( 
!isset($blocks[$i][FOX_ENDIF]) ) {
-                                                       if( 
self::findIfElseIndexes($tokens, $blocks, $i, 
self::findLastParenthesis($tokens, $i)) !== true ) {
-                                                               return false;
-                                                       }
+                                               if( 
!isset($blocks[$i][FOX_ENDIF]) && self::findIfElseIndexes($tokens, $blocks, $i, 
self::findLastParenthesis($tokens, $i)) !== true ) {
+                                                       return false;
                                                }
                                                $i = $blocks[$i][FOX_ENDIF];
                                                break 2;
@@ -717,25 +727,30 @@
                                case T_DOC_COMMENT:
                                case T_WHITESPACE:
                                        break; // ignore it
-                               //case T_ELSEIF:
+                               case T_ELSEIF:
+                                       if( !isset($blocks[$i]) && 
self::findIfElseIndexes($tokens, $blocks, $i, 
self::findLastParenthesis($tokens, $i)) !== true ) {
+                                               return false;
+                                       }
+                                       $blocks[$ifIndex][FOX_ELSE] = $i; // We 
fount T_ELSE or T_ELSEIF
+                                       if( isset($blocks[$i][FOX_ENDIF]) ) {
+                                               $blocks[$ifIndex][FOX_ENDIF] = 
$blocks[$i][FOX_ENDIF];
+                                               break 2;
+                                       }
+                                       $i = $blocks[$i][FOX_ENDBLOCK];
+                                       break;
                                case T_ELSE:
-                                       if( self::findIfElseIndexes($tokens, 
$blocks, $i, $i+1) !== true ) {
+                                       if( !isset($blocks[$i]) && 
self::findIfElseIndexes($tokens, $blocks, $i, $i+1) !== true ) {
                                                return false;
                                        }
                                        $blocks[$ifIndex][FOX_ELSE] = $i; // We 
fount T_ELSE or T_ELSEIF
                                        $blocks[$ifIndex][FOX_ENDIF] = 
$blocks[$i][FOX_ENDBLOCK];
-                                       \MWDebug::log( 'T_ELSE ' . 
var_export($blocks[$ifIndex], true));
                                        break 2; //              Exit
                                default: // ELSE not exists
                                        $blocks[$ifIndex][FOX_ELSE] = false;
                                        $blocks[$ifIndex][FOX_ENDIF] = 
$blocks[$ifIndex][FOX_ENDBLOCK];
-                                       //$blocks[$ifIndex][FOX_ENDIF] = 
$blocks[$index][FOX_ENDBLOCK];
-                                       \MWDebug::log( 'default ' . 
var_export($blocks[$ifIndex], true));
                                        break 2;
                        }
                }
-               //$blocks[$ifIndex][FOX_ELSE] = $else;
-               \MWDebug::log( "function findIfElseIndexes(\$tokens, $ifIndex, 
$index) @ count = $count, i = $i, " . var_export($blocks[$ifIndex], true) );
                return true;
        }
 
diff --git a/tests/phpunit/includes/InterpreterTest.php 
b/tests/phpunit/includes/InterpreterTest.php
index 54e966d..96c8a3f 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -744,6 +744,41 @@
                                'false'
                                );
        }
-
+       public function testRun_echo_elseif_1() {
+               $this->assertEquals(
+                               Interpreter::run('if( true ) echo "one"; 
elseif( true ) echo "two"; else echo "three";'),
+                               'one'
+                               );
+       }
+       public function testRun_echo_elseif_2() {
+               $this->assertEquals(
+                               Interpreter::run('if( false ) echo "one"; 
elseif( true ) echo "two"; else echo "three";'),
+                               'two'
+                               );
+       }
+       public function testRun_echo_elseif_3() {
+               $this->assertEquals(
+                               Interpreter::run('if( false ) echo "one"; 
elseif( false ) echo "two"; else echo "three";'),
+                               'three'
+                               );
+       }
+       public function testRun_echo_elseif_4() {
+               $this->assertEquals(
+                               Interpreter::run('if( true ) { echo "*"; echo 
"one"; } elseif( true ) { echo "*"; echo "two"; } else { echo "*"; echo 
"three"; }'),
+                               '*one'
+                               );
+       }
+       public function testRun_echo_elseif_5() {
+               $this->assertEquals(
+                               Interpreter::run('if( false ) { echo "*"; echo 
"one"; } elseif( true ) { echo "*"; echo "two"; } else { echo "*"; echo 
"three"; }'),
+                               '*two'
+                               );
+       }
+       public function testRun_echo_elseif_6() {
+               $this->assertEquals(
+                               Interpreter::run('if( false ) { echo "*"; echo 
"one"; } elseif( false ) { echo "*"; echo "two"; } else { echo "*"; echo 
"three"; }'),
+                               '*three'
+                               );
+       }
 
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I980945f0029ab661399194277a74a1a9e2535347
Gerrit-PatchSet: 2
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