aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2017-09-12 15:32:08 +0300
committerGitHub <noreply@github.com>2017-09-12 15:32:08 +0300
commit3cd949c04fe0aa3820694b47e951bb6f1528ac72 (patch)
tree8c2a14bcf09439e0544f6af3300d2229d0f39e44
parent3436e0a51ff4c709226178f897e053c0fae8da4c (diff)
parent8d602b30dd9bf9e1b926c0d732c2ff3fe7b6644a (diff)
Merge pull request #20 from ochi51/master
Fixes exponentiation operator
-rw-r--r--.gitignore3
l---------bin/phpunit1
-rw-r--r--composer.json9
-rw-r--r--src/NXP/Classes/Lexer.php4
-rw-r--r--src/NXP/Classes/Token/TokenDegree.php4
-rw-r--r--tests/MathTest.php6
6 files changed, 22 insertions, 5 deletions
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/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/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
*/