diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2020-05-20 06:04:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-20 06:04:31 +0300 |
commit | 949334d6c37d384800c639ba9941e94f5157f5ac (patch) | |
tree | ecfbcc7e3033ef1f11c25baca1c9d32f496d8ee1 /src/NXP/Classes/Operator.php | |
parent | 01415abc9d7f7401d9f4c09fbbec24930c65a097 (diff) |
WIP: New generation (#62)
* Massive refactoring
More clean structure
Parsing without regular expressions
* Cleanup unused imports
* Fix version string for Travis
* 7.1 downgrade
* Fix readme
Diffstat (limited to 'src/NXP/Classes/Operator.php')
-rw-r--r-- | src/NXP/Classes/Operator.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php new file mode 100644 index 0000000..53550a9 --- /dev/null +++ b/src/NXP/Classes/Operator.php @@ -0,0 +1,69 @@ +<?php + + +namespace NXP\Classes; + + +use NXP\Exception\IncorrectExpressionException; +use ReflectionFunction; + +class Operator +{ + /** + * @var string + */ + public $operator; + + /** + * @var bool + */ + public $isRightAssoc; + + /** + * @var int + */ + public $priority; + + /** + * @var callable<\SplStack> + */ + public $function; + + /** + * @var int + */ + public $places; + + /** + * Operator constructor. + * @param string $operator + * @param bool $isRightAssoc + * @param int $priority + * @param callable $function + */ + public function __construct(string $operator, bool $isRightAssoc, int $priority, callable $function) + { + $this->operator = $operator; + $this->isRightAssoc = $isRightAssoc; + $this->priority = $priority; + $this->function = $function; + $this->function = $function; + $reflection = new ReflectionFunction($function); + $this->places = $reflection->getNumberOfParameters(); + } + + public function execute(&$stack) + { + if (count($stack) < $this->places) { + throw new IncorrectExpressionException(); + } + $args = []; + for ($i = 0; $i < $this->places; $i++) { + array_unshift($args, array_pop($stack)->value); + } + + $result = call_user_func_array($this->function, $args); + + return new Token(Token::Literal, $result); + } +}
\ No newline at end of file |