blib/lib/Syntax/Keyword/Match/Deparse.pm

filter: Statementstmt 80.0% 88 / 110 Branchbran 56.0% 28 / 50 Conditioncond 46.8% 15 / 32 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 62.8% 142 / 226 CC 37 SCAR 33.2
CC
37
Cov
68%
op_dump
42.0
pp_leave
35.8
is_match_on_topic
9.4
CRAP 27.7
↑ Summary blib/lib/Syntax/Keyword/Match.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2024 -- leonerd@leonerd.org.uk
5  
6 package Syntax::Keyword::Match::Deparse 0.15;
7  
8 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use B qw( opnumber OPf_KIDS OPf_STACKED );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 require B::Deparse;
14  
15 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 OP_AND => opnumber('and'),
17 OP_COND_EXPR => opnumber('cond_expr'),
18 OP_CUSTOM => opnumber('custom'),
19 OP_ENTER => opnumber('enter'),
20 OP_LINESEQ => opnumber('lineseq'),
21 OP_MATCH => opnumber('match'),
22 OP_NULL => opnumber('null'),
23 OP_OR => opnumber('or'),
24 OP_PADSV => opnumber('padsv'),
25 OP_PADSV_STORE => opnumber('padsv_store'),
26 OP_SASSIGN => opnumber('sassign'),
27 1 };
28  
29 =head1 NAME
30  
31 C<Syntax::Keyword::Match::Deparse> - L<B::Deparse> support for L<Syntax::Keyword::Match>
32  
33 =head1 DESCRIPTION
34  
35 Loading this module will apply some hacks onto L<B::Deparse> that attempts to
36 provide deparse support for code which uses the syntax provided by
37 L<Syntax::Keyword::Match>.
38  
39 =cut
40  
41 1 my $orig_pp_leave;
42 {
43 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 $orig_pp_leave = *{"B::Deparse::pp_leave"}{CODE};
46 1 *{"B::Deparse::pp_leave"} = \&pp_leave;
47 }
48  
49 sub op_dump
50 0 ▶{
Subroutine op_dump: not called CC=6 SCAR=37.4
Pod undocumented
51 0 my $o = shift;
52 0 my $ret = $o->name;
53  
54 0 ▶ my $kid = $o->flags & OPf_KIDS ? $o->first : undef;
Branch: 1 branch
BranchTrueFalseTotal
$o->flags & 4 ? : 0 0 0
55 0 ▶ if( $kid && !B::Deparse::null($kid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid and not B::Deparse::null($kid)) 0 0 0
Condition0%0/3
$kid and not B::Deparse::null($kid)
!ll&&!rl&&r
000
MC/DC0%0/2
$kid and not B::Deparse::null($kid)
$kid not B::Deparse::null($kid)
Truth table0%0/3
A and not(B)
A= $kidB= not B::Deparse::null($kid)
ABresult
0X0
100
111
56 0 $ret .= "[\n";
57 0 ▶ while( $kid && !B::Deparse::null($kid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid and not B::Deparse::null($kid)) 0 0 0
Condition0%0/3
$kid and not B::Deparse::null($kid)
!ll&&!rl&&r
000
MC/DC0%0/2
$kid and not B::Deparse::null($kid)
$kid not B::Deparse::null($kid)
Truth table0%0/3
A and not(B)
A= $kidB= not B::Deparse::null($kid)
ABresult
0X0
100
111
58 0 $ret .= join( "\n", map { " $_" } split m/\n/, op_dump($kid) ) . "\n";
59 0 $kid = $kid->sibling;
60 }
61 0 $ret .= "]";
62 }
63  
64 0 return $ret;
65 }
66  
67 1 my %operator_for_name = (
68 eq => "==",
69 seq => "eq",
70 match => "=~",
71 isa => "isa",
72 );
73  
74 sub operator_name
75 7 ▶{
Subroutine operator_name: called CC=2 SCAR=7.1
Pod undocumented
76 7 my ( $o ) = @_;
77 7 my $opname = $o->name;
78 7 ▶ return $operator_for_name{$opname} // die "TODO: operator name of $opname";
Condition50%1/2
$operator_for_name{$opname} // die("TODO: operator name of $opname")
l!l
70
MC/DC0%0/1
$operator_for_name{$opname} // die("TODO: operator name of $opname")
$operator_for_name{$opname}
Truth table50%1/2
A
A= $operator_for_name{$opname}
Aresult
11
00
79 }
80  
81 sub is_match_on_topic
82 15 ▶{
Subroutine is_match_on_topic: called CC=5 SCAR=22.5
Pod undocumented
83 15 my ( $o, $topicix ) = @_;
84  
85 15 ▶ $o->type == OP_MATCH or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$o->type == 31 or return 0 2 13 15
86  
87 2 ▶ if( $^V ge v5.22.0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^V ge v5.22.0) { } 2 0 2
elsif ($o->flags & 64) { } 0 0 0
88 # Perl 5.22 could do OP_MATCH on targ
89 2 return $o->targ == $topicix;
90 }
91 elsif( $o->flags & OPf_STACKED ) {
92 0 my $kid = $o->first;
93 0 ▶ return $kid->type == OP_PADSV && $kid->targ == $topicix;
Condition0%0/3
$kid->type == 9 && $kid->targ == $topicix
!ll&&!rl&&r
000
MC/DC0%0/2
$kid->type == 9 && $kid->targ == $topicix
$kid->type == 9 $kid->targ == $topicix
Truth table0%0/3
A && B
A= $kid->type == 9B= $kid->targ == $topicix
ABresult
0X0
100
111
94 }
95 else {
96 0 return 0;
97 }
98 }
99  
100 sub pp_leave
101 7 ▶{
Subroutine pp_leave: called CC=27 SCAR=35.8
Pod undocumented
102 7 my $self = shift;
103 7 my ( $op ) = @_;
104  
105 7 my $enter = $op->first;
106 7 ▶ $enter->type == OP_ENTER or
Branch: 1 branch
BranchTrueFalseTotal
$enter->type == 205 or return $self->$orig_pp_leave(@_) 7 0 7
107 return $self->$orig_pp_leave( @_ );
108  
109 7 my $assign = $enter->sibling;
110 7 my $topicix; my $topicop;
111 7 ▶ if( $^V ge v5.38.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^V ge v5.38.0) { } 7 0 7
112 # Since perl 5.38.0 we had OP_PADSV_STORE
113 7 ▶ $assign->type == OP_PADSV_STORE or
Branch: 1 branch
BranchTrueFalseTotal
$assign->type == 10 or return $self->$orig_pp_leave(@_) 7 0 7
114 return $self->$orig_pp_leave( @_ );
115  
116 7 my $varname = $self->padname( $topicix = $assign->targ );
117 7 ▶ $varname eq '$(Syntax::Keyword::Match/topic)' or
Branch: 1 branch
BranchTrueFalseTotal
$varname eq "\$(Syntax::Keyword::Match/topic)" or return $self->$orig_pp_leave(@_) 7 0 7
118 return $self->$orig_pp_leave( @_ );
119  
120 7 $topicop = $assign->first;
121 }
122 else {
123 # Earlier perls had regular OP_SASSIGN with OP_PADSV target
124 0 ▶ $assign->type == OP_SASSIGN or
Branch: 1 branch
BranchTrueFalseTotal
$assign->type == 37 or return $self->$orig_pp_leave(@_) 0 0 0
125 return $self->$orig_pp_leave( @_ );
126  
127 0 $topicop = $assign->first;
128  
129 0 my $padsvop = $topicop->sibling;
130 0 ▶ $padsvop->type == OP_PADSV or
Branch: 1 branch
BranchTrueFalseTotal
$padsvop->type == 9 or return $self->$orig_pp_leave(@_) 0 0 0
131 return $self->$orig_pp_leave( @_ );
132  
133 0 my $varname = $self->padname( $topicix = $padsvop->targ );
134 0 ▶ $varname eq '$(Syntax::Keyword::Match/topic)' or
Branch: 1 branch
BranchTrueFalseTotal
$varname eq "\$(Syntax::Keyword::Match/topic)" or return $self->$orig_pp_leave(@_) 0 0 0
135 return $self->$orig_pp_leave( @_ );
136 }
137  
138 7 my $cmpop;
139 my @caseblocks;
140 7 my $kid = $assign->sibling;
141 7 ▶ while( !B::Deparse::null($kid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (B::Deparse::null($kid)) 15 6 21
142 15 ▶ if( $kid->type == OP_NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->type == 0) 14 1 15
143 14 $kid = $kid->first;
144 }
145  
146 15 ▶ if( $kid->type == OP_LINESEQ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid->type == 201) 1 14 15
147 1 push @caseblocks, "default {" . B::Deparse::scopeop( 1, $self, $kid, 0 ) . "}";
148 1 last;
149 }
150  
151 14 my ( $condop, $block );
152 14 ▶ if( $kid->type == OP_COND_EXPR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kid->type == 187) { } 8 6 14
elsif ($kid->type == 183) { } 6 0 6
153 8 $condop = $kid->first;
154 8 $block = $condop->sibling;
155 8 $kid = $block->sibling;
156 }
157 elsif( $kid->type == OP_AND ) {
158 6 $condop = $kid->first;
159 6 $block = $condop->sibling;
160 6 $kid = $block->sibling; # should be NULL
161 }
162 else {
163 0 warn op_dump($kid);
164 0 die "TODO: not sure how to handle kid=", $kid->name;
165 }
166  
167 14 my @cases;
168 14 ▶ while( $condop and !B::Deparse::null($condop) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condop and not B::Deparse::null($condop)) 15 14 29
Condition66%2/3
$condop and not B::Deparse::null($condop)
!ll&&!rl&&r
14015
MC/DC50%1/2
$condop and not B::Deparse::null($condop)
$condop not B::Deparse::null($condop)
Truth table66%2/3
A and not(B)
A= $condopB= not B::Deparse::null($condop)
ABresult
0X0
100
111
169 15 ▶ if( $condop->type == OP_NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condop->type == 0) 1 14 15
170 1 $condop = $condop->first;
171 }
172  
173 15 my $cond1;
174 15 ▶ if( $condop->type == OP_OR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condop->type == 184) { } 1 14 15
175 1 $cond1 = $condop->first;
176 1 $condop = $cond1->sibling;
177 }
178 else {
179 14 $cond1 = $condop;
180 14 $condop = undef;
181 }
182  
183 15 my $condlhs = $cond1->first;
184 15 ▶ if( is_match_on_topic( $cond1, $topicix ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_match_on_topic($cond1, $topicix)) { } 2 13 15
elsif (not B::Deparse::null($condlhs) and $condlhs->type == 9 and $condlhs->targ == $topicix) { } 12 1 13
185 2 ▶ die "Unsure how to handle mismatched case cond ops"
Branch: 1 branch
BranchTrueFalseTotal
if $cmpop and $cmpop->type != $cond1->type 0 2 2
Condition66%2/3
$cmpop and $cmpop->type != $cond1->type
!ll&&!rl&&r
110
MC/DC0%0/2
$cmpop and $cmpop->type != $cond1->type
$cmpop $cmpop->type != $cond1->type
Truth table66%2/3
A and B
A= $cmpopB= $cmpop->type != $cond1->type
ABresult
0X0
100
111
186 if $cmpop and $cmpop->type != $cond1->type;
187 2 ▶ $cmpop //= $cond1;
Condition66%2/3
$cmpop //= $cond1
l!l&&r!l&&!r
110
MC/DC0%0/2
$cmpop //= $cond1
$cmpop $cond1
Truth table66%2/3
A //= B
A= $cmpopB= $cond1
ABresult
1X1
011
000
188 # Need to mangle out the target name
189 2 my $pattern = $self->deparse( $cond1, @_ );
190 2 $pattern =~ s{^\(\$\(Syntax::Keyword::Match/topic\) =~ (.*)\)$}{m$1};
191 2 push @cases, "case ($pattern)";
192 }
193 ▶ elsif( !B::Deparse::null($condlhs) and $condlhs->type == OP_PADSV and $condlhs->targ == $topicix ) {
Condition66%4/6
not B::Deparse::null($condlhs) and $condlhs->type == 9 and $condlhs->targ == $topicix
!ll&&!rl&&r
1012
not B::Deparse::null($condlhs) and $condlhs->type == 9
!ll&&!rl&&r
0112
MC/DC33%1/3
not B::Deparse::null($condlhs) and $condlhs->type == 9 and $condlhs->targ == $topicix
not B::Deparse::null($condlhs) $condlhs->type == 9 $condlhs->targ == $topicix
Truth table50%2/4
not(A) and B and C
A= not B::Deparse::null($condlhs)B= $condlhs->type == 9C= $condlhs->targ == $topicix
ABCresult
0XX0
10X0
1100
1111
194 # There's no way perl code could see the topic padname, so this
195 # must be a plain case(EXPR)
196 12 my $condval = $condlhs->sibling;
197  
198 # TODO: custom ops might be weird
199 12 ▶ die "Unsure how to handle mismatched case cond ops"
Branch: 1 branch
BranchTrueFalseTotal
if $cmpop and $cmpop->type != $cond1->type 0 12 12
Condition66%2/3
$cmpop and $cmpop->type != $cond1->type
!ll&&!rl&&r
660
MC/DC0%0/2
$cmpop and $cmpop->type != $cond1->type
$cmpop $cmpop->type != $cond1->type
Truth table66%2/3
A and B
A= $cmpopB= $cmpop->type != $cond1->type
ABresult
0X0
100
111
200 if $cmpop and $cmpop->type != $cond1->type;
201 12 ▶ $cmpop //= $cond1;
Condition66%2/3
$cmpop //= $cond1
l!l&&r!l&&!r
660
MC/DC0%0/2
$cmpop //= $cond1
$cmpop $cond1
Truth table66%2/3
A //= B
A= $cmpopB= $cond1
ABresult
1X1
011
000
202  
203 12 push @cases, "case (" . $self->deparse( $condval, @_ ) . ")";
204 }
205 else {
206 1 my $cond = $self->deparse( $cond1, @_ );
207 1 $cond =~ s/^\((.*)\)$/$1/; # trim surrounding () so we don't get two
208 1 push @cases, "case if ($cond)";
209 }
210 }
211  
212 14 push @caseblocks, join( ", ", @cases ) .
213 " {" . B::Deparse::scopeop( 1, $self, $block, 0 ) . "}";
214 }
215  
216 7 my $topic = $self->deparse( $topicop, @_ );
217  
218 # Ugh it'd be great if B::Deparse had a solution to this
219 7 my $cmp = operator_name( $cmpop );
220  
221 7 return "match ($topic : $cmp) {\n\t" . join( "\n", @caseblocks ) . "\n\b}";
222 }
223  
224 =head1 TODO
225  
226 =over 4
227  
228 =item *
229  
230 Integrate with custom ops (C<equ>, C<eqr>, etc..)
231  
232 =item *
233  
234 Handle the experimental dispatch op feature
235  
236 =back
237  
238 =cut
239  
240 =head1 AUTHOR
241  
242 Paul Evans <leonerd@leonerd.org.uk>
243  
244 =cut
245  
246 1 0x55AA;
↑ Summary blib/lib/Syntax/Keyword/Match.pm »