Changeset 7242 for lang/perl/R-Writer
- Timestamp:
- 02/28/08 17:37:53 (5 years ago)
- Location:
- lang/perl/R-Writer/trunk
- Files:
-
- 2 added
- 3 modified
-
lib/R/Writer.pm (modified) (8 diffs)
-
lib/R/Writer/Call.pm (added)
-
lib/R/Writer/Var.pm (modified) (2 diffs)
-
t/02_simple.t (modified) (3 diffs)
-
t/03_assign.t (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/R-Writer/trunk/lib/R/Writer.pm
r7162 r7242 5 5 use warnings; 6 6 use base qw(Class::Accessor::Fast); 7 use R::Writer::Call; 7 8 use R::Writer::Encoder; 8 9 use R::Writer::Range; … … 21 22 sub append { 22 23 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 } 32 27 33 28 sub R … … 61 56 { 62 57 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], 68 61 end_of_call_chain => ! defined wantarray 69 }; 62 ); 63 push @{$self->{statements}}, $call; 70 64 return $self; 71 65 } … … 80 74 } 81 75 76 sub 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 87 sub 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 82 105 sub obj_as_string 83 106 { … … 87 110 88 111 if ($ref eq 'CODE') { 89 return $self-> function($obj);112 return $self->obj_as_string($obj->()); 90 113 } 91 114 elsif ($ref =~ /^R::Writer/) { 92 return $obj->as_string 115 return $obj->as_string($self); 93 116 } 94 117 elsif ($ref eq "SCALAR") { … … 114 137 } 115 138 116 sub as_string117 {118 my $self = shift;119 my $ret = "";120 121 for my $s (@{$self->{statements}}) {122 # If {call} is present, then this is a function call123 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 } @$args133 ) . ")" . $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 144 139 sub var 145 140 { … … 147 142 148 143 my $obj = R::Writer::Var->new($var, $value, $self); 149 $self->append($obj->as_string( ));144 $self->append($obj->as_string($self)); 150 145 return $self; 151 146 } … … 206 201 fiddle with DB package on my own. gugod++) 207 202 203 =head1 METHODS 204 205 =head2 expression | expr 206 207 The expression() method creates an mathematical expression, which can be 208 assigned to a variable. Expressions that are not assigned to a variable is 209 pretty useless, so when using expression(), you need to pass the result 210 to some other method that can accept it: 211 212 $R->var(y => $R->expression('a * x ^ 2 + b * x + 1')); 213 214 The above yields 215 216 y <- expression("a * x ^ 2 + b * x + 1"); 217 208 218 =head1 TODO 209 219 -
lang/perl/R-Writer/trunk/lib/R/Writer/Var.pm
r7128 r7242 18 18 { 19 19 my $self = shift; 20 my $c = shift; 20 21 my $var = $self->name; 21 22 my $value = $self->value; … … 34 35 } 35 36 elsif ($ref eq 'CODE') { 36 $s = "$var <- " . $ self->function($value);37 $s = "$var <- " . $c->obj_as_string($value->()); 37 38 } 38 39 elsif ($ref =~ /^R::Writer/) { 39 $s = "$var <- " . $value->as_string( );40 $s = "$var <- " . $value->as_string($c); 40 41 } 41 42 elsif ($ref eq 'REF') { -
lang/perl/R-Writer/trunk/t/02_simple.t
r7128 r7242 11 11 my $R = R::Writer->new(); 12 12 $R->call("demo", "plotmath"); 13 like($R->as_string, qr/demo\("plotmath"\); \n/);13 like($R->as_string, qr/demo\("plotmath"\);/); 14 14 } 15 15 … … 35 35 my $y = $R->var(y => 11); 36 36 $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);/); 38 38 } 39 39 … … 48 48 ->call(cat => \'y') 49 49 ; 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);/); 51 51 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)