1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler; |
7
|
|
|
|
8
|
|
|
use PHPSA\Context; |
9
|
|
|
use PhpParser\Node; |
10
|
|
|
use PHPSA\Compiler\Statement\AbstractCompiler; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use PhpParser\Node\Stmt; |
13
|
|
|
|
14
|
|
|
class Statement |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param Node\Stmt $stmt |
18
|
|
|
* @return AbstractCompiler |
19
|
|
|
*/ |
20
|
8 |
|
protected function factory($stmt) |
21
|
|
|
{ |
22
|
8 |
|
switch (get_class($stmt)) { |
23
|
8 |
|
case Stmt\Return_::class: |
24
|
8 |
|
return new Statement\ReturnSt(); |
25
|
|
|
case Stmt\While_::class: |
26
|
|
|
return new Statement\WhileSt(); |
27
|
|
|
case Stmt\Switch_::class: |
28
|
|
|
return new Statement\SwitchSt(); |
29
|
|
|
case Stmt\If_::class: |
30
|
|
|
return new Statement\IfSt(); |
31
|
|
|
case Stmt\Do_::class: |
32
|
|
|
return new Statement\DoSt(); |
33
|
|
|
case Stmt\For_::class: |
34
|
|
|
return new Statement\ForSt(); |
35
|
|
|
case Stmt\Foreach_::class: |
36
|
|
|
return new Statement\ForeachSt(); |
37
|
|
|
case Stmt\TryCatch::class: |
38
|
|
|
return new Statement\TryCatchSt(); |
39
|
|
|
case Stmt\Catch_::class: |
40
|
|
|
return new Statement\CatchSt(); |
41
|
|
|
case Stmt\Throw_::class: |
42
|
|
|
return new Statement\ThrowSt(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
throw new RuntimeException('Unknown statement: ' . get_class($stmt)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Node\Stmt $stmt |
50
|
|
|
* @param Context $context |
51
|
|
|
*/ |
52
|
8 |
|
public function __construct(Node\Stmt $stmt, Context $context) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
/** |
56
|
|
|
* @todo Think a little bit more about own statement for break; |
57
|
|
|
*/ |
58
|
8 |
|
if ($stmt instanceof Stmt\Break_) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
$context->getEventManager()->fire( |
63
|
8 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
64
|
8 |
|
new Event\StatementBeforeCompile( |
65
|
8 |
|
$stmt, |
66
|
|
|
$context |
67
|
8 |
|
) |
68
|
8 |
|
); |
69
|
|
|
|
70
|
8 |
|
$compiler = $this->factory($stmt); |
71
|
8 |
|
} catch (\Exception $e) { |
72
|
|
|
$context->debug('StatementCompiler is not implemented for ' . get_class($stmt)); |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
$compiler->pass($stmt, $context); |
77
|
8 |
|
} |
78
|
|
|
} |
79
|
|
|
|