From 5d19d0660c395cf500c01c9f3f1e451f6914080b Mon Sep 17 00:00:00 2001 From: bajb Date: Fri, 1 Apr 2016 14:29:41 +0100 Subject: Avoid division by zero --- src/NXP/Classes/Token/TokenDivision.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/NXP') diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php index 479a4ec..7e55640 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); } -- cgit v1.2.3 From 9acdc3bb5736f876c69dec51380580a4aadef201 Mon Sep 17 00:00:00 2001 From: bajb Date: Fri, 1 Apr 2016 14:52:34 +0100 Subject: Specifically check for 0 in division to allow for negative operations --- src/NXP/Classes/Token/TokenDivision.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/NXP') diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php index 7e55640..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 = $op2->getValue() > 0 ? $op1->getValue() / $op2->getValue() : 0; + $result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0; return new TokenNumber($result); } -- cgit v1.2.3 From 9e0c01722f8daf63cb1ceb77a0f0e729d2096362 Mon Sep 17 00:00:00 2001 From: charles VILLETTE Date: Thu, 7 Jul 2016 14:36:01 +0200 Subject: Removing the numeric test on setVar. --- src/NXP/MathExecutor.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/NXP') diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 7eaa914..5cb4bca 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -91,10 +91,6 @@ class MathExecutor */ public function setVar($variable, $value) { - if (!is_numeric($value)) { - throw new \Exception("Variable value must be a number"); - } - $this->variables[$variable] = $value; return $this; -- cgit v1.2.3 From 1086fccc92e0800c873baf69fc3256008b883691 Mon Sep 17 00:00:00 2001 From: ochi51 Date: Tue, 12 Sep 2017 20:47:22 +0900 Subject: Bug fixes about "Undefined offset -1" if stack is empty array and token association is RIGHT_ASSOC --- src/NXP/Classes/Lexer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/NXP') 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); } -- cgit v1.2.3 From 8d602b30dd9bf9e1b926c0d732c2ff3fe7b6644a Mon Sep 17 00:00:00 2001 From: ochi51 Date: Tue, 12 Sep 2017 20:54:51 +0900 Subject: Fixes exponentiation operator --- .gitignore | 3 ++- bin/phpunit | 1 + composer.json | 9 +++++++++ src/NXP/Classes/Token/TokenDegree.php | 4 ++-- tests/MathTest.php | 6 ++++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 120000 bin/phpunit (limited to 'src/NXP') diff --git a/.gitignore b/.gitignore index 9f33dd5..83436db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ -.idea/ \ No newline at end of file +.idea/ +composer.lock \ No newline at end of file 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 a32dc52..dc35c2f 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,16 @@ "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/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/tests/MathTest.php b/tests/MathTest.php index f8a271e..a83a0d4 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -33,6 +33,12 @@ class MathTest extends \PHPUnit_Framework_TestCase $this->assertEquals($calculator->execute('1 / 0'), 0); } + public function testExponentiation() + { + $calculator = new MathExecutor(); + $this->assertEquals($calculator->execute('10 ^ 2'), 100); + } + /** * Expressions data provider */ -- cgit v1.2.3 From 29d32a155f9956d1041a4420c19986565f695040 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Fri, 31 Aug 2018 12:22:58 -0400 Subject: Add ability to get functions and operators that have been registered --- src/NXP/Classes/TokenFactory.php | 22 ++++++++++++++++++++++ src/NXP/MathExecutor.php | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'src/NXP') diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 19ba1cf..2b1e00e 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 @@ -68,6 +80,16 @@ class TokenFactory $this->operators = array_unique($this->operators); } + /** + * Get registered operators + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->operators; + } + /** * Add variable * @param string $name diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index f348355..0c3d185 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -156,6 +156,16 @@ class MathExecutor return $this; } + /** + * Get all registered operators to executor + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->tokenFactory->getOperators(); + } + /** * Add function to executor * @@ -171,6 +181,17 @@ class MathExecutor return $this; } + /** + * Get all registered functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->tokenFactory->getFunctions(); + } + /** * Execute expression * -- cgit v1.2.3