blib/lib/Text/Xslate/Compiler.pm

filter: Statementstmt 96.5% 735 / 761 Branchbran 85.8% 237 / 276 Conditioncond 81.7% 85 / 104 MC/DCmcdc 65.0% 39 / 60 Subroutinesub 100% 65 / 65 Podpod 0.0% 0 / 9 totaltotal 91.0% 1161 / 1275 CC 194 SCAR 22.5
CC
194
Cov
93%
_optimize_vmcode
27.3
_process_cascade
20.7
compile
14.0
CRAP 9.4
↑ Summary blib/lib/Text/Xslate/Runner.pm »
1 package Text::Xslate::Compiler;
2 351 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 351 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 351 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 351 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 351 ▶use Text::Xslate::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 351 ▶use Text::Xslate::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 $DEBUG
11 value_to_literal
12 is_int any_in
13 make_error
14 p
15 351 );
16  
17 #use constant _VERBOSE => scalar($DEBUG =~ /\b verbose \b/xms);
18 351 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 351 _DUMP_ASM => scalar($DEBUG =~ /\b dump=asm \b/xms),
20 _DUMP_AST => scalar($DEBUG =~ /\b dump=ast \b/xms),
21 _DUMP_GEN => scalar($DEBUG =~ /\b dump=gen \b/xms),
22 _DUMP_CAS => scalar($DEBUG =~ /\b dump=cascade \b/xms),
23  
24 _OP_NAME => 0,
25 _OP_ARG => 1,
26 _OP_LINE => 2,
27 _OP_FILE => 3,
28 _OP_LABEL => 4,
29 _OP_COMMENT => 5,
30  
31 _FOR_LOOP => 1,
32 _WHILE_LOOP => 2,
33 351 };
34  
35  
36 351 our $OPTIMIZE = scalar(($DEBUG =~ /\b optimize=(\d+) \b/xms)[0]);
37 351 ▶if(not defined $OPTIMIZE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Text::Xslate::Compiler::OPTIMIZE) 351 0 351
38 351 $OPTIMIZE = 1; # enable optimization by default
39 }
40  
41 351 our @CARP_NOT = qw(Text::Xslate Text::Xslate::Parser);
42  
43 {
44 351 package Text::Xslate;
45 351 our %OPS; # to avoid 'once' warnings;
46 }
47  
48 351 my %binary = (
49 '==' => 'eq',
50 '!=' => 'ne',
51 '<' => 'lt',
52 '<=' => 'le',
53 '>' => 'gt',
54 '>=' => 'ge',
55  
56 '~~' => 'match',
57  
58 '<=>' => 'ncmp',
59 'cmp' => 'scmp',
60  
61 '+' => 'add',
62 '-' => 'sub',
63 '*' => 'mul',
64 '/' => 'div',
65 '%' => 'mod',
66  
67 '~' => 'concat',
68 'x' => 'repeat',
69  
70 '+|' => 'bitor',
71 '+&' => 'bitand',
72 '+^' => 'bitxor',
73  
74 'min' => 'lt', # a < b ? a : b
75 'max' => 'gt', # a > b ? a : b
76  
77 '[' => 'fetch_field',
78 );
79 351 my %logical_binary = (
80 '&&' => 'and',
81 '||' => 'or',
82 '//' => 'dor',
83 );
84  
85 351 my %unary = (
86 '!' => 'not',
87 '+' => 'noop',
88 '-' => 'minus',
89 '+^' => 'bitneg',
90  
91 'max_index' => 'max_index', # for loop context vars
92 );
93  
94 351 my %goto_family = map { $_ => undef } qw(
95 for_iter
96 and
97 dand
98 or
99 dor
100 goto
101 );
102  
103 351 my %builtin = (
104 'html_escape' => ['builtin_html_escape',
105 \&Text::Xslate::Util::html_escape],
106 'uri_escape' => ['builtin_uri_escape',
107 \&Text::Xslate::Util::uri_escape],
108 'mark_raw' => ['builtin_mark_raw',
109 \&Text::Xslate::Util::mark_raw],
110 'unmark_raw' => ['builtin_unmark_raw',
111 \&Text::Xslate::Util::unmark_raw],
112  
113 'raw' => ['builtin_mark_raw',
114 \&Text::Xslate::Util::mark_raw],
115  
116 'html' => ['builtin_html_escape',
117 \&Text::Xslate::Util::html_escape],
118 'uri' => ['builtin_uri_escape',
119 \&Text::Xslate::Util::uri_escape],
120  
121 'is_array_ref' => ['builtin_is_array_ref',
122 \&Text::Xslate::Util::is_array_ref],
123 'is_hash_ref' => ['builtin_is_hash_ref',
124 \&Text::Xslate::Util::is_hash_ref],
125 );
126  
127 351 has lvar_id => ( # local variable id
128 is => 'rw',
129 isa => 'Int',
130  
131 init_arg => undef,
132 );
133  
134 351 has lvar => ( # local variable id table
135 is => 'rw',
136 isa => 'HashRef[Int]',
137  
138 init_arg => undef,
139 );
140  
141 351 has const => (
142 is => 'rw',
143 isa => 'ArrayRef',
144  
145 init_arg => undef,
146 );
147  
148 351 has macro_table => (
149 is => 'rw',
150 isa => 'HashRef',
151  
152 predicate => 'has_macro_table',
153 init_arg => undef,
154 );
155  
156 351 has engine => ( # Xslate engine
157 is => 'ro',
158 isa => 'Object',
159 required => 0,
160 weak_ref => 1,
161 );
162  
163 351 has dependencies => (
164 is => 'ro',
165 isa => 'ArrayRef',
166 init_arg => undef,
167 );
168  
169 351 has type => (
170 is => 'rw',
171 isa => enum([qw(html xml text)]),
172 default => 'html',
173 );
174  
175 351 has syntax => (
176 is => 'rw',
177  
178 default => 'Kolon',
179 );
180  
181 has parser_option => (
182 is => 'rw',
183 isa => 'HashRef',
184  
185 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
186 351 );
187  
188 351 has parser => (
189 is => 'rw',
190 isa => 'Object', # Text::Xslate::Parser
191  
192 handles => [qw(define_function)],
193  
194 lazy => 1,
195 builder => '_build_parser',
196 init_arg => undef,
197 );
198  
199 351 has input_layer => (
200 is => 'ro',
201 default => ':utf8',
202 );
203  
204 487 ▶sub _build_parser {
Subroutine _build_parser: called CC=2 SCAR=6.9
205 487 my($self) = @_;
206 487 my $syntax = $self->syntax;
207 487 ▶ if(ref($syntax)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $syntax) { } 2 485 487
208 2 return $syntax;
209 }
210 else {
211 485 my $parser_class = Mouse::Util::load_first_existing_class(
212 "Text::Xslate::Syntax::" . $syntax,
213 $syntax,
214 );
215 return $parser_class->new(
216 485 %{$self->parser_option},
217 engine => $self->engine,
218 compiler => $self,
219 );
220 }
221 }
222  
223 351 has cascade => (
224 is => 'rw',
225 init_arg => undef,
226 );
227  
228 351 has [qw(header footer macro)] => (
229 is => 'rw',
230 isa => 'ArrayRef',
231 );
232  
233 351 has current_file => (
234 is => 'rw',
235  
236 init_arg => undef,
237 );
238  
239 351 has file => (
240 is => 'rw',
241  
242 init_arg => undef,
243 );
244  
245 has overridden_builtin => (
246 is => 'ro',
247 isa => 'HashRef',
248  
249 487 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
250 351 );
251  
252 3029 ▶sub lvar_use {
Subroutine lvar_use: called CC=1 SCAR=0.0
Pod undocumented
253 3029 my($self, $n) = @_;
254  
255 3029 return $self->lvar_id + $n;
256 }
257  
258 97683 ▶sub filename {
Subroutine filename: called CC=2 SCAR=6.9
Pod undocumented
259 97683 my($self) = @_;
260 97683 my $file = $self->file;
261 97683 ▶ return ref($file) ? '<string>' : $file;
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 57624 40059 97683
262 }
263  
264 6996 ▶sub compile {
Subroutine compile: called CC=14 SCAR=26.4
Pod undocumented
265 6996 my($self, $input, %args) = @_;
266  
267 # each compiling process is independent
268 6996 local $self->{macro_table} = {};
269 6996 local $self->{lvar_id } = 0;
270 6996 local $self->{lvar} = {};
271 6996 local $self->{const} = [];
272 6996 local $self->{in_loop} = 0;
273 6996 local $self->{dependencies} = [];
274 6996 local $self->{cascade};
275 6996 local $self->{header} = $self->{header};
276 6996 local $self->{footer} = $self->{footer};
277 6996 local $self->{macro} = $self->{macro};
278 6996 local $self->{current_file} = '<string>'; # for opinfo
279 6996 ▶ local $self->{file} = $args{file} || \$input;
Condition100%2/2
$args{'file'} || \$input
l!l
40752921
MC/DC100%1/1
$args{'file'} || \$input
$args{'file'}
Truth table100%2/2
A
A= $args{'file'}
Aresult
11
00
280  
281 6996 ▶ if(my $engine = $self->engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $engine = $self->engine) 6986 10 6996
282 6986 my $ob = $self->overridden_builtin;
283 6986 Internals::SvREADONLY($ob, 0);
284 6986 foreach my $name(keys %builtin) {
285 62874 my $f = $engine->{function}{$name};
286 62874 $ob->{$name} = ( $builtin{$name}[1] != $f ) + 0;
287 }
288 6986 Internals::SvREADONLY($ob, 1);
289 }
290  
291 6996 my $parser = $self->parser;
292  
293 6996 my $header = delete $self->{header};
294 6996 my $footer = delete $self->{footer};
295 6996 my $macro = delete $self->{macro};
296  
297 6996 ▶ if(!$args{omit_augment}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"omit_augment"}) 4542 2454 6996
298 4542 ▶ if($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 18 4524 4542
299 18 substr $input, 0, 0, $self->_cat_files($header);
300 }
301 4542 ▶ if($footer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($footer) 18 4524 4542
302 18 $input .= $self->_cat_files($footer);
303 }
304 }
305 6996 ▶ if($macro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro) 4 6992 6996
306 4 ▶ if(!grep { $_ eq $self->current_file } @$macro) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $self->current_file;} @$macro) 4 0 4
307 4 substr $input, 0, 0, $self->_cat_files($macro);
308 }
309 }
310  
311 6996 my @code; # main code
312 {
313 6996 my $ast = $parser->parse($input, %args);
314 6898 print STDERR p($ast) if _DUMP_AST;
315 6898 @code = (
316 $self->opcode(set_opinfo => undef, file => $self->current_file, line => 1),
317 $self->compile_ast($ast),
318 $self->opcode('end'),
319 );
320 }
321  
322 6868 my $cascade = $self->cascade;
323 6868 ▶ if(defined $cascade) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cascade) 160 6708 6868
324 160 $self->_process_cascade($cascade, \%args, \@code);
325 }
326  
327 6858 ▶ push @code, $self->_flush_macro_table() if $self->has_macro_table;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_macro_table 6858 0 6858
328  
329 6858 ▶ if($OPTIMIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE) 6858 0 6858
330 6858 $self->_optimize_vmcode(\@code) for 1 .. 3;
331 }
332  
333 6858 print STDERR "// ", $self->filename, "\n",
334 $self->as_assembly(\@code, scalar($DEBUG =~ /\b ix \b/xms))
335 if _DUMP_ASM;
336  
337 {
338 6858 my %uniq;
339 push @code,
340 180 map { [ depend => $_ ] }
341 6858 ▶ grep { !ref($_) and !$uniq{$_}++ } @{$self->dependencies};
Condition66%2/3
ref $_ || $uniq{$_}++
l!l&&r!l&&!r
340180
MC/DC50%1/2
ref $_ || $uniq{$_}++
ref $_ $uniq{$_}++
Truth table66%2/3
A || B
A= ref $_B= $uniq{$_}++
ABresult
1X1
011
000
342 }
343  
344 6858 return \@code;
345 }
346  
347 105123 ▶sub opcode { # build an opcode
Subroutine opcode: called CC=6 SCAR=17.9
Pod undocumented
348 105123 my($self, $name, $arg, %args) = @_;
349 105123 my $symbol = $args{symbol};
350 105123 my $file = $args{file};
351 105123 my $label = $args{label};
352 105123 ▶ if(not defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 97031 8092 105123
353 97031 $file = $self->filename;
354 97031 ▶ if(defined $file and $file ne $self->current_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and $file ne $self->current_file) { } 3937 93094 97031
Condition66%2/3
defined $file and $file ne $self->current_file
!ll&&!rl&&r
0930943937
MC/DC50%1/2
defined $file and $file ne $self->current_file
defined $file $file ne $self->current_file
Truth table66%2/3
A and B
A= defined $fileB= $file ne $self->current_file
ABresult
0X0
100
111
355 3937 $self->current_file($file);
356 }
357 else {
358 93094 $file = undef;
359 }
360 }
361 # name, arg, label, line, file, comment
362 return [ $name => $arg,
363 $args{line} || (ref $symbol ? $symbol->line : undef),
364 $file,
365 $label,
366 $args{comment},
367 105123 ▶ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $symbol ? : 2766 59345 62111
Condition100%3/3
$args{'line'} || (ref $symbol ? $symbol->line : undef)
l!l&&r!l&&!r
43012263059481
MC/DC100%2/2
$args{'line'} || (ref $symbol ? $symbol->line : undef)
$args{'line'} (ref $symbol ? $symbol->line : undef)
Truth table100%3/3
A || B
A= $args{'line'}B= (ref $symbol ? $symbol->line : undef)
ABresult
1X1
011
000
368 }
369  
370 11927 ▶sub push_expr {
Subroutine push_expr: called CC=2 SCAR=6.9
Pod undocumented
371 11927 my($self, $node) = @_;
372  
373 11927 my $list_op = $node->arity eq 'range';
374 11927 my @code = ($self->compile_ast($node));
375 11927 ▶ if(not $list_op) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list_op) 11913 14 11927
376 11913 push @code, $self->opcode('push');
377 }
378 11927 return @code;
379 }
380  
381  
382 40 ▶sub _cat_files {
Subroutine _cat_files: called CC=3 SCAR=11.1
383 40 my($self, $files) = @_;
384 40 ▶ my $engine = $self->engine || $self->_error("No Xslate engine which header/footer requires");
Condition33%1/3
$self->engine || $self->_error('No Xslate engine which header/footer requires')
l!l&&r!l&&!r
4000
MC/DC0%0/2
$self->engine || $self->_error('No Xslate engine which header/footer requires')
$self->engine $self->_error('No Xslate engine which header/footer requires')
Truth table33%1/3
A || B
A= $self->engineB= $self->_error('No Xslate engine which header/footer requires')
ABresult
1X1
011
000
385 40 my $s = '';
386 40 foreach my $file(@{$files}) {
387 52 my $fullpath = $engine->find_file($file)->{fullpath};
388 52 $s .= $engine->slurp_template( $self->input_layer, $fullpath );
389 52 $self->requires($fullpath);
390 }
391 40 return $s;
392 }
393  
394 351 our $_lv = -1;
395  
396 36303 ▶sub compile_ast {
Subroutine compile_ast: called CC=6 SCAR=18.0
Pod undocumented
397 36303 my($self, $ast) = @_;
398 36303 ▶ return if not defined $ast;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ast 289 36014 36303
399  
400 36014 local $_lv = $_lv + 1 if _DUMP_GEN;
401  
402 36014 my @code;
403 36014 ▶ foreach my $node(ref($ast) eq 'ARRAY' ? @{$ast} : $ast) {
Branch: 1 branch
BranchTrueFalseTotal
ref $ast eq 'ARRAY' ? : 9335 26679 36014
404 57285 ▶ Scalar::Util::blessed($node) or Carp::confess("[BUG] Not a node object: " . p($node));
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($node) or Carp::confess('[BUG] Not a node object: ' . p($node)) 57285 0 57285
405  
406 57285 printf STDERR "%s"."generate %s (%s)\n", "." x $_lv, $node->arity, $node->id if _DUMP_GEN;
407  
408 57285 ▶ my $generator = $self->can('_generate_' . $node->arity)
Condition50%1/2
$self->can('_generate_' . $node->arity) || Carp::confess('[BUG] Unexpected node: ' . p($node))
l!l
572850
MC/DC0%0/1
$self->can('_generate_' . $node->arity) || Carp::confess('[BUG] Unexpected node: ' . p($node))
$self->can('_generate_' . $node->arity)
Truth table50%1/2
A
A= $self->can('_generate_' . $node->arity)
Aresult
11
00
409 || Carp::confess("[BUG] Unexpected node: " . p($node));
410  
411 57285 push @code, $self->$generator($node);
412 }
413  
414 35946 return @code;
415 }
416  
417 160 ▶sub _process_cascade {
Subroutine _process_cascade: called CC=19 SCAR=30.3
418 160 my($self, $cascade, $args, $main_code) = @_;
419 160 printf STDERR "# cascade %s %s", $self->file, $cascade->dump if _DUMP_CAS;
420 160 ▶ my $engine = $self->engine
Condition33%1/3
$self->engine || $self->_error('Cannot cascade templates without Xslate engine', $cascade)
l!l&&r!l&&!r
16000
MC/DC0%0/2
$self->engine || $self->_error('Cannot cascade templates without Xslate engine', $cascade)
$self->engine $self->_error('Cannot cascade templates without Xslate engine', $cascade)
Truth table33%1/3
A || B
A= $self->engineB= $self->_error('Cannot cascade templates without Xslate engine', $cascade)
ABresult
1X1
011
000
421 || $self->_error("Cannot cascade templates without Xslate engine", $cascade);
422  
423 160 my($base_file, $base_code);
424 160 my $base = $cascade->first;
425 my @components = $cascade->second
426 160 ▶ ? (map{ $self->_bare_to_file($_) } @{$cascade->second})
Branch: 1 branch
BranchTrueFalseTotal
$cascade->second ? : 22 138 160
427 : ();
428 160 my $vars = $cascade->third;
429  
430 160 ▶ if(defined $base) { # pure cascade
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 146 14 160
431 146 $base_file = $self->_bare_to_file($base);
432 144 $base_code = $engine->load_file($base_file);
433 140 $self->requires( $engine->find_file($base_file)->{fullpath} );
434 }
435 else { # overlay
436 14 $base_file = $args->{file}; # only for error messages
437 14 $base_code = $main_code;
438  
439 14 ▶ if(defined $args->{fullpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"fullpath"}) 0 14 14
440 0 $self->requires( $args->{fullpath} );
441 }
442  
443 14 push @{$main_code}, $self->_flush_macro_table();
444 }
445  
446 154 foreach my $cfile(@components) {
447 26 my $code = $engine->load_file($cfile);
448 26 my $fullpath = $engine->find_file($cfile)->{fullpath};
449  
450 26 my $mtable = $self->macro_table;
451 26 my $macro;
452 26 foreach my $c(@{$code}) {
453 # $c = [name, arg, line, file, symbol ]
454  
455 # retrieve macros from assembly code
456 316 ▶ if($c->[_OP_NAME] eq 'macro_begin' .. $c->[_OP_NAME] eq 'macro_end') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->[0] eq 'macro_begin' .. $c->[0] eq 'macro_end') { } 200 116 316
elsif ($c->[0] eq 'depend') { } 0 116 116
457 200 ▶ if($c->[_OP_NAME] eq 'macro_begin') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->[0] eq 'macro_begin') { } 46 154 200
elsif ($c->[0] eq 'macro_nargs') { } 0 154 154
elsif ($c->[0] eq 'macro_outer') { } 0 154 154
elsif ($c->[0] eq 'macro_end') { } 46 108 154
458 46 $macro = [];
459 46 $macro = {
460 name => $c->[_OP_ARG],
461 line => $c->[_OP_LINE],
462 file => $c->[_OP_FILE],
463 body => [],
464 };
465 46 ▶ push @{ $mtable->{$c->[_OP_ARG]} ||= [] }, $macro;
Condition50%1/2
$$mtable{$c->[1]} ||= []
l!l
046
MC/DC0%0/1
$$mtable{$c->[1]} ||= []
$$mtable{$c->[1]}
Truth table50%1/2
A
A= $$mtable{$c->[1]}
Aresult
11
00
466 }
467 elsif($c->[_OP_NAME] eq 'macro_nargs') {
468 0 $macro->{nargs} = $c->[_OP_ARG];
469 }
470 elsif($c->[_OP_NAME] eq 'macro_outer') {
471 0 $macro->{outer} = $c->[_OP_ARG];
472 }
473 elsif($c->[_OP_NAME] eq 'macro_end') {
474 # noop
475 }
476 else {
477 108 push @{$macro->{body}}, $c;
478 }
479 }
480 elsif($c->[_OP_NAME] eq 'depend') {
481 0 $self->requires($c->[_OP_ARG]);
482 }
483 }
484 26 $self->requires($fullpath);
485 26 $self->_process_cascade_file($cfile, $base_code);
486 }
487  
488 154 ▶ if(defined $base) { # pure cascade
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 140 14 154
489 140 $self->_process_cascade_file($base_file, $base_code);
490 136 ▶ if(defined $vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vars) 36 100 136
491 36 unshift @{$base_code}, $self->_localize_vars($vars);
492 }
493  
494 136 foreach my $c(@{$main_code}) {
495 372 ▶ if($c->[_OP_NAME] eq 'print_raw_s'
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[0] eq "print_raw_s" and $c->[1] =~ / [^ \t\r\n] /msx) 0 372 372
Condition66%2/3
$c->[0] eq "print_raw_s" and $c->[1] =~ / [^ \t\r\n] /msx
!ll&&!rl&&r
2721000
MC/DC0%0/2
$c->[0] eq "print_raw_s" and $c->[1] =~ / [^ \t\r\n] /msx
$c->[0] eq "print_raw_s" $c->[1] =~ / [^ \t\r\n] /msx
Truth table66%2/3
A and B
A= $c->[0] eq "print_raw_s"B= $c->[1] =~ / [^ \t\r\n] /msx
ABresult
0X0
100
111
496 && $c->[_OP_ARG] =~ m{ [^ \t\r\n] }xms) {
497 0 Carp::carp("Xslate: Useless use of text '$c->[1]'");
498 }
499 }
500 136 @{$main_code} = @{$base_code};
501 }
502 else { # overlay
503 14 return;
504 }
505 }
506  
507 166 ▶sub _process_cascade_file {
Subroutine _process_cascade_file: called CC=14 SCAR=26.4
508 166 my($self, $file, $base_code) = @_;
509 166 printf STDERR "# cascade file %s\n", p($file) if _DUMP_CAS;
510 166 my $mtable = $self->macro_table;
511  
512 166 ▶ for(my $i = 0; $i < @{$base_code}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$base_code;}) 1624 162 1786
513 1624 my $c = $base_code->[$i];
514 1624 ▶ if($c->[_OP_NAME] ne 'macro_begin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[0] ne "macro_begin") 1476 148 1624
515 1476 next;
516 }
517  
518 # macro
519 148 my $name = $c->[_OP_ARG];
520 148 $name =~ s/\@.+$//;
521 148 printf STDERR "# macro %s\n", $name if _DUMP_CAS;
522  
523 148 ▶ if(exists $mtable->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mtable->{$name}) 4 144 148
524 4 my $m = $mtable->{$name};
525 4 ▶ if(ref($m) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $m ne "HASH") 0 4 4
526 0 $self->_error('[BUG] Unexpected macro structure: '
527 . p($m) );
528 }
529  
530 $self->_error(
531 "Redefinition of macro/block $name in " . $file
532 . " (you must use block modifiers to override macros/blocks)",
533 $m->{line}
534 4 );
535 }
536  
537 144 my $before = delete $mtable->{$name . '@before'};
538 144 my $around = delete $mtable->{$name . '@around'};
539 144 my $after = delete $mtable->{$name . '@after'};
540  
541 144 ▶ if(defined $before) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $before) 52 92 144
542 52 my $n = scalar @{$base_code};
543 52 foreach my $m(@{$before}) {
544 52 splice @{$base_code}, $i+1, 0, @{$m->{body}};
545 }
546 52 $i += scalar(@{$base_code}) - $n;
547 }
548  
549 144 my $macro_start = $i+1;
550 144 ▶ $i++ while($base_code->[$i][_OP_NAME] ne 'macro_end'); # move to the end
Branch: 1 branch
BranchTrueFalseTotal
$base_code->[$i][0] ne "macro_end" and do { ++$i } 808 144 952
551  
552 144 ▶ if(defined $around) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $around) 42 102 144
553 42 my @original = splice @{$base_code}, $macro_start, ($i - $macro_start);
554 42 $i = $macro_start;
555  
556 42 my @body;
557 42 foreach my $m(@{$around}) {
558 42 push @body, @{$m->{body}};
559 }
560 42 ▶ for(my $j = 0; $j < @body; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @body) 284 42 326
561 284 ▶ if($body[$j][_OP_NAME] eq 'super') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body[$j][0] eq "super") 14 270 284
562 14 splice @body, $j, 1, @original;
563 }
564 }
565 42 splice @{$base_code}, $macro_start, 0, @body;
566  
567 42 $i += scalar(@body);
568 }
569  
570 144 ▶ if(defined $after) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $after) 50 94 144
571 50 foreach my $m(@{$after}) {
572 50 splice @{$base_code}, $i, 0, @{$m->{body}};
573 }
574 }
575 }
576 162 return;
577 }
578  
579  
580 6872 ▶sub _flush_macro_table {
Subroutine _flush_macro_table: called CC=6 SCAR=17.9
581 6872 my($self) = @_;
582 6872 my $mtable = $self->macro_table;
583 6872 my @code;
584 6872 foreach my $macros(values %{$mtable}) {
585 542 ▶ foreach my $macro(ref($macros) eq 'ARRAY' ? @{$macros} : $macros) {
Branch: 1 branch
BranchTrueFalseTotal
ref $macros eq 'ARRAY' ? : 58 484 542
586 push @code,
587 $self->opcode( macro_begin => $macro->{name},
588 file => $macro->{file},
589 542 line => $macro->{line} );
590  
591 push @code, $self->opcode( macro_nargs => $macro->{nargs} )
592 542 ▶ if $macro->{nargs};
Branch: 1 branch
BranchTrueFalseTotal
if $macro->{"nargs"} 182 360 542
593  
594 push @code, $self->opcode( macro_outer => $macro->{outer} )
595 542 ▶ if $macro->{outer};
Branch: 1 branch
BranchTrueFalseTotal
if $macro->{"outer"} 24 518 542
596  
597 542 push @code, @{ $macro->{body} }, $self->opcode('macro_end');
598 }
599 }
600 6872 %{$mtable} = ();
601 6872 return @code;
602 }
603  
604 920 ▶sub _generate_name {
Subroutine _generate_name: called CC=3 SCAR=11.0
605 920 my($self, $node) = @_;
606  
607 920 my $id = $node->value; # may be aliased
608 920 ▶ if(defined(my $lvar_id = $self->lvar->{$id})) { # constants
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $lvar_id = $self->lvar->{$id})) 142 778 920
609 142 my $code = $self->const->[$lvar_id];
610 142 ▶ if(defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) { } 46 96 142
611 # because the constant value is very simple,
612 # its definition is optimized away.
613 # only its value remains.
614 46 return @{$code};
615 }
616 else {
617 96 return $self->opcode( load_lvar => $lvar_id, symbol => $node );
618 }
619 }
620  
621 778 return $self->opcode( fetch_symbol => $id, line => $node->line );
622 }
623  
624 2 ▶sub _generate_operator {
Subroutine _generate_operator: called CC=1 SCAR=0.0
625 2 my($self, $node) = @_;
626 # This method is called when an operators is used as an expression,
627 # e.g. <: + :>, so simply throws the error
628 2 $self->_error("Invalid expression", $node);
629 }
630  
631 4879 ▶sub _can_optimize_print {
Subroutine _can_optimize_print: called CC=8 SCAR=20.9
632 4879 my($self, $name, $node) = @_;
633  
634 4879 ▶ return 0 if !$OPTIMIZE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Text::Xslate::Compiler::OPTIMIZE 0 4879 4879
635 4879 ▶ return 0 if !($name eq 'print' or $name eq 'print_raw');
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "print" or $name eq "print_raw" 0 4879 4879
Condition66%2/3
$name eq "print" or $name eq "print_raw"
l!l&&r!l&&!r
4839400
MC/DC0%0/2
$name eq "print" or $name eq "print_raw"
$name eq "print" $name eq "print_raw"
Truth table66%2/3
A or B
A= $name eq "print"B= $name eq "print_raw"
ABresult
1X1
011
000
636  
637 4879 my $maybe_name = $node->first;
638 return $node->arity eq 'call'
639 && $maybe_name->arity eq 'name'
640 628 && @{$node->second} == 1 # args of the filter
641 && any_in($maybe_name->id, qw(raw mark_raw html))
642 4879 ▶ && !$self->overridden_builtin->{$maybe_name->id};
Condition100%12/12
$node->arity eq 'call' && $maybe_name->arity eq 'name' && @{$node->second;} == 1 && any_in($maybe_name->id, ('raw', 'mark_raw', 'html')) && !$self->overridden_builtin->{$maybe_name->id}
!ll&&!rl&&r
4819654
$node->arity eq "call" and $maybe_name->arity eq "name" and @{$node->second;} == 1 and any_in($maybe_name->id, ("raw", "mark_raw", "html"))
!ll&&!rl&&r
463718260
$node->arity eq "call" and $maybe_name->arity eq "name" and @{$node->second;} == 1
!ll&&!rl&&r
4251386242
$node->arity eq "call" and $maybe_name->arity eq "name"
!ll&&!rl&&r
420348628
MC/DC100%5/5
$node->arity eq 'call' && $maybe_name->arity eq 'name' && @{$node->second;} == 1 && any_in($maybe_name->id, ('raw', 'mark_raw', 'html')) && !$self->overridden_builtin->{$maybe_name->id}
$node->arity eq "call" $maybe_name->arity eq "name" @{$node->second;} == 1 any_in($maybe_name->id, ("raw", "mark_raw", "html")) !$self->overridden_builtin->{$maybe_name->id}
Truth table100%6/6
A and B and C and D && not(E)
A= $node->arity eq "call"B= $maybe_name->arity eq "name"C= @{$node->second;} == 1D= any_in($maybe_name->id, ("raw", "mark_raw", "html"))E= !$self->overridden_builtin->{$maybe_name->id}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
643 }
644  
645 # also deal with smart escaping
646 25575 ▶sub _generate_print {
Subroutine _generate_print: called CC=11 SCAR=24.0
647 25575 my($self, $node) = @_;
648  
649 25575 my @code;
650  
651 25575 my $proc = $node->id;
652 25575 ▶ if($proc eq 'print' and $self->type eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc eq "print" and $self->type eq "text") 56 25519 25575
Condition100%3/3
$proc eq "print" and $self->type eq "text"
!ll&&!rl&&r
20724479556
MC/DC100%2/2
$proc eq "print" and $self->type eq "text"
$proc eq "print" $self->type eq "text"
Truth table100%3/3
A and B
A= $proc eq "print"B= $self->type eq "text"
ABresult
0X0
100
111
653 56 $proc = 'print_raw';
654 }
655  
656 25575 foreach my $arg(@{ $node->first }){
657 25637 ▶ if( $proc eq 'print' && $self->overridden_builtin->{html_escape} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($proc eq 'print' and $self->overridden_builtin->{'html_escape'}) { } 10 25627 25637
elsif (exists $Text::Xslate::OPS{$proc . '_s'} and $arg->arity eq 'literal') { } 20748 4879 25627
elsif ($self->_can_optimize_print($proc, $arg)) { } 54 4825 4879
Condition100%3/3
$proc eq "print" and $self->overridden_builtin->{"html_escape"}
!ll&&!rl&&r
20788483910
MC/DC100%2/2
$proc eq "print" and $self->overridden_builtin->{"html_escape"}
$proc eq "print" $self->overridden_builtin->{"html_escape"}
Truth table100%3/3
A and B
A= $proc eq "print"B= $self->overridden_builtin->{"html_escape"}
ABresult
0X0
100
111
658 # default behaviour of print() is overridden
659 10 push @code,
660 $self->opcode('pushmark'),
661 $self->compile_ast($arg),
662 $self->opcode('push'),
663 $self->opcode('fetch_symbol' => 'html_escape'),
664 $self->opcode('funcall'),
665 $self->opcode('print_raw');
666 }
667 ▶ elsif(exists $Text::Xslate::OPS{$proc . '_s'}
Condition100%3/3
exists $Text::Xslate::OPS{$proc . '_s'} and $arg->arity eq "literal"
!ll&&!rl&&r
48394020748
MC/DC100%2/2
exists $Text::Xslate::OPS{$proc . '_s'} and $arg->arity eq "literal"
exists $Text::Xslate::OPS{$proc . '_s'} $arg->arity eq "literal"
Truth table100%3/3
A and B
A= exists $Text::Xslate::OPS{$proc . '_s'}B= $arg->arity eq "literal"
ABresult
0X0
100
111
668 && $arg->arity eq 'literal'){
669 20748 push @code,
670 $self->opcode( $proc . '_s' => $arg->value,
671 line => $arg->line );
672 }
673 elsif($self->_can_optimize_print($proc, $arg)){
674 54 my $filter = $arg->first;
675 54 my $filter_name = $filter->id;
676 54 ▶ my $command = $builtin{ $filter_name }[0] eq 'builtin_mark_raw'
Branch: 1 branch
BranchTrueFalseTotal
$builtin{$filter_name}[0] eq 'builtin_mark_raw' ? : 30 24 54
677 ? 'print_raw' # mark_raw, raw
678 : 'print'; # html
679  
680 54 push @code,
681 $self->compile_ast($arg->second->[0]),
682 $self->opcode(
683 $command => undef,
684 symbol => $filter );
685  
686 }
687 else {
688 4825 push @code,
689 $self->compile_ast($arg),
690 $self->opcode( $proc => undef, line => $node->line );
691 }
692 }
693  
694 25557 ▶ if(!@code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@code) 0 25557 25557
695 0 $self->_error("$node requires at least one argument", $node);
696 }
697 25557 return @code;
698 }
699  
700 2506 ▶sub _generate_include {
Subroutine _generate_include: called CC=3 SCAR=11.0
701 2506 my($self, $node) = @_;
702  
703 2506 my $file = $node->first;
704 2506 ▶ my @code = (
Branch: 1 branch
BranchTrueFalseTotal
ref $file eq 'ARRAY' ? : 6 2500 2506
705 ( ref($file) eq 'ARRAY'
706 ? $self->opcode( literal => $self->_bare_to_file($file) )
707 : $self->compile_ast($file) ),
708 $self->opcode( $node->id => undef, line => $node->line ),
709 );
710  
711 2506 ▶ if(defined(my $vars = $node->second)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $vars = $node->second)) 34 2472 2506
712 34 @code = ($self->opcode('enter'),
713 $self->_localize_vars($vars),
714 @code,
715 $self->opcode('leave'),
716 );
717 }
718 2504 return @code;
719 }
720  
721 178 ▶sub _bare_to_file {
Subroutine _bare_to_file: called CC=3 SCAR=11.0
722 178 my($self, $file) = @_;
723 178 ▶ if(ref($file) eq 'ARRAY') { # myapp::foo
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'ARRAY') { } 138 40 178
elsif ($file->arity eq 'literal') { } 38 2 40
724 138 return join('/', map { $_->value } @{$file}) . $self->{engine}->{suffix};
725 }
726 elsif($file->arity eq 'literal') {
727 38 return $file->value;
728 }
729 else {
730 2 $self->_error("Expected a name or string literal", $file);
731 }
732 }
733  
734 160 ▶sub _generate_cascade {
Subroutine _generate_cascade: called CC=2 SCAR=7.4
735 160 my($self, $node) = @_;
736 160 ▶ if(defined $self->cascade) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->cascade) 0 160 160
737 0 $self->_error("Cannot cascade twice in a template", $node);
738 }
739 160 $self->cascade( $node );
740 160 return;
741 }
742  
743 # XXX: need more consideration
744 402 ▶sub _compile_loop_block {
Subroutine _compile_loop_block: called CC=6 SCAR=17.9
745 402 my($self, $block) = @_;
746 402 my @block_code = $self->compile_ast($block);
747  
748 402 foreach my $op(@block_code) {
749 3288 ▶ if(any_in( $op->[_OP_NAME], qw(pushmark loop_control))) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_in($op->[0], ("pushmark", "loop_control"))) 50 3238 3288
750 # pushmark ... funcall (or something) may create mortal SVs
751 # so surround the block with ENTER and LEAVE
752 50 unshift @block_code, $self->opcode('enter');
753 50 push @block_code, $self->opcode('leave');
754 50 last;
755 }
756 }
757  
758 402 foreach my $i(1 .. (@block_code-1)) {
759 3318 my $op = $block_code[$i];
760 3318 ▶ if($op->[_OP_NAME] eq 'loop_control') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->[0] eq "loop_control") 20 3298 3318
761 20 my $type = $op->[_OP_ARG];
762 20 $op->[_OP_NAME] = 'goto';
763  
764 20 $op->[_OP_ARG] = (@block_code - $i);
765  
766 20 ▶ $op->[_OP_ARG] += 1 if $type eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "last" 12 8 20
767 }
768 }
769  
770 402 return @block_code;
771 }
772  
773 352 ▶sub _generate_for {
Subroutine _generate_for: called CC=2 SCAR=6.9
774 352 my($self, $node) = @_;
775 352 my $expr = $node->first;
776 352 my $vars = $node->second;
777 352 my $block = $node->third;
778  
779 352 ▶ if(@{$vars} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$vars;} != 1) 0 352 352
780 0 $self->_error("A for-loop requires single variable for each item", $node);
781 }
782 352 local $self->{lvar} = { %{$self->lvar} }; # new scope
783 352 local $self->{const} = [ @{$self->const} ]; # new scope
784 352 local $self->{in_loop} = _FOR_LOOP;
785  
786 352 my @code = $self->compile_ast($expr);
787  
788 352 my($iter_var) = @{$vars};
789 352 my $lvar_id = $self->lvar_id;
790 352 my $lvar_name = $iter_var->id;
791  
792 352 $self->lvar->{$lvar_name} = $lvar_id;
793 352 $self->lvar->{'($_)'} = $lvar_id;
794  
795 352 push @code, $self->opcode( for_start => $lvar_id, symbol => $iter_var );
796  
797 # a for statement uses three local variables (container, iterator, and item)
798 352 local $self->{lvar_id} = $self->lvar_use(3);
799  
800 352 my @block_code = $self->_compile_loop_block($block);
801 352 push @code,
802 $self->opcode( literal_i => $lvar_id, symbol => $iter_var ),
803 $self->opcode( for_iter => scalar(@block_code) + 2 ),
804 @block_code,
805 $self->opcode( goto => -(scalar(@block_code) + 2), comment => "end for" );
806  
807 352 return @code;
808 }
809  
810 16 ▶sub _generate_for_else {
Subroutine _generate_for_else: called CC=1 SCAR=0.0
811 16 my($self, $node) = @_;
812  
813 16 my $for_block = $node->first;
814 16 my $else_block = $node->second;
815  
816 16 my @code = (
817 $self->compile_ast($for_block),
818 );
819  
820 # 'for' block sets __a with true if the loop count > 0
821 16 my @else = $self->compile_ast($else_block);
822 16 push @code, (
823 $self->opcode( or => scalar(@else) + 1, comment => 'for-else' ),
824 @else,
825 );
826  
827 16 return @code;
828 }
829  
830 50 ▶sub _generate_while {
Subroutine _generate_while: called CC=3 SCAR=11.0
831 50 my($self, $node) = @_;
832 50 my $expr = $node->first;
833 50 my $vars = $node->second;
834 50 my $block = $node->third;
835  
836 50 ▶ if(@{$vars} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$vars;} > 1) 0 50 50
837 0 $self->_error("A while-loop requires one or zero variable for each items", $node);
838 }
839  
840 50 (my $cond_op, undef, $expr) = $self->_prepare_cond_expr($expr);
841  
842 # TODO: combine all the loop contexts into single one
843 50 local $self->{lvar} = { %{$self->lvar} }; # new scope
844 50 local $self->{const} = [ @{$self->const} ]; # new scope
845 50 local $self->{in_loop} = _WHILE_LOOP;
846  
847 50 my @code = $self->compile_ast($expr);
848  
849 50 my($iter_var) = @{$vars};
850 50 my($lvar_id, $lvar_name);
851  
852 50 ▶ if(@{$vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$vars;}) 20 30 50
853 20 $lvar_id = $self->lvar_id;
854 20 $lvar_name = $iter_var->id;
855 20 $self->lvar->{$lvar_name} = $lvar_id;
856 20 push @code, $self->opcode( save_to_lvar => $lvar_id, symbol => $iter_var );
857 }
858  
859 50 local $self->{lvar_id} = $self->lvar_use(scalar @{$vars});
860 50 my @block_code = $self->_compile_loop_block($block);
861 50 return @code,
862 $self->opcode( $cond_op => scalar(@block_code) + 2, symbol => $node ),
863 @block_code,
864 $self->opcode( goto => -(scalar(@block_code) + scalar(@code) + 1), comment => "end while" );
865  
866 0 return @code;
867 }
868  
869 24 ▶sub _generate_loop_control {
Subroutine _generate_loop_control: called CC=6 SCAR=18.0
870 24 my($self, $node) = @_;
871 24 my $type = $node->id;
872  
873 24 ▶ any_in($type, qw(last next))
Branch: 1 branch
BranchTrueFalseTotal
any_in($type, ("last", "next")) or $self->_error("[BUG] Unknown loop control statement '${type}'") 24 0 24
874 or $self->_error("[BUG] Unknown loop control statement '$type'");
875  
876 24 ▶ if(not $self->{in_loop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_loop"}) 4 20 24
877 4 $self->_error("Use of loop control statement ($type) outside of loops");
878 }
879  
880 20 my @cleanup;
881 20 ▶ if( $self->{in_loop} == _FOR_LOOP && $type eq 'last' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_loop"} == 1 and $type eq "last") 4 16 20
Condition100%3/3
$self->{"in_loop"} == 1 and $type eq "last"
!ll&&!rl&&r
1244
MC/DC100%2/2
$self->{"in_loop"} == 1 and $type eq "last"
$self->{"in_loop"} == 1 $type eq "last"
Truth table100%3/3
A and B
A= $self->{"in_loop"} == 1B= $type eq "last"
ABresult
0X0
100
111
882 4 my $lvar_id = $self->lvar->{'($_)'};
883 4 ▶ defined($lvar_id)
Branch: 1 branch
BranchTrueFalseTotal
defined $lvar_id or $self->_error("[BUG] Undefined loop iterator") 4 0 4
884 or $self->_error('[BUG] Undefined loop iterator');
885  
886 4 @cleanup = (
887 $self->opcode( 'nil', undef,
888 comment => 'to clean the loop context' ),
889 $self->opcode( save_to_lvar => $lvar_id + 0), # item
890 $self->opcode( save_to_lvar => $lvar_id + 1), # iterator
891 $self->opcode( save_to_lvar => $lvar_id + 2), # body
892 $self->opcode( literal_i => 1 ), # for 'for-else'
893 );
894 }
895  
896 20 return $self->opcode('leave'),
897 @cleanup,
898 $self->opcode('loop_control' => $type, comment => $type);
899 }
900  
901 652 ▶sub _generate_proc { # definition of macro, block, before, around, after
Subroutine _generate_proc: called CC=6 SCAR=17.9
902 652 my($self, $node) = @_;
903 652 my $type = $node->id;
904 652 my $name = $node->first->id;
905 652 my @args = map{ $_->id } @{$node->second};
906 652 my $block = $node->third;
907  
908 652 local $self->{lvar} = { %{$self->lvar} }; # new scope
909 652 local $self->{const} = [ @{$self->const} ]; # new scope
910  
911 652 my $lvar_used = $self->lvar_id;
912 652 my $arg_ix = 0;
913 652 foreach my $arg(@args) {
914 # to fetch ST(ix)
915 # Note that arg_ix must be start from 1
916 238 $self->lvar->{$arg} = $lvar_used + $arg_ix++;
917 }
918  
919 652 local $self->{lvar_id} = $self->lvar_use($arg_ix);
920  
921 652 my $opinfo = $self->opcode(set_opinfo => undef, file => $self->filename, line => $node->line);
922 652 my %macro = (
923 name => $name,
924 nargs => $arg_ix,
925 body => [ $opinfo, $self->compile_ast($block) ],
926 line => $opinfo->[2],
927 file => $opinfo->[3],
928 outer => $lvar_used,
929 );
930  
931 652 ▶ if(any_in($type, qw(macro block))) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_in($type, ('macro', 'block'))) { } 496 156 652
932 496 ▶ if(exists $self->macro_table->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->macro_table->{$name}) 4 492 496
933 4 my $m = $self->macro_table->{$name};
934 4 ▶ if(p(\%macro) ne p($m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (p(\%macro) ne p($m)) 4 0 4
935 4 $self->_error("Redefinition of $type $name is forbidden", $node);
936 }
937 }
938 492 $self->macro_table->{$name} = \%macro;
939 }
940 else {
941 156 my $fq_name = sprintf '%s@%s', $name, $type;
942 156 $macro{name} = $fq_name;
943 156 ▶ push @{ $self->macro_table->{ $fq_name } ||= [] }, \%macro;
Condition50%1/2
$self->macro_table->{$fq_name} ||= []
l!l
0156
MC/DC0%0/1
$self->macro_table->{$fq_name} ||= []
$self->macro_table->{$fq_name}
Truth table50%1/2
A
A= $self->macro_table->{$fq_name}
Aresult
11
00
944 }
945 648 return;
946 }
947  
948 86 ▶sub _generate_lambda {
Subroutine _generate_lambda: called CC=1 SCAR=0.0
949 86 my($self, $node) = @_;
950  
951 86 my $macro = $node->first;
952 86 $self->compile_ast($macro);
953 86 return $self->opcode( fetch_symbol => $macro->first->id, line => $node->line );
954 }
955  
956 839 ▶sub _prepare_cond_expr {
Subroutine _prepare_cond_expr: called CC=6 SCAR=17.9
957 839 my($self, $expr) = @_;
958 839 my $t = "and";
959 839 my $f = "or";
960  
961 839 ▶ while($expr->id eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->id eq "!") 62 839 901
962 62 $expr = $expr->first;
963 62 ($t, $f) = ($f, $t);
964 }
965  
966 839 ▶ if($expr->is_logical and any_in($expr->id, qw(== !=))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->is_logical and any_in($expr->id, ("==", "!="))) 334 505 839
Condition100%3/3
$expr->is_logical and any_in($expr->id, ("==", "!="))
!ll&&!rl&&r
40699334
MC/DC100%2/2
$expr->is_logical and any_in($expr->id, ("==", "!="))
$expr->is_logical any_in($expr->id, ("==", "!="))
Truth table100%3/3
A and B
A= $expr->is_logicalB= any_in($expr->id, ("==", "!="))
ABresult
0X0
100
111
967 334 my $rhs = $expr->second;
968 334 ▶ if($rhs->arity eq "nil") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs->arity eq "nil") 78 256 334
969 # add prefix 'd' (i.e. "and" to "dand", "or" to "dor")
970 78 substr $t, 0, 0, 'd';
971 78 substr $f, 0, 0, 'd';
972  
973 78 ▶ if($expr->id eq "==") {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->id eq "==") 36 42 78
974 36 ($t, $f) = ($f, $t);
975 }
976 78 $expr = $expr->first;
977 }
978 }
979  
980 839 return($t, $f, $expr);
981 }
982  
983 789 ▶sub _generate_if {
Subroutine _generate_if: called CC=9 SCAR=22.0
984 789 my($self, $node) = @_;
985 789 my $first = $node->first;
986 789 my $second = $node->second;
987 789 my $third = $node->third;
988  
989 789 my($cond_true, $cond_false, $expr) = $self->_prepare_cond_expr($first);
990  
991 789 local $self->{lvar} = { %{$self->lvar} }; # new scope
992 789 local $self->{const} = [ @{$self->const} ]; # new scope
993 789 my @cond = $self->compile_ast($expr);
994  
995 789 my @then = do {
996 789 local $self->{lvar} = { %{$self->lvar} }; # new scope
997 789 local $self->{const} = [ @{$self->const} ]; # new scope
998 789 $self->compile_ast($second);
999 };
1000  
1001 789 my @else = do {
1002 789 local $self->{lvar} = { %{$self->lvar} }; # new scope
1003 789 local $self->{const} = [ @{$self->const} ]; # new scope
1004 789 $self->compile_ast($third);
1005 };
1006  
1007 789 ▶ if($OPTIMIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE) 789 0 789
1008 789 ▶ if($self->_code_is_literal(@cond)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_code_is_literal(@cond)) 202 587 789
1009 202 my $value = $cond[0][_OP_ARG];
1010 202 ▶ if($cond_true eq 'and' ? $value : !$value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cond_true eq 'and' ? $value : !$value) { } 150 52 202
$cond_true eq 'and' ? : 194 8 202
1011 150 return @then;
1012 }
1013 else {
1014 52 return @else;
1015 }
1016 }
1017 }
1018  
1019 587 ▶ if( (@then and @else) or !$OPTIMIZE) {
Branch: 2 branches
BranchTrueFalseTotal
if (@then and @else or not $Text::Xslate::Compiler::OPTIMIZE) { } 436 151 587
elsif (not @else) { } 143 8 151
Condition83%5/6
@then and @else or not $Text::Xslate::Compiler::OPTIMIZE
l!l&&r!l&&!r
4360151
@then and @else
!ll&&!rl&&r
13138436
MC/DC66%2/3
@then and @else or not $Text::Xslate::Compiler::OPTIMIZE
@then @else not $Text::Xslate::Compiler::OPTIMIZE
Truth table60%3/5
A and B or not(C)
A= @thenB= @elseC= not $Text::Xslate::Compiler::OPTIMIZE
ABCresult
11X1
0X11
1011
0X00
1000
1020 return(
1021 436 @cond,
1022 $self->opcode( $cond_true => scalar(@then) + 2, comment => $node->id . ' (then)' ),
1023 @then,
1024 $self->opcode( goto => scalar(@else) + 1, comment => $node->id . ' (else)' ),
1025 @else,
1026 );
1027 }
1028 elsif(!@else) { # no @else
1029 return(
1030 143 @cond,
1031 $self->opcode( $cond_true => scalar(@then) + 1, comment => $node->id . ' (then/no-else)' ),
1032 @then,
1033 );
1034 }
1035 else { # no @then
1036 return(
1037 8 @cond,
1038 $self->opcode( $cond_false => scalar(@else) + 1, comment => $node->id . ' (else/no-then)'),
1039 @else,
1040 );
1041 }
1042 }
1043  
1044 78 ▶sub _generate_given {
Subroutine _generate_given: called CC=2 SCAR=6.9
1045 78 my($self, $node) = @_;
1046 78 my $expr = $node->first;
1047 78 my $vars = $node->second;
1048 78 my $block = $node->third;
1049  
1050 78 ▶ if(@{$vars} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$vars;} > 1) 0 78 78
1051 0 $self->_error("A given block requires one or zero variables", $node);
1052 }
1053 78 local $self->{lvar} = { %{$self->lvar} }; # new scope
1054 78 local $self->{const} = [ @{$self->const} ]; # new scope
1055  
1056 78 my @code = $self->compile_ast($expr);
1057  
1058 78 my($lvar) = @{$vars};
1059 78 my $lvar_id = $self->lvar_id;
1060 78 my $lvar_name = $lvar->id;
1061  
1062 78 $self->lvar->{$lvar_name} = $lvar_id;
1063  
1064 78 local $self->{lvar_id} = $self->lvar_use(1); # topic variable
1065 78 push @code, $self->opcode( save_to_lvar => $lvar_id, symbol => $lvar ),
1066 $self->compile_ast($block);
1067  
1068 78 return @code;
1069 }
1070  
1071 6436 ▶sub _generate_variable {
Subroutine _generate_variable: called CC=3 SCAR=11.0
1072 6436 my($self, $node) = @_;
1073  
1074 6436 ▶ if(defined(my $lvar_id = $self->lvar->{$node->value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $lvar_id = $self->lvar->{$node->value})) { } 860 5576 6436
1075 860 return $self->opcode( load_lvar => $lvar_id, symbol => $node );
1076 }
1077 else {
1078 5576 my $name = $self->_variable_to_value($node);
1079 5576 ▶ if($name =~ /~/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /~/) 16 5560 5576
1080 16 $self->_error("Undefined iterator variable $node", $node);
1081 }
1082 5560 return $self->opcode( fetch_s => $name, line => $node->line );
1083 }
1084 }
1085  
1086 14 ▶sub _generate_super {
Subroutine _generate_super: called CC=1 SCAR=0.0
1087 14 my($self, $node) = @_;
1088  
1089 14 return return $self->opcode( super => undef, symbol => $node );
1090 }
1091  
1092 14709 ▶sub _generate_literal {
Subroutine _generate_literal: called CC=1 SCAR=0.0
1093 14709 my($self, $node) = @_;
1094 14709 return $self->opcode( literal => $node->value );
1095 }
1096  
1097 138 ▶sub _generate_nil {
Subroutine _generate_nil: called CC=1 SCAR=0.0
1098 138 my($self) = @_;
1099 138 return $self->opcode('nil');
1100 }
1101  
1102 12 ▶sub _generate_vars {
Subroutine _generate_vars: called CC=1 SCAR=0.0
1103 12 my($self) = @_;
1104 12 return $self->opcode('vars');
1105 }
1106  
1107 288 ▶sub _generate_composer {
Subroutine _generate_composer: called CC=2 SCAR=6.9
1108 288 my($self, $node) = @_;
1109  
1110 288 my $list = $node->first;
1111 288 ▶ my $type = $node->id eq '{' ? 'make_hash' : 'make_array';
Branch: 1 branch
BranchTrueFalseTotal
$node->id eq '{' ? : 98 190 288
1112  
1113 return
1114 $self->opcode( pushmark => undef, comment => $type ),
1115 288 (map{ $self->push_expr($_) } @{$list}),
1116 $self->opcode($type),
1117 ;
1118 }
1119  
1120 66 ▶sub _generate_unary {
Subroutine _generate_unary: called CC=4 SCAR=14.1
1121 66 my($self, $node) = @_;
1122  
1123 66 my $id = $node->id;
1124 66 ▶ if(exists $unary{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $unary{$id}) { } 66 0 66
1125 66 my @operand = $self->compile_ast($node->first);
1126 my @code = (
1127 @operand,
1128 66 $self->opcode( $unary{$id} )
1129 );
1130 66 ▶ if( $OPTIMIZE and $self->_code_is_literal(@operand) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@operand)) 34 32 66
Condition66%2/3
$Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@operand)
!ll&&!rl&&r
03234
MC/DC50%1/2
$Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@operand)
$Text::Xslate::Compiler::OPTIMIZE $self->_code_is_literal(@operand)
Truth table66%2/3
A and B
A= $Text::Xslate::Compiler::OPTIMIZEB= $self->_code_is_literal(@operand)
ABresult
0X0
100
111
1131 34 $self->_fold_constants(\@code);
1132 }
1133 66 return @code;
1134 }
1135 else {
1136 0 $self->_error("Unary operator $id is not implemented", $node);
1137 }
1138 }
1139  
1140 646 ▶sub _generate_field {
Subroutine _generate_field: called CC=4 SCAR=13.9
1141 646 my($self, $node) = @_;
1142  
1143 646 my @lhs = $self->compile_ast($node->first);
1144 630 my $field = $node->second;
1145  
1146 # $foo.field
1147 # $foo["field"]
1148 630 ▶ if($field->arity eq "literal") {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->arity eq 'literal') { } 522 108 630
1149 return
1150 522 @lhs,
1151 $self->opcode( fetch_field_s => $field->value );
1152 }
1153 # $foo[expression]
1154 else {
1155 108 local $self->{lvar_id} = $self->lvar_use(1);
1156 108 my @rhs = $self->compile_ast($field);
1157 108 ▶ if($OPTIMIZE and $self->_code_is_literal(@rhs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@rhs)) 28 80 108
Condition66%2/3
$Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@rhs)
!ll&&!rl&&r
08028
MC/DC50%1/2
$Text::Xslate::Compiler::OPTIMIZE and $self->_code_is_literal(@rhs)
$Text::Xslate::Compiler::OPTIMIZE $self->_code_is_literal(@rhs)
Truth table66%2/3
A and B
A= $Text::Xslate::Compiler::OPTIMIZEB= $self->_code_is_literal(@rhs)
ABresult
0X0
100
111
1158 return
1159 28 @lhs,
1160 $self->opcode( fetch_field_s => $rhs[0][1] );
1161 }
1162 return
1163 80 @lhs,
1164 $self->opcode( save_to_lvar => $self->lvar_id ),
1165 @rhs,
1166 $self->opcode( load_lvar_to_sb => $self->lvar_id ),
1167 $self->opcode( 'fetch_field' ),
1168 ;
1169 }
1170  
1171 }
1172  
1173 1917 ▶sub _generate_binary {
Subroutine _generate_binary: called CC=7 SCAR=19.5
1174 1917 my($self, $node) = @_;
1175  
1176 1917 my @lhs = $self->compile_ast($node->first);
1177  
1178 1913 my $id = $node->id;
1179 1913 ▶ if(exists $binary{$id}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $binary{$id}) { } 1463 450 1913
elsif (exists $logical_binary{$id}) { } 450 0 450
1180 1463 local $self->{lvar_id} = $self->lvar_use(1);
1181 1463 my @rhs = $self->compile_ast($node->second);
1182 my @code = (
1183 @lhs,
1184 $self->opcode( save_to_lvar => $self->lvar_id ),
1185 @rhs,
1186 $self->opcode( load_lvar_to_sb => $self->lvar_id ),
1187 1463 $self->opcode( $binary{$id} ),
1188 );
1189  
1190 1463 ▶ if(any_in($id, qw(min max))) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_in($id, ("min", "max"))) 60 1403 1463
1191 60 local $self->{lvar_id} = $self->lvar_use(1);
1192 60 splice @code, -1, 0,
1193 $self->opcode(save_to_lvar => $self->lvar_id ); # save lhs
1194 60 push @code,
1195 $self->opcode( or => +2 , symbol => $node ),
1196 $self->opcode( load_lvar_to_sb => $self->lvar_id ), # on true
1197 # fall through
1198 $self->opcode( 'move_from_sb' ), # on false
1199 }
1200  
1201 1463 ▶ if($OPTIMIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE) 1463 0 1463
1202 1463 ▶ if( $self->_code_is_literal(@lhs) and $self->_code_is_literal(@rhs) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_code_is_literal(@lhs) and $self->_code_is_literal(@rhs)) 246 1217 1463
Condition100%3/3
$self->_code_is_literal(@lhs) and $self->_code_is_literal(@rhs)
!ll&&!rl&&r
114374246
MC/DC100%2/2
$self->_code_is_literal(@lhs) and $self->_code_is_literal(@rhs)
$self->_code_is_literal(@lhs) $self->_code_is_literal(@rhs)
Truth table100%3/3
A and B
A= $self->_code_is_literal(@lhs)B= $self->_code_is_literal(@rhs)
ABresult
0X0
100
111
1203 246 $self->_fold_constants(\@code);
1204 }
1205 }
1206 1463 return @code;
1207 }
1208 elsif(exists $logical_binary{$id}) {
1209 450 my @rhs = $self->compile_ast($node->second);
1210 return
1211 @lhs,
1212 450 $self->opcode( $logical_binary{$id} => scalar(@rhs) + 1, symbol => $node ),
1213 @rhs;
1214 }
1215  
1216 0 $self->_error("Binary operator $id is not implemented", $node);
1217 }
1218  
1219 14 ▶sub _generate_range {
Subroutine _generate_range: called CC=2 SCAR=7.0
1220 14 my($self, $node) = @_;
1221  
1222 14 ▶ $self->can_be_in_list_context
Branch: 1 branch
BranchTrueFalseTotal
$self->can_be_in_list_context or $self->_error("Range operator must be in list context") 14 0 14
1223 or $self->_error("Range operator must be in list context");
1224  
1225 14 my @lhs = $self->compile_ast($node->first);
1226  
1227 14 local $self->{lvar_id} = $self->lvar_use(1);
1228 14 my @rhs = $self->compile_ast($node->second);
1229 return(
1230 14 @lhs,
1231 $self->opcode( save_to_lvar => $self->lvar_id ),
1232 @rhs,
1233 $self->opcode( load_lvar_to_sb => $self->lvar_id ),
1234 $self->opcode( 'range' ),
1235 );
1236 }
1237  
1238 493 ▶sub _generate_methodcall {
Subroutine _generate_methodcall: called CC=1 SCAR=0.0
1239 493 my($self, $node) = @_;
1240  
1241 493 my $args = $node->third;
1242 493 my $method = $node->second->value;
1243 return (
1244 $self->opcode( pushmark => undef, comment => $method ),
1245 $self->push_expr($node->first),
1246 493 (map { $self->push_expr($_) } @{$args}),
1247 $self->opcode( methodcall_s => $method, line => $node->line ),
1248 );
1249 }
1250  
1251 936 ▶sub _generate_call {
Subroutine _generate_call: called CC=4 SCAR=13.9
1252 936 my($self, $node) = @_;
1253 936 my $callable = $node->first; # function or macro
1254 936 my $args = $node->second;
1255  
1256 936 ▶ if(my $intern = $builtin{$callable->id} and !$self->overridden_builtin->{$callable->id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $intern = $builtin{$callable->id} and not $self->overridden_builtin->{$callable->id}) 108 828 936
Condition100%3/3
my $intern = $builtin{$callable->id} and not $self->overridden_builtin->{$callable->id}
!ll&&!rl&&r
8208108
MC/DC100%2/2
my $intern = $builtin{$callable->id} and not $self->overridden_builtin->{$callable->id}
my $intern = $builtin{$callable->id} not $self->overridden_builtin->{$callable->id}
Truth table100%3/3
A and not(B)
A= my $intern = $builtin{$callable->id}B= not $self->overridden_builtin->{$callable->id}
ABresult
0X0
100
111
1257 108 ▶ if(@{$args} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 1) 0 108 108
1258 0 $self->_error("Wrong number of arguments for $callable", $node);
1259 }
1260  
1261 108 return $self->compile_ast($args->[0]),
1262 [ $intern->[0] => undef, $node->line ];
1263 }
1264  
1265 return(
1266 $self->opcode( pushmark => undef, comment => $callable->id ),
1267 828 (map { $self->push_expr($_) } @{$args}),
1268 $self->compile_ast($callable),
1269 $self->opcode( 'funcall' )
1270 );
1271 }
1272  
1273 # $~iterator
1274 86 ▶sub _generate_iterator {
Subroutine _generate_iterator: called CC=2 SCAR=7.2
1275 86 my($self, $node) = @_;
1276  
1277 86 my $item_var = $node->first;
1278 86 my $lvar_id = $self->lvar->{$item_var};
1279 86 ▶ if(!defined($lvar_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lvar_id) 0 86 86
1280 0 $self->_error("Refer to iterator $node, but $item_var is not defined",
1281 $node);
1282 }
1283  
1284 86 return $self->opcode(
1285 load_lvar => $lvar_id + 1,
1286 symbol => $node,
1287 );
1288 }
1289  
1290 # $~iterator.body
1291 32 ▶sub _generate_iterator_body {
Subroutine _generate_iterator_body: called CC=2 SCAR=7.2
1292 32 my($self, $node) = @_;
1293  
1294 32 my $item_var = $node->first;
1295 32 my $lvar_id = $self->lvar->{$item_var};
1296 32 ▶ if(!defined($lvar_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lvar_id) 0 32 32
1297 0 $self->_error("Refer to iterator $node.body, but $item_var is not defined",
1298 $node);
1299 }
1300  
1301 32 return $self->opcode(
1302 load_lvar => $lvar_id + 2,
1303 symbol => $node,
1304 );
1305 }
1306  
1307 118 ▶sub _generate_assign {
Subroutine _generate_assign: called CC=5 SCAR=16.2
1308 118 my($self, $node) = @_;
1309 118 my $lhs = $node->first;
1310 118 my $rhs = $node->second;
1311 118 my $is_decl = $node->third;
1312  
1313 118 my $lvar = $self->lvar;
1314 118 my $lvar_name = $lhs->id;
1315  
1316 118 ▶ if($node->id ne "=") {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->id ne "=") 0 118 118
1317 0 $self->_error("Assignment ($node) is not supported", $node);
1318 }
1319  
1320 118 my @expr = $self->compile_ast($rhs);
1321  
1322 118 ▶ if($is_decl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_decl) 94 24 118
1323 94 $lvar->{$lvar_name} = $self->lvar_id;
1324 94 $self->{lvar_id} = $self->lvar_use(1); # don't use local()
1325 }
1326  
1327 118 ▶ if(!exists $lvar->{$lvar_name} or $lhs->arity ne "variable") {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $lvar->{$lvar_name} or $lhs->arity ne "variable") 2 116 118
Condition66%2/3
not exists $lvar->{$lvar_name} or $lhs->arity ne "variable"
l!l&&r!l&&!r
02116
MC/DC50%1/2
not exists $lvar->{$lvar_name} or $lhs->arity ne "variable"
not exists $lvar->{$lvar_name} $lhs->arity ne "variable"
Truth table66%2/3
not(A) or B
A= not exists $lvar->{$lvar_name}B= $lhs->arity ne "variable"
ABresult
1X1
011
000
1328 2 $self->_error("Cannot modify $lhs, which is not a lexical variable", $node);
1329 }
1330  
1331 return
1332 @expr,
1333 116 $self->opcode( save_to_lvar => $lvar->{$lvar_name}, symbol => $lhs, comment => $node->id);
1334 }
1335  
1336 146 ▶sub _generate_constant {
Subroutine _generate_constant: called CC=4 SCAR=13.9
1337 146 my($self, $node) = @_;
1338 146 my $lhs = $node->first;
1339 146 my $rhs = $node->second;
1340  
1341 146 my @expr = $self->compile_ast($rhs);
1342  
1343 146 my $lvar = $self->lvar;
1344 146 my $lvar_id = $self->lvar_id;
1345 146 my $lvar_name = $lhs->id;
1346 146 $lvar->{$lvar_name} = $lvar_id;
1347 146 $self->{lvar_id} = $self->lvar_use(1); # don't use local()
1348  
1349 146 ▶ if($OPTIMIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Compiler::OPTIMIZE) 146 0 146
1350 146 ▶ if(@expr == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@expr == 1 and any_in($expr[0][0], ("literal", "load_lvar"))) 66 80 146
Condition100%3/3
@expr == 1 and any_in($expr[0][0], ("literal", "load_lvar"))
!ll&&!rl&&r
621866
MC/DC100%2/2
@expr == 1 and any_in($expr[0][0], ("literal", "load_lvar"))
@expr == 1 any_in($expr[0][0], ("literal", "load_lvar"))
Truth table100%3/3
A and B
A= @expr == 1B= any_in($expr[0][0], ("literal", "load_lvar"))
ABresult
0X0
100
111
1351 && any_in($expr[0][_OP_NAME], qw(literal load_lvar))) {
1352 66 $expr[0][_OP_COMMENT] = "constant $lvar_name";
1353 66 $self->const->[$lvar_id] = \@expr;
1354 66 return @expr; # no real definition
1355 }
1356 }
1357  
1358 return
1359 80 @expr,
1360 $self->opcode( save_to_lvar => $lvar_id, symbol => $lhs, comment => $node->id);
1361 }
1362  
1363 72 ▶sub _localize_vars {
Subroutine _localize_vars: called CC=5 SCAR=16.1
1364 72 my($self, $vars) = @_;
1365 72 my @localize;
1366 72 my @pairs = @{$vars};
1367  
1368 72 ▶ if( (@pairs % 2) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs % 2 != 0) 16 56 72
1369 16 ▶ if(@pairs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs == 1) { } 14 2 16
1370 14 return $self->compile_ast(@pairs),
1371 $self->opcode( 'localize_vars' );
1372 }
1373 else {
1374 2 $self->_error("You must pass pairs of expressions to include");
1375 }
1376 }
1377  
1378 56 ▶ while(my($key, $expr) = splice @pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 66 56 122
1379 66 ▶ if(!any_in($key->arity, qw(literal variable))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (any_in($key->arity, ("literal", "variable"))) 0 66 66
1380 0 $self->_error("You must pass a simple name to localize variables", $key);
1381 }
1382 66 push @localize,
1383 $self->compile_ast($expr),
1384 $self->opcode( localize_s => $key->value, symbol => $key );
1385 }
1386 56 return @localize;
1387 }
1388  
1389 5576 ▶sub _variable_to_value {
Subroutine _variable_to_value: called CC=1 SCAR=0.0
1390 5576 my($self, $arg) = @_;
1391  
1392 5576 my $name = $arg->value;
1393 5576 $name =~ s/\$//;
1394 5576 return $name;
1395 }
1396  
1397 218 ▶sub requires {
Subroutine requires: called CC=1 SCAR=0.0
Pod undocumented
1398 218 my($self, @files) = @_;
1399 218 push @{ $self->dependencies }, @files;
1400 218 return;
1401 }
1402  
1403 14 ▶sub can_be_in_list_context {
Subroutine can_be_in_list_context: called CC=3 SCAR=11.3
Pod undocumented
1404 14 my $i = 2;
1405 14 ▶ while(my $funcname = (caller ++$i)[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $funcname = (caller ++$i)[3]) 28 0 28
1406 28 ▶ if($funcname =~ /::_generate_(\w+) \z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcname =~ /::_generate_(\w+) \z/msx) 14 14 28
1407 14 return any_in($1, qw(
1408 methodcall
1409 call
1410 composer
1411 ));
1412 }
1413 }
1414 0 return 0;
1415 }
1416  
1417 # optimizatin stuff
1418  
1419 2746 ▶sub _code_is_literal {
Subroutine _code_is_literal: called CC=3 SCAR=11.0
1420 2746 my($self, @code) = @_;
1421 2746 ▶ return @code == 1
Condition83%5/6
@code == 1 && ($code[0][0] eq 'literal' || $code[0][0] eq 'literal_i')
!ll&&!rl&&r
6221294830
$code[0][0] eq "literal" or $code[0][0] eq "literal_i"
l!l&&r!l&&!r
83001294
MC/DC66%2/3
@code == 1 && ($code[0][0] eq 'literal' || $code[0][0] eq 'literal_i')
@code == 1 $code[0][0] eq "literal" $code[0][0] eq "literal_i"
Truth table75%3/4
A && B or C
A= @code == 1B= $code[0][0] eq "literal"C= $code[0][0] eq "literal_i"
ABCresult
0XX0
1000
11X1
1011
1422 && ( $code[0][_OP_NAME] eq 'literal'
1423 || $code[0][_OP_NAME] eq 'literal_i');
1424 }
1425  
1426 280 ▶sub _fold_constants {
Subroutine _fold_constants: called CC=3 SCAR=11.2
1427 280 my($self, $code) = @_;
1428 280 ▶ my $engine = $self->engine or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $engine = $self->engine or return 0 280 0 280
1429  
1430 280 local $engine->{warn_handler} = \&Carp::croak;
1431 280 local $engine->{die_handler} = \&Carp::croak;
1432 280 local $engine->{verbose} = 1;
1433  
1434 280 my $result = eval {
1435 280 my @tmp_code = (@{$code}, $self->opcode('print_raw'), $self->opcode('end'));
1436 280 $engine->_assemble(\@tmp_code, '<string>', undef, undef, undef);
1437 280 $engine->render('<string>');
1438 };
1439 280 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 280 280
1440 0 Carp::carp("[BUG] Constant folding failed (ignored): $@");
1441 0 return 0;
1442 }
1443  
1444 280 @{$code} = ($self->opcode( literal => $result, comment => "optimized by constant folding"));
1445 280 return 1;
1446 }
1447  
1448  
1449 12503 ▶sub _noop {
Subroutine _noop: called CC=1 SCAR=0.0
1450 12503 my($self, $op) = @_;
1451 12503 @{$op} = @{ $self->opcode( noop => undef, comment => "ex-$op->[0]") };
1452 12503 return;
1453 }
1454  
1455 20574 ▶sub _optimize_vmcode {
Subroutine _optimize_vmcode: called CC=27 SCAR=33.1
1456 20574 my($self, $c) = @_;
1457  
1458 # calculate goto addresses
1459 # eg:
1460 #
1461 # goto +3
1462 # foo
1463 # noop
1464 # bar // goto destination
1465 #
1466 # to be:
1467 #
1468 # goto +2
1469 # foo
1470 # bar // goto destination
1471  
1472 20574 my @goto_addr;
1473 20574 ▶ for(my $i = 0; $i < @{$c}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$c;}) 254901 20574 275475
1474 254901 ▶ if(exists $goto_family{ $c->[$i][_OP_NAME] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $goto_family{$c->[$i][0]}) 7047 247854 254901
1475 7047 my $addr = $c->[$i][_OP_ARG]; # relational addr
1476  
1477 # mark ragens that goto family have its effects
1478 7047 ▶ my @range = $addr > 0
Branch: 1 branch
BranchTrueFalseTotal
$addr > 0 ? : 5835 1212 7047
1479 ? ($i .. ($i+$addr-1)) # positive
1480 : (($i+$addr) .. $i); # negative
1481  
1482 7047 foreach my $j(@range) {
1483 44101 ▶ push @{$goto_addr[$j] ||= []}, $c->[$i];
Condition100%2/2
$goto_addr[$j] ||= []
l!l
1766026441
MC/DC100%1/1
$goto_addr[$j] ||= []
$goto_addr[$j]
Truth table100%2/2
A
A= $goto_addr[$j]
Aresult
11
00
1484 }
1485 }
1486 }
1487  
1488 20574 ▶ for(my $i = 0; $i < @{$c}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$c;}) 254901 20574 275475
1489 254901 my $name = $c->[$i][_OP_NAME];
1490 254901 ▶ if($name eq 'print_raw_s') {
Branch: 4 branches
BranchTrueFalseTotal
if ($name eq 'print_raw_s') { } 29412 225489 254901
elsif ($name eq 'save_to_lvar') { } 2531 222958 225489
elsif ($name eq 'literal') { } 38941 184017 222958
elsif ($name eq 'fetch_field') { } 276 183741 184017
1491 # merge a chain of print_raw_s into single command
1492 29412 my $j = $i + 1; # from the next op
1493 29412 ▶ while($j < @{$c}
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$c;} and $c->[$j][0] eq "print_raw_s" and join($", @{$goto_addr[$i] or [];}) eq join($", @{$goto_addr[$j] or [];})) 11286 29412 40698
Condition83%5/6
$j < @{$c;} and $c->[$j][0] eq "print_raw_s" and join($", @{$goto_addr[$i] or [];}) eq join($", @{$goto_addr[$j] or [];})
!ll&&!rl&&r
2907234011286
$j < @{$c;} and $c->[$j][0] eq "print_raw_s"
!ll&&!rl&&r
02907211626
MC/DC66%2/3
$j < @{$c;} and $c->[$j][0] eq "print_raw_s" and join($", @{$goto_addr[$i] or [];}) eq join($", @{$goto_addr[$j] or [];})
$j < @{$c;} $c->[$j][0] eq "print_raw_s" join($", @{$goto_addr[$i] or [];}) eq join($", @{$goto_addr[$j] or [];})
Truth table75%3/4
A and B and C
A= $j < @{$c;}B= $c->[$j][0] eq "print_raw_s"C= join($", @{$goto_addr[$i] or [];}) eq join($", @{$goto_addr[$j] or [];})
ABCresult
0XX0
10X0
1100
1111
1494 && $c->[$j][_OP_NAME] eq 'print_raw_s'
1495 11626 ▶ && "@{$goto_addr[$i] || []}" eq "@{$goto_addr[$j] || []}") {
Condition100%4/4
$goto_addr[$i] || []
l!l
61411012
$goto_addr[$j] || []
l!l
31011316
MC/DC100%1/1
$goto_addr[$i] || []
$goto_addr[$i]
MC/DC100%1/1
$goto_addr[$j] || []
$goto_addr[$j]
Truth table100%2/2
A
A= $goto_addr[$i]
Aresult
11
00
Truth table100%2/2
A
A= $goto_addr[$j]
Aresult
11
00
1496  
1497 11286 $c->[$i][_OP_ARG] .= $c->[$j][_OP_ARG];
1498  
1499 11286 $self->_noop($c->[$j]);
1500 11286 $j++;
1501 }
1502 }
1503 elsif($name eq 'save_to_lvar') {
1504 # use registers, instead of local variables
1505 #
1506 # given:
1507 # save_to_lvar $n
1508 # <single-op>
1509 # load_lvar_to_sb $n
1510 # convert into:
1511 # move_to_sb
1512 # <single-op>
1513 2531 my $it = $c->[$i];
1514 2531 my $nn = $c->[$i+2]; # next next
1515 2531 ▶ if(defined($nn)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nn and $nn->[0] eq "load_lvar_to_sb" and $nn->[1] == $it->[1]) 1217 1314 2531
Condition66%4/6
defined $nn and $nn->[0] eq "load_lvar_to_sb" and $nn->[1] == $it->[1]
!ll&&!rl&&r
131401217
defined $nn and $nn->[0] eq "load_lvar_to_sb"
!ll&&!rl&&r
013141217
MC/DC33%1/3
defined $nn and $nn->[0] eq "load_lvar_to_sb" and $nn->[1] == $it->[1]
defined $nn $nn->[0] eq "load_lvar_to_sb" $nn->[1] == $it->[1]
Truth table50%2/4
A and B and C
A= defined $nnB= $nn->[0] eq "load_lvar_to_sb"C= $nn->[1] == $it->[1]
ABCresult
0XX0
10X0
1100
1111
1516 && $nn->[_OP_NAME] eq 'load_lvar_to_sb'
1517 && $nn->[_OP_ARG] == $it->[_OP_ARG]) {
1518 1217 @{$it} = @{$self->opcode( move_to_sb => undef, comment => "ex-$it->[0]" )};
1519  
1520 1217 $self->_noop($nn);
1521 }
1522 }
1523 elsif($name eq 'literal') {
1524 38941 ▶ if(is_int($c->[$i][_OP_ARG])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int($c->[$i][1])) 1861 37080 38941
1525 1861 $c->[$i][_OP_NAME] = 'literal_i';
1526 1861 $c->[$i][_OP_ARG] = int($c->[$i][_OP_ARG]); # force int
1527 }
1528 }
1529 elsif($name eq 'fetch_field') {
1530 276 my $prev = $c->[$i-1];
1531 276 ▶ if($prev->[_OP_NAME] =~ /^literal/) { # literal or literal_i
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->[0] =~ /^literal/) 0 276 276
1532 0 $c->[$i][_OP_NAME] = 'fetch_field_s';
1533 0 $c->[$i][_OP_ARG] = $prev->[_OP_ARG]; # arg
1534  
1535 0 $self->_noop($prev);
1536 }
1537 }
1538 }
1539  
1540 # remove noop
1541 20574 ▶ for(my $i = 0; $i < @{$c}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$c;}) 243678 20574 264252
1542 243678 ▶ if($c->[$i][_OP_NAME] eq 'noop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[$i][0] eq "noop") 11223 232455 243678
1543 11223 ▶ if(defined $goto_addr[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $goto_addr[$i]) 776 10447 11223
1544 776 foreach my $goto(@{ $goto_addr[$i] }) {
1545 # reduce its absolute value
1546 1192 ▶ $goto->[1] > 0
Branch: 1 branch
BranchTrueFalseTotal
$goto->[1] > 0 ? : 862 330 1192
1547 ? $goto->[1]-- # positive
1548 : $goto->[1]++; # negative
1549 }
1550 }
1551 11223 splice @{$c}, $i, 1;
1552 # adjust @goto_addr, but it may be empty
1553 11223 ▶ splice @goto_addr, $i, 1 if @goto_addr > $i;
Branch: 1 branch
BranchTrueFalseTotal
if @goto_addr > $i 1241 9982 11223
1554 }
1555 }
1556 20574 return;
1557 }
1558  
1559 14 ▶sub as_assembly {
Subroutine as_assembly: called CC=9 SCAR=22.1
Pod undocumented
1560 14 my($self, $code_ref, $addix) = @_;
1561  
1562 14 my $asm = "";
1563 14 foreach my $ix(0 .. (@{$code_ref}-1)) {
1564 94 my($name, $arg, $line, $file, $label, $comment) = @{$code_ref->[$ix]};
1565 94 ▶ $asm .= "$ix:" if $addix; # for debugging
Branch: 1 branch
BranchTrueFalseTotal
if $addix 0 94 94
1566  
1567 # "$opname $arg #$line:$file *$symbol // $comment"
1568 94 ▶ ref($name) and die "Oops: " . p($code_ref->[$ix]);
Branch: 1 branch
BranchTrueFalseTotal
ref $name and die 'Oops: ' . p($code_ref->[$ix]) 0 94 94
1569 94 $asm .= $name;
1570 94 ▶ if(defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 22 72 94
1571 22 $asm .= " " . value_to_literal($arg);
1572 }
1573 94 ▶ if(defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 56 38 94
1574 56 $asm .= " #$line";
1575 56 ▶ if(defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 14 42 56
1576 14 $asm .= ":" . value_to_literal($file);
1577 }
1578 }
1579 94 ▶ if(defined $label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label) 0 94 94
1580 0 $asm .= " " . value_to_literal($label);
1581 }
1582 94 ▶ if(defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 8 86 94
1583 8 $asm .= " // $comment";
1584 }
1585 94 $asm .= "\n";
1586 }
1587 14 return $asm;
1588 }
1589  
1590 36 ▶sub _error {
Subroutine _error: called CC=2 SCAR=6.9
1591 36 my($self, $message, $node) = @_;
1592  
1593 36 ▶ my $line = ref($node) ? $node->line : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node ? : 26 10 36
1594 36 die $self->make_error($message, $self->file, $line);
1595 }
1596  
1597 351 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
1598 351 ▶no Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
1599  
1600 351 __PACKAGE__->meta->make_immutable;
1601 __END__
↑ Summary blib/lib/Text/Xslate/Runner.pm »