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);
443 @keys = map { $_ eq 'd' ? ('k', 'd') : $_ } @keys;
445 for my $key (@keys) {
446 my $madprop = $self->{mp}{$key};
447 next unless defined $madprop;
448 if (ref $madprop eq 'PLXML::mad_op') {
450 push @vals, $madprop->blockast($self, @_);
453 push @vals, $madprop->ast($self, @_);
458 if ($white = $self->{mp}{"_$key"}) {
459 push @vals, p5::junk->new($white);
461 my $type = $madtype{$key} || "p5::token";
462 push @vals, $type->new($madprop);
463 if ($white = $self->{mp}{"#$key"}) {
464 push @vals, p5::junk->new($white);
479 for my $kid (@{$$self{Kids}}) {
480 push @newkids, $kid->ast($self, @_);
482 return $self->newtype->new(Kids => [uc $self->key(), "(", @newkids, ")"]);
487 my $desc = $self->desc();
488 if ($desc =~ /\((.*?)\)/) {
492 return " <<" . $self->key() . ">> ";
501 package PLXML::Characters;
503 sub ast { die "oops" }
504 sub pair { die "oops" }
506 package PLXML::madprops;
519 # We need to guarantee key uniqueness at this point.
520 for my $kid (@{$$self{Kids}}) {
521 my ($k,$v) = $kid->pair($self, @_);
523 $k .= 'x' while exists $hash{$k};
527 $hash{FIRST} = $firstthing;
528 $hash{LAST} = $lastthing;
532 package PLXML::mad_op;
536 my $key = $$self{key};
542 $self->prepreproc(@_);
544 for my $kid (@{$$self{Kids}}) {
545 push @vals, $kid->ast($self, @_);
551 return P5AST::op_list->new(Kids => [@vals]);
557 $self->prepreproc(@_);
559 for my $kid (@{$$self{Kids}}) {
560 push @vals, $kid->blockast($self, @_);
566 return P5AST::op_lineseq->new(Kids => [@vals]);
570 package PLXML::mad_pv;
574 my $key = $$self{key};
575 my $val = $$self{val};
576 $val =~ s/STUPIDXML\(#x(\w+)\)/chr(hex $1)/eg;
580 package PLXML::mad_sv;
584 my $key = $$self{key};
585 my $val = $$self{val};
586 $val =~ s/STUPIDXML\(#x(\w+)\)/chr(hex $1)/eg;
590 package PLXML::baseop;
597 for my $kid (@{$$self{Kids}}) {
598 push @newkids, $kid->ast($self, @_);
601 push @retval, uc $self->key(), "(", @newkids , ")";
604 push @retval, $self->madness('o ( )');
606 return $self->newtype->new(Kids => [@retval]);
609 package PLXML::baseop_unop;
613 my @newkids = $self->madness('d o (');
615 if (exists $$self{Kids}) {
616 my $arg = $$self{Kids}[0];
617 push @newkids, $arg->ast($self, @_) if defined $arg;
619 push @newkids, $self->madness(')');
621 return $self->newtype()->new(Kids => [@newkids]);
624 package PLXML::binop;
630 my $left = $$self{Kids}[0];
631 push @newkids, $left->ast($self, @_);
633 push @newkids, $self->madness('o');
635 my $right = $$self{Kids}[1];
636 if (defined $right) {
637 push @newkids, $right->ast($self, @_);
640 return $self->newtype->new(Kids => [@newkids]);
645 package PLXML::filestatop;
650 my @newkids = $self->madness('o (');
652 if (@{$$self{Kids}}) {
653 for my $kid (@{$$self{Kids}}) {
654 push @newkids, $kid->ast($self, @_);
658 push @newkids, $self->madness('O');
660 push @newkids, $self->madness(')');
662 return $self->newtype->new(Kids => [@newkids]);
665 package PLXML::listop;
672 if (@retval = $self->madness('X')) {
673 my @before, $self->madness('o x');
674 return P5AST::listop->new(Kids => [@before,@retval]);
677 push @retval, $self->madness('o d ( [ {');
680 for my $kid (@{$$self{Kids}}) {
681 next if ref $kid eq 'PLXML::op_pushmark';
682 next if ref $kid eq 'PLXML::op_null' and
683 defined $$kid{was} and $$kid{was} eq 'pushmark';
684 push @newkids, $kid->ast($self, @_);
690 push @retval, $self->madness('S');
692 push @retval, @newkids;
694 push @retval, $self->madness('} ] )');
695 return $self->newtype->new(Kids => [@retval,@after]);
698 package PLXML::logop;
704 push @newkids, $self->madness('o (');
705 for my $kid (@{$$self{Kids}}) {
706 push @newkids, $kid->ast($self, @_);
708 push @newkids, $self->madness(')');
709 return $self->newtype->new(Kids => [@newkids]);
714 package PLXML::loopexop;
718 my @newkids = $self->madness('o (');
720 if ($$self{mp}{L} or not $$self{flags} =~ /\bSPECIAL\b/) {
721 my @label = $self->madness('L');
723 push @newkids, @label;
726 my $arg = $$self{Kids}[0];
727 push @newkids, $arg->ast($self, @_) if defined $arg;
730 push @newkids, $self->madness(')');
732 return $self->newtype->new(Kids => [@newkids]);
736 package PLXML::padop;
738 package PLXML::padop_svop;
745 return P5AST::pmop->new(Kids => []) unless exists $$self{flags};
747 my $bits = $self->fetchbits($$self{flags},@_);
750 if ($bits->{binding}) {
751 push @newkids, $bits->{binding};
752 push @newkids, $self->madness('~');
754 if (exists $bits->{regcomp} and $bits->{regcomp}) {
755 my @front = $self->madness('q');
756 my @back = $self->madness('Q');
757 push @newkids, @front, $bits->{regcomp}, @back,
760 elsif ($$self{mp}{q}) {
761 push @newkids, $self->madness('q = Q m');
763 elsif ($$self{mp}{X}) {
764 push @newkids, $self->madness('X m');
767 push @newkids, $self->madness('e m');
770 return $self->newtype->new(Kids => [@newkids]);
776 for my $key (grep {!/^Kids/} keys %$pmop) {
777 $bits->{$key} = $pmop->{$key};
780 # Have to delete all the fake evals of the repl. This is a pain...
781 if (@{$$pmop{Kids}}) {
782 my $really = $$pmop{Kids}[0]{Kids}[0];
783 if (ref $really eq 'PLXML::op_substcont') {
784 $really = $$really{Kids}[0];
786 while ((ref $really) =~ /^PLXML::op_.*(null|entereval)/) {
787 if (exists $$really{was}) {
788 $bits->{repl} = $really->ast(@_);
791 $really = $$really{Kids}[0];
793 if (ref $really eq 'PLXML::op_scope' and
794 @{$$really{Kids}} == 1 and
795 ref $$really{Kids}[0] eq 'PLXML::op_null' and
796 not @{$$really{Kids}[0]{Kids}})
801 if (ref $really eq 'PLXML::op_leave' and
802 @{$$really{Kids}} == 2 and
803 ref $$really{Kids}[1] eq 'PLXML::op_null' and
804 not @{$$really{Kids}[1]{Kids}})
809 if ((ref $really) =~ /^PLXML::op_(scope|leave)/) {
810 # should be at inner do {...} here, so skip that fakery too
811 $bits->{repl} = $really->newtype->new(Kids => [$really->PLXML::op_lineseq::lineseq(@_)]);
812 # but retrieve the whitespace before fake '}'
813 if ($$really{mp}{'_}'}) {
814 push(@{$bits->{repl}->{Kids}}, p5::junk->new($$really{mp}{'_}'}));
817 else { # something else, padsv probably
818 $bits->{repl} = $really->ast(@_);
825 my $flags = shift || '';
827 my @kids = @{$$self{Kids}};
830 my $arg = shift @kids;
831 innerpmop($arg,\%bits, $self, @_);
832 if ($flags =~ /STACKED/) {
834 $bits{binding} = $arg->ast($self, @_);
836 if ($bits{when} ne "COMP" and @kids) {
838 $bits{regcomp} = $arg->ast($self, @_);
840 if (not exists $bits{repl} and @kids) {
842 $bits{repl} = $arg->ast($self, @_);
848 package PLXML::pvop_svop;
854 my @newkids = $self->madness('o (');
856 if (exists $$self{Kids}) {
857 my $arg = $$self{Kids}[0];
858 push @newkids, $arg->ast($self, @_) if defined $arg;
860 push @newkids, $self->madness(')');
862 return $self->newtype->new(Kids => [@newkids]);
866 package PLXML::Characters;
867 package PLXML::madprops;
868 package PLXML::mad_op;
869 package PLXML::mad_pv;
870 package PLXML::baseop;
871 package PLXML::baseop_unop;
872 package PLXML::binop;
874 package PLXML::filestatop;
875 package PLXML::listop;
876 package PLXML::logop;
878 package PLXML::loopexop;
879 package PLXML::padop;
880 package PLXML::padop_svop;
882 package PLXML::pvop_svop;
884 package PLXML::op_null;
886 # Null nodes typed by first madprop.
892 'p' => sub { # peg for #! line, etc.
895 push @newkids, $self->madness('p px');
897 return P5AST::peg->new(Kids => [@newkids])
899 '(' => sub { # extra parens around the whole thing
902 push @newkids, $self->madness('dx d o (');
903 for my $kid (@{$$self{Kids}}) {
904 push @newkids, $kid->ast($self, @_);
906 push @newkids, $self->madness(')');
907 return P5AST::parens->new(Kids => [@newkids])
909 '~' => sub { # binding operator
912 push @newkids, $$self{Kids}[0]->ast($self,@_);
913 push @newkids, $self->madness('~');
914 push @newkids, $$self{Kids}[1]->ast($self,@_);
915 return P5AST::bindop->new(Kids => [@newkids])
917 ';' => sub { # null statements/blocks
920 push @newkids, $self->madness('{ ; }');
922 return P5AST::nothing->new(Kids => [@newkids])
924 'I' => sub { # if or unless statement keyword
927 push @newkids, $self->madness('L I (');
929 for my $kid (@{$$self{Kids}}) {
930 push @subkids, $kid->ast($self, @_);
932 die "oops in op_null->new" unless @subkids == 1;
933 my $newself = $subkids[0];
934 @subkids = @{$$newself{Kids}};
936 unshift @{$subkids[0]{Kids}}, @newkids;
937 push @{$subkids[0]{Kids}}, $self->madness(')');
938 return bless($newself, 'P5AST::condstate');
943 my @module = $self->madness('U');
944 my @args = $self->madness('A');
945 my $module = $module[-1]{Kids}[-1];
946 if ($module->uni eq 'bytes') {
947 $::curenc = Nomad::encnum('iso-8859-1');
949 elsif ($module->uni eq 'utf8') {
950 if ($$self{mp}{o} eq 'no') {
951 $::curenc = Nomad::encnum('iso-8859-1');
954 $::curenc = Nomad::encnum('utf-8');
957 elsif ($module->uni eq 'encoding') {
958 if ($$self{mp}{o} eq 'no') {
959 $::curenc = Nomad::encnum('iso-8859-1');
962 $::curenc = Nomad::encnum(eval $args[0]->p5text); # XXX bletch
965 # (Surrounding {} ends up here if use is only thing in block.)
966 push @newkids, $self->madness('{ o');
967 push @newkids, @module;
968 push @newkids, $self->madness('V');
969 push @newkids, @args;
970 push @newkids, $self->madness('S ; }');
972 return P5AST::use->new(Kids => [@newkids])
974 '?' => sub { # ternary
978 my @condkids = @{$$self{Kids}[0]{Kids}};
980 push @newkids, $condkids[0]->ast($self,@_), $self->madness('?');
981 push @newkids, $condkids[1]->ast($self,@_), $self->madness(':');
982 push @newkids, $condkids[2]->ast($self,@_);
983 return P5AST::ternary->new(Kids => [@newkids])
985 '&' => sub { # subroutine
988 push @newkids, $self->madness('d n s a : { & } ;');
990 return P5AST::sub->new(Kids => [@newkids])
992 'i' => sub { # modifier if
995 push @newkids, $self->madness('i');
996 my $cond = $$self{Kids}[0];
998 for my $kid (@{$$cond{Kids}}) {
999 push @subkids, $kid->ast($self, @_);
1001 push @newkids, shift @subkids;
1002 unshift @newkids, @subkids;
1003 return P5AST::condmod->new(Kids => [@newkids])
1005 'P' => sub { # package declaration
1008 push @newkids, $self->madness('o');
1009 push @newkids, $self->madness('P');
1010 push @newkids, $self->madness(';');
1012 return P5AST::package->new(Kids => [@newkids])
1014 'F' => sub { # format
1016 my @newkids = $self->madness('F n b');
1018 return P5AST::format->new(Kids => [@newkids])
1020 'x' => sub { # qw literal
1022 return P5AST::qwliteral->new(Kids => [$self->madness('x')])
1024 'q' => sub { # random quote
1026 return P5AST::quote->new(Kids => [$self->madness('q = Q')])
1028 'X' => sub { # random literal
1030 return P5AST::token->new(Kids => [$self->madness('X')])
1032 ':' => sub { # attr list
1034 return P5AST::attrlist->new(Kids => [$self->madness(':')])
1036 ',' => sub { # "unary ," so to speak
1039 push @newkids, $self->madness(',');
1040 push @newkids, $$self{Kids}[0]->ast($self,@_);
1041 return P5AST::listelem->new(Kids => [@newkids])
1043 'C' => sub { # constant conditional
1046 push @newkids, $$self{Kids}[0]->ast($self,@_);
1047 my @folded = $self->madness('C');
1049 my @t = $self->madness('t');
1050 my @e = $self->madness('e');
1052 return P5AST::op_cond_expr->new(
1054 $self->madness('I ('),
1056 $self->madness(') ?'),
1057 P5AST::op_cond_expr->new(Kids => [@newkids]),
1058 $self->madness(':'),
1063 return P5AST::op_cond_expr->new(
1065 $self->madness('I ('),
1067 $self->madness(') ?'),
1069 $self->madness(':'),
1074 return P5AST::op_null->new(Kids => [@newkids])
1076 '+' => sub { # unary +
1079 push @newkids, $self->madness('+');
1080 push @newkids, $$self{Kids}[0]->ast($self,@_);
1081 return P5AST::preplus->new(Kids => [@newkids])
1083 'D' => sub { # do block
1086 push @newkids, $self->madness('D');
1087 push @newkids, $$self{Kids}[0]->ast($self,@_);
1088 return P5AST::doblock->new(Kids => [@newkids])
1090 '3' => sub { # C-style for loop
1095 my (undef, $init, $lineseq) = @{$$self{Kids}[0]{Kids}};
1096 my (undef, $leaveloop) = @{$$lineseq{Kids}};
1097 my (undef, $null) = @{$$leaveloop{Kids}};
1103 if (exists $$null{was} and $$null{was} eq 'and') {
1104 ($lineseq2) = @{$$null{Kids}};
1107 ($and) = @{$$null{Kids}};
1108 ($cond, $lineseq2) = @{$$and{Kids}};
1110 if ($$lineseq2{mp}{'{'}) {
1114 ($block, $cont) = @{$$lineseq2{Kids}};
1117 push @newkids, $self->madness('L 3 (');
1118 push @newkids, $init->ast($self,@_);
1119 push @newkids, $self->madness('1');
1120 if (defined $cond) {
1121 push @newkids, $cond->ast($self,@_);
1123 elsif (defined $null) {
1124 push @newkids, $null->madness('1');
1126 push @newkids, $self->madness('2');
1127 if (defined $cont) {
1128 push @newkids, $cont->ast($self,@_);
1130 push @newkids, $self->madness(')');
1131 push @newkids, $block->blockast($self,@_);
1133 return P5AST::cfor->new(Kids => [@newkids])
1135 'o' => sub { # random useless operator
1138 push @newkids, $self->madness('o');
1139 my $kind = $newkids[-1] || '';
1140 $kind = $kind->uni if ref $kind;
1142 for my $kid (@{$$self{Kids}}) {
1143 push @subkids, $kid->ast($self, @_);
1145 if ($kind eq '=') { # stealth readline
1146 unshift(@newkids, shift(@subkids));
1147 push(@newkids, @subkids);
1148 return P5AST::op_aassign->new(Kids => [@newkids])
1151 my $newself = $subkids[0];
1152 splice(@{$newself->{Kids}}, 1, 0,
1153 $self->madness('ox ('),
1163 # Null nodes are an untyped mess inside Perl. Instead of fixing it there,
1164 # we derive an effective type either from the "was" field or the first madprop.
1165 # (The individual routines select the actual new type.)
1169 my $was = $$self{was} || 'peg';
1170 my $mad = $$self{mp}{FIRST} || "unknown";
1172 # First try for a "was".
1173 my $meth = "PLXML::op_${was}::astnull";
1174 if (exists &{$meth}) {
1175 return $self->$meth(@_);
1178 # Look at first madprop.
1179 if (exists $astmad{$mad}) {
1180 return $astmad{$mad}->($self);
1182 warn "No mad $mad" unless $mad eq 'unknown';
1184 # Do something generic.
1186 for my $kid (@{$$self{Kids}}) {
1187 push @newkids, $kid->ast($self, @_);
1189 return $self->newtype->new(Kids => [@newkids]);
1195 local $::curenc = $::curenc;
1196 return $self->madness('{ ; }');
1199 package PLXML::op_stub;
1203 return $self->newtype->new(Kids => [$self->madness(', x ( ) q = Q')]);
1206 package PLXML::op_scalar;
1211 my @pre = $self->madness('o q');
1213 if ($op->uni =~ /^<</) {
1215 my $opstub = bless { start => $op }, 'P5AST::heredoc';
1216 push @newkids, $opstub;
1217 push @newkids, $self->madness('(');
1219 my @kids = @{$$self{Kids}};
1222 for my $kid (@kids) {
1223 next if ref $kid eq 'PLXML::op_pushmark';
1224 next if ref $kid eq 'PLXML::op_null' and
1225 defined $$kid{was} and $$kid{was} eq 'pushmark';
1226 push @divert, $kid->ast($self, @_);
1228 $opstub->{doc} = P5AST::op_list->new(Kids => [@divert]);
1229 $opstub->{end} = ($self->madness('Q'))[-1];
1231 push @newkids, $self->madness(')');
1233 return $self->newtype->new(Kids => [@pre,@newkids]);
1235 return $self->PLXML::baseop_unop::ast();
1238 package PLXML::op_pushmark;
1242 package PLXML::op_wantarray;
1243 package PLXML::op_const;
1248 return unless $$self{mp};
1249 push @newkids, $self->madness('q = Q X : f O ( )');
1250 return P5AST::op_const->new(Kids => [@newkids]);
1255 return unless %{$$self{mp}};
1260 my @args = $self->madness('f');
1263 elsif (exists $self->{mp}{q}) {
1264 push @args, $self->madness('d q');
1265 if ($args[-1]->uni =~ /^<</) {
1266 my $opstub = bless { start => pop(@args) }, 'P5AST::heredoc';
1267 $opstub->{doc} = P5AST::op_const->new(Kids => [$self->madness('=')]);
1268 $opstub->{end} = ($self->madness('Q'))[-1];
1269 push @args, $opstub;
1272 push @args, $self->madness('= Q');
1275 elsif (exists $self->{mp}{X}) {
1276 push @before, $self->madness('d'); # was local $[ probably
1277 if (not $$self{mp}{O}) {
1278 push @before, $self->madness('o'); # was unary
1280 my @X = $self->madness(': X');
1281 if (exists $$self{private} and $$self{private} =~ /BARE/) {
1282 return $self->newtype->new(Kids => [@X]);
1287 $self->madness('x'),
1289 if ($$self{mp}{O}) {
1290 push @args, $self->madness('o O');
1293 elsif (exists $self->{mp}{O}) {
1294 push @args, $self->madness('O');
1296 elsif ($$self{private} =~ /\bBARE\b/) {
1297 @args = ($$self{PV});
1299 elsif (exists $$self{mp}{o}) {
1300 @args = $self->madness('o');
1302 elsif (exists $$self{PV}) {
1303 @args = ('"', $$self{PV}, '"');
1305 elsif (exists $$self{NV}) {
1308 elsif (exists $$self{IV}) {
1312 @args = $self->SUPER::text(@_);
1314 return $self->newtype->new(Kids => [@before, @args]);
1318 package PLXML::op_gvsv;
1324 for my $attr (qw/gv GV flags/) {
1325 if (exists $$self{$attr}) {
1326 push @args, $attr, $$self{$attr};
1329 push @retval, @args;
1330 push @retval, $self->madness('X');
1331 return $self->newtype->new(Kids => [@retval]);
1334 package PLXML::op_gv;
1339 push @newkids, $self->madness('X K');
1341 return $self->newtype->new(Kids => [@newkids]);
1344 package PLXML::op_gelem;
1349 local $::curstate; # in case there are statements in subscript
1350 local $::curenc = $::curenc;
1352 push @newkids, $self->madness('dx d');
1353 for my $kid (@{$$self{Kids}}) {
1354 push @newkids, $kid->ast($self, @_);
1356 splice @newkids, -1, 0, $self->madness('o {');
1357 push @newkids, $self->madness('}');
1359 return $self->newtype->new(Kids => [@newkids]);
1362 package PLXML::op_padsv;
1367 push @args, $self->madness('dx d ( $ )');
1369 return $self->newtype->new(Kids => [@args]);
1372 package PLXML::op_padav;
1374 sub astnull { ast(@_) }
1379 push @retval, $self->madness('dx d (');
1380 push @retval, $self->madness('$ @');
1381 push @retval, $self->madness(') o O');
1382 return $self->newtype->new(Kids => [@retval]);
1385 package PLXML::op_padhv;
1387 sub astnull { ast(@_) }
1392 push @retval, $self->madness('dx d (');
1393 push @retval, $self->madness('$ @ %');
1394 push @retval, $self->madness(') o O');
1395 return $self->newtype->new(Kids => [@retval]);
1398 package PLXML::op_padany;
1400 package PLXML::op_pushre;
1404 if ($$self{mp}{q}) {
1405 return $self->madness('q = Q m');
1407 if ($$self{mp}{X}) {
1408 return $self->madness('X m');
1410 if ($$self{mp}{e}) {
1411 return $self->madness('e m');
1413 return $$self{Kids}[1]->ast($self,@_), $self->madness('m');
1416 package PLXML::op_rv2gv;
1422 push @newkids, $self->madness('dx d ( * $');
1423 push @newkids, $$self{Kids}[0]->ast();
1424 push @newkids, $self->madness(')');
1425 return $self->newtype->new(Kids => [@newkids]);
1428 package PLXML::op_rv2sv;
1432 return P5AST::op_rv2sv->new(Kids => [$self->madness('O o dx d ( $ ) : a')]);
1439 push @newkids, $self->madness('dx d ( $');
1440 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
1441 push @newkids, $$self{Kids}[0]->ast();
1443 push @newkids, $self->madness(') : a');
1444 return $self->newtype->new(Kids => [@newkids]);
1447 package PLXML::op_av2arylen;
1453 push @newkids, $$self{Kids}[0]->madness('l');
1454 push @newkids, $$self{Kids}[0]->ast();
1455 return $self->newtype->new(Kids => [@newkids]);
1458 package PLXML::op_rv2cv;
1463 push @newkids, $self->madness('X');
1464 return @newkids if @newkids;
1465 if (exists $$self{mp}{'&'}) {
1466 push @newkids, $self->madness('&');
1467 if (@{$$self{Kids}}) {
1468 push @newkids, $$self{Kids}[0]->ast(@_);
1472 push @newkids, $$self{Kids}[0]->ast(@_);
1474 return P5AST::op_rv2cv->new(Kids => [@newkids]);
1481 push @newkids, $self->madness('&');
1482 if (@{$$self{Kids}}) {
1483 push @newkids, $$self{Kids}[0]->ast();
1485 return $self->newtype->new(Kids => [@newkids]);
1488 package PLXML::op_anoncode;
1492 my $arg = $$self{Kids}[0];
1493 local $::curstate; # hide nested statements in sub
1494 local $::curenc = $::curenc;
1496 return $arg->ast(@_);
1498 return ';'; # XXX literal ; should come through somewhere
1501 package PLXML::op_prototype;
1502 package PLXML::op_refgen;
1506 my @newkids = $self->madness('o s a');
1508 if (exists $$self{Kids}) {
1509 my $arg = $$self{Kids}[0];
1510 push @newkids, $arg->ast($self, @_) if defined $arg;
1513 my $res = $self->newtype->new(Kids => [@newkids]);
1517 package PLXML::op_srefgen;
1522 if ($$self{mp}{FIRST} eq '{') {
1523 local $::curstate; # this is officially a block, so hide it
1524 local $::curenc = $::curenc;
1525 push @newkids, $self->madness('{');
1526 for my $kid (@{$$self{Kids}}) {
1527 push @newkids, $kid->ast($self, @_);
1529 push @newkids, $self->madness('; }');
1530 return P5AST::op_stringify->new(Kids => [@newkids]);
1533 push @newkids, $self->madness('o [');
1534 for my $kid (@{$$self{Kids}}) {
1535 push @newkids, $kid->ast($self, @_);
1537 push @newkids, $self->madness(']');
1538 return P5AST::op_stringify->new(Kids => [@newkids]);
1542 package PLXML::op_ref;
1543 package PLXML::op_bless;
1544 package PLXML::op_backtick;
1549 if (exists $self->{mp}{q}) {
1550 push @args, $self->madness('q');
1551 if ($args[-1]->uni =~ /^<</) {
1552 my $opstub = bless { start => $args[-1] }, 'P5AST::heredoc';
1553 $args[-1] = $opstub;
1554 $opstub->{doc} = P5AST::op_const->new(Kids => [$self->madness('=')]);
1555 $opstub->{end} = ($self->madness('Q'))[-1];
1558 push @args, $self->madness('= Q');
1561 return $self->newtype->new(Kids => [@args]);
1564 package PLXML::op_glob;
1568 my @retval = $self->madness('o q = Q');
1569 if (not @retval or $retval[-1]->uni eq 'glob') {
1570 push @retval, $self->madness('(');
1571 push @retval, $$self{Kids}[0]->ast($self,@_);
1572 push @retval, $self->madness(')');
1574 return P5AST::op_glob->new(Kids => [@retval]);
1577 package PLXML::op_readline;
1582 if (exists $$self{mp}{q}) {
1583 @retval = $self->madness('q = Q');
1585 elsif (exists $$self{mp}{X}) {
1586 @retval = $self->madness('X');
1588 return P5AST::op_readline->new(Kids => [@retval]);
1598 if (exists $$self{mp}{q}) {
1599 @args = $self->madness('q = Q');
1601 elsif (exists $$self{mp}{X}) {
1602 @args = $self->madness('X');
1604 elsif (exists $$self{GV}) {
1607 elsif (@{$$self{Kids}}) {
1608 @args = $self->PLXML::unop::ast(@_);
1611 @args = $self->SUPER::text(@_);
1613 return $self->newtype->new(Kids => [@retval,@args]);
1617 package PLXML::op_rcatline;
1618 package PLXML::op_regcmaybe;
1619 package PLXML::op_regcreset;
1620 package PLXML::op_regcomp;
1624 $self->PLXML::unop::ast(@_);
1627 package PLXML::op_match;
1631 my $retval = $self->SUPER::ast(@_);
1633 if (not $p5re = $retval->p5text()) {
1634 $retval = $self->newtype->new(Kids => [$self->madness('X q = Q m')]);
1635 $p5re = $retval->p5text();
1637 if ($deinterpolate) {
1638 $retval->{P5re} = P5re::qrparse($p5re);
1643 package PLXML::op_qr;
1648 if (exists $$self{flags}) {
1649 $retval = $self->SUPER::ast(@_);
1652 $retval = $self->newtype->new(Kids => [$self->madness('X q = Q m')]);
1654 if ($deinterpolate) {
1655 my $p5re = $retval->p5text();
1656 $retval->{P5re} = P5re::qrparse($p5re);
1661 package PLXML::op_subst;
1666 my $bits = $self->fetchbits($$self{flags},@_);
1669 if ($bits->{binding}) {
1670 push @newkids, $bits->{binding};
1671 push @newkids, $self->madness('~');
1673 my $X = p5::token->new($$self{mp}{X});
1674 my @lfirst = $self->madness('q');
1675 my @llast = $self->madness('Q');
1678 $self->madness('E'), # XXX s/b e probably
1680 my @rfirst = $self->madness('z');
1681 my @rlast = $self->madness('Z');
1682 my @mods = $self->madness('m');
1683 if ($rfirst[-1]->uni ne $llast[-1]->uni) {
1684 push @newkids, @rfirst;
1686 # remove the fake '\n' if /e and '#' in replacement.
1687 if (@mods and $mods[0] =~ m/e/ and ($self->madness('R'))[0]->uni =~ m/#/) {
1688 unshift @rlast, bless {}, 'chomp'; # hack to remove '\n'
1690 push @newkids, $bits->{repl}, @rlast, @mods;
1692 my $retval = $self->newtype->new(Kids => [@newkids]);
1693 if ($deinterpolate) {
1694 my $p5re = $retval->p5text();
1695 $retval->{P5re} = P5re::qrparse($p5re);
1700 package PLXML::op_substcont;
1701 package PLXML::op_trans;
1706 # my $bits = $self->fetchbits($$self{flags},@_);
1709 my @lfirst = $self->madness('q');
1710 my @llast = $self->madness('Q');
1713 $self->madness('E'),
1715 my @rfirst = $self->madness('z');
1716 my @repl = $self->madness('R');
1717 my @rlast = $self->madness('Z');
1718 my @mods = $self->madness('m');
1719 if ($rfirst[-1]->uni ne $llast[-1]->uni) {
1720 push @newkids, @rfirst;
1723 push @newkids, @repl, @rlast, @mods;
1725 my $res = $self->newtype->new(Kids => [@newkids]);
1729 package PLXML::op_sassign;
1735 my $right = $$self{Kids}[1];
1736 eval { push @newkids, $right->ast($self, @_); };
1738 push @newkids, $self->madness('o');
1740 my $left = $$self{Kids}[0];
1741 push @newkids, $left->ast($self, @_);
1743 return $self->newtype->new(Kids => [@newkids]);
1746 package PLXML::op_aassign;
1748 sub astnull { ast(@_) }
1754 my $right = $$self{Kids}[1];
1755 push @newkids, $right->ast($self, @_);
1757 push @newkids, $self->madness('o');
1759 my $left = $$self{Kids}[0];
1760 push @newkids, $left->ast($self, @_);
1762 return $self->newtype->new(Kids => [@newkids]);
1765 package PLXML::op_chop;
1766 package PLXML::op_schop;
1767 package PLXML::op_chomp;
1768 package PLXML::op_schomp;
1769 package PLXML::op_defined;
1770 package PLXML::op_undef;
1771 package PLXML::op_study;
1772 package PLXML::op_pos;
1773 package PLXML::op_preinc;
1777 if ($$self{targ}) { # stealth post inc or dec
1778 return $self->PLXML::op_postinc::ast(@_);
1780 return $self->SUPER::ast(@_);
1783 package PLXML::op_i_preinc;
1785 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1787 package PLXML::op_predec;
1789 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1791 package PLXML::op_i_predec;
1793 sub ast { my $self = shift; $self->PLXML::op_preinc::ast(@_); }
1795 package PLXML::op_postinc;
1801 if (exists $$self{Kids}) {
1802 my $arg = $$self{Kids}[0];
1803 push @newkids, $arg->ast($self, @_) if defined $arg;
1805 push @newkids, $self->madness('o');
1807 my $res = $self->newtype->new(Kids => [@newkids]);
1811 package PLXML::op_i_postinc;
1813 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1815 package PLXML::op_postdec;
1817 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1819 package PLXML::op_i_postdec;
1821 sub ast { my $self = shift; $self->PLXML::op_postinc::ast(@_); }
1823 package PLXML::op_pow;
1824 package PLXML::op_multiply;
1825 package PLXML::op_i_multiply;
1826 package PLXML::op_divide;
1827 package PLXML::op_i_divide;
1828 package PLXML::op_modulo;
1829 package PLXML::op_i_modulo;
1830 package PLXML::op_repeat;
1834 return $self->SUPER::ast(@_)
1835 unless exists $$self{private} and $$self{private} =~ /DOLIST/;
1837 my $newself = $$self{Kids}[0]->ast($self,@_);
1838 splice @{$newself->{Kids}}, -1, 0, $self->madness('o');
1840 return bless $newself, $self->newtype; # rebless the op_null
1843 package PLXML::op_add;
1844 package PLXML::op_i_add;
1845 package PLXML::op_subtract;
1846 package PLXML::op_i_subtract;
1847 package PLXML::op_concat;
1854 my $left = $$self{Kids}[0];
1855 push @newkids, $left->ast($self, @_);
1857 push @newkids, $self->madness('o');
1859 my $right = $$self{Kids}[1];
1860 push @newkids, $right->ast($self, @_);
1861 return P5AST::op_concat->new(Kids => [@newkids]);
1870 my $left = $$self{Kids}[0];
1871 push @newkids, $left->ast($self, @_);
1873 push @newkids, $self->madness('o');
1875 my $right = $$self{Kids}[1];
1876 push @newkids, $right->ast($self, @_);
1878 return $self->newtype->new(Kids => [@newkids, @after]);
1881 package PLXML::op_stringify;
1890 my @front = $self->madness('q (');
1891 my @back = $self->madness(') Q');
1892 my @M = $self->madness('M');
1894 push @newkids, $M[0], $self->madness('o');
1896 push @newkids, @front;
1897 for my $kid (@{$$self{Kids}}) {
1898 push @newkids, $kid->ast($self, @_);
1900 push @newkids, @back;
1901 return P5AST::op_stringify->new(Kids => [@newkids]);
1904 package PLXML::op_left_shift;
1905 package PLXML::op_right_shift;
1906 package PLXML::op_lt;
1907 package PLXML::op_i_lt;
1908 package PLXML::op_gt;
1909 package PLXML::op_i_gt;
1910 package PLXML::op_le;
1911 package PLXML::op_i_le;
1912 package PLXML::op_ge;
1913 package PLXML::op_i_ge;
1914 package PLXML::op_eq;
1915 package PLXML::op_i_eq;
1916 package PLXML::op_ne;
1917 package PLXML::op_i_ne;
1918 package PLXML::op_ncmp;
1919 package PLXML::op_i_ncmp;
1920 package PLXML::op_slt;
1921 package PLXML::op_sgt;
1922 package PLXML::op_sle;
1923 package PLXML::op_sge;
1924 package PLXML::op_seq;
1925 package PLXML::op_sne;
1926 package PLXML::op_scmp;
1927 package PLXML::op_bit_and;
1928 package PLXML::op_bit_xor;
1929 package PLXML::op_bit_or;
1930 package PLXML::op_negate;
1931 package PLXML::op_i_negate;
1932 package PLXML::op_not;
1936 my @newkids = $self->madness('o (');
1938 if (@newkids and $newkids[-1]->uni eq '!~') {
1943 if (exists $$self{Kids}) {
1944 my $arg = $$self{Kids}[0];
1945 push @newkids, $arg->ast($self, @_) if defined $arg;
1948 splice @{$newkids[-1][0]{Kids}}, -2, 0, @swap; # XXX WAG
1950 push @newkids, $self->madness(')');
1952 my $res = $self->newtype->new(Kids => [@newkids]);
1956 package PLXML::op_complement;
1957 package PLXML::op_atan2;
1958 package PLXML::op_sin;
1959 package PLXML::op_cos;
1960 package PLXML::op_rand;
1961 package PLXML::op_srand;
1962 package PLXML::op_exp;
1963 package PLXML::op_log;
1964 package PLXML::op_sqrt;
1965 package PLXML::op_int;
1966 package PLXML::op_hex;
1967 package PLXML::op_oct;
1968 package PLXML::op_abs;
1969 package PLXML::op_length;
1970 package PLXML::op_substr;
1971 package PLXML::op_vec;
1972 package PLXML::op_index;
1973 package PLXML::op_rindex;
1974 package PLXML::op_sprintf;
1975 package PLXML::op_formline;
1976 package PLXML::op_ord;
1977 package PLXML::op_chr;
1978 package PLXML::op_crypt;
1979 package PLXML::op_ucfirst;
1983 return $self->PLXML::listop::ast(@_);
1986 package PLXML::op_lcfirst;
1990 return $self->PLXML::listop::ast(@_);
1993 package PLXML::op_uc;
1997 return $self->PLXML::listop::ast(@_);
2000 package PLXML::op_lc;
2004 return $self->PLXML::listop::ast(@_);
2007 package PLXML::op_quotemeta;
2011 return $self->PLXML::listop::ast(@_);
2014 package PLXML::op_rv2av;
2018 return P5AST::op_rv2av->new(Kids => [$self->madness('$ @')]);
2024 if (ref $$self{Kids}[0] eq 'PLXML::op_const' and $$self{mp}{'O'}) {
2025 return $self->madness('O');
2029 push @before, $self->madness('dx d (');
2032 push @newkids, $self->madness('$ @ K');
2033 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
2034 push @newkids, $$self{Kids}[0]->ast();
2037 push @after, $self->madness(') a');
2038 return $self->newtype->new(Kids => [@before, @newkids, @after]);
2041 package PLXML::op_aelemfast;
2045 return $self->madness('$');
2048 package PLXML::op_aelem;
2053 push @newkids, $self->madness('dx d');
2054 for my $kid (@{$$self{Kids}}) {
2055 push @newkids, $kid->ast($self, @_);
2057 splice @newkids, -1, 0, $self->madness('a [');
2058 push @newkids, $self->madness(']');
2059 return P5AST::op_aelem->new(Kids => [@newkids]);
2065 my @before = $self->madness('dx d');
2067 for my $kid (@{$$self{Kids}}) {
2068 push @newkids, $kid->ast(@_);
2070 splice @newkids, -1, 0, $self->madness('a [');
2071 push @newkids, $self->madness(']');
2073 return $self->newtype->new(Kids => [@before, @newkids]);
2076 package PLXML::op_aslice;
2081 push @newkids, $self->madness('[');
2082 for my $kid (@{$$self{Kids}}) {
2083 push @newkids, $kid->ast(@_);
2085 unshift @newkids, pop @newkids;
2086 unshift @newkids, $self->madness('dx d');
2087 push @newkids, $self->madness(']');
2088 return P5AST::op_aslice->new(Kids => [@newkids]);
2095 push @newkids, $self->madness('[');
2096 for my $kid (@{$$self{Kids}}) {
2097 push @newkids, $kid->ast(@_);
2099 unshift @newkids, pop @newkids;
2100 unshift @newkids, $self->madness('dx d');
2101 push @newkids, $self->madness(']');
2103 return $self->newtype->new(Kids => [@newkids]);
2106 package PLXML::op_each;
2107 package PLXML::op_values;
2108 package PLXML::op_keys;
2109 package PLXML::op_delete;
2110 package PLXML::op_exists;
2111 package PLXML::op_rv2hv;
2115 return P5AST::op_rv2hv->new(Kids => [$self->madness('$')]);
2122 push @before, $self->madness('dx d (');
2125 push @newkids, $self->madness('$ @ % K');
2126 if (ref $$self{Kids}[0] ne "PLXML::op_gv") {
2127 push @newkids, $$self{Kids}[0]->ast();
2130 push @after, $self->madness(') a');
2131 return $self->newtype->new(Kids => [@before, @newkids, @after]);
2134 package PLXML::op_helem;
2138 local $::curstate; # hash subscript potentially a lineseq
2139 local $::curenc = $::curenc;
2142 push @newkids, $self->madness('dx d');
2143 for my $kid (@{$$self{Kids}}) {
2144 push @newkids, $kid->ast($self, @_);
2146 splice @newkids, -1, 0, $self->madness('a {');
2147 push @newkids, $self->madness('}');
2148 return P5AST::op_helem->new(Kids => [@newkids]);
2153 local $::curstate; # hash subscript potentially a lineseq
2154 local $::curenc = $::curenc;
2156 my @before = $self->madness('dx d');
2158 for my $kid (@{$$self{Kids}}) {
2159 push @newkids, $kid->ast($self, @_);
2161 splice @newkids, -1, 0, $self->madness('a {');
2162 push @newkids, $self->madness('}');
2164 return $self->newtype->new(Kids => [@before, @newkids]);
2168 package PLXML::op_hslice;
2173 push @newkids, $self->madness('{');
2174 for my $kid (@{$$self{Kids}}) {
2175 push @newkids, $kid->ast(@_);
2177 unshift @newkids, pop @newkids;
2178 unshift @newkids, $self->madness('dx d');
2179 push @newkids, $self->madness('}');
2180 return P5AST::op_hslice->new(Kids => [@newkids]);
2187 push @newkids, $self->madness('{');
2188 for my $kid (@{$$self{Kids}}) {
2189 push @newkids, $kid->ast(@_);
2191 unshift @newkids, pop @newkids;
2192 unshift @newkids, $self->madness('dx d');
2193 push @newkids, $self->madness('}');
2195 return $self->newtype->new(Kids => [@newkids]);
2198 package PLXML::op_unpack;
2199 package PLXML::op_pack;
2200 package PLXML::op_split;
2201 package PLXML::op_join;
2202 package PLXML::op_list;
2209 if (@retval = $self->madness('X')) {
2210 push @before, $self->madness('x o');
2211 return @before,@retval;
2213 my @kids = @{$$self{Kids}};
2214 for my $kid (@kids) {
2215 next if ref $kid eq 'PLXML::op_pushmark';
2216 next if ref $kid eq 'PLXML::op_null' and
2217 defined $$kid{was} and $$kid{was} eq 'pushmark';
2218 push @newkids, $kid->ast($self, @_);
2222 my @newnewkids = ();
2223 push @newnewkids, $self->madness('dx d (');
2224 push @newnewkids, @newkids;
2225 push @newnewkids, $self->madness(') :');
2226 return P5AST::op_list->new(Kids => [@newnewkids]);
2234 if (@retval = $self->madness('X')) {
2235 push @before, $self->madness('o');
2236 return $self->newtype->new(Kids => [@before,@retval]);
2238 push @retval, $self->madness('dx d (');
2241 for my $kid (@{$$self{Kids}}) {
2242 push @newkids, $kid->ast($self, @_);
2245 my @newnewkids = ();
2246 push @newnewkids, @newkids;
2247 @newkids = @newnewkids;
2248 push @retval, @newkids;
2249 push @retval, $self->madness(') :');
2250 return $self->newtype->new(Kids => [@retval]);
2253 package PLXML::op_lslice;
2259 if ($$self{mp}{q}) {
2260 push @newkids, $self->madness('q = Q');
2262 elsif ($$self{mp}{x}) {
2263 push @newkids, $self->madness('x');
2266 push @newkids, $self->madness('(');
2267 my $list = $$self{Kids}[1];
2268 push @newkids, $list->ast($self, @_);
2269 push @newkids, $self->madness(')');
2272 push @newkids, $self->madness('[');
2274 my $slice = $$self{Kids}[0];
2275 push @newkids, $slice->ast($self, @_);
2276 push @newkids, $self->madness(']');
2278 return $self->newtype->new(Kids => [@newkids]);
2281 package PLXML::op_anonlist;
2282 package PLXML::op_anonhash;
2283 package PLXML::op_splice;
2284 package PLXML::op_push;
2285 package PLXML::op_pop;
2286 package PLXML::op_shift;
2287 package PLXML::op_unshift;
2288 package PLXML::op_sort;
2289 package PLXML::op_reverse;
2293 $self->PLXML::listop::ast(@_);
2296 package PLXML::op_grepstart;
2297 package PLXML::op_grepwhile;
2298 package PLXML::op_mapstart;
2299 package PLXML::op_mapwhile;
2300 package PLXML::op_range;
2304 return $self->PLXML::binop::ast(@_);
2307 package PLXML::op_flip;
2308 package PLXML::op_flop;
2309 package PLXML::op_and;
2314 my @first = $self->madness('1');
2315 my @second = $self->madness('2');
2316 my @stuff = $$self{Kids}[0]->ast();
2317 if (my @I = $self->madness('I')) {
2320 push @newkids, $self->madness('(');
2321 push @newkids, @stuff;
2322 push @newkids, $self->madness(')');
2323 push @newkids, @second;
2327 push @newkids, $self->madness('(');
2328 push @newkids, @first;
2329 push @newkids, $self->madness(')');
2330 push @newkids, @stuff;
2333 elsif (my @i = $self->madness('i')) {
2335 push @newkids, @second;
2337 push @newkids, @stuff;
2340 push @newkids, @stuff;
2342 push @newkids, @first;
2345 elsif (my @o = $self->madness('o')) {
2347 push @newkids, @stuff;
2349 push @newkids, @second;
2352 push @newkids, @first;
2354 push @newkids, @stuff;
2357 return P5AST::op_and->new(Kids => [@newkids]);
2360 package PLXML::op_or;
2365 my @first = $self->madness('1');
2366 my @second = $self->madness('2');
2367 my @i = $self->madness('i');
2368 my @stuff = $$self{Kids}[0]->ast();
2371 push @newkids, @second;
2372 push @newkids, $self->madness('i');
2373 push @newkids, @stuff;
2376 push @newkids, @stuff;
2377 push @newkids, $self->madness('o');
2378 push @newkids, @second;
2383 push @newkids, @stuff;
2384 push @newkids, $self->madness('i');
2385 push @newkids, @first;
2388 push @newkids, @first;
2389 push @newkids, $self->madness('o');
2390 push @newkids, @stuff;
2393 return "P5AST::op_$$self{was}"->new(Kids => [@newkids]);
2397 package PLXML::op_xor;
2398 package PLXML::op_cond_expr;
2399 package PLXML::op_andassign;
2400 package PLXML::op_orassign;
2401 package PLXML::op_method;
2402 package PLXML::op_entersub;
2407 if ($$self{mp}{q}) {
2408 return $self->madness('q = Q');
2410 if ($$self{mp}{X}) { # <FH> override?
2411 return $self->madness('X');
2413 if ($$self{mp}{A}) {
2414 return $self->astmethod(@_);
2416 if ($$self{mp}{a}) {
2417 return $self->astarrow(@_);
2423 my @kids = @{$$self{Kids}};
2424 if (@kids == 1 and ref $kids[0] eq 'PLXML::op_null' and $kids[0]{was} =~ /list/) {
2425 @kids = @{$kids[0]{Kids}};
2427 my $dest = pop @kids;
2428 my @dest = $dest->ast($self, @_);
2430 if (ref($dest) =~ /method/) {
2431 my $invocant = shift @kids;
2432 $invocant = shift @kids if ref($invocant) eq 'PLXML::op_pushmark';
2433 my @invocant = $invocant->ast($self, @_);
2434 push @retval, @dest;
2435 push @retval, @invocant;
2437 elsif (exists $$self{mp}{o} and $$self{mp}{o} eq 'do') {
2438 push @retval, $self->madness('o');
2439 push @retval, @dest;
2442 push @retval, $self->madness('o');
2443 push @retval, @dest;
2446 my $kid = shift(@kids);
2447 push @newkids, $kid->ast($self, @_);
2450 push @retval, $self->madness('(');
2451 push @retval, @newkids;
2452 push @retval, $self->madness(')');
2453 return $self->newtype->new(Kids => [@retval]);
2460 for my $kid (@{$$self{Kids}}) {
2461 next if ref $kid eq 'PLXML::op_pushmark';
2462 next if ref $kid eq 'PLXML::op_null' and
2463 defined $$kid{was} and $$kid{was} eq 'pushmark';
2467 if ($$self{flags} =~ /\bSTACKED\b/) {
2468 push @invocant, shift(@kids)->ast($self, @_);
2470 for my $kid (@kids) {
2471 push @newkids, $kid->ast($self, @_);
2473 my $dest = pop(@newkids);
2474 if (ref $dest eq 'PLXML::op_rv2cv' and $$self{flags} =~ /\bMOD\b/) {
2475 $dest = pop(@newkids);
2479 push @retval, @invocant;
2480 push @retval, $self->madness('A');
2481 push @retval, $dest;
2482 push @retval, $self->madness('(');
2483 push @retval, @newkids;
2484 push @retval, $self->madness(')');
2485 return $self->newtype->new(Kids => [@retval]);
2492 my @kids = @{$$self{Kids}};
2493 if (@kids == 1 and ref $kids[0] eq 'PLXML::op_null' and $kids[0]{was} =~ /list/) {
2494 @kids = @{$kids[0]{Kids}};
2497 my $kid = shift(@kids);
2498 push @newkids, $kid->ast($self, @_);
2500 my @dest = $kids[0]->ast($self, @_);
2502 push @retval, @dest;
2503 push @retval, $self->madness('a');
2504 push @retval, $self->madness('(');
2505 push @retval, @newkids;
2506 push @retval, $self->madness(')');
2507 return $self->newtype->new(Kids => [@retval]);
2510 package PLXML::op_leavesub;
2514 if (ref $$self{Kids}[0] eq "PLXML::op_null") {
2515 return $$self{Kids}[0]->ast(@_);
2517 return $$self{Kids}[0]->blockast($self, @_);
2520 package PLXML::op_leavesublv;
2525 return $$self{Kids}[0]->blockast($self, @_);
2528 package PLXML::op_caller;
2529 package PLXML::op_warn;
2530 package PLXML::op_die;
2531 package PLXML::op_reset;
2532 package PLXML::op_lineseq;
2536 my @kids = @{$$self{Kids}};
2537 local $::curstate = 0; # (probably redundant, but that's okay)
2538 local $::prevstate = 0;
2539 local $::curenc = $::curenc;
2544 my $kid = shift @kids;
2545 my $thing = $kid->ast($self, @_);
2546 next unless defined $thing;
2547 if ($::curstate ne $::prevstate) {
2549 push @newstuff, $::prevstate->madness(';');
2550 push @{$newprev->{Kids}}, @newstuff if $newprev;
2553 $::prevstate = $::curstate;
2555 push @retval, $thing;
2557 elsif ($::prevstate) {
2558 push @newstuff, $thing;
2561 push @retval, $thing;
2565 push @newstuff, $::prevstate->madness(';');
2566 push @{$newprev->{Kids}}, @newstuff if $newprev;
2578 push @retval, $self->madness('{');
2580 my @newkids = $self->PLXML::op_lineseq::lineseq(@_);
2581 push @retval, @newkids;
2583 push @retval, $self->madness('; }');
2584 return $self->newtype->new(Kids => [@retval]);
2587 package PLXML::op_nextstate;
2589 sub newtype { return "P5AST::statement" }
2594 push @newkids, $self->madness('L');
2595 $::curstate = $self;
2596 return P5AST::statement->new(Kids => [@newkids]);
2603 push @newkids, $self->madness('L');
2604 $::curstate = $self;
2605 return $self->newtype->new(Kids => [@newkids]);
2609 package PLXML::op_dbstate;
2610 package PLXML::op_unstack;
2611 package PLXML::op_enter;
2615 package PLXML::op_leave;
2624 my $mad = $$self{mp}{FIRST} || "unknown";
2630 push @tmpkids, $self->{Kids};
2631 my $anddo = $$self{Kids}[-1]{Kids}[0]{Kids};
2632 eval { push @newkids, $anddo->[1]->ast($self,@_); };
2633 push @newkids, "[[[NOANDDO]]]" if $@;
2634 push @newkids, $self->madness('w');
2635 push @newkids, $anddo->[0]->ast($self,@_);
2637 return $self->newtype->new(Kids => [@newkids]);
2641 push @retval, $self->madness('o {');
2643 my @newkids = $self->PLXML::op_lineseq::lineseq(@_);
2644 push @retval, @newkids;
2645 push @retval, $self->madness(q/; }/);
2646 my $retval = $self->newtype->new(Kids => [@retval]);
2648 if ($$self{mp}{C}) {
2651 push @before, $self->madness('I ( C )');
2652 if ($$self{mp}{t}) {
2653 push @before, $self->madness('t');
2655 elsif ($$self{mp}{e}) {
2656 push @after, $self->madness('e');
2658 return P5AST::op_cond->new(Kids => [@before, $retval, @after]);
2665 package PLXML::op_scope;
2672 push @newkids, $self->madness('o');
2674 push @newkids, $self->madness('{');
2675 push @newkids, $self->PLXML::op_lineseq::lineseq(@_);
2676 push @newkids, $self->madness('; }');
2678 my @folded = $self->madness('C');
2680 my @t = $self->madness('t');
2681 my @e = $self->madness('e');
2683 return $self->newtype->new(
2685 $self->madness('I ('),
2687 $self->madness(')'),
2688 $self->newtype->new(Kids => [@newkids]),
2693 return $self->newtype->new(
2695 $self->madness('I ('),
2697 $self->madness(')'),
2699 $self->newtype->new(Kids => [@newkids])
2703 return $self->newtype->new(Kids => [@newkids]);
2706 package PLXML::op_enteriter;
2710 my (undef,$range,$var) = @{$self->{Kids}};
2712 push @retval, $self->madness('v');
2713 if (!@retval and defined $var) {
2714 push @retval, $var->ast($self,@_);
2719 if (ref $range eq 'PLXML::op_null' and $$self{flags} =~ /STACKED/) {
2720 my (undef,$min,$max) = @{$range->{Kids}};
2721 push @retval, $min->ast($self,@_);
2723 if (exists $$range{mp}{O}) { # deeply buried .. operator
2724 PLXML::prepreproc($$range{mp}{O});
2726 $$range{mp}{'O'}{Kids}[0]{Kids}[0]{Kids}[0]{Kids}[0]->madness('o')
2729 push @retval, '..'; # XXX missing whitespace
2731 push @retval, $max->ast($self,@_);
2735 push @retval, $range->ast($self,@_);
2737 return $self->newtype->new(Kids => [@retval]);
2740 package PLXML::op_iter;
2741 package PLXML::op_enterloop;
2746 package PLXML::op_leaveloop;
2753 my $enterloop = $$self{Kids}[0];
2754 my $nextthing = $$self{Kids}[1];
2756 if ($$self{mp}{W}) {
2757 push @retval, $self->madness('L');
2758 push @newkids, $self->madness('W d');
2760 if (ref $enterloop eq 'PLXML::op_enteriter') {
2761 my ($var,@rest) = @{$enterloop->ast($self,@_)->{Kids}};
2762 push @newkids, $var if $var;
2763 push @newkids, $self->madness('q ( x = Q');
2764 push @newkids, @rest;
2767 push @newkids, $self->madness('(');
2768 push @newkids, $enterloop->ast($self,@_);
2773 if (ref $nextthing eq 'PLXML::op_null') {
2774 if ($$nextthing{mp}{'1'}) {
2775 push @newkids, $nextthing->madness('1');
2776 push @newkids, $self->madness(')');
2777 push @newkids, $$nextthing{Kids}[0]->blockast($self,@_);
2779 elsif ($$nextthing{mp}{'2'}) {
2780 push @newkids, $$nextthing{Kids}[0]->ast($self,@_);
2781 push @newkids, $self->madness(')');
2782 push @newkids, $$nextthing{mp}{'2'}->blockast($self,@_);
2784 elsif ($$nextthing{mp}{'U'}) {
2785 push @newkids, $nextthing->ast($self,@_);
2788 # bypass the op_null
2789 $andor = $nextthing->{Kids}[0];
2791 push @newkids, $$andor{Kids}[0]->ast($self, @_);
2793 push @newkids, $self->madness(')');
2795 push @newkids, $$andor{Kids}[1]->blockast($self, @_);
2800 $andor = $nextthing;
2801 push @newkids, $nextthing->madness('O');
2802 push @newkids, $self->madness(')');
2803 push @newkids, $nextthing->blockast($self, @_);
2805 if ($$self{mp}{w}) {
2806 push @newkids, $self->madness('w');
2807 push @newkids, $enterloop->ast($self,@_);
2810 push @retval, @newkids;
2812 return $self->newtype->new(Kids => [@retval]);
2815 package PLXML::op_return;
2816 package PLXML::op_last;
2817 package PLXML::op_next;
2818 package PLXML::op_redo;
2819 package PLXML::op_dump;
2820 package PLXML::op_goto;
2821 package PLXML::op_exit;
2822 package PLXML::op_open;
2823 package PLXML::op_close;
2824 package PLXML::op_pipe_op;
2825 package PLXML::op_fileno;
2826 package PLXML::op_umask;
2827 package PLXML::op_binmode;
2828 package PLXML::op_tie;
2829 package PLXML::op_untie;
2830 package PLXML::op_tied;
2831 package PLXML::op_dbmopen;
2832 package PLXML::op_dbmclose;
2833 package PLXML::op_sselect;
2834 package PLXML::op_select;
2835 package PLXML::op_getc;
2836 package PLXML::op_read;
2837 package PLXML::op_enterwrite;
2838 package PLXML::op_leavewrite;
2839 package PLXML::op_prtf;
2840 package PLXML::op_print;
2841 package PLXML::op_say;
2842 package PLXML::op_sysopen;
2843 package PLXML::op_sysseek;
2844 package PLXML::op_sysread;
2845 package PLXML::op_syswrite;
2846 package PLXML::op_send;
2847 package PLXML::op_recv;
2848 package PLXML::op_eof;
2849 package PLXML::op_tell;
2850 package PLXML::op_seek;
2851 package PLXML::op_truncate;
2852 package PLXML::op_fcntl;
2853 package PLXML::op_ioctl;
2854 package PLXML::op_flock;
2855 package PLXML::op_socket;
2856 package PLXML::op_sockpair;
2857 package PLXML::op_bind;
2858 package PLXML::op_connect;
2859 package PLXML::op_listen;
2860 package PLXML::op_accept;
2861 package PLXML::op_shutdown;
2862 package PLXML::op_gsockopt;
2863 package PLXML::op_ssockopt;
2864 package PLXML::op_getsockname;
2865 package PLXML::op_getpeername;
2866 package PLXML::op_lstat;
2867 package PLXML::op_stat;
2868 package PLXML::op_ftrread;
2869 package PLXML::op_ftrwrite;
2870 package PLXML::op_ftrexec;
2871 package PLXML::op_fteread;
2872 package PLXML::op_ftewrite;
2873 package PLXML::op_fteexec;
2874 package PLXML::op_ftis;
2875 package PLXML::op_fteowned;
2876 package PLXML::op_ftrowned;
2877 package PLXML::op_ftzero;
2878 package PLXML::op_ftsize;
2879 package PLXML::op_ftmtime;
2880 package PLXML::op_ftatime;
2881 package PLXML::op_ftctime;
2882 package PLXML::op_ftsock;
2883 package PLXML::op_ftchr;
2884 package PLXML::op_ftblk;
2885 package PLXML::op_ftfile;
2886 package PLXML::op_ftdir;
2887 package PLXML::op_ftpipe;
2888 package PLXML::op_ftlink;
2889 package PLXML::op_ftsuid;
2890 package PLXML::op_ftsgid;
2891 package PLXML::op_ftsvtx;
2892 package PLXML::op_fttty;
2893 package PLXML::op_fttext;
2894 package PLXML::op_ftbinary;
2895 package PLXML::op_chdir;
2896 package PLXML::op_chown;
2897 package PLXML::op_chroot;
2898 package PLXML::op_unlink;
2899 package PLXML::op_chmod;
2900 package PLXML::op_utime;
2901 package PLXML::op_rename;
2902 package PLXML::op_link;
2903 package PLXML::op_symlink;
2904 package PLXML::op_readlink;
2905 package PLXML::op_mkdir;
2906 package PLXML::op_rmdir;
2907 package PLXML::op_open_dir;
2908 package PLXML::op_readdir;
2909 package PLXML::op_telldir;
2910 package PLXML::op_seekdir;
2911 package PLXML::op_rewinddir;
2912 package PLXML::op_closedir;
2913 package PLXML::op_fork;
2914 package PLXML::op_wait;
2915 package PLXML::op_waitpid;
2916 package PLXML::op_system;
2917 package PLXML::op_exec;
2918 package PLXML::op_kill;
2919 package PLXML::op_getppid;
2920 package PLXML::op_getpgrp;
2921 package PLXML::op_setpgrp;
2922 package PLXML::op_getpriority;
2923 package PLXML::op_setpriority;
2924 package PLXML::op_time;
2925 package PLXML::op_tms;
2926 package PLXML::op_localtime;
2927 package PLXML::op_gmtime;
2928 package PLXML::op_alarm;
2929 package PLXML::op_sleep;
2930 package PLXML::op_shmget;
2931 package PLXML::op_shmctl;
2932 package PLXML::op_shmread;
2933 package PLXML::op_shmwrite;
2934 package PLXML::op_msgget;
2935 package PLXML::op_msgctl;
2936 package PLXML::op_msgsnd;
2937 package PLXML::op_msgrcv;
2938 package PLXML::op_semget;
2939 package PLXML::op_semctl;
2940 package PLXML::op_semop;
2941 package PLXML::op_require;
2942 package PLXML::op_dofile;
2943 package PLXML::op_entereval;
2947 local $::curstate; # eval {} has own statement sequence
2948 return $self->SUPER::ast(@_);
2951 package PLXML::op_leaveeval;
2952 package PLXML::op_entertry;
2953 package PLXML::op_leavetry;
2958 return $self->PLXML::op_leave::ast(@_);
2961 package PLXML::op_ghbyname;
2962 package PLXML::op_ghbyaddr;
2963 package PLXML::op_ghostent;
2964 package PLXML::op_gnbyname;
2965 package PLXML::op_gnbyaddr;
2966 package PLXML::op_gnetent;
2967 package PLXML::op_gpbyname;
2968 package PLXML::op_gpbynumber;
2969 package PLXML::op_gprotoent;
2970 package PLXML::op_gsbyname;
2971 package PLXML::op_gsbyport;
2972 package PLXML::op_gservent;
2973 package PLXML::op_shostent;
2974 package PLXML::op_snetent;
2975 package PLXML::op_sprotoent;
2976 package PLXML::op_sservent;
2977 package PLXML::op_ehostent;
2978 package PLXML::op_enetent;
2979 package PLXML::op_eprotoent;
2980 package PLXML::op_eservent;
2981 package PLXML::op_gpwnam;
2982 package PLXML::op_gpwuid;
2983 package PLXML::op_gpwent;
2984 package PLXML::op_spwent;
2985 package PLXML::op_epwent;
2986 package PLXML::op_ggrnam;
2987 package PLXML::op_ggrgid;
2988 package PLXML::op_ggrent;
2989 package PLXML::op_sgrent;
2990 package PLXML::op_egrent;
2991 package PLXML::op_getlogin;
2992 package PLXML::op_syscall;
2993 package PLXML::op_lock;
2994 package PLXML::op_threadsv;
2995 package PLXML::op_setstate;
2996 package PLXML::op_method_named;
3000 return $self->madness('O');
3003 package PLXML::op_dor;
3007 $self->PLXML::op_or::astnull(@_);
3010 package PLXML::op_dorassign;
3011 package PLXML::op_custom;