-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpScriptRenderer.php
More file actions
312 lines (267 loc) · 9.84 KB
/
Copy pathPhpScriptRenderer.php
File metadata and controls
312 lines (267 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
declare(strict_types=1);
namespace PhpScript\Core;
use PhpScript\Ast\ArrayAccess;
use PhpScript\Ast\Assignment;
use PhpScript\Ast\BinaryOperation;
use PhpScript\Ast\BreakStatement;
use PhpScript\Ast\ContinueStatement;
use PhpScript\Ast\EchoStatement;
use PhpScript\Ast\ForeachStatement;
use PhpScript\Ast\ForStatement;
use PhpScript\Ast\FunctionCall;
use PhpScript\Ast\Identifier;
use PhpScript\Ast\IfStatement;
use PhpScript\Ast\Literal;
use PhpScript\Ast\MemberAccess;
use PhpScript\Ast\NoOp;
use PhpScript\Ast\PostfixOperation;
use PhpScript\Ast\Program;
use PhpScript\Ast\UnaryOperation;
use PhpScript\Ast\Variable;
use PhpScript\Contracts\AstTraverserInterface;
use PhpScript\Contracts\Node;
use PhpScript\Exceptions\AstTraverserException;
final class PhpScriptRenderer implements AstTraverserInterface
{
private string $generatedCode = '';
private int $indentLevel = 0;
/**
* @param string[] $allowedFunctions
*
* @codeCoverageIgnore
*/
public function setAllowedFunctions(array $allowedFunctions): void
{
// do nothing
}
/**
* @return \PhpScript\Core\Token[]
*
* @codeCoverageIgnore
*/
public function getSourceMap(): array
{
return [];
}
public function traverse(Node $node): string
{
$this->generatedCode = '';
$this->doTraverse($node);
return $this->generatedCode;
}
private function doTraverse(Node $node): void
{
match ($node::class) {
Program::class => $this->traverseProgram($node),
EchoStatement::class => $this->traverseEchoStatement($node),
IfStatement::class => $this->traverseIfStatement($node),
ForStatement::class => $this->traverseForStatement($node),
ForeachStatement::class => $this->traverseForeachStatement($node),
BreakStatement::class => $this->traverseBreakStatement($node),
ContinueStatement::class => $this->traverseContinueStatement($node),
Assignment::class => $this->traverseAssignment($node),
BinaryOperation::class => $this->traverseBinaryOperation($node),
UnaryOperation::class => $this->traverseUnaryOperation($node),
PostfixOperation::class => $this->traversePostfixOperation($node),
ArrayAccess::class => $this->traverseArrayAccess($node),
MemberAccess::class => $this->traverseMemberAccess($node),
FunctionCall::class => $this->traverseFunctionCall($node),
Variable::class => $this->traverseVariable($node),
Identifier::class => $this->traverseIdentifier($node),
Literal::class => $this->traverseLiteral($node),
NoOp::class => $this->traverseNoOp(),
// @codeCoverageIgnoreStart
default => throw AstTraverserException::unknownNodeType($node::class),
// @codeCoverageIgnoreEnd
};
}
private function traverseProgram(Program $node): void
{
foreach ($node->statements as $statement) {
$this->doTraverse($statement);
$this->generatedCode .= "\n" . $this->lineIndent();
}
}
private function traverseEchoStatement(EchoStatement $node): void
{
$this->generatedCode .= 'echo ';
$this->doTraverse($node->expression);
$this->generatedCode .= ';';
}
private function traverseIfStatement(IfStatement $node): void
{
$this->generatedCode .= 'if (';
$this->doTraverse($node->condition);
$this->generatedCode .= ') {' . "\n" . $this->lineIndent(1);
$this->doTraverse($node->then);
$this->generatedCode .= "\n" . $this->lineIndent(-1) . '}';
if ($node->else instanceof Node) {
$this->generatedCode .= ' else {' . "\n" . $this->lineIndent(1);
$this->doTraverse($node->else);
$this->generatedCode .= "\n" . $this->lineIndent(-1) . '}' . "\n" . $this->lineIndent();
} else {
$this->generatedCode .= "\n" . $this->lineIndent();
}
}
private function traverseForStatement(ForStatement $node): void
{
$this->generatedCode .= 'for (';
if ($node->initializer instanceof Node) {
$this->doTraverse($node->initializer);
$this->generatedCode .= ' ';
} else {
$this->generatedCode .= '; ';
}
if ($node->condition instanceof Node) {
$this->doTraverse($node->condition);
}
$this->generatedCode .= '; ';
if ($node->increment instanceof Node) {
$this->doTraverse($node->increment);
}
$this->generatedCode .= ') {' . "\n" . $this->lineIndent(1);
$this->doTraverse($node->body);
$this->generatedCode .= "\n" . $this->lineIndent(-1) . '}' . "\n" . $this->lineIndent();
}
private function traverseForeachStatement(ForeachStatement $node): void
{
$this->generatedCode .= 'foreach (';
$this->doTraverse($node->iterable);
$this->generatedCode .= ' as ';
if ($node->key instanceof Variable) {
$this->doTraverse($node->key);
$this->generatedCode .= ' => ';
}
$this->doTraverse($node->value);
$this->generatedCode .= ') {' . "\n" . $this->lineIndent(1);
$this->doTraverse($node->body);
$this->generatedCode .= "\n" . $this->lineIndent(-1) . '}' . "\n" . $this->lineIndent();
}
public function visitBreakStatement(BreakStatement $node): string
{
$this->traverseBreakStatement($node);
return $this->generatedCode;
}
private function traverseBreakStatement(BreakStatement $node): void
{
$this->generatedCode .= $node->level === 1 ? 'break;' : "break {$node->level};";
}
public function visitContinueStatement(ContinueStatement $node): string
{
$this->traverseContinueStatement($node);
return $this->generatedCode;
}
private function traverseContinueStatement(ContinueStatement $node): void
{
$this->generatedCode .= $node->level === 1 ? 'continue;' : "continue {$node->level};";
}
private function traverseAssignment(Assignment $node): void
{
$this->doTraverse($node->variable);
$this->generatedCode .= ' = ';
$this->doTraverse($node->expression);
$this->generatedCode .= ';';
}
private function traverseBinaryOperation(BinaryOperation $node): void
{
$this->doTraverse($node->left);
$operator = match ($node->operator) {
TokenType::T_PLUS => '+',
TokenType::T_MINUS => '-',
TokenType::T_MULTIPLY => '*',
TokenType::T_DIVIDE => '/',
TokenType::T_COMPARE_EQUALS => '==',
TokenType::T_COMPARE_UNEQUALS => '!=',
TokenType::T_GREATER_THAN => '>',
TokenType::T_LESS_THAN => '<',
default => throw AstTraverserException::unknownOperator($node->operator->value),
};
$this->generatedCode .= ' ' . $operator . ' ';
$this->doTraverse($node->right);
}
private function traverseUnaryOperation(UnaryOperation $node): void
{
$operator = match ($node->operator) {
TokenType::T_BANG => '!',
TokenType::T_MINUS => '-',
default => throw AstTraverserException::unknownOperator($node->operator->value),
};
$this->generatedCode .= $operator;
$this->doTraverse($node->right);
}
private function traversePostfixOperation(PostfixOperation $node): void
{
$this->doTraverse($node->left);
$operator = match ($node->operator) {
TokenType::T_INCREMENT => '++',
TokenType::T_DECREMENT => '--',
default => throw AstTraverserException::unknownOperator($node->operator->value),
};
$this->generatedCode .= $operator;
}
private function traverseArrayAccess(ArrayAccess $node): void
{
$this->doTraverse($node->array);
$this->generatedCode .= '[';
$this->doTraverse($node->key);
$this->generatedCode .= ']';
}
private function traverseMemberAccess(MemberAccess $node): void
{
$this->doTraverse($node->object);
$this->generatedCode .= '.';
$this->doTraverse($node->property);
}
private function traverseFunctionCall(FunctionCall $node): void
{
$this->doTraverse($node->callee);
$this->generatedCode .= '(';
foreach ($node->arguments as $i => $argument) {
$this->doTraverse($argument);
if ($i < count($node->arguments) - 1) {
$this->generatedCode .= ', ';
}
}
$this->generatedCode .= ')';
}
private function traverseVariable(Variable $node): void
{
$this->generatedCode .= $node->name;
}
private function traverseIdentifier(Identifier $node): void
{
$this->generatedCode .= $node->name;
}
private function traverseLiteral(Literal $node): void
{
$value = $node->value;
if (is_numeric($value)) {
$this->generatedCode .= (string) $value;
return;
}
if (is_string($value)) {
if ($value === PHP_EOL) {
$this->generatedCode .= 'LINEBREAK';
return;
}
$this->generatedCode .= "'" . addslashes($value) . "'";
return;
}
if (is_bool($value)) {
$this->generatedCode .= $value ? 'true' : 'false';
return;
}
if ($value === null) {
$this->generatedCode .= 'null';
return;
}
throw AstTraverserException::unknownLiteralType(gettype($value));
}
private function traverseNoOp(): void {}
private function lineIndent(int $increment = 0): string
{
$this->indentLevel += $increment;
return str_repeat(' ', $this->indentLevel * 4);
}
}