Skip to content

Commit 1aa5804

Browse files
committed
Perlito5 - parser - autoquote fix
1 parent 376e088 commit 1aa5804

File tree

7 files changed

+61
-15
lines changed

7 files changed

+61
-15
lines changed

TODO-perlito5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ TODO list for Perlito5
6161
--- check signature in sort()
6262
--- fix the prototype for 'stat(*)' (see t/test.pl in the perl test suite)
6363

64-
-- $v{x::y} autoquotes the index when x::y() exists
6564
-- $v->{x} doesn't interpolate inside double quotes
6665
-- deref inside double quotes:
6766
$ node perlito5.js -Bjs -e ' my $x = "123"; my $y = \$x; print "[$$y]\n" '

html/perlito5.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,13 @@ var p5100 = p5pkg['main'];
18921892
var v_index;
18931893
(v_index = (List__.shift()));
18941894
if ( (p5bool(p5call(v_index, "isa", ['Perlito5::AST::Apply'], 0)) && p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('bareword'))) ) {
1895-
throw(p5call(p5pkg["Perlito5::AST::Val::Buf"], "new", ['buf', (p5str(( p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) ? (p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) + '::') : '')) + p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('code')))], p5want));
1895+
(function () {
1896+
var v_full_name;
1897+
(v_full_name = ((p5str(( p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) ? (p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) + '::') : '')) + p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('code')))));
1898+
if ( !( (p5pkg["Perlito5"]["v_PROTO"])._hash_.hasOwnProperty(v_full_name)) ) {
1899+
throw(p5call(p5pkg["Perlito5::AST::Val::Buf"], "new", ['buf', v_full_name], p5want));
1900+
};
1901+
})();
18961902
};
18971903
return (p5context([v_index], p5want));
18981904
}
@@ -2468,7 +2474,13 @@ var p5100 = p5pkg['main'];
24682474
var v_level;
24692475
(v_level = (List__.shift()));
24702476
if ( (p5bool(p5call(v_index, "isa", ['Perlito5::AST::Apply'], 0)) && p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('bareword'))) ) {
2471-
(v_index = (p5call(p5pkg["Perlito5::AST::Val::Buf"], "new", ['buf', (p5str(( p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) ? (p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) + '::') : '')) + p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('code')))], 0)));
2477+
(function () {
2478+
var v_full_name;
2479+
(v_full_name = ((p5str(( p5bool((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) ? (p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('namespace')) + '::') : '')) + p5str((v_index || (v_index = new p5HashRef({})))._hash_.p5hget('code')))));
2480+
if ( !( (p5pkg["Perlito5"]["v_PROTO"])._hash_.hasOwnProperty(v_full_name)) ) {
2481+
(v_index = (p5call(p5pkg["Perlito5::AST::Val::Buf"], "new", ['buf', v_full_name], 0)));
2482+
};
2483+
})();
24722484
};
24732485
return (p5pkg["Perlito5::Javascript2"].to_str([v_index, v_level], p5want));
24742486
});

