aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/Token/TokenFunction.php
blob: 432f1077bdbe735eda6933ba641cebe2ea7b1414 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/**
 * This file is part of the MathExecutor package
 *
 * (c) Alexander Kiryukhin
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 */

namespace NXP\Classes\Token;

/**
 * @author Alexander Kiryukhin <a.kiryukhin@mail.ru>
 */
class TokenFunction extends AbstractContainerToken implements InterfaceFunction
{
    /**
     * @return string
     */
    public static function getRegex()
    {
        return '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
    }

    /**
     * @param array $stack
     * @return $this
     */
    public function execute(&$stack)
    {
        $args = [];
        list($places, $function) = $this->value;
        for ($i = 0; $i < $places; $i++) {
            array_unshift($args, array_pop($stack)->getValue());
        }

        $result = call_user_func_array($function, $args);

        return new TokenNumber($result);
    }
}