Pastakhov has uploaded a new change for review. https://gerrit.wikimedia.org/r/58864
Change subject: add if construct without block (version 0.0.10) ...................................................................... add if construct without block (version 0.0.10) example: if ( true ) echo "hello"; if ( false ) echo "be-be-be"; if ( (5+5)*10 ) echo " world"; ---------------------------- hello world Change-Id: I26b3f996abc2503fc2a3433cd87a53bc43c788d3 --- M Foxway.php M includes/Interpreter.php M includes/Runtime.php M tests/phpunit/includes/InterpreterTest.php 4 files changed, 93 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway refs/changes/64/58864/1 diff --git a/Foxway.php b/Foxway.php index 4ece886..5b10b48 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.9' ); +define( 'Foxway_VERSION' , '0.0.10' ); // Register this extension on Special:Version $wgExtensionCredits['parserhook'][] = array( diff --git a/includes/Interpreter.php b/includes/Interpreter.php index 7e69297..dc1991f 100644 --- a/includes/Interpreter.php +++ b/includes/Interpreter.php @@ -55,6 +55,7 @@ $incrementVariable = false; $variableName = null; $variableValue = null; + $commandResult = null; $line = 1; $runtime = new Runtime(); @@ -76,7 +77,8 @@ switch ($id) { case ';': - $return .= $runtime->getCommandResult($debug); + // TODO: check parenthess level??? + $commandResult = $runtime->getCommandResult($debug); break; case ',': $runtime->separateParams(); @@ -87,7 +89,9 @@ break; case ')': $parenthesesLevel--; - $runtime->parenthesesClose(); + if( $runtime->parenthesesClose() ) { + $commandResult = $runtime->getCommandResult($debug); + } if( $parenthesesLevel == 0 ) { unset($expected[array_search(')', $expected)]); } @@ -135,7 +139,7 @@ break; case '?': if( $runtime->getMathResult() ) { // true - $expectTernarySeparators++; // just go further + $expectTernarySeparators++; // just go next } else { // false, to skip to the ternary operator separator $tmp_skip = 0; // it to parse the syntax of nested ternary operators $skipedTokens = 0; // it for debug messages and check correct syntax @@ -155,7 +159,7 @@ if( $tmp_skip > 0 ) { // were the embedded ternary operator? $tmp_skip--; } else { /************************ EXIT HERE ***********************************/ - break 2; // found the required separator, we go from here and just go further + break 2; // found the required separator, we go from here and just go next } /************************ EXIT HERE ***********************************/ break; case T_WHITESPACE: @@ -181,7 +185,7 @@ } $id = ':'; next($tokens); - //just go further + //just go next } break; case ':': @@ -254,13 +258,16 @@ } } break; + case T_IF: + $expected = array('('); + // break is not necessary here case T_ECHO: if($is_debug) { $i = array_push($debug, $text)-1; } else { $i = false; } - $runtime->addCommand('echo', $i); + $runtime->addCommand($id, $i); break; case T_CONSTANT_ENCAPSED_STRING: $is_apostrophe = substr($text, 0, 1) == '\'' ? true : false; @@ -364,6 +371,44 @@ $incrementVariable = false; } + /***************** COMMAND RESULT *******************************/ + if( !is_null($commandResult) ) { + if( is_string($commandResult) ) { + $return .= $commandResult; + }elseif( is_array($commandResult) ) { + list($command, $result) = $commandResult; + switch ($command) { + case T_ECHO: + $return .= $result; + break; + case T_IF: + if( $result ) { + // just go next + $expected = false; + } else { + // skip next statement + $token = current($tokens); + do { + if( $token == ';' ) { + break; + } + } while( $token = next($tokens) ); + if($is_debug) { + $debug[] = ')<span style="color:#969696"> ... </span>'; + } + if( $token === false ) { + $return .= $return .= '<br><span class="error" title="' . __LINE__ . '">' . wfMessage( 'foxway-php-syntax-error-unexpected', '$end', $line )->escaped() . '</span>'; + break 2; + } + $expected = array(';'); + continue 2; + } + break; + } + } + $commandResult = null; + } + /***************** EXPECT *************************************/ switch ($id) { @@ -395,6 +440,11 @@ break; case T_ECHO: $expectListParams = true; + // break is not necessary here + case '(': // TODO: remove $id == '(' && + if( $id == '(' && $expected != array('(') ) { + break; + } // break is not necessary here case ',': case '=': @@ -480,7 +530,7 @@ break; } - // Debug info + /***************** DEBUG INFO *********************************/ if($is_debug) { switch ($id) { case T_COMMENT: @@ -509,6 +559,7 @@ $debug[] = '<span style="color:#0000E6" title="'. token_name($id) . '">' . htmlspecialchars($text) . '</span>'; break; case T_ECHO: + case T_IF: case T_VARIABLE: break; case '?': diff --git a/includes/Runtime.php b/includes/Runtime.php index 7340f34..51a764b 100644 --- a/includes/Runtime.php +++ b/includes/Runtime.php @@ -256,10 +256,16 @@ switch ($this->lastCommand) { case false: // ++$variable OR --$variable; break; - case 'echo': - $return = implode('', $this->listParams); + case T_ECHO: + $return = array( T_ECHO, implode('', $this->listParams) ); if( $this->lastDebug !== false ) { $debug[$this->lastDebug] = '<span style="color:#0000E6" title="'. token_name(T_ECHO) . ' do ' . htmlspecialchars($return) . '">' . $debug[$this->lastDebug] . '</span>'; + } + break; + case T_IF: + $return = array( T_IF, $this->lastParam ); + if( $this->lastDebug !== false ) { + $debug[$this->lastDebug] = '<span style="color:#0000E6" title="'. ($this->lastParam ? 'TRUE' : 'FALSE') . '">' . $debug[$this->lastDebug] . '</span>'; } break; default: @@ -358,5 +364,8 @@ public function parenthesesClose() { $this->doMath(); $this->popStack(); + if( !is_null($this->lastCommand) && $this->lastCommand != T_ECHO && substr($this->lastCommand, 0, 1) != '$') { + return $this->lastCommand; + } } } diff --git a/tests/phpunit/includes/InterpreterTest.php b/tests/phpunit/includes/InterpreterTest.php index 7cbba63..eac89b8 100644 --- a/tests/phpunit/includes/InterpreterTest.php +++ b/tests/phpunit/includes/InterpreterTest.php @@ -601,4 +601,27 @@ ); } + public function testRun_echo_if_simple_1() { + $this->assertEquals( + Interpreter::run('if ( true ) echo "hello";'), + 'hello' + ); + } + public function testRun_echo_if_simple_2() { + $this->assertEquals( + Interpreter::run('if ( false ) echo "hello";'), + '' + ); + } + public function testRun_echo_if_simple_3() { + $this->assertEquals( + Interpreter::run(' +if ( 5+5 ) echo "hello"; +if ( 5-5 ) echo " === FALSE === "; +if ( (5+5)/4 ) echo "world"; +if ( -5+5 ) echo " === FALSE === "; +if ( ((74+4)*(4+6)+88)*4 ) echo "!!!";'), + 'helloworld!!!' + ); + } } -- To view, visit https://gerrit.wikimedia.org/r/58864 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I26b3f996abc2503fc2a3433cd87a53bc43c788d3 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Foxway Gerrit-Branch: master Gerrit-Owner: Pastakhov <pastak...@yandex.ru> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits