protected function doInterpret(IContext $ctx, $l, $r) { if ($r == 0) { throw new DivisionByZero('Деление на 0'); } $ctx->set($this, $l / $r); }
function interpret(IContext $ctx) { $foo = $ctx->getFunction($this->name) ?: (isset(self::$list[$this->name]) ? self::$list[$this->name] : null); if (!$foo) { throw new FunctionNotFoundException("Function {$this->name} not found"); } $values = array_map(function ($e) use($ctx) { $e->interpret($ctx); return $ctx->get($e); }, $this->arguments); $ctx->set($this, call_user_func_array($foo, $values)); }
public function testFunc() { $fl = new I\FL('twice(3.14)'); $this->assertEquals($fl->evaluate(), '6.28'); $fl = new I\FL('PI()'); $this->assertEquals($fl->evaluate(), pi()); $fl = new I\FL('sum(cos(0), 2, 3, twice(2), 5)'); $this->assertEquals($fl->evaluate(), '15'); $fl = new I\FL('sum(cos(0), 2, 3, twice(2), 5)'); $ctx = new I\IContext(); $ctx->regFunction('sum', function () { return implode('', func_get_args()); }); $this->assertEquals($fl->evaluate($ctx), '12345'); }
protected function doInterpret(IContext $ctx, $l, $r) { $ctx->set($this, pow($l, $r)); }
function interpret(IContext $context) { $context->set($this, $this->value); }