Pastakhov has uploaded a new change for review.

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


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, 56 insertions(+), 14 deletions(-)


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

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..5128524 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -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>';
@@ -417,6 +418,7 @@
                                                                        
$blocks[$elseIndex][FOX_VALUE] = false;
                                                                }
                                                                $expected = 
false;
+                                                               \MWDebug::log( 
"COMMAND RESULT " . var_export($blocks, true) );
                                                                // just go next
                                                        } else {
                                                                // skip next 
statement
@@ -436,20 +438,31 @@
                                                                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 +635,7 @@
                                                break;
                                        case T_ECHO:
                                        case T_IF:
+                                       case T_ELSEIF:
                                        case T_VARIABLE:
                                                break;
                                        case '?':
@@ -688,10 +702,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,9 +729,20 @@
                                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];
+                                       \MWDebug::log( 'T_ELSEIF ' . 
var_export($blocks[$ifIndex], true));
+                                       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
diff --git a/tests/phpunit/includes/InterpreterTest.php 
b/tests/phpunit/includes/InterpreterTest.php
index 54e966d..c0128ae 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -744,6 +744,25 @@
                                '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'
+                               );
+       }
+
 
 
 }

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

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