Passed
Push — master ( 676938...54b931 )
by Observer
01:29
created

Interpreter::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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
    static array $default_styles = []; // Массив дефолтных стилей
17
18
    static bool $throw_errors = true; // Выводить ли ошибки интерпретации
19
    static bool $allow_multimethods_calls = true; // Можно ли использовать многоуровневые вызовы методов (->method1->method2)
20
21
    /**
22
     * * Интерпретирование синтаксического дерева
23
     * Выполняет то, что было сгенерировано парсером VST кода
24
     * 
25
     * @param AST $tree - Абстрактное Синтаксическое Дерево (АСД), сгенерированное VST Parser'ом
26
     * [@param array $parent = null] - нода-родитель дерева (системная настройка)
27
     * 
28
     * @return array - возвращает список созданных стилей
29
     */
30
    public static function run (AST $tree, Node $parent = null): array
31
    {
32
        foreach ($tree->getNodes () as $id => $node)
33
        {
34
            if ($node->type == \VLF\STYLE_DEFINITION)
35
            {
36
                $name  = $node->args['name'];
37
                $nodes = $node->getNodes ();
38
39
                if ($node->args['parents'] !== null)
40
                    foreach ($node->args['parents'] as $parent)
41
                        if (!isset (self::$styles[$parent]) && !isset (self::$default_styles[$parent]))
42
                        {
43
                            if (self::$throw_errors)
44
                                throw new \Exception ('Style "'. $parent .'" not founded');
45
                        }
46
47
                        else $nodes = array_merge (self::$styles[$parent] ?? self::$default_styles[$parent], $nodes);
48
49
                if ($node->args['is_default'])
50
                    self::$default_styles[$name] = isset (self::$objects[$name]) ?
51
                        array_merge (self::$default_styles[$name], $nodes) : $nodes;
52
53
                else self::$styles[$name] = isset (self::$objects[$name]) ?
54
                    array_merge (self::$styles[$name], $nodes) : $nodes;
55
            }
56
57
            list (self::$styles, self::$default_styles) = self::run (new AST (array_map (
58
                fn ($node) => $node->export (), $node->getNodes ())), $node);
59
        }
60
61
        return [self::$styles, self::$default_styles];
62
    }
63
	
64
	public static function clear (): void
65
	{
66
		self::$styles = [];
67
		self::$default_styles = [];
68
	}
69
}
70