Permalink
Browse files

Perlito5 - perl6 - refactor_range_operator - use .keys

  • Loading branch information...
1 parent 1bbe89c commit f7a7e8ff304b513763babc71f57c2e8d379a8e1b @fglock committed Oct 17, 2013
Showing with 56 additions and 5 deletions.
  1. +20 −2 perlito5.pl
  2. +4 −0 src5/lib/Perlito5/Perl6/Emitter.pm
  3. +32 −3 src5/lib/Perlito5/Perl6/TreeGrammar.pm
View
@@ -12291,11 +12291,26 @@ package Perlito5::Perl6::TreeGrammar;
# use Perlito5::TreeGrammar
sub Perlito5::Perl6::TreeGrammar::refactor_range_operator {
my($class, $in) = @_;
- Perlito5::TreeGrammar::render(['And' => ['Lookup' => 'code', ['Value' => 'infix:<..>']], ['Lookup' => 'arguments', ['And' => ['Index' => 0, ['And' => ['Ref' => 'Perlito5::AST::Val::Int'], ['Lookup' => 'int', ['Value' => 0]]]], ['Index' => 1, ['And' => ['Ref' => 'Perlito5::AST::Val::Int'], ['Action' => sub {
+ Perlito5::TreeGrammar::render(['And' => ['Lookup' => 'code', ['Value' => 'infix:<..>']], ['Lookup' => 'arguments', ['And' => ['Index' => 0, ['And' => ['Ref' => 'Perlito5::AST::Val::Int'], ['Lookup' => 'int', ['Value' => 0]]]], ['Index' => 1, ['Or' => ['And' => ['Ref' => 'Perlito5::AST::Val::Int'], ['Action' => sub {
$in->{'code'} = 'p6_prefix:<^>';
$_[0]->{'int'}++;
shift(@{$in->{'arguments'}})
- }]]]]]], $in)
+ }]], ['And' => ['Ref' => 'Perlito5::AST::Apply'], ['Lookup' => 'code', ['Value' => 'prefix:<$#>']], ['Action' => sub {
+ bless($in, 'Perlito5::AST::Call');
+ delete($in->{'code'});
+ $in->{'method'} = 'keys';
+ shift(@{$in->{'arguments'}});
+ my $invocant = shift(@{$in->{'arguments'}});
+ $in->{'invocant'} = $invocant->{'arguments'}->[0]
+ }]], ['And' => ['Ref' => 'Perlito5::AST::Var'], ['Lookup' => 'sigil', ['Value' => '$#']], ['Action' => sub {
+ bless($in, 'Perlito5::AST::Call');
+ delete($in->{'code'});
+ $in->{'method'} = 'keys';
+ $in->{'arguments'} = [];
+ my $invocant = $_[0];
+ $invocant->{'sigil'} = '@';
+ $in->{'invocant'} = $invocant
+ }]]]]]]], $in)
}
sub Perlito5::Perl6::TreeGrammar::refactor_while_glob {
my($class, $in) = @_;
@@ -12671,6 +12686,9 @@ package Perlito5::AST::Apply;
}
if ($code eq 'infix:<..>') {
Perlito5::Perl6::TreeGrammar->refactor_range_operator($self);
+ if (ref($self) ne 'Perlito5::AST::Apply') {
+ return($self->emit_perl6())
+ }
$code = $self->{'code'}
}
$code = $op_translate{$code}
@@ -442,6 +442,10 @@ package Perlito5::AST::Apply;
}
if ( $code eq 'infix:<..>' ) {
Perlito5::Perl6::TreeGrammar->refactor_range_operator($self);
+ if (ref($self) ne 'Perlito5::AST::Apply') {
+ # the node was refactored into something else
+ return $self->emit_perl6();
+ }
$code = $self->{code};
}
@@ -12,15 +12,44 @@ sub refactor_range_operator {
[ Lookup => 'int', [ Value => 0 ] ]
]
], # first argument is 0
- [ Index => 1, [ And => [ Ref => 'Perlito5::AST::Val::Int' ],
+ [ Index => 1, [ Or =>
+ # 0 .. 9 ==> ^10
+ [ And => [ Ref => 'Perlito5::AST::Val::Int' ],
[ Action => sub {
$in->{code} = 'p6_prefix:<^>';
$_[0]{int}++;
shift @{ $in->{arguments} };
}
],
- ]
- # TODO 0..$#num to @num.keys
+ ],
+ # 0..$#$num to $num.keys
+ [ And => [ Ref => 'Perlito5::AST::Apply' ],
+ [ Lookup => 'code', [ Value => 'prefix:<$#>' ] ],
+ [ Action => sub {
+ bless $in, 'Perlito5::AST::Call';
+ delete $in->{code};
+ $in->{method} = 'keys';
+ shift @{ $in->{arguments} };
+ my $invocant = shift @{ $in->{arguments} };
+ $in->{invocant} = $invocant->{arguments}[0];
+ }
+ ],
+ ],
+ # 0..$#num to @num.keys
+ [ And => [ Ref => 'Perlito5::AST::Var' ],
+ [ Lookup => 'sigil', [ Value => '$#' ] ],
+ [ Action => sub {
+ bless $in, 'Perlito5::AST::Call';
+ delete $in->{code};
+ $in->{method} = 'keys';
+ $in->{arguments} = [];
+ my $invocant = $_[0];
+ $invocant->{sigil} = '@';
+ $in->{invocant} = $invocant;
+ }
+ ],
+ ],
+ ]
],
]
]

0 comments on commit f7a7e8f

Please sign in to comment.