blib/lib/Dallycot/Library.pm

filter: Statementstmt 88.1% 163 / 185 Branchbran 68.7% 33 / 48 Conditioncond 46.3% 19 / 41 MC/DCmcdc 15.3% 4 / 26 Subroutinesub 96.9% 32 / 33 Podpod 0.0% 0 / 11 totaltotal 72.9% 251 / 344 CC 40 SCAR 18.1
CC
40
Cov
78%
apply
18.7
min_arity
6.9
get_assignment
5.6
CRAP 6.1
« blib/lib/Dallycot/Library/Linguistics.pm ↑ Summary blib/lib/Dallycot/Registry.pm »
1 package Dallycot::Library;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Base for adding namespaced functions to Dallycot.
5  
6 =head1 SYNOPSIS
7  
8 package MyLibrary;
9  
10 use Moose;
11 extends 'Dallycot::Library';
12  
13 ns 'http://www.example.com/library#';
14  
15 define foo => << 'EOD';
16 (a, b) :> ((a * b) mod (b - a))
17 EOD
18  
19 define bar => sub {
20 my($library, $engine, $options, @params) = @_;
21 # Perl implementation
22 };
23  
24 =cut
25  
26 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use MooseX::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 9 ▶use namespace::autoclean -except => [qw/_libraries/];
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 9 ▶use MooseX::Types::Moose qw/ArrayRef CodeRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 9 ▶use Dallycot::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 9 ▶use Dallycot::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 9 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 9 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 9 ▶use Promises qw(deferred collect);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 use Module::Pluggable
47 9 inner => 1,
48 instantiate => 'instance',
49 sub_name => '_libraries',
50 9 ▶ search_path => 'Dallycot::Library';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 9 our @LIBRARIES;
53  
54 sub libraries {
55 10 ▶ return @LIBRARIES if @LIBRARIES;
Subroutine libraries: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @Dallycot::Library::LIBRARIES 1 9 10
56 9 return @LIBRARIES = grep { $_->isa('Dallycot::Library') } shift->_libraries;
57 }
58  
59 9 my %engines;
60  
61 my %namespaces;
62  
63 sub ns {
64 72 ▶ my ( $meta, $uri ) = @_;
Subroutine ns: called CC=2 SCAR=7.1
Pod undocumented
65  
66 72 Dallycot::Registry->instance->register_namespace( $uri, $meta->{'package'} );
67  
68 72 $namespaces{ $meta->{'package'} } = $uri;
69  
70 72 ▶ my $engine = $engines{ $meta->{'package'} } ||= Dallycot::Processor->new;
Condition33%1/3
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
l!l&&r!l&&!r
0720
MC/DC0%0/2
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
$engines{$meta->{"package"}} "Dallycot::Processor"->new
Truth table33%1/3
A ||= B
A= $engines{$meta->{"package"}}B= "Dallycot::Processor"->new
ABresult
1X1
011
000
71 72 uses( $meta, $uri );
72 72 return;
73 }
74  
75 sub namespace {
76 517 ▶ my ($class) = @_;
Subroutine namespace: called CC=2 SCAR=7.6
Pod undocumented
77  
78 517 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
05170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
79  
80 517 return $namespaces{$class};
81 }
82  
83 9 my %definitions;
84 9 my %uses_promises;
85  
86 sub define {
87 882 ▶ my ( $meta, $name, @options ) = @_;
Subroutine define: called CC=5 SCAR=16.8
Pod undocumented
88  
89 882 my $body = pop @options;
90 882 my %options = @options;
91  
92 882 ▶ my $definitions = $definitions{ $meta->{'package'} } ||= {};
Condition100%2/2
$definitions{$meta->{"package"}} ||= {}
l!l
81072
MC/DC100%1/1
$definitions{$meta->{"package"}} ||= {}
$definitions{$meta->{"package"}}
Truth table100%2/2
A
A= $definitions{$meta->{"package"}}
Aresult
11
00
93  
94 882 ▶ if ( is_CodeRef($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_CodeRef($body)) { } 504 378 882
95  
96 # Perl subroutine
97 504 my $uri_promise = deferred;
98 504 $uri_promise->resolve( $meta->{'package'}->_uri_for_name($name) );
99  
100 504 $definitions->{$name} = {
101 %options,
102 uri => $uri_promise,
103 coderef => $body
104 };
105 }
106 else {
107 # Dallycot source
108 378 my $parser = Dallycot::Parser->new;
109 378 my $parsed = $parser->parse($body);
110 378 ▶ my $engine = $engines{ $meta->{'package'} } ||= Dallycot::Processor->new;
Condition33%1/3
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
l!l&&r!l&&!r
37800
MC/DC0%0/2
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
$engines{$meta->{"package"}} "Dallycot::Processor"->new
Truth table33%1/3
A ||= B
A= $engines{$meta->{"package"}}B= "Dallycot::Processor"->new
ABresult
1X1
011
000
111  
112 378 ▶ if ( !$parsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 378 378
113 0 croak "Unable to parse Dallycot source for $name";
114 }
115  
116 $uses_promises{ $meta->{'package'} }->done(
117 sub {
118 $definitions->{$name} = {
119 %options,
120 375 expression => $engine->with_child_scope->execute( @{$parsed} )->catch(
121 sub {
122 0 ▶ my ($err) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
123  
124 0 print STDERR "Error defining $name: $err\n";
125 0 croak $err;
126 }
127 )
128 375 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
129 }
130 378 );
131 }
132 882 return;
133 }
134  
135 sub uses {
136 108 ▶ my ( $meta, @uris ) = @_;
Subroutine uses: called CC=3 SCAR=11.0
Pod undocumented
137  
138 108 ▶ my $engine = $engines{ $meta->{'package'} } ||= Dallycot::Processor->new;
Condition33%1/3
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
l!l&&r!l&&!r
10800
MC/DC0%0/2
$engines{$meta->{"package"}} ||= "Dallycot::Processor"->new
$engines{$meta->{"package"}} "Dallycot::Processor"->new
Truth table33%1/3
A ||= B
A= $engines{$meta->{"package"}}B= "Dallycot::Processor"->new
ABresult
1X1
011
000
139  
140 my $promise = Dallycot::Registry->instance->register_used_namespaces(@uris)->then(
141 sub {
142 108 ▶ $engine->append_namespace_search_path(@uris);
Subroutine __ANON__: called CC=1 SCAR=0.0
143 }
144 108 );
145  
146 108 my $prior_promise = $uses_promises{ $meta->{'package'} };
147 108 ▶ if ($prior_promise) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prior_promise) { } 36 72 108
148 36 ▶ $prior_promise = $prior_promise->then( sub {$promise} );
Subroutine __ANON__: called CC=1 SCAR=0.0
149 }
150 else {
151 72 $prior_promise = $promise;
152 }
153 108 $uses_promises{ $meta->{'package'} } = $prior_promise;
154  
155 108 return;
156 }
157  
158 9 Moose::Exporter->setup_import_methods(
159 with_meta => [qw(ns define uses)],
160 also => 'Moose',
161 );
162  
163 sub init_meta {
164 74 ▶ my ( undef, %p ) = @_;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod undocumented
165  
166 74 my $meta = MooseX::Singleton->init_meta(%p);
167 74 $meta->superclasses(__PACKAGE__);
168 74 return $meta;
169 }
170  
171 sub has_assignment {
172 1310 ▶ my ( $self, $name ) = @_;
Subroutine has_assignment: called CC=2 SCAR=7.0
Pod undocumented
173  
174 1310 my $def = $self->get_definition($name);
175 1310 ▶ return defined($def) && keys %$def;
Condition66%2/3
defined $def && keys %$def
!ll&&!rl&&r
4020908
MC/DC50%1/2
defined $def && keys %$def
defined $def keys %$def
Truth table66%2/3
A && B
A= defined $defB= keys %$def
ABresult
0X0
100
111
176 }
177  
178 sub get_assignment {
179 310 ▶ my ( $self, $name ) = @_;
Subroutine get_assignment: called CC=5 SCAR=17.3
Pod undocumented
180  
181 310 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
03100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
182  
183 310 my $def = $self->get_definition($name);
184  
185 310 ▶ return unless defined $def && keys %$def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and keys %$def 0 310 310
Condition33%1/3
defined $def and keys %$def
!ll&&!rl&&r
00310
MC/DC0%0/2
defined $def and keys %$def
defined $def keys %$def
Truth table33%1/3
A and B
A= defined $defB= keys %$def
ABresult
0X0
100
111
186 310 ▶ if ( $def->{expression} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{'expression'}) { } 236 74 310
187 236 return $def->{expression};
188 }
189 else {
190 74 return $def->{uri};
191 }
192 }
193  
194 sub _uri_for_name {
195 517 ▶ my ( $class, $name ) = @_;
Subroutine _uri_for_name: called CC=2 SCAR=7.6
196  
197 517 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
05170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
198  
199 517 return Dallycot::Value::URI->new( $class->namespace . $name );
200 }
201  
202 sub get_definition {
203 1817 ▶ my ( $class, $name ) = @_;
Subroutine get_definition: called CC=5 SCAR=16.7
Pod undocumented
204  
205 1817 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1817 1817
206  
207 1817 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
018170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
208  
209 1817 my $definitions = $definitions{$class};
210  
211 1817 ▶ if ( exists $definitions->{$name} && defined $definitions->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $definitions->{$name} and defined $definitions->{$name}) { } 1415 402 1817
Condition66%2/3
exists $definitions->{$name} and defined $definitions->{$name}
!ll&&!rl&&r
40201415
MC/DC50%1/2
exists $definitions->{$name} and defined $definitions->{$name}
exists $definitions->{$name} defined $definitions->{$name}
Truth table66%2/3
A and B
A= exists $definitions->{$name}B= defined $definitions->{$name}
ABresult
0X0
100
111
212 1415 return $definitions->{$name};
213 }
214 else {
215 402 return;
216 }
217 }
218  
219 sub min_arity {
220 8 ▶ my ( $self, $name ) = @_;
Subroutine min_arity: called CC=5 SCAR=19.3
Pod undocumented
221  
222 8 my $def = $self->get_definition($name);
223  
224 8 ▶ if ( !$def ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 0 8 8
225 0 return 0;
226 }
227  
228 8 ▶ if ( $def->{coderef} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{'coderef'}) { } 8 0 8
229 8 ▶ if ( defined( $def->{arity} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{'arity'}) { } 8 0 8
230 8 ▶ if ( is_ArrayRef( $def->{arity} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ArrayRef($def->{'arity'})) { } 0 8 8
231 0 return $def->{arity}->[0];
232 }
233 else {
234 8 return $def->{arity};
235 }
236 }
237 else {
238 0 return 0;
239 }
240 }
241 else {
242 0 return 0;
243 }
244 }
245  
246 sub _is_placeholder {
247 241 ▶ my ( $self, $obj ) = @_;
Subroutine _is_placeholder: called CC=2 SCAR=7.1
248  
249 241 ▶ return blessed($obj) && $obj->isa('Dallycot::AST::Placeholder');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Dallycot::AST::Placeholder')
!ll&&!rl&&r
021526
MC/DC50%1/2
builtin::blessed($obj) && $obj->isa('Dallycot::AST::Placeholder')
builtin::blessed($obj) $obj->isa('Dallycot::AST::Placeholder')
Truth table66%2/3
A && B
A= builtin::blessed($obj)B= $obj->isa('Dallycot::AST::Placeholder')
ABresult
0X0
100
111
250 }
251  
252 sub apply {
253 189 ▶ my ( $self, $name, $parent_engine, $options, @bindings ) = @_;
Subroutine apply: called CC=14 SCAR=29.3
Pod undocumented
254  
255 189 my $def = $self->get_definition($name);
256  
257 189 ▶ if ( !$def ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 0 189 189
258 0 my $d = deferred;
259 0 $d->reject("$name is undefined.");
260 0 return $d->promise;
261 }
262  
263 189 ▶ if ( $def->{coderef} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($def->{'coderef'}) { } 133 56 189
elsif ($def->{'expression'}) { } 56 0 56
264 133 ▶ if ( defined $def->{arity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def->{"arity"}) 133 0 133
265 133 ▶ if ( is_ArrayRef( $def->{arity} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ArrayRef($def->{'arity'})) { } 10 123 133
elsif ($def->{'arity'} != @bindings) { } 0 123 123
266 10 ▶ if ( $def->{arity}->[0] > @bindings
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"arity"}[0] > @bindings or @{$def->{"arity"};} > 1 and @bindings > $def->{"arity"}[1]) 0 10 10
Condition50%3/6
$def->{"arity"}[0] > @bindings or @{$def->{"arity"};} > 1 and @bindings > $def->{"arity"}[1]
l!l&&r!l&&!r
0010
@{$def->{"arity"};} > 1 and @bindings > $def->{"arity"}[1]
!ll&&!rl&&r
370
MC/DC0%0/3
$def->{"arity"}[0] > @bindings or @{$def->{"arity"};} > 1 and @bindings > $def->{"arity"}[1]
$def->{"arity"}[0] > @bindings @{$def->{"arity"};} > 1 @bindings > $def->{"arity"}[1]
Truth table25%1/4
A or B and C
A= $def->{"arity"}[0] > @bindingsB= @{$def->{"arity"};} > 1C= @bindings > $def->{"arity"}[1]
ABCresult
1XX1
0111
00X0
0100
267 10 || ( @{ $def->{arity} } > 1 && @bindings > $def->{arity}->[1] ) )
268 {
269 0 my $d = deferred;
270 $d->reject( "Expected "
271 . $def->{arity}->[0] . " to "
272 0 . $def->{arity}->[1]
273 . " arguments but found "
274 . scalar(@bindings) );
275 0 return $d->promise;
276 }
277 }
278 elsif ( $def->{arity} != @bindings ) {
279 0 my $d = deferred;
280 0 $d->reject( "Expected " . $def->{arity} . " argument(s)s but found " . scalar(@bindings) );
281 0 return $d->promise;
282 }
283 }
284  
285 # we look for placeholders and return a lambda if there are any
286 133 ▶ if ( grep { $self->_is_placeholder($_) } @bindings ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$self->_is_placeholder($_);} @bindings) { } 13 120 133
elsif ($def->{'hold'}) { } 0 120 120
287 13 my ( @filled_bindings, @filled_identifiers, @args, @new_args );
288 13 foreach my $binding (@bindings) {
289 26 ▶ if ( $self->_is_placeholder($binding) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_placeholder($binding)) { } 13 13 26
290 13 push @new_args, '__arg_' . $#args;
291 13 push @args, '__arg_' . $#args;
292 }
293 else {
294 13 push @filled_identifiers, '__arg_' . $#args;
295 13 push @args, '__arg_' . $#args;
296 13 push @filled_bindings, $binding;
297 }
298 }
299 13 my $engine = $parent_engine->with_child_scope;
300 return collect( $engine->collect(@filled_bindings), $engine->collect( values %$options ) )->then(
301 sub {
302 13 ▶ my ( $collected_bindings, $new_values ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
303 13 my @collected_bindings = @$collected_bindings;
304 13 my @new_values = @$new_values;
305 13 my %new_options;
306 13 @new_options{ keys %$options } = @new_values;
307 return Dallycot::Value::Lambda->new(
308 expression => Dallycot::AST::Apply->new(
309 $self->_uri_for_name($name),
310 26 [ map { bless [$_] => 'Dallycot::AST::Fetch' } @args ],
311 ),
312 bindings => \@new_args,
313 options => \%new_options,
314 closure_environment =>
315 13 { map { $filled_identifiers[$_] => $collected_bindings[$_] } ( 0 .. $#filled_identifiers ) }
316 );
317 }
318 13 );
319 }
320 elsif ( $def->{hold} ) {
321 0 my $engine = $parent_engine->with_child_scope;
322 0 return $def->{coderef}->( $engine, $options, @bindings );
323 }
324 else {
325 120 my $engine = $parent_engine->with_child_scope;
326 return collect( $engine->collect(@bindings), $engine->collect( values %$options ) )->then(
327 sub {
328 119 ▶ my ( $collected_bindings, $new_values ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
329  
330 119 my @collected_bindings = @$collected_bindings;
331 119 my @new_values = @$new_values;
332 119 my %new_options;
333 119 ▶ @new_options{ keys %{ $def->{options} || {} } } = values %{ $def->{options} || {} };
Branch: 2 branches
BranchTrueFalseTotal
$def->{"options"} or {} 0 119 119
$def->{"options"} or {} 0 119 119
334 119 @new_options{ keys %$options } = @new_values;
335 119 $def->{coderef}->( $engine, \%new_options, @collected_bindings );
336 }
337 120 );
338 }
339 }
340 elsif ( $def->{expression} ) {
341 56 my $engine = $parent_engine->with_child_scope;
342 return $def->{expression}->then(
343 sub {
344 56 ▶ my ($lambda) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
345 56 $lambda->apply( $engine, $options, @bindings );
346 }
347 56 );
348 }
349 else {
350 0 my $d = deferred;
351 0 $d->reject("Value is not a lambda");
352 0 return $d->promise;
353 }
354 }
355  
356 9 __PACKAGE__->meta->make_immutable;
357  
358 9 __PACKAGE__->libraries;
359  
360 9 1;
« blib/lib/Dallycot/Library/Linguistics.pm ↑ Summary blib/lib/Dallycot/Registry.pm »