| 1 | package Compile::Generators; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use 5.006001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | our $VERSION = '0.11'; | |||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Module::Compile -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 3 | my $label = 'GENAAAA'; | |||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | sub pmc_compile { | ||||||||
|
Subroutine
pmc_compile: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 12 | 3 | my ($class, $input) = @_; | |||||||||
| 13 | 3 | my $output; | |||||||||
| 14 | 3 | ▶ | while ($output = $class->compile($input)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 7 | ▶ | last if $output eq $input; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 4 | $input = $output; | |||||||||
| 17 | } | ||||||||||
| 18 | 3 | return $output; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 7 | ▶ | sub compile { | ||||||||
|
Subroutine
compile: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 22 | 7 | my ($class, $source) = @_; | |||||||||
| 23 | 7 | ▶ | return $source unless $source =~ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | /^(sub .*):generator (.*\n)((?s:.*?))(^\}.*\n)/m; | ||||||||||
| 25 | 4 | my $start = "$1$2"; | |||||||||
| 26 | 4 | my $end = $4; | |||||||||
| 27 | 4 | my ($static, $dynamic) = split /^\s*\n/m, $3, 2; | |||||||||
| 28 | 4 | ▶ | if (not defined $dynamic) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 3 | $dynamic = $static; | |||||||||
| 30 | 3 | $static = ''; | |||||||||
| 31 | } | ||||||||||
| 32 | 4 | ▶ | while ($dynamic =~ s/^(\s*yield\s*(.*))/#$1\n \$__GEN_STATE__ = '$label'; return $2; ${label}:/m) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 8 | $label++; | |||||||||
| 34 | } | ||||||||||
| 35 | # $dynamic =~ s/^/ /mg; | ||||||||||
| 36 | |||||||||||
| 37 | 4 | my $code = <<"_"; | |||||||||
| 38 | ${start} my \$__GEN_STATE__ = undef; | ||||||||||
| 39 | ${static} | ||||||||||
| 40 | return sub { | ||||||||||
| 41 | goto \$__GEN_STATE__ if defined \$__GEN_STATE__; | ||||||||||
| 42 | ${dynamic} | ||||||||||
| 43 | \$__GEN_STATE__ = '$label'; | ||||||||||
| 44 | ${label}: | ||||||||||
| 45 | return; | ||||||||||
| 46 | } | ||||||||||
| 47 | ${end} | ||||||||||
| 48 | _ | ||||||||||
| 49 | 4 | $label++; | |||||||||
| 50 | 4 | $source =~ s/^sub .*:generator .*\n(?s:.*?)^\}.*\n/$code/m; | |||||||||
| 51 | 4 | return $source; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 3 | 1; | |||||||||
| 55 | |||||||||||
| 56 | __DATA__ | ||||||||||