This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #77452] Deparse BEGIN blocks in the right place
authorFather Chrysostomos <sprout@cpan.org>
Fri, 7 Nov 2014 04:32:58 +0000 (20:32 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Fri, 7 Nov 2014 06:18:55 +0000 (22:18 -0800)
commit8635e3c238f87f82dab918053e6f0b6a1a2525e6
tree8b1e97bb9b986be7fcc83d38e6c96fb5e421ebaa
parente13b6327463c1929bcd39bcdb74881e384ae113a
[perl #77452] Deparse BEGIN blocks in the right place

In the op tree, a statement consists of a nextstate/dbstate op (of
class cop) followed by the contents of the statement.  This cop is
created after the statement has been parsed.  So if you have nested
statements, the outermost statement has the highest sequence number
(cop_seq).  Every sub (including BEGIN blocks) has a sequence number
indicating where it occurs in its containing sub.

So

 BEGIN { } #1
 # seq 2
 {
   # seq 1
   ...
 }

is indistinguishable from

 # seq 2
 {
   BEGIN { } #1
   # seq 1
   ...
 }

because the sequence number of the BEGIN block is 1 in both examples.

By reserving a sequence number at the start of every block and using
it once the block has finished parsing, we can do this:

 BEGIN { } #1
 # seq 1
 {
   # seq 2
   ...
 }

 # seq 1
 {
   BEGIN { } #2
   # seq 2
   ...
 }

and now B::Deparse can tell where to put the blocks.

PL_compiling.cop_seq was unused, so this is where I am stashing
the pending sequence number.
ext/B/B/Concise.pm
lib/B/Deparse.pm
lib/B/Deparse.t
op.c
pad.c
perly.act
perly.h
perly.tab
perly.y