]> git.vpit.fr Git - perl/modules/Sub-Nary.git/blobdiff - lib/Sub/Nary.pm
Test for definedness of returned values because 0 is really { 0 => 1 } in our context
[perl/modules/Sub-Nary.git] / lib / Sub / Nary.pm
index b6ad798ebbe50604af0198ef7d183e8ebb90e5f2..52a3f8402e4c4783a0441da8ed35b24f4466ad9f 100644 (file)
@@ -262,7 +262,7 @@ sub inspect {
 
   my $op = $op->first;
   my ($r1, $l1) = $self->inspect($op);
-  return $r1, $l1 if $r1 and zero $l1;
+  return $r1, $l1 if defined $r1 and zero $l1;
   my $c = count $l1;
 
   $op = $op->sibling;
@@ -440,7 +440,7 @@ sub pp_const {
  if ($c eq 'AV') {
   $n = $sv->FILL + 1
  } elsif ($c eq 'HV') {
-  $n = 2 * $sv->FILL
+  $n = 2 * $sv->KEYS
  }
 
  return undef, $n
@@ -484,21 +484,30 @@ sub pp_leaveloop {
 
  $op = $op->first;
  my ($r1, $l1);
- if (name($op) eq 'enteriter') {
+ my $for;
+ if (name($op) eq 'enteriter') { # for loop ?
+  $for = 1;
   ($r1, $l1) = $self->inspect($op);
-  return $r1, $l1 if $r1 and zero $l1;
+  return $r1, $l1 if defined $r1 and zero $l1;
  }
 
  $op = $op->sibling;
- my $r;
+ my ($r2, $l2);
  if (name($op->first) eq 'and') {
-  ($r, my $l) = ($self->inspect($op->first->first))[0];
-  return $r, $l if $r and zero $l;
-  $r = ($self->inspect($op->first->first->sibling))[0];
+  ($r2, $l2) = $self->inspect($op->first->first);
+  return $r2, $l2 if defined $r2 and zero $l2;
+  my $c = count $l2;
+  return { list => 1 }, undef if !$for and defined $r2;
+  my ($r3, $l3) = $self->inspect($op->first->first->sibling);
+  return { list => 1 }, undef if defined $r3 and defined $l3;
+  $r2 = add $r2, scale $c, $r3;
  } else {
-  $r = ($self->inspect($op))[0];
+  ($r2, $l2) = $self->inspect($op);
+  return { list => 1 }, undef if defined $r2 and defined $l2;
  }
 
+ my $r = (defined $r1) ? add $r1, scale +(1 - count $r1), $r2
+                       : $r2;
  my $c = 1 - count $r;
  diag "& leaveloop $c" if $DEBUG;
  return $r, $c ? { 0 => $c } : undef;
@@ -542,11 +551,12 @@ sub pp_grepwhile {
  $op = $op->first->sibling;
 
  my ($r2, $l2) = $self->inspect($op->sibling);
- return $r2, $l2 if $r2 and zero $l2;
+ return $r2, $l2 if defined $r2 and zero $l2;
  my $c2 = count $l2; # First one to happen
 
  my ($r1, $l1) = $self->inspect($op);
- return (add $r2, scale $c2, $r1), undef if $r1 and zero $l1 and not zero $l2;
+ return (add $r2, scale $c2, $r1), undef if defined $r1 and zero $l1
+                                                        and not zero $l2;
  diag Dumper [ [ $r1, $l1 ], [ $r2, $l2 ] ] if $DEBUG;
  my $c1 = count $l1;
 
@@ -566,11 +576,12 @@ sub pp_mapwhile {
  $op = $op->first->sibling;
 
  my ($r2, $l2) = $self->inspect($op->sibling);
- return $r2, $l2 if $r2 and zero $l2;
+ return $r2, $l2 if defined $r2 and zero $l2;
  my $c2 = count $l2; # First one to happen
 
  my ($r1, $l1) = $self->inspect($op);
- return (add $r2, scale $c2, $r1), undef if $r1 and zero $l1 and not zero $l2;
+ return (add $r2, scale $c2, $r1), undef if defined $r1 and zero $l1
+                                                        and not zero $l2;
  diag Dumper [ [ $r1, $l1 ], [ $r2, $l2 ] ] if $DEBUG;
  my $c1 = count $l1;