diff options
author | NeonXP <frei@neonxp.info> | 2013-09-06 05:42:09 +0400 |
---|---|---|
committer | NeonXP <frei@neonxp.info> | 2013-09-06 05:42:09 +0400 |
commit | 9cdc34290a84093b1c4640118289a7cf56d55125 (patch) | |
tree | 2187fa94cc9f152c9196e7bd4fe23a2c81f60ffd /src/NXP/Classes/Token | |
parent | f172123a0dccdca7651c7ad552175924a16b9458 (diff) |
Mass refactoring
Some changes:
+ Added support of functions with multiple arguments
+ Added some default function (min, max, avg). just example of multiple arguments :)
- Removed variables support (I think they pointless)
~ All tokens now in individual classes
~ Parsing based on regular expressions
~ Fix negative numbers
~ Fix grouping with brackets
Diffstat (limited to 'src/NXP/Classes/Token')
-rw-r--r-- | src/NXP/Classes/Token/AbstractContainerToken.php | 46 | ||||
-rw-r--r-- | src/NXP/Classes/Token/AbstractOperator.php | 20 | ||||
-rw-r--r-- | src/NXP/Classes/Token/InterfaceFunction.php | 23 | ||||
-rw-r--r-- | src/NXP/Classes/Token/InterfaceOperator.php | 33 | ||||
-rw-r--r-- | src/NXP/Classes/Token/InterfaceToken.php | 22 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenComma.php | 25 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDegree.php | 54 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDivision.php | 54 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenFunction.php | 41 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenLeftBracket.php | 25 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenMinus.php | 54 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenMultiply.php | 54 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenNumber.php | 25 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenPlus.php | 54 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenRightBracket.php | 25 |
15 files changed, 555 insertions, 0 deletions
diff --git a/src/NXP/Classes/Token/AbstractContainerToken.php b/src/NXP/Classes/Token/AbstractContainerToken.php new file mode 100644 index 0000000..12d49d2 --- /dev/null +++ b/src/NXP/Classes/Token/AbstractContainerToken.php @@ -0,0 +1,46 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +abstract class AbstractContainerToken implements InterfaceToken +{ + /** + * @var string + */ + protected $value; + + /** + * @param string $value + */ + public function __construct($value) + { + $this->value = $value; + } + + /** + * @param string $value + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * @return string + */ + public function getValue() + { + return $this->value; + } +} diff --git a/src/NXP/Classes/Token/AbstractOperator.php b/src/NXP/Classes/Token/AbstractOperator.php new file mode 100644 index 0000000..6cdfe99 --- /dev/null +++ b/src/NXP/Classes/Token/AbstractOperator.php @@ -0,0 +1,20 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +abstract class AbstractOperator implements InterfaceToken, InterfaceOperator +{ + const RIGHT_ASSOC = 'RIGHT'; + const LEFT_ASSOC = 'LEFT'; +} diff --git a/src/NXP/Classes/Token/InterfaceFunction.php b/src/NXP/Classes/Token/InterfaceFunction.php new file mode 100644 index 0000000..be867b0 --- /dev/null +++ b/src/NXP/Classes/Token/InterfaceFunction.php @@ -0,0 +1,23 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +interface InterfaceFunction +{ + /** + * @param array $stack + * @return $this + */ + public function execute(&$stack); +} diff --git a/src/NXP/Classes/Token/InterfaceOperator.php b/src/NXP/Classes/Token/InterfaceOperator.php new file mode 100644 index 0000000..da6cdf0 --- /dev/null +++ b/src/NXP/Classes/Token/InterfaceOperator.php @@ -0,0 +1,33 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +interface InterfaceOperator +{ + /** + * @return int + */ + public function getPriority(); + + /** + * @return string + */ + public function getAssociation(); + + /** + * @param array $stack + * @return TokenNumber + */ + public function execute(&$stack); +} diff --git a/src/NXP/Classes/Token/InterfaceToken.php b/src/NXP/Classes/Token/InterfaceToken.php new file mode 100644 index 0000000..86fec91 --- /dev/null +++ b/src/NXP/Classes/Token/InterfaceToken.php @@ -0,0 +1,22 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +interface InterfaceToken +{ + /** + * @return string + */ + public static function getRegex(); +} diff --git a/src/NXP/Classes/Token/TokenComma.php b/src/NXP/Classes/Token/TokenComma.php new file mode 100644 index 0000000..f590e32 --- /dev/null +++ b/src/NXP/Classes/Token/TokenComma.php @@ -0,0 +1,25 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +class TokenComma implements InterfaceToken +{ + /** + * @return string + */ + public static function getRegex() + { + return '\,'; + } +} diff --git a/src/NXP/Classes/Token/TokenDegree.php b/src/NXP/Classes/Token/TokenDegree.php new file mode 100644 index 0000000..8488dcd --- /dev/null +++ b/src/NXP/Classes/Token/TokenDegree.php @@ -0,0 +1,54 @@ +<?php +/** +* This file is part of the MathExecutor package +* +* (c) Alexander Kiryukhin +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code +*/ + +namespace NXP\Classes\Token; + +/** +* @author Alexander Kiryukhin <alexander@symdev.org> +*/ +class TokenDegree extends AbstractOperator +{ + /** + * @return string + */ + public static function getRegex() + { + return '\^'; + } + + /** + * @return int + */ + public function getPriority() + { + return 3; + } + + /** + * @return string + */ + public function getAssociation() + { + return self::RIGHT_ASSOC; + } + + /** + * @param InterfaceToken[] $stack + * @return $this + */ + public function execute(&$stack) + { + $op2 = array_pop($stack); + $op1 = array_pop($stack); + $result = $op1->getValue() ^ $op2->getValue(); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php new file mode 100644 index 0000000..479a4ec --- /dev/null +++ b/src/NXP/Classes/Token/TokenDivision.php @@ -0,0 +1,54 @@ +<?php +/** +* This file is part of the MathExecutor package +* +* (c) Alexander Kiryukhin +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code +*/ + +namespace NXP\Classes\Token; + +/** +* @author Alexander Kiryukhin <alexander@symdev.org> +*/ +class TokenDivision extends AbstractOperator +{ + /** + * @return string + */ + public static function getRegex() + { + return '\/'; + } + + /** + * @return int + */ + public function getPriority() + { + return 2; + } + + /** + * @return string + */ + public function getAssociation() + { + return self::LEFT_ASSOC; + } + + /** + * @param InterfaceToken[] $stack + * @return $this + */ + public function execute(&$stack) + { + $op2 = array_pop($stack); + $op1 = array_pop($stack); + $result = $op1->getValue() / $op2->getValue(); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenFunction.php b/src/NXP/Classes/Token/TokenFunction.php new file mode 100644 index 0000000..23f64bd --- /dev/null +++ b/src/NXP/Classes/Token/TokenFunction.php @@ -0,0 +1,41 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +class TokenFunction extends AbstractContainerToken implements InterfaceFunction +{ + /** + * @return string + */ + public static function getRegex() + { + return '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; + } + + /** + * @param array $stack + * @return $this + */ + public function execute(&$stack) + { + $args = array(); + list($places, $function) = $this->value; + for ($i = 0; $i < $places; $i++) { + array_push($args, array_pop($stack)->getValue()); + } + $result = call_user_func_array($function, $args); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenLeftBracket.php b/src/NXP/Classes/Token/TokenLeftBracket.php new file mode 100644 index 0000000..0cfdc1a --- /dev/null +++ b/src/NXP/Classes/Token/TokenLeftBracket.php @@ -0,0 +1,25 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +class TokenLeftBracket implements InterfaceToken +{ + /** + * @return string + */ + public static function getRegex() + { + return '\('; + } +} diff --git a/src/NXP/Classes/Token/TokenMinus.php b/src/NXP/Classes/Token/TokenMinus.php new file mode 100644 index 0000000..0463d4c --- /dev/null +++ b/src/NXP/Classes/Token/TokenMinus.php @@ -0,0 +1,54 @@ +<?php +/** +* This file is part of the MathExecutor package +* +* (c) Alexander Kiryukhin +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code +*/ + +namespace NXP\Classes\Token; + +/** +* @author Alexander Kiryukhin <alexander@symdev.org> +*/ +class TokenMinus extends AbstractOperator +{ + /** + * @return string + */ + public static function getRegex() + { + return '\-'; + } + + /** + * @return int + */ + public function getPriority() + { + return 1; + } + + /** + * @return string + */ + public function getAssociation() + { + return self::LEFT_ASSOC; + } + + /** + * @param InterfaceToken[] $stack + * @return $this + */ + public function execute(&$stack) + { + $op2 = array_pop($stack); + $op1 = array_pop($stack); + $result = $op1->getValue() - $op2->getValue(); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenMultiply.php b/src/NXP/Classes/Token/TokenMultiply.php new file mode 100644 index 0000000..e6fd960 --- /dev/null +++ b/src/NXP/Classes/Token/TokenMultiply.php @@ -0,0 +1,54 @@ +<?php +/** +* This file is part of the MathExecutor package +* +* (c) Alexander Kiryukhin +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code +*/ + +namespace NXP\Classes\Token; + +/** +* @author Alexander Kiryukhin <alexander@symdev.org> +*/ +class TokenMultiply extends AbstractOperator +{ + /** + * @return string + */ + public static function getRegex() + { + return '\*'; + } + + /** + * @return int + */ + public function getPriority() + { + return 2; + } + + /** + * @return string + */ + public function getAssociation() + { + return self::LEFT_ASSOC; + } + + /** + * @param InterfaceToken[] $stack + * @return $this + */ + public function execute(&$stack) + { + $op2 = array_pop($stack); + $op1 = array_pop($stack); + $result = $op1->getValue() * $op2->getValue(); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenNumber.php b/src/NXP/Classes/Token/TokenNumber.php new file mode 100644 index 0000000..763ca04 --- /dev/null +++ b/src/NXP/Classes/Token/TokenNumber.php @@ -0,0 +1,25 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +class TokenNumber extends AbstractContainerToken +{ + /** + * @return string + */ + public static function getRegex() + { + return '\-?\d+\.?\d*'; + } +} diff --git a/src/NXP/Classes/Token/TokenPlus.php b/src/NXP/Classes/Token/TokenPlus.php new file mode 100644 index 0000000..f9562e7 --- /dev/null +++ b/src/NXP/Classes/Token/TokenPlus.php @@ -0,0 +1,54 @@ +<?php +/** +* This file is part of the MathExecutor package +* +* (c) Alexander Kiryukhin +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code +*/ + +namespace NXP\Classes\Token; + +/** +* @author Alexander Kiryukhin <alexander@symdev.org> +*/ +class TokenPlus extends AbstractOperator +{ + /** + * @return string + */ + public static function getRegex() + { + return '\+'; + } + + /** + * @return int + */ + public function getPriority() + { + return 1; + } + + /** + * @return string + */ + public function getAssociation() + { + return self::LEFT_ASSOC; + } + + /** + * @param InterfaceToken[] $stack + * @return $this + */ + public function execute(&$stack) + { + $op2 = array_pop($stack); + $op1 = array_pop($stack); + $result = $op1->getValue() + $op2->getValue(); + + return new TokenNumber($result); + } +} diff --git a/src/NXP/Classes/Token/TokenRightBracket.php b/src/NXP/Classes/Token/TokenRightBracket.php new file mode 100644 index 0000000..c68473a --- /dev/null +++ b/src/NXP/Classes/Token/TokenRightBracket.php @@ -0,0 +1,25 @@ +<?php +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Classes\Token; + +/** + * @author Alexander Kiryukhin <alexander@symdev.org> + */ +class TokenRightBracket implements InterfaceToken +{ + /** + * @return string + */ + public static function getRegex() + { + return '\)'; + } +} |