1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VLF; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Нода синтаксического дерева |
7
|
|
|
*/ |
8
|
|
|
class Node |
9
|
|
|
{ |
10
|
|
|
public ?string $line = null; // Строка |
11
|
|
|
public array $words = []; // Список слов |
12
|
|
|
public int $height = 0; // Высота строки |
13
|
|
|
public int $type = 0; // Тип ноды |
14
|
|
|
public array $args = []; // Аргументы ноды |
15
|
|
|
|
16
|
|
|
protected array $nodes = []; // Ветви ноды |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Конструктор ноды |
20
|
|
|
* |
21
|
|
|
* [@param array $node = null] - массив для импорта ноды |
22
|
|
|
*/ |
23
|
|
|
public function __construct (array $node = null) |
24
|
|
|
{ |
25
|
|
|
if ($node !== null) |
26
|
|
|
$this->import ($node); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Импорт ноды |
31
|
|
|
* |
32
|
|
|
* @param array $node - массив для импорта |
33
|
|
|
* |
34
|
|
|
* @return Node - возвращает саму себя |
35
|
|
|
*/ |
36
|
|
|
public function import (array $node): Node |
37
|
|
|
{ |
38
|
|
|
$this->line = $node['line'] ?? null; |
39
|
|
|
$this->words = $node['words'] ?? []; |
40
|
|
|
$this->height = $node['height'] ?? 0; |
41
|
|
|
$this->type = $node['type'] ?? 0; |
42
|
|
|
$this->args = $node['args'] ?? []; |
43
|
|
|
|
44
|
|
|
$this->nodes = array_map (fn (array $t) => new Node ($t), $node['nodes'] ?? []); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Экспорт ноды |
51
|
|
|
* |
52
|
|
|
* @return array - возвращает массив экспортированной ноды |
53
|
|
|
*/ |
54
|
|
|
public function export (): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'line' => $this->line, |
58
|
|
|
'words' => $this->words, |
59
|
|
|
'height' => $this->height, |
60
|
|
|
'type' => $this->type, |
61
|
|
|
'args' => $this->args, |
62
|
|
|
|
63
|
|
|
'nodes' => array_map (fn (Node $node) => $node->export (), $this->nodes) |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Добавить ветвь ноды |
69
|
|
|
* |
70
|
|
|
* @param Node $node - нода для добавления |
71
|
|
|
* |
72
|
|
|
* @return Node - возвращает саму себя |
73
|
|
|
*/ |
74
|
|
|
public function push (Node $node): Node |
75
|
|
|
{ |
76
|
|
|
$this->nodes[] = $node; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Получить список ветвей ноды |
83
|
|
|
* |
84
|
|
|
* @return array - возвращает список ветвей |
85
|
|
|
*/ |
86
|
|
|
public function getNodes (): array |
87
|
|
|
{ |
88
|
|
|
return $this->nodes; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|