perlito5.pl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7590,7 +7590,10 @@ sub Perlito5::AST::Lookup::autoquote {
75907590
((my $self) = shift());
75917591
((my $index) = shift());
75927592
if (($index->isa('Perlito5::AST::Apply') && $index->{'bareword'})) {
7593-
return (Perlito5::AST::Val::Buf->new('buf', ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'})))
7593+
((my $full_name) = ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'}));
7594+
if (!(exists($Perlito5::PROTO->{$full_name}))) {
7595+
return (Perlito5::AST::Val::Buf->new('buf', $full_name))
7596+
}
75947597
};
75957598
$index
75967599
};
@@ -7998,7 +8001,10 @@ package Perlito5::Javascript2;
79988001
((my $index) = shift());
79998002
((my $level) = shift());
80008003
if (($index->isa('Perlito5::AST::Apply') && $index->{'bareword'})) {
8001-
($index = Perlito5::AST::Val::Buf->new('buf', ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'})))
8004+
((my $full_name) = ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'}));
8005+
if (!(exists($Perlito5::PROTO->{$full_name}))) {
8006+
($index = Perlito5::AST::Val::Buf->new('buf', $full_name))
8007+
}
80028008
};
80038009
return (to_str($index, $level))
80048010
};
@@ -9563,7 +9569,10 @@ package Perlito5::Javascript3;
95639569
((my $index) = shift());
95649570
((my $level) = shift());
95659571
if (($index->isa('Perlito5::AST::Apply') && $index->{'bareword'})) {
9566-
($index = Perlito5::AST::Val::Buf->new('buf', ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'})))
9572+
((my $full_name) = ((($index->{'namespace'} ? ($index->{'namespace'} . '::') : '')) . $index->{'code'}));
9573+
if (!(exists($Perlito5::PROTO->{$full_name}))) {
9574+
($index = Perlito5::AST::Val::Buf->new('buf', $full_name))
9575+
}
95679576
};
95689577
return (to_str($index, $level))
95699578
};

src5/lib/Perlito5/AST.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ sub autoquote {
5858
&& $index->{bareword}
5959
)
6060
{
61-
return Perlito5::AST::Val::Buf->new( buf => ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code} );
61+
my $full_name = ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code};
62+
if ( !exists $Perlito5::PROTO->{$full_name} ) {
63+
return Perlito5::AST::Val::Buf->new( buf => $full_name );
64+
}
6265
}
6366

6467
$index;

src5/lib/Perlito5/Javascript2/Emitter.pm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,12 @@ package Perlito5::Javascript2;
398398
&& $index->{bareword}
399399
)
400400
{
401-
$index = Perlito5::AST::Val::Buf->new(
402-
buf => ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code}
403-
);
401+
my $full_name = ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code};
402+
if ( !exists $Perlito5::PROTO->{$full_name} ) {
403+
$index = Perlito5::AST::Val::Buf->new(
404+
buf => $full_name
405+
);
406+
}
404407
}
405408

406409
return to_str($index, $level);

src5/lib/Perlito5/Javascript3/Emitter.pm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,14 @@ package Perlito5::Javascript3;
410410
&& $index->{bareword}
411411
)
412412
{
413-
$index = Perlito5::AST::Val::Buf->new(
414-
buf => ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code}
415-
);
413+
my $full_name = ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code};
414+
if ( !exists $Perlito5::PROTO->{$full_name} ) {
415+
$index = Perlito5::AST::Val::Buf->new(
416+
buf => $full_name
417+
);
418+
}
416419
}
417-
420+
418421
return to_str($index, $level);
419422
}
420423

t5/01-perlito/05-hash.t

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use v5;
22
use strict;
33
use feature 'say';
44

5-
say '1..11';
5+
say '1..15';
66
my %a;
77
say 'ok 1 - create hash';
88
$a{abc} = 3;
@@ -54,3 +54,20 @@ print 'not ' if !defined $c1->{c};
5454
say "ok 11 - defined item";
5555

5656

57+
# autoquote
58+
59+
my %v;
60+
sub x1 () { 1230 } $v{x1()} = 120; # '1230' => 120
61+
sub x2 () { 1231 } $v{x2} = 121; # 'x2' => 121
62+
sub x3 () { 1232 } $v{main::x3} = 122; # '1232' => 122
63+
64+
{
65+
no strict 'subs';
66+
$v{main::x4} = 123; # 'main::x4' => 123
67+
}
68+
69+
print 'not ' if $v{'1230'} != 120; say "ok 12 - no autoquote for function call with parenthesis";
70+
print 'not ' if $v{'x2'} != 121; say "ok 13 - autoquote for bareword without colons";
71+
print 'not ' if $v{'1232'} != 122; say "ok 14 - no autoquote for function call without parenthesis";
72+
print 'not ' if $v{'main::x4'} != 123; say "ok 15 - autoquote for bareword with colons";
73+

0 commit comments

Comments
 (0)