Plisco/Macro.pm

filter: Statementstmt 87.5% 224 / 256 Branchbran 62.2% 56 / 90 Conditioncond 60.0% 9 / 15 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 17.8% 13 / 73 Podpod 94.8% 55 / 58 totaltotal 72.1% 362 / 502 CC 58 SCAR 20.2
CC
58
Cov
80%
_expand
23.0
filter
20.0
_extract_arguments
16.5
CRAP 7.6
« Plisco/EPD.pm ↑ Summary Plisco/EPD/Record.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2021-2026 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software. It comes without any warranty, to
7 # the extent permitted by applicable law. You can redistribute it
8 # and/or modify it under the terms of the Do What the Fuck You Want
9 # to Public License, Version 2, as published by Sam Hocevar. See
10 # http://www.wtfpl.net/ for more details.
11  
12 package Chess::Plisco::Macro;
13 2 $Chess::Plisco::Macro::VERSION = 'v1.0.3';
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use PPI::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub _define;
20 sub _define_from_file;
21 sub preprocess;
22 sub _extract_arguments;
23 sub _split_arguments;
24 sub _expand;
25 sub _expand_placeholders;
26 sub _expand_placeholder;
27  
28 2 my %defines;
29  
30 # The no-op empty subroutines only exist so that Pod::Coverage works
31 # correctly.
32 2 _define cp_pos_white_pieces => '$p', '$p->[CP_POS_WHITE_PIECES]';
33 0 ▶sub cp_pos_white_pieces {}
Subroutine cp_pos_white_pieces: not called CC=1 SCAR=0.0
Pod documented
34 2 _define cp_pos_black_pieces => '$p', '$p->[CP_POS_BLACK_PIECES]';
35 0 ▶sub cp_pos_black_pieces {}
Subroutine cp_pos_black_pieces: not called CC=1 SCAR=0.0
Pod documented
36 2 _define cp_pos_pawns => '$p', '$p->[CP_POS_PAWNS]';
37 0 ▶sub cp_pos_pawns {}
Subroutine cp_pos_pawns: not called CC=1 SCAR=0.0
Pod documented
38 2 _define cp_pos_knights => '$p', '$p->[CP_POS_KNIGHTS]';
39 0 ▶sub cp_pos_knights {}
Subroutine cp_pos_knights: not called CC=1 SCAR=0.0
Pod documented
40 2 _define cp_pos_bishops => '$p', '$p->[CP_POS_BISHOPS]';
41 0 ▶sub cp_pos_bishops {}
Subroutine cp_pos_bishops: not called CC=1 SCAR=0.0
Pod documented
42 2 _define cp_pos_queens => '$p', '$p->[CP_POS_QUEENS]';
43 0 ▶sub cp_pos_queens {}
Subroutine cp_pos_queens: not called CC=1 SCAR=0.0
Pod documented
44 2 _define cp_pos_rooks => '$p', '$p->[CP_POS_ROOKS]';
45 0 ▶sub cp_pos_rooks {}
Subroutine cp_pos_rooks: not called CC=1 SCAR=0.0
Pod documented
46 2 _define cp_pos_kings => '$p', '$p->[CP_POS_KINGS]';
47 0 ▶sub cp_pos_kings {}
Subroutine cp_pos_kings: not called CC=1 SCAR=0.0
Pod documented
48 2 _define cp_pos_halfmoves => '$p', '$p->[CP_POS_HALFMOVES]';
49 0 ▶sub cp_pos_halfmoves {}
Subroutine cp_pos_halfmoves: not called CC=1 SCAR=0.0
Pod undocumented
50 2 _define cp_pos_signature => '$p', '$p->[CP_POS_SIGNATURE]';
51 0 ▶sub cp_pos_signature {}
Subroutine cp_pos_signature: not called CC=1 SCAR=0.0
Pod documented
52 2 _define cp_pos_last_move => '$p', '$p->[CP_POS_LAST_MOVE]';
53 0 ▶sub cp_pos_last_move {}
Subroutine cp_pos_last_move: not called CC=1 SCAR=0.0
Pod documented
54 2 _define cp_pos_castling_rights => '$p', '$p->[CP_POS_CASTLING_RIGHTS]';
55 0 ▶sub cp_pos_castling_rights {}
Subroutine cp_pos_castling_rights: not called CC=1 SCAR=0.0
Pod documented
56 2 _define cp_pos_white_king_side_castling_right => '$p',
57 '($p->[CP_POS_CASTLING_RIGHTS] & 1)';
58 0 ▶sub cp_pos_white_king_side_castling_right {}
Subroutine cp_pos_white_king_side_castling_right: not called CC=1 SCAR=0.0
Pod documented
59 2 _define cp_pos_white_queen_side_castling_right => '$p',
60 '(($p->[CP_POS_CASTLING_RIGHTS] & 2))';
61 0 ▶sub cp_pos_white_queen_side_castling_right {}
Subroutine cp_pos_white_queen_side_castling_right: not called CC=1 SCAR=0.0
Pod documented
62 2 _define cp_pos_black_king_side_castling_right => '$p',
63 '(($p->[CP_POS_CASTLING_RIGHTS] & 4))';
64 0 ▶sub cp_pos_black_king_side_castling_right {}
Subroutine cp_pos_black_king_side_castling_right: not called CC=1 SCAR=0.0
Pod documented
65 2 _define cp_pos_black_queen_side_castling_right => '$p',
66 '($p->[CP_POS_CASTLING_RIGHTS] & 8)';
67 0 ▶sub cp_pos_black_queen_side_castling_right {}
Subroutine cp_pos_black_queen_side_castling_right: not called CC=1 SCAR=0.0
Pod documented
68 2 _define cp_pos_turn => '$p', '$p->[CP_POS_TURN]';
69 0 ▶sub cp_pos_turn {}
Subroutine cp_pos_turn: not called CC=1 SCAR=0.0
Pod documented
70 2 _define cp_pos_to_move => '$p', '$p->[CP_POS_TURN]';
71 0 ▶sub cp_pos_to_move {}
Subroutine cp_pos_to_move: not called CC=1 SCAR=0.0
Pod documented
72 2 _define cp_pos_en_passant_shift => '$p', '$p->[CP_POS_EN_PASSANT_SHIFT]';
73 0 ▶sub cp_pos_en_passant_shift {}
Subroutine cp_pos_en_passant_shift: not called CC=1 SCAR=0.0
Pod documented
74 2 _define cp_pos_halfmove_clock => '$p', '$p->[CP_POS_HALFMOVE_CLOCK]';
75 0 ▶sub cp_pos_halfmove_clock {}
Subroutine cp_pos_halfmove_clock: not called CC=1 SCAR=0.0
Pod undocumented
76 2 _define cp_pos_material => '$p', '$p->[CP_POS_MATERIAL]';
77 0 ▶sub cp_pos_material {}
Subroutine cp_pos_material: not called CC=1 SCAR=0.0
Pod documented
78  
79 # Bits of a move:
80 #
81 # - moving piece (3 bits)
82 # - captured piece (3 bits)
83 # - promotion (3 bits)
84 # - from (6 bits)
85 # - to (6 bits)
86 # - colour (1 bit)
87 # - en passant flag (1 bit)
88 #
89 # When ordering moves we often want to use the combination of attacker, victim,
90 # and optionally promotion as an index into lookup tables. We want to do just
91 # a bitwise AND and not a shift followed by a bitwise and for it.
92  
93 2 _define cp_move_piece => '$m', '(($m) & 0x7)';
94 0 ▶sub cp_move_piece {}
Subroutine cp_move_piece: not called CC=1 SCAR=0.0
Pod documented
95 2 _define cp_move_set_piece => '$m', '$a',
96 '(($m) = (($m) & ~0x7) | ($a))';
97 0 ▶sub cp_move_set_piece {}
Subroutine cp_move_set_piece: not called CC=1 SCAR=0.0
Pod documented
98  
99 2 _define cp_move_captured => '$m', '((($m) >> 3) & 0x7)';
100 0 ▶sub cp_move_captured {}
Subroutine cp_move_captured: not called CC=1 SCAR=0.0
Pod documented
101 2 _define cp_move_set_captured => '$m', '$c',
102 '(($m) = (($m) & ~0x38) | (($c)) << 3)';
103 0 ▶sub cp_move_set_captured {}
Subroutine cp_move_set_captured: not called CC=1 SCAR=0.0
Pod documented
104  
105 2 _define cp_move_promote => '$m', '((($m) >> 6) & 0x7)';
106 0 ▶sub cp_move_promote {}
Subroutine cp_move_promote: not called CC=1 SCAR=0.0
Pod documented
107 2 _define cp_move_set_promote => '$m', '$p',
108 '(($m) = (($m) & ~0x1c0) | (($p)) << 6)';
109 0 ▶sub cp_move_set_promote {}
Subroutine cp_move_set_promote: not called CC=1 SCAR=0.0
Pod documented
110  
111 2 _define cp_move_from => '$m', '((($m) >> 9) & 0x3f)';
112 0 ▶sub cp_move_from {}
Subroutine cp_move_from: not called CC=1 SCAR=0.0
Pod documented
113 2 _define cp_move_set_from => '$m', '$f',
114 '(($m) = (($m) & ~0x7e00) | (($f)) << 9)';
115 0 ▶sub cp_move_set_from {}
Subroutine cp_move_set_from: not called CC=1 SCAR=0.0
Pod documented
116  
117 2 _define cp_move_to => '$m', '((($m) >> 15) & 0x3f)';
118 0 ▶sub cp_move_to {}
Subroutine cp_move_to: not called CC=1 SCAR=0.0
Pod documented
119 2 _define cp_move_set_to => '$m', '$t',
120 '(($m) = (($m) & ~0x1f8000) | (($t)) << 15)';
121 0 ▶sub cp_move_set_to {}
Subroutine cp_move_set_to: not called CC=1 SCAR=0.0
Pod documented
122  
123 2 _define cp_move_colour => '$m', '((($m) >> 21) & 0x1)';
124 0 ▶sub cp_move_colour {}
Subroutine cp_move_colour: not called CC=1 SCAR=0.0
Pod documented
125 2 _define cp_move_set_colour => '$m', '$c',
126 '(($m) = (($m) & ~0x200000) | (($c)) << 21)';
127 2 _define cp_move_color => '$m', '((($m) >> 21) & 0x1)';
128 0 ▶sub cp_move_color {}
Subroutine cp_move_color: not called CC=1 SCAR=0.0
Pod documented
129 2 _define cp_move_set_color => '$m', '$c',
130 '(($m) = (($m) & ~0x200000) | (($c)) << 21)';
131  
132 2 _define cp_move_en_passant => '$m', '((($m) >> 22) & 0x1)';
133 0 ▶sub cp_move_en_passant {}
Subroutine cp_move_en_passant: not called CC=1 SCAR=0.0
Pod documented
134 2 _define cp_move_set_en_passant => '$m', '$e',
135 '(($m) = (($m) & ~0x400000) | (($e) & 0x1) << 22)';
136  
137 2 _define cp_move_coordinate_notation => '$m', 'cp_shift_to_square(cp_move_from $m) . cp_shift_to_square(cp_move_to $m) . CP_PIECE_CHARS->[CP_BLACK]->[cp_move_promote $m]';
138 0 ▶sub cp_move_coordinate_notation {}
Subroutine cp_move_coordinate_notation: not called CC=1 SCAR=0.0
Pod documented
139  
140 2 _define cp_move_significant => '$m', '(($m) & 0x1fffc0)';
141 0 ▶sub cp_move_significant {}
Subroutine cp_move_significant: not called CC=1 SCAR=0.0
Pod documented
142  
143 2 _define cp_move_equivalent => '$m1', '$m2',
144 '(cp_move_significant($m1) == cp_move_significant($m2))';
145 0 ▶sub cp_move_equivalent {}
Subroutine cp_move_equivalent: not called CC=1 SCAR=0.0
Pod documented
146  
147 2 _define cp_move_compress => '$m', '((($m) & 0x1fffc0) >> 6)';
148 sub cp_move_compress {}
149  
150 2 _define cp_move_uncompress => '$m', '(($m) << 6)';
151 0 ▶sub cp_move_compress {}
Subroutine cp_move_compress: not called CC=1 SCAR=0.0
Pod documented
152  
153 # Bitboard macros.
154 2 _define cp_bitboard_popcount => '$b', '$c',
155 '{ my $_b = $b; for ($c = 0; $_b; ++$c) { $_b &= $_b - 1; } }';
156 0 ▶sub cp_bitboard_popcount {}
Subroutine cp_bitboard_popcount: not called CC=1 SCAR=0.0
Pod documented
157 2 _define cp_bitboard_clear_but_least_set => '$b', '(($b) & -($b))';
158 0 ▶sub cp_bitboard_clear_but_least_set {}
Subroutine cp_bitboard_clear_but_least_set: not called CC=1 SCAR=0.0
Pod documented
159 2 _define_from_file cp_bitboard_clear_but_most_set => '$bb', 'clearButMostSet.pm';
160 0 ▶sub cp_bitboard_clear_but_most_set {}
Subroutine cp_bitboard_clear_but_most_set: not called CC=1 SCAR=0.0
Pod documented
161 2 _define_from_file cp_bitboard_count_isolated_trailing_zbits => '$bb',
162 'countIsolatedTrailingZbits.pm';
163 0 ▶sub cp_bitboard_count_isolated_trailing_zbits {}
Subroutine cp_bitboard_count_isolated_trailing_zbits: not called CC=1 SCAR=0.0
Pod documented
164 2 _define_from_file cp_bitboard_count_trailing_zbits => '$bb', 'countTrailingZbits.pm';
165 0 ▶sub cp_bitboard_count_trailing_zbits {}
Subroutine cp_bitboard_count_trailing_zbits: not called CC=1 SCAR=0.0
Pod documented
166 2 _define cp_bitboard_clear_least_set => '$bb', '(($bb) & (($bb) - 1))';
167 0 ▶sub cp_bitboard_clear_least_set {}
Subroutine cp_bitboard_clear_least_set: not called CC=1 SCAR=0.0
Pod documented
168 2 _define cp_bitboard_more_than_one_set => '$bb', '($bb && ($bb & ($bb - 1)))';
169 0 ▶sub cp_bitboard_more_than_one_set {}
Subroutine cp_bitboard_more_than_one_set: not called CC=1 SCAR=0.0
Pod documented
170  
171 # Magic moves.
172 2 _define cp_mm_bmagic => '$s', '$o',
173 'CP_MAGICMOVESBDB->[$s][(((($o) & CP_MAGICMOVES_B_MASK->[$s]) * CP_MAGICMOVES_B_MAGICS->[$s]) >> 55) & ((1 << (64 - 55)) - 1)]';
174 0 ▶sub cp_mm_bmagic {}
Subroutine cp_mm_bmagic: not called CC=1 SCAR=0.0
Pod documented
175 2 _define cp_mm_rmagic => '$s', '$o',
176 'CP_MAGICMOVESRDB->[$s][(((($o) & CP_MAGICMOVES_R_MASK->[$s]) * CP_MAGICMOVES_R_MAGICS->[$s]) >> 52) & ((1 << (64 - 52)) - 1)]';
177 0 ▶sub cp_mm_rmagic {}
Subroutine cp_mm_rmagic: not called CC=1 SCAR=0.0
Pod documented
178  
179 # Conversion between different notions of a square.
180 2 _define cp_coordinates_to_shift => '$f', '$r', '(($r << 3) + $f)';
181 0 ▶sub cp_coordinates_to_shift {}
Subroutine cp_coordinates_to_shift: not called CC=1 SCAR=0.0
Pod documented
182 2 _define cp_shift_to_coordinates => '$s', '($s & 0x7, $s >> 3)';
183 0 ▶sub cp_shift_to_coordinates {}
Subroutine cp_shift_to_coordinates: not called CC=1 SCAR=0.0
Pod documented
184 2 _define cp_coordinates_to_square => '$f', '$r', 'chr(97 + $f) . (1 + $r)';
185 0 ▶sub cp_coordinates_to_square {}
Subroutine cp_coordinates_to_square: not called CC=1 SCAR=0.0
Pod documented
186 2 _define cp_square_to_coordinates => '$s', '(ord($s) - 97, -1 + substr $s, 1)';
187 0 ▶sub cp_square_to_coordinates {}
Subroutine cp_square_to_coordinates: not called CC=1 SCAR=0.0
Pod documented
188 2 _define cp_square_to_shift => '$s',
189 '(((substr $s, 1) - 1) << 3) + ord($s) - 97';
190 0 ▶sub cp_square_to_shift {}
Subroutine cp_square_to_shift: not called CC=1 SCAR=0.0
Pod documented
191 2 _define cp_shift_to_square => '$s', 'chr(97 + ($s & 0x7)) . (1 + ($s >> 3))';
192 0 ▶sub cp_shift_to_square {}
Subroutine cp_shift_to_square: not called CC=1 SCAR=0.0
Pod documented
193  
194 2 _define_from_file _cp_moves_from_mask => '$t', '@m', '$b', 'movesFromMask.pm';
195 2 _define_from_file _cp_promotion_moves_from_mask => '$t', '@m', '$b',
196 'promotionMovesFromMask.pm';
197 2 _define_from_file _cp_pos_move_pinned =>
198 '$p', '$from', '$to', '$ks', '$mp', '$hp', 'movePinned.pm';
199 2 _define_from_file _cp_pos_colour_attacked => '$p', '$c', '$shift', 'attacked.pm';
200 2 _define_from_file _cp_pos_move_attacked => '$p', '$from', '$to', 'moveAttacked.pm';
201 2 _define _cp_pawn_double_step => '$f', '$t', '(!(($t - $f) & 0x9))';
202  
203 2 _define _cp_is_decisive => '$v', '(($v >= VALUE_TB_WIN_IN_MAX_PLY) || ($v <= VALUE_TB_LOSS_IN_MAX_PLY))';
204  
205 # Bit twiddling.
206 2 _define_from_file cp_abs => '$v', 'abs.pm';
207 0 ▶sub cp_abs {}
Subroutine cp_abs: not called CC=1 SCAR=0.0
Pod documented
208  
209 # At least as fast as the versions w/o branching for example
210 # a - ((a -b) & ((a - b) >> 63)), and there are no overflow issues.
211 2 _define cp_max => '$A', '$B', '((($A) > ($B)) ? ($A) : ($B))';
212 0 ▶sub cp_max {}
Subroutine cp_max: not called CC=1 SCAR=0.0
Pod documented
213 2 _define cp_min => '$A', '$B', '((($A) < ($B)) ? ($A) : ($B))';
214 0 ▶sub cp_min {}
Subroutine cp_min: not called CC=1 SCAR=0.0
Pod documented
215  
216 2 _define cp_clamp => '$v', '$lo', '$hi', '($v) < ($lo) ? ($lo) : ($v) > ($hi) ? ($hi) : ($v)';
217  
218 # Zobrist keys.
219 2 _define _cp_zk_lookup => '$p', '$c', '$s', '$zk_pieces[((($p) << 7) | (($c) << 6) | ($s)) - 128]';
220  
221 ## TT probes and stores.
222 2 _define_from_file _cp_value_from_tt => '$v', '$p', '$r50c', 'valueFromTT.pm';
223 0 ▶sub _cp_value_from_tt {}
Subroutine _cp_value_from_tt: not called CC=1 SCAR=0.0
224 2 _define_from_file _cp_value_to_tt => '$v', '$p', 'valueToTT.pm';
225 0 ▶sub _cp_value_to_tt {}
Subroutine _cp_value_to_tt: not called CC=1 SCAR=0.0
226  
227 0 ▶sub import {
Subroutine import: not called CC=1 SCAR=6.9
228 0 my ($type) = @_;
229  
230 0 my $self = {
231 __source => '',
232 __eof => 0,
233 };
234  
235 0 filter_add(bless $self); ## no critic
236 }
237  
238 0 ▶sub filter {
Subroutine filter: not called CC=4 SCAR=30.0
Pod undocumented
239 0 my ($self) = @_;
240  
241 0 ▶ return 0 if $self->{__eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__eof"} 0 0 0
242  
243 0 my $status = filter_read();
244  
245 0 ▶ if ($status > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status > 0) { } 0 0 0
elsif ($status == 0) { } 0 0 0
246 0 $self->{__source} .= $_;
247 0 $_ = '';
248 } elsif ($status == 0) {
249 0 $_ = preprocess $self->{__source};
250 0 $self->{__eof} = 1;
251 0 return 1;
252 }
253  
254 0 return $status;
255 }
256  
257 5 ▶sub _expand {
Subroutine _expand: called CC=19 SCAR=31.4
258 5 my ($parent, $invocation) = @_;
259  
260 # First find the invocation.
261 5 my @siblings = $parent->children;
262 5 my $count = -1;
263 5 my $idx;
264 5 foreach my $sibling (@siblings) {
265 5 ++$count;
266 5 ▶ if ($sibling == $invocation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling == $invocation) 5 0 5
267 5 $idx = $count;
268 5 last;
269 }
270 }
271  
272 5 ▶ return if !defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 5 5
273  
274 # First remove all elements following the invocation, and later re-add
275 # them.
276 5 my $name = $invocation->content;
277  
278 5 my $definition = $defines{$name};
279 5 my $code = $definition->{code}->content;
280 5 $code =~ s/\n//g;
281 5 my $cdoc = PPI::Document->new(\$code);
282 5 my $cut = 0;
283 5 ▶ if (@{$definition->{args}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$definition->{'args'};} == 0) { } 0 5 5
284 # Just a constant, no arguments.
285 # Check whether there is a list following, and discard it.
286 0 my $to;
287 0 ▶ foreach ($to = $idx + 1; $to < @siblings; ++$to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < @siblings) 0 0 0
288 0 ▶ last if $siblings[$to]->significant;
Branch: 1 branch
BranchTrueFalseTotal
if $siblings[$to]->significant 0 0 0
289 }
290 0 ▶ if ($to < @siblings && $siblings[$to]->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < @siblings and $siblings[$to]->isa("PPI::Structure::List")) 0 0 0
Condition0%0/3
$to < @siblings and $siblings[$to]->isa("PPI::Structure::List")
!ll&&!rl&&r
000
MC/DC0%0/2
$to < @siblings and $siblings[$to]->isa("PPI::Structure::List")
$to < @siblings $siblings[$to]->isa("PPI::Structure::List")
Truth table0%0/3
A and B
A= $to < @siblingsB= $siblings[$to]->isa("PPI::Structure::List")
ABresult
0X0
100
111
291 0 $cut = $to - $idx;
292 }
293 } else {
294 5 my @arguments = _extract_arguments $invocation;
295 5 my @placeholders = @{$definition->{args}};
296 5 my %placeholders;
297 5 ▶ for (my $i = 0; $i < @placeholders; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @placeholders) 6 5 11
298 6 my $placeholder = $placeholders[$i];
299 6 ▶ if ($i > $#arguments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $#arguments) { } 0 6 6
300 0 $placeholders{$placeholder} = [];
301 } else {
302 6 $placeholders{$placeholder} = $arguments[$i];
303 }
304 }
305 5 _expand_placeholders $cdoc, %placeholders;
306  
307 5 my ($to, $first_significant);
308 5 ▶ foreach ($to = $idx + 1; $to < @siblings; ++$to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < @siblings) 8 2 10
309 8 ▶ if (!defined $first_significant && $siblings[$to]->significant) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $first_significant and $siblings[$to]->significant) 5 3 8
Condition100%3/3
not defined $first_significant and $siblings[$to]->significant
!ll&&!rl&&r
125
MC/DC100%2/2
not defined $first_significant and $siblings[$to]->significant
not defined $first_significant $siblings[$to]->significant
Truth table100%3/3
not(A) and B
A= not defined $first_significantB= $siblings[$to]->significant
ABresult
0X0
100
111
310 5 $first_significant = $siblings[$to];
311 5 ▶ if ($first_significant->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_significant->isa("PPI::Structure::List")) 3 2 5
312 3 --$to;
313 3 last;
314 }
315 }
316 }
317 5 ▶ $to = $idx if $to >= @siblings;
Branch: 1 branch
BranchTrueFalseTotal
if $to >= @siblings 2 3 5
318 5 $cut = $to - $idx + 1;
319 }
320  
321 5 $parent->remove_child($invocation);
322  
323 5 my @tail;
324 5 ▶ for (my $i = $idx + 1; $i < @siblings; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @siblings) 10 5 15
325 10 push @tail, $parent->remove_child($siblings[$i]);
326 }
327  
328 5 splice @tail, 0, $cut;
329  
330 5 my @children = $cdoc->children;
331 5 foreach my $child (@children) {
332 5 $cdoc->remove_child($child);
333 }
334  
335  
336 5 foreach my $sibling (@children, @tail) {
337 10 $parent->add_element($sibling);
338 }
339  
340 5 return $invocation;
341 }
342  
343 6 ▶sub _expand_placeholders {
Subroutine _expand_placeholders: called CC=2 SCAR=6.9
344 6 my ($doc, %placeholders) = @_;
345  
346 174 ▶ my $words = $doc->find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
347 ($_[1]->isa('PPI::Token::Symbol') || $_[1]->isa('PPI::Token::Word'))
348 174 ▶ && exists $placeholders{$_[1]->content}
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Symbol") || $_[1]->isa("PPI::Token::Word") and exists $placeholders{$_[1]->content} 10 164 174
Condition66%2/3
$_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::Word")
l!l&&r!l&&!r
100164
MC/DC50%1/2
$_[1]->isa("PPI::Token::Symbol") or $_[1]->isa("PPI::Token::Word")
$_[1]->isa("PPI::Token::Symbol") $_[1]->isa("PPI::Token::Word")
Truth table66%2/3
A or B
A= $_[1]->isa("PPI::Token::Symbol")B= $_[1]->isa("PPI::Token::Word")
ABresult
1X1
011
000
349 6 });
350  
351 6 foreach my $word (@$words) {
352 10 _expand_placeholder $word, @{$placeholders{$word->content}};
353 }
354 }
355  
356 10 ▶sub _expand_placeholder {
Subroutine _expand_placeholder: called CC=6 SCAR=18.6
357 10 my ($word, @arglist) = @_;
358  
359 # Find the word in the parent.
360 10 my $parent = $word->parent;
361 10 my $idx;
362  
363 10 my @siblings = $parent->children;
364 10 my $word_idx;
365 my @tail;
366 10 ▶ for (my $i = 0; $i < @siblings; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @siblings) 10 10 20
367 10 ▶ if (defined $word_idx) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $word_idx) { } 0 10 10
elsif ($siblings[$i] == $word) { } 10 0 10
368 0 my $sibling = $siblings[$i];
369 0 $parent->remove_child($sibling);
370 0 push @tail, $sibling;
371 } elsif ($siblings[$i] == $word) {
372 10 $word_idx = $i;
373 10 $parent->remove_child($word);
374 }
375 }
376  
377 10 foreach my $token (@arglist) {
378 # We have to clone the token, in case it had been used before.
379 10 $token = $token->clone;
380 }
381  
382 10 foreach my $token (@arglist, @tail) {
383 10 $parent->add_element($token);
384 }
385 }
386  
387 5 ▶sub preprocess {
Subroutine preprocess: called CC=3 SCAR=11.1
Pod documented
388 5 my ($content) = @_;
389  
390 5 my ($head, $code, $tail);
391  
392 5 ▶ if ($content =~ /(.*\n)# *__BEGIN_MACROS__.*?\n(.*\n)# *__END_MACROS__.*?\n(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /(.*\n)# *__BEGIN_MACROS__.*?\n(.*\n)# *__END_MACROS__.*?\n(.*)/s) { } 0 5 5
393 0 ($head, $code, $tail) = ($1, $2, $3);
394 0 $head .= "\n";
395 0 $tail = "\n$tail";
396 } else {
397 5 $head = '';
398 5 $code = $content;
399 5 $tail = '';
400 }
401  
402 5 my $source = PPI::Document->new(\$code);
403  
404 # We always replace the last macro invocation only, and then re-scan the
405 # document. This should ensure that nested macro invocations will work.
406 5 while (1) {
407 190 ▶ my $invocations = $source->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
408 190 ▶ $_[1]->isa('PPI::Token::Word') && exists $defines{$_[1]->content}
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Token::Word") and exists $defines{$_[1]->content} 9 181 190
409 10 });
410  
411 10 ▶ last if !$invocations;
Branch: 1 branch
BranchTrueFalseTotal
unless $invocations 5 5 10
412  
413 5 my $invocation = $invocations->[-1];
414 5 my $parent = $invocation->parent;
415  
416 5 _expand $parent, $invocation;
417 }
418  
419 5 return $head . $source->content . $tail;
420 }
421  
422 142 ▶sub _define {
Subroutine _define: called CC=4 SCAR=16.1
423 142 my ($name, @args) = @_;
424  
425 142 my $code = pop @args;
426 142 ▶ $code = '' if !defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 142 142
427  
428 142 ▶ if (exists $defines{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $defines{$name}) 0 142 142
429 0 require Carp;
430 0 Carp::croak("duplicate macro definition '$name'");
431 }
432  
433 142 my $code_doc = PPI::Document->new(\$code);
434 142 ▶ if (!$code_doc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code_doc) 0 142 142
435 0 require Carp;
436 0 my $msg = $@->message;
437 0 Carp::croak("cannot parse code for '$name': $msg\n");
438 }
439  
440 142 $code_doc->prune('PPI::Token::Comment');
441  
442 142 $defines{$name} = {
443 args => [@args],
444 code => $code_doc,
445 };
446  
447 142 return;
448 }
449  
450 22 ▶sub _define_from_file {
Subroutine _define_from_file: called CC=2 SCAR=7.0
451 22 my ($name, @args) = @_;
452  
453 22 my $relname = pop @args;
454 22 my $filename = __FILE__;
455 22 $filename =~ s{\.pm$}{/$relname};
456  
457 22 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "cannot open '${filename}' for reading: $!" 22 0 22
458 or die "cannot open '$filename' for reading: $!";
459
460 22 my $code = join '', <$fh>;
461  
462 22 return _define $name, @args, $code;
463 }
464  
465 5 ▶sub _extract_arguments {
Subroutine _extract_arguments: called CC=15 SCAR=28.0
466 5 my ($word) = @_;
467  
468 5 my $parent = $word->parent;
469 5 my @siblings = $parent->children;
470 5 my $pos;
471 5 ▶ for (my $i = 0; $i < @siblings; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @siblings) 5 0 5
472 5 ▶ if ($siblings[$i] == $word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($siblings[$i] == $word) 5 0 5
473 5 $pos = $i;
474 5 last;
475 }
476 }
477  
478 5 ▶ return if !defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 5 5
479  
480 # No arguments?
481 5 ▶ return if $pos == $#siblings;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == $#siblings 0 5 5
482  
483 # Skip insignicant tokens.
484 5 my $argidx;
485 5 ▶ for (my $i = $pos + 1; $i < @siblings; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @siblings) 7 0 7
486 7 ▶ if ($siblings[$i]->significant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($siblings[$i]->significant) 5 2 7
487 5 $argidx = $i;
488 5 last;
489 }
490 }
491  
492 5 ▶ return if !defined $argidx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argidx 0 5 5
493  
494 5 my @argnodes;
495 5 my $argnodes_parent = $parent;
496  
497 5 ▶ if ($siblings[$argidx]->isa('PPI::Token::Structure')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($siblings[$argidx]->isa('PPI::Token::Structure')) { } 0 5 5
elsif ($siblings[$argidx]->isa('PPI::Structure::List')) { } 3 2 5
498 # No arguments.
499 0 return;
500 } elsif ($siblings[$argidx]->isa('PPI::Structure::List')) {
501 # Call with parentheses. The only child should be an expression.
502 3 my @expression = $siblings[$argidx]->children;
503 3 ▶ return if @expression != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @expression != 1 0 3 3
504 3 $argnodes_parent = $expression[0];
505 3 ▶ return if !$argnodes_parent->isa('PPI::Statement::Expression');
Branch: 1 branch
BranchTrueFalseTotal
unless $argnodes_parent->isa("PPI::Statement::Expression") 0 3 3
506 3 @argnodes = $argnodes_parent->children;
507 } else {
508 2 ▶ for (my $i = $argidx; $i < @siblings; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @siblings) 3 1 4
509 # Call without parentheses.
510 3 ▶ if ($siblings[$i]->isa('PPI::Token::Structure')
Branch: 1 branch
BranchTrueFalseTotal
if ($siblings[$i]->isa("PPI::Token::Structure") and ";" eq $siblings[$i]->content) 1 2 3
Condition66%2/3
$siblings[$i]->isa("PPI::Token::Structure") and ";" eq $siblings[$i]->content
!ll&&!rl&&r
201
MC/DC50%1/2
$siblings[$i]->isa("PPI::Token::Structure") and ";" eq $siblings[$i]->content
$siblings[$i]->isa("PPI::Token::Structure") ";" eq $siblings[$i]->content
Truth table66%2/3
A and B
A= $siblings[$i]->isa("PPI::Token::Structure")B= ";" eq $siblings[$i]->content
ABresult
0X0
100
111
511 && ';' eq $siblings[$i]->content) {
512 1 last;
513 }
514  
515 2 push @argnodes, $siblings[$i];
516 }
517 }
518  
519 5 return _split_arguments $argnodes_parent, @argnodes;
520 }
521  
522 5 ▶sub _split_arguments {
Subroutine _split_arguments: called CC=8 SCAR=21.0
523 5 my ($parent, @argnodes) = @_;
524  
525 5 my @arguments;
526 my @argument;
527  
528 5 ▶ for (my $i = 0; $i < @argnodes; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @argnodes) 8 5 13
529 8 my $argnode = $argnodes[$i];
530  
531 8 $parent->remove_child($argnode);
532  
533 8 ▶ if ($argnode->isa('PPI::Token::Operator')
Branch: 1 branch
BranchTrueFalseTotal
if ($argnode->isa('PPI::Token::Operator') and ',' eq $argnode->content) { } 1 7 8
Condition66%2/3
$argnode->isa("PPI::Token::Operator") and "," eq $argnode->content
!ll&&!rl&&r
701
MC/DC50%1/2
$argnode->isa("PPI::Token::Operator") and "," eq $argnode->content
$argnode->isa("PPI::Token::Operator") "," eq $argnode->content
Truth table66%2/3
A and B
A= $argnode->isa("PPI::Token::Operator")B= "," eq $argnode->content
ABresult
0X0
100
111
534 && ',' eq $argnode->content) {
535 1 push @arguments, [@argument];
536 1 undef @argument;
537 } else {
538 7 push @argument, $argnode;
539 }
540 }
541 5 ▶ push @arguments, [@argument] if @argument;
Branch: 1 branch
BranchTrueFalseTotal
if @argument 5 0 5
542  
543 5 foreach my $argument (@arguments) {
544 6 ▶ while (!$argument->[0]->significant) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($argument->[0]->significant) 1 6 7
545 1 shift @$argument;
546 }
547 6 ▶ while (!$argument->[-1]->significant) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($argument->[-1]->significant) 0 6 6
548 0 pop @$argument;
549 }
550 }
551  
552 5 return @arguments;
553 }
554  
555 2 1;
« Plisco/EPD.pm ↑ Summary Plisco/EPD/Record.pm »