Changeset 7242 for lang/perl/R-Writer

Show
Ignore:
Timestamp:
02/28/08 17:37:53 (7 years ago)
Author:
daisuke
Message:

updates

Location:
lang/perl/R-Writer/trunk
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/R-Writer/trunk/lib/R/Writer.pm

    r7162 r7242  
    55use warnings; 
    66use base qw(Class::Accessor::Fast); 
     7use R::Writer::Call; 
    78use R::Writer::Encoder; 
    89use R::Writer::Range; 
     
    2122sub append { 
    2223    my $self = shift; 
    23  
    24     if ( R::Writer::Util::__IN_RWRITER_PACKAGES__ ) { 
    25         push @{ $self->{statements} }, { code => shift }; 
    26         return $self; 
    27     } 
    28  
    29     return $self->call("append", @_); 
    30 } 
    31  
     24    push @{ $self->{statements} }, { code => shift }; 
     25    return $self; 
     26} 
    3227 
    3328sub R 
     
    6156{ 
    6257    my ($self, $function, @args) = @_; 
    63  
    64     push @{$self->{statements}}, { 
    65         object => delete $self->{object} || undef, 
    66         call   => $function, 
    67         args   => \@args, 
     58    my $call = R::Writer::Call->new( 
     59        call => $function, 
     60        args => [@args], 
    6861        end_of_call_chain => ! defined wantarray 
    69     }; 
     62    ); 
     63    push @{$self->{statements}}, $call; 
    7064    return $self; 
    7165} 
     
    8074} 
    8175 
     76sub expression 
     77{ 
     78    my ($self, $expr) = @_; 
     79    return R::Writer::Call->new( 
     80        call => 'expression', 
     81        args => [ $expr ], 
     82        end_of_call_chain => 1, 
     83    ); 
     84} 
     85*expr = \&expression; 
     86 
     87sub as_string 
     88{ 
     89    my $self = shift; 
     90    my $ret = ""; 
     91 
     92    for my $s (@{$self->{statements}}) { 
     93        if (eval { $s->isa('R::Writer::Call') }) { 
     94            $ret .= $s->as_string($self); 
     95        } 
     96        elsif (my $c = $s->{code}) { 
     97            my $delimiter = defined $s->{delimiter}  ? $s->{delimiter} : ";"; 
     98            $c .= $delimiter unless $c =~ /$delimiter\s*$/s; 
     99            $ret .= $c ."\n"; 
     100        } 
     101    } 
     102    return $ret; 
     103} 
     104 
    82105sub obj_as_string 
    83106{ 
     
    87110 
    88111    if ($ref eq 'CODE') { 
    89         return $self->function($obj); 
     112        return $self->obj_as_string($obj->()); 
    90113    } 
    91114    elsif ($ref =~ /^R::Writer/) { 
    92         return $obj->as_string 
     115        return $obj->as_string($self); 
    93116    } 
    94117    elsif ($ref eq "SCALAR") { 
     
    114137} 
    115138 
    116 sub as_string 
    117 { 
    118     my $self = shift; 
    119     my $ret = ""; 
    120  
    121     for my $s (@{$self->{statements}}) { 
    122         # If {call} is present, then this is a function call 
    123         if (my $f = $s->{call}) { 
    124             my $delimiter = 
    125                 defined($s->{delimiter}) ? $s->{delimiter} : ($s->{end_of_call_chain} ? ";" : "."); 
    126             my $args = $s->{args}; 
    127             $ret .= ($s->{object} ? "$s->{object}." : "" ) . 
    128                 "$f(" . 
    129                     join(",", 
    130                          map { 
    131                              $self->obj_as_string( $_ ); 
    132                          } @$args 
    133                      ) . ")" . $delimiter . "\n" 
    134         } 
    135         elsif (my $c = $s->{code}) { 
    136             my $delimiter = defined $s->{delimiter}  ? $s->{delimiter} : ";"; 
    137             $c .= $delimiter unless $c =~ /$delimiter\s*$/s; 
    138             $ret .= $c ."\n"; 
    139         } 
    140     } 
    141     return $ret; 
    142 } 
    143  
    144139sub var 
    145140{ 
     
    147142 
    148143    my $obj = R::Writer::Var->new($var, $value, $self); 
    149     $self->append($obj->as_string()); 
     144    $self->append($obj->as_string($self)); 
    150145    return $self; 
    151146} 
     
    206201fiddle with DB package on my own. gugod++) 
    207202 
     203=head1 METHODS 
     204 
     205=head2 expression | expr 
     206 
     207The expression() method creates an mathematical expression, which can be 
     208assigned to a variable. Expressions that are not assigned to a variable is 
     209pretty useless, so when using expression(), you need to pass the result 
     210to some other method that can accept it: 
     211 
     212  $R->var(y => $R->expression('a * x ^ 2 + b * x + 1')); 
     213 
     214The above yields 
     215 
     216  y <- expression("a * x ^ 2 + b * x + 1"); 
     217 
    208218=head1 TODO 
    209219 
  • lang/perl/R-Writer/trunk/lib/R/Writer/Var.pm

    r7128 r7242  
    1818{ 
    1919    my $self  = shift; 
     20    my $c     = shift; 
    2021    my $var   = $self->name; 
    2122    my $value = $self->value; 
     
    3435    } 
    3536    elsif ($ref eq 'CODE') { 
    36         $s = "$var <- " . $self->function($value); 
     37        $s = "$var <- " . $c->obj_as_string($value->()); 
    3738    } 
    3839    elsif ($ref =~ /^R::Writer/) { 
    39         $s = "$var <- " . $value->as_string(); 
     40        $s = "$var <- " . $value->as_string($c); 
    4041    } 
    4142    elsif ($ref eq 'REF') { 
  • lang/perl/R-Writer/trunk/t/02_simple.t

    r7128 r7242  
    1111    my $R = R::Writer->new(); 
    1212    $R->call("demo", "plotmath"); 
    13     like($R->as_string, qr/demo\("plotmath"\);\n/); 
     13    like($R->as_string, qr/demo\("plotmath"\);/); 
    1414} 
    1515 
     
    3535    my $y = $R->var(y => 11); 
    3636    $R->call(c => \("x", "y")); 
    37     is($R->as_string, qq/x <- 11;\ny <- 11;\nc(x,y);\n/); 
     37    is($R->as_string, qq/x <- 11;\ny <- 11;\nc(x,y);/); 
    3838} 
    3939 
     
    4848         ->call(cat => \'y') 
    4949    ; 
    50     is( $R->as_string, qq/x <- 1;\ny <- x + 1;\ncat(y);\n/); 
     50    is( $R->as_string, qq/x <- 1;\ny <- x + 1;\ncat(y);/); 
    5151}