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\Readable; |
13
|
|
|
use Railt\SDL\Renderer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Opcode |
17
|
|
|
*/ |
18
|
|
|
class Opcode implements OpcodeInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array|string[]|null |
22
|
|
|
*/ |
23
|
|
|
protected static $opcodes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $operation; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $operands; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $id; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Readable |
42
|
|
|
*/ |
43
|
|
|
private $file; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int|null |
47
|
|
|
*/ |
48
|
|
|
private $offset; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Opcode constructor. |
52
|
|
|
* @param int $operation |
53
|
|
|
* @param mixed ...$operands |
54
|
|
|
*/ |
55
|
|
|
public function __construct(int $operation, ...$operands) |
56
|
|
|
{ |
57
|
|
|
$this->operation = $operation; |
58
|
|
|
$this->operands = $operands; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $operation |
63
|
|
|
* @return Opcode |
64
|
|
|
*/ |
65
|
|
|
public function rebind(int $operation): Opcode |
66
|
|
|
{ |
67
|
|
|
$this->operation = $operation; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param int $id |
74
|
|
|
* @param Readable $file |
75
|
|
|
* @param int $offset |
76
|
|
|
* @return Opcode |
77
|
|
|
*/ |
78
|
|
|
public function mount(int $id, Readable $file, int $offset = 0): Opcode |
79
|
|
|
{ |
80
|
|
|
$this->id = $id; |
81
|
|
|
$this->file = $file; |
82
|
|
|
$this->offset = $offset; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function getId(): int |
91
|
|
|
{ |
92
|
|
|
\assert($this->id !== null, 'Opcode is unmounted'); |
93
|
|
|
|
94
|
|
|
return $this->id; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function getOffset(): int |
101
|
|
|
{ |
102
|
|
|
\assert($this->offset !== null, 'Opcode is unmounted'); |
103
|
|
|
|
104
|
|
|
return $this->offset; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getOperation(): int |
111
|
|
|
{ |
112
|
|
|
return $this->operation; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $id |
117
|
|
|
* @return mixed|null |
118
|
|
|
*/ |
119
|
|
|
public function getOperand(int $id) |
120
|
|
|
{ |
121
|
|
|
return $this->operands[$id] ?? null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return iterable |
126
|
|
|
*/ |
127
|
|
|
public function getOperands(): iterable |
128
|
|
|
{ |
129
|
|
|
return $this->operands; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return Readable |
134
|
|
|
*/ |
135
|
|
|
public function getFile(): Readable |
136
|
|
|
{ |
137
|
|
|
\assert($this->file !== null, 'Opcode is unmounted'); |
138
|
|
|
|
139
|
|
|
return $this->file; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function __toString(): string |
146
|
|
|
{ |
147
|
|
|
$operands = []; |
148
|
|
|
|
149
|
|
|
foreach ($this->operands as $i => $operand) { |
150
|
|
|
$operands[] = \sprintf('%d => %s', $i, Renderer::render($operand)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return \vsprintf('#%s %s {%s}', [ |
154
|
|
|
$this->id ?? '?', |
155
|
|
|
$this->getName(), |
156
|
|
|
\implode(', ', $operands), |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getName(): string |
164
|
|
|
{ |
165
|
|
|
return static::getOperationName($this->operation); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param int $operation |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public static function getOperationName(int $operation): string |
173
|
|
|
{ |
174
|
|
|
if (static::$opcodes === null) { |
175
|
|
|
static::$opcodes = []; |
176
|
|
|
|
177
|
|
|
try { |
178
|
|
|
$reflection = new \ReflectionClass(static::class); |
179
|
|
|
foreach ($reflection->getConstants() as $name => $value) { |
180
|
|
|
static::$opcodes[$value] = $name; |
181
|
|
|
} |
182
|
|
|
} catch (\ReflectionException $e) { |
183
|
|
|
return ''; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return static::$opcodes[$operation] ?? static::$opcodes[static::RL_NOP]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.