From: Vincent Pit Date: Tue, 5 Aug 2008 13:14:35 +0000 (+0200) Subject: Add support for constant ranges, and tests for kinds all ranges X-Git-Tag: v0.02~4 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Nary.git;a=commitdiff_plain;h=cab48037c2f684dd60d1860bdc2efa3d10d3e7a3 Add support for constant ranges, and tests for kinds all ranges --- diff --git a/lib/Sub/Nary.pm b/lib/Sub/Nary.pm index 5c26315..427ee8c 100644 --- a/lib/Sub/Nary.pm +++ b/lib/Sub/Nary.pm @@ -204,7 +204,7 @@ $ops{$_} = 0 for qw/stub nextstate/; $ops{$_} = 1 for qw/padsv/; $ops{$_} = 'list' for qw/padav/; $ops{$_} = 'list' for qw/padhv rv2hv/; -$ops{$_} = 'list' for qw/padany flip match entereval readline/; +$ops{$_} = 'list' for qw/padany match entereval readline/; $ops{each} = { 0 => 0.5, 2 => 0.5 }; $ops{stat} = { 0 => 0.5, 13 => 0.5 }; @@ -446,6 +446,32 @@ sub pp_aassign { $_[0]->expect_any($_[1]->first) } sub pp_leaveloop { $_[0]->expect_return($_[1]->first->sibling) } +sub pp_flip { + my ($self, $op) = @_; + + $op = $op->first; + return 'list' if name($op) ne 'range'; + + my $begin = $op->first; + if (name($begin) eq 'const') { + my $end = $begin->sibling; + if (name($end) eq 'const') { + $begin = $self->const_sv($begin); + $end = $self->const_sv($end); + no warnings 'numeric'; + return int(${$end->object_2svref}) - int(${$begin->object_2svref}) + 1; + } else { + my ($p, $r) = $self->expect_return($end); + return $p => 1 if $r; + } + } else { + my ($p, $r) = $self->expect_return($begin); + return $p => 1 if $r; + } + + return 'list' +} + =head1 EXPORT An object-oriented module shouldn't export any function, and so does this one. diff --git a/t/20-return.t b/t/20-return.t index e7e852d..191a386 100644 --- a/t/20-return.t +++ b/t/20-return.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 50; +use Test::More tests => 56; use Sub::Nary; @@ -46,7 +46,13 @@ my @tests = ( [ sub { return $x, @a }, 'list' ], [ sub { return %h, $y }, 'list' ], - [ sub { return 1 .. 3 }, 'list' ], + [ sub { return 1 .. 3 }, 3 ], + [ sub { return $x .. 3 }, 'list' ], + [ sub { return 1 .. $x }, 'list' ], + [ sub { return '2foo' .. 4 }, 3 ], + [ sub { my @a = (7, 8); return @a .. 4 }, 'list' ], + [ sub { return do { return 1, 2 } .. 3 }, 2 ], + [ sub { return 1 .. do { return 2, 3 } }, 2 ], [ sub { for (1, 2, 3) { return } }, 0 ], [ sub { for (1, 2, 3) { } return 1, 2; }, 2 ], diff --git a/t/21-list.t b/t/21-list.t index 6de15c2..3acf659 100644 --- a/t/21-list.t +++ b/t/21-list.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 36; +use Test::More tests => 40; use Sub::Nary; @@ -40,8 +40,12 @@ my @tests = ( [ sub { $x, @a }, 'list' ], [ sub { %h, $y }, 'list' ], - [ sub { 1 .. 3 }, 'list' ], - [ sub { my @a = (1 .. 4) }, 4 ], + [ sub { 1 .. 3 }, 3 ], + [ sub { $x .. 3 }, 'list' ], + [ sub { 1 .. $x }, 'list' ], + [ sub { '2foo' .. 4 }, 3 ], + [ sub { my @a = (7, 8); @a .. 4 }, 'list' ], + [ sub { my @a = (1 .. 4) }, 4 ], [ sub { "banana" =~ /(a)/g }, 'list' ],