Perlito5/Perl6/Emitter.pm untested

filter: Statementstmt 0.0% 0 / 362 Branchbran 0.0% 0 / 206 Conditioncond 0.0% 0 / 162 MC/DCmcdc 0.0% 0 / 101 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 877 CC 104 SCAR 60.0
CC
104
Cov
0%
emit_perl6
1260.0
emit_perl6
272.0
emit_perl6
110.0
CRAP 402.1
1 use v5;
2 use Perlito5::AST;
3 use strict;
4 use Perlito5::Perl6::TreeGrammar;
5  
6 package Perlito5::Perl6 {
7 sub emit_perl6_block {
8 my $block = $_[0];
9 return [ 'block',
10 map { defined($_) && $_->emit_perl6() } @$block
11 ];
12 }
13  
14 my %safe_char = (
15 ' ' => 1,
16 '!' => 1,
17 '"' => 1,
18 '#' => 1,
19 '$' => 1,
20 '%' => 1,
21 '&' => 1,
22 '(' => 1,
23 ')' => 1,
24 '*' => 1,
25 '+' => 1,
26 ',' => 1,
27 '-' => 1,
28 '.' => 1,
29 '/' => 1,
30 ':' => 1,
31 ';' => 1,
32 '<' => 1,
33 '=' => 1,
34 '>' => 1,
35 '?' => 1,
36 '@' => 1,
37 '[' => 1,
38 ']' => 1,
39 '^' => 1,
40 '_' => 1,
41 '`' => 1,
42 '{' => 1,
43 '|' => 1,
44 '}' => 1,
45 '~' => 1,
46 );
47  
48 sub escape_string {
49 my $s = shift;
50 my @out;
51 my $tmp = '';
52 return "''" if $s eq '';
53 # return 0+$s if (0+$s) eq $s; # XXX - this breaks with 'nan' (perl6 uses 'NaN')
54 for my $i (0 .. length($s) - 1) {
55 my $c = substr($s, $i, 1);
56 if ( ($c ge 'a' && $c le 'z')
57 || ($c ge 'A' && $c le 'Z')
58 || ($c ge '0' && $c le '9')
59 || exists( $safe_char{$c} )
60 )
61 {
62 $tmp = $tmp . $c;
63 }
64 else {
65 push @out, "'$tmp'" if $tmp ne '';
66 push @out, "chr(" . ord($c) . ")";
67 $tmp = '';
68 }
69 }
70 push @out, "'$tmp'" if $tmp ne '';
71 return @out if @out < 2;
72 return [ op => 'list:<~>', @out ];
73 }
74 }
75  
76 package Perlito5::AST::CompUnit;
77 {
78 sub emit_perl6 {
79 my $self = $_[0];
80 my @body = grep { defined($_) } @{$self->{body}};
81 my @out;
82 my $pkg = { name => 'main', body => [] };
83 for my $stmt (@body) {
84 if (ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{code} eq 'package') {
85 $Perlito5::PKG_NAME = $stmt->{namespace};
86 push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
87 [ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
88 ]
89 if @{ $pkg->{body} };
90 $pkg = { name => $stmt->{namespace}, body => [] };
91 }
92 else {
93 push @{ $pkg->{body} }, $stmt;
94 }
95 }
96 push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
97 [ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
98 ]
99 if @{ $pkg->{body} };
100 return @out;
101 }
102 sub emit_perl6_program {
103 my $comp_units = $_[0];
104 my @body = @$comp_units;
105 my @out;
106 push @out, [ comment => Perlito5::Compiler::do_not_edit("#") ];
107 my $pkg = { name => 'main', body => [] };
108 for my $stmt (@body) {
109 if (ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{code} eq 'package') {
110 $Perlito5::PKG_NAME = $stmt->{namespace};
111 if ( @{ $pkg->{body} } ) {
112 if ( $pkg->{name} eq 'main' ) {
113 push @out, map { $_->emit_perl6() } @{ $pkg->{body} }
114 }
115 else {
116 push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
117 [ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
118 ]
119 }
120 }
121 $pkg = { name => $stmt->{namespace}, body => [] };
122 }
123 else {
124 push @{ $pkg->{body} }, $stmt;
125 }
126 }
127 if ( @{ $pkg->{body} } ) {
128 if ( $pkg->{name} eq 'main' ) {
129 push @out, map { $_->emit_perl6() } @{ $pkg->{body} }
130 }
131 else {
132 push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
133 [ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
134 ]
135 }
136 }
137 return @out;
138 }
139 }
140  
141 package Perlito5::AST::Int;
142 {
143 sub emit_perl6 {
144 my $self = $_[0];
145 [ number => $self->{int} ];
146 }
147 }
148  
149 package Perlito5::AST::Num;
150 {
151 sub emit_perl6 {
152 my $self = $_[0];
153 [ number => $self->{num} ];
154 }
155 }
156  
157 package Perlito5::AST::Buf;
158 {
159 sub emit_perl6 {
160 my $self = $_[0];
161 Perlito5::Perl6::escape_string( $self->{buf} );
162 }
163 }
164  
165 package Perlito5::AST::Block;
166 {
167 sub emit_perl6 {
168 my $self = $_[0];
169 my @out;
170 push @out, [ label => $self->{label} ]
171 if $self->{label};
172 if ($self->{name}) {
173 push @out, [ stmt => [ keyword => $self->{name} ], Perlito5::Perl6::emit_perl6_block($self->{stmts}) ];
174 }
175 else {
176 push @out, Perlito5::Perl6::emit_perl6_block($self->{stmts});
177 }
178 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
179 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl6::emit_perl6_block($self->{continue}{stmts}) ]
180 }
181 return @out;
182 }
183 }
184  
185 package Perlito5::AST::Index;
186 {
187 sub emit_perl6_index {
188 my $self = $_[0];
189 my $index = $self->{index_exp};
190 if ( $index->isa('Perlito5::AST::Apply')
191 && $index->{code} eq 'prefix:<->'
192 )
193 {
194 my $arg = $index->{arguments}[0];
195 if ($arg->isa('Perlito5::AST::Int')) {
196 # [*-1]
197 return [ op => 'infix:<->', [ bareword => '*' ], $arg->emit_perl6() ];
198 }
199 }
200 return $self->{index_exp}->emit_perl6();
201 }
202  
203 sub emit_perl6 {
204 my $self = $_[0];
205 if ( $self->{obj}->isa('Perlito5::AST::Apply')
206 && $self->{obj}->{code} eq 'prefix:<@>'
207 )
208 {
209 return [ apply => '[', $self->{obj}->emit_perl6(), $self->emit_perl6_index ];
210 }
211 if ( $self->{obj}->isa('Perlito5::AST::Var')
212 && ( $self->{obj}->sigil eq '$' || $self->{obj}->sigil eq '@' )
213 )
214 {
215 $self->{obj}{sigil} = '@';
216 return [ apply => '[', $self->{obj}->emit_perl6(), $self->emit_perl6_index ];
217 }
218 if ( $self->{obj}->isa('Perlito5::AST::Apply')
219 && $self->{obj}->{code} eq 'prefix:<$>'
220 )
221 {
222 # $$a[0] ==> $a[0]
223 return [ apply => '[', $self->{obj}{arguments}[0]->emit_perl6(),
224 $self->emit_perl6_index ];
225 }
226 return [ op => 'infix:<.>', $self->{obj}->emit_perl6(),
227 [ op => 'circumfix:<[ ]>', $self->emit_perl6_index ] ];
228 }
229 }
230  
231 package Perlito5::AST::Lookup;
232 {
233 sub emit_perl6 {
234 my $self = $_[0];
235 if ( $self->{obj}->isa('Perlito5::AST::Apply')
236 && $self->{obj}->{code} eq 'prefix:<@>'
237 )
238 {
239 $self->{obj}{sigil} = '%';
240 return [ apply => '{', $self->{obj}->emit_perl6(), $self->autoquote($self->{index_exp})->emit_perl6() ];
241 }
242 if ( $self->{obj}->isa('Perlito5::AST::Var')
243 && ( $self->{obj}->sigil eq '$' || $self->{obj}->sigil eq '@' )
244 )
245 {
246 $self->{obj}{sigil} = '%';
247 return [ apply => '{', $self->{obj}->emit_perl6(), $self->autoquote($self->{index_exp})->emit_perl6() ];
248 }
249 if ( $self->{obj}->isa('Perlito5::AST::Apply')
250 && $self->{obj}->{code} eq 'prefix:<$>'
251 )
252 {
253 # $$a{0} ==> $a{0}
254 return [ apply => '{', $self->{obj}{arguments}[0]->emit_perl6(),
255 $self->autoquote($self->{index_exp})->emit_perl6() ];
256 }
257 return [ op => 'infix:<.>', $self->{obj}->emit_perl6(),
258 [ op => 'circumfix:<{ }>', $self->autoquote($self->{index_exp})->emit_perl6() ] ];
259 }
260 }
261  
262 package Perlito5::AST::Var;
263 {
264 sub emit_perl6 {
265 my $self = $_[0];
266  
267 if ( $self->{sigil} eq '$#' ) {
268 my $v = Perlito5::AST::Var->new( %$self, sigil => '@' );
269 return [ op => 'infix:<.>', $v->emit_perl6(), [ keyword => 'end' ] ];
270 }
271  
272 my $str_name = $self->{name};
273 $str_name = '\\\\' if $str_name eq '\\'; # escape $\
274 $str_name = '\\"' if $str_name eq '"'; # escape $"
275  
276 if ($self->{sigil} eq '::') {
277 if ($self->{namespace} eq '__PACKAGE__') {
278 return [ bareword => '$?PACKAGE' ];
279 }
280 return $self->{namespace};
281 }
282  
283 # Normalize the sigil
284 my $ns = '';
285 if ($self->{namespace}) {
286 if ($self->{namespace} eq 'main' && substr($self->{name}, 0, 1) eq '^') {
287 # don't add the namespace to special variables
288 return $self->{sigil} . '{' . $self->{name} . '}'
289 }
290 else {
291 $ns = $self->{namespace} . '::';
292 }
293 }
294 my $bareword = $ns . $str_name;
295 my $c = substr($self->{name}, 0, 1);
296 if ( ($c ge 'a' && $c le 'z')
297 || ($c ge 'A' && $c le 'Z')
298 || ($c eq '_')
299 )
300 {
301 return '@*ARGS' if $self->{sigil} eq '@' && $bareword eq 'ARGV';
302 return $self->{sigil} . $bareword;
303 }
304  
305 if ($self->{sigil} eq '$') {
306 return '"\n"' if $bareword eq '/'; # XXX
307 return '$*PID' if $bareword eq '$';
308 return '$*PROGRAM_NAME' if $bareword eq '0';
309 return '$!' if $bareword eq '@';
310 return '$' . ($bareword - 1) if $bareword >= 1;
311 }
312  
313 my $str = $self->{sigil} . "{'" . $bareword . "'}";
314 return $str;
315 }
316 }
317  
318  
319 package Perlito5::AST::Call;
320 {
321 sub emit_perl6 {
322 my $self = $_[0];
323 my $invocant = $self->{invocant}->emit_perl6();
324 if ( $self->{method} eq 'postcircumfix:<[ ]>' ) {
325 return [ op => 'infix:<.>', $invocant,
326 [ op => 'circumfix:<[ ]>', $self->{arguments}->emit_perl6() ] ];
327 }
328 if ( $self->{method} eq 'postcircumfix:<{ }>' ) {
329 return [ op => 'infix:<.>', $invocant,
330 [ op => 'circumfix:<{ }>', Perlito5::AST::Lookup->autoquote($self->{arguments})->emit_perl6() ] ];
331 }
332 my $meth = $self->{method};
333 if ($meth eq 'postcircumfix:<( )>') {
334 if ( ( ref($self->{invocant}) eq 'Perlito5::AST::Var'
335 && $self->{invocant}{sigil} eq '&'
336 )
337 || ( ref($self->{invocant}) eq 'Perlito5::AST::Apply'
338 && $self->{invocant}{code} eq 'prefix:<&>'
339 )
340 )
341 {
342 # &subr(args)
343 return [ apply => '(', $invocant, map { $_->emit_perl6() } @{$self->{arguments}} ];
344 }
345 $meth = '';
346 }
347 if ( ref($meth) eq 'Perlito5::AST::Var' ) {
348 $meth = $meth->emit_perl6();
349 }
350 if ( $meth ) {
351 return [ call => $invocant, $meth, map { $_->emit_perl6() } @{$self->{arguments}} ];
352 }
353 return [ op => 'infix:<.>', $invocant, [ op => 'list:<,>', map { $_->emit_perl6() } @{$self->{arguments}} ] ];
354 }
355 }
356  
357 package Perlito5::AST::Apply;
358 {
359 my %special_var = (
360 chr(15) => '$*VM', # $^O
361 );
362 my %op_translate = (
363 'list:<.>' => 'list:<~>',
364 'infix:<.=>' => 'infix:<~=>',
365 'infix:<=~>' => 'infix:<~~>',
366 'infix:<!~>' => 'infix:<!~~>',
367 'infix:<cmp>' => 'infix:<leq>',
368 'ternary:<? :>' => 'ternary:<?? !!>',
369 'reverse' => 'flip',
370 );
371  
372 sub emit_perl6_args {
373 my $self = $_[0];
374 return () if !$self->{arguments};
375 return map { $_->emit_perl6() } @{$self->{arguments}};
376 }
377 sub emit_perl6 {
378 my $self = $_[0];
379 my $code = $self->{code};
380  
381 if (ref $code) {
382 return [ op => 'infix:<.>', $code->emit_perl6(), $self->emit_perl6_args() ];
383 }
384  
385 if ($code eq 'infix:<=>>') {
386 return [ op => $code,
387 Perlito5::AST::Lookup->autoquote($self->{arguments}[0])->emit_perl6(),
388 $self->{arguments}[1]->emit_perl6() ]
389 }
390 if ($code eq 'nan' && !$self->{namespace}) {
391 return [ keyword => 'NaN' ];
392 }
393 if ($code eq 'inf' && !$self->{namespace}) {
394 return [ keyword => 'Inf' ];
395 }
396 if ($code eq '__PACKAGE__' && !$self->{namespace}) {
397 return [ bareword => '$?PACKAGE' ];
398 }
399 if ($code eq 'prefix:<$#>') {
400 return [ op => 'infix:<.>', $self->{arguments}[0]->emit_perl6(), [ keyword => 'end' ] ];
401 }
402 if ($code eq 'scalar') {
403 my $arg = $self->{arguments}[0];
404 if ($arg->isa('Perlito5::AST::Var') && $arg->{sigil} eq '@') {
405 # @a.elems
406 return [ op => 'infix:<.>', $arg->emit_perl6(), [ keyword => 'elems' ] ];
407 }
408 }
409 if ( ( $code eq 'shift'
410 || $code eq 'pop'
411 )
412 && !@{$self->{arguments}}
413 )
414 {
415 # TODO - @ARGV instead of @_ depending on context
416 return [ apply => '(', $code, '@_' ];
417 }
418 if ( $code eq 'readline'
419 && ref($self->{arguments}[0]) eq 'Perlito5::AST::Buf'
420 && $self->{arguments}[0]{buf} eq ''
421 )
422 {
423 # TODO - glob or <> depending on context
424 return [ apply => '(', [ keyword => 'lines' ], ];
425 }
426 if ($code eq 'infix:<x>' ) {
427 my $arg = $self->{arguments}->[0];
428 if ( ref($arg) eq 'Perlito5::AST::Apply' && $arg->{code} eq 'circumfix:<( )>') {
429 # ($v) x $i
430 $code = 'infix:<xx>';
431 }
432 }
433 if ( ( $code eq 'print'
434 || $code eq 'say'
435 )
436 && !@{$self->{arguments}}
437 )
438 {
439 return [ keyword => '.' . $code ];
440 }
441 if ( $code eq 'infix:<..>' ) {
442 Perlito5::Perl6::TreeGrammar->refactor_range_operator($self);
443 if (ref($self) ne 'Perlito5::AST::Apply') {
444 # the node was refactored into something else
445 return $self->emit_perl6();
446 }
447 $code = $self->{code};
448 }
449  
450 $code = $op_translate{$code} if $op_translate{$code};
451  
452 if ( $code eq 'prefix:<$>' ) {
453 my $arg = $self->{arguments}->[0];
454 return $special_var{$arg->{buf}}
455 if $arg->isa('Perlito5::AST::Buf') && exists $special_var{$arg->{buf}};
456 }
457  
458 if ( $Perlito5::Perl6::PrettyPrinter::op{ $code } ) {
459 return [ op => $code, $self->emit_perl6_args() ];
460 }
461 if ($code eq 'undef') {
462 if (@{$self->{arguments}}) {
463 die "TODO - undef(expr)";
464 }
465 else {
466 return 'Any';
467 }
468 }
469  
470 my $ns = '';
471 if ($self->{namespace}) {
472 $ns = $self->{namespace} . '::';
473 }
474 $code = $ns . $code;
475  
476 if ($self->{code} eq 'p5:s') {
477 my $modifier = $self->{arguments}->[2]->{buf};
478 $modifier = ':' . $modifier if $modifier;
479 return 's:P5' . $modifier
480 . '!' . $self->{arguments}->[0]->{buf} # emit_perl6()
481 . '!' . $self->{arguments}->[1]->{buf} # emit_perl6()
482 . '!';
483  
484 }
485 if ($self->{code} eq 'p5:m') {
486 my $s;
487 if ($self->{arguments}->[0]->isa('Perlito5::AST::Buf')) {
488 $s = $self->{arguments}->[0]->{buf}
489 }
490 else {
491 for my $ast (@{$self->{arguments}[0]{arguments}}) {
492 if ($ast->isa('Perlito5::AST::Buf')) {
493 $s .= $ast->{buf}
494 }
495 else {
496 $s .= $ast->emit_perl6(); # variable name
497 }
498 }
499 }
500 my $modifier = $self->{arguments}->[1]->{buf};
501 $modifier = ':' . $modifier if $modifier;
502  
503 return 'm:P5' . $modifier . '!' . $s . '!';
504 }
505 if ($self->{code} eq 'p5:tr') {
506 return 'tr!' . $self->{arguments}->[0]->{buf} # emit_perl6()
507 . '!' . $self->{arguments}->[1]->{buf} # emit_perl6()
508 . '!';
509 }
510  
511 if ($self->{code} eq 'package') {
512 $Perlito5::PKG_NAME = $self->{namespace};
513 return [ stmt => 'class', [ bareword => $self->{namespace} ] ]
514 }
515  
516 if ($code eq 'map' || $code eq 'grep' || $code eq 'sort') {
517 if ( $self->{special_arg} ) {
518 # TODO - test 'special_arg' type (scalar, block, ...)
519 return [ op => 'prefix:<' . $code . '>',
520 [ 'block', map { $_->emit_perl6() } @{$self->{special_arg}{stmts}} ],
521 [ 'op' => 'list:<,>', $self->emit_perl6_args() ],
522 ]
523 }
524 return [ apply => '(', $code, $self->emit_perl6_args() ];
525 }
526  
527 if ( $self->{bareword} && !@{$self->{arguments}} ) {
528 return [ bareword => $code ];
529 }
530 if ( $code eq 'eval' ) {
531 $code = 'EVAL';
532 }
533 return [ apply => '(', $code, $self->emit_perl6_args() ];
534 }
535 }
536  
537 package Perlito5::AST::If;
538 {
539 sub emit_perl6 {
540 my $self = $_[0];
541 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
542 return [ stmt_modifier => $self->{body}->emit_perl6(),
543 [ stmt => 'if', $self->{cond}->emit_perl6() ] ];
544 }
545 if ($self->{otherwise} && ref($self->{otherwise}) ne 'Perlito5::AST::Block') {
546 return [ stmt_modifier => $self->{otherwise}->emit_perl6(),
547 [ stmt => 'unless', $self->{cond}->emit_perl6() ] ];
548 }
549 my @out = ( [ stmt => [ keyword => 'if' ],
550 $self->{cond}->emit_perl6(),
551 Perlito5::Perl6::emit_perl6_block($self->{body}->stmts)
552 ] );
553 my $otherwise = $self->{otherwise};
554  
555 while ( $otherwise
556 && @{ $otherwise->{stmts} } == 1
557 && ref($otherwise->{stmts}[0]) eq 'Perlito5::AST::If'
558 && ($otherwise->{stmts}[0]{body} && ref($otherwise->{stmts}[0]{body}) eq 'Perlito5::AST::Block')
559 )
560 {
561 push @out, [ stmt => [ keyword => 'elsif' ],
562 $otherwise->{stmts}[0]{cond}->emit_perl6(),
563 Perlito5::Perl6::emit_perl6_block($otherwise->{stmts}[0]{body}{stmts})
564 ];
565 $otherwise = $otherwise->{stmts}[0]{otherwise};
566 }
567  
568 return @out if !($otherwise && scalar(@{ $otherwise->stmts }));
569  
570 push @out, [ stmt => [ keyword => 'else' ],
571 Perlito5::Perl6::emit_perl6_block($otherwise->stmts)
572 ];
573 return @out;
574 }
575 }
576  
577 package Perlito5::AST::When;
578 {
579 sub emit_perl6 {
580 my $self = $_[0];
581 return [ stmt => [ keyword => 'when' ],
582 $self->{cond}->emit_perl6(),
583 Perlito5::Perl6::emit_perl6_block($self->{body}->stmts)
584 ];
585 }
586 }
587  
588  
589 package Perlito5::AST::While;
590 {
591 sub emit_perl6 {
592 my $self = $_[0];
593  
594 Perlito5::Perl6::TreeGrammar->refactor_while_glob($self);
595 if (ref($self) ne 'Perlito5::AST::While') {
596 # the node was refactored into something else
597 return $self->emit_perl6();
598 }
599  
600 my @out;
601 push @out, [ label => $self->{label} ]
602 if $self->{label};
603 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
604 return @out,
605 [ stmt_modifier => $self->{body}->emit_perl6(),
606 [ stmt => [ keyword => 'while' ], $self->{cond}->emit_perl6() ] ];
607 }
608 push @out, [ stmt => [ keyword => 'while' ],
609 $self->{cond}->emit_perl6(),
610 Perlito5::Perl6::emit_perl6_block($self->{body}->stmts)
611 ];
612 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
613 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl6::emit_perl6_block($self->{continue}{stmts}) ]
614 }
615 return @out;
616 }
617 }
618  
619 package Perlito5::AST::For;
620 {
621 sub emit_perl6 {
622 my $self = $_[0];
623 my @out;
624 push @out, [ label => $self->{label} ]
625 if $self->{label};
626  
627 if ($self->{body} && ref($self->{body}) ne 'Perlito5::AST::Block') {
628 return @out,
629 [ stmt_modifier => $self->{body}->emit_perl6(),
630 [ stmt => 'for', $self->{cond}->emit_perl6() ] ];
631 }
632  
633 my $cond;
634 my $keyword;
635 if (ref($self->{cond}) eq 'ARRAY') {
636 # C-style for
637 $keyword = 'loop';
638 $cond = [ paren_semicolon => '(',
639 ( $self->{cond}[0] ? $self->{cond}[0]->emit_perl6() : [] ),
640 ( $self->{cond}[1] ? $self->{cond}[1]->emit_perl6() : [] ),
641 ( $self->{cond}[2] ? $self->{cond}[2]->emit_perl6() : [] ),
642 ];
643 }
644 else {
645 $keyword = 'for';
646 $cond = $self->{cond}->emit_perl6();
647 }
648  
649 my @sig;
650 my $sig_ast = $self->{topic};
651 if (!$sig_ast) {
652 # $_
653 }
654 else {
655 $sig_ast = $sig_ast->{var}
656 if ref($sig_ast) eq 'Perlito5::AST::Decl';
657 @sig = ( '->', $sig_ast->emit_perl6() );
658 }
659 push @out, [ stmt => [ keyword => $keyword ],
660 $cond,
661 @sig,
662 Perlito5::Perl6::emit_perl6_block($self->{body}->stmts)
663 ];
664 if ($self->{continue} && @{ $self->{continue}{stmts} }) {
665 push @out, [ stmt => [ keyword => 'continue' ], Perlito5::Perl6::emit_perl6_block($self->{continue}{stmts}) ]
666 }
667 return @out;
668 }
669 }
670  
671 package Perlito5::AST::Decl;
672 {
673 sub emit_perl6 {
674 my $self = $_[0];
675 return [ op => 'prefix:<' . $self->{decl} . '>',
676 ($self->{type} ? $self->{type} : ()),
677 $self->{var}->emit_perl6()
678 ];
679 }
680 }
681  
682 package Perlito5::AST::Sub;
683 {
684 sub emit_perl6 {
685 my $self = $_[0];
686  
687 Perlito5::Perl6::TreeGrammar->refactor_sub_arguments($self);
688  
689 my @parts;
690  
691 if ($self->{args}) {
692 # from refactor_sub_arguments
693  
694 push @parts, [ paren => '(',
695 ( map {[ var => $_->emit_perl6(), '?' ]} @{$self->{args}} ),
696 [ var => '*@_' ]
697 ];
698 }
699 else {
700  
701 # TODO - use sig
702  
703 # push @parts, [ paren => '(', [ bareword => $self->{sig} ] ]
704 # if defined $self->{sig};
705  
706 push @parts, [ paren => '(', [ var => '*@_' ] ];
707 }
708  
709 push @parts, Perlito5::Perl6::emit_perl6_block($self->{block}{stmts})
710 if defined $self->{block};
711 return [ op => 'prefix:<sub>', @parts ] if !$self->{name};
712  
713 my $is_our = 1; # our is default in perl5
714 $is_our = 0 if $self->{decl} eq 'my';
715  
716 my $name = $self->{name};
717 if ( $Perlito5::PKG_NAME ne $self->{namespace} ) {
718 $name = $self->{namespace} . "::" . $name;
719 $is_our = 0;
720 }
721  
722 if ($is_our) {
723 return [ stmt => [ keyword => 'our' ], [ keyword => 'sub' ], [ bareword => $name ], @parts ];
724 }
725 else {
726 return [ stmt => [ keyword => 'sub' ], [ bareword => $name ], @parts ];
727 }
728 }
729 }
730  
731 1;
732  
733 =begin
734  
735 =head1 NAME
736  
737 Perlito5::Perl6::Emit - Code generator for Perlito5-in-Perl5
738  
739 =head1 SYNOPSIS
740  
741 $program->emit_perl6() # generated Perl6 code
742  
743 =head1 DESCRIPTION
744  
745 This module generates Perl6 code for the Perlito compiler.
746  
747 =head1 AUTHORS
748  
749 Flavio Soibelmann Glock <fglock@gmail.com>.
750 The Pugs Team.
751  
752 =head1 SEE ALSO
753  
754 The Perl 6 homepage at L<http://dev.perl.org/perl6>.
755  
756 The Pugs homepage at L<http://pugscode.org/>.
757  
758 =head1 COPYRIGHT
759  
760 Copyright 2006, 2009, 2011, 2012 by Flavio Soibelmann Glock, Audrey Tang and others.
761  
762 This program is free software; you can redistribute it and/or modify it
763 under the same terms as Perl itself.
764  
765 See L<http://www.perl.com/perl/misc/Artistic.html>
766  
767 =end