Perlito5/Perl5/Emitter.pm untested

filter: Statementstmt 0.0% 0 / 305 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 174 MC/DCmcdc 0.0% 0 / 103 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 779 CC 76 SCAR 55.2
CC
76
Cov
0%
emit_perl5
812.0
emit_perl5
90.0
emit_perl5
42.0
CRAP 249.1
1 use v5;
2 use Perlito5::AST;
3 use strict;
4  
5 package Perlito5::Perl5;
6 {
7 sub escape_string {
8 return Perlito5::Dumper::escape_string($_[0]);
9 }
10 sub emit_perl5_block {
11 my $block = $_[0];
12 return [ 'block',
13 map { defined($_) && $_->emit_perl5() } @$block
14 ];
15 }
16 }
17  
18 package Perlito5::AST::CompUnit;
19 {
20 sub emit_perl5 {
21 my $self = $_[0];
22 return [ 'block', (
23 [ stmt => [ keyword => 'package'], [ bareword => $self->{name} ] ],
24 map { defined($_) && $_->emit_perl5() } @{$self->{body}}
25 )
26 ];
27 }
28 sub emit_perl5_program {
29 my $comp_units = $_[0];
30 return
31 [ comment => Perlito5::Compiler::do_not_edit("#") ],
32 map { $_->emit_perl5() }
33 map { ref($_) eq 'ARRAY' ? @$_ : $_ }
34 @$comp_units;
35 }
36 }
37  
38 package Perlito5::AST::Int;
39 {
40 sub emit_perl5 {
41 my $self = $_[0];
42 [ number => $self->{int} ];
43 }
44 }
45  
46 package Perlito5::AST::Num;
47 {
48 sub emit_perl5 {
49 my $self = $_[0];
50 [ number => $self->{num} ];
51 }
52 }
53  
54 package Perlito5::AST::Buf;
55 {
56 sub emit_perl5 {
57 my $self = $_[0];
58 if ($self->{is_vstring}) {
59 return join(".", map { ord($_) } split( //, $self->{buf} ));
60 }
61 Perlito5::Perl5::escape_string( $self->{buf} );
62 }
63 }
64  
65 package Perlito5::AST::Block;
66 {
67 sub emit_perl5 {
68 my $self = $_[0];
69 my @out;
70 push @out, [ label => $self->{label} ]
71 if $self->{label};
72 if ($self->{name}) {
73 push @out, [ stmt => [ keyword => $self->{name} ], Perlito5::Perl5::emit_perl5_block($self->{stmts}) ];
74 }
75 else {
76 push @out, Perlito5::Perl5::emit_perl5_block($self->{stmts});
77 }
78 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
79 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl5::emit_perl5_block($self->{continue}{stmts}) ]
80 }
81 return @out;
82 }
83 }
84  
85 package Perlito5::AST::Index;
86 {
87 sub emit_perl5 {
88 my $self = $_[0];
89 if ( ( $self->{obj}->isa('Perlito5::AST::Apply')
90 && $self->{obj}->{code} eq 'prefix:<@>'
91 )
92 || ( $self->{obj}->isa('Perlito5::AST::Var')
93 && ( $self->{obj}->sigil eq '$' || $self->{obj}->sigil eq '@' )
94 )
95 )
96 {
97 return [ apply => '[', $self->{obj}->emit_perl5(), $self->{index_exp}->emit_perl5() ];
98 }
99 if ( ( $self->{obj}->isa('Perlito5::AST::Apply')
100 && $self->{obj}->{code} eq 'prefix:<%>'
101 )
102 || ( $self->{obj}->isa('Perlito5::AST::Var')
103 && ( $self->{obj}->sigil eq '%' )
104 )
105 )
106 {
107 return [ apply => '[', $self->{obj}->emit_perl5(), $self->{index_exp}->emit_perl5() ];
108 }
109 if ( $self->{obj}->isa('Perlito5::AST::Apply')
110 && $self->{obj}->{code} eq 'prefix:<$>'
111 )
112 {
113 # $$a[0] ==> $a->[0]
114 return [ op => 'infix:<->>', $self->{obj}{arguments}[0]->emit_perl5(),
115 [ op => 'circumfix:<[ ]>', $self->{index_exp}->emit_perl5() ] ];
116 }
117 return [ op => 'infix:<->>', $self->{obj}->emit_perl5(),
118 [ op => 'circumfix:<[ ]>', $self->{index_exp}->emit_perl5() ] ];
119 }
120 }
121  
122 package Perlito5::AST::Lookup;
123 {
124 sub emit_perl5 {
125 my $self = $_[0];
126 if ( ( $self->{obj}->isa('Perlito5::AST::Apply')
127 && $self->{obj}->{code} eq 'prefix:<@>'
128 )
129 || ( $self->{obj}->isa('Perlito5::AST::Var')
130 && ( $self->{obj}->sigil eq '$' || $self->{obj}->sigil eq '@' )
131 )
132 )
133 {
134 return [ apply => '{', $self->{obj}->emit_perl5(), $self->autoquote($self->{index_exp})->emit_perl5() ];
135 }
136 if ( ( $self->{obj}->isa('Perlito5::AST::Apply')
137 && $self->{obj}->{code} eq 'prefix:<%>'
138 )
139 || ( $self->{obj}->isa('Perlito5::AST::Var')
140 && ( $self->{obj}->sigil eq '%' )
141 )
142 )
143 {
144 # perl5.20: %a{ x, y }
145 return [ apply => '{', $self->{obj}->emit_perl5(), $self->autoquote($self->{index_exp})->emit_perl5() ];
146 }
147 if ( $self->{obj}->isa('Perlito5::AST::Apply')
148 && $self->{obj}->{code} eq 'prefix:<$>'
149 )
150 {
151 # $$a{0} ==> $a->{0}
152 return [ op => 'infix:<->>', $self->{obj}{arguments}[0]->emit_perl5(),
153 [ op => 'circumfix:<{ }>', $self->autoquote($self->{index_exp})->emit_perl5() ] ];
154 }
155 return [ op => 'infix:<->>', $self->{obj}->emit_perl5(),
156 [ op => 'circumfix:<{ }>', $self->autoquote($self->{index_exp})->emit_perl5() ] ];
157 }
158 }
159  
160 package Perlito5::AST::Var;
161 {
162 sub emit_perl5 {
163 my $self = $_[0];
164 $self = $self->to_begin_scratchpad();
165  
166 my $str_name = $self->{name};
167 my $c = substr($str_name, 0, 1);
168  
169 if ($c ne "" && $c lt " " && $self->{sigil} ne "::") {
170 return $self->{sigil} . "{^" . chr( ord($c) + ord("A") - 1 ) . substr($str_name, 1) . "}";
171 }
172  
173 # Normalize the sigil
174 my $ns = '';
175 if ($self->{namespace}) {
176 return $self->{namespace} . '::'
177 if $self->{sigil} eq '::';
178 if ($self->{namespace} eq 'main' && substr($str_name, 0, 1) eq '^') {
179 # don't add the namespace to special variables
180 return $self->{sigil} . '{' . $str_name . '}'
181 }
182 else {
183 $ns = $self->{namespace} . '::';
184 }
185 }
186  
187 if ( ($c ge 'a' && $c le 'z')
188 || ($c ge 'A' && $c le 'Z')
189 || ($c eq '_')
190 || ($str_name eq '/' || $str_name eq '&')
191 || ( (0 + $str_name) eq $str_name ) # numeric
192 )
193 {
194 return $self->{sigil} . $ns . $str_name
195 }
196 return $self->{sigil} . "{" . Perlito5::Perl5::escape_string( $ns . $str_name ) . "}";
197 }
198 }
199  
200 package Perlito5::AST::Call;
201 {
202 sub emit_perl5 {
203 my $self = $_[0];
204 my $invocant = $self->{invocant}->emit_perl5();
205 if ( $self->{method} eq 'postcircumfix:<[ ]>' ) {
206 return [ op => 'infix:<->>', $invocant,
207 [ op => 'circumfix:<[ ]>', $self->{arguments}->emit_perl5() ] ];
208 }
209 if ( $self->{method} eq 'postcircumfix:<{ }>' ) {
210 return [ op => 'infix:<->>', $invocant,
211 [ op => 'circumfix:<{ }>', Perlito5::AST::Lookup->autoquote($self->{arguments})->emit_perl5() ] ];
212 }
213 my $meth = $self->{method};
214 if ($meth eq 'postcircumfix:<( )>') {
215 $meth = '';
216 }
217 if ( ref($meth) ) {
218 $meth = $meth->emit_perl5();
219 }
220 if ( $meth ) {
221 return [ call => $invocant, $meth, map { $_->emit_perl5() } @{$self->{arguments}} ];
222 }
223 return [ op => 'infix:<->>', $invocant, [ op => 'list:<,>', map { $_->emit_perl5() } @{$self->{arguments}} ] ];
224 }
225 }
226  
227 package Perlito5::AST::Apply;
228 {
229 sub emit_perl5_args {
230 my $self = $_[0];
231 return () if !$self->{arguments};
232 return map { $_->emit_perl5() } @{$self->{arguments}};
233 }
234 sub emit_perl5_choose_regex_quote {
235 if (! grep { $_ =~ /\// } @_) {
236 return "/";
237 }
238 if (! grep { $_ =~ /!/ } @_) {
239 return "!";
240 }
241 if (! grep { $_ =~ /%/ } @_) {
242 return "%";
243 }
244 if (! grep { $_ =~ /:/ } @_) {
245 return ":";
246 }
247 if (! grep { $_ =~ /;/ } @_) {
248 return ";";
249 }
250 return "^";
251 }
252 sub emit_perl5_regex_expression {
253 my $ast = $_[0];
254 if ($ast->isa('Perlito5::AST::Buf')) {
255 my $replace = $ast->{buf};
256 # $replace =~ s{\\}{\\\\}g;
257 return $replace;
258 }
259 if ($ast->isa('Perlito5::AST::Apply') && $ast->{code} eq 'list:<.>') {
260 # concatenate arguments
261 my $s = "";
262 for my $a (@{$ast->{arguments}}) {
263 $s .= emit_perl5_regex_expression($a);
264 }
265 return $s;
266 }
267 # variable, lookup, index
268 my $out = [];
269 Perlito5::Perl5::PrettyPrinter::pretty_print([$ast->emit_perl5()], 0, $out);
270 my $code = join('', @{$out});
271 chomp $code;
272 return $code;
273 }
274 sub emit_perl5 {
275 my $self = $_[0];
276 if (ref $self->{code}) {
277  
278 my $code = $self->{code};
279 if ( ref($code) eq 'Perlito5::AST::Apply' && $code->code eq "prefix:<&>") {
280 # &$c()
281 return [ apply => '(', $code->emit_perl5(), $self->emit_perl5_args() ];
282 }
283  
284 return [ op => 'infix:<->>', $self->{code}->emit_perl5(), $self->emit_perl5_args() ];
285 }
286 if ($self->{code} eq 'infix:<=>>') {
287 return [ op => $self->{code},
288 Perlito5::AST::Lookup->autoquote($self->{arguments}[0])->emit_perl5(),
289 $self->{arguments}[1]->emit_perl5() ]
290 }
291  
292 if ($self->{namespace} eq 'Perlito5') {
293 # if ($self->{code} eq 'nop') {
294 # return ();
295 # }
296 if ($self->{code} eq 'eval_ast') {
297 $self->{namespace} = 'Perlito5::Perl5::Runtime';
298 }
299 }
300  
301 my $ns = '';
302 if ($self->{namespace}) {
303 $ns = $self->{namespace} . '::';
304 }
305 my $code = $ns . $self->{code};
306  
307 if ($code eq 'circumfix:<{ }>' && @{ $self->{'arguments'} } == 1) {
308 # disambiguate block {1} from hash {"1"=>undef}
309 return ['op', $code, $self->{'arguments'}->[0]->emit_perl5(), "" ];
310 }
311  
312 if ( $code eq 'prefix:<$>'
313 || $code eq 'prefix:<@>'
314 || $code eq 'prefix:<%>'
315 || $code eq 'prefix:<&>'
316 || $code eq 'prefix:<*>'
317 || $code eq 'prefix:<$#>')
318 {
319 my $arg = $self->{'arguments'}->[0];
320 if (ref($arg) eq 'Perlito5::AST::Apply' && $arg->{code} eq 'do') {
321 my $arg = $arg->{'arguments'}->[0];
322 if (ref($arg) eq 'Perlito5::AST::Block') {
323 return ['op' => $code, $arg->emit_perl5() ];
324 }
325 }
326 # if (ref($arg) eq 'Perlito5::AST::Apply') {
327 $code =~ /<([^>]+)>/;
328 my $cap = $1;
329 return [ apply => '{', $cap, $arg->emit_perl5() ];
330 # }
331 }
332 if (( $code eq 'eval'
333 || $code eq 'do'
334 )
335 && ref($self->{'arguments'}->[0]) eq 'Perlito5::AST::Block'
336 )
337 {
338 return ['op' => 'prefix:<' . $code . '>', $self->{'arguments'}->[0]->emit_perl5()]
339 }
340  
341 if ( $Perlito5::Perl5::PrettyPrinter::op{ $self->{code} } ) {
342 return [ op => $self->{code}, $self->emit_perl5_args() ];
343 }
344  
345 if ($self->{code} eq 'p5:s') {
346 my $replace0 = emit_perl5_regex_expression($self->{arguments}->[0]);
347 my $replace1 = emit_perl5_regex_expression($self->{arguments}->[1]);
348 my $q = emit_perl5_choose_regex_quote(
349 $replace0,
350 $replace1,
351 $self->{arguments}->[2]->{buf},
352 );
353 return 's' . $q . $replace0 # emit_perl5()
354 . $q . $replace1 # emit_perl5()
355 . $q . $self->{arguments}->[2]->{buf};
356  
357 }
358 if ($self->{code} eq 'p5:m') {
359 my $replace0 = emit_perl5_regex_expression($self->{arguments}->[0]);
360 my $q = emit_perl5_choose_regex_quote(
361 $replace0,
362 $self->{arguments}->[1]->{buf},
363 );
364 return 'm' . $q . $replace0 . $q . $self->{arguments}->[1]->{buf};
365 }
366 if ($self->{code} eq 'p5:tr') {
367 my $replace0 = emit_perl5_regex_expression($self->{arguments}->[0]);
368 my $replace1 = emit_perl5_regex_expression($self->{arguments}->[1]);
369 my $q = emit_perl5_choose_regex_quote(
370 $replace0,
371 $replace1,
372 $self->{arguments}->[2]->{buf},
373 );
374 return 'tr' . $q . $replace0 # emit_perl5()
375 . $q . $replace1 # emit_perl5()
376 . $q . $self->{arguments}->[2]->{buf};
377 }
378 if ($self->{code} eq 'p5:qr') {
379 my $replace0 = emit_perl5_regex_expression($self->{arguments}->[0]);
380 my $q = emit_perl5_choose_regex_quote(
381 $replace0,
382 $self->{arguments}->[1]->{buf},
383 );
384 return 'qr' . $q . $replace0 . $q . $self->{arguments}->[1]->{buf};
385 }
386 if ($self->{code} eq 'p5:format') {
387 my @arg = @{ $self->{arguments} };
388 my $name = shift @arg;
389 $name = $name->{buf} if ref $name;
390 my @str;
391 push @str, "format " . ( $name ? $name . " " : "" ) . "=";
392 while (@arg) {
393 my $picture = shift @arg;
394 push @str, $picture->{buf};
395 my $ast = shift @arg;
396 if ($ast) {
397 my $out = [];
398 Perlito5::Perl5::PrettyPrinter::pretty_print([$ast->emit_perl5()], 0, $out);
399 my $code = join('', @{$out});
400 chomp $code;
401 push @str, $code;
402 }
403 }
404 push @str, ".\n";
405 return join("\n", @str);
406 }
407  
408 if ($self->{code} eq 'package') { return [ stmt => 'package', [ bareword => $self->{namespace} ] ] }
409  
410 if ( $code eq 'map'
411 || $code eq 'grep'
412 || $code eq 'sort'
413 || $code eq 'print'
414 || $code eq 'use'
415 ) {
416 if ( $self->{special_arg} ) {
417 # TODO - test 'special_arg' type (scalar, block, ...)
418 return [ op => 'prefix:<' . $code . '>',
419 $self->{special_arg}->emit_perl5,
420 [ 'op' => 'list:<,>', $self->emit_perl5_args() ],
421 ];
422 }
423 return [ apply => '(', $code, $self->emit_perl5_args() ];
424 }
425  
426 if ($code eq 'readline') {
427 return [ paren => '<', $self->emit_perl5_args() ];
428 }
429  
430 $code = "&" . $code
431 if $self->{ignore_proto};
432  
433 if ( $self->{bareword} && !@{$self->{arguments}} ) {
434 my $effective_name = ($self->{namespace} || $Perlito5::PKG_NAME) . '::' . $self->{code};
435 if (exists $Perlito5::PROTO->{$effective_name}) {
436 $code = $effective_name;
437 }
438 else {
439 # shift / push / return
440 return ['bareword' => $code]
441 }
442 }
443 return [ apply => '(', $code, $self->emit_perl5_args() ];
444 }
445 }
446  
447 package Perlito5::AST::If;
448 {
449 sub emit_perl5 {
450 my $self = $_[0];
451 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
452 return [ stmt_modifier => $self->{body}->emit_perl5(),
453 [ stmt => 'if', $self->{cond}->emit_perl5() ] ];
454 }
455 if ($self->{otherwise} && ref($self->{otherwise}) ne 'Perlito5::AST::Block') {
456 return [ stmt_modifier => $self->{otherwise}->emit_perl5(),
457 [ stmt => 'unless', $self->{cond}->emit_perl5() ] ];
458 }
459 my @out = ( [ stmt => [ keyword => 'if' ],
460 [ paren => '(', $self->{cond}->emit_perl5() ],
461 Perlito5::Perl5::emit_perl5_block($self->{body}->stmts)
462 ] );
463 my $otherwise = $self->{otherwise};
464  
465 while ( $otherwise
466 && @{ $otherwise->{stmts} } == 1
467 && ref($otherwise->{stmts}[0]) eq 'Perlito5::AST::If'
468 && ($otherwise->{stmts}[0]{body} && ref($otherwise->{stmts}[0]{body}) eq 'Perlito5::AST::Block')
469 )
470 {
471 push @out, [ stmt => [ keyword => 'elsif' ],
472 [ paren => '(', $otherwise->{stmts}[0]{cond}->emit_perl5() ],
473 Perlito5::Perl5::emit_perl5_block($otherwise->{stmts}[0]{body}{stmts})
474 ];
475 $otherwise = $otherwise->{stmts}[0]{otherwise};
476 }
477  
478 return @out if !($otherwise && scalar(@{ $otherwise->stmts }));
479  
480 push @out, [ stmt => [ keyword => 'else' ],
481 Perlito5::Perl5::emit_perl5_block($otherwise->stmts)
482 ];
483 return @out;
484 }
485 }
486  
487 package Perlito5::AST::When;
488 {
489 sub emit_perl5 {
490 my $self = $_[0];
491 return [ stmt => [ keyword => 'when' ],
492 [ paren => '(', $self->{cond}->emit_perl5() ],
493 Perlito5::Perl5::emit_perl5_block($self->{body}->stmts)
494 ];
495 }
496 }
497  
498  
499 package Perlito5::AST::While;
500 {
501 sub emit_perl5 {
502 my $self = $_[0];
503 my @out;
504 push @out, [ label => $self->{label} ]
505 if $self->{label};
506 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
507 return @out,
508 [ stmt_modifier => $self->{body}->emit_perl5(),
509 [ stmt => [ keyword => 'while' ], $self->{cond}->emit_perl5() ] ];
510 }
511 push @out, [ stmt => [ keyword => 'while' ],
512 [ paren => '(', $self->{cond}->emit_perl5() ],
513 Perlito5::Perl5::emit_perl5_block($self->{body}->stmts)
514 ];
515 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
516 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl5::emit_perl5_block($self->{continue}{stmts}) ]
517 }
518 return @out;
519 }
520 }
521  
522 package Perlito5::AST::For;
523 {
524 sub emit_perl5 {
525 my $self = $_[0];
526 my @out;
527 push @out, [ label => $self->{label} ]
528 if $self->{label};
529  
530 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
531 return @out,
532 [ stmt_modifier => $self->{body}->emit_perl5(),
533 [ stmt => 'for', $self->{cond}->emit_perl5() ] ];
534 }
535  
536 my $cond;
537 if (ref($self->{cond}) eq 'ARRAY') {
538 # C-style for
539 $cond = [ paren_semicolon => '(',
540 ( $self->{cond}[0] ? $self->{cond}[0]->emit_perl5() : [] ),
541 ( $self->{cond}[1] ? $self->{cond}[1]->emit_perl5() : [] ),
542 ( $self->{cond}[2] ? $self->{cond}[2]->emit_perl5() : [] ),
543 ];
544 }
545 else {
546 $cond = [ paren => '(', $self->{cond}->emit_perl5() ];
547 }
548  
549 my @sig;
550 my $sig_ast = $self->{topic};
551 if (!$sig_ast) {
552 # $_
553 }
554 else {
555 @sig = $sig_ast->emit_perl5();
556 }
557 push @out, [ stmt => [ keyword => 'for' ],
558 @sig,
559 $cond,
560 Perlito5::Perl5::emit_perl5_block($self->{body}->stmts)
561 ];
562 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
563 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl5::emit_perl5_block($self->{continue}{stmts}) ]
564 }
565 return @out;
566 }
567 }
568  
569 package Perlito5::AST::Decl;
570 {
571 sub emit_perl5 {
572 my $self = $_[0];
573  
574 my $var = $self->{var};
575 return $var->emit_perl5() if ref($var) eq 'Perlito5::AST::Var' && $var->is_begin_scratchpad();
576  
577 return [ op => 'prefix:<' . $self->{decl} . '>',
578 ($self->{type} ? $self->{type} : ()),
579 $var->emit_perl5()
580 ];
581 }
582 }
583  
584 package Perlito5::AST::Sub;
585 {
586 sub emit_perl5 {
587 my $self = $_[0];
588 my @sig;
589 my @parts;
590 # Note: enable this to perform state() vars macro substitution
591 # if (my $node = $self->maybe_rewrite_statevars()) {
592 # return $node->emit_perl5(@_[1..$#_]);
593 # }
594 push @sig, [ paren => '(', [ bareword => $self->{sig} ] ]
595 if defined $self->{sig};
596  
597 if (defined $self->{block}) {
598 push @parts, Perlito5::Perl5::emit_perl5_block( $self->{'block'}{stmts} );
599 }
600  
601 return [ op => 'prefix:<sub>', @sig, @parts ] if !$self->{name};
602 return [ stmt => [ keyword => 'sub' ], [ bareword => $self->{namespace} . "::" . $self->{name} ], @sig, @parts ];
603 }
604 }
605  
606 1;
607  
608 =begin
609  
610 =head1 NAME
611  
612 Perlito5::Perl5::Emit - Code generator for Perlito5-in-Perl5
613  
614 =head1 SYNOPSIS
615  
616 $program->emit_perl5() # generated Perl5 code
617  
618 =head1 DESCRIPTION
619  
620 This module generates Perl5 code for the Perlito compiler.
621  
622 =head1 AUTHORS
623  
624 Flavio Soibelmann Glock <fglock@gmail.com>.
625 The Pugs Team.
626  
627 =head1 SEE ALSO
628  
629 The Perl 6 homepage at L<http://dev.perl.org/perl6>.
630  
631 The Pugs homepage at L<http://pugscode.org/>.
632  
633 =head1 COPYRIGHT
634  
635 Copyright 2006, 2009, 2011, 2012 by Flavio Soibelmann Glock, Audrey Tang and others.
636  
637 This program is free software; you can redistribute it and/or modify it
638 under the same terms as Perl itself.
639  
640 See L<http://www.perl.com/perl/misc/Artistic.html>
641  
642 =end