Skip to content

Commit

Permalink
Modulus operator
Browse files Browse the repository at this point in the history
Better field type checking

It refs #17
  • Loading branch information
madorin committed Jan 7, 2021
1 parent 7ee7b20 commit 2927e17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PHP Mathematical expression parser and evaluator

* Fast evaluation
* Compact codebase
* Operators: + - * / ^
* Operators: + - * / ^ %
* Brackets, nested, unlimited levels
* Variables: predefined or estimated dynamically
* Functions: predefined or connected dynamically
Expand All @@ -20,7 +20,7 @@ PHP Mathematical expression parser and evaluator

## Installation

Run
Using [Composer](https://getcomposer.org) run

```bash
$ composer require madorin/matex
Expand All @@ -29,7 +29,7 @@ $ composer require madorin/matex
See [MANUAL.md](doc/MANUAL.md) for more details and options.


## Examples
## Usage

Basic:
```php
Expand Down Expand Up @@ -172,4 +172,4 @@ Dorin Marcoci - <[email protected]> - <https://www.marcodor.com>

## License

Matex is distributed under MIT license. See [LICENSE.md](LICENSE.md) for more details.
Matex is distributed under MIT license. See [license](LICENSE.md) for more details.
16 changes: 14 additions & 2 deletions doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@ echo $evaluator->execute('1 + 2');

## Usage

See [examples/README.md](../examples/README.md) for code samples.
See [examples](../examples/README.md) for code samples.


## Operators

Matex supports the following operators:

+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
% Modulus


## Author
Expand All @@ -56,4 +68,4 @@ Dorin Marcoci - <[email protected]> - <https://www.marcodor.com>

## License

Matex is distributed under MIT license. See [LICENSE.md](../LICENSE.md) for more details.
Matex is distributed under MIT license. See [license](../LICENSE.md) for more details.
21 changes: 14 additions & 7 deletions src/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function getArguments(&$arguments = []): bool {
elseif ($char == '(') $b++;
$this->pos++;
}
if (!in_array($char, [false, '+', '-', '/', '*', '^', ')']))
if (!in_array($char, [false, '+', '-', '/', '*', '^', '%', ')']))
return false;
$this->addArgument($arguments, substr($this->text, $mark, $this->pos - $mark - 1));
return true;
Expand Down Expand Up @@ -100,8 +100,12 @@ private function getFunction(string $name) {
return call_user_func_array($routine['ref'], $this->proArguments($arguments));
}

private function checkFloats($a, $b) {
if (is_float($a) && is_float($b)) return;
private function isNumer($value): bool {
return is_float($value) || is_integer($value);
}

private function checkNumers($a, $b) {
if ($this->isNumer($a) && $this->isNumer($b)) return;
throw new Exception('Non-numeric value', 8);
}

Expand All @@ -121,7 +125,7 @@ private function term() {
$this->pos++;
$value = $this->calculate();
$this->pos++;
if (!in_array($this->text[$this->pos] ?? false, [false, '+', '-', '/', '*', '^', ')']))
if (!in_array($this->text[$this->pos] ?? false, [false, '+', '-', '/', '*', '^', '%', ')']))
throw new Exception('Syntax error', 1);
return $minus ? - $value : $value;
}
Expand All @@ -138,10 +142,10 @@ private function term() {

private function subTerm() {
$value = $this->term();
while (in_array($char = $this->text[$this->pos] ?? false, ['*', '/', '^'])) {
while (in_array($char = $this->text[$this->pos] ?? false, ['*', '/', '^', '%'])) {
$this->pos++;
$term = $this->term();
$this->checkFloats($value, $term);
$this->checkNumers($value, $term);
switch ($char) {
case '*':
$value *= $term;
Expand All @@ -154,6 +158,9 @@ private function subTerm() {
case '^':
$value **= $term;
break;
case '%':
$value %= $term;
break;
}
}
return $value;
Expand All @@ -169,7 +176,7 @@ private function calculate() {
$value .= $subTerm;
continue;
}
$this->checkFloats($value, $subTerm);
$this->checkNumers($value, $subTerm);
if ($char == '-') $subTerm = -$subTerm;
$value += $subTerm;
}
Expand Down

0 comments on commit 2927e17

Please sign in to comment.