forked from ircmaxell/php-math-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make null handling configurable, static interface, typed exception
- Loading branch information
Stefan Ackermann
committed
Apr 17, 2020
1 parent
318c34f
commit 66c9ead
Showing
16 changed files
with
493 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class InvalidSyntaxException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Options\NullHandling; | ||
|
||
class Addition extends Operator | ||
{ | ||
protected $precedence = 4; | ||
public function getPrecedence(): int | ||
{ | ||
return 4; | ||
} | ||
|
||
public function operate(array &$stack) | ||
public function operate(array &$stack, array $options) | ||
{ | ||
$left = array_pop($stack)->operate($stack); | ||
$right = array_pop($stack)->operate($stack); | ||
|
||
if ($left === null || $right === null) { | ||
return null; | ||
} | ||
|
||
return $left + $right; | ||
return NullHandling::withNullHandling( | ||
$stack, | ||
$options, | ||
static function ($left, $right) { | ||
return $left + $right; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Options\NullHandling; | ||
|
||
class Division extends Operator | ||
{ | ||
protected $precedence = 5; | ||
public function getPrecedence(): int | ||
{ | ||
return 5; | ||
} | ||
|
||
public function operate(array &$stack) | ||
public function operate(array &$stack, $options) | ||
{ | ||
$left = array_pop($stack)->operate($stack); | ||
$right = array_pop($stack)->operate($stack); | ||
|
||
if ($left === null || $right === null) { | ||
return null; | ||
} | ||
|
||
if ($left === 0) { | ||
return null; | ||
} | ||
|
||
return $right / $left; | ||
return NullHandling::withNullHandling( | ||
$stack, | ||
$options, | ||
static function ($left, $right) { | ||
if ($right === 0) { | ||
return null; | ||
} | ||
|
||
return $left / $right; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Options\NullHandling; | ||
|
||
class Modulo extends Operator | ||
{ | ||
protected $precedence = 5; | ||
public function getPrecedence(): int | ||
{ | ||
return 5; | ||
} | ||
|
||
public function operate(array &$stack) | ||
public function operate(array &$stack, array $options) | ||
{ | ||
$left = array_pop($stack)->operate($stack); | ||
$right = array_pop($stack)->operate($stack); | ||
|
||
if ($left === null || $right === null) { | ||
return null; | ||
} | ||
|
||
if ($left === 0) { | ||
return null; | ||
} else { | ||
return $right % $left; | ||
} | ||
return NullHandling::withNullHandling( | ||
$stack, | ||
$options, | ||
static function ($left, $right) { | ||
if ($right === 0) { | ||
return null; | ||
} else { | ||
return $left % $right; | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Options\NullHandling; | ||
|
||
class Multiplication extends Operator | ||
{ | ||
protected $precedence = 5; | ||
|
||
public function operate(array &$stack) | ||
public function getPrecedence(): int | ||
{ | ||
return 5; | ||
} | ||
|
||
public function operate(array &$stack, array $options) | ||
{ | ||
$left = array_pop($stack)->operate($stack); | ||
$right = array_pop($stack)->operate($stack); | ||
if ($left === null || $right === null) { | ||
return null; | ||
} | ||
return $left * $right; | ||
return NullHandling::withNullHandling( | ||
$stack, | ||
$options, | ||
static function ($left, $right) { | ||
return $left * $right; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Expression; | ||
|
||
class Parenthesis extends Expression | ||
{ | ||
protected $precedence = 6; | ||
|
||
public function operate(array &$stack) | ||
public function operate(array &$stack, array $options = []) | ||
{ | ||
} | ||
|
||
public function getPrecedence() | ||
public function getPrecedence(): int | ||
{ | ||
return $this->precedence; | ||
return 6; | ||
} | ||
|
||
public function isNoOp() | ||
public function isNoOp(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isParenthesis() | ||
public function isParenthesis(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function isOpen() | ||
public function isOpen(): bool | ||
{ | ||
return $this->value == '('; | ||
return $this->value === '('; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MathParser\Expressions; | ||
|
||
use MathParser\Options\NullHandling; | ||
|
||
class Power extends Operator | ||
{ | ||
protected $precedence = 6; | ||
|
||
public function operate(array &$stack) | ||
public function getPrecedence(): int | ||
{ | ||
return 6; | ||
} | ||
|
||
public function isLeftAssoc(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function operate(array &$stack, array $options) | ||
{ | ||
$right = array_pop($stack)->operate($stack); | ||
$left = array_pop($stack)->operate($stack); | ||
|
||
if ($left === null || $right === null) { | ||
return null; | ||
} | ||
return pow($left, $right); | ||
return NullHandling::withNullHandling( | ||
$stack, | ||
$options, | ||
static function ($left, $right) { | ||
return $left ** $right; | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.