Changeset 22573 for src/perl6

Show
Ignore:
Timestamp:
10/11/08 02:47:01 (6 weeks ago)
Author:
lwall
Message:

[viv] can now reproduce t/01-sanity/01-tap.t
[STD] remember list separators

Location:
src/perl6
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/STD.pm

    r22572 r22573  
    638638    | $ 
    639639    | <?before <[\)\]\}]> > 
    640     | [<statement><.eat_terminator> ]* 
     640    | [<statement><eat_terminator> ]* 
    641641    ] 
    642642} 
     
    647647    [ 
    648648    | <?before <[\)\]\}]> > 
    649     | [<statement><.eat_terminator> ]* 
     649    | [<statement><eat_terminator> ]* 
    650650    ] 
    651651} 
     
    34103410                self.deb("reducing list") if $*DEBUG +& DEBUG::EXPR; 
    34113411                my @list; 
     3412                my @delims = $op; 
    34123413                push @list, pop(@termstack); 
    34133414                my $s = $op<sym>; 
     
    34213422                        self.worry("Missing term in " ~ $s ~ " list"); 
    34223423                    } 
    3423                     pop(@opstack); 
     3424                    push @delims, pop(@opstack); 
    34243425                } 
    34253426                if @termstack and defined @termstack[0] { 
     
    34303431                } 
    34313432                @list = reverse @list if @list > 1; 
    3432                 $op<list> = [@list]; 
    3433                 $op<_arity> = 'LIST'; 
    3434                 push @termstack, $op._REDUCE('EXPR'); 
     3433                @delims = reverse @delims if @delims > 1; 
     3434                my $nop = $op.cursor_fresh(); 
     3435                $nop<O> = $op<O>; 
     3436                $nop<list> = [@list]; 
     3437                $nop<delims> = [@delims]; 
     3438                $nop<_arity> = 'LIST'; 
     3439                push @termstack, $nop._REDUCE('EXPR'); 
    34353440            } 
    34363441            when 'unary' { 
  • src/perl6/viv

    r22569 r22573  
    1414my $OPT_pos = 0; 
    1515my $OPT_match = 0; 
     16our $ORIG; 
     17our $POS; 
     18my %did_ws; 
     19 
    1620 
    1721sub MAIN { 
     
    2832        elsif ($switch eq '--p6') { 
    2933            $output = 'p6'; 
     34            $OPT_pos = 1; 
    3035        } 
    3136        elsif ($switch eq '--pos') { 
     
    6368        my $self = shift; 
    6469        my $match = shift; 
     70        $ORIG ||= ${$match->{_orig}}; 
     71        $POS ||= $match->{'_'}; 
    6572        my $r = hoist($match); 
    6673        (my $class = $AUTOLOAD) =~ s/^Actions/VAST/; 
     
    93100            elsif ($k eq '_from') { 
    94101                $r{POS} = $v if $OPT_pos; 
    95                 if (exists $node->{'_'}[$v]{'ws'}) { 
    96                     my $wsstart = $node->{'_'}[$v]{'ws'}; 
     102                if (exists $$POS[$v]{'ws'}) { 
     103                    my $wsstart = $$POS[$v]{'ws'}; 
    97104                    $r{WS} = $v - $wsstart if defined $wsstart and $wsstart < $v 
    98105                } 
     
    133140                else { 
    134141                    $r{$k} = $v; 
    135                     $r{TEXT} = $text; 
    136                 } 
    137             } 
    138         } 
    139         $r{TEXT} = $text unless keys %r; 
     142                } 
     143            } 
     144        } 
     145        $r{TEXT} = $text unless exists $r{zygs}; 
    140146        \%r; 
    141147    } 
     
    145151        my $match = shift; 
    146152        my $r = hoist($match); 
    147         (my $class = $r->{kind} // "STD::TERM") =~ s/^STD/VAST/; 
     153        (my $class = $r->{kind} // ref $r) =~ s/^STD/VAST/; 
    148154        gen_class($class); 
    149155        $match->{''} = bless $r, $class; 
     
    165171 
    166172    sub emit_p6 { my $self = shift; 
    167         my $text; 
     173        my $text = $self->get_ws; 
    168174        my @sym; 
    169175        if (exists $self->{sym}) { 
     
    177183        } 
    178184        if ($self->{zygs}) { 
    179             my @zyg = $self->visit_zygs; 
    180             my $arity = $self->{arity} // ''; 
     185            my @zyg = $self->get_zygs; 
     186            my $arity = $self->{ARITY} // ''; 
    181187            if ($arity eq 'BINARY') { 
    182                 $text .= shift @zyg; 
    183                 $text .= shift @sym; 
    184                 $text .= shift @zyg; 
     188                $text .= $zyg[0] . $zyg[2] . $zyg[1]; 
    185189            } 
    186190            elsif ($arity eq 'UNARY') { 
    187191                if ($self->{post}) { 
    188                     $text .= shift @zyg; 
    189                     $text .= shift @sym; 
     192                    $text .= $zyg[0] . $zyg[1]; 
    190193                } 
    191194                else { 
    192                     $text .= shift @sym; 
    193                     $text .= shift @zyg; 
     195                    $text .= $zyg[1] . $zyg[0]; 
    194196                } 
    195197            } 
    196198            else { 
    197                 $text .= join('', @zyg); 
     199                $text .= join('', reverse @zyg); 
    198200            } 
    199201        } 
    200202        elsif (exists $self->{TEXT}) { 
    201             $text = $self->{TEXT}; 
     203            $text .= $self->{TEXT}; 
    202204        } 
    203205        elsif (@sym) { 
    204206            $text .= join('', @sym); 
    205207        } 
    206         $text; 
    207     } 
    208  
    209     sub visit_zygs { my $self = shift; 
     208        $self->ret($text); 
     209    } 
     210 
     211    sub ret { my $self = shift; 
     212        warn ref $self, " returns ", $_[0], "\n"; 
     213        $_[0]; 
     214    } 
     215 
     216    sub get_zygs { my $self = shift; 
    210217        my @zygs; 
    211218        if ($self->{zygs}) { 
    212219            my $zygs = $self->{zygs}; 
    213220            for my $key (sort {$zygs->{$a} <=> $zygs->{$b}} keys %$zygs) { 
    214                 my $part = $self->{$key}; 
    215                 if (ref $part eq 'ARRAY') { 
    216                     my @kids = @$part; 
    217                     for my $kid (@kids) { 
    218                         push @zygs, $kid->emit_p6 // ''; 
    219                     } 
    220                 } 
    221                 elsif (ref $part) { 
    222                     push @zygs, $part->emit_p6 // ''; 
     221                push @zygs, $self->get_zyg($key); 
     222            } 
     223        } 
     224        @zygs; 
     225    } 
     226 
     227    sub get_zyg { my $self = shift; 
     228        my $key = shift; 
     229        my $part = $self->{$key}; 
     230        my @zygs; 
     231        if (ref $part eq 'ARRAY') { 
     232            my @kids = @$part; 
     233            for my $kid (@kids) { 
     234                if (ref $kid) { 
     235                    push @zygs, $kid->emit_p6 // ''; 
    223236                } 
    224237                else { 
    225                     push @zygs, $key . '=' . $part; 
    226                 } 
    227             } 
     238                    push @zygs, $kid; 
     239                } 
     240            } 
     241        } 
     242        elsif (ref $part) { 
     243            push @zygs, $part->emit_p6 // ''; 
     244        } 
     245        else { 
     246            push @zygs, $key . '=' . $part; 
    228247        } 
    229248        @zygs; 
    230249    } 
    231 } 
    232  
     250 
     251    sub get_ws { my $self = shift; 
     252        my $ws = $self->{WS} // 0; 
     253        my $pos = $self->{POS}; 
     254        if ($ws and not $did_ws{$pos}++) { 
     255            substr($ORIG, $pos - $ws, $ws) 
     256        } 
     257        else { 
     258            ''; 
     259        } 
     260    } 
     261         
     262} 
     263 
     264{ package VAST::sample; our @ISA = 'VAST::Base'; 
     265    sub emit_p6 { my $self = shift; 
     266    } 
     267} 
     268 
     269{ package VAST::AddSym; our @ISA = 'VAST::Base'; 
     270    sub emit_p6 { my $self = shift; 
     271        my $text = $self->get_ws; 
     272        $text .= $self->{sym}; 
     273        $text .= $self->SUPER::emit_p6(@_); 
     274        $text; 
     275    } 
     276} 
     277 
     278{ package VAST::comp_unit; our @ISA = 'VAST::Base'; 
     279    sub emit_p6 { my $self = shift; 
     280        my $text = $self->SUPER::emit_p6(@_); 
     281        my $finalws = $$POS[-1]{ws}; 
     282        $text .= substr($ORIG, $finalws, -1) if $finalws; 
     283        $self->ret($text); 
     284    } 
     285} 
     286 
     287{ package VAST::Comma; our @ISA = 'VAST::Base'; 
     288    sub emit_p6 { my $self = shift; 
     289        my $text = $self->get_ws; 
     290         
     291        my @list = $self->get_zyg('list'); 
     292        my @delims = $self->get_zyg('delims'); 
     293        while (@list) { 
     294            $text .= shift(@list) . (shift(@delims)//''); 
     295        } 
     296        $self->ret($text); 
     297    } 
     298} 
     299 
     300{ package VAST::statementlist; our @ISA = 'VAST::Base'; 
     301    sub emit_p6 { my $self = shift; 
     302        my $text = $self->get_ws; 
     303         
     304        my @statement = $self->get_zyg('statement'); 
     305        my @terminator = $self->get_zyg('eat_terminator'); 
     306        while (@statement or @terminator) { 
     307            $text .= shift(@statement) . (shift(@terminator)//''); 
     308        } 
     309        $self->ret($text); 
     310    } 
     311} 
     312 
     313{ package VAST::nibbler; our @ISA = 'VAST::Base'; 
     314    sub emit_p6 { my $self = shift; 
     315        my $text = ''; 
     316        my @nibbles = $self->get_zyg('nibbles'); 
     317        for my $nibble (@nibbles) { 
     318            if (ref $nibble) { 
     319                $text .= $nibble->emit_p6; 
     320            } 
     321            else { 
     322                $text .= $nibble; 
     323            } 
     324        } 
     325        $self->ret($text); 
     326    } 
     327} 
     328 
     329{ package VAST::quibble; our @ISA = 'VAST::Base'; 
     330    sub emit_p6 { my $self = shift; 
     331        my $text = $self->get_ws; 
     332         
     333        my @babble = @{$self->{babble}{B}}; 
     334        my @nibble = $self->get_zyg('nibble'); 
     335        $text .= $babble[0] . $nibble[0] . $babble[1]; 
     336        $self->ret($text); 
     337    } 
     338} 
     339 
     340{ package VAST::quote; our @ISA = 'VAST::Base'; 
     341    sub emit_p6 { my $self = shift; 
     342        my $text = $self->get_ws; 
     343         
     344        if ($self->{nibble}) { 
     345            my @nibble = $self->get_zyg('nibble'); 
     346            $text .= $self->{sym}[0] . $nibble[0] . $self->{sym}[1]; 
     347        } 
     348        else {   
     349            my @quibble = $self->get_zyg('quibble'); 
     350            $text .= $self->{sym} . $quibble[0]; 
     351        } 
     352        $self->ret($text); 
     353    } 
     354} 
     355 
     356{ package VAST::statement_control; our @ISA = 'VAST::AddSym'; } 
     357{ package VAST::version; our @ISA = 'VAST::AddSym'; } 
    233358 
    234359if ($0 eq __FILE__) {