diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2018-09-06 20:41:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-06 20:41:13 +0300 |
commit | 7b657db13f0b76432ca0039e45f4b8f5e09879c2 (patch) | |
tree | 954c50f2644212742442e0027efb081620d2a109 | |
parent | a969571772a9761f67672b229741f209f5391560 (diff) | |
parent | aa1a092a9e231e8f923ab40365dff092c2908903 (diff) |
Merge branch 'master' into dev
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | README.md | 1 | ||||
l--------- | bin/phpunit | 1 | ||||
-rw-r--r-- | composer.json | 11 | ||||
-rw-r--r-- | src/NXP/Classes/Lexer.php | 4 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDegree.php | 4 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenDivision.php | 2 | ||||
-rw-r--r-- | src/NXP/Classes/TokenFactory.php | 22 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 21 | ||||
-rw-r--r-- | tests/MathTest.php | 12 |
10 files changed, 74 insertions, 7 deletions
@@ -1,2 +1,3 @@ vendor/ -.idea/
\ No newline at end of file +.idea/ +composer.lock
\ No newline at end of file @@ -1,3 +1,4 @@ +[![Stories in Ready](https://badge.waffle.io/NeonXP/MathExecutor.png?label=ready&title=Ready)](https://waffle.io/NeonXP/MathExecutor) # MathExecutor [![Build Status](https://travis-ci.org/NeonXP/MathExecutor.png?branch=master)](https://travis-ci.org/NeonXP/MathExecutor) diff --git a/bin/phpunit b/bin/phpunit new file mode 120000 index 0000000..4ba3256 --- /dev/null +++ b/bin/phpunit @@ -0,0 +1 @@ +../vendor/phpunit/phpunit/phpunit
\ No newline at end of file diff --git a/composer.json b/composer.json index 586ad96..dc35c2f 100644 --- a/composer.json +++ b/composer.json @@ -4,14 +4,23 @@ "minimum-stability": "stable", "keywords": ["math","parser","expression","calculator"], "homepage": "http://github.com/NeonXP/MathExecutor", - "license": "GPLv2", + "license": "MIT", "authors": [ { "name": "Alexander 'NeonXP' Kiryukhin", "email": "frei@neonxp.info" } ], + "require": { + "php": ">=5.6" + }, + "require-dev": { + "phpunit/phpunit": "~5.0" + }, "autoload": { "psr-0": {"NXP": "src/"} + }, + "config": { + "bin-dir": "bin" } } diff --git a/src/NXP/Classes/Lexer.php b/src/NXP/Classes/Lexer.php index b66ea87..e541732 100644 --- a/src/NXP/Classes/Lexer.php +++ b/src/NXP/Classes/Lexer.php @@ -99,13 +99,13 @@ class Lexer while ( count($stack) > 0 && ($stack[count($stack)-1] instanceof InterfaceOperator) && - ( + (( $token->getAssociation() == AbstractOperator::LEFT_ASSOC && $token->getPriority() <= $stack[count($stack)-1]->getPriority() ) || ( $token->getAssociation() == AbstractOperator::RIGHT_ASSOC && $token->getPriority() < $stack[count($stack)-1]->getPriority() - ) + )) ) { $output[] = array_pop($stack); } diff --git a/src/NXP/Classes/Token/TokenDegree.php b/src/NXP/Classes/Token/TokenDegree.php index 8488dcd..c31b66e 100644 --- a/src/NXP/Classes/Token/TokenDegree.php +++ b/src/NXP/Classes/Token/TokenDegree.php @@ -41,13 +41,13 @@ class TokenDegree extends AbstractOperator /** * @param InterfaceToken[] $stack - * @return $this + * @return TokenNumber */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); - $result = $op1->getValue() ^ $op2->getValue(); + $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 index 479a4ec..f1c35ff 100644 --- a/src/NXP/Classes/Token/TokenDivision.php +++ b/src/NXP/Classes/Token/TokenDivision.php @@ -47,7 +47,7 @@ class TokenDivision extends AbstractOperator { $op2 = array_pop($stack); $op1 = array_pop($stack); - $result = $op1->getValue() / $op2->getValue(); + $result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0; return new TokenNumber($result); } diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 07b9ea8..85c4a62 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -51,6 +51,18 @@ class TokenFactory $this->functions[$name] = array($places, $function); } + + /** + * get functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->functions; + } + /** * Add operator * @param string $operatorClass @@ -69,6 +81,16 @@ class TokenFactory } /** + * Get registered operators + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->operators; + } + + /** * Add variable * @param string $name * @param mixed $value diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 80f0bde..c5c7add 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -157,6 +157,16 @@ class MathExecutor } /** + * Get all registered operators to executor + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->tokenFactory->getOperators(); + } + + /** * Add function to executor * * @param string $name Name of function @@ -172,6 +182,17 @@ class MathExecutor } /** + * Get all registered functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->tokenFactory->getFunctions(); + } + + /** * Execute expression * * @param $expression diff --git a/tests/MathTest.php b/tests/MathTest.php index 2b2727f..9bf8d97 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -27,6 +27,18 @@ class MathTest extends \PHPUnit_Framework_TestCase $this->assertEquals($calculator->execute($expression), $phpResult); } + public function testZeroDevision() + { + $calculator = new MathExecutor(); + $this->assertEquals($calculator->execute('1 / 0'), 0); + } + + public function testExponentiation() + { + $calculator = new MathExecutor(); + $this->assertEquals($calculator->execute('10 ^ 2'), 100); + } + /** * Expressions data provider */ |