4 # ast type info is generally still implicit
5 # the combined madness calls are actually losing type information
6 # brace madprops tend to be too low in the tree
7 # could use about 18 more refactorings...
8 # lots of unused cruft left around from previous refactorings
23 my $filename = $options{'input'} or die;
24 $deinterpolate = $options{'deinterpolate'};
25 my $YAML = $options{'YAML'};
27 local $SIG{__DIE__} = sub {
29 $e =~ s/\n$/\n [NODE $filename line $::prevstate->{line}]/ if $::prevstate;
35 my $p1 = XML::Parser->new(Style => 'Objects', Pkg => 'PLXML');
36 $p1->setHandlers('Char' => sub { warn "Chars $_[1]" if $_[1] =~ /\S/; });
38 # First slurp XML into tree of objects.
40 my $root = $p1->parsefile($filename);
42 # Now turn XML tree into something more like an AST.
44 PLXML::prepreproc($root->[0]);
45 my $ast = P5AST->new('Kids' => [$root->[0]->ast()]);
50 return YAML::Syck::Dump($ast);
53 # Finally, walk AST to produce new program.
55 my $text = $ast->p5text(); # returns encoded, must output raw
61 $::curenc = 1; # start in iso-8859-1, sigh...
63 $::H = "HeredocHere000";
82 'o' => 'p5::operator',
83 '~' => 'p5::operator',
98 'a' => 'p5::operator',
99 'A' => 'p5::operator',
100 'd' => 'p5::declarator',
105 'q' => 'p5::openquote',
106 'Q' => 'p5::closequote',
110 's' => 'p5::declarator',
111 # 'V' => 'p5::version',
116 $Data::Dumper::Indent = 1;
117 $Data::Dumper::Quotekeys = 0;
120 my $text = Dumper(@_);
121 # doesn't scale well, alas
122 1 while $text =~ s/(.*)^([^\n]*)bless\( \{\n(.*?)^(\s*\}), '([^']*)' \)([^\n]*)/$1$2$5 {\n$3$4$6 # $5/ms;
123 $text =~ s/PLXML:://g;
125 my ($package, $filename, $line) = caller;
126 my $subroutine = (caller(1))[3];
127 $text =~ s/\n?\z/, called from $subroutine, line $line\n/;
147 warn xdolist($indent,"LIST",@_);
150 my $type = ref $_[0];
152 warn xdoitem($indent,$type,@_);
155 warn xdoitem($indent,"ITEM",@_);
163 my $in = ' ' x ($indent * 2);
165 $result .= "$in<$tag>\n" if defined $tag;
167 my $itt = ref $it || "ITEM";
169 $result .= xdoitem($indent+1,$itt,$it);
171 $result .= "$in</$tag>\n" if defined $tag;
179 my $in = ' ' x ($indent * 2);
180 my $result = "$in<$tag>\n";
181 my @keys = sort keys %$hash;
184 $longest = length($k) if length($k) > $longest;
189 $K = $$hash{$k}, next if $k eq 'Kids';
190 my $sp = ' ' x ($longest - length($k));
191 if (ref $$hash{$k}) {
192 $tmp = xdoitem($indent+1,"kv",$$hash{$k});
193 $tmp =~ s!^ *<kv>\n *</kv>!$in <kv/>!;
196 $tmp = xdoitem($indent+1,"kv",$$hash{$k});
198 $k =~ s/([\t\n'"<>&])/$xmlrepl{$1}/g;
199 $tmp =~ s/<kv/<kv k='$k'$sp/ or
200 $tmp =~ s/^(.*)$/$in <kv k='$k'>\n$in $1$in <\/kv>\n/s;
204 $result .= xdolist($indent, undef, @$K);
206 $result .= "$in</$tag>\n";
213 my $in = ' ' x ($indent * 2);
216 $item =~ s/([\t\n'"<>&])/$xmlrepl{$1}/g;
217 return "$in<$tag>$item</$tag>\n";
219 (my $newtag = $r) =~ s/::/:/g;
221 if ($t =~ /\bARRAY\b/) {
223 return xdolist($indent,$tag,@{$item});
226 return "$in<$tag />\n";
229 if ($t =~ /\bHASH\b/) {
230 return xdohash($indent,$tag,$item);
233 return "$in<$newtag>$$item</$newtag>\n";
236 return "$in<$newtag type='$r'/>\n";
250 tdolist($indent,"LIST",@_);
253 my $type = ref $_[0];
255 tdoitem($indent,$type,@_);
258 tdoitem($indent,"ITEM",@_);
266 my $tag = shift || "ARRAY";
267 my $in = ' ' x ($indent * 2);
271 my $itt = ref $it || "ITEM";
272 print STDERR $in," ";
273 tdoitem($indent+1,$itt,$it);
287 my $in = ' ' x ($indent * 2);
289 print STDERR "$tag => {\n";
291 my @keys = sort keys %$hash;
294 $longest = length($k) if length($k) > $longest;
298 my $sp = ' ' x ($longest - length($k));
299 print STDERR "$in $k$sp => ";
300 tdoitem($indent+1,"",$$hash{$k});
302 print STDERR " # Kids";
306 print STDERR "$in} # $tag";
313 if (not defined $item) {
314 print STDERR "UNDEF";
317 # my $in = ' ' x ($indent * 2);
320 $item =~ s/([\t\n"])/$trepl{$1}/g;
321 print STDERR "\"$item\"";
326 my $str = $$item{uni};
327 my $enc = $enc[$$item{enc}] . ' ';
328 $enc =~ s/iso-8859-1 //;
329 $str =~ s/([\t\n"])/$trepl{$1}/g;
330 print STDERR "$r $enc\"$str\"";
332 elsif ($t =~ /\bARRAY\b/) {
333 tdolist($indent,$tag,@{$item});
335 elsif ($t =~ /\bHASH\b/) {
336 tdohash($indent,$tag,$item);
339 print STDERR "$r type='$r'";
346 if (not exists $enc{$encname}) {
348 return $enc{$encname} = $#enc;
350 return $enc{$encname};
362 die "Too many args to new" if @_;
363 die "Attempt to bless non-text $text" if ref $text;
364 return bless( { uni => $text,
369 sub uni { my $self = shift; $$self{uni}; } # internal stuff all in utf8
373 my $enc = $enc[$$self{enc} || 0];
374 return encode($enc, $$self{uni});
377 package p5::closequote; BEGIN { @p5::closequote::ISA = 'p5::punct'; }
378 package p5::closer; BEGIN { @p5::closer::ISA = 'p5::punct'; }
379 package p5::declarator; BEGIN { @p5::declarator::ISA = 'p5::token'; }
380 package p5::junk; BEGIN { @p5::junk::ISA = 'p5::text'; }
381 package p5::label; BEGIN { @p5::label::ISA = 'p5::token'; }
382 #package p5::name; BEGIN { @p5::name::ISA = 'p5::token'; }
383 package p5::opener; BEGIN { @p5::opener::ISA = 'p5::punct'; }
384 package p5::openquote; BEGIN { @p5::openquote::ISA = 'p5::punct'; }
385 package p5::operator; BEGIN { @p5::operator::ISA = 'p5::token'; }
386 package p5::punct; BEGIN { @p5::punct::ISA = 'p5::token'; }
387 package p5::remod; BEGIN { @p5::remod::ISA = 'p5::token'; }
388 package p5::sigil; BEGIN { @p5::sigil::ISA = 'p5::punct'; }
389 package p5::token; BEGIN { @p5::token::ISA = 'p5::text'; }
390 #package p5::version; BEGIN { @p5::version::ISA = 'p5::token'; }
392 ################################################################
393 # Routines to turn XML tree into an AST. Mostly this amounts to hoisting
394 # misplaced nodes and flattening various things into lists.
399 ::x("AUTOLOAD $PLXML::AUTOLOAD", @_);
400 return "[[[ $PLXML::AUTOLOAD ]]]";
405 my $kids = $$self{Kids};
409 for ($i = 0; $i < @$kids; $i++) {
410 if (ref $kids->[$i] eq "PLXML::madprops") {
411 $self->{mp} = splice(@$kids, $i, 1)->hash($self,@_);
416 prepreproc($kids->[$i], $self, @_);
424 if (ref $self eq 'PLXML::op_null' and $$self{was}) {
425 return "PLXML::op_$$self{was}"->key();
434 my $t = ref $self || $self;
435 $t = "PLXML::op_$$self{was}" if $t eq 'PLXML::op_null' and $$self{was};
436 $t =~ s/PLXML/P5AST/ or die "Bad type: $t";
442 my @keys = split(' ', shift);
444 for my $key (@keys) {
445 my $madprop = $self->{mp}{$key};
446 next unless defined $madprop;
447 if (ref $madprop eq 'PLXML::mad_op') {
449 push @vals, $madprop->blockast($self, @_);
452 push @vals, $madprop->ast($self, @_);
457 if ($white = $self->{mp}{"_$key"}) {
458 push @vals, p5::junk->new($white);
460 my $type = $madtype{$key} || "p5::token";
461 push @vals, $type->new($madprop);
462 if ($white = $self->{mp}{"#$key"}) {
463 push @vals, p5::junk->new($white);
478 for my $kid (@{$$self{Kids}}) {
479 push @newkids, $kid->ast($self, @_);
481 return $self->newtype->new(Kids => [uc $self->key(), "(", @newkids, ")"]);
486 my $desc = $self->desc();
487 if ($desc =~ /\((.*?)\)/) {
491 return " <<" . $self->key() . ">> ";
500 package PLXML::Characters;
502 sub ast { die "oops" }
503 sub pair { die "oops" }
505 package PLXML::madprops;
518 # We need to guarantee key uniqueness at this point.
519 for my $kid (@{$$self{Kids}}) {
520 my ($k,$v) = $kid->pair($self, @_);
522 $k .= 'x' while exists $hash{$k};
526 $hash{FIRST} = $firstthing;
527 $hash{LAST} = $lastthing;
531 package PLXML::mad_op;
535 my $key = $$self{key};
541 $self->prepreproc(@_);
543 for my $kid (@{$$self{Kids}}) {
544 push @vals, $kid->ast($self, @_);
550 return P5AST::op_list->new(Kids => [@vals]);
556 $self->prepreproc(@_);
558 for my $kid (@{$$self{Kids}}) {
559 push @vals, $kid->blockast($self, @_);
565 return P5AST::op_lineseq->new(Kids => [@vals]);
569 package PLXML::mad_pv;
573 my $key = $$self{key};
574 my $val = $$self{val};
575 $val =~ s/STUPIDXML\(#x(\w+)\)/chr(hex $1)/eg;
579 package PLXML::mad_sv;
583 my $key = $$self{key};
584 my $val = $$self{val};
585 $val =~ s/STUPIDXML\(#x(\w+)\)/chr(hex $1)/eg;
589 package PLXML::baseop;
596 for my $kid (@{$$self{Kids}}) {
597 push @newkids, $kid->ast($self, @_);
600 push @retval, uc $self->key(), "(", @newkids , ")";
603 push @retval, $self->madness('o ( )');
605 return $self->newtype->new(Kids => [@retval]);
608 package PLXML::baseop_unop;
612 my @newkids = $self->madness('d o (');
614 if (exists $$self{Kids}) {
615 my $arg = $$self{Kids}[0];
616 push @newkids, $arg->ast($self, @_) if defined $arg;
618 push @newkids, $self->madness(')');
620 return $self->newtype()->new(Kids => [@newkids]);
623 package PLXML::binop;
629 my $left = $$self{Kids}[0];
630 push @newkids, $left->ast($self, @_);
632 push @newkids, $self->madness('o');
634 my $right = $$self{Kids}[1];
635 if (defined $right) {
636 push @newkids, $right->ast($self, @_);
639 return $self->newtype->new(Kids => [@newkids]);
644 package PLXML::filestatop;
649 my @newkids = $self->madness('o (');
651 if (@{$$self{Kids}}) {
652 for my $kid (@{$$self{Kids}}) {
653 push @newkids, $kid->ast($self, @_);
657 push @newkids, $self->madness('O');
659 push @newkids, $self->madness(')');
661 return $self->newtype->new(Kids => [@newkids]);
664 package PLXML::listop;
671 if (@retval = $self->madness('X')) {
672 my @before, $self->madness('o x');
673 return P5AST::listop->new(Kids => [@before,@retval]);
676 push @retval, $self->madness('o ( [ {');
679 for my $kid (@{$$self{Kids}}) {
680 next if ref $kid eq 'PLXML::op_pushmark';
681 next if ref $kid eq 'PLXML::op_null' and
682 defined $$kid{was} and $$kid{was} eq 'pushmark';
683 push @newkids, $kid->ast($self, @_);
689 push @retval, $self->madness('S');
691 push @retval, @newkids;
693 push @retval, $self->madness('} ] )');
694 return $self->newtype->new(Kids => [@retval,@after]);
697 package PLXML::logop;
703 push @newkids, $self->madness('o (');
704 for my $kid (@{$$self{Kids}}) {
705 push @newkids, $kid->ast($self, @_);
707 push @newkids, $self->madness(')');
708 return $self->newtype->new(Kids => [@newkids]);
713 package PLXML::loopexop;
717 my @newkids = $self->madness('o (');
719 if ($$self{mp}{L} or not $$self{flags} =~ /\bSPECIAL\b/) {
720 my @label = $self->madness('L');
722 push @newkids, @label;
725 my $arg = $$self{Kids}[0];
726 push @newkids, $arg->ast($self, @_) if defined $arg;
729 push @newkids, $self->madness(')');
731 return $self->newtype->new(Kids => [@newkids]);
735 package PLXML::padop;
737 package PLXML::padop_svop;
744 return P5AST::pmop->new(Kids => []) unless exists $$self{flags};
746 my $bits = $self->fetchbits($$self{flags},@_);
749 if ($bits->{binding}) {
750 push @newkids, $bits->{binding};
751 push @newkids, $self->madness('~');
753 if (exists $bits->{regcomp} and $bits->{regcomp}) {
754 my @front = $self->madness('q');
755 my @back = $self->madness('Q');
756 push @newkids, @front, $bits->{regcomp}, @back,
759 elsif ($$self{mp}{q}) {
760 push @newkids, $self->madness('q = Q m');
762 elsif ($$self{mp}{X}) {
763 push @newkids, $self->madness('X m');
766 push @newkids, $self->madness('e m');
769 return $self->newtype->new(Kids => [@newkids]);
775 for my $key (grep {!/^Kids/} keys %$pmop) {
776 $bits->{$key} = $pmop->{$key};
779 # Have to delete all the fake evals of the repl. This is a pain...
780 if (@{$$pmop{Kids}}) {
781 my $really = $$pmop{Kids}[0]{Kids}[0];
782 if (ref $really eq 'PLXML::op_substcont') {
783 $really = $$really{Kids}[0];
785 while ((ref $really) =~ /^PLXML::op_.*(null|entereval)/) {
786 if (exists $$really{was}) {
787 $bits->{repl} = $really->ast(@_);
790 $really = $$really{Kids}[0];
792 if (ref $really eq 'PLXML::op_scope' and
793 @{$$really{Kids}} == 1 and
794 ref $$really{Kids}[0] eq 'PLXML::op_null' and
795 not @{$$really{Kids}[0]{Kids}})
800 if (ref $really eq 'PLXML::op_leave' and
801 @{$$really{Kids}} == 2 and
802 ref $$really{Kids}[1] eq 'PLXML::op_null' and
803 not @{$$really{Kids}[1]{Kids}})
808 if ((ref $really) =~ /^PLXML::op_(scope|leave)/) {
809 # should be at inner do {...} here, so skip that fakery too
810 $bits->{repl} = $really->newtype->new(Kids => [$really->PLXML::op_lineseq::lineseq(@_)]);
811 # but retrieve the whitespace before fake '}'
812 if ($$really{mp}{'_}'}) {
813 push(@{$bits->{repl}->{Kids}}, p5::junk->new($$really{mp}{'_}'}));
816 else { # something else, padsv probably
817 $bits->{repl} = $really->ast(@_);
824 my $flags = shift || '';
826 my @kids = @{$$self{Kids}};
829 my $arg = shift @kids;
830 innerpmop($arg,\%bits, $self, @_);
831 if ($flags =~ /STACKED/) {
833 $bits{binding} = $arg->ast($self, @_);
835 if ($bits{when} ne "COMP" and @kids) {
837 $bits{regcomp} = $arg->ast($self, @_);
839 if (not exists $bits{repl} and @kids) {
841 $bits{repl} = $arg->ast($self, @_);
847 package PLXML::pvop_svop;
853 my @newkids = $self->madness('o (');
855 if (exists $$self{Kids}) {
856 my $arg = $$self{Kids}[0];
857 push @newkids, $arg->ast($self, @_) if defined $arg;
859 push @newkids, $self->madness(')');
861 return $self->newtype->new(Kids => [@newkids]);
865 package PLXML::Characters;
866 package PLXML::madprops;
867 package PLXML::mad_op;
868 package PLXML::mad_pv;
869 package PLXML::baseop;
870 package PLXML::baseop_unop;
871 package PLXML::binop;
873 package PLXML::filestatop;
874 package PLXML::listop;
875 package PLXML::logop;
877 package PLXML::loopexop;
878 package PLXML::padop;
879 package PLXML::padop_svop;
881 package PLXML::pvop_svop;
883 package PLXML::op_null;
885 # Null nodes typed by first madprop.
891 'p' => sub { # peg for #! line, etc.
894 push @newkids, $self->madness('p px');
896 return P5AST::peg->new(Kids => [@newkids])
898 '(' => sub { # extra parens around the whole thing
901 push @newkids, $self->madness('dx d o (');
902 for my $kid (@{$$self{Kids}}) {
903 push @newkids, $kid->ast($self, @_);
905 push @newkids, $self->madness(')');
906 return P5AST::parens->new(Kids => [@newkids])
908 '~' => sub { # binding operator
911 push @newkids, $$self{Kids}[0]->ast($self,@_);
912 push @newkids, $self->madness('~');
913 push @newkids, $$self{Kids}[1]->ast($self,@_);
914 return P5AST::bindop->new(Kids => [@newkids])
916 ';' => sub { # null statements/blocks
919 push @newkids, $self->madness('{ ; }');
921 return P5AST::nothing->new(Kids => [@newkids])
923 'I' => sub { # if or unless statement keyword
926 push @newkids, $self->madness('L I (');
928 for my $kid (@{$$self{Kids}}) {
929 push @subkids, $kid->ast($self, @_);
931 die "oops in op_null->new" unless @subkids == 1;
932 my $newself = $subkids[0];
933 @subkids = @{$$newself{Kids}};
935 unshift @{$subkids[0]{Kids}}, @newkids;
936 push @{$subkids[0]{Kids}}, $self->madness(')');
937 return bless($newself, 'P5AST::condstate');
942 my @module = $self->madness('U');
943 my @args = $self->madness('A');
944 my $module = $module[-1]{Kids}[-1];
945 if ($module->uni eq 'bytes') {
946 $::curenc = Nomad::encnum('iso-8859-1');
948 elsif ($module->uni eq 'utf8') {
949 if ($$self{mp}{o} eq 'no') {
950 $::curenc = Nomad::encnum('iso-8859-1');
953 $::curenc = Nomad::encnum('utf-8');
956 elsif ($module->uni eq 'encoding') {
957 if ($$self{mp}{o} eq 'no') {
958 $::curenc = Nomad::encnum('iso-8859-1');
961 $::curenc = Nomad::encnum(eval $args[0]->p5text); # XXX bletch
964 # (Surrounding {} ends up here if use is only thing in block.)
965 push @newkids, $self->madness('{ o');
966 push @newkids, @module;
967 push @newkids, $self->madness('V');
968 push @newkids, @args;
969 push @newkids, $self->madness('S ; }');
971 return P5AST::use->new(Kids => [@newkids])
973 '?' => sub { # ternary
977 my @condkids = @{$$self{Kids}[0]{Kids}};
979 push @newkids, $condkids[0]->ast($self,@_), $self->madness('?');
980 push @newkids, $condkids[1]->ast($self,@_), $self->madness(':');
981 push @newkids, $condkids[2]->ast($self,@_);
982 return P5AST::ternary->new(Kids => [@newkids])
984 '&' => sub { # subroutine
987 push @newkids, $self->madness('d n s a : { & } ;');
989 return P5AST::sub->new(Kids => [@newkids])
991 'i' => sub { # modifier if
994 push @newkids, $self->madness('i');
995 my $cond = $$self{Kids}[0];
997 for my $kid (@{$$cond{Kids}}) {
998 push @subkids, $kid->ast($self, @_);
1000 push @newkids, shift @subkids;
1001 unshift @newkids, @subkids;
1002 return P5AST::condmod->new(Kids => [@newkids])
1004 'P' => sub { # package declaration
1007 push @newkids, $self->madness('o');
1008 push @newkids, $self->madness('P');
1009 push @newkids, $self->madness(';');
1011 return P5AST::package->new(Kids => [@newkids])
1013 'F' => sub { # format
1015 my @newkids = $self->madness('F n b');
1017 return P5AST::format->new(Kids => [@newkids])
1019 'x' => sub { # qw literal
1021 return P5AST::qwliteral->new(Kids => [$self->madness('x')])
1023 'q' => sub { # random quote
1025 return P5AST::quote->new(Kids => [$self->madness('q = Q')])
1027 'X' => sub { # random literal
1029 return P5AST::token->new(Kids => [$self->madness('X')])
1031 ':' => sub { # attr list
1033 return P5AST::attrlist->new(Kids => [$self->madness(':')])
1035 ',' => sub { # "unary ," so to speak
1038 push @newkids, $self->madness(',');
1039 push @newkids, $$self{Kids}[0]->ast($self,@_);
1040 return P5AST::listelem->new(Kids => [@newkids])
1042 'C' => sub { # constant conditional
1045 push @newkids, $$self{Kids}[0]->ast($self,@_);
1046 my @folded = $self->madness('C');
1048 my @t = $self->madness('t');
1049 my @e = $self->madness('e');
1051 return P5AST::op_cond_expr->new(
1053 $self->madness('I ('),
1055 $self->madness(') ?'),
1056 P5AST::op_cond_expr->new(Kids => [@newkids]),
1057 $self->madness(':'),
1062 return P5AST::op_cond_expr->new(
1064 $self->madness('I ('),
1066 $self->madness(') ?'),
1068 $self->madness(':'),
1073 return P5AST::op_null->new(Kids => [@newkids])
1075 '+' => sub { # unary +
1078 push @newkids, $self->madness('+');
1079 push @newkids, $$self{Kids}[0]->ast($self,@_);
1080 return P5AST::preplus->new(Kids => [@newkids])
1082 'D' => sub { # do block
1085 push @newkids, $self->madness('D');
1086 push @newkids, $$self{Kids}[0]->ast($self,@_);
1087 return P5AST::doblock->new(Kids => [@newkids])
1089 '3' => sub { # C-style for loop
1094 my (undef, $init, $lineseq) = @{$$self{Kids}[0]{Kids}};
1095 my (undef, $leaveloop) = @{$$lineseq{Kids}};
1096 my (undef, $null) = @{$$leaveloop{Kids}};
1102 if (exists $$null{was} and $$null{was} eq 'and') {
1103 ($lineseq2) = @{$$null{Kids}};
1106 ($and) = @{$$null{Kids}};
1107 ($cond, $lineseq2) = @{$$and{Kids}};
1109 if ($$lineseq2{mp}{'{'}) {
1113 ($block, $cont) = @{$$lineseq2{Kids}};
1116 push @newkids, $self->madness('L 3 (');
1117 push @newkids, $init->ast($self,@_);
1118 push @newkids, $self->madness('1');
1119 if (defined $cond) {
1120 push @newkids, $cond->ast($self,@_);
1122 elsif (defined $null) {
1123 push @newkids, $null->madness('1');
1125 push @newkids, $self->madness('2');
1126 if (defined $cont) {
1127 push @newkids, $cont->ast($self,@_);
1129 push @newkids, $self->madness(')');
1130 push @newkids, $block->blockast($self,@_);
1132 return P5AST::cfor->new(Kids => [@newkids])
1134 'o' => sub { # random useless operator
1137 push @newkids, $self->madness('o');
1138 my $kind = $newkids[-1] || '';
1139 $kind = $kind->uni if ref $kind;
1141 for my $kid (@{$$self{Kids}}) {
1142 push @subkids, $kid->ast($self, @_);
1144 if ($kind eq '=') { # stealth readline
1145 unshift(@newkids, shift(@subkids));
1146 push(@newkids, @subkids);
1147 return P5AST::op_aassign->new(Kids => [@newkids])
1150 my $newself = $subkids[0];
1151 splice(@{$newself->{Kids}}, 1, 0,
1152 $self->madness('ox ('),
1162 # Null nodes are an untyped mess inside Perl. Instead of fixing it there,
1163 # we derive an effective type either from the "was" field or the first madprop.
1164 # (The individual routines select the actual new type.)
1168 my $was = $$self{was} || 'peg';
1169 my $mad = $$self{mp}{FIRST} || "unknown";
1171 # First try for a "was".
1172 my $meth = "PLXML::op_${was}::astnull";
1173 if (exists &{$meth}) {
1174 return $self->$meth(@_);
1177 # Look at first madprop.
1178 if (exists $astmad{$mad}) {
1179 return $astmad{$mad}->($self);
1181 warn "No mad $mad" unless $mad eq 'unknown';
1183 # Do something generic.
1185 for my $kid (@{$$self{Kids}}) {
1186 push @newkids, $kid->ast($self, @_);
1188 return $self->newtype->new(Kids => [@newkids]);
1194 local $::curenc = $::curenc;
1195 return $self->madness('{ ; }');
1198 package PLXML::op_stub;
1202 return $self->newtype->new(Kids => [$self->madness(', x ( ) q = Q')]);
1205 package PLXML::op_scalar;
1210 my @pre = $self->madness('o q');
1212 if ($op->uni =~ /^<</) {
1214 my $opstub = bless { start => $op }, 'P5AST::heredoc';
1215 push @newkids, $opstub;
1216 push @newkids, $self->madness('(');
1218 my @kids = @{$$self{Kids}};
1221 for my $kid (@kids) {
1222 next if ref $kid eq 'PLXML::op_pushmark';
1223 next if ref $kid eq 'PLXML::op_null' and
1224 defined $$kid{was} and $$kid{was} eq 'pushmark';
1225 push @divert, $kid->ast($self, @_);
1227 $opstub->{doc} = P5AST::op_list->new(Kids => [@divert]);
1228 $opstub->{end} = ($self->madness('Q'))[-1];
1230 push @newkids, $self->madness(')');
1232 return $self->newtype->new(Kids => [@pre,@newkids]);
1234 return $self->PLXML::baseop_unop::ast();
1237 package PLXML::op_pushmark;
1241 package PLXML::op_wantarray;
1242 package PLXML::op_const;
1247 return unless $$self{mp};
1248 push @newkids, $self->madness('q = Q X : f O ( )');
1249 return P5AST::op_const->new(Kids => [@newkids]);
1254 return unless %{$$self{mp}};
1259 my @args = $self->madness('f');
1262 elsif (exists $self->{mp}{q}) {
1263 push @args, $self->madness('d q');
1264 if ($args[-1]->uni =~ /^<</) {
1265 my $opstub = bless { start => pop(@args) }, 'P5AST::heredoc';
1266 $opstub->{doc} = P5AST::op_const->new(Kids => [$self->madness('=')]);
1267 $opstub->{end} = ($self->madness('Q'))[-1];
1268 push @args, $opstub;
1271 push @args, $self->madness('= Q');
1274 elsif (exists $self->{mp}{X}) {
1275 push @before, $self->madness('d'); # was local $[ probably
1276 if (not $$self{mp}{O}) {
1277 push @before, $self->madness('o'); # was unary
1279 my @X = $self->madness(': X');
1280 if (exists $$self{private} and $$self{private} =~ /BARE/) {
1281 return $self->newtype->new(Kids => [@X]);
1286 $self->madness('x'),
1288 if ($$self{mp}{O}) {
1289 push @args, $self->madness('o O');
1292 elsif (exists $self->{mp}{O}) {
1293 push @args, $self->madness('O');
1295 elsif ($$self{private} =~ /\bBARE\b/) {
1296 @args = ($$self{PV});
1298 elsif (exists $$self{mp}{o}) {
1299 @args = $self->madness('o');
1301 elsif (exists $$self{PV}) {
1302 @args = ('"', $$self{PV}, '"');
1304 elsif (exists $$self{NV}) {
1307 elsif (exists $$self{IV}) {
1311 @args = $self->SUPER::text(@_);
1313 return $self->newtype->new(Kids => [@before, @args]);
1317 package PLXML::op_gvsv;
1323 for my $attr (qw/gv GV flags/) {
1324 if (exists $$self{$attr}) {
1325 push @args, $attr, $$self{$attr};
1328 push @retval, @args;
1329 push @retval, $self->madness('X');
1330 return $self->newtype->new(Kids => [@retval]);
1333 package PLXML::op_gv;
1338 push @newkids, $self->madness('X K');
1340 return $self->newtype->new(Kids => [@newkids]);
1343 package PLXML::op_gelem;
1348 local $::curstate; # in case there are statements in subscript
1349 local $::curenc = $::curenc;
1351 push @newkids, $self->madness('dx d');
1352 for my $kid (@{$$self{Kids}}) {
1353 push @newkids, $kid->ast($self, @_);
1355 splice @newkids, -1, 0, $self->madness('o {');
1356 push @newkids, $self->madness('}');
1358 return $self->newtype->new(Kids => [@newkids]);
1361 package PLXML::op_padsv;
1366 push @args, $self->madness('dx d ( $ )');
1368 return $self->newtype->new(Kids => [@args]);
1371 package PLXML::op_padav;
1373 sub astnull { ast(@_) }
1378 push @retval, $self->madness('dx d (');
1379 push @retval, $self->madness('$ @');
1380 push @retval, $self->madness(') o O');
1381 return $self->newtype->new(Kids => [@retval]);
1384 package PLXML::op_padhv;
1386 sub astnull { ast(@_) }
1391 push @retval, $self->madness('dx d (');
1392 push @retval, $self->madness('$ @ %');
1393 push @retval, $self->madness(') o O');
1394 return $self->newtype->new(Kids => [@retval]);
1397 package PLXML::op_padany;
1399 package PLXML::op_pushre;
1403 if ($$self{mp}{q}) {
1404 return $self->madness('q = Q m');
1406 if ($$self{mp}{X}) {
1407 return $self->madness('X m');
1409 if ($$self{mp}{e}) {
1410 return $self->madness('e m');
1412 return $$self{Kids}[1]->ast($self,@_), $self->madness('m');
1415 package PLXML::op_rv2gv;
1421 push @newkids, $self->madness('dx d ( * $');
1422 push @newkids, $$self{Kids}[0]->ast();
1423 push @newkids, $self->madness(')');
1424 return $self->newtype->new(Kids => [@newkids]);
1427 package PLXML::op_rv2sv;
1431 return P5AST::op_rv2sv->new(Kids => [$self->madness('O o dx d ( $ ) : a')]);
1438 push @newkids, $self->madness('dx d ( $');
1439 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
1440 push @newkids, $$self{Kids}[0]->ast();
1442 push @newkids, $self->madness(') : a');
1443 return $self->newtype->new(Kids => [@newkids]);
1446 package PLXML::op_av2arylen;
1452 push @newkids, $$self{Kids}[0]->madness('l');
1453 push @newkids, $$self{Kids}[0]->ast();
1454 return $self->newtype->new(Kids => [@newkids]);
1457 package PLXML::op_rv2cv;
1462 push @newkids, $self->madness('X');
1463 return @newkids if @newkids;
1464 if (exists $$self{mp}{'&'}) {
1465 push @newkids, $self->madness('&');
1466 if (@{$$self{Kids}}) {
1467 push @newkids, $$self{Kids}[0]->ast(@_);
1471 push @newkids, $$self{Kids}[0]->ast(@_);
1473 return P5AST::op_rv2cv->new(Kids => [@newkids]);
1480 push @newkids, $self->madness('&');
1481 if (@{$$self{Kids}}) {
1482 push @newkids, $$self{Kids}[0]->ast();
1484 return $self->newtype->new(Kids => [@newkids]);
1487 package PLXML::op_anoncode;
1491 my $arg = $$self{Kids}[0];
1492 local $::curstate; # hide nested statements in sub
1493 local $::curenc = $::curenc;
1495 return $arg->ast(@_);
1497 return ';'; # XXX literal ; should come through somewhere
1500 package PLXML::op_prototype;
1501 package PLXML::op_refgen;
1505 my @newkids = $self->madness('o s a');
1507 if (exists $$self{Kids}) {
1508 my $arg = $$self{Kids}[0];
1509 push @newkids, $arg->ast($self, @_) if defined $arg;
1512 my $res = $self->newtype->new(Kids => [@newkids]);
1516 package PLXML::op_srefgen;
1521 if ($$self{mp}{FIRST} eq '{') {
1522 local $::curstate; # this is officially a block, so hide it
1523 local $::curenc = $::curenc;
1524 push @newkids, $self->madness('{');
1525 for my $kid (@{$$self{Kids}}) {
1526 push @newkids, $kid->ast($self, @_);
1528 push @newkids, $self->madness('; }');
1529 return P5AST::op_stringify->new(Kids => [@newkids]);
1532 push @newkids, $self->madness('o [');
1533 for my $kid (@{$$self{Kids}}) {
1534 push @newkids, $kid->ast($self, @_);
1536 push @newkids, $self->madness(']');
1537 return P5AST::op_stringify->new(Kids => [@newkids]);
1541 package PLXML::op_ref;
1542 package PLXML::op_bless;
1543 package PLXML::op_backtick;
1548 if (exists $self->{mp}{q}) {
1549 push @args, $self->madness('q');
1550 if ($args[-1]->uni =~ /^<</) {
1551 my $opstub = bless { start => $args[-1] }, 'P5AST::heredoc';
1552 $args[-1] = $opstub;
1553 $opstub->{doc} = P5AST::op_const->new(Kids => [$self->madness('=')]);
1554 $opstub->{end} = ($self->madness('Q'))[-1];
1557 push @args, $self->madness('= Q');
1560 return $self->newtype->new(Kids => [@args]);
1563 package PLXML::op_glob;
1567 my @retval = $self->madness('o q = Q');
1568 if (not @retval or $retval[-1]->uni eq 'glob') {
1569 push @retval, $self->madness('(');
1570 push @retval, $$self{Kids}[0]->ast($self,@_);
1571 push @retval, $self->madness(')');
1573 return P5AST::op_glob->new(Kids => [@retval]);
1576 package PLXML::op_readline;
1581 if (exists $$self{mp}{q}) {
1582 @retval = $self->madness('q = Q');
1584 elsif (exists $$self{mp}{X}) {
1585 @retval = $self->madness('X');
1587 return P5AST::op_readline->new(Kids => [@retval]);
1597 if (exists $$self{mp}{q}) {
1598 @args = $self->madness('q = Q');
1600 elsif (exists $$self{mp}{X}) {
1601 @args = $self->madness('X');
1603 elsif (exists $$self{GV}) {
1606 elsif (@{$$self{Kids}}) {
1607 @args = $self->PLXML::unop::ast(@_);
1610 @args = $self->SUPER::text(@_);
1612 return $self->newtype->new(Kids => [@retval,@args]);
1616 package PLXML::op_rcatline;
1617 package PLXML::op_regcmaybe;
1618 package PLXML::op_regcreset;
1619 package PLXML::op_regcomp;
1623 $self->PLXML::unop::ast(@_);
1626 package PLXML::op_match;
1630 my $retval = $self->SUPER::ast(@_);
1632 if (not $p5re = $retval->p5text()) {
1633 $retval = $self->newtype->new(Kids => [$self->madness('X q = Q m')]);
1634 $p5re = $retval->p5text();
1636 if ($deinterpolate) {
1637 $retval->{P5re} = P5re::qrparse($p5re);
1642 package PLXML::op_qr;
1647 if (exists $$self{flags}) {
1648 $retval = $self->SUPER::ast(@_);
1651 $retval = $self->newtype->new(Kids => [$self->madness('X q = Q m')]);
1653 if ($deinterpolate) {
1654 my $p5re = $retval->p5text();
1655 $retval->{P5re} = P5re::qrparse($p5re);
1660 package PLXML::op_subst;
1665 my $bits = $self->fetchbits($$self{flags},@_);
1668 if ($bits->{binding}) {
1669 push @newkids, $bits->{binding};
1670 push @newkids, $self->madness('~');
1672 my $X = p5::token->new($$self{mp}{X});
1673 my @lfirst = $self->madness('q');
1674 my @llast = $self->madness('Q');
1677 $self->madness('E'), # XXX s/b e probably
1679 my @rfirst = $self->madness('z');
1680 my @rlast = $self->madness('Z');
1681 my @mods = $self->madness('m');
1682 if ($rfirst[-1]->uni ne $llast[-1]->uni) {
1683 push @newkids, @rfirst;
1686 push @newkids, $bits->{repl}, @rlast, @mods;
1688 my $retval = $self->newtype->new(Kids => [@newkids]);
1689 if ($deinterpolate) {
1690 my $p5re = $retval->p5text();
1691 $retval->{P5re} = P5re::qrparse($p5re);
1696 package PLXML::op_substcont;
1697 package PLXML::op_trans;
1702 # my $bits = $self->fetchbits($$self{flags},@_);
1705 my @lfirst = $self->madness('q');
1706 my @llast = $self->madness('Q');
1709 $self->madness('E'),
1711 my @rfirst = $self->madness('z');
1712 my @repl = $self->madness('R');
1713 my @rlast = $self->madness('Z');
1714 my @mods = $self->madness('m');
1715 if ($rfirst[-1]->uni ne $llast[-1]->uni) {
1716 push @newkids, @rfirst;
1719 push @newkids, @repl, @rlast, @mods;
1721 my $res = $self->newtype->new(Kids => [@newkids]);
1725 package PLXML::op_sassign;
1731 my $right = $$self{Kids}[1];
1732 eval { push @newkids, $right->ast($self, @_); };
1734 push @newkids, $self->madness('o');
1736 my $left = $$self{Kids}[0];
1737 push @newkids, $left->ast($self, @_);
1739 return $self->newtype->new(Kids => [@newkids]);
1742 package PLXML::op_aassign;
1744 sub astnull { ast(@_) }
1750 my $right = $$self{Kids}[1];
1751 push @newkids, $right->ast($self, @_);
1753 push @newkids, $self->madness('o');
1755 my $left = $$self{Kids}[0];
1756 push @newkids, $left->ast($self, @_);
1758 return $self->newtype->new(Kids => [@newkids]);
1761 package PLXML::op_chop;
1762 package PLXML::op_schop;
1763 package PLXML::op_chomp;
1764 package PLXML::op_schomp;
1765 package PLXML::op_defined;
1766 package PLXML::op_undef;
1767 package PLXML::op_study;
1768 package PLXML::op_pos;
1769 package PLXML::op_preinc;
1773 if ($$self{targ}) { # stealth post inc or dec
1774 return $self->PLXML::op_postinc::ast(@_);
1776 return $self->SUPER::ast(@_);
1779 package PLXML::op_i_preinc;
1781 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1783 package PLXML::op_predec;
1785 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1787 package PLXML::op_i_predec;
1789 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1791 package PLXML::op_postinc;
1797 if (exists $$self{Kids}) {
1798 my $arg = $$self{Kids}[0];
1799 push @newkids, $arg->ast($self, @_) if defined $arg;
1801 push @newkids, $self->madness('o');
1803 my $res = $self->newtype->new(Kids => [@newkids]);
1807 package PLXML::op_i_postinc;
1809 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1811 package PLXML::op_postdec;
1813 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1815 package PLXML::op_i_postdec;
1817 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1819 package PLXML::op_pow;
1820 package PLXML::op_multiply;
1821 package PLXML::op_i_multiply;
1822 package PLXML::op_divide;
1823 package PLXML::op_i_divide;
1824 package PLXML::op_modulo;
1825 package PLXML::op_i_modulo;
1826 package PLXML::op_repeat;
1830 return $self->SUPER::ast(@_)
1831 unless exists $$self{private} and $$self{private} =~ /DOLIST/;
1833 my $newself = $$self{Kids}[0]->ast($self,@_);
1834 splice @{$newself->{Kids}}, -1, 0, $self->madness('o');
1836 return bless $newself, $self->newtype; # rebless the op_null
1839 package PLXML::op_add;
1840 package PLXML::op_i_add;
1841 package PLXML::op_subtract;
1842 package PLXML::op_i_subtract;
1843 package PLXML::op_concat;
1850 my $left = $$self{Kids}[0];
1851 push @newkids, $left->ast($self, @_);
1853 push @newkids, $self->madness('o');
1855 my $right = $$self{Kids}[1];
1856 push @newkids, $right->ast($self, @_);
1857 return P5AST::op_concat->new(Kids => [@newkids]);
1866 my $left = $$self{Kids}[0];
1867 push @newkids, $left->ast($self, @_);
1869 push @newkids, $self->madness('o');
1871 my $right = $$self{Kids}[1];
1872 push @newkids, $right->ast($self, @_);
1874 return $self->newtype->new(Kids => [@newkids, @after]);
1877 package PLXML::op_stringify;
1886 my @front = $self->madness('q (');
1887 my @back = $self->madness(') Q');
1888 my @M = $self->madness('M');
1890 push @newkids, $M[0], $self->madness('o');
1892 push @newkids, @front;
1893 for my $kid (@{$$self{Kids}}) {
1894 push @newkids, $kid->ast($self, @_);
1896 push @newkids, @back;
1897 return P5AST::op_stringify->new(Kids => [@newkids]);
1900 package PLXML::op_left_shift;
1901 package PLXML::op_right_shift;
1902 package PLXML::op_lt;
1903 package PLXML::op_i_lt;
1904 package PLXML::op_gt;
1905 package PLXML::op_i_gt;
1906 package PLXML::op_le;
1907 package PLXML::op_i_le;
1908 package PLXML::op_ge;
1909 package PLXML::op_i_ge;
1910 package PLXML::op_eq;
1911 package PLXML::op_i_eq;
1912 package PLXML::op_ne;
1913 package PLXML::op_i_ne;
1914 package PLXML::op_ncmp;
1915 package PLXML::op_i_ncmp;
1916 package PLXML::op_slt;
1917 package PLXML::op_sgt;
1918 package PLXML::op_sle;
1919 package PLXML::op_sge;
1920 package PLXML::op_seq;
1921 package PLXML::op_sne;
1922 package PLXML::op_scmp;
1923 package PLXML::op_bit_and;
1924 package PLXML::op_bit_xor;
1925 package PLXML::op_bit_or;
1926 package PLXML::op_negate;
1927 package PLXML::op_i_negate;
1928 package PLXML::op_not;
1932 my @newkids = $self->madness('o (');
1934 if (@newkids and $newkids[-1]->uni eq '!~') {
1939 if (exists $$self{Kids}) {
1940 my $arg = $$self{Kids}[0];
1941 push @newkids, $arg->ast($self, @_) if defined $arg;
1944 splice @{$newkids[-1][0]{Kids}}, -2, 0, @swap; # XXX WAG
1946 push @newkids, $self->madness(')');
1948 my $res = $self->newtype->new(Kids => [@newkids]);
1952 package PLXML::op_complement;
1953 package PLXML::op_atan2;
1954 package PLXML::op_sin;
1955 package PLXML::op_cos;
1956 package PLXML::op_rand;
1957 package PLXML::op_srand;
1958 package PLXML::op_exp;
1959 package PLXML::op_log;
1960 package PLXML::op_sqrt;
1961 package PLXML::op_int;
1962 package PLXML::op_hex;
1963 package PLXML::op_oct;
1964 package PLXML::op_abs;
1965 package PLXML::op_length;
1966 package PLXML::op_substr;
1967 package PLXML::op_vec;
1968 package PLXML::op_index;
1969 package PLXML::op_rindex;
1970 package PLXML::op_sprintf;
1971 package PLXML::op_formline;
1972 package PLXML::op_ord;
1973 package PLXML::op_chr;
1974 package PLXML::op_crypt;
1975 package PLXML::op_ucfirst;
1979 return $self->PLXML::listop::ast(@_);
1982 package PLXML::op_lcfirst;
1986 return $self->PLXML::listop::ast(@_);
1989 package PLXML::op_uc;
1993 return $self->PLXML::listop::ast(@_);
1996 package PLXML::op_lc;
2000 return $self->PLXML::listop::ast(@_);
2003 package PLXML::op_quotemeta;
2007 return $self->PLXML::listop::ast(@_);
2010 package PLXML::op_rv2av;
2014 return P5AST::op_rv2av->new(Kids => [$self->madness('$ @')]);
2020 if (ref $$self{Kids}[0] eq 'PLXML::op_const' and $$self{mp}{'O'}) {
2021 return $self->madness('O');
2025 push @before, $self->madness('dx d (');
2028 push @newkids, $self->madness('$ @ K');
2029 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
2030 push @newkids, $$self{Kids}[0]->ast();
2033 push @after, $self->madness(') a');
2034 return $self->newtype->new(Kids => [@before, @newkids, @after]);
2037 package PLXML::op_aelemfast;
2041 return $self->madness('$');
2044 package PLXML::op_aelem;
2049 push @newkids, $self->madness('dx d');
2050 for my $kid (@{$$self{Kids}}) {
2051 push @newkids, $kid->ast($self, @_);
2053 splice @newkids, -1, 0, $self->madness('a [');
2054 push @newkids, $self->madness(']');
2055 return P5AST::op_aelem->new(Kids => [@newkids]);
2061 my @before = $self->madness('dx d');
2063 for my $kid (@{$$self{Kids}}) {
2064 push @newkids, $kid->ast(@_);
2066 splice @newkids, -1, 0, $self->madness('a [');
2067 push @newkids, $self->madness(']');
2069 return $self->newtype->new(Kids => [@before, @newkids]);
2072 package PLXML::op_aslice;
2077 push @newkids, $self->madness('[');
2078 for my $kid (@{$$self{Kids}}) {
2079 push @newkids, $kid->ast(@_);
2081 unshift @newkids, pop @newkids;
2082 unshift @newkids, $self->madness('dx d');
2083 push @newkids, $self->madness(']');
2084 return P5AST::op_aslice->new(Kids => [@newkids]);
2091 push @newkids, $self->madness('[');
2092 for my $kid (@{$$self{Kids}}) {
2093 push @newkids, $kid->ast(@_);
2095 unshift @newkids, pop @newkids;
2096 unshift @newkids, $self->madness('dx d');
2097 push @newkids, $self->madness(']');
2099 return $self->newtype->new(Kids => [@newkids]);
2102 package PLXML::op_each;
2103 package PLXML::op_values;
2104 package PLXML::op_keys;
2105 package PLXML::op_delete;
2106 package PLXML::op_exists;
2107 package PLXML::op_rv2hv;
2111 return P5AST::op_rv2hv->new(Kids => [$self->madness('$')]);
2118 push @before, $self->madness('dx d (');
2121 push @newkids, $self->madness('$ @ % K');
2122 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
2123 push @newkids, $$self{Kids}[0]->ast();
2126 push @after, $self->madness(') a');
2127 return $self->newtype->new(Kids => [@before, @newkids, @after]);
2130 package PLXML::op_helem;
2134 local $::curstate; # hash subscript potentially a lineseq
2135 local $::curenc = $::curenc;
2138 push @newkids, $self->madness('dx d');
2139 for my $kid (@{$$self{Kids}}) {
2140 push @newkids, $kid->ast($self, @_);
2142 splice @newkids, -1, 0, $self->madness('a {');
2143 push @newkids, $self->madness('}');
2144 return P5AST::op_helem->new(Kids => [@newkids]);
2149 local $::curstate; # hash subscript potentially a lineseq
2150 local $::curenc = $::curenc;
2152 my @before = $self->madness('dx d');
2154 for my $kid (@{$$self{Kids}}) {
2155 push @newkids, $kid->ast($self, @_);
2157 splice @newkids, -1, 0, $self->madness('a {');
2158 push @newkids, $self->madness('}');
2160 return $self->newtype->new(Kids => [@before, @newkids]);
2164 package PLXML::op_hslice;
2169 push @newkids, $self->madness('{');
2170 for my $kid (@{$$self{Kids}}) {
2171 push @newkids, $kid->ast(@_);
2173 unshift @newkids, pop @newkids;
2174 unshift @newkids, $self->madness('dx d');
2175 push @newkids, $self->madness('}');
2176 return P5AST::op_hslice->new(Kids => [@newkids]);
2183 push @newkids, $self->madness('{');
2184 for my $kid (@{$$self{Kids}}) {
2185 push @newkids, $kid->ast(@_);
2187 unshift @newkids, pop @newkids;
2188 unshift @newkids, $self->madness('dx d');
2189 push @newkids, $self->madness('}');
2191 return $self->newtype->new(Kids => [@newkids]);
2194 package PLXML::op_unpack;
2195 package PLXML::op_pack;
2196 package PLXML::op_split;
2197 package PLXML::op_join;
2198 package PLXML::op_list;
2205 if (@retval = $self->madness('X')) {
2206 push @before, $self->madness('x o');
2207 return @before,@retval;
2209 my @kids = @{$$self{Kids}};
2210 for my $kid (@kids) {
2211 next if ref $kid eq 'PLXML::op_pushmark';
2212 next if ref $kid eq 'PLXML::op_null' and
2213 defined $$kid{was} and $$kid{was} eq 'pushmark';
2214 push @newkids, $kid->ast($self, @_);
2218 my @newnewkids = ();
2219 push @newnewkids, $self->madness('dx d (');
2220 push @newnewkids, @newkids;
2221 push @newnewkids, $self->madness(') :');
2222 return P5AST::op_list->new(Kids => [@newnewkids]);
2230 if (@retval = $self->madness('X')) {
2231 push @before, $self->madness('o');
2232 return $self->newtype->new(Kids => [@before,@retval]);
2234 push @retval, $self->madness('dx d (');
2237 for my $kid (@{$$self{Kids}}) {
2238 push @newkids, $kid->ast($self, @_);
2241 my @newnewkids = ();
2242 push @newnewkids, @newkids;
2243 @newkids = @newnewkids;
2244 push @retval, @newkids;
2245 push @retval, $self->madness(') :');
2246 return $self->newtype->new(Kids => [@retval]);
2249 package PLXML::op_lslice;
2255 if ($$self{mp}{q}) {
2256 push @newkids, $self->madness('q = Q');
2258 elsif ($$self{mp}{x}) {
2259 push @newkids, $self->madness('x');
2262 push @newkids, $self->madness('(');
2263 my $list = $$self{Kids}[1];
2264 push @newkids, $list->ast($self, @_);
2265 push @newkids, $self->madness(')');
2268 push @newkids, $self->madness('[');
2270 my $slice = $$self{Kids}[0];
2271 push @newkids, $slice->ast($self, @_);
2272 push @newkids, $self->madness(']');
2274 return $self->newtype->new(Kids => [@newkids]);
2277 package PLXML::op_anonlist;
2278 package PLXML::op_anonhash;
2279 package PLXML::op_splice;
2280 package PLXML::op_push;
2281 package PLXML::op_pop;
2282 package PLXML::op_shift;
2283 package PLXML::op_unshift;
2284 package PLXML::op_sort;
2285 package PLXML::op_reverse;
2289 $self->PLXML::listop::ast(@_);
2292 package PLXML::op_grepstart;
2293 package PLXML::op_grepwhile;
2294 package PLXML::op_mapstart;
2295 package PLXML::op_mapwhile;
2296 package PLXML::op_range;
2300 return $self->PLXML::binop::ast(@_);
2303 package PLXML::op_flip;
2304 package PLXML::op_flop;
2305 package PLXML::op_and;
2310 my @first = $self->madness('1');
2311 my @second = $self->madness('2');
2312 my @stuff = $$self{Kids}[0]->ast();
2313 if (my @I = $self->madness('I')) {
2316 push @newkids, $self->madness('(');
2317 push @newkids, @stuff;
2318 push @newkids, $self->madness(')');
2319 push @newkids, @second;
2323 push @newkids, $self->madness('(');
2324 push @newkids, @first;
2325 push @newkids, $self->madness(')');
2326 push @newkids, @stuff;
2329 elsif (my @i = $self->madness('i')) {
2331 push @newkids, @second;
2333 push @newkids, @stuff;
2336 push @newkids, @stuff;
2338 push @newkids, @first;
2341 elsif (my @o = $self->madness('o')) {
2343 push @newkids, @stuff;
2345 push @newkids, @second;
2348 push @newkids, @first;
2350 push @newkids, @stuff;
2353 return P5AST::op_and->new(Kids => [@newkids]);
2356 package PLXML::op_or;
2361 my @first = $self->madness('1');
2362 my @second = $self->madness('2');
2363 my @i = $self->madness('i');
2364 my @stuff = $$self{Kids}[0]->ast();
2367 push @newkids, @second;
2368 push @newkids, $self->madness('i');
2369 push @newkids, @stuff;
2372 push @newkids, @stuff;
2373 push @newkids, $self->madness('o');
2374 push @newkids, @second;
2379 push @newkids, @stuff;
2380 push @newkids, $self->madness('i');
2381 push @newkids, @first;
2384 push @newkids, @first;
2385 push @newkids, $self->madness('o');
2386 push @newkids, @stuff;
2389 return "P5AST::op_$$self{was}"->new(Kids => [@newkids]);
2393 package PLXML::op_xor;
2394 package PLXML::op_cond_expr;
2395 package PLXML::op_andassign;
2396 package PLXML::op_orassign;
2397 package PLXML::op_method;
2398 package PLXML::op_entersub;
2403 if ($$self{mp}{q}) {
2404 return $self->madness('q = Q');
2406 if ($$self{mp}{X}) { # <FH> override?
2407 return $self->madness('X');
2409 if ($$self{mp}{A}) {
2410 return $self->astmethod(@_);
2412 if ($$self{mp}{a}) {
2413 return $self->astarrow(@_);
2419 my @kids = @{$$self{Kids}};
2420 if (@kids == 1 and ref $kids[0] eq 'PLXML::op_null' and $kids[0]{was} =~ /list/) {
2421 @kids = @{$kids[0]{Kids}};
2423 my $dest = pop @kids;
2424 my @dest = $dest->ast($self, @_);
2426 if (ref($dest) =~ /method/) {
2427 my $invocant = shift @kids;
2428 $invocant = shift @kids if ref($invocant) eq 'PLXML::op_pushmark';
2429 my @invocant = $invocant->ast($self, @_);
2430 push @retval, @dest;
2431 push @retval, @invocant;
2433 elsif (exists $$self{mp}{o} and $$self{mp}{o} eq 'do') {
2434 push @retval, $self->madness('o');
2435 push @retval, @dest;
2438 push @retval, $self->madness('o');
2439 push @retval, @dest;
2442 my $kid = shift(@kids);
2443 push @newkids, $kid->ast($self, @_);
2446 push @retval, $self->madness('(');
2447 push @retval, @newkids;
2448 push @retval, $self->madness(')');
2449 return $self->newtype->new(Kids => [@retval]);
2456 for my $kid (@{$$self{Kids}}) {
2457 next if ref $kid eq 'PLXML::op_pushmark';
2458 next if ref $kid eq 'PLXML::op_null' and
2459 defined $$kid{was} and $$kid{was} eq 'pushmark';
2463 if ($$self{flags} =~ /\bSTACKED\b/) {
2464 push @invocant, shift(@kids)->ast($self, @_);
2466 for my $kid (@kids) {
2467 push @newkids, $kid->ast($self, @_);
2469 my $dest = pop(@newkids);
2470 if (ref $dest eq 'PLXML::op_rv2cv' and $$self{flags} =~ /\bMOD\b/) {
2471 $dest = pop(@newkids);
2475 push @retval, @invocant;
2476 push @retval, $self->madness('A');
2477 push @retval, $dest;
2478 push @retval, $self->madness('(');
2479 push @retval, @newkids;
2480 push @retval, $self->madness(')');
2481 return $self->newtype->new(Kids => [@retval]);
2488 my @kids = @{$$self{Kids}};
2489 if (@kids == 1 and ref $kids[0] eq 'PLXML::op_null' and $kids[0]{was} =~ /list/) {
2490 @kids = @{$kids[0]{Kids}};
2493 my $kid = shift(@kids);
2494 push @newkids, $kid->ast($self, @_);
2496 my @dest = $kids[0]->ast($self, @_);
2498 push @retval, @dest;
2499 push @retval, $self->madness('a');
2500 push @retval, $self->madness('(');
2501 push @retval, @newkids;
2502 push @retval, $self->madness(')');
2503 return $self->newtype->new(Kids => [@retval]);
2506 package PLXML::op_leavesub;
2510 if (ref $$self{Kids}[0] eq "PLXML::op_null") {
2511 return $$self{Kids}[0]->ast(@_);
2513 return $$self{Kids}[0]->blockast($self, @_);
2516 package PLXML::op_leavesublv;
2521 return $$self{Kids}[0]->blockast($self, @_);
2524 package PLXML::op_caller;
2525 package PLXML::op_warn;
2526 package PLXML::op_die;
2527 package PLXML::op_reset;
2528 package PLXML::op_lineseq;
2532 my @kids = @{$$self{Kids}};
2533 local $::curstate = 0; # (probably redundant, but that's okay)
2534 local $::prevstate = 0;
2535 local $::curenc = $::curenc;
2540 my $kid = shift @kids;
2541 my $thing = $kid->ast($self, @_);
2542 next unless defined $thing;
2543 if ($::curstate ne $::prevstate) {
2545 push @newstuff, $::prevstate->madness(';');
2546 push @{$newprev->{Kids}}, @newstuff if $newprev;
2549 $::prevstate = $::curstate;
2551 push @retval, $thing;
2553 elsif ($::prevstate) {
2554 push @newstuff, $thing;
2557 push @retval, $thing;
2561 push @newstuff, $::prevstate->madness(';');
2562 push @{$newprev->{Kids}}, @newstuff if $newprev;
2574 push @retval, $self->madness('{');
2576 my @newkids = $self->PLXML::op_lineseq::lineseq(@_);
2577 push @retval, @newkids;
2579 push @retval, $self->madness('; }');
2580 return $self->newtype->new(Kids => [@retval]);
2583 package PLXML::op_nextstate;
2585 sub newtype { return "P5AST::statement" }
2590 push @newkids, $self->madness('L');
2591 $::curstate = $self;
2592 return P5AST::statement->new(Kids => [@newkids]);
2599 push @newkids, $self->madness('L');
2600 $::curstate = $self;
2601 return $self->newtype->new(Kids => [@newkids]);
2605 package PLXML::op_dbstate;
2606 package PLXML::op_unstack;
2607 package PLXML::op_enter;
2611 package PLXML::op_leave;
2620 my $mad = $$self{mp}{FIRST} || "unknown";
2626 push @tmpkids, $self->{Kids};
2627 my $anddo = $$self{Kids}[-1]{Kids}[0]{Kids};
2628 eval { push @newkids, $anddo->[1]->ast($self,@_); };
2629 push @newkids, "[[[NOANDDO]]]" if $@;
2630 push @newkids, $self->madness('w');
2631 push @newkids, $anddo->[0]->ast($self,@_);
2633 return $self->newtype->new(Kids => [@newkids]);
2637 push @retval, $self->madness('o {');
2639 my @newkids = $self->PLXML::op_lineseq::lineseq(@_);
2640 push @retval, @newkids;
2641 push @retval, $self->madness(q/; }/);
2642 my $retval = $self->newtype->new(Kids => [@retval]);
2644 if ($$self{mp}{C}) {
2647 push @before, $self->madness('I ( C )');
2648 if ($$self{mp}{t}) {
2649 push @before, $self->madness('t');
2651 elsif ($$self{mp}{e}) {
2652 push @after, $self->madness('e');
2654 return P5AST::op_cond->new(Kids => [@before, $retval, @after]);
2661 package PLXML::op_scope;
2668 push @newkids, $self->madness('o');
2670 push @newkids, $self->madness('{');
2671 push @newkids, $self->PLXML::op_lineseq::lineseq(@_);
2672 push @newkids, $self->madness('; }');
2674 my @folded = $self->madness('C');
2676 my @t = $self->madness('t');
2677 my @e = $self->madness('e');
2679 return $self->newtype->new(
2681 $self->madness('I ('),
2683 $self->madness(')'),
2684 $self->newtype->new(Kids => [@newkids]),
2689 return $self->newtype->new(
2691 $self->madness('I ('),
2693 $self->madness(')'),
2695 $self->newtype->new(Kids => [@newkids])
2699 return $self->newtype->new(Kids => [@newkids]);
2702 package PLXML::op_enteriter;
2706 my (undef,$range,$var) = @{$self->{Kids}};
2708 push @retval, $self->madness('v');
2709 if (!@retval and defined $var) {
2710 push @retval, $var->ast($self,@_);
2715 if (ref $range eq 'PLXML::op_null' and $$self{flags} =~ /STACKED/) {
2716 my (undef,$min,$max) = @{$range->{Kids}};
2717 push @retval, $min->ast($self,@_);
2719 if (exists $$range{mp}{O}) { # deeply buried .. operator
2720 PLXML::prepreproc($$range{mp}{O});
2722 $$range{mp}{'O'}{Kids}[0]{Kids}[0]{Kids}[0]{Kids}[0]->madness('o')
2725 push @retval, '..'; # XXX missing whitespace
2727 push @retval, $max->ast($self,@_);
2731 push @retval, $range->ast($self,@_);
2733 return $self->newtype->new(Kids => [@retval]);
2736 package PLXML::op_iter;
2737 package PLXML::op_enterloop;
2742 package PLXML::op_leaveloop;
2749 my $enterloop = $$self{Kids}[0];
2750 my $nextthing = $$self{Kids}[1];
2752 if ($$self{mp}{W}) {
2753 push @retval, $self->madness('L');
2754 push @newkids, $self->madness('W d');
2756 if (ref $enterloop eq 'PLXML::op_enteriter') {
2757 my ($var,@rest) = @{$enterloop->ast($self,@_)->{Kids}};
2758 push @newkids, $var if $var;
2759 push @newkids, $self->madness('q ( x = Q');
2760 push @newkids, @rest;
2763 push @newkids, $self->madness('(');
2764 push @newkids, $enterloop->ast($self,@_);
2769 if (ref $nextthing eq 'PLXML::op_null') {
2770 if ($$nextthing{mp}{'1'}) {
2771 push @newkids, $nextthing->madness('1');
2772 push @newkids, $self->madness(')');
2773 push @newkids, $$nextthing{Kids}[0]->blockast($self,@_);
2775 elsif ($$nextthing{mp}{'2'}) {
2776 push @newkids, $$nextthing{Kids}[0]->ast($self,@_);
2777 push @newkids, $self->madness(')');
2778 push @newkids, $$nextthing{mp}{'2'}->blockast($self,@_);
2780 elsif ($$nextthing{mp}{'U'}) {
2781 push @newkids, $nextthing->ast($self,@_);
2784 # bypass the op_null
2785 $andor = $nextthing->{Kids}[0];
2787 push @newkids, $$andor{Kids}[0]->ast($self, @_);
2789 push @newkids, $self->madness(')');
2791 push @newkids, $$andor{Kids}[1]->blockast($self, @_);
2796 $andor = $nextthing;
2797 push @newkids, $nextthing->madness('O');
2798 push @newkids, $self->madness(')');
2799 push @newkids, $nextthing->blockast($self, @_);
2801 if ($$self{mp}{w}) {
2802 push @newkids, $self->madness('w');
2803 push @newkids, $enterloop->ast($self,@_);
2806 push @retval, @newkids;
2808 return $self->newtype->new(Kids => [@retval]);
2811 package PLXML::op_return;
2812 package PLXML::op_last;
2813 package PLXML::op_next;
2814 package PLXML::op_redo;
2815 package PLXML::op_dump;
2816 package PLXML::op_goto;
2817 package PLXML::op_exit;
2818 package PLXML::op_open;
2819 package PLXML::op_close;
2820 package PLXML::op_pipe_op;
2821 package PLXML::op_fileno;
2822 package PLXML::op_umask;
2823 package PLXML::op_binmode;
2824 package PLXML::op_tie;
2825 package PLXML::op_untie;
2826 package PLXML::op_tied;
2827 package PLXML::op_dbmopen;
2828 package PLXML::op_dbmclose;
2829 package PLXML::op_sselect;
2830 package PLXML::op_select;
2831 package PLXML::op_getc;
2832 package PLXML::op_read;
2833 package PLXML::op_enterwrite;
2834 package PLXML::op_leavewrite;
2835 package PLXML::op_prtf;
2836 package PLXML::op_print;
2837 package PLXML::op_sysopen;
2838 package PLXML::op_sysseek;
2839 package PLXML::op_sysread;
2840 package PLXML::op_syswrite;
2841 package PLXML::op_send;
2842 package PLXML::op_recv;
2843 package PLXML::op_eof;
2844 package PLXML::op_tell;
2845 package PLXML::op_seek;
2846 package PLXML::op_truncate;
2847 package PLXML::op_fcntl;
2848 package PLXML::op_ioctl;
2849 package PLXML::op_flock;
2850 package PLXML::op_socket;
2851 package PLXML::op_sockpair;
2852 package PLXML::op_bind;
2853 package PLXML::op_connect;
2854 package PLXML::op_listen;
2855 package PLXML::op_accept;
2856 package PLXML::op_shutdown;
2857 package PLXML::op_gsockopt;
2858 package PLXML::op_ssockopt;
2859 package PLXML::op_getsockname;
2860 package PLXML::op_getpeername;
2861 package PLXML::op_lstat;
2862 package PLXML::op_stat;
2863 package PLXML::op_ftrread;
2864 package PLXML::op_ftrwrite;
2865 package PLXML::op_ftrexec;
2866 package PLXML::op_fteread;
2867 package PLXML::op_ftewrite;
2868 package PLXML::op_fteexec;
2869 package PLXML::op_ftis;
2870 package PLXML::op_fteowned;
2871 package PLXML::op_ftrowned;
2872 package PLXML::op_ftzero;
2873 package PLXML::op_ftsize;
2874 package PLXML::op_ftmtime;
2875 package PLXML::op_ftatime;
2876 package PLXML::op_ftctime;
2877 package PLXML::op_ftsock;
2878 package PLXML::op_ftchr;
2879 package PLXML::op_ftblk;
2880 package PLXML::op_ftfile;
2881 package PLXML::op_ftdir;
2882 package PLXML::op_ftpipe;
2883 package PLXML::op_ftlink;
2884 package PLXML::op_ftsuid;
2885 package PLXML::op_ftsgid;
2886 package PLXML::op_ftsvtx;
2887 package PLXML::op_fttty;
2888 package PLXML::op_fttext;
2889 package PLXML::op_ftbinary;
2890 package PLXML::op_chdir;
2891 package PLXML::op_chown;
2892 package PLXML::op_chroot;
2893 package PLXML::op_unlink;
2894 package PLXML::op_chmod;
2895 package PLXML::op_utime;
2896 package PLXML::op_rename;
2897 package PLXML::op_link;
2898 package PLXML::op_symlink;
2899 package PLXML::op_readlink;
2900 package PLXML::op_mkdir;
2901 package PLXML::op_rmdir;
2902 package PLXML::op_open_dir;
2903 package PLXML::op_readdir;
2904 package PLXML::op_telldir;
2905 package PLXML::op_seekdir;
2906 package PLXML::op_rewinddir;
2907 package PLXML::op_closedir;
2908 package PLXML::op_fork;
2909 package PLXML::op_wait;
2910 package PLXML::op_waitpid;
2911 package PLXML::op_system;
2912 package PLXML::op_exec;
2913 package PLXML::op_kill;
2914 package PLXML::op_getppid;
2915 package PLXML::op_getpgrp;
2916 package PLXML::op_setpgrp;
2917 package PLXML::op_getpriority;
2918 package PLXML::op_setpriority;
2919 package PLXML::op_time;
2920 package PLXML::op_tms;
2921 package PLXML::op_localtime;
2922 package PLXML::op_gmtime;
2923 package PLXML::op_alarm;
2924 package PLXML::op_sleep;
2925 package PLXML::op_shmget;
2926 package PLXML::op_shmctl;
2927 package PLXML::op_shmread;
2928 package PLXML::op_shmwrite;
2929 package PLXML::op_msgget;
2930 package PLXML::op_msgctl;
2931 package PLXML::op_msgsnd;
2932 package PLXML::op_msgrcv;
2933 package PLXML::op_semget;
2934 package PLXML::op_semctl;
2935 package PLXML::op_semop;
2936 package PLXML::op_require;
2937 package PLXML::op_dofile;
2938 package PLXML::op_entereval;
2942 local $::curstate; # eval {} has own statement sequence
2943 return $self->SUPER::ast(@_);
2946 package PLXML::op_leaveeval;
2947 package PLXML::op_entertry;
2948 package PLXML::op_leavetry;
2953 return $self->PLXML::op_leave::ast(@_);
2956 package PLXML::op_ghbyname;
2957 package PLXML::op_ghbyaddr;
2958 package PLXML::op_ghostent;
2959 package PLXML::op_gnbyname;
2960 package PLXML::op_gnbyaddr;
2961 package PLXML::op_gnetent;
2962 package PLXML::op_gpbyname;
2963 package PLXML::op_gpbynumber;
2964 package PLXML::op_gprotoent;
2965 package PLXML::op_gsbyname;
2966 package PLXML::op_gsbyport;
2967 package PLXML::op_gservent;
2968 package PLXML::op_shostent;
2969 package PLXML::op_snetent;
2970 package PLXML::op_sprotoent;
2971 package PLXML::op_sservent;
2972 package PLXML::op_ehostent;
2973 package PLXML::op_enetent;
2974 package PLXML::op_eprotoent;
2975 package PLXML::op_eservent;
2976 package PLXML::op_gpwnam;
2977 package PLXML::op_gpwuid;
2978 package PLXML::op_gpwent;
2979 package PLXML::op_spwent;
2980 package PLXML::op_epwent;
2981 package PLXML::op_ggrnam;
2982 package PLXML::op_ggrgid;
2983 package PLXML::op_ggrent;
2984 package PLXML::op_sgrent;
2985 package PLXML::op_egrent;
2986 package PLXML::op_getlogin;
2987 package PLXML::op_syscall;
2988 package PLXML::op_lock;
2989 package PLXML::op_threadsv;
2990 package PLXML::op_setstate;
2991 package PLXML::op_method_named;
2995 return $self->madness('O');
2998 package PLXML::op_dor;
3002 $self->PLXML::op_or::astnull(@_);
3005 package PLXML::op_dorassign;
3006 package PLXML::op_custom;