develooper Front page | perl.perl5.porters | Postings from August 2008

Re: unless(...) terser than if(!...)

Thread Previous | Thread Next
From:
Vincent Pit
Date:
August 29, 2008 04:44
Subject:
Re: unless(...) terser than if(!...)
Message ID:
48B7E0E9.2050106@profvince.com

> Couldn't we optimise the former to the latter fairly easily?
Here's a patch that (sort of) does this, with a little bonus.

There was already a bit of logic for that in new_logop that's used for
"unless ($x) { }" and "until ($x) { }". But at this point it's not
possible to optimize "if (!$x) { }" because we can't tell if our AND|OR
op is describing an "if (!$x) { }" (which we can optimize) or a "!$x &&
$y" (which we can't), as we need to know that we're in void context and
this is only set later. The only way I found was to check for "if (!$x)
{ }" in scalarvoid(), when we know we're in void context. We can't free
the NOT op, because there may still be next pointers to it, but
nullifying it is fine.

The good news is, we can recognize in new_logop constructs like "!$x &&
!$y" and turn them into "!($x || $y)", which enables a sweet chain
reaction for "!$x && !$y && !$z && !$t && ...".

Both combined give :

$ ./perl -Ilib -MO=Concise -e 'print if !$a && !$b && !$c'
a  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
-     <1> null vK/1 ->a
6        <|> or(other->7) vK/1 ->a
-           <1> ex-not sK/1 ->6
b              <|> or(other->c) sK/1 ->6
4                 <|> or(other->5) sK/1 ->b
-                    <1> ex-rv2sv sK/1 ->4
3                       <#> gvsv[*a] s ->4
-                    <1> ex-rv2sv sK/1 ->-
5                       <#> gvsv[*b] s ->6
-                 <1> ex-rv2sv sK/1 ->-
c                    <#> gvsv[*c] s ->6
9           <@> print vK ->a
7              <0> pushmark s ->8
-              <1> ex-rv2sv sK/1 ->9
8                 <#> gvsv[*_] s ->9
-e syntax OK

As for overloading, I don't know.

Vincent.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About