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\IR; |
11
|
|
|
|
12
|
|
|
use Railt\Io\PositionInterface; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class JoinableOpcode |
17
|
|
|
*/ |
18
|
|
|
class JoinableOpcode extends Opcode implements PositionInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Readable |
27
|
|
|
*/ |
28
|
|
|
private $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $offset; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Closure |
37
|
|
|
*/ |
38
|
|
|
private $description; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* JoinableOpcode constructor. |
42
|
|
|
* @param OpcodeInterface $opcode |
43
|
|
|
* @param int $id |
44
|
|
|
* @param Readable $file |
45
|
|
|
* @param int $offset |
46
|
|
|
*/ |
47
|
|
|
public function __construct(OpcodeInterface $opcode, int $id, Readable $file, int $offset = 0) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($opcode->getOperation(), ...$opcode->getOperands()); |
50
|
|
|
|
51
|
|
|
$this->id = $id; |
52
|
|
|
$this->file = $file; |
53
|
|
|
$this->offset = $offset; |
54
|
|
|
$this->description = function() use ($opcode): string { |
55
|
|
|
return \trim((string)(new \ReflectionObject($opcode))->getDocComment(), " \t\n\r\0\x0B/*"); |
56
|
|
|
}; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getDescription(): string |
63
|
|
|
{ |
64
|
|
|
return \preg_replace_callback('/\$(\d+)/iu', function(array $m): string { |
65
|
|
|
return $this->operandToString($this->operands[(int)$m[1]] ?? null); |
66
|
|
|
}, ($this->description)()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Readable |
71
|
|
|
*/ |
72
|
|
|
public function getFile(): Readable |
73
|
|
|
{ |
74
|
|
|
return $this->file; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return PositionInterface |
79
|
|
|
*/ |
80
|
|
|
private function getPosition(): PositionInterface |
81
|
|
|
{ |
82
|
|
|
return $this->file->getPosition($this->offset); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function getLine(): int |
89
|
|
|
{ |
90
|
|
|
return $this->getPosition()->getLine(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getColumn(): int |
97
|
|
|
{ |
98
|
|
|
return $this->getPosition()->getColumn(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function getId(): int |
105
|
|
|
{ |
106
|
|
|
return $this->id; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function __toString(): string |
113
|
|
|
{ |
114
|
|
|
return \vsprintf('%4s %-10s %-3d %-3d %s', [ |
115
|
|
|
'#' . $this->getId(), |
116
|
|
|
\pathinfo($this->getFile()->getPathname(), \PATHINFO_FILENAME), |
117
|
|
|
$this->getLine(), |
118
|
|
|
$this->getColumn(), |
119
|
|
|
parent::__toString() |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|