/
TODO-perlito5
567 lines (389 loc) · 15.2 KB
/
TODO-perlito5
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
TODO list for Perlito5
* perlito5 command-line
* libraries
-- these should probably go into namespace Perlito5X::*
-- Test (implemented as Perlito5::Test)
-- Data::Dumper (implemented as Perlito5X::Dumper)
-- create perlito5-specific libs for:
feature.pm
Config.pm
overload.pm
bytes.pm
integer.pm
lib.pm
Carp.pm
Tie::Array
Tie::Hash
Tie::Scalar
Symbol
alternately, check $^H for strictness - such that perl's own strict.pm just works
and ${^WARNING_BITS} for warnings
* Parser
-- "'" meaning "::"
$'m # $::m
$m' # String found where operator expected
package X'Y # X::Y
package X' # Invalid version format (non-numeric data)
-- attributes
http://perldoc.perl.org/attributes.html
missing MODIFY_CODE_ATTRIBUTES handlers
-- create __DATA__
%Perlito5::DATA_SECTION contains the __DATA__ for each package
-- compile-time eval() is not bound to the "program" environment, but to the "compiler" environment instead
see README-perlito5-js near "Compile-time / Run-time interleaving"
my $v;
BEGIN { $v = "123" }
use Module $v; # $v is not accessible at compile-time
-- parse the regexes
create an AST for regexes
-- prototypes (signatures)
http://perldoc.perl.org/perlsub.html#Prototypes
check that undeclared barewords give the right error
*foo = sub () { ... } # does prototype work here?
check signature in sort()
fix the prototype for 'stat(*)' (see t/test.pl in the perl test suite)
'&@' - See Try::Tiny
-- "namespace" parsing
tests: t5/01-perlito/26-syntax-namespace.t
$ perl -e ' { package X; sub print { CORE::print(">$_[1]<\n") } } my $x = bless {}, "X"; print $x "xxx" '
Not a GLOB reference at -e line 1.
$ perl -e ' { package X; sub printx { CORE::print(">$_[1]<\n") } } my $x = bless {}, "X"; printx $x "xxx" '
>xxx<
$ perl -MO=Deparse -e ' print X:: "xxx" '
print X 'xxx';
$ perl -e ' use strict; my $x = X::; print $x '
X
$ perl -e ' use strict; my $x = X; print $x '
Bareword "X" not allowed while "strict subs" in use
$ perl perlito5.pl -MO=Deparse -e ' ::X::x::y '
join("", ::{'main::X::'} x main::y);
$ perl -MO=Deparse -e ' ::X '
'???';
$ perl -MO=Deparse -e ' sub X {} ::X '
sub X { }
X;
$ perl -e ' $::X::::X = 3; print $main::X::::X ' # 3
$ perl -e ' $::X::::X = 3; print $main::main::X::::X ' # 3
$ perl -e ' $::X::::X = 3; print $main::X::main::X ' # empty
$ perl -e ' $::X::::X = 3; print $main::X::X ' # empty
$ perl -e ' $::X::::X = 3; print $::::X::::X ' # empty
-- CORE:: namespace can be used with operators:
$ perl -MO=Deparse -e ' $x CORE::and $v '
$v if $x;
$ perl -MO=Deparse -e ' @v = CORE::qw/ a b c / '
@v = ('a', 'b', 'c');
$ perl -MO=Deparse -e ' $x CORE::+ $v '
CORE:: is not a keyword
-- strict and warnings: create options like 'subs', 'refs'
-- things that work in perlito5, but which are errors in 'perl'
$ perl -e ' my @things= map { no warnings; 123 } @list; '
"no" not allowed in expression
quotes vs. hash lookups:
$ perl -e ' q}} '
# ok
$ perl -e ' $x{ q}} } '
Unmatched right curly bracket at -e line 1, at end of line
$ perl -e ' $x{ q]] } '
# ok
string interpolation with nested quotes of the same type:
$ perl -e ' " $x{"x"} " '
String found where operator expected at -e line 1, near "x"} ""
* Add tests for fixed bugs:
---
resolve "filehandle" methods
$ perl -e ' package X; open(X, ">", "x"); X->print(1234) '
$ cat x
1234
$ nodejs perlito5.js -Isrc5/lib -Cjs -e ' package X; open(X, ">", "x"); X->print(1234) '
---
variable declarations in expressions
our $Verbose ||= 0;
our (%Cache);
my $args = @_ or @_ = @$exports;
---
add additional variants of "for"
# "our" with localization
$ perl -e ' use strict; our $x = 123; for our $x (1,2,3) { 2 } print "$x\n" '
123
# variable in scope with localization
$ perl -e ' use strict; my $x = 123; for $x (1,2,3) { 2 } print "$x\n" '123
123
---
add "print", "printf", "say" special parsing - note this is related to indirect object notation
indirect object notation
http://lwn.net/Articles/451486/
http://www.modernperlbooks.com/mt/2009/08/the-problems-with-indirect-object-notation.html
http://shadow.cat/blog/matt-s-trout/indirect-but-still-fatal/
http://perlbuzz.com/mechanix/2008/02/the-perils-of-perl-5s-indirect.html
method Module $param;
new Class( arg => $value );
new Class::($args);
say $config->{output} "This is a diagnostic message!"; # indirect call
say {$config->{output}} "This is a diagnostic message!"; # say to filehandle
use Class;
sub Class {
warn 'Called Class sub not Class package';
'Class'
}
my $q = Class->new; # calls the Class sub above
my $s = new Class; # throws a 'Bareword found where operator expected' error
my $t = Class::->new # this works
my $u = new Class::; # this also works (even with sub main in the current package)
sbertrang++ noted this is also valid:
print( STDERR "123" )
---
add tests for signatures: "empty" _ $ ;$
---
add test for "sub _" should be in package "main"
$ perl -MO=Deparse -e ' package X; sub _ { 123 } '
package X;
sub main::_ {
123;
}
---
add test for defined-or vs. m// (2012/9/25 Конрад Боровски <notifications@github.com>)
Note: fixed; see test t5/01-perlito/25-syntax-defined-or.t
$ perl perlito5.pl -Isrc5/lib -Cast-perl5 -e ' shift // 2 '
Number or Bareword found where operator expected
$ perl perlito5.pl -Isrc5/lib -Cast-perl5 -e ' shift / 2 '
Can't find string terminator '/' anywhere before EOF
---
add test for filetest operators special case:
' -f($file).".bak" ' should be equivalent to -f "$file.bak"
parses as -(f($file)).".bak"
but: ' -f ($file).".bak" '
parses correctly
This seems to be because there is a rule that \w followed by '(' is a function call;
this needs more testing: ' ... and(2) '
Test: redefine 'and', 'not' and check what works.
' $s111++ + $s222 '
parses as (+$s222)++
' $step++ < $steps '
Can't find string terminator '>' anywhere before EOF
-- from moritz, Schwern and others at
http://stackoverflow.com/questions/161872/hidden-features-of-perl
- you can use letters as delimiters
$ perl -Mstrict -wle 'print q bJet another perl hacker.b'
Jet another perl hacker.
Likewise you can write regular expressions:
m xabcx
# same as m/abc/
* Perl6 backend
-- keep comments
-- context: wantarray, return-comma
sub x { return 7, 8 }
vs. sub x { return (7, 8) }
use an "out-of-band" parameter to set the call context, like:
$v = x( :scalar ) # 8
$v = x( :list ) # 2
-- <> is lines()
-- 0..$#num to @num.keys
-- choose @*ARGS or @_ in shift() and pop()
-- typeglob assignment
-- "given" statement not implemented
-- refactoring sub arguments
my $x = $_[0];
my ($x, $y, @rest) = @_; # check if @_ is unused elsewhere in the sub
-- placeholder
my ($a, $, $c) = 1..3;
($a, *, $c) = 1..3;
-- __PACKAGE__
-- specialized refactoring for packages that introduce syntax
Try::Tiny
List::MoreUtils
Moose
-- no strict
-- bless
-- tests
* Perl5 backend
-- "given" statement not implemented
-- "default" statement not implemented
-- ${^NAME} needs curly-escaping
- fix regex delimiters, or escape the regexes
* Javascript backend
-- DESTROY
Try::Tiny uses DESTROY to implement finally() - and it doesn't execute in js:
$ nodejs perlito5.js -Isrc5/lib -I. -I /usr/local/lib/perl5/site_perl/5.20.0 -e ' use Try::Tiny; try { print "this\n" }; try { die "this" } catch { print "catched\n" } finally { print "done\n" } '
this
catched
$ perl -e ' use Try::Tiny; try { print "this\n" }; try { die "this" } catch { print "catched\n" } finally { print "done\n" } '
this
catched
done
-- constant subroutines
-- prototype mismatch
$ perl -e ' sub X () { 123 } print X, "\n"; eval " sub X { 456 } "; '
123
Prototype mismatch: sub main::X () vs none at (eval 1) line 1.
Constant subroutine X redefined at (eval 1) line 1.
-- reference to scalar doesn't work
$ node perlito5.js -Isrc5/lib -I. -e ' use Data::Dumper; $v = [20, \$v ]; print Dumper ($v) '
$VAR1 = [
20,
\undef,
];
-- phase order
print x(); # js will try to execute this before the sub declaration
sub x { "ok\n"; }
# TypeError: Object [object Object] has no method 'x'
-- assign old-self to my / local
local $Some_Global = $Some_Global;
-- missing some types of subroutine signatures
-- AUTOLOAD() called from UNIVERSAL autovivifies packages
add tests
-- delete() in the middle of an array turns exists() off:
$ perl -e ' @a = (3..7); delete $a[2]; print "exists ", (exists $a[$_] ? 1 : 0), "\n" for 0 .. $#a '
exists 1
exists 1
exists 0
exists 1
exists 1
-- delete() in src5/lib/Perlito5/Grammar/String.pm doesn't seem to work:
delete($quote_flags->{$flag_to_reset});
delete($quote_flags->{last_flag});
-- "~~" operator not implemented; See also "when" implementation
-- "given" statement not implemented
-- "when" should use a "break" exception inside "given", and a "next" exception inside "for".
-- "default" statement not implemented
-- javascript errors don't show in the global error handler when running in node.js
-- "autoload" the compiler if eval-string or require() are used (eval-string needs the compiler at run-time)
https://github.com/fglock/Perlito/issues/23
-- symbol variables like $] ${"main::\$"} $#_
-- check that @_, $_, $a, $b and other special variables are in the right context (lexical, global, package global)
-- add alternate mro's
-- cache the mro
-- add regex compiler
-- /e modifier
-- /x modifier
-- support all perl5 regex syntax
-- @v = /x/g
-- regex variables localization in blocks
$ perl -e ' "a" =~ /(.)/; print $1; { "b" =~ /(.)/; print $1; } print $1, "\n"; '
aba
$ perl -e ' "a" =~ /(.)/; print $1; { "b" =~ //; print $1; } print $1, "\n"; '
abb
$ perl -e ' "a" =~ /(.)/; print $1; { "b" =~ /x/; print $1; } print $1, "\n"; '
aaa
-- some qr() and quotemeta() details
$ perl -e ' my $x = qr/ \A x /x; my $y = qr/$x y \Q[z]/; use Data::Dumper; print Dumper $x; print Dumper $y; '
$VAR1 = qr/(?x-ism: \A x )/;
$VAR1 = qr/(?-xism:(?x-ism: \A x ) y \[z\])/;
$ perl -e ' print " a b \Q [ a \nn"; '
a b \ \[\ a\ \
n
$ perl -e ' print "x\Q[\Qx]\Ex\n" '
x\[x\\\]x\ # '\' is quoted, but 'Q' disappears
-- qr() returns a Regexp object
{
package Regexp;
sub x { 123 }
}
$a = qr//;
print $a->x, "\n"; # 123
-- bug: variable redeclaration does not work
-- javascript "var" erases the outer value within the whole current lexical scope
-- bug: "my" variables - this doesn't work as expected: my $v = $v
possible fix: rename variables
possible fix: initialize variables to null
-- add tests using closures, to check that the redeclared variable is a different variable
$ perl -e ' my $x = 10; print "$x\n"; my $x; print "$x\n"; '
10
[space]
$ nodejs perlito5.js -Isrc5/lib -e ' my $x = 10; print "$x\n"; my $x; print "$x\n"; '
10
10
-- lvalue ternary: ($a_or_b ? $a : $b) = $c;
-- lvalue substr()
-- 4-arguments substr()
-- lvalue pos($str)
-- pos($str)
-- lvalue chomp(), chop()
-- lvalue $#a
-- lvalue subroutine
-- bug: variable aliases create copies instead
for (@x) { $_++ } # doesn't change @x
-- generate more compact code; maybe use more subroutines instead of inlining;
autovivification is probably the most verbose part of the code.
Use less "throw" - it is not (yet?) optimized by V8
-- in the browser: implement "use" with XMLHttpRequest (what are the security implications?)
-- aliasing between lexicals and globals
$ perl -e 'use strict; my $x = 3; *main::z = \$x; print $main::z; '
3
-- finish "overload" implementation
See: p5str
-- pack(), unpack()
-- BEGIN{} should execute in the environment of the program under compilation
-- BEGIN/END that are defined inside blocks/closures need to run inside some pseudo-environment
even if the closure was never created or used in the first place
-- flip-flop operator
if either operand to scalar '..' is a constant the value is implicitly compared to the input line number ($.)
-- caller() in nodejs and Chrome:
$ nodejs perlito5.js -Isrc5/lib -I. -e ' sub x { print JS::inline("new Error().stack") }; sub yy { x() }; my $f = sub { yy() }; $f->() '
"Error
at tmp104 [as x] (eval at <anonymous> (/perlito5.js:31586:57), <anonymous>:10:63)
at tmp105 [as yy] (eval at <anonymous> (/perlito5.js:31586:57), <anonymous>:18:26)
at tmp106 (eval at <anonymous> (/perlito5.js:31586:57), <anonymous>:27:28)
at eval (eval at <anonymous> (/perlito5.js:31586:57), <anonymous>:32:7)
..."
* Javascript - implemented but missing more tests
-- '&' prototype
add tests; see Try::Tiny
-- add symbol tables for scalar, array and hash
-- references to typeglobs:
$ perl -e ' print ref(\*main) '
GLOB
-- prototype() can be set by aliasing:
*x = sub ($$) { 123 }; *y = *x; print prototype(\&y) # $$
-- "or" has SCALAR context (Abigail++):
See: t5/01-perlito/23-eval.t
-- 'next', 'last' in expression
(*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
... ) and last
-- check that \(@a) and \@a have different meanings
-- 'x' in list context
@{$cache}{@$ok} = (1) x @$ok;
-- while () {}
this is implemented - it now needs some tests:
# http://blogs.perl.org/users/peter_martini/2014/05/spelunking-why-while-is-my-new-favorite-perl-ism.html
while () {} # infinite loop - while(1)
while (()) {} # no loop
-- pass @_ to &sub
$ node perlito5.js -I./src5/lib -Cjs -e ' @_ = (1,2); &foo; '
# call foo(@_)
* Nice to Have
-- keep comments
-- debugging symbols
-- line numbers in error messages
-- caller()
-- "when"
-- run more of the "perl" test suite
-- proper "use strict" and "use warnings"
-- use the same error messages and warnings as 'perl'
-- no warnings 'redefine';
-- __LINE__, __FILE__
-- INIT{}, END{}
look at the implementation in perlito6-in-Go
-- source code - remove Perl 6 code such as "token"
(fixed: This is only loaded if the grammar compiler is needed)
-- *{ $name }{CODE}->();
-- local(*{$caller."::a"}) = \my $a;
-- *{$pkg . "::foo"} = \&bar;
-- local $SIG{__WARN__};
-- bug https://github.com/fglock/Perlito/issues/10
"Perlito 5 JS has syntax errors"
Tried
YUI Compressor online
and
Google Closure Compiler
http://closure-compiler.appspot.com/home
Both failed with syntax errors.
* Oddities
-- from moritz, Schwern and others at
http://stackoverflow.com/questions/161872/hidden-features-of-perl
- you can give subs numeric names if you use symbolic references
$ perl -lwe '*4 = sub { print "yes" }; 4->()'
yes
* Deprecate
-- Interpreter backend
this is not being maintained; the code is still in src5/lib/Perlito5/Eval.pm just in case
alternately, use the interpreter to compute constant foldings