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\IR; |
11
|
|
|
|
12
|
|
|
use Railt\SDL\Exception\CompilerException; |
13
|
|
|
use Railt\SDL\Exception\NotFoundException; |
14
|
|
|
use Railt\SDL\IR\SymbolTable\VarSymbol; |
15
|
|
|
use Railt\SDL\IR\SymbolTable\VarSymbolInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SymbolTable |
19
|
|
|
*/ |
20
|
|
|
class SymbolTable implements SymbolTableInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $id = 0; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array|VarSymbolInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $variables = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param VarSymbolInterface $record |
34
|
|
|
* @return int |
35
|
|
|
*/ |
36
|
|
|
public function declare(VarSymbolInterface $record): int |
37
|
|
|
{ |
38
|
|
|
$id = $this->id++; |
39
|
|
|
|
40
|
|
|
$this->variables[$id] = $record; |
41
|
|
|
|
42
|
|
|
return $id; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $addr |
47
|
|
|
* @return VarSymbolInterface |
48
|
|
|
* @throws NotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function fetch(int $addr): VarSymbolInterface |
51
|
|
|
{ |
52
|
|
|
if (! isset($this->variables[$addr])) { |
53
|
|
|
$error = \sprintf('Mismatched variable address 0x%08x', $addr); |
54
|
|
|
throw new NotFoundException($error); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->variables[$addr]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return VarSymbol[]|\Traversable<int,VarSymbol> |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function getIterator(): \Traversable |
64
|
|
|
{ |
65
|
|
|
foreach ($this->variables as $id => $symbol) { |
66
|
|
|
yield $id => $symbol; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function __debugInfo(): array |
74
|
|
|
{ |
75
|
|
|
$variables = []; |
76
|
|
|
foreach ($this->variables as $id => $var) { |
77
|
|
|
$variables[$id] = (string)$var; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return [ |
81
|
|
|
'size' => $this->count(), |
82
|
|
|
'variables' => $variables |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function count(): int |
90
|
|
|
{ |
91
|
|
|
return \count($this->variables); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.