1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeacefulBit\Slate\Parser\Nodes; |
4
|
|
|
|
5
|
|
|
use function Nerd\Common\Strings\toString; |
6
|
|
|
|
7
|
|
|
use PeacefulBit\Slate\Core\Frame; |
8
|
|
|
use PeacefulBit\Slate\Exceptions\EvaluatorException; |
9
|
|
|
|
10
|
|
|
class CallExpression extends Node |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Node |
14
|
|
|
*/ |
15
|
|
|
private $callee; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Node[] |
19
|
|
|
*/ |
20
|
|
|
private $arguments; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Node $callee |
24
|
|
|
* @param Node[] $arguments |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Node $callee, array $arguments) |
27
|
|
|
{ |
28
|
|
|
$this->callee = $callee; |
29
|
|
|
$this->arguments = $arguments; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return Node |
34
|
|
|
*/ |
35
|
|
|
public function getCallee(): Node |
36
|
|
|
{ |
37
|
|
|
return $this->callee; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Node[] |
42
|
|
|
*/ |
43
|
|
|
public function getArguments(): array |
44
|
|
|
{ |
45
|
|
|
return $this->arguments; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function __toString() |
52
|
|
|
{ |
53
|
|
|
$prefix = '(' . strval($this->getCallee()) . ' '; |
54
|
|
|
$suffix = ')'; |
55
|
|
|
|
56
|
|
|
$arguments = array_map('strval', $this->getArguments()); |
57
|
|
|
|
58
|
|
|
return $prefix . implode(' ', $arguments) . $suffix; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Frame $frame |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws EvaluatorException |
65
|
|
|
*/ |
66
|
|
|
public function evaluate(Frame $frame) |
67
|
|
|
{ |
68
|
|
|
$callee = $frame->evaluate($this->getCallee()); |
69
|
|
|
|
70
|
|
|
if (!$callee instanceof CallableNode) { |
71
|
|
|
throw new EvaluatorException(sprintf("%s is not callable", toString($callee))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $callee->call($frame, $this->getArguments()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Frame $frame |
79
|
|
|
* @return CallExpression |
80
|
|
|
*/ |
81
|
|
|
public function close(Frame $frame) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
// $closedArgs = array_map(function (Node $argument) use ($frame) { |
|
|
|
|
84
|
|
|
// return $argument instanceof Identifier ? $argument->evaluate($frame) : $argument; |
85
|
|
|
// }, $this->getArguments()); |
86
|
|
|
|
87
|
|
|
return new self($this->getCallee(), $this->getArguments()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.