Skip to content

Commit

Permalink
v3.0
Browse files Browse the repository at this point in the history
Support PHP 8.1, use null handling on single variable, clean up
  • Loading branch information
SaschaKliche committed Oct 18, 2022
1 parent b997a91 commit 222f487
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 204 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.phar
.idea
.idea
/.phpunit.result.cache
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "perbility/math-parser",
"description": "A simple shunting-yard based math parser and executor for PHP",
"version": "2.0.2",
"keywords": ["math", "eval"],
"homepage": "https://github.com/PERBILITY/php-math-parser",
"license": "MIT",
Expand All @@ -15,6 +14,9 @@
"name": "Stefan Ackermann"
}
],
"require": {
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^8"
},
Expand Down
14 changes: 11 additions & 3 deletions lib/MathParser/Expressions/Addition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Addition extends Operator
{
/**
* @return int
*/
public function getPrecedence(): int
{
return 4;
}

public function operate(array &$stack, array $options)

/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
12 changes: 10 additions & 2 deletions lib/MathParser/Expressions/Division.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Division extends Operator
{
/**
* @return int
*/
public function getPrecedence(): int
{
return 5;
}

public function operate(array &$stack, $options)
/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

declare(strict_types=1);

namespace MathParser;
namespace MathParser\Expressions;

use MathParser\Expressions\Addition;
use MathParser\Expressions\Division;
use MathParser\Expressions\Modulo;
use MathParser\Expressions\Multiplication;
use MathParser\Expressions\Number;
use MathParser\Expressions\Parenthesis;
use MathParser\Expressions\Power;
use MathParser\Expressions\Subtraction;
use MathParser\Expressions\Unary;
use RuntimeException;

abstract class Expression
{
protected $value = '';
/**
* @var mixed
*/
protected mixed $value = '';

public function __construct($value)
/**
* @param mixed $value
*/
public function __construct(mixed $value)
{
$this->value = $value;
}

public static function factory($value): Expression
/**
* @param mixed $value
* @return Expression
*/
public static function factory(mixed $value): Expression
{
if (is_object($value) && $value instanceof self) {
if ($value instanceof self) {
return $value;
} elseif (is_numeric($value)) {
// +0 => to number conversion (int or float)
Expand All @@ -49,37 +51,20 @@ public static function factory($value): Expression
} elseif (strlen($value) >= 2 && $value[0] === '$') {
return new Variable(substr($value, 1));
}
throw new \RuntimeException('Undefined Value ' . $value);
throw new RuntimeException('Undefined Value ' . $value);
}

abstract public function operate(array &$stack, array $options);
/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
abstract public function operate(array &$stack, array $options): mixed;

public function isOperator(): bool
{
return false;
}

public function isUnary(): bool
{
return false;
}

public function isParenthesis(): bool
{
return false;
}

public function isNoOp(): bool
{
return false;
}

public function isVariable(): bool
{
return false;
}

public function render()
/**
* @return mixed
*/
public function render(): mixed
{
return $this->value;
}
Expand Down
14 changes: 11 additions & 3 deletions lib/MathParser/Expressions/Modulo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Modulo extends Operator
{
/**
* @return int
*/
public function getPrecedence(): int
{
return 5;
}

public function operate(array &$stack, array $options)

/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
14 changes: 11 additions & 3 deletions lib/MathParser/Expressions/Multiplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Multiplication extends Operator
{
/**
* @return int
*/
public function getPrecedence(): int
{
return 5;
}

public function operate(array &$stack, array $options)

/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
9 changes: 6 additions & 3 deletions lib/MathParser/Expressions/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace MathParser\Expressions;

use MathParser\Expression;

class Number extends Expression
{
public function operate(array &$stack, $options)
/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return $this->value;
}
Expand Down
15 changes: 7 additions & 8 deletions lib/MathParser/Expressions/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace MathParser\Expressions;

use MathParser\Expression;

abstract class Operator extends Expression
{
protected $leftAssoc = true;
protected bool $leftAssoc = true;

/**
* @return int
*/
abstract public function getPrecedence(): int;

/**
* @return bool
*/
public function isLeftAssoc(): bool
{
return $this->leftAssoc;
}

public function isOperator(): bool
{
return true;
}
}
27 changes: 13 additions & 14 deletions lib/MathParser/Expressions/Parenthesis.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@

namespace MathParser\Expressions;

use MathParser\Expression;

class Parenthesis extends Expression
{

public function operate(array &$stack, array $options = [])
/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options = []): mixed
{
return null;
}

/**
* @return int
*/
public function getPrecedence(): int
{
return 6;
}

public function isNoOp(): bool
{
return true;
}

public function isParenthesis(): bool
{
return true;
}

/**
* @return bool
*/
public function isOpen(): bool
{
return $this->value === '(';
Expand Down
20 changes: 15 additions & 5 deletions lib/MathParser/Expressions/Power.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Power extends Operator
{

/**
* @return int
*/
public function getPrecedence(): int
{
return 6;
}


/**
* @return bool
*/
public function isLeftAssoc(): bool
{
return false;
}

public function operate(array &$stack, array $options)

/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
14 changes: 11 additions & 3 deletions lib/MathParser/Expressions/Subtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

namespace MathParser\Expressions;

use MathParser\Options\NullHandling;
use MathParser\Util\NullHandling;

class Subtraction extends Operator
{
/**
* @return int
*/
public function getPrecedence(): int
{
return 4;
}

public function operate(array &$stack, array $options)

/**
* @param Expression[] $stack
* @param mixed[] $options
* @return mixed
*/
public function operate(array &$stack, array $options): mixed
{
return NullHandling::withNullHandling(
$stack,
Expand Down
Loading

0 comments on commit 222f487

Please sign in to comment.