Pastakhov has uploaded a new change for review.
https://gerrit.wikimedia.org/r/59814
Change subject: Refactoring (version 0.1.0)
......................................................................
Refactoring (version 0.1.0)
Patch Set 1: phase 1, move all operations to class Runtime
Change-Id: I9745c8908e43c372f64519e1ad781bfb30fe6fa1
---
M Foxway.php
M includes/Interpreter.php
M includes/Runtime.php
M tests/phpunit/includes/InterpreterTest.php
4 files changed, 255 insertions(+), 274 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway
refs/changes/14/59814/1
diff --git a/Foxway.php b/Foxway.php
index ff83034..24c4479 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.11' );
+define( 'Foxway_VERSION' , '0.1.0' );
// Register this extension on Special:Version
$wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Interpreter.php b/includes/Interpreter.php
index da34abd..1608bff 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -58,10 +58,11 @@
$blocks = array();
$expected = false;
$expectListParams = false;
+ $expectVoidParams = false;
$expectCurlyClose = false;
$expectQuotesClose = false;
$expectTernarySeparators = 0;
- $parenthesesLevel = 0;
+ $currentParenthesesLevel = null;
$curlyLever = 0;
$IfIndex = false;
$incrementVariable = false;
@@ -69,7 +70,10 @@
$variableValue = null;
$commandResult = null;
$line = 1;
+
$runtime = new Runtime();
+ $operators = $runtime->getOperators();
+ $operators[] = '(';
$countTokens = count($tokens);
for( $index = 0; $index < $countTokens; $index++ ){
@@ -82,76 +86,26 @@
//\MWDebug::log( "$index WHILE " .
var_export($token,true) );
- if( $expected && in_array($id, self::$skipTokenIds) ===
false && in_array($id, $expected) === false) {
+ /*if( $expected && in_array($id, self::$skipTokenIds)
=== false && in_array($id, $expected) === false) {
$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>';
break;
- }
+ }*/
switch ($id) {
case ';':
// TODO: check parenthess level???
$commandResult =
$runtime->getCommandResult($debug);
break;
- case ',':
- $runtime->separateParams();
- break;
- case '(':
- $parenthesesLevel++;
- $runtime->parenthesesOpen();
- break;
case ')':
- $parenthesesLevel--;
- if( $runtime->parenthesesClose() ) {
- $commandResult =
$runtime->getCommandResult($debug);
+ $result = $runtime->addOperator( $id );
+ if(is_array($result) ) {
+ $commandResult = $result;
}
- if( $parenthesesLevel == 0 ) {
-
unset($expected[array_search(')', $expected)]);
- }
- break;
- case '=':
- case T_CONCAT_EQUAL: // .=
- case T_PLUS_EQUAL: // +=
- case T_MINUS_EQUAL: // -=
- case T_MUL_EQUAL: // *=
- case T_DIV_EQUAL: // /=
- case T_MOD_EQUAL: // %=
- case T_AND_EQUAL: // &=
- case T_OR_EQUAL: // |=
- case T_XOR_EQUAL: // ^=
- case T_SL_EQUAL: // <<
- case T_SR_EQUAL: // >>
- $runtime->setVariableOperator( $id );
- break;
- case '.':
- case '+':
- case '-':
- case '*':
- case '/':
- case '%':
- case '&':
- case '|':
- 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)
- 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 );
+ \MWDebug::log( ') @ ' .
var_export($result, true) );
break;
case '?':
- if( $runtime->getMathResult() ) { //
true
+ if( $runtime->addOperator( $id ) ) { //
true
$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
@@ -251,25 +205,6 @@
// If we are here, then we do not
expect to find separator ternary
$return .= '<br><span class="error"
title="' . __LINE__ . '">' . wfMessage( 'foxway-php-syntax-error-unexpected',
$id, $line )->escaped() . '</span>';
break 2;
- case T_INC: // ++
- case T_DEC: // --
- if( $incrementVariable === false ) {
- $incrementVariable = $id;
- $expected = array( T_VARIABLE );
- } else {
- $variableValue =
$runtime->getVariableValue( $variableName );
- if( $id == T_INC ) {
- $variableValue++;
- } else {
- $variableValue--;
- }
-
$runtime->setVariableValue($variableName, $variableValue);
- $expected =
self::$arrayOperators;
- if($expectListParams &&
$parenthesesLevel == 0){
- $expected[] = ',';
- }
- }
- break;
case T_ELSE:
case T_ELSEIF:
if( isset($blocks[$index]) ) {
@@ -282,6 +217,8 @@
break;
case T_IF:
$IfIndex = $index;
+ // break is not necessary here
+ case T_ARRAY:
$expected = array('(');
// break is not necessary here
case T_ECHO:
@@ -310,74 +247,12 @@
$runtime->addParam(
self::process_slashes($text, false) );
break;
case T_VARIABLE:
- if( $expected && in_array(T_VARIABLE,
$expected) ) {
- $variableName = substr($text,
1);
- $variableValue =
$runtime->getVariableValue( $variableName );
- if( $incrementVariable == T_INC
|| $incrementVariable == T_DEC ) {
- if( $incrementVariable
== T_INC ) {
-
$variableValue++;
- } else {
-
$variableValue--;
- }
-
$runtime->setVariableValue( $variableName, $variableValue);
- $variableName = false;
- } else {
- $incrementVariable =
$variableName;
- }
- if( $expectCurlyClose ) {
- $expected = array( '}'
);
- } else {
- $expected =
self::$arrayOperators;
- if( $variableName !==
false ){
- $expected[] =
T_INC;
- $expected[] =
T_DEC;
- }
- if( $parenthesesLevel )
{
- $expected[] =
')';
- }
- }
- if( $expectListParams &&
$parenthesesLevel == 0 ) {
- $expected[] = ',';
- }
- if( $expectQuotesClose ) {
- $expected[] =
T_VARIABLE; //echo "$s$s";
- $expected[] = '"';
-
$runtime->addOperator('.');
- }
-
$runtime->addParam($variableValue);
- if($is_debug) {
- if(
is_null($variableValue) ) {
- $debug[] =
'<span style="color:red" title="'.token_name($id).' = '.htmlspecialchars(
var_export($variableValue, true) ).'">' . $text . '</span>';
- } else {
- $debug[] =
'<span style="color:#6D3206" title="'.token_name($id).' = '.htmlspecialchars(
var_export($variableValue, true) ).'">' . $text . '</span>';
- }
- }
- } else {
- if($is_debug) {
- $i = array_push($debug,
$text)-1;
- } else {
- $i = false;
- }
- $variableName = substr($text,
1);
- $runtime->setVariable(
$variableName, $i );
- $incrementVariable =
$variableName;
- $expected = array(
- '=',
- T_CONCAT_EQUAL,
- T_PLUS_EQUAL,
- T_MINUS_EQUAL,
- T_MUL_EQUAL,
- T_DIV_EQUAL,
- T_MOD_EQUAL,
- T_AND_EQUAL,
- T_OR_EQUAL,
- T_XOR_EQUAL,
- T_SL_EQUAL,
- T_SR_EQUAL,
- T_INC,
- T_DEC,
- );
+ if( $expectQuotesClose ) {
+ $expected[] = T_VARIABLE;
//echo "$s$s";
+ $expected[] = '"';
+ $runtime->addOperator('.');
}
+ $runtime->addParam( "\0$text" );
break;
case T_STRING:
if( strcasecmp($text, 'true') == 0 ) {
@@ -387,6 +262,11 @@
} else {
$return .= '<br><span
class="error" title="' . __LINE__ . '">' . wfMessage(
'foxway-php-syntax-error-unexpected', "'$text'", $line )->escaped() . '</span>';
break 2;
+ }
+ break;
+ default:
+ if( in_array($id, $operators) ) {
+ $runtime->addOperator($id);
}
break;
}
@@ -470,8 +350,7 @@
$commandResult = null;
}
- /***************** EXPECT
*************************************/
-
+ /***************** EXPECT PHASE ONE
**************************/
switch ($id) {
case ';':
case T_ELSE:
@@ -493,21 +372,19 @@
case T_DNUMBER:
case T_STRING:
$expected = self::$arrayOperators;
- if($expectListParams &&
$parenthesesLevel == 0){
+ if($expectListParams &&
$currentParenthesesLevel == 0){
$expected[] = ',';
}
- if( $parenthesesLevel ) {
+ if( $currentParenthesesLevel ) {
$expected[] = ')';
}
break;
- case T_ECHO:
- $expectListParams = true;
- // break is not necessary here
- case '(': // TODO: remove $id == '(' &&
- if( $id == '(' && $expected !=
array('(') ) {
+ case '(':
+ if( $expected != array('(') ) {
break;
}
// break is not necessary here
+ case T_ECHO:
case ',':
case '=':
case T_CONCAT_EQUAL: // .=
@@ -562,6 +439,7 @@
T_STRING_CAST, // (string)
T_ARRAY_CAST, // (array)
T_BOOL_CAST, // (bool)
+ T_ARRAY, // array()
);
break;
case T_CURLY_OPEN:
@@ -597,6 +475,21 @@
break;
}
+ /***************** EXPECT PHASE TWO
**************************/
+ switch ($id) {
+ case T_ARRAY:
+ $expectVoidParams = true;
+ // break is not necessary here
+ case T_ECHO:
+ $expectListParams = true;
+ break;
+ case '(':
+ if( $expectVoidParams ) {
+ $expected[] = ')';
+ }
+ break;
+ }
+
/***************** DEBUG INFO
*********************************/
if($is_debug) {
switch ($id) {
@@ -623,6 +516,7 @@
case T_STRING_CAST: // (string)
case T_ARRAY_CAST: // (array)
case T_BOOL_CAST: // (bool)
+ case T_ARRAY: // array()
$debug[] = '<span
style="color:#0000E6" title="'. token_name($id) . '">' .
htmlspecialchars($text) . '</span>';
break;
case T_ELSEIF:
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 8e51681..2828a5c 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -22,9 +22,10 @@
private static $variables = array();
// @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),
+ private static $operatorsPrecedence = array(
+ //array('['),
+ // ++ -- (int)
(float) (string) (array) (bool)
+ array(T_INC, T_DEC, '~', T_INT_CAST, T_DOUBLE_CAST,
T_STRING_CAST, T_ARRAY_CAST, T_BOOL_CAST),
array('!'),
array('*', '/', '%'),
array('+', '-', '.'),
@@ -39,11 +40,28 @@
array('|'),
array('&&'),
array('||'),
+ array('?', ':'),
+ // += -=
*= /= .=
%= &=
|= ^= <<= >>=
=>
+ array('=', T_PLUS_EQUAL, T_MINUS_EQUAL, T_MUL_EQUAL,
T_DIV_EQUAL, T_CONCAT_EQUAL, T_MOD_EQUAL, T_AND_EQUAL, T_OR_EQUAL, T_XOR_EQUAL,
T_SL_EQUAL, T_SR_EQUAL, T_DOUBLE_ARROW),
+ array(T_LOGICAL_AND), // and
+ array(T_LOGICAL_XOR), // xor
+ array(T_LOGICAL_OR), // or
+ array(','),
);
+
+ public function getOperators() {
+ static $operators = array();
+ if( count($operators) == 0 ) {
+ foreach (self::$operatorsPrecedence as $value) {
+ $operators = array_merge($operators, $value);
+ }
+ }
+ return $operators;
+ }
private function getOperatorPrecedence( $operator ) {
$precendence = 0;
- foreach ($this->operatorsPrecedence as $operators) {
+ foreach (self::$operatorsPrecedence as $operators) {
if( in_array($operator, $operators) ) {
break;
}
@@ -55,6 +73,7 @@
private function pushStack() {
$this->stack[] = array($this->lastCommand, $this->lastDebug,
$this->listParams, $this->lastOperator, $this->variableOperator,
$this->mathMemory);
$this->resetRegisters();
+ \MWDebug::log( 'function pushStack() @ stack = ' .
var_export($this->stack, true) );
}
private function popStack() {
@@ -63,6 +82,7 @@
} else {
list($this->lastCommand, $this->lastDebug,
$this->listParams, $this->lastOperator, $this->variableOperator,
$this->mathMemory) = array_pop($this->stack);
}
+ \MWDebug::log( 'function popStack() @ stack = ' .
var_export($this->stack, true) );
}
private function resetRegisters() {
@@ -75,8 +95,6 @@
$this->mathMemory = array();
}
-
-
public function addCommand( $name, $debug ) {
if( $this->lastCommand ) {
$this->pushStack();
@@ -86,58 +104,128 @@
}
public function addParam( $param ) {
-//\MWDebug::log( "function addParam( '$param' ) @ lastOperator=" .
var_export($this->lastOperator, true) );
+\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->lastOperator = false;
- } elseif( !is_null($this->lastParam) ) {
- $this->listParams[] = $this->lastParam; // TODO:it
seems that this is unnecessary
}
$this->lastParam = $param;
$this->doMath(0);
-//\MWDebug::log("lastParam = $this->lastParam, lastOperator =
$this->lastOperator, mathMemory = " . var_export($this->mathMemory, true) );
+\MWDebug::log("END function addParam lastParam = $this->lastParam,
lastOperator = $this->lastOperator, mathMemory = " .
var_export($this->mathMemory, true) );
}
- public function separateParams() {
+ /*public function separateParams() {
//\MWDebug::log("function separateParams()");
$this->doMath();
$this->listParams[] = $this->lastParam;
$this->lastParam = null;
+ }*/
+
+ public function parenthesesOpen() {
+ if( $this->lastOperator ) {
+ $precedence = $this->getOperatorPrecedence(
$this->lastOperator );
+ $this->mathMemory[$precedence] =
array($this->lastOperator, $this->lastParam);
+ $this->lastOperator = false;
+ }
+ $this->pushStack();
}
- public function addOperator( $operator ) {
- $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
- $this->doMath($precedence);
- $this->lastOperator = $operator;
+ public function parenthesesClose() {
+ $this->doMath();
+ $this->popStack();
+ if( !is_null($this->lastCommand) && $this->lastCommand !=
T_ECHO ) {
+ return $this->lastCommand;
}
}
- private function doMath( $precedence = 11 ) { //11 =
count($operatorsPrecedence)-1
+ public function addOperator( $operator ) {
+\MWDebug::log("function addOperator( $operator ) @ lastOperator = '" .
var_export($this->lastOperator, true) . "', lastParam = '" .
var_export($this->lastParam, true) );
+ switch ($operator) {
+ case '(':
+ $this->parenthesesOpen();
+ break;
+ case ')':
+ $this->parenthesesClose();
+ if( !is_null($this->lastCommand) &&
$this->lastCommand != T_ECHO ) {
+ return array( $this->lastCommand,
$this->lastParam);
+ }
+ break;
+ default:
+ $precedence = $this->getOperatorPrecedence(
$operator );
+ //
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;
+ case T_INC: // TODO
+ if( $this->lastOperator
|| is_null($this->lastParam) ) {
+ if(
!isset($this->mathMemory[0]) ) {
+
$this->mathMemory[0] = array();
+ }
+
$this->mathMemory[0][] = $operator;
+ \MWDebug::log(
'mathMemory = ' .var_export($this->mathMemory, true));
+ } else {
+
$variableLastParam = false;
+ if(
substr($this->lastParam, 0, 2) == "\0$" ) {
+
$variableLastParam = substr($this->lastParam, 2);
+ if(
!isset(self::$variables[$variableLastParam]) ) {
+
\MWDebug::log( "USE not init variable **************************
$variableLastParam");
+ }
+
$this->lastParam = self::$variables[$variableLastParam];
+
self::$variables[$variableLastParam]++;
+
\MWDebug::log( 'variables ' .var_export(self::$variables, true) );
+ }
+ }
+ break;
+ case T_DEC: // TODO
+ if( $this->lastOperator
|| is_null($this->lastParam) ) {
+ if(
!isset($this->mathMemory[0]) ) {
+
$this->mathMemory[0] = array();
+ }
+
$this->mathMemory[0][] = $operator;
+ \MWDebug::log(
'mathMemory = ' .var_export($this->mathMemory, true));
+ } else {
+
$variableLastParam = false;
+ if(
substr($this->lastParam, 0, 2) == "\0$" ) {
+
$variableLastParam = substr($this->lastParam, 2);
+ if(
!isset(self::$variables[$variableLastParam]) ) {
+
\MWDebug::log( "USE not init variable **************************
$variableLastParam");
+ }
+
$this->lastParam = self::$variables[$variableLastParam];
+
self::$variables[$variableLastParam]--;
+
\MWDebug::log( 'variables ' .var_export(self::$variables, true) );
+ }
+ }
+ break;
+ default:
+ \MWDebug::log(
__METHOD__ . " unknown operator '$operator'" );
+ break;
+ }
+ } else {
+ //doOperation for higher precedence
+ $this->doMath($precedence);
+ $this->lastOperator = $operator;
+ }
+ break;
+ }
+ return $this->lastParam;
+ }
+
+ private function doMath( $precedence = 17 ) { //17 =
count($operatorsPrecedence)-1
+\MWDebug::log( "function doMath( $precedence )");
if( isset($this->mathMemory[0]) ) {
while( $mathZerroMemory =
array_pop($this->mathMemory[0]) ) {
$this->doOperation($mathZerroMemory);
@@ -153,7 +241,70 @@
}
private function doOperation($operator, $param = null) {
+ $variableParam = false;
+ if( substr($param, 0, 2) == "\0$" ) {
+ $variableParam = substr($param, 2);
+ if( !isset(self::$variables[$variableParam]) ) {
+ self::$variables[$variableParam] = "\0";
+ }
+ $param = self::$variables[$variableParam];
+ }
+
+ $variableLastParam = false;
+ if( substr($this->lastParam, 0, 2) == "\0$" ) {
+ $variableLastParam = substr($this->lastParam, 2);
+ if( !isset(self::$variables[$variableLastParam]) ) {
+ \MWDebug::log( "USE not init variable
************************** $variableLastParam");
+ }
+ $this->lastParam = self::$variables[$variableLastParam];
+ }
+
switch ($operator) {
+ case '=':
+ self::$variables[$variableParam] =
$this->lastParam;
+ break;
+ case T_CONCAT_EQUAL:// .=
+ self::$variables[$variableParam] .=
$this->lastParam;
+ break;
+ case T_PLUS_EQUAL:// +=
+ self::$variables[$variableParam] +=
$this->lastParam;
+ break;
+ case T_MINUS_EQUAL:// -=
+ self::$variables[$variableParam] -=
$this->lastParam;
+ break;
+ case T_MUL_EQUAL: // *=
+ self::$variables[$variableParam] *=
$this->lastParam;
+ break;
+ case T_DIV_EQUAL: // /=
+ self::$variables[$variableParam] /=
$this->lastParam;
+ break;
+ case T_MOD_EQUAL: // %=
+ self::$variables[$variableParam] %=
$this->lastParam;
+ break;
+ case T_AND_EQUAL:// &=
+ self::$variables[$variableParam] &=
$this->lastParam;
+ break;
+ case T_OR_EQUAL:// |=
+ self::$variables[$variableParam] |=
$this->lastParam;
+ break;
+ case T_XOR_EQUAL:// ^=
+ self::$variables[$variableParam] ^=
$this->lastParam;
+ break;
+ case T_SL_EQUAL:// <<=
+ self::$variables[$variableParam] <<=
$this->lastParam;
+ break;
+ case T_SR_EQUAL:// >>=
+ self::$variables[$variableParam] >>=
$this->lastParam;
+ break;
+ case T_INC: // ++$variable
+ $this->lastParam =
++self::$variables[$variableLastParam];
+ break;
+ case T_DEC: // --$variable
+ $this->lastParam =
--self::$variables[$variableLastParam];
+ break;
+ case ',':
+ $this->listParams[] = $param;
+ break;
case '.':
$this->lastParam = $param . $this->lastParam;
break;
@@ -237,7 +388,7 @@
\MWDebug::log( __METHOD__ . " unknown operator
'$operator'" );
break;
}
-//\MWDebug::log( "function doOperation('$operator', '$param') @
'$this->lastParam'");
+\MWDebug::log( "function doOperation('$operator', '$param') @ lastParam =
'$this->lastParam', " . 'listParams = '.var_export($this->listParams, true) );
}
public function getMathResult() {
@@ -248,9 +399,10 @@
}
public function getCommandResult( &$debug ) {
-// \MWDebug::log('function getCommandResult()');
+ \MWDebug::log('function getCommandResult()');
$this->doMath();
- $this->listParams[] = $this->lastParam;
+ $this->doOperation(',', $this->lastParam);
+ //$this->listParams[] = $this->lastParam;
$return = null;
switch ($this->lastCommand) {
@@ -269,52 +421,17 @@
}
break;
default:
- $lastCommand = $this->lastCommand;
+ $return = 'Error! Unknown operator "' .
htmlspecialchars($this->variableOperator) . '" in ' . __METHOD__;
+ \MWDebug::log($return);
+ /*$lastCommand = $this->lastCommand;
if( substr($lastCommand, 0, 1) == '$' ) {
$varName = substr($lastCommand, 1);
switch ($this->variableOperator) {
- case '=':
-
self::$variables[$varName] = $this->lastParam;
- break;
- case T_CONCAT_EQUAL:// .=
-
self::$variables[$varName] .= $this->lastParam;
- break;
- case T_PLUS_EQUAL:// +=
-
self::$variables[$varName] += $this->lastParam;
- break;
- case T_MINUS_EQUAL:// -=
-
self::$variables[$varName] -= $this->lastParam;
- break;
- case T_MUL_EQUAL: // *=
-
self::$variables[$varName] *= $this->lastParam;
- break;
- case T_DIV_EQUAL: // /=
-
self::$variables[$varName] /= $this->lastParam;
- break;
- case T_MOD_EQUAL: // %=
-
self::$variables[$varName] %= $this->lastParam;
- break;
- case T_AND_EQUAL:// &=
-
self::$variables[$varName] &= $this->lastParam;
- break;
- case T_OR_EQUAL:// |=
-
self::$variables[$varName] |= $this->lastParam;
- break;
- case T_XOR_EQUAL:// ^=
-
self::$variables[$varName] ^= $this->lastParam;
- break;
- case T_SL_EQUAL:// <<=
-
self::$variables[$varName] <<= $this->lastParam;
- break;
- case T_SR_EQUAL:// >>=
-
self::$variables[$varName] >>= $this->lastParam;
- break;
case false: // $variable++ OR
$variable--;
break;
default:
// TODO exception
- $return = 'Error!
Unknown operator "' . htmlspecialchars($this->variableOperator) . '" in ' .
__METHOD__;
- \MWDebug::log($return);
+
break 2;
}
if( $this->lastDebug !== false ) {
@@ -325,47 +442,11 @@
// TODO exception
$return = 'Error in ' . __METHOD__ .
'lastCommand = \'' . htmlspecialchars( var_export($this->lastCommand, true) )
.'\'';
}
- break;
+ break;*/
}
$this->popStack();
+ $this->lastParam = null;
return $return;
}
- public function setVariable( $name, $debug ) {
- $this->addCommand("\$$name", $debug);
- }
-
- public function setVariableValue( $name, $value) {
- self::$variables[$name] = $value;
- }
-
- public function setVariableOperator( $operator ) {
- $this->variableOperator = $operator;
- }
-
- public function getVariableValue( $name ) {
- if( isset(self::$variables[$name]) ) {
- return self::$variables[$name];
- } else {
- //TODO THROW
- return null;
- }
- }
-
- public function parenthesesOpen() {
- if( $this->lastOperator ) {
- $precedence = $this->getOperatorPrecedence(
$this->lastOperator );
- $this->mathMemory[$precedence] =
array($this->lastOperator, $this->lastParam);
- $this->lastOperator = false;
- }
- $this->pushStack();
- }
-
- 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 96c8a3f..070565e 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -331,6 +331,12 @@
}
public function testRun_echo_math_Increment_2() {
$this->assertEquals(
+ Interpreter::run('$a = 10; echo $a++ + $a +
++$a;'),
+ '33'
+ );
+ }
+ public function testRun_echo_math_Increment_3() {
+ $this->assertEquals(
Interpreter::run('
$a = 10;
$a++;
--
To view, visit https://gerrit.wikimedia.org/r/59814
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9745c8908e43c372f64519e1ad781bfb30fe6fa1
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