Perlito5/AST/Captures.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 181 CC 14 SCAR 28.6
CC
14
Cov
0%
get_captures
56.0
get_captures
20.0
get_captures
12.0
CRAP 17.5
1 use v5;
2  
3 use Perlito5::AST;
4 use strict;
5  
6 package Perlito5::AST::CompUnit;
7 {
8 sub get_captures { () }
9 }
10  
11 package Perlito5::AST::Int;
12 {
13 sub get_captures { () }
14 }
15  
16 package Perlito5::AST::Num;
17 {
18 sub get_captures { () }
19 }
20  
21 package Perlito5::AST::Buf;
22 {
23 sub get_captures { () }
24 }
25  
26 package Perlito5::AST::Block;
27 {
28 sub get_captures {
29 my ($self) = @_;
30 my @var;
31 for my $stmt (@{$self->{stmts}}) {
32 push @var, $stmt->get_captures();
33 }
34 return @var;
35 }
36 }
37  
38 package Perlito5::AST::Index;
39 {
40 sub get_captures {
41 my $self = shift;
42 my @var;
43 push @var, $self->{obj}->get_captures();
44 push @var, $self->{index_exp}->get_captures();
45 return @var;
46 }
47 }
48  
49 package Perlito5::AST::Lookup;
50 {
51 sub get_captures {
52 my $self = shift;
53 my @var;
54 push @var, $self->{obj}->get_captures();
55 push @var, $self->{index_exp}->get_captures();
56 return @var;
57 }
58 }
59  
60 package Perlito5::AST::Var;
61 {
62 sub get_captures {
63 my $self = shift;
64 if ($self->{sigil} eq '$#') {
65 return Perlito5::AST::Var->new( %$self, sigil => '@' );
66 }
67 return ($self);
68 }
69 }
70  
71 package Perlito5::AST::Decl;
72 {
73 sub get_captures {
74 # non-free variables are not captured
75 return { dont => $_[0]{var}{_id} }
76 }
77 }
78  
79 package Perlito5::AST::Call;
80 {
81 sub get_captures {
82 my $self = shift;
83 my @var;
84 push @var, $self->{method}->get_captures() if ref($self->{method});
85 push @var, $self->{invocant}->get_captures();
86 my $args = $self->{arguments};
87 if ($args) {
88 if (ref($args) eq "ARRAY") {
89 push @var, map { $_->get_captures() } @$args;
90 }
91 else {
92 push @var, $args->get_captures();
93 }
94 }
95 return @var;
96 }
97 }
98  
99 package Perlito5::AST::Apply;
100 {
101 sub get_captures {
102 my $self = shift;
103 my $code = $self->{code};
104 my @var;
105 push @var, $code->get_captures()
106 if ref($code);
107 push @var, map { $_->get_captures() }
108 @{ $self->{arguments} }
109 if $self->{arguments};
110 push @var, $self->{special_arg}->get_captures()
111 if ref($self->{special_arg});
112  
113 if ($code eq 'eval' && $self->{_scope}) {;
114 push @var, @{ $self->{_scope}{block} };
115 }
116  
117 if ($code eq 'my' || $code eq 'our' || $code eq 'state') {
118 push @var, ( map { ref($_) eq 'Perlito5::AST::Var'
119 ? ( { dont => $_->{_id} } )
120 : ()
121 }
122 @{ $self->{arguments} }
123 );
124 }
125 return @var;
126 }
127 }
128  
129 package Perlito5::AST::If;
130 {
131 sub get_captures {
132 my $self = shift;
133 my @var;
134 push @var, $self->{cond}->get_captures();
135 push @var, $self->{body}->get_captures();
136 push @var, $self->{otherwise}->get_captures();
137 return @var;
138 }
139 }
140  
141  
142 package Perlito5::AST::When;
143 {
144 sub get_captures {
145 my $self = shift;
146 my @var;
147 push @var, $self->{cond}->get_captures();
148 push @var, $self->{body}->get_captures();
149 return @var;
150 }
151 }
152  
153  
154 package Perlito5::AST::While;
155 {
156 sub get_captures {
157 my $self = shift;
158 my @var;
159 push @var, $self->{cond}->get_captures();
160 push @var, $self->{body}->get_captures();
161 return @var;
162 }
163 }
164  
165 package Perlito5::AST::For;
166 {
167 sub get_captures {
168 my $self = shift;
169 my @var;
170 my $body =
171 ref($self->{body}) ne 'Perlito5::AST::Block'
172 ? [ $self->{body} ]
173 : $self->{body}{stmts};
174 push @var, map { $_->get_captures() }
175 grep { defined }
176 @$body,
177 $self->{topic},
178 ( ref( $self->{cond} ) eq 'ARRAY'
179 ? @{ $self->{cond} }
180 : $self->{cond} );
181 return @var;
182 }
183 }
184  
185 package Perlito5::AST::Sub;
186 {
187 sub get_captures {
188 my $self = shift;
189 return if !$self->{block}; # predeclaration
190 return $self->{block}->get_captures()
191 }
192 }
193  
194 1;
195  
196 =begin
197  
198 =head1 NAME
199  
200 Perlito5::AST::Captures
201  
202 =head1 SYNOPSIS
203  
204 $ast->get_captures()
205  
206 =head1 DESCRIPTION
207  
208 This module generates a list of captured variables
209  
210 =head1 AUTHORS
211  
212 Flavio Soibelmann Glock <fglock@gmail.com>.
213  
214 =head1 COPYRIGHT
215  
216 Copyright 2015 by Flavio Soibelmann Glock.
217  
218 This program is free software; you can redistribute it and/or modify it
219 under the same terms as Perl itself.
220  
221 See L<http://www.perl.com/perl/misc/Artistic.html>
222  
223 =end