Pugs/Emitter/Perl6/Perl5.pm untested

filter: Statementstmt 0.0% 0 / 321 Branchbran 0.0% 0 / 260 Conditioncond 0.0% 0 / 122 MC/DCmcdc 0.0% 0 / 73 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 822 CC 131 SCAR 64.7
CC
131
Cov
0%
default
1806.0
_emit
650.0
infix
272.0
CRAP 646.6
1 package Pugs::Emitter::Perl6::Perl5;
2  
3 # p6-ast to perl5 emitter
4  
5 use strict;
6 use warnings;
7 use Data::Dumper;
8 $Data::Dumper::Indent = 1;
9  
10 use Pugs::Emitter::Rule::Perl5::Ratchet;
11  
12 our %env;
13  
14 sub _mangle_ident {
15 my $s = shift;
16 $s =~ s/ ([^a-zA-Z0-9_:]) / '_'.ord($1).'_' /xge;
17 return $s;
18 }
19  
20 sub _mangle_var {
21 my $s = $_[0];
22 #warn "mangle: $s";
23
24 # perl6 => perl5 variables
25 return '%::ENV' if $s eq '%*ENV';
26 return '$^O' if $s eq '$*OS';
27
28 # special variables
29 return '$::_EXCL_' if $s eq '$!';
30  
31 substr($s,1) =~ s/ ([^a-zA-Z0-9_:]) / '_'.ord($1).'_' /xge;
32 return $s;
33 }
34  
35 sub _var_get {
36 my $n = $_[0];
37
38 if ( ! exists $n->{scalar} ) {
39 if ( exists $n->{bare_block} ) {
40 # TODO - check if it is a comma-delimited list
41 return ' sub ' . _emit( $n );
42 }
43 return _emit( $n );
44 }
45  
46 my $s = $n->{scalar};
47  
48 return $env{$s}{get}
49 if exists $env{$s} &&
50 exists $env{$s}{get};
51
52 # default
53 return "\$self->{'" . substr($s,2) . "'}"
54 if substr($s,1,1) eq '.';
55 return _mangle_var( $s );
56 }
57  
58 sub _var_set {
59 my $s = $_[0];
60
61 #warn "emit: set $s - ", Dumper %env;
62
63 return $env{$s}{set}
64 if exists $env{$s}{set};
65
66 # default
67 return sub { _mangle_var( $s ) . " = " . $_[0] };
68 }
69  
70 sub _not_implemented {
71 my ( $n, $what ) = @_;
72 return "die q(not implemented $what: " . Dumper( $n ) . ")";
73 }
74  
75 sub emit {
76
77 # <audreyt> %Namespace:: = (); # clear stash
78 local %env;
79
80 my ($grammar, $ast) = @_;
81 # runtime parameters: $grammar, $string, $state, $arg_list
82 # rule parameters: see Runtime::Rule.pm
83 warn Pugs::Runtime::Perl6::perl( $ast )
84 if $ENV{V6DUMPAST};
85 return _emit( $ast );
86 #"do{\n" .
87 #_emit( $ast, ' ' ) . "\n" .
88 #"}";
89 }
90  
91 sub _emit_code {
92 my $code = $_[0];
93 if (substr($code, 1,1) eq '?') {
94 my $caller_level = 0;
95 while ($code =~ s/^&\?CALLER::/&?/) {
96 ++$caller_level;
97 }
98 my $name = substr($code, 2);
99 # special!
100 if ($name eq 'ROUTINE') {
101 return "Pugs::Runtime::Perl6::Routine->new(Devel::Caller::caller_cv($caller_level))";
102 }
103 die 'unhandled magic variable';
104 }
105  
106 return $code;
107 }
108  
109 sub _emit {
110 my $n = $_[0];
111 #die "_emit: ", Dumper( $n );
112 #warn "_emit: ", Dumper( $n );
113
114 # 'undef' example: parameter list, in a sub call without parameters
115 return ''
116 unless defined $n;
117
118 die "unknown node: ", Dumper( $n )
119 unless ref( $n ) eq 'HASH';
120  
121  
122 return join ( ";\n",
123 map { _emit( $_ ) } @{$n->{statements}}
124 ) ||
125 " # empty block\n"
126 if exists $n->{statements};
127
128  
129 return _mangle_ident( $n->{bareword} )
130 if exists $n->{bareword};
131  
132 return _mangle_ident( $n->{dot_bareword} )
133 if exists $n->{dot_bareword};
134
135 return _emit_code($n->{code})
136 if exists $n->{code};
137
138 return $n->{int}
139 if exists $n->{int};
140
141 return $n->{num}
142 if exists $n->{num};
143
144 return '{' . _emit( $n->{pair}{key} ) . '=>' . _emit( $n->{pair}{value} ) . '}'
145 if exists $n->{pair};
146
147 return _var_get( $n )
148 if exists $n->{scalar};
149
150 return _mangle_var( $n->{array} )
151 if exists $n->{array};
152
153 return _mangle_var( $n->{hash} )
154 if exists $n->{hash};
155
156 return '"' . $n->{double_quoted} . '"'
157 if exists $n->{double_quoted};
158
159 return '\'' . $n->{single_quoted} . '\''
160 if exists $n->{single_quoted};
161
162 return 'qw(' . $n->{angle_quoted} . ')'
163 if exists $n->{angle_quoted};
164
165 return assoc_list( $n )
166 if exists $n->{assoc} && $n->{assoc} eq 'list';
167
168 if ( exists $n->{fixity} ) {
169 return infix( $n )
170 if $n->{fixity} eq 'infix';
171 return prefix( $n )
172 if $n->{fixity} eq 'prefix';
173 return postfix( $n )
174 if $n->{fixity} eq 'postfix';
175 return circumfix( $n )
176 if $n->{fixity} eq 'circumfix';
177 return postcircumfix( $n )
178 if $n->{fixity} eq 'postcircumfix';
179 return ternary( $n )
180 if $n->{fixity} eq 'ternary';
181 }
182
183 return statement( $n )
184 if exists $n->{statement};
185  
186 return default( $n );
187 }
188  
189 sub assoc_list {
190 my $n = $_[0];
191 # print "list emit_rule: ", Dumper( $n );
192  
193 if ( $n->{op1} eq ';' ||
194 $n->{op1} eq ',' ) {
195 return join ( $n->{op1} . "\n",
196 map { _emit( $_ ) } @{$n->{list}}
197 );
198 }
199
200 return _not_implemented( $n->{op1}, "list-op" );
201 }
202  
203 sub _emit_parameter_signature {
204 my $n = $_[0] or return '';
205 return '' unless @$n;
206 # { var => '$self', invocant => 1 },
207 # { var => '$title' },
208 # { var => '$subtitle', optional => 1 },
209 # { var => '$case', named_only => 1 },
210 # { var => '$justify', named_only => 1, required => 1});
211  
212 return join(",\n ", map { _emit_data_bind_param_spec($_) } @$n );
213  
214  
215 }
216  
217 sub _emit_data_bind_param_spec {
218 my %param = %{$_[0]};
219 # XXX: translate other attributes
220 $param{var} = delete $param{name};
221 $param{var} = delete $param{code} if $param{code};
222 my $dumped = Dumper(\%param);
223 $dumped =~ s/^\$VAR1 = //g;
224 $dumped =~ s/;$//;
225 $dumped =~ s/\n//mg;
226 return $dumped;
227 }
228  
229 sub _emit_parameter_binding {
230 my $n = $_[0];
231 # no parameters
232 return ''
233 unless defined $n;
234 #warn "parameter list: ",Dumper $n;
235  
236 # 'name' => '$desc', $v
237 # 'optional' => 1, $v?
238 # 'named_only' => 1, :$v
239 # 'type' => 'Str' Str $v
240 # 'is_slurpy' => 1, *$v
241 # 'attribute' => \@attr $v is rw
242  
243 my @params = @$n or return '';
244 my $param = join( ',' ,
245 map { _emit( {%$_, scalar => $_->{name}} ) } grep { !exists $_->{type} || $_->{type} ne 'Code' } @params
246 );
247 return((length($param) ? " my ($param);\n" : '').
248 " Data::Bind->arg_bind(\\\@_);\n");
249 }
250  
251 sub _emit_parameter_capture {
252 my $n = $_[0];
253 return '' unless $n;
254  
255 # XXX: gah i am lazy
256 if ( exists $n->{fixity} && $n->{fixity} eq 'circumfix') {
257 $n = $n->{exp1} or return '';
258 }
259 $n = { list => [$n] }
260 if !($n->{assoc} && $n->{assoc} eq 'list');
261  
262 my (@named, @positional);
263 for (@{$n->{list}}) {
264 if (my $pair = $_->{pair}) {
265 push @named, $pair->{key}{single_quoted}.' => \\('._emit($pair->{value}).')';
266 }
267 else {
268 push @positional, '\\('._emit($_).')';
269 }
270 }
271  
272 return '['.join(',', @positional).'], {'.join(',', @named).'}';
273 }
274  
275 sub default {
276 my $n = $_[0];
277 #warn "emit: ", Dumper( $n );
278
279 if ( exists $n->{die} ) {
280 return "do { die '" . $n->{die} . "' }";
281 }
282  
283 if ( exists $n->{pointy_block} ) {
284 # XXX: no signature yet
285 return "sub {\n" . _emit( $n->{pointy_block} ) . "\n }\n";
286 }
287  
288 if ( exists $n->{bare_block} ) {
289 if ( exists $n->{trait} ) {
290 # BEGIN/END
291 return $n->{trait} . " {\n" . _emit( $n->{bare_block} ) . "\n }";
292 }
293 return "{\n" . _emit( $n->{bare_block} ) . "\n }\n";
294 }
295  
296 if ( $n->{op1} eq 'call' ) {
297 # warn "call: ",Dumper $n;
298  
299 if ( $n->{sub}{bareword} eq 'grammar' ||
300 $n->{sub}{bareword} eq 'class' ||
301 $n->{sub}{bareword} eq 'module' ) {
302 # Moose: package xxx; use Moose;
303 # class Point;
304 #warn "class: ",Dumper $n;
305 local %env;
306 my $id;
307 $id = exists $n->{param}{cpan_bareword}
308 # ? _mangle_ident( $n->{param}{cpan_bareword} )
309 ? $n->{param}{cpan_bareword}
310 : _emit( $n->{param}{sub} );
311 my @a = split "-", $id;
312 my $version = ( @a > 1 && $a[-1] =~ /^[0-9]/ ? $a[-1] : '' );
313 return 'package ' . $a[0].';' .
314 ( $version ? ";\$$a[0]::VERSION = '$version'" : '' ) .
315 ( $n->{sub}{bareword} eq 'grammar'
316 ? ';use Pugs::Compiler::Rule' .
317 ';use base \'Pugs::Grammar::Base\''
318 : '' ) .
319 ( $n->{sub}{bareword} eq 'class'
320 ? ';use Moose'
321 : '' ) .
322 ";use Exporter 'import'; push our \@ISA, 'Exporter' ;our \@EXPORT";
323 }
324  
325 if ( $n->{sub}{bareword} eq 'is' ) {
326 # is Point;
327 #warn "inheritance: ",Dumper $n;
328 my $id;
329 $id = exists $n->{param}{cpan_bareword}
330 ? _mangle_ident( $n->{param}{cpan_bareword} )
331 : _emit( $n->{param}{sub} );
332 my @a = split "-", $id;
333 my $version = ( @a > 1 && $a[-1] =~ /^[0-9]/ ? $a[-1] : '' );
334 return "extends '" . $a[0] . "'";
335 }
336
337 if ( $n->{sub}{bareword} eq 'call' ) {
338 # call;
339 #warn "super call: ",Dumper $n;
340 return "super"; # param list?
341 }
342
343 if ( $n->{sub}{bareword} eq 'use' ) {
344 # use v6-alpha
345 if ( exists $n->{param}{cpan_bareword} ) {
346 if ( $n->{param}{cpan_bareword} =~ /^v6-/ ) {
347 return " # use v6-alpha\n";
348 }
349 }
350 #warn "call: ",Dumper $n;
351 if ( $n->{param}{sub}{bareword} =~ /^v5/ ) {
352 return "warn 'use v5 - not implemented'";
353 }
354 if ( $n->{param}{sub}{bareword} eq 'v6' ) {
355 return " # use v6\n";
356 }
357 # use module::name 'param'
358 return "use " . _emit( $n->{param}{sub} ) .
359 (exists $n->{param}{param} ? _emit($n->{param}{param}) : '' );
360 }
361  
362 return " " . $n->{sub}{bareword} . " '', " . _emit( $n->{param} )
363 if $n->{sub}{bareword} eq 'print' ||
364 $n->{sub}{bareword} eq 'warn';
365 return " print '', " . _emit( $n->{param} ) . ";\n" .
366 " print " . '"\n"'
367 if $n->{sub}{bareword} eq 'say';
368  
369 # TODO - other builtins
370 return " (defined " . _emit( $n->{param} ) . ")"
371 if $n->{sub}{bareword} eq 'defined';
372
373 # XXX: handle args
374 return "Pugs::Runtime::Perl6::Routine->new(Devel::Caller::caller_cv(1))"
375 if $n->{sub}{bareword} eq 'caller';
376  
377 # ???
378 $n->{sub}{bareword} = 'die'
379 if $n->{sub}{bareword} eq 'fail';
380
381  
382 # XXX: builtins
383 my $subname = $n->{sub}{bareword};
384 if ($subname eq 'defined' || $subname eq 'substr' || $subname eq 'split' || $subname eq 'die' || $subname eq 'return') {
385 return ' ' . _mangle_ident( $n->{sub}{bareword} ) . '(' . _emit( $n->{param} ) . ')';
386 }
387 return ' ' . _mangle_ident( $n->{sub}{bareword} ) . '(' . _emit_parameter_capture( $n->{param} ) . ')';
388 }
389
390 if ( $n->{op1} eq 'method_call' ) {
391 #warn "method_call: ", Dumper( $n );
392 if ( $n->{method}{dot_bareword} eq 'print' ||
393 $n->{method}{dot_bareword} eq 'warn' ) {
394 my $s = _emit( $n->{self} );
395 if ( $s eq _mangle_var('$*ERR') ) {
396 return " print STDERR '', " . _emit( $n->{param} );
397 }
398 return " print '', $s";
399 }
400 if ( $n->{method}{dot_bareword} eq 'say' ) {
401 my $s = _emit( $n->{self} );
402 if ( $s eq _mangle_var('$*ERR') ) {
403 return " print STDERR '', " . _emit( $n->{param} ) . ', "\n"';
404 }
405 return " print '', $s" . ', "\n"';
406 }
407 if ( $n->{method}{dot_bareword} eq 'perl' ) {
408 return 'Pugs::Runtime::Perl6::perl(' . _emit( $n->{self} ) . ")\n";
409 }
410 # TODO: other builtins
411 if ( $n->{method}{dot_bareword} eq 'defined' ) {
412 return '(defined ' . _emit( $n->{self} ) . ")\n";
413 }
414
415 #warn "method_call: ", Dumper( $n );
416  
417 # constructor
418 if ( exists $n->{self}{bareword} ) {
419 # Str.new;
420 return
421 " " . _emit( $n->{self} ) . "->" . _emit( $n->{method} ) .
422 "(" . _emit( $n->{param} ) . ") ";
423 }
424
425 # "autobox"
426
427 if ( exists $n->{self}{code} && $n->{method}{dot_bareword} eq 'goto') {
428 # &code.goto;
429 return
430 " \@_ = (" . _emit_parameter_capture( $n->{param} ) . ");\n" .
431 " " . _emit( $n->{method} ) . " " .
432 _emit( $n->{self} );
433 }
434 if ( exists $n->{self}{code} ) {
435 # &?ROUTINE.name;
436 return
437 _emit( $n->{self} ) . "->" .
438 _emit( $n->{method} ) . "(" . _emit( $n->{param} ) . ")"
439 }
440
441 #warn "method: ", Dumper( $n );
442 if ( exists $n->{self}{scalar} ) {
443 # $.scalar.method(@param)
444 return " " . _emit( $n->{self} ) . '->' .
445 _emit( $n->{method} ) .
446 '(' . _emit( $n->{param} ) . ')'
447 if $n->{self}{scalar} =~ /^\$\./;
448
449 # $scalar.++;
450 # runtime decision - method or lib call
451 return
452 "( Scalar::Util::blessed " . _emit( $n->{self} ) . " ? " .
453
454 _emit( $n->{self} ) . "->" .
455 _emit( $n->{method} ) . "(" . _emit( $n->{param} ) . ")" .
456
457 " : " .
458
459 " Pugs::Runtime::Perl6::Scalar::" . _emit( $n->{method}, ' ' ) .
460 "(" . _emit( $n->{self} ) .
461 ", " . _emit( $n->{param} ) . ")" .
462
463 " )";
464 }
465
466 if ( exists $n->{self}{op1} ) {
467 # %var<item>.++;
468 return
469 _emit( $n->{self} ) . "->" .
470 _emit( $n->{method} ) . "(" . _emit( $n->{param} ) . ")";
471 }
472
473 # normal methods or subs
474
475 return " " . _mangle_ident( $n->{sub}{bareword} ) .
476 '(' .
477 join ( ";\n", # XXX
478 map { _emit( $_ ) } @{$n->{param}}
479 ) .
480 ')';
481 }
482  
483 if ( exists $n->{substitution}) {
484 return 'XXXX';
485 }
486  
487 return _not_implemented( $n, "syntax" );
488 }
489  
490 sub statement {
491 my $n = $_[0];
492 #warn "statement: ", Dumper( $n );
493
494 if ( $n->{statement} eq 'if' ||
495 $n->{statement} eq 'unless' ) {
496 return " " . $n->{statement} .
497 '(' . _emit( $n->{exp1} ) . ')' .
498 " {\n" . _emit( $n->{exp2} ) . "\n }\n" .
499 " else" .
500 " {\n" . _emit( $n->{exp3} ) . "\n }";
501 }
502  
503 if ( $n->{statement} eq 'sub' ||
504 $n->{statement} eq 'submethod' ||
505 $n->{statement} eq 'method' ) {
506 #warn "sub: ",Dumper $n;
507  
508 my $name = _mangle_ident( $n->{name} );
509  
510 my $export = '';
511 for my $attr ( @{$n->{attribute}} ) {
512 if ( $attr->[0]{bareword} eq 'is' &&
513 $attr->[1]{bareword} eq 'export' ) {
514 $export = "push \@EXPORT, '$name';";
515 }
516 }
517  
518 return $export .
519 " sub " . $name .
520 " {\n" .
521 (
522 $n->{statement} =~ /method/
523 ? " my \$self = shift; " # default invocant
524 : ""
525 ) .
526 _emit_parameter_binding( $n->{signature} ) .
527 _emit( $n->{block} ) .
528 "\n }\n" .
529 "## Signature for $name\n" .
530 " Data::Bind->sub_signature\n".
531 " (\\&$name, ". _emit_parameter_signature ( $n->{signature} ) . ");\n";
532 }
533  
534 if ( $n->{statement} eq 'for' ) {
535 #warn "for: ",Dumper $n;
536 if ( exists $n->{exp2}{pointy_block} ) {
537 return " " . $n->{statement} .
538 ( $n->{exp2}{signature}
539 ? ' my ' . _emit( $n->{exp2}{signature} )
540 : ''
541 ) .
542 ' ( ' . _emit( $n->{exp1} ) . ' )' .
543 " { " . _emit( $n->{exp2}{pointy_block} ) . " }";
544 }
545 return " " . $n->{statement} .
546 ' ( ' . _emit( $n->{exp1} ) . ' )' .
547 " { " . _emit( $n->{exp2} ) . " }";
548 }
549  
550 if ( $n->{statement} eq 'rule' ||
551 $n->{statement} eq 'token' ||
552 $n->{statement} eq 'regex' ) {
553 #warn "rule: ",Dumper $n;
554  
555 my $name = _mangle_ident( $n->{name} );
556  
557 my $export = '';
558 for my $attr ( @{$n->{attribute}} ) {
559 if ( $attr->[0]{bareword} eq 'is' &&
560 $attr->[1]{bareword} eq 'export' ) {
561 $export = "push \@EXPORT, '$name';";
562 }
563 }
564  
565 my $perl5 = Pugs::Emitter::Rule::Perl5::Ratchet::emit(
566 'Pugs::Grammar::Base',
567 $n->{block},
568 {}, # options
569 );
570 $perl5 =~ s/^sub/sub $name/ if $name;
571 # TODO - _emit_parameter_binding( $n->{signature} ) .
572 return $export .
573 $perl5 .
574 "## Signature for $name\n" .
575 " Data::Bind->sub_signature\n".
576 " (\\&$name, ". _emit_parameter_signature ( $n->{signature} ) . ");\n";
577 }
578  
579 return _not_implemented( $n, "statement" );
580 }
581  
582 sub infix {
583 my $n = $_[0];
584 #print "infix: ", Dumper( $n );
585  
586 if ( $n->{op1}{op} eq '~' ) {
587 return _emit( $n->{exp1} ) . ' . ' . _emit( $n->{exp2} );
588 }
589 if ( $n->{op1}{op} eq '~=' ) {
590 return _emit( $n->{exp1} ) . ' .= ' . _emit( $n->{exp2} );
591 }
592 if ( $n->{op1}{op} eq '//' ||
593 $n->{op1}{op} eq 'err' ) {
594 return ' do { my $_tmp_ = ' . _emit( $n->{exp1} ) .
595 '; defined $_tmp_ ? $_tmp_ : ' . _emit( $n->{exp2} ) . '}';
596 }
597
598 if ( $n->{op1}{op} eq ':=' ) {
599 #warn "bind: ", Dumper( $n );
600 if ( exists $n->{exp2}{scalar} ) {
601 return " tie " . _emit( $n->{exp1} ) .
602 ", 'Pugs::Runtime::Perl6::Scalar::Alias', " .
603 "\\" . _emit( $n->{exp2} );
604 }
605 else {
606 # XXX: for now, should use data::bind
607 return _emit( $n->{exp1}).' = '._emit( $n->{exp2});
608 }
609 }
610 if ( $n->{op1}{op} eq '~~' ) {
611 if ( my $subs = $n->{exp2}{substitution} ) {
612 # XXX: use Pugs::Compiler::RegexPerl5
613 # XXX: escape
614 return _emit( $n->{exp1} ) . ' =~ s{' . $subs->{substitution}[0]. '}{'. $subs->{substitution}->[1] .'}' .
615 ( $subs->{options}{g} ? 'g' : '' )
616 if $subs->{options}{p5};
617 return _not_implemented( $n, "rule" );
618 }
619 return _emit( $n->{exp1} ) . ' =~ (ref(' . _emit( $n->{exp2} ).') eq "REGEX" ? '._emit($n->{exp2}).' : quotemeta('._emit($n->{exp2}).'))';
620 }
621  
622 if ( $n->{op1}{op} eq '=' ) {
623 # warn "{'='}: ", Dumper( $n );
624 if ( exists $n->{exp1}{scalar} ) {
625 #warn "set $n->{exp1}{scalar}";
626 return _var_set( $n->{exp1}{scalar} )->( _var_get( $n->{exp2} ) );
627 }
628 if ( exists $n->{exp1}{op1} &&
629 $n->{exp1}{op1}{op} eq 'has' ) {
630 #warn "{'='}: ", Dumper( $n );
631 # XXX - changes the AST
632 push @{ $n->{exp1}{attribute} },
633 [ { bareword => 'default' },
634 $n->{exp2}
635 ];
636 #warn "{'='}: ", Dumper( $n );
637 return _emit( $n->{exp1} );
638 }
639 return _emit( $n->{exp1} ) .
640 " = " . _var_get( $n->{exp2} );
641 }
642  
643 if ( $n->{op1}{op} eq '+=' ) {
644 #warn "{'='}: ", Dumper( $n );
645 if ( exists $n->{exp1}{scalar} ) {
646 #warn "set $n->{exp1}{scalar}";
647 return _var_set( $n->{exp1}{scalar} )->(
648 _emit(
649 {
650 fixity => 'infix',
651 op1 => { op => '+' },
652 exp1 => $n->{exp1},
653 exp2 => $n->{exp2},
654 }
655 )
656 );
657 }
658 return _emit( $n->{exp1} ) .
659 " = " . _emit( $n->{exp2} );
660 }
661  
662 if ( exists $n->{exp2}{bare_block} ) {
663 # $a = { 42 }
664 return " " . _emit( $n->{exp1} ) . ' ' .
665 $n->{op1}{op} . ' ' . "sub " . _emit( $n->{exp2} );
666 }
667  
668 return _emit( $n->{exp1} ) . ' ' .
669 $n->{op1}{op} . ' ' . _emit( $n->{exp2} );
670 }
671  
672 sub circumfix {
673 my $n = $_[0];
674 # print "infix: ", Dumper( $n );
675
676 if ( $n->{op1}{op} eq '(' &&
677 $n->{op2}{op} eq ')' ) {
678 return '()'
679 unless defined $n->{exp1};
680 return '(' . _emit( $n->{exp1} ) . ')';
681 }
682
683 return _not_implemented( $n, "circumfix" );
684 }
685  
686 sub postcircumfix {
687 my $n = $_[0];
688 #warn "postcircumfix: ", Dumper( $n );
689
690 if ( $n->{op1}{op} eq '(' &&
691 $n->{op2}{op} eq ')' ) {
692 # warn "postcircumfix:<( )> ", Dumper( $n );
693 # $.scalar(@param)
694 return " " . _emit( $n->{exp1} ) .
695 '->(' . _emit( $n->{exp2} ) . ')'
696 if exists $n->{exp1}{scalar} &&
697 $n->{exp1}{scalar} =~ /^\$\./;
698 }
699
700 if ( $n->{op1}{op} eq '[' &&
701 $n->{op2}{op} eq ']' ) {
702  
703 if ( ! exists $n->{exp2} ) {
704 # $array[]
705 return '@{ ' . _emit( $n->{exp1} ) . ' }';
706 }
707
708 # avoid p5 warning - "@a[1] better written as $a[1]"
709 if ( ( exists $n->{exp2}{int}
710 || exists $n->{exp2}{scalar}
711 )
712 && exists $n->{exp1}{array} ) {
713 my $name = _emit( $n->{exp1} );
714 $name =~ s/^\@/\$/;
715 return $name . '[' . _emit( $n->{exp2} ) . ']';
716 }
717
718 return _emit( $n->{exp1} ) . '[' . _emit( $n->{exp2} ) . ']';
719 }
720
721 return _not_implemented( $n, "postcircumfix" );
722 }
723  
724 sub prefix {
725 my $n = $_[0];
726 # print "prefix: ", Dumper( $n );
727
728 if ( $n->{op1}{op} eq ':' ) {
729 return _emit( $n->{exp1} ) . " # XXX :\$var not implemented\n";
730 }
731
732 if ( $n->{op1}{op} eq 'my' ||
733 $n->{op1}{op} eq 'our' ) {
734 #die "not implemented 'attribute'",Dumper $n
735 # if @{$n->{attribute}};
736 return $n->{op1}{op} . ' ' . _emit( $n->{exp1} );
737 }
738  
739 if ( $n->{op1}{op} eq 'has' ) {
740 # Moose: has 'xxx';
741 # has $x;
742 #warn "has: ",Dumper $n;
743
744 my $name = _emit( $n->{exp1} );
745 #my $name = _emit( $n->{exp1} );
746 $name =~ s/^\$//; # remove sigil
747
748 my $raw_name;
749 $raw_name = $n->{exp1}{scalar} if exists $n->{exp1}{scalar};
750 $env{$raw_name}{set} = sub {
751 "\$self->" . substr($raw_name,2) . "(" . $_[0] . ")"
752 };
753 # is rw?
754 #warn Dumper @{$n->{attribute}};
755 my $is_rw = grep { $_->[0]{bareword} eq 'is' &&
756 $_->[1]{bareword} eq 'rw' } @{$n->{attribute}};
757 $env{$raw_name}{set} = sub {
758 "\$self->{'" . substr($raw_name,2) . "'} = " . $_[0]
759 }
760 if $is_rw;
761
762 my $attr = join( ', ',
763 map {
764 join( ' => ', map { "'" . _emit($_) . "'" } @$_ )
765 } @{$n->{attribute}}
766 );
767  
768 return $n->{op1}{op} . " '" . substr($raw_name,2) . "' => ( $attr )";
769 }
770  
771 if ( $n->{op1}{op} eq 'try' ) {
772 #warn "try: ", Dumper( $n );
773 #if ( exists $n->{trait} ) {
774 # # CATCH/CONTROL
775 # return $n->{trait} . " {\n" . _emit( $n->{bare_block} ) . "\n }";
776 #}
777 return 'eval ' . _emit( $n->{exp1} ) . "; " .
778 _mangle_var( '$!' ) . " = \$@;";
779 }
780 if ( $n->{op1}{op} eq 'eval' ) {
781 return
782 'do { ' .
783 'use Pugs::Compiler::Perl6; ' . # XXX - load at start
784 'local $@; ' .
785 # call Perl::Tidy here? - see v6.pm ???
786 'my $p6 = Pugs::Compiler::Perl6->compile( ' . _emit( $n->{exp1} ) . ' ); ' .
787 'my @result = eval $p6->{perl5}; ' . # XXX - test want()
788 _mangle_var( '$!' ) . ' = $@; ' .
789 '@result }'; # /do
790 }
791 if ( $n->{op1}{op} eq '~' ) {
792 return ' "" . ' . _emit( $n->{exp1} );
793 }
794 if ( $n->{op1}{op} eq '!' ) {
795 return _emit( $n->{exp1} ) . ' ? 0 : 1 ';
796 }
797 if ( $n->{op1}{op} eq '++' ||
798 $n->{op1}{op} eq '--' ||
799 $n->{op1}{op} eq '+' ) {
800 return $n->{op1}{op} . _emit( $n->{exp1} );
801 }
802
803 return _not_implemented( $n, "prefix" );
804 }
805  
806 sub postfix {
807 my $n = $_[0];
808 # print "postfix: ", Dumper( $n );
809  
810 if ( $n->{op1}{op} eq '++' ||
811 $n->{op1}{op} eq '--' ) {
812 return _emit( $n->{exp1} ) . $n->{op1}{op};
813 }
814  
815 if ( $n->{op1}{op} eq 'ANGLE' ) {
816 my $name = _emit( $n->{exp1} );
817 $name =~ s/^\%/\$/;
818 return $name . '{ \'' . $n->{op1}{angle_quoted} . '\' }';
819 }
820  
821 return _not_implemented( $n, "postfix" );
822 }
823  
824 sub ternary {
825 my $n = $_[0];
826 # print "ternary: ", Dumper( $n );
827  
828 if ( $n->{op1}{op} eq '??' ||
829 $n->{op2}{op} eq '!!' ) {
830 return _emit( $n->{exp1} ) .
831 ' ? ' . _emit( $n->{exp2} ) .
832 ' : ' . _emit( $n->{exp3} ) ;
833 }
834  
835 return _not_implemented( $n, "ternary" );
836 }
837 1;