-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHP.class.php
More file actions
executable file
·1034 lines (915 loc) · 33.4 KB
/
PHP.class.php
File metadata and controls
executable file
·1034 lines (915 loc) · 33.4 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace lang\ast\emit;
use lang\ast\Code;
use lang\ast\nodes\{InstanceExpression, ScopeExpression, BinaryExpression, Variable, Literal, ArrayLiteral, Block};
use lang\ast\types\{IsUnion, IsFunction, IsArray, IsMap};
use lang\ast\{Emitter, Node, Type};
abstract class PHP extends Emitter {
const PROPERTY = 0;
const METHOD = 1;
protected $literals= [];
/**
* Emit type literal or NULL if no type should be emitted
*
* @param ?lang.ast.Type $type
* @return ?string
*/
public function literal($type) {
return null === $type ? null : $this->literals[get_class($type)]($type);
}
/**
* Returns the simple name for use in a declaration
*
* @param string $name E.g. `\lang\ast\Parse`
* @return string In the above example, `Parse`.
*/
protected function declaration($name) {
return substr($name, strrpos($name, '\\') + 1);
}
/**
* Returns whether a given node is a constant expression:
*
* - Any literal
* - Arrays where all members are literals
* - Scope expressions with literal members (self::class, T::const)
* - Binary expression where left- and right hand side are literals
*
* @see https://wiki.php.net/rfc/const_scalar_exprs
* @param lang.ast.Result $result
* @param lang.ast.Node $node
* @return bool
*/
protected function isConstant($result, $node) {
if ($node instanceof Literal) {
return true;
} else if ($node instanceof ArrayLiteral) {
foreach ($node->values as $node) {
if (!$this->isConstant($result, $node)) return false;
}
return true;
} else if ($node instanceof ScopeExpression) {
return (
$node->member instanceof Literal &&
is_string($node->type) &&
!$result->lookup($node->type)->rewriteEnumCase($node->member->expression)
);
} else if ($node instanceof BinaryExpression) {
return $this->isConstant($result, $node->left) && $this->isConstant($result, $node->right);
}
return false;
}
/**
* As of PHP 7.4: Property type declarations support all type declarations
* supported by PHP with the exception of void and callable.
*
* @see https://wiki.php.net/rfc/typed_properties_v2#supported_types
* @param ?lang.ast.Type $type
* @return ?string
*/
protected function propertyType($type) {
if (null === $type || $type instanceof IsFunction || 'callable' === $type->literal()) {
return null;
} else {
return $this->literal($type);
}
}
/**
* Enclose a node inside a closure
*
* @param lang.ast.Result $result
* @param lang.ast.Node $node
* @param ?lang.ast.nodes.Signature $signature
* @param function(lang.ast.Result, lang.ast.Node): void $emit
*/
protected function enclose($result, $node, $signature, $emit) {
$capture= [];
foreach ($result->codegen->search($node, 'variable') as $var) {
if (isset($result->locals[$var->name])) {
$capture[$var->name]= true;
}
}
unset($capture['this']);
$result->stack[]= $result->locals;
$result->locals= [];
if ($signature) {
$result->out->write('function');
$this->emitSignature($result, $signature);
foreach ($signature->parameters as $param) {
unset($capture[$param->name]);
}
} else {
$result->out->write('function()');
}
if ($capture) {
$result->out->write('use($'.implode(', $', array_keys($capture)).')');
foreach ($capture as $name => $_) {
$result->locals[$name]= true;
}
}
$result->out->write('{');
$emit($result, $node);
$result->out->write('}');
$result->locals= array_pop($result->stack);
}
/**
* Emits local initializations
*
* @param lang.ast.Result $result
* @param [:lang.ast.Node] $init
* @return void
*/
protected function emitInitializations($result, $init) {
foreach ($init as $assign => $expression) {
$result->out->write($assign.'=');
$this->emitOne($result, $expression);
$result->out->write(';');
}
}
/**
* Convert blocks to IIFEs to allow a list of statements where PHP syntactically
* doesn't, e.g. `fn`-style lambdas or match expressions.
*
* @param lang.ast.Result $result
* @param lang.ast.Node $expression
* @return void
*/
protected function emitAsExpression($result, $expression) {
if ($expression instanceof Block) {
$result->out->write('(');
$this->enclose($result, $expression, null, function($result, $expression) {
$this->emitAll($result, $expression->statements);
});
$result->out->write(')()');
} else {
$this->emitOne($result, $expression);
}
}
protected function emitDirectives($result, $directives) {
$result->out->write('declare(');
foreach ($directives->declare as $directive => $value) {
$result->out->write($directive.'=');
$this->emitOne($result, $value);
}
$result->out->write(')');
}
protected function emitNamespace($result, $declaration) {
$result->out->write('namespace '.$declaration->name);
}
protected function emitImport($result, $import) {
foreach ($import->names as $name => $alias) {
$result->out->write('use '.$import->type.' '.$name.($alias ? ' as '.$alias : '').';');
}
}
protected function emitCode($result, $code) {
$result->out->write($code->value);
}
protected function emitLiteral($result, $literal) {
$result->out->write($literal->expression);
}
protected function emitEcho($result, $echo) {
$result->out->write('echo ');
foreach ($echo->expressions as $i => $expr) {
if ($i++) $result->out->write(',');
$this->emitOne($result, $expr);
}
}
protected function emitBlock($result, $block) {
$result->out->write('{');
$this->emitAll($result, $block->statements);
$result->out->write('}');
}
protected function emitStatic($result, $static) {
foreach ($static->initializations as $variable => $initial) {
$result->out->write('static $'.$variable);
if ($initial) {
if ($this->isConstant($result, $initial)) {
$result->out->write('=');
$this->emitOne($result, $initial);
} else {
$result->out->write('= null; null === $'.$variable.' && $'.$variable.'= ');
$this->emitOne($result, $initial);
}
}
$result->out->write(';');
}
}
protected function emitVariable($result, $variable) {
$result->out->write('$'.$variable->name);
}
protected function emitCast($result, $cast) {
static $native= ['string' => true, 'int' => true, 'float' => true, 'bool' => true, 'array' => true, 'object' => true];
$name= $cast->type->name();
if ('?' === $name[0]) {
$result->out->write('cast(');
$this->emitOne($result, $cast->expression);
$result->out->write(',\''.$name.'\', false)');
} else if (isset($native[$name])) {
$result->out->write('('.$cast->type->literal().')');
$this->emitOne($result, $cast->expression);
} else {
$result->out->write('cast(');
$this->emitOne($result, $cast->expression);
$result->out->write(',\''.$name.'\')');
}
}
protected function emitArray($result, $array) {
if (empty($array->values)) {
$result->out->write('[]');
return;
}
$unpack= false;
foreach ($array->values as $pair) {
if ('unpack' === $pair[1]->kind) {
$unpack= true;
break;
}
}
if ($unpack) {
$result->out->write('array_merge([');
foreach ($array->values as $pair) {
if ($pair[0]) {
$this->emitOne($result, $pair[0]);
$result->out->write('=>');
}
if ('unpack' === $pair[1]->kind) {
if ('array' === $pair[1]->expression->kind) {
$result->out->write('],');
$this->emitOne($result, $pair[1]->expression);
$result->out->write(',[');
} else {
$t= $result->temp();
$result->out->write('],('.$t.'=');
$this->emitOne($result, $pair[1]->expression);
$result->out->write(') instanceof \Traversable ? iterator_to_array('.$t.') : '.$t.',[');
}
} else {
$this->emitOne($result, $pair[1]);
$result->out->write(',');
}
}
$result->out->write('])');
} else {
$result->out->write('[');
foreach ($array->values as $pair) {
if ($pair[0]) {
$this->emitOne($result, $pair[0]);
$result->out->write('=>');
}
$this->emitOne($result, $pair[1]);
$result->out->write(',');
}
$result->out->write(']');
}
}
protected function emitParameter($result, $parameter) {
if ($parameter->type && $t= $this->literal($parameter->type)) {
$result->out->write($t.' ');
}
if ($parameter->variadic) {
$result->out->write('... $'.$parameter->name);
} else {
$result->out->write(($parameter->reference ? '&' : '').'$'.$parameter->name);
}
if ($parameter->default) {
if ($this->isConstant($result, $parameter->default)) {
$result->out->write('=');
$this->emitOne($result, $parameter->default);
} else {
$result->out->write('=null');
$result->locals[1]['null === $'.$parameter->name.' && $'.$parameter->name]= $parameter->default;
}
}
$result->locals[$parameter->name]= true;
}
protected function emitSignature($result, $signature) {
$result->out->write('(');
foreach ($signature->parameters as $i => $parameter) {
if ($i++) $result->out->write(',');
$this->emitParameter($result, $parameter);
}
$result->out->write(')');
if ($signature->returns && $t= $this->literal($signature->returns)) {
$result->out->write(':'.$t);
}
}
protected function emitFunction($result, $function) {
$result->stack[]= $result->locals;
$result->locals= [];
$result->out->write('function '.$function->name);
$this->emitSignature($result, $function->signature);
$result->out->write('{');
$this->emitAll($result, $function->body);
$result->out->write('}');
$result->locals= array_pop($result->stack);
}
protected function emitClosure($result, $closure) {
$result->stack[]= $result->locals;
$result->locals= [];
$result->out->write('function');
$this->emitSignature($result, $closure->signature);
if ($closure->use) {
$result->out->write(' use('.implode(',', $closure->use).') ');
foreach ($closure->use as $variable) {
$result->locals[substr($variable, 1)]= true;
}
}
$result->out->write('{');
$this->emitAll($result, $closure->body);
$result->out->write('}');
$result->locals= array_pop($result->stack);
}
protected function emitLambda($result, $lambda) {
$result->out->write('fn');
$this->emitSignature($result, $lambda->signature);
$result->out->write('=>');
$this->emitOne($result, $lambda->body);
}
protected function emitEnumCase($result, $case) {
$result->out->write('case '.$case->name);
if ($case->expression) {
$result->out->write('=');
$this->emitOne($result, $case->expression);
}
$result->out->write(';');
}
protected function emitEnum($result, $enum) {
array_unshift($result->type, $enum);
array_unshift($result->meta, []);
$result->locals= [[], []];
$result->out->write('enum '.$this->declaration($enum->name));
$enum->base && $result->out->write(':'.$enum->base);
$enum->implements && $result->out->write(' implements '.implode(', ', $enum->implements));
$result->out->write('{');
foreach ($enum->body as $member) {
$this->emitOne($result, $member);
}
// Initializations
$result->out->write('static function __init() {');
$this->emitInitializations($result, $result->locals[0]);
$this->emitMeta($result, $enum->name, $enum->annotations, $enum->comment);
$result->out->write('}} '.$enum->name.'::__init();');
array_shift($result->type);
}
protected function emitClass($result, $class) {
array_unshift($result->type, $class);
array_unshift($result->meta, []);
$result->locals= [[], []];
$result->out->write(implode(' ', $class->modifiers).' class '.$this->declaration($class->name));
$class->parent && $result->out->write(' extends '.$class->parent);
$class->implements && $result->out->write(' implements '.implode(', ', $class->implements));
$result->out->write('{');
foreach ($class->body as $member) {
$this->emitOne($result, $member);
}
// Create constructor for property initializations to non-static scalars
// which have not already been emitted inside constructor
if ($result->locals[1]) {
$result->out->write('public function __construct() {');
$this->emitInitializations($result, $result->locals[1]);
$result->out->write('}');
}
$result->out->write('static function __init() {');
$this->emitInitializations($result, $result->locals[0]);
$this->emitMeta($result, $class->name, $class->annotations, $class->comment);
$result->out->write('}} '.$class->name.'::__init();');
array_shift($result->type);
}
/** Stores lowercased, unnamespaced name in annotations for BC reasons! */
protected function annotations($result, $annotations) {
$lookup= [];
foreach ($annotations as $name => $arguments) {
$p= strrpos($name, '\\');
$key= lcfirst(false === $p ? $name : substr($name, $p + 1));
$result->out->write("'".$key."' => ");
$name === $key || $lookup[$key]= $name;
if (empty($arguments)) {
$result->out->write('null,');
} else if (1 === sizeof($arguments) && isset($arguments[0])) {
$this->emitOne($result, $arguments[0]);
$result->out->write(',');
} else {
$result->out->write('[');
foreach ($arguments as $name => $argument) {
is_string($name) && $result->out->write("'".$name."' => ");
$this->emitOne($result, $argument);
$result->out->write(',');
}
$result->out->write('],');
}
}
return $lookup;
}
/** Emits annotations in XP format - and mappings for their names */
protected function attributes($result, $annotations, $target) {
$result->out->write('DETAIL_ANNOTATIONS => [');
$lookup= $this->annotations($result, $annotations);
$result->out->write('], DETAIL_TARGET_ANNO => [');
foreach ($target as $name => $annotations) {
$result->out->write("'$".$name."' => [");
foreach ($this->annotations($result, $annotations) as $key => $value) {
$lookup[$key]= $value;
}
$result->out->write('],');
}
foreach ($lookup as $key => $value) {
$result->out->write("'".$key."' => '".$value."',");
}
$result->out->write(']');
}
/** Removes leading, intermediate and trailing stars from apidoc comments */
private function comment($comment) {
if (null === $comment || '' === $comment) {
return 'null';
} else if ('/' === $comment[0]) {
return "'".str_replace("'", "\\'", trim(preg_replace('/\n\s+\* ?/', "\n", substr($comment, 3, -2))))."'";
} else {
return "'".str_replace("'", "\\'", $comment)."'";
}
}
/** Emit meta information so that the reflection API won't have to parse it */
protected function emitMeta($result, $name, $annotations, $comment) {
if (null === $name) {
$result->out->write('\xp::$meta[strtr(self::class, "\\\\", ".")]= [');
} else {
$result->out->write('\xp::$meta[\''.strtr(ltrim($name, '\\'), '\\', '.').'\']= [');
}
$result->out->write('"class" => [');
$this->attributes($result, $annotations, []);
$result->out->write(', DETAIL_COMMENT => '.$this->comment($comment).'],');
foreach (array_shift($result->meta) as $type => $lookup) {
$result->out->write($type.' => [');
foreach ($lookup as $key => $meta) {
$result->out->write("'".$key."' => [");
$this->attributes($result, $meta[DETAIL_ANNOTATIONS], $meta[DETAIL_TARGET_ANNO]);
$result->out->write(', DETAIL_RETURNS => \''.$meta[DETAIL_RETURNS].'\'');
$result->out->write(', DETAIL_COMMENT => '.$this->comment($meta[DETAIL_COMMENT]));
$result->out->write(', DETAIL_ARGUMENTS => [\''.implode('\', \'', $meta[DETAIL_ARGUMENTS]).'\']],');
}
$result->out->write('],');
}
$result->out->write('];');
}
protected function emitInterface($result, $interface) {
array_unshift($result->meta, []);
$result->out->write('interface '.$this->declaration($interface->name));
$interface->parents && $result->out->write(' extends '.implode(', ', $interface->parents));
$result->out->write('{');
foreach ($interface->body as $member) {
$this->emitOne($result, $member);
$result->out->write("\n");
}
$result->out->write('}');
$this->emitMeta($result, $interface->name, $interface->annotations, $interface->comment);
}
protected function emitTrait($result, $trait) {
array_unshift($result->meta, []);
$result->out->write('trait '.$this->declaration($trait->name));
$result->out->write('{');
foreach ($trait->body as $member) {
$this->emitOne($result, $member);
$result->out->write("\n");
}
$result->out->write('}');
$this->emitMeta($result, $trait->name, $trait->annotations, $trait->comment);
}
protected function emitUse($result, $use) {
$result->out->write('use '.implode(',', $use->types));
if ($use->aliases) {
$result->out->write('{');
foreach ($use->aliases as $reference => $alias) {
$result->out->write($reference.' '.key($alias).' '.current($alias).';');
}
$result->out->write('}');
} else {
$result->out->write(';');
}
}
protected function emitConst($result, $const) {
$result->out->write(implode(' ', $const->modifiers).' const '.$const->name.'=');
$this->emitOne($result, $const->expression);
$result->out->write(';');
}
protected function emitProperty($result, $property) {
$result->meta[0][self::PROPERTY][$property->name]= [
DETAIL_RETURNS => $property->type ? $property->type->name() : 'var',
DETAIL_ANNOTATIONS => $property->annotations,
DETAIL_COMMENT => $property->comment,
DETAIL_TARGET_ANNO => [],
DETAIL_ARGUMENTS => []
];
$result->out->write(implode(' ', $property->modifiers).' '.$this->propertyType($property->type).' $'.$property->name);
if (isset($property->expression)) {
if ($this->isConstant($result, $property->expression)) {
$result->out->write('=');
$this->emitOne($result, $property->expression);
} else if (in_array('static', $property->modifiers)) {
$result->locals[0]['self::$'.$property->name]= $property->expression;
} else {
$result->locals[1]['$this->'.$property->name]= $property->expression;
}
}
$result->out->write(';');
}
protected function emitMethod($result, $method) {
// Include non-static initializations for members in constructor, removing
// them to signal emitClass() no constructor needs to be generated.
if ('__construct' === $method->name && isset($result->locals[1])) {
$locals= ['this' => true, 1 => $result->locals[1]];
$result->locals[1]= [];
} else {
$locals= ['this' => true, 1 => []];
}
$result->stack[]= $result->locals;
$result->locals= $locals;
$meta= [
DETAIL_RETURNS => $method->signature->returns ? $method->signature->returns->name() : 'var',
DETAIL_ANNOTATIONS => $method->annotations,
DETAIL_COMMENT => $method->comment,
DETAIL_TARGET_ANNO => [],
DETAIL_ARGUMENTS => []
];
$result->out->write(implode(' ', $method->modifiers).' function '.$method->name);
$this->emitSignature($result, $method->signature);
$promoted= '';
foreach ($method->signature->parameters as $param) {
$meta[DETAIL_TARGET_ANNO][$param->name]= $param->annotations;
$meta[DETAIL_ARGUMENTS][]= $param->type ? $param->type->name() : 'var';
if (isset($param->promote)) {
$promoted.= $param->promote.' $'.$param->name.';';
$result->locals[1]['$this->'.$param->name]= new Code(($param->reference ? '&$' : '$').$param->name);
$result->meta[0][self::PROPERTY][$param->name]= [
DETAIL_RETURNS => $param->type ? $param->type->name() : 'var',
DETAIL_ANNOTATIONS => [],
DETAIL_COMMENT => null,
DETAIL_TARGET_ANNO => [],
DETAIL_ARGUMENTS => []
];
}
if (isset($param->default) && !$this->isConstant($result, $param->default)) {
$meta[DETAIL_TARGET_ANNO][$param->name]['default']= [$param->default];
}
}
if (null === $method->body) {
$result->out->write(';');
} else {
$result->out->write(' {');
$this->emitInitializations($result, $result->locals[1]);
$this->emitAll($result, $method->body);
$result->out->write('}');
}
$result->out->write($promoted);
$result->meta[0][self::METHOD][$method->name]= $meta;
$result->locals= array_pop($result->stack);
}
protected function emitBraced($result, $braced) {
$result->out->write('(');
$this->emitOne($result, $braced->expression);
$result->out->write(')');
}
protected function emitBinary($result, $binary) {
$this->emitOne($result, $binary->left);
$result->out->write(' '.$binary->operator.' ');
$this->emitOne($result, $binary->right);
}
protected function emitPrefix($result, $unary) {
$result->out->write($unary->operator.' ');
$this->emitOne($result, $unary->expression);
}
protected function emitSuffix($result, $unary) {
$this->emitOne($result, $unary->expression);
$result->out->write($unary->operator);
}
protected function emitTernary($result, $ternary) {
$this->emitOne($result, $ternary->condition);
$result->out->write('?');
$this->emitOne($result, $ternary->expression);
$result->out->write(':');
$this->emitOne($result, $ternary->otherwise);
}
protected function emitOffset($result, $offset) {
$this->emitOne($result, $offset->expression);
if (null === $offset->offset) {
$result->out->write('[]');
} else {
$result->out->write('[');
$this->emitOne($result, $offset->offset);
$result->out->write(']');
}
}
protected function emitAssign($result, $target) {
if ('variable' === $target->kind) {
$result->out->write('$'.$target->name);
$result->locals[$target->name]= true;
} else if ('array' === $target->kind) {
$result->out->write('list(');
foreach ($target->values as $pair) {
$this->emitAssign($result, $pair[1]);
$result->out->write(',');
}
$result->out->write(')');
} else {
$this->emitOne($result, $target);
}
}
protected function emitAssignment($result, $assignment) {
$this->emitAssign($result, $assignment->variable);
$result->out->write($assignment->operator);
$this->emitOne($result, $assignment->expression);
}
protected function emitReturn($result, $return) {
$result->out->write('return ');
$return->expression && $this->emitOne($result, $return->expression);
}
protected function emitIf($result, $if) {
$result->out->write('if (');
$this->emitOne($result, $if->expression);
$result->out->write(') {');
$this->emitAll($result, $if->body);
$result->out->write('}');
if ($if->otherwise) {
$result->out->write('else {');
$this->emitAll($result, $if->otherwise);
$result->out->write('}');
}
}
protected function emitSwitch($result, $switch) {
$result->out->write('switch (');
$this->emitOne($result, $switch->expression);
$result->out->write(') {');
foreach ($switch->cases as $case) {
if ($case->expression) {
$result->out->write('case ');
$this->emitOne($result, $case->expression);
$result->out->write(':');
} else {
$result->out->write('default:');
}
$this->emitAll($result, $case->body);
}
$result->out->write('}');
}
protected function emitMatch($result, $match) {
$t= $result->temp();
if (null === $match->expression) {
$result->out->write('('.$t.'=true)');
} else {
$result->out->write('('.$t.'=');
$this->emitOne($result, $match->expression);
$result->out->write(')');
}
$b= 0;
foreach ($match->cases as $case) {
foreach ($case->expressions as $expression) {
$b && $result->out->write($t);
$result->out->write('===(');
$this->emitOne($result, $expression);
$result->out->write(')?');
$this->emitAsExpression($result, $case->body);
$result->out->write(':(');
$b++;
}
}
// Emit IIFE for raising an error until we have throw expressions
if (null === $match->default) {
$result->out->write('function() use('.$t.') { throw new \\Error("Unhandled match value of type ".gettype('.$t.')); })(');
} else {
$this->emitAsExpression($result, $match->default);
}
$result->out->write(str_repeat(')', $b));
}
protected function emitCatch($result, $catch) {
$capture= $catch->variable ? '$'.$catch->variable : $result->temp();
if (empty($catch->types)) {
$result->out->write('catch(\\Throwable '.$capture.') {');
} else {
$result->out->write('catch('.implode('|', $catch->types).' '.$capture.') {');
}
$this->emitAll($result, $catch->body);
$result->out->write('}');
}
protected function emitTry($result, $try) {
$result->out->write('try {');
$this->emitAll($result, $try->body);
$result->out->write('}');
if (isset($try->catches)) {
foreach ($try->catches as $catch) {
$this->emitCatch($result, $catch);
}
}
if (isset($try->finally)) {
$result->out->write('finally {');
$this->emitAll($result, $try->finally);
$result->out->write('}');
}
}
protected function emitThrow($result, $throw) {
$result->out->write('throw ');
$this->emitOne($result, $throw->expression);
$result->out->write(';');
}
protected function emitThrowExpression($result, $throw) {
$result->out->write('(');
$this->enclose($result, $throw->expression, null, function($result, $expression) {
$result->out->write('throw ');
$this->emitOne($result, $expression);
$result->out->write(';');
});
$result->out->write(')()');
}
protected function emitForeach($result, $foreach) {
$result->out->write('foreach (');
$this->emitOne($result, $foreach->expression);
$result->out->write(' as ');
if ($foreach->key) {
$this->emitOne($result, $foreach->key);
$result->out->write(' => ');
}
$this->emitOne($result, $foreach->value);
$result->out->write(') {');
$this->emitAll($result, $foreach->body);
$result->out->write('}');
}
protected function emitFor($result, $for) {
$result->out->write('for (');
$this->emitArguments($result, $for->initialization);
$result->out->write(';');
$this->emitArguments($result, $for->condition);
$result->out->write(';');
$this->emitArguments($result, $for->loop);
$result->out->write(') {');
$this->emitAll($result, $for->body);
$result->out->write('}');
}
protected function emitDo($result, $do) {
$result->out->write('do');
$result->out->write('{');
$this->emitAll($result, $do->body);
$result->out->write('} while (');
$this->emitOne($result, $do->expression);
$result->out->write(');');
}
protected function emitWhile($result, $while) {
$result->out->write('while (');
$this->emitOne($result, $while->expression);
$result->out->write(') {');
$this->emitAll($result, $while->body);
$result->out->write('}');
}
protected function emitBreak($result, $break) {
$result->out->write('break ');
$break->expression && $this->emitOne($result, $break->expression);
$result->out->write(';');
}
protected function emitContinue($result, $continue) {
$result->out->write('continue ');
$continue->expression && $this->emitOne($result, $continue->expression);
$result->out->write(';');
}
protected function emitLabel($result, $label) {
$result->out->write($label->name.':');
}
protected function emitGoto($result, $goto) {
$result->out->write('goto '.$goto->label);
}
protected function emitInstanceOf($result, $instanceof) {
$type= $instanceof->type;
// Supported: instanceof T, instanceof $t, instanceof $t->MEMBER; instanceof T::MEMBER
// Unsupported: instanceof EXPR
if ($type instanceof Variable || $type instanceof InstanceExpression || $type instanceof ScopeExpression) {
$this->emitOne($result, $instanceof->expression);
$result->out->write(' instanceof ');
$this->emitOne($result, $type);
} else if ($type instanceof Node) {
$t= $result->temp();
$result->out->write('('.$t.'= ');
$this->emitOne($result, $type);
$result->out->write(')?');
$this->emitOne($result, $instanceof->expression);
$result->out->write(' instanceof '.$t.':null');
} else {
$this->emitOne($result, $instanceof->expression);
$result->out->write(' instanceof '.$type);
}
}
protected function emitArguments($result, $arguments) {
$i= 0;
foreach ($arguments as $argument) {
if ($i++) $result->out->write(',');
$this->emitOne($result, $argument);
}
}
protected function emitNew($result, $new) {
if ($new->type instanceof Node) {
$t= $result->temp();
$result->out->write('('.$t.'= ');
$this->emitOne($result, $new->type);
$result->out->write(') ? new '.$t.'(');
$this->emitArguments($result, $new->arguments);
$result->out->write(') : null');
} else {
$result->out->write('new '.$new->type.'(');
$this->emitArguments($result, $new->arguments);
$result->out->write(')');
}
}
protected function emitNewClass($result, $new) {
array_unshift($result->meta, []);
$result->out->write('(new class(');
$this->emitArguments($result, $new->arguments);
$result->out->write(')');
// Allow "extends self" to reference enclosing class (except if this
// class is an anonymous class!)
if ('self' === $new->definition->parent && $result->type && $result->type[0]->name) {
$result->out->write(' extends '.$result->type[0]->name);
} else if ($new->definition->parent) {
$result->out->write(' extends '.$new->definition->parent);
}
$new->definition->implements && $result->out->write(' implements '.implode(', ', $new->definition->implements));
array_unshift($result->type, $new->definition);
$result->out->write('{');
foreach ($new->definition->body as $member) {
$this->emitOne($result, $member);
$result->out->write("\n");
}
$result->out->write('function __new() {');
$this->emitMeta($result, null, [], null);
$result->out->write('return $this; }})->__new()');
array_shift($result->type);
}
protected function emitCallable($result, $callable) {
$this->emitOne($result, $callable->expression);
$result->out->write('(...)');
}
protected function emitInvoke($result, $invoke) {
$this->emitOne($result, $invoke->expression);
$result->out->write('(');
$this->emitArguments($result, $invoke->arguments);
$result->out->write(')');
}
protected function emitScope($result, $scope) {
if ($scope->type instanceof Variable) {
$this->emitOne($result, $scope->type);
$result->out->write('::');
$this->emitOne($result, $scope->member);
} else if ($scope->type instanceof Node) {
$t= $result->temp();
$result->out->write('('.$t.'=');
$this->emitOne($result, $scope->type);
$result->out->write(')?'.$t.'::');
$this->emitOne($result, $scope->member);
$result->out->write(':null');
} else if ($scope->member instanceof Literal && $result->lookup($scope->type)->rewriteEnumCase($scope->member->expression)) {
$result->out->write($scope->type.'::$'.$scope->member->expression);
} else {
$result->out->write($scope->type.'::');
$this->emitOne($result, $scope->member);
}
}
protected function emitInstance($result, $instance) {
if ('new' === $instance->expression->kind) {
$result->out->write('(');
$this->emitOne($result, $instance->expression);
$result->out->write(')->');
} else {
$this->emitOne($result, $instance->expression);
$result->out->write('->');
}
if ('literal' === $instance->member->kind) {
$result->out->write($instance->member->expression);
} else {
$result->out->write('{');
$this->emitOne($result, $instance->member);
$result->out->write('}');
}
}
protected function emitNullsafeInstance($result, $instance) {
$t= $result->temp();