1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Frontend\Builder\Instruction; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\SDL\Frontend\Builder\BaseBuilder; |
14
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
15
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class VariableBuilder |
19
|
|
|
*/ |
20
|
|
|
class VariableBuilder extends BaseBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private const VARIABLE_DEFINITIONS = [ |
26
|
|
|
'ConstantDefinition', |
27
|
|
|
'VariableDefinition', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param RuleInterface $rule |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function match(RuleInterface $rule): bool |
35
|
|
|
{ |
36
|
|
|
return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ContextInterface $ctx |
41
|
|
|
* @param RuleInterface $rule |
42
|
|
|
* @return \Generator|\Closure |
43
|
|
|
*/ |
44
|
|
|
public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator |
45
|
|
|
{ |
46
|
|
|
yield function() use ($ctx, $rule): \Generator { |
47
|
|
|
/** @var ValueInterface $value */ |
48
|
|
|
[$isConstant, $value] = [$this->isConstant($rule), yield $this->getValueNode($rule)]; |
|
|
|
|
49
|
|
|
|
50
|
|
|
foreach ($rule->find('> #VariableName') as $name) { |
51
|
|
|
$variable = $name->first('> :T_VARIABLE')->getValue(1); |
52
|
|
|
|
53
|
|
|
$record = $ctx->declare($variable)->set($value); |
54
|
|
|
|
55
|
|
|
$isConstant ? $record->lock() : $record->unlock(); |
56
|
|
|
} |
57
|
|
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param RuleInterface $rule |
62
|
|
|
* @return RuleInterface |
63
|
|
|
*/ |
64
|
|
|
private function getValueNode(RuleInterface $rule): RuleInterface |
65
|
|
|
{ |
66
|
|
|
return $rule->first('> #VariableValue')->getChild(0); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param RuleInterface $rule |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
private function isConstant(RuleInterface $rule): bool |
74
|
|
|
{ |
75
|
|
|
return $rule->getName() === 'ConstantDefinition'; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.