Passed
Branch dev (e0c5fc)
by Observer
01:14
created

Interpreter::run()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
rs 8.4444
cc 8
nc 7
nop 2
1
<?php
2
3
namespace VLF\VST;
4
5
use \VLF\{
6
    AST,
7
    Node
8
};
9
10
/**
11
 * Интерпретатор AST VST разметки
12
 */
13
class Interpreter extends \VLF\Interpreter
14
{
15
    static array $styles = []; // Массив созданных стилей (название => стиль)
16
17
    static bool $throw_errors = true; // Выводить ли ошибки интерпретации
18
    static bool $allow_multimethods_calls = true; // Можно ли использовать многоуровневые вызовы методов (->method1->method2)
19
20
    /**
21
     * * Интерпретирование синтаксического дерева
22
     * Выполняет то, что было сгенерировано парсером VST кода
23
     * 
24
     * @param AST $tree - Абстрактное Синтаксическое Дерево (АСД), сгенерированное VST Parser'ом
25
     * [@param array $parent = null] - нода-родитель дерева (системная настройка)
26
     * 
27
     * @return array - возвращает список созданных объектов
28
     */
29
    public static function run (AST $tree, Node $parent = null): array
30
    {
31
        foreach ($tree->getNodes () as $id => $node)
32
        {
33
            if ($node->type == \VLF\STYLE_DEFINITION)
34
            {
35
                $name  = $node->args['name'];
36
                $nodes = $node->getNodes ();
37
38
                if ($node->args['parents'] !== null)
39
                    foreach ($node->args['parents'] as $parent)
40
                    {
41
                        if (!isset (self::$styles[$parent]) && self::$throw_errors)
42
                            throw new \Exception ('Style "'. $parent .'" not founded');
43
44
                        $nodes = array_merge ($nodes, self::$styles[$parent]);
45
                    }
46
47
                self::$styles[$name] = isset (self::$objects[$name]) ?
48
                    array_merge (self::$styles[$name], $nodes) : $nodes;
49
            }
50
51
            self::$styles = self::run (new AST (array_map (
52
                fn ($node) => $node->export (), $node->getNodes ())), $node);
53
        }
54
55
        return self::$styles;
56
    }
57
}
58