LispPerl/Evaler.pm

filter: Statementstmt 84.1% 439 / 522 Branchbran 70.9% 176 / 248 Conditioncond 75.2% 85 / 113 MC/DCmcdc 45.3% 29 / 64 Subroutinesub 98.1% 52 / 53 Podpod 38.2% 13 / 34 totaltotal 76.7% 794 / 1034 CC 181 SCAR 41.2
CC
181
Cov
79%
perlfunc_call
164.0
_eval
102.1
bind
36.3
CRAP 61.8
↑ Summary LispPerl/Printer.pm »
1 package Language::LispPerl::Evaler;
2 6 $Language::LispPerl::Evaler::VERSION = '0.007';
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Language::LispPerl::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Language::LispPerl::Var;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Language::LispPerl::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Language::LispPerl::BuiltIns;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶BEGIN{
Subroutine BEGIN: called CC=2 SCAR=7.9
17 # The test compatible File::Share
18 6 eval{ require File::Share; File::Share->import('dist_dir'); };
19 6 ▶ if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
20 # The production only File::ShareDir
21 0 require File::ShareDir;
22 0 File::ShareDir->import('dist_dir');
23 }
24 };
25  
26  
27 6 our $namespace_key = "0namespace0";
28  
29 9 ▶has 'scopes' => ( is => 'ro', default => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
30 9 return [ { $namespace_key => [] } ]
31 6 });
32 9 ▶has 'loaded_files' => ( is => 'ro', default => sub{ {}; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 9 ▶has 'file_stack' => ( is => 'ro', default => sub{ []; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
35 9 ▶has 'caller' => ( is => 'ro' , default => sub{ []; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 6 has 'quotation_scope' => ( is => 'ro', default => 0 );
38 6 has 'syntaxquotation_scope' => ( is => 'ro', default => 0 );
39  
40 6 has 'exception' => ( is => 'rw' );
41  
42 # The container for the builtin functions.
43 6 has 'builtins' => ( is => 'ro', lazy_build => 1 );
44  
45 8 ▶sub _build_builtins{
Subroutine _build_builtins: called CC=1 SCAR=0.0
46 8 my ($self) = @_;
47 8 return Language::LispPerl::BuiltIns->new({ evaler => $self });
48 }
49  
50 4 ▶sub to_hash{
Subroutine to_hash: called CC=1 SCAR=0.0
Pod undocumented
51 4 my ($self) = @_;
52 return {
53 4 'scopes' => Language::LispPerl::Printer::to_perl( $self->scopes() ),
54 'loaded_files' => Language::LispPerl::Printer::to_perl( $self->loaded_files() ),
55 'file_stack' => Language::LispPerl::Printer::to_perl( $self->file_stack() ),
56 'caller' => Language::LispPerl::Printer::to_perl( $self->caller() ),
57 'quotation_scope' => Language::LispPerl::Printer::to_perl( $self->quotation_scope() ),
58 'syntaxquotation_scope' => Language::LispPerl::Printer::to_perl( $self->syntaxquotation_scope() ),
59 'exception' => Language::LispPerl::Printer::to_perl( $self->exception() ),
60 __class => $self->blessed(),
61 };
62 }
63  
64 2 ▶sub from_hash{
Subroutine from_hash: called CC=1 SCAR=0.0
Pod undocumented
65 2 my ($class, $hash) = @_;
66 return $class->new({
67 2 map { $_ => Language::LispPerl::Reader::from_perl( $hash->{$_} ) } keys %$hash
68 });
69 }
70  
71 =head2 new_instance
72  
73 Returns a new instance of this with the same builtins and everything else reset.
74  
75 Usage:
76  
77 my $other_evaler = $this->new_instance();
78  
79 =cut
80  
81 1 ▶sub new_instance{
Subroutine new_instance: called CC=1 SCAR=0.0
Pod documented
82 1 my ($self) = @_;
83 1 return ref($self)->new( { builtins => $self->builtins() } );
84 }
85  
86 5 ▶sub clear_exception{
Subroutine clear_exception: called CC=1 SCAR=0.0
Pod undocumented
87 5 my ($self) = @_;
88 5 $self->{exception} = undef;
89 }
90  
91 148 ▶sub push_scope {
Subroutine push_scope: called CC=2 SCAR=6.9
Pod undocumented
92 148 my $self = shift;
93 148 ▶ my $context = shift() || confess("Cannot push untrue context");
Condition50%1/2
shift() || confess('Cannot push untrue context')
l!l
1480
MC/DC0%0/1
shift() || confess('Cannot push untrue context')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
94 148 my %c = %{$context};
95 148 my @ns = @{ $c{$namespace_key} };
96 148 $c{$namespace_key} = \@ns;
97 148 unshift @{ $self->scopes() }, \%c;
98 }
99  
100 147 ▶sub pop_scope {
Subroutine pop_scope: called CC=1 SCAR=0.0
Pod undocumented
101 147 my $self = shift;
102 147 shift @{ $self->scopes() };
103 }
104  
105 3145 ▶sub current_scope {
Subroutine current_scope: called CC=1 SCAR=0.0
Pod undocumented
106 3145 my $self = shift;
107 3145 my $scope = @{ $self->scopes() }[0];
108 3145 return $scope;
109 }
110  
111 148 ▶sub push_caller {
Subroutine push_caller: called CC=1 SCAR=0.0
Pod undocumented
112 148 my $self = shift;
113 148 my $ast = shift;
114 148 unshift @{ $self->caller() }, $ast;
115 }
116  
117 148 ▶sub pop_caller {
Subroutine pop_caller: called CC=1 SCAR=0.0
Pod undocumented
118 148 my $self = shift;
119 148 shift @{ $self->caller() };
120 }
121  
122 10 ▶sub caller_size {
Subroutine caller_size: called CC=1 SCAR=0.0
Pod undocumented
123 10 my $self = shift;
124 10 scalar @{ $self->caller() };
125 }
126  
127 =head2 copy_caller
128  
129 Returns a shallow copy of this caller's stack.
130  
131 Usage:
132  
133 my $caller_stack = $this->copy_caller();
134  
135 =cut
136  
137 6 ▶sub copy_caller{
Subroutine copy_caller: called CC=1 SCAR=0.0
Pod documented
138 6 my ($self) = @_;
139 6 return [ @{ $self->caller() } ];
140 }
141  
142 =head2 copy_current_scope
143  
144 Take a shallow copy of the current scope that
145 is adequate for function and macro contexts
146  
147 =cut
148  
149 112 ▶sub copy_current_scope{
Subroutine copy_current_scope: called CC=1 SCAR=0.0
Pod documented
150 112 my ($self) = @_;
151 # Take a shallow copy of the current_scope
152 112 my %c = %{ $self->current_scope() };
153  
154 # Take a shallow copy of the namespace (keyed by namespace_key)
155 112 my @ns = @{ $c{$namespace_key} };
156 112 $c{$namespace_key} = \@ns;
157  
158 112 return \%c;
159 }
160  
161 5 ▶sub push_namespace {
Subroutine push_namespace: called CC=1 SCAR=0.0
Pod undocumented
162 5 my $self = shift;
163 5 my $namespace = shift;
164 5 my $scope = $self->current_scope();
165 5 unshift @{ $scope->{$namespace_key} }, $namespace;
166 }
167  
168 5 ▶sub pop_namespace {
Subroutine pop_namespace: called CC=1 SCAR=0.0
Pod undocumented
169 5 my $self = shift;
170 5 my $scope = $self->current_scope();
171 5 shift @{ $scope->{$namespace_key} };
172 }
173  
174 1869 ▶sub current_namespace {
Subroutine current_namespace: called CC=2 SCAR=6.9
Pod undocumented
175 1869 my $self = shift;
176 1869 my $scope = $self->current_scope();
177 1869 my $namespace = @{ $scope->{$namespace_key} }[0];
178 1869 ▶ return "" if ( !defined $namespace );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespace 1810 59 1869
179 59 return $namespace;
180 }
181  
182 =head2 new_var
183  
184 From a name and a value, creates a new L<Language::LispPerl::Var> under the
185 key 'name' in $this->current_scope();
186  
187 Usage:
188  
189 $this->new_var( 'bla' , 1 );
190  
191 =cut
192  
193 397 ▶sub new_var {
Subroutine new_var: called CC=1 SCAR=0.0
Pod documented
194 397 my $self = shift;
195 397 my $name = shift;
196 397 my $value = shift;
197 397 my $scope = $self->current_scope();
198 397 $name = $self->current_namespace() . "#" . $name;
199 397 $scope->{$name} = Language::LispPerl::Var->new({ name => $name, value => $value });
200 }
201  
202 =head2 var
203  
204 Lookup the L<Language::LispPerl::Var> by name in the current scope or in the current namespace.
205 Returns undef if no such variable is found.
206  
207 Usage:
208  
209 if( my $var = $this->var( 'blabla' ) ){
210 ...
211 }
212  
213 =cut
214  
215 744 ▶sub var {
Subroutine var: called CC=4 SCAR=14.0
Pod documented
216 744 my $self = shift;
217 744 my $name = shift;
218 744 my $scope = $self->current_scope();
219 744 ▶ if ( exists $scope->{$name} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $scope->{$name}) { } 6 738 744
elsif (exists $$scope{$self->current_namespace . '#' . $name}) { } 732 6 738
elsif (exists $$scope{'#' . $name}) { } 6 0 6
220 6 return $scope->{$name};
221 }
222 elsif ( exists $scope->{ $self->current_namespace() . "#" . $name } ) {
223 732 return $scope->{ $self->current_namespace() . "#" . $name };
224 }
225 elsif ( exists $scope->{ "#" . $name } ) {
226 6 return $scope->{ "#" . $name };
227 }
228 0 return undef;
229 }
230  
231 =head2 current_file
232  
233 Returns the current file on the file_stack or '.' if no such thing
234 exists.
235  
236 =cut
237  
238 0 ▶sub current_file {
Subroutine current_file: not called CC=2 SCAR=17.9
Pod documented
239 0 my $self = shift;
240 0 my $sd = scalar @{ $self->{file_stack} };
241 0 ▶ if ( $sd == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sd == 0) { } 0 0 0
242 0 return ".";
243 }
244 else {
245 0 return ${ $self->{file_stack} }[ $sd - 1 ];
246 }
247 }
248  
249 =head2 search_file
250  
251 Looks up the given file name (fully qualified, with clp extension or not)
252 in this package's share directory or in @INC.
253  
254 dies with an error if no file can be found.
255  
256 Usage:
257  
258 $this->search_file('core');
259  
260 =cut
261  
262 10 ▶sub search_file {
Subroutine search_file: called CC=6 SCAR=18.2
Pod documented
263 10 my $self = shift;
264 10 my $file = shift;
265  
266 10 my $dist_dir = dist_dir( 'Language-LispPerl' );
267 10 $log->debug("Using dist dir = $dist_dir");
268  
269 10 foreach my $ext ( '', '.clp' ) {
270 15 ▶ if ( -f "$file$ext" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f "$file$ext") { } 2 13 15
elsif (-f $dist_dir . "/lisp/$file$ext") { } 8 5 13
271 2 return "$file$ext";
272 }
273 elsif ( -f $dist_dir. "/lisp/$file$ext" ) {
274 8 return $dist_dir . "/lisp/$file$ext";
275 }
276 5 foreach my $p (@INC) {
277 40 ▶ if ( -f "$p/$file$ext" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$p/$file$ext") 0 40 40
278 0 return "$p/$file$ext";
279 }
280 }
281 }
282 0 Language::LispPerl::Logger::error( "cannot find " . $file );
283 }
284  
285 =head2 load
286  
287 Reads a file once if it hasn't been read before, for loading
288 libraries in the global scope.
289  
290 Usage:
291  
292 $this->load(/path/to/file.clp');
293  
294 =cut
295  
296 10 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod documented
297 10 my $self = shift;
298 10 my $file = shift;
299  
300 Language::LispPerl::Logger::error(
301 "cannot require file " . $file . " in non-global scope" )
302 10 ▶ if scalar @{ $self->scopes() } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->scopes;} > 1 0 10 10
303  
304 10 $file = File::Spec->rel2abs( $self->search_file($file) );
305  
306 10 ▶ return 1 if exists $self->{loaded_files}->{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"loaded_files"}{$file} 2 8 10
307 8 $self->{loaded_files}->{$file} = 1;
308 8 push @{ $self->{file_stack} }, $file;
309 8 my $res = $self->read($file);
310 8 pop @{ $self->{file_stack} };
311 8 return $res;
312 }
313  
314 =head2 read
315  
316 Reads and evaluates in this evaler
317 all the expressions in the given filename
318 and returns the last evaluation result.
319  
320 Usage:
321  
322 $this->read('/path/to/file.clp');
323  
324 =cut
325  
326 9 ▶sub read {
Subroutine read: called CC=1 SCAR=0.0
Pod documented
327 9 my $self = shift;
328 9 my $file = shift;
329 9 my $reader = Language::LispPerl::Reader->new();
330 9 $reader->read_file($file);
331 9 my $res = undef;
332 9 ▶ $reader->ast()->each( sub { $res = $self->_eval( $_[0] ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
333 9 return $res;
334 }
335  
336 6 our $empty_list = Language::LispPerl::Seq->new({ type => "list" });
337 6 our $true = Language::LispPerl::Atom->new({type => "bool", value => "true" });
338 6 our $false = Language::LispPerl::Atom->new({type => "bool", value => "false"});
339 6 our $nil = Language::LispPerl::Atom->new({type => "nil", value => "nil"});
340  
341 49 ▶sub true{ return $true; }
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
342 30 ▶sub false{ return $false; }
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
343 135 ▶sub nil{ return $nil; }
Subroutine nil: called CC=1 SCAR=0.0
Pod undocumented
344 1 ▶sub empty_list{ return $empty_list; }
Subroutine empty_list: called CC=1 SCAR=0.0
Pod undocumented
345  
346 =head2 eval
347  
348 Evaluates a string and returns the result of the latest expression (or dies
349 with an error).
350  
351 Return the nil/nil atom when the given string is empty.
352  
353 Usage:
354  
355 my $res = $this->eval(q|( - 1 1 ) ( + 1 2 )|);
356 # $res->value() is 3
357  
358 =cut
359  
360 106 ▶sub eval {
Subroutine eval: called CC=3 SCAR=11.0
Pod documented
361 106 my ($self, $str) = @_;
362 106 ▶ unless( length( defined( $str ) ? $str : '' ) ){
Branch: 2 branches
BranchTrueFalseTotal
unless (length(defined $str ? $str : "")) 1 105 106
defined $str ? : 105 1 106
363 1 return $nil;
364 }
365  
366 105 my $reader = Language::LispPerl::Reader->new();
367 105 $reader->read_string($str);
368 105 my $res = undef;
369 105 ▶ $reader->ast()->each( sub { $res = $self->_eval( $_[0] ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
370 103 return $res;
371 }
372  
373  
374 =head2 bind
375  
376 Associate the current L<Language::LispPerl::Atom> or L<Language::LispPerl::Seq>
377 with the correct Perl/Lisp space values.
378  
379 =cut
380  
381 1760 ▶sub bind {
Subroutine bind: called CC=36 SCAR=35.9
Pod documented
382 1760 my $self = shift;
383 1760 my $ast = shift;
384 1760 my $class = $ast->class();
385 1760 my $type = $ast->type();
386 1760 my $value = $ast->value();
387 1760 ▶ if ( $type eq "symbol" and $value eq "true" ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type eq 'symbol' and $value eq 'true') { } 21 1739 1760
elsif ($type eq 'symbol' and $value eq 'false') { } 14 1725 1739
elsif ($type eq 'symbol' and $value eq 'nil') { } 1 1724 1725
elsif ($type eq 'accessor') { } 3 1721 1724
elsif ($type eq 'sender') { } 0 1721 1721
elsif ($type eq 'syntaxquotation' or $type eq 'quotation') { } 110 1611 1721
elsif ($type eq 'symbol' and $self->{'syntaxquotation_scope'} == 0 and $self->{'quotation_scope'} == 0 or $type eq 'dequotation' and $self->{'syntaxquotation_scope'} > 0) { } 1277 334 1611
elsif ($type eq 'symbol' and $self->{'quotation_scope'} > 0) { } 48 286 334
elsif ($class eq 'Seq') { } 138 148 286
Condition100%3/3
$type eq "symbol" and $value eq "true"
!ll&&!rl&&r
424131521
MC/DC100%2/2
$type eq "symbol" and $value eq "true"
$type eq "symbol" $value eq "true"
Truth table100%3/3
A and B
A= $type eq "symbol"B= $value eq "true"
ABresult
0X0
100
111
388 21 return $true;
389 }
390 ▶ elsif ( $type eq "symbol" and $value eq "false" ) {
Condition100%3/3
$type eq "symbol" and $value eq "false"
!ll&&!rl&&r
424130114
MC/DC100%2/2
$type eq "symbol" and $value eq "false"
$type eq "symbol" $value eq "false"
Truth table100%3/3
A and B
A= $type eq "symbol"B= $value eq "false"
ABresult
0X0
100
111
391 14 return $false;
392 }
393 ▶ elsif ( $type eq "symbol" and $value eq "nil" ) {
Condition100%3/3
$type eq "symbol" and $value eq "nil"
!ll&&!rl&&r
42413001
MC/DC100%2/2
$type eq "symbol" and $value eq "nil"
$type eq "symbol" $value eq "nil"
Truth table100%3/3
A and B
A= $type eq "symbol"B= $value eq "nil"
ABresult
0X0
100
111
394 1 return $nil;
395 }
396 elsif ( $type eq "accessor" ) {
397 3 return Language::LispPerl::Atom->new({ type => "accessor", value => $self->bind($value) } );
398 }
399 elsif ( $type eq "sender" ) {
400 0 return Language::LispPerl::Atom->new({ type => "sender", value => $self->bind($value) });
401 }
402 ▶ elsif ( $type eq "syntaxquotation" or $type eq "quotation" ) {
Condition100%3/3
$type eq "syntaxquotation" or $type eq "quotation"
l!l&&r!l&&!r
71391611
MC/DC100%2/2
$type eq "syntaxquotation" or $type eq "quotation"
$type eq "syntaxquotation" $type eq "quotation"
Truth table100%3/3
A or B
A= $type eq "syntaxquotation"B= $type eq "quotation"
ABresult
1X1
011
000
403 110 ▶ $self->{syntaxquotation_scope} += 1 if $type eq "syntaxquotation";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "syntaxquotation" 71 39 110
404 110 ▶ $self->{quotation_scope} += 1 if $type eq "quotation";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "quotation" 39 71 110
405 110 my $r = $self->bind($value);
406 110 ▶ $self->{syntaxquotation_scope} -= 1 if $type eq "syntaxquotation";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "syntaxquotation" 71 39 110
407 110 ▶ $self->{quotation_scope} -= 1 if $type eq "quotation";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "quotation" 39 71 110
408 110 return $r;
409 }
410 ▶ elsif (
Condition91%11/12
$type eq "symbol" and $self->{"syntaxquotation_scope"} == 0 and $self->{"quotation_scope"} == 0 or $type eq "dequotation" and $self->{"syntaxquotation_scope"} > 0
l!l&&r!l&&!r
1119158334
$type eq "symbol" and $self->{"syntaxquotation_scope"} == 0 and $self->{"quotation_scope"} == 0
!ll&&!rl&&r
444481119
$type eq "symbol" and $self->{"syntaxquotation_scope"} == 0
!ll&&!rl&&r
3111331167
$type eq "dequotation" and $self->{"syntaxquotation_scope"} > 0
!ll&&!rl&&r
3340158
MC/DC80%4/5
$type eq "symbol" and $self->{"syntaxquotation_scope"} == 0 and $self->{"quotation_scope"} == 0 or $type eq "dequotation" and $self->{"syntaxquotation_scope"} > 0
$type eq "symbol" $self->{"syntaxquotation_scope"} == 0 $self->{"quotation_scope"} == 0 $type eq "dequotation" $self->{"syntaxquotation_scope"} > 0
Truth table50%5/10
A and B and C or D and E
A= $type eq "symbol"B= $self->{"syntaxquotation_scope"} == 0C= $self->{"quotation_scope"} == 0D= $type eq "dequotation"E= $self->{"syntaxquotation_scope"} > 0
ABCDEresult
111XX1
0XX111
10X111
110111
0XX0X0
0XX100
10X0X0
10X100
1100X0
110100
411 (
412 $type eq "symbol" and $self->{syntaxquotation_scope} == 0
413 and $self->{quotation_scope} == 0
414 )
415 or ( $type eq "dequotation" and $self->{syntaxquotation_scope} > 0 )
416 )
417 {
418 $ast->error("dequotation should be in syntax quotation scope")
419 1277 ▶ if ( $type eq "dequotation" and $self->{syntaxquotation_scope} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "dequotation" and $self->{"syntaxquotation_scope"} == 0 0 1277 1277
Condition66%2/3
$type eq "dequotation" and $self->{"syntaxquotation_scope"} == 0
!ll&&!rl&&r
11191580
MC/DC0%0/2
$type eq "dequotation" and $self->{"syntaxquotation_scope"} == 0
$type eq "dequotation" $self->{"syntaxquotation_scope"} == 0
Truth table66%2/3
A and B
A= $type eq "dequotation"B= $self->{"syntaxquotation_scope"} == 0
ABresult
0X0
100
111
420 1277 my $name = $value;
421 1277 ▶ if ( $type eq "dequotation" and $value =~ /^@(\S+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "dequotation" and $value =~ /^@(\S+)$/) 50 1227 1277
Condition100%3/3
$type eq "dequotation" and $value =~ /^@(\S+)$/
!ll&&!rl&&r
111910850
MC/DC100%2/2
$type eq "dequotation" and $value =~ /^@(\S+)$/
$type eq "dequotation" $value =~ /^@(\S+)$/
Truth table100%3/3
A and B
A= $type eq "dequotation"B= $value =~ /^@(\S+)$/
ABresult
0X0
100
111
422 50 $name = $1;
423 }
424 1277 ▶ return $ast
Branch: 1 branch
BranchTrueFalseTotal
if $self->word_is_reserved($name) 705 572 1277
425 if $self->word_is_reserved( $name );
426 572 my $var = $self->var($name);
427 572 ▶ $ast->error("unbound symbol '$name'") if !defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 0 572 572
428 572 return $var->value();
429 }
430 ▶ elsif ( $type eq "symbol"
Condition100%3/3
$type eq "symbol" and $self->{"quotation_scope"} > 0
!ll&&!rl&&r
15313348
MC/DC100%2/2
$type eq "symbol" and $self->{"quotation_scope"} > 0
$type eq "symbol" $self->{"quotation_scope"} > 0
Truth table100%3/3
A and B
A= $type eq "symbol"B= $self->{"quotation_scope"} > 0
ABresult
0X0
100
111
431 and $self->{quotation_scope} > 0 )
432 {
433 48 my $q = Language::LispPerl::Atom->new({ type => "quotation", value => $value });
434 48 return $q;
435 }
436 elsif ( $class eq "Seq" ) {
437 138 ▶ return $empty_list if $type eq "list" and $ast->size() == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "list" and $ast->size == 0 2 136 138
Condition100%3/3
$type eq "list" and $ast->size == 0
!ll&&!rl&&r
51312
MC/DC100%2/2
$type eq "list" and $ast->size == 0
$type eq "list" $ast->size == 0
Truth table100%3/3
A and B
A= $type eq "list"B= $ast->size == 0
ABresult
0X0
100
111
438 136 my $list = Language::LispPerl::Seq->new({ type => "list" });
439 136 $list->type($type);
440 136 foreach my $i ( @{$value} ) {
441 383 ▶ if ( $i->type() eq "dequotation" and $i->value() =~ /^@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i->type eq 'dequotation' and $i->value =~ /^@/) { } 50 333 383
Condition100%3/3
$i->type eq "dequotation" and $i->value =~ /^@/
!ll&&!rl&&r
22710650
MC/DC100%2/2
$i->type eq "dequotation" and $i->value =~ /^@/
$i->type eq "dequotation" $i->value =~ /^@/
Truth table100%3/3
A and B
A= $i->type eq "dequotation"B= $i->value =~ /^@/
ABresult
0X0
100
111
442 50 my $dl = $self->bind($i);
443 50 ▶ $i->error( "~@ should be given a list but got " . $dl->type() )
Branch: 1 branch
BranchTrueFalseTotal
if $dl->type ne "list" 0 50 50
444 if $dl->type() ne "list";
445 50 foreach my $di ( @{ $dl->value() } ) {
446 76 $list->append($di);
447 }
448 }
449 else {
450 333 $list->append( $self->bind($i) );
451 }
452 }
453 136 return $list;
454 }
455 148 return $ast;
456 }
457  
458 2570 ▶sub _eval {
Subroutine _eval: called CC=79 SCAR=46.3
459 2570 my $self = shift;
460 2570 my $ast = shift;
461 2570 my $class = $ast->class();
462 2570 my $type = $ast->type();
463 2570 my $value = $ast->value();
464 2570 ▶ if ( $type eq "list" ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type eq 'list') { } 877 1693 2570
elsif ($type eq 'accessor') { } 33 1660 1693
elsif ($type eq 'sender') { } 1 1659 1660
elsif ($type eq 'symbol') { } 1145 514 1659
elsif ($type eq 'syntaxquotation') { } 70 444 514
elsif ($type eq 'quotation') { } 39 405 444
elsif ($class eq 'Seq' and $type eq 'vector') { } 10 395 405
elsif ($class eq 'Seq' and $type eq 'map' || $type eq 'meta') { } 27 368 395
elsif ($class eq 'Seq' and $type eq 'xml') { } 21 694 715
465 877 my $size = $ast->size();
466 877 ▶ if ( $size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size == 0) 5 872 877
467 5 return $empty_list;
468 }
469 872 my $f = $self->_eval( $ast->first() );
470 872 my $ftype = $f->type();
471 872 my $fvalue = $f->value();
472 872 ▶ if ( $ftype eq "symbol" ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($ftype eq 'symbol') { } 703 169 872
elsif ($ftype eq 'key accessor') { } 11 158 169
elsif ($ftype eq 'index accessor') { } 22 136 158
elsif ($ftype eq 'function') { } 79 57 136
elsif ($ftype eq 'perlfunction') { } 0 57 57
elsif ($ftype eq 'macro') { } 57 0 57
473 703 return $self->builtin( $f, $ast );
474 }
475 elsif ( $ftype eq "key accessor" ) {
476 11 ▶ $ast->error("key accessor expects >= 1 arguments") if $size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 1 0 11 11
477 11 my $m = $self->_eval( $ast->second() );
478 11 my $mtype = $m->type();
479 11 my $mvalue = $m->value();
480 11 ▶ $ast->error(
Branch: 1 branch
BranchTrueFalseTotal
if $mtype ne "map" and $mtype ne "meta" 0 11 11
Condition66%2/3
$mtype ne "map" and $mtype ne "meta"
!ll&&!rl&&r
470
MC/DC0%0/2
$mtype ne "map" and $mtype ne "meta"
$mtype ne "map" $mtype ne "meta"
Truth table66%2/3
A and B
A= $mtype ne "map"B= $mtype ne "meta"
ABresult
0X0
100
111
481 "key accessor expects a map or meta as the first arguments but got "
482 . $mtype )
483 if $mtype ne "map" and $mtype ne "meta";
484 11 ▶ if ( $size == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size == 2) { } 7 4 11
elsif ($size == 3) { } 4 0 4
485  
486 #$ast->error("key " . $fvalue . " does not exist")
487 7 ▶ return $nil if !exists $mvalue->{$fvalue};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mvalue->{$fvalue} 0 7 7
488 7 return $mvalue->{$fvalue};
489 }
490 elsif ( $size == 3 ) {
491 4 my $v = $self->_eval( $ast->third() );
492 4 ▶ if ( $v->type() eq "nil" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->type eq 'nil') { } 0 4 4
493 0 delete $mvalue->{$fvalue};
494 0 return $nil;
495 }
496 else {
497 4 $mvalue->{$fvalue} = $v;
498 4 return $mvalue->{$fvalue};
499 }
500 }
501 else {
502 0 $ast->error("key accessor expects <= 2 arguments");
503 }
504 }
505 elsif ( $ftype eq "index accessor" ) {
506 22 ▶ $ast->error("index accessor expects >= 1 arguments") if $size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 1 0 22 22
507 22 my $v = $self->_eval( $ast->second() );
508 22 my $vtype = $v->type();
509 22 my $vvalue = $v->value();
510 22 ▶ $ast->error(
Branch: 1 branch
BranchTrueFalseTotal
if $vtype ne "vector" and $vtype ne "list" and $vtype ne "xml" 0 22 22
Condition83%5/6
$vtype ne "vector" and $vtype ne "list" and $vtype ne "xml"
!ll&&!rl&&r
1840
$vtype ne "vector" and $vtype ne "list"
!ll&&!rl&&r
1084
MC/DC0%0/3
$vtype ne "vector" and $vtype ne "list" and $vtype ne "xml"
$vtype ne "vector" $vtype ne "list" $vtype ne "xml"
Truth table75%3/4
A and B and C
A= $vtype ne "vector"B= $vtype ne "list"C= $vtype ne "xml"
ABCresult
0XX0
10X0
1100
1111
511 "index accessor expects a vector or list or xml as the first arguments but got "
512 . $vtype )
513 if $vtype ne "vector"
514 and $vtype ne "list"
515 and $vtype ne "xml";
516 $ast->error("index is bigger than size")
517 22 ▶ if $fvalue >= scalar @{$vvalue};
Branch: 1 branch
BranchTrueFalseTotal
if $fvalue >= scalar @{$vvalue;} 0 22 22
518 22 ▶ if ( $size == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size == 2) { } 21 1 22
elsif ($size == 3) { } 1 0 1
519 21 return $vvalue->[$fvalue];
520 }
521 elsif ( $size == 3 ) {
522 1 $vvalue->[$fvalue] = $self->_eval( $ast->third() );
523 1 return $vvalue->[$fvalue];
524 }
525 else {
526 0 $ast->error("index accessor expects <= 2 arguments");
527 }
528 }
529 elsif ( $ftype eq "function" ) {
530 # Fallback to current scope if the function
531 # definition didnt shallow copy its current scope at the time of definition.
532 # This is the case when the evaler is persisted and then defrosted.
533 79 ▶ my $scope = defined( $f->context() ) ? $f->context() : $self->copy_current_scope();
Branch: 1 branch
BranchTrueFalseTotal
defined $f->context ? : 75 4 79
534  
535 79 my $fn = $fvalue;
536 79 my $fargs = $fn->second();
537 79 my @rargs = $ast->slice( 1 .. $size - 1 );
538 79 my @rrargs = ();
539 79 foreach my $arg (@rargs) {
540 115 push @rrargs, $self->_eval($arg);
541 }
542 79 $self->push_scope($scope);
543 79 $self->push_caller($fn);
544 79 my $rest_args = undef;
545 79 my $i = 0;
546 79 my $fargsvalue = $fargs->value();
547 79 my $fargsn = scalar @{$fargsvalue};
548 79 my $rrargsn = scalar @rrargs;
549  
550 79 ▶ for ( $i = 0 ; $i < $fargsn ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $fargsn) 116 79 195
551 116 my $name = $fargsvalue->[$i]->value();
552 116 ▶ if ( $name eq "&" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '&') { } 4 112 116
553 4 $i++;
554 4 $name = $fargsvalue->[$i]->value();
555 4 $rest_args = Language::LispPerl::Seq->new({ type => "list" });
556 4 $self->new_var( $name, $rest_args );
557 }
558 else {
559 112 ▶ $ast->error("real arguments < formal arguments")
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $rrargsn 0 112 112
560 if $i >= $rrargsn;
561 112 $self->new_var( $name, $rrargs[$i] );
562 }
563 }
564 79 ▶ if ( defined $rest_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest_args) { } 4 75 79
565 4 $i -= 2;
566 4 ▶ for ( ; $i < $rrargsn ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrargsn) 3 4 7
567 3 $rest_args->append( $rrargs[$i] );
568 }
569 }
570 else {
571 75 ▶ $ast->error("real arguments > formal arguments")
Branch: 1 branch
BranchTrueFalseTotal
if $i < $rrargsn 0 75 75
572 if $i < $rrargsn;
573 }
574 79 my @body = $fn->slice( 2 .. $fn->size() - 1 );
575 79 my $res;
576 79 foreach my $b (@body) {
577 109 $res = $self->_eval($b);
578 }
579 78 $self->pop_scope();
580 78 $self->pop_caller();
581 78 return $res;
582 }
583 elsif ( $ftype eq "perlfunction" ) {
584 0 my $meta = undef;
585 0 ▶ $meta = $self->_eval( $ast->second() )
Branch: 1 branch
BranchTrueFalseTotal
if defined $ast->second and $ast->second->type eq "meta" 0 0 0
Condition0%0/3
defined $ast->second and $ast->second->type eq "meta"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ast->second and $ast->second->type eq "meta"
defined $ast->second $ast->second->type eq "meta"
Truth table0%0/3
A and B
A= defined $ast->secondB= $ast->second->type eq "meta"
ABresult
0X0
100
111
586 if defined $ast->second()
587 and $ast->second()->type() eq "meta";
588 0 my $perl_func = \&{ $f->value() };
589 0 ▶ my @args = $ast->slice( ( defined $meta ? 2 : 1 ) .. $size - 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $meta ? : 0 0 0
590 0 return $self->perlfunc_call( $perl_func, $meta, \@args, $ast );
591 }
592 elsif ( $ftype eq "macro" ) {
593 57 ▶ my $scope = defined( $f->context() ) ? $f->context() : $self->copy_current_scope();
Branch: 1 branch
BranchTrueFalseTotal
defined $f->context ? : 56 1 57
594 57 my $fn = $fvalue;
595 57 my $fargs = $fn->third();
596 57 my @rargs = $ast->slice( 1 .. $ast->size() - 1 );
597 57 $self->push_scope($scope);
598 57 $self->push_caller($fn);
599 57 my $rest_args = undef;
600 57 my $i = 0;
601 57 my $fargsvalue = $fargs->value();
602 57 my $fargsn = scalar @{$fargsvalue};
603 57 my $rargsn = scalar @rargs;
604  
605 57 ▶ for ( $i = 0 ; $i < $fargsn ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $fargsn) 154 57 211
606 154 my $name = $fargsvalue->[$i]->value();
607 154 ▶ if ( $name eq "&" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '&') { } 53 101 154
608 53 $i++;
609 53 $name = $fargsvalue->[$i]->value();
610 53 $rest_args = Language::LispPerl::Seq->new({ type => "list" });
611 53 $self->new_var( $name, $rest_args );
612 }
613 else {
614 101 ▶ $ast->error("real arguments < formal arguments")
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $rargsn 0 101 101
615 if $i >= $rargsn;
616 101 $self->new_var( $name, $rargs[$i] );
617 }
618 }
619 57 ▶ if ( defined $rest_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest_args) { } 53 4 57
620 53 $i -= 2;
621 53 ▶ for ( ; $i < $rargsn ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rargsn) 79 53 132
622 79 $rest_args->append( $rargs[$i] );
623 }
624 }
625 else {
626 4 ▶ $ast->error("real arguments > formal arguments")
Branch: 1 branch
BranchTrueFalseTotal
if $i < $rargsn 0 4 4
627 if $i < $rargsn;
628 }
629 57 my @body = $fn->slice( 3 .. $fn->size() - 1 );
630 57 my $res;
631 57 foreach my $b (@body) {
632 57 $res = $self->_eval($b);
633 }
634 57 $self->pop_scope();
635 57 $self->pop_caller();
636 57 return $self->_eval($res);
637 }
638 else {
639 0 $ast->error("expect a function or function name or index/key accessor");
640 }
641 }
642 elsif ( $type eq "accessor" ) {
643 33 my $av = $self->_eval($value);
644 33 my $a = Language::LispPerl::Atom->new({ type => "unknown", value => $av->value() });
645 33 my $at = $av->type();
646 33 ▶ if ( $at eq "number" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($at eq 'number') { } 22 11 33
elsif ($at eq 'string' or $at eq 'keyword') { } 11 0 11
647 22 $a->type("index accessor");
648 }
649 ▶ elsif ( $at eq "string" or $at eq "keyword" ) {
Condition66%2/3
$at eq "string" or $at eq "keyword"
l!l&&r!l&&!r
650
MC/DC0%0/2
$at eq "string" or $at eq "keyword"
$at eq "string" $at eq "keyword"
Truth table66%2/3
A or B
A= $at eq "string"B= $at eq "keyword"
ABresult
1X1
011
000
650 11 $a->type("key accessor");
651 }
652 else {
653 0 $ast->error(
654 "unsupport type " . $at . " for accessor but got " . $at );
655 }
656 33 return $a;
657 }
658 elsif ( $type eq "sender" ) {
659 1 my $sn = $self->_eval($value);
660 1 ▶ $ast->error( "sender expects a string or keyword but got " . $type )
Branch: 1 branch
BranchTrueFalseTotal
if $sn->type ne "string" and $sn->type ne "keyword" 0 1 1
Condition33%1/3
$sn->type ne "string" and $sn->type ne "keyword"
!ll&&!rl&&r
100
MC/DC0%0/2
$sn->type ne "string" and $sn->type ne "keyword"
$sn->type ne "string" $sn->type ne "keyword"
Truth table33%1/3
A and B
A= $sn->type ne "string"B= $sn->type ne "keyword"
ABresult
0X0
100
111
661 if $sn->type() ne "string"
662 and $sn->type() ne "keyword";
663 1 my $s = Language::LispPerl::Atom->new({ type => "symbol", value => $sn->value() });
664 1 return $self->bind($s);
665 }
666 elsif ( $type eq "symbol" ) {
667 1145 return $self->bind($ast);
668 }
669 elsif ( $type eq "syntaxquotation" ) {
670 70 return $self->bind($ast);
671 }
672 elsif ( $type eq "quotation" ) {
673 39 return $self->bind($ast);
674 }
675 ▶ elsif ( $class eq "Seq" and $type eq "vector" ) {
Condition100%3/3
$class eq "Seq" and $type eq "vector"
!ll&&!rl&&r
3474810
MC/DC100%2/2
$class eq "Seq" and $type eq "vector"
$class eq "Seq" $type eq "vector"
Truth table100%3/3
A and B
A= $class eq "Seq"B= $type eq "vector"
ABresult
0X0
100
111
676 10 my $v = Language::LispPerl::Atom->new({ type => "vector" });
677 10 my @vv = ();
678 10 foreach my $i ( @{$value} ) {
679 26 push @vv, $self->_eval($i);
680 }
681 10 $v->value( \@vv );
682 10 return $v;
683 }
684 ▶ elsif ( $class eq "Seq" and ( $type eq "map" or $type eq "meta" ) ) {
Condition100%6/6
$class eq "Seq" and $type eq "map" || $type eq "meta"
!ll&&!rl&&r
3472127
$type eq "map" or $type eq "meta"
l!l&&r!l&&!r
111621
MC/DC100%3/3
$class eq "Seq" and $type eq "map" || $type eq "meta"
$class eq "Seq" $type eq "map" $type eq "meta"
Truth table100%4/4
A and B or C
A= $class eq "Seq"B= $type eq "map"C= $type eq "meta"
ABCresult
0XX0
1000
11X1
1011
685 27 my $m = Language::LispPerl::Atom->new({ type => "map" });
686 27 my %mv = ();
687 27 my $n = scalar @{$value};
688 27 ▶ $ast->error( $type . " should have even number of items" )
Branch: 1 branch
BranchTrueFalseTotal
if $n % 2 != 0 0 27 27
689 if ( $n % 2 ) != 0;
690 27 ▶ for ( my $i = 0 ; $i < $n ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 28 27 55
691 28 my $k = $self->_eval( $value->[$i] );
692 28 ▶ $ast->error( $type
Branch: 1 branch
BranchTrueFalseTotal
if $k->type ne "keyword" and $k->type ne "string" 0 28 28
Condition66%2/3
$k->type ne "keyword" and $k->type ne "string"
!ll&&!rl&&r
2620
MC/DC0%0/2
$k->type ne "keyword" and $k->type ne "string"
$k->type ne "keyword" $k->type ne "string"
Truth table66%2/3
A and B
A= $k->type ne "keyword"B= $k->type ne "string"
ABresult
0X0
100
111
693 . " expects keyword or string as key but got "
694 . $k->type() )
695 if ( $k->type() ne "keyword"
696 and $k->type() ne "string" );
697 28 my $v = $self->_eval( $value->[ $i + 1 ] );
698 28 $mv{ $k->value() } = $v;
699 }
700 27 $m->value( \%mv );
701 27 ▶ $m->type("meta") if $type eq "meta";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "meta" 16 11 27
702 27 return $m;
703 }
704 ▶ elsif ( $class eq "Seq" and $type eq "xml" ) {
Condition66%2/3
$class eq "Seq" and $type eq "xml"
!ll&&!rl&&r
347021
MC/DC50%1/2
$class eq "Seq" and $type eq "xml"
$class eq "Seq" $type eq "xml"
Truth table66%2/3
A and B
A= $class eq "Seq"B= $type eq "xml"
ABresult
0X0
100
111
705 21 my $size = $ast->size();
706 21 ▶ $ast->error("xml expects >= 1 arguments") if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 21 21
707 21 my $first = $ast->first();
708 21 my $firsttype = $first->type();
709 21 ▶ if ( $firsttype ne "symbol" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firsttype ne "symbol") 2 19 21
710 2 $first = $self->_eval($first);
711 2 $firsttype = $first->type();
712 }
713 $ast->error(
714 21 ▶ "xml expects a symbol or string or keyword as name but got "
Branch: 1 branch
BranchTrueFalseTotal
if $firsttype ne "symbol" and $firsttype ne "string" and $firsttype ne "keyword" 0 21 21
Condition50%3/6
$firsttype ne "symbol" and $firsttype ne "string" and $firsttype ne "keyword"
!ll&&!rl&&r
2100
$firsttype ne "symbol" and $firsttype ne "string"
!ll&&!rl&&r
1920
MC/DC0%0/3
$firsttype ne "symbol" and $firsttype ne "string" and $firsttype ne "keyword"
$firsttype ne "symbol" $firsttype ne "string" $firsttype ne "keyword"
Truth table50%2/4
A and B and C
A= $firsttype ne "symbol"B= $firsttype ne "string"C= $firsttype ne "keyword"
ABCresult
0XX0
10X0
1100
1111
715 . $firsttype )
716 if $firsttype ne "symbol"
717 and $firsttype ne "string"
718 and $firsttype ne "keyword";
719 21 my @items = ();
720 21 my $xml = Language::LispPerl::Atom->new({ type => "xml", value => \@items });
721 21 $xml->{name} = $first->value();
722 21 my @rest = $ast->slice( 1 .. $size - 1 );
723 21 foreach my $i (@rest) {
724 27 my $iv = $self->_eval($i);
725 27 my $it = $iv->type();
726 27 ▶ $ast->error(
Branch: 1 branch
BranchTrueFalseTotal
if $it ne "string" and $it ne "xml" and $it ne "meta" and $it ne "list" 0 27 27
Condition88%8/9
$it ne "string" and $it ne "xml" and $it ne "meta" and $it ne "list"
!ll&&!rl&&r
2520
$it ne "string" and $it ne "xml" and $it ne "meta"
!ll&&!rl&&r
2052
$it ne "string" and $it ne "xml"
!ll&&!rl&&r
1197
MC/DC0%0/4
$it ne "string" and $it ne "xml" and $it ne "meta" and $it ne "list"
$it ne "string" $it ne "xml" $it ne "meta" $it ne "list"
Truth table80%4/5
A and B and C and D
A= $it ne "string"B= $it ne "xml"C= $it ne "meta"D= $it ne "list"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
727 "xml expects string or xml or meta or list as items but got "
728 . $it )
729 if $it ne "string"
730 and $it ne "xml"
731 and $it ne "meta"
732 and $it ne "list";
733 27 ▶ if ( $it eq "meta" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($it eq 'meta') { } 5 22 27
elsif ($it eq 'list') { } 2 20 22
734 5 $xml->meta_data($iv);
735 }
736 elsif ( $it eq "list" ) {
737 2 foreach my $i ( @{ $iv->value() } ) {
738 4 push @items, $i;
739 }
740 }
741 else {
742 ;
743 20 push @items, $iv;
744 }
745 }
746 21 return $xml;
747 }
748 347 return $ast;
749 }
750  
751 =head2 word_is_reserved
752  
753 Is the given word reserved?
754 Usage:
755  
756 if( $this->word_is_reserved('bla') ){
757 ...
758 }
759  
760 =cut
761  
762 1355 ▶sub word_is_reserved{
Subroutine word_is_reserved: called CC=1 SCAR=0.0
Pod documented
763 1355 my ($self, $word ) = @_;
764 1355 return $self->builtins()->has_function( $word );
765 }
766  
767 703 ▶sub builtin {
Subroutine builtin: called CC=2 SCAR=7.4
Pod undocumented
768 703 my ($self, $f , $ast) = @_;
769  
770 703 my $fn = $f->value();
771  
772 703 ▶ if( my $function = $self->builtins()->has_function( $fn ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $function = $self->builtins->has_function($fn)) 703 0 703
773 703 return $self->builtins()->call_function( $function , $ast , $f );
774 }
775  
776 0 confess "Builtin function '$fn' is not implemented";
777 }
778  
779 22 ▶sub perlfunc_call {
Subroutine perlfunc_call: called CC=26 SCAR=51.0
Pod undocumented
780 22 my $self = shift;
781 22 my $perl_func = shift;
782 22 my $meta = shift;
783 22 my $rargs = shift;
784 22 my $ast = shift;
785  
786 22 my $ret_type = "scalar";
787 22 my @fargtypes = ();
788 22 ▶ if ( defined $meta ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta) 6 16 22
789 6 ▶ if ( exists $meta->value()->{"return"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $meta->value->{"return"}) 6 0 6
790 6 my $rt = $meta->value()->{"return"};
791 6 ▶ $ast->error(
Branch: 1 branch
BranchTrueFalseTotal
if $rt->type ne "string" and $rt->type ne "keyword" 0 6 6
Condition33%1/3
$rt->type ne "string" and $rt->type ne "keyword"
!ll&&!rl&&r
600
MC/DC0%0/2
$rt->type ne "string" and $rt->type ne "keyword"
$rt->type ne "string" $rt->type ne "keyword"
Truth table33%1/3
A and B
A= $rt->type ne "string"B= $rt->type ne "keyword"
ABresult
0X0
100
111
792 "return expects a string or keyword but got " . $rt->type() )
793 if $rt->type() ne "string"
794 and $rt->type() ne "keyword";
795 6 $ret_type = $rt->value();
796 }
797 6 ▶ if ( exists $meta->value()->{"arguments"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $meta->value->{"arguments"}) 0 6 6
798 0 my $ats = $meta->value()->{"arguments"};
799 0 ▶ $ast->error( "arguments expect a vector but got " . $ats->type() )
Branch: 1 branch
BranchTrueFalseTotal
if $ats->type ne "vector" 0 0 0
800 if $ats->type() ne "vector";
801 0 foreach my $arg ( @{ $ats->value() } ) {
802 0 ▶ $ast->error(
Branch: 1 branch
BranchTrueFalseTotal
if $arg->type ne "string" and $arg->type ne "keyword" 0 0 0
Condition0%0/3
$arg->type ne "string" and $arg->type ne "keyword"
!ll&&!rl&&r
000
MC/DC0%0/2
$arg->type ne "string" and $arg->type ne "keyword"
$arg->type ne "string" $arg->type ne "keyword"
Truth table0%0/3
A and B
A= $arg->type ne "string"B= $arg->type ne "keyword"
ABresult
0X0
100
111
803 "arguments expect a vector of string or keyword but got "
804 . $arg->type() )
805 if $arg->type() ne "string"
806 and $arg->type() ne "keyword";
807 0 push @fargtypes, $arg->value();
808 }
809 }
810 }
811 22 my @args = ();
812 22 my $i = 0;
813 22 foreach my $arg ( @{$rargs} ) {
814 29 my $pobj = $self->clj2perl( $self->_eval($arg) );
815 29 ▶ if ( $i < scalar @fargtypes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @fargtypes) { } 0 29 29
816 0 my $ft = $fargtypes[$i];
817 0 ▶ if ( $ft eq "scalar" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ft eq 'scalar') { } 0 0 0
elsif ($ft eq 'array') { } 0 0 0
elsif ($ft eq 'hash') { } 0 0 0
elsif ($ft eq 'ref') { } 0 0 0
818 0 push @args, $pobj;
819 }
820 elsif ( $ft eq "array" ) {
821 0 push @args, @{$pobj};
822 }
823 elsif ( $ft eq "hash" ) {
824 0 push @args, %{$pobj};
825 }
826 elsif ( $ft eq "ref" ) {
827 0 push @args, \$pobj;
828 }
829 else {
830 0 push @args, $pobj;
831 }
832 }
833 else {
834 29 ▶ if ( ref($pobj) eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pobj eq 'ARRAY') { } 0 29 29
elsif (ref $pobj eq 'HASH') { } 0 29 29
835 0 push @args, @{$pobj};
836 }
837 elsif ( ref($pobj) eq "HASH" ) {
838 0 push @args, %{$pobj};
839 }
840 else {
841 29 push @args, $pobj;
842 }
843 }
844 29 $i++;
845 }
846  
847 22 ▶ if ( $ret_type eq "scalar" ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($ret_type eq 'scalar') { } 21 1 22
elsif ($ret_type eq 'ref-scalar') { } 0 1 1
elsif ($ret_type eq 'array') { } 0 1 1
elsif ($ret_type eq 'ref-array') { } 0 1 1
elsif ($ret_type eq 'hash') { } 0 1 1
elsif ($ret_type eq 'ref-hash') { } 0 1 1
elsif ($ret_type eq 'nil') { } 0 1 1
elsif ($ret_type eq 'raw') { } 1 0 1
848 21 my $r = $perl_func->(@args);
849 21 return &wrap_perlobj($r);
850 }
851 elsif ( $ret_type eq "ref-scalar" ) {
852 0 my $r = $perl_func->(@args);
853 0 return &wrap_perlobj( \$r );
854 }
855 elsif ( $ret_type eq "array" ) {
856 0 my @r = $perl_func->(@args);
857 0 return &wrap_perlobj(@r);
858 }
859 elsif ( $ret_type eq "ref-array" ) {
860 0 my @r = $perl_func->(@args);
861 0 return &wrap_perlobj( \@r );
862 }
863 elsif ( $ret_type eq "hash" ) {
864 0 my %r = $perl_func->(@args);
865 0 return &wrap_perlobj(%r);
866 }
867 elsif ( $ret_type eq "ref-hash" ) {
868 0 my %r = $perl_func->(@args);
869 0 return &wrap_perlobj( \%r );
870 }
871 elsif ( $ret_type eq "nil" ) {
872 0 $perl_func->(@args);
873 0 return $nil;
874 }
875 elsif ( $ret_type eq 'raw' ) {
876  
877 # The perl function is expected to return a raw Language::LispPerl::Atom
878 1 return $perl_func->(@args);
879 }
880 else {
881 0 my $r = \$perl_func->(@args);
882 0 return &wrap_perlobj($r);
883 }
884  
885 }
886  
887 32 ▶sub clj2perl {
Subroutine clj2perl: called CC=15 SCAR=35.0
Pod undocumented
888 32 my $self = shift;
889 32 my $ast = shift;
890 32 my $type = $ast->type();
891 32 my $value = $ast->value();
892 32 ▶ if ( $type eq "string"
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'string' or $type eq 'number' or $type eq 'quotation' or $type eq 'keyword' or $type eq 'perlobject') { } 28 4 32
elsif ($type eq 'bool') { } 0 4 4
elsif ($type eq 'nil') { } 1 3 4
elsif ($type eq 'list' or $type eq 'vector') { } 0 3 3
elsif ($type eq 'map') { } 0 3 3
elsif ($type eq 'function') { } 3 0 3
Condition75%9/12
$type eq "string" or $type eq "number" or $type eq "quotation" or $type eq "keyword" or $type eq "perlobject"
l!l&&r!l&&!r
2174
$type eq "string" or $type eq "number" or $type eq "quotation" or $type eq "keyword"
l!l&&r!l&&!r
21011
$type eq "string" or $type eq "number" or $type eq "quotation"
l!l&&r!l&&!r
21011
$type eq "string" or $type eq "number"
l!l&&r!l&&!r
21011
MC/DC40%2/5
$type eq "string" or $type eq "number" or $type eq "quotation" or $type eq "keyword" or $type eq "perlobject"
$type eq "string" $type eq "number" $type eq "quotation" $type eq "keyword" $type eq "perlobject"
Truth table50%3/6
A or B or C or D or E
A= $type eq "string"B= $type eq "number"C= $type eq "quotation"D= $type eq "keyword"E= $type eq "perlobject"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
893 or $type eq "number"
894 or $type eq "quotation"
895 or $type eq "keyword"
896 or $type eq "perlobject" )
897 {
898 28 return $value;
899 }
900 elsif ( $type eq "bool" ) {
901 0 ▶ if ( $value eq "true" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq 'true') { } 0 0 0
902 0 return 1;
903 }
904 else {
905 0 return 0;
906 }
907 }
908 elsif ( $type eq "nil" ) {
909 1 return undef;
910 }
911 ▶ elsif ( $type eq "list" or $type eq "vector" ) {
Condition33%1/3
$type eq "list" or $type eq "vector"
l!l&&r!l&&!r
003
MC/DC0%0/2
$type eq "list" or $type eq "vector"
$type eq "list" $type eq "vector"
Truth table33%1/3
A or B
A= $type eq "list"B= $type eq "vector"
ABresult
1X1
011
000
912 0 my @r = ();
913 0 foreach my $i ( @{$value} ) {
914 0 push @r, $self->clj2perl($i);
915 }
916 0 return \@r;
917 }
918 elsif ( $type eq "map" ) {
919 0 my %r = ();
920 0 foreach my $k ( keys %{$value} ) {
921 0 $r{$k} = $self->clj2perl( $value->{$k} );
922 }
923 0 return \%r;
924 }
925 elsif ( $type eq "function" ) {
926 3 ▶ my $f = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
927 3 my @args = @_;
928 3 my $cljf = Language::LispPerl::Seq->new({ type => "list" });
929 3 $cljf->append($ast);
930 3 foreach my $arg (@args) {
931 3 $cljf->append( $self->perl2clj($arg) );
932 }
933 3 return $self->clj2perl( $self->_eval($cljf) );
934 3 };
935 3 return $f;
936 }
937 else {
938 0 $ast->error(
939 "unsupported type '" . $type . "' for clj2perl object conversion" );
940 }
941 }
942  
943 21 ▶sub wrap_perlobj {
Subroutine wrap_perlobj: called CC=2 SCAR=8.4
Pod undocumented
944 21 my $v = shift;
945 21 ▶ while ( ref($v) eq "REF" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "REF") 0 21 21
946 0 $v = ${$v};
947 }
948 21 return Language::LispPerl::Atom->new({ type => "perlobject", value => $v });
949 }
950  
951 =head2 perl2clj
952  
953 Turn a native perl Object into a new L<Language::LispPerl::Atom>
954  
955 Usage:
956  
957 my $new_atom = $evaler->perl2clj( .. perl object .. );
958  
959 =cut
960  
961 13 ▶sub perl2clj {
Subroutine perl2clj: called CC=9 SCAR=26.5
Pod documented
962 13 my ($self, $v) = @_;
963 13 ▶ if ( !defined ref($v) or ref($v) eq "" ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined ref $v or ref $v eq '') { } 6 7 13
elsif (ref $v eq 'SCALAR') { } 0 7 7
elsif (ref $v eq 'HASH') { } 0 7 7
elsif (ref $v eq 'ARRAY') { } 4 3 7
elsif (ref $v eq 'CODE') { } 0 3 3
Condition66%2/3
not defined ref $v or ref $v eq ""
l!l&&r!l&&!r
067
MC/DC50%1/2
not defined ref $v or ref $v eq ""
not defined ref $v ref $v eq ""
Truth table66%2/3
not(A) or B
A= not defined ref $vB= ref $v eq ""
ABresult
1X1
011
000
964 6 return Language::LispPerl::Atom->new({ type => "string", value => $v });
965 }
966 elsif ( ref($v) eq "SCALAR" ) {
967 0 return Language::LispPerl::Atom->new({ type => "string", value => ${$v} });
968 }
969 elsif ( ref($v) eq "HASH" ) {
970 0 my %m = ();
971 0 foreach my $k ( keys %{$v} ) {
972 0 $m{$k} = $self->perl2clj( $v->{$k} );
973 }
974 0 return Language::LispPerl::Atom->new({ type => "map", value => \%m });
975 }
976 elsif ( ref($v) eq "ARRAY" ) {
977 4 my @a = ();
978 4 foreach my $i ( @{$v} ) {
979 4 push @a, $self->perl2clj($i);
980 }
981 4 return Language::LispPerl::Atom->new({ type => "vector", value => \@a });
982 }
983 elsif ( ref($v) eq "CODE" ) {
984 0 return Language::LispPerl::Atom->new({ type => "perlfunction", value => $v });
985 }
986 else {
987 3 return Language::LispPerl::Atom->new({ type => "perlobject", value => $v });
988  
989 #$ast->error("expect a reference of scalar or hash or array");
990 }
991 }
992  
993 1 ▶sub trace_vars {
Subroutine trace_vars: called CC=2 SCAR=6.9
Pod undocumented
994 1 my $self = shift;
995 1 print @{ $self->scopes() } . "\n";
996 1 foreach my $vn ( keys %{ $self->current_scope() } ) {
997 17 print
998 "$vn\n" # . Language::LispPerl::Printer::to_string(${$self->current_scope()}{$vn}->value()) . "\n";
999 }
1000 }
1001  
1002 6 __PACKAGE__->meta->make_immutable();
1003 6 1;
↑ Summary LispPerl/Printer.pm »