blib/lib/Dallycot/Value/Lambda.pm

filter: Statementstmt 81.2% 121 / 149 Branchbran 67.8% 19 / 28 Conditioncond 66.6% 18 / 27 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 9 totaltotal 72.9% 183 / 251 CC 30 SCAR 16.9
CC
30
Cov
77%
new
9.2
_get_bindings
9.0
_arity_in_range
8.3
CRAP 5.4
« blib/lib/Dallycot/Registry.pm ↑ Summary blib/lib/Dallycot/Value/String.pm »
1 package Dallycot::Value::Lambda;
2 30 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: An expression with an accompanying closure environment
5  
6 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 30 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use parent 'Dallycot::Value::Any';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 ▶use Promises qw(deferred collect);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 30 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 30 Readonly my $EXPRESSION => 0;
20 30 Readonly my $BINDINGS => 1;
21 30 Readonly my $BINDINGS_WITH_DEFAULTS => 2;
22 30 Readonly my $OPTIONS => 3;
23 30 Readonly my $CLOSURE_ENVIRONMENT => 4;
24 30 Readonly my $CLOSURE_NAMESPACES => 5;
25 30 Readonly my $CLOSURE_NAMESPACE_PATH => 6;
26  
27 sub new {
28 796 ▶ my ( $class, %options ) = @_;
Subroutine new: called CC=9 SCAR=22.2
Pod undocumented
29  
30 my ( $expression, $bindings, $bindings_with_defaults, $options,
31 $closure_environment, $closure_namespaces, $namespace_search_path, $engine )
32 = @options{
33 796 qw(expression bindings bindings_with_defaults options closure_environment closure_namespaces namespace_search_path engine)
34 };
35  
36 796 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
07960
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
37  
38 796 my ($closure_context);
39  
40 796 ▶ $bindings ||= [];
Condition50%1/2
$bindings ||= []
l!l
7960
MC/DC0%0/1
$bindings ||= []
$bindings
Truth table50%1/2
A
A= $bindings
Aresult
11
00
41 796 ▶ $bindings_with_defaults ||= [];
Condition100%2/2
$bindings_with_defaults ||= []
l!l
78313
MC/DC100%1/1
$bindings_with_defaults ||= []
$bindings_with_defaults
Truth table100%2/2
A
A= $bindings_with_defaults
Aresult
11
00
42 796 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
7960
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
43 796 ▶ $closure_environment ||= {};
Condition100%2/2
$closure_environment ||= {}
l!l
26770
MC/DC100%1/1
$closure_environment ||= {}
$closure_environment
Truth table100%2/2
A
A= $closure_environment
Aresult
11
00
44 796 ▶ $closure_namespaces ||= {};
Condition100%2/2
$closure_namespaces ||= {}
l!l
13783
MC/DC100%1/1
$closure_namespaces ||= {}
$closure_namespaces
Truth table100%2/2
A
A= $closure_namespaces
Aresult
11
00
45 796 ▶ $namespace_search_path ||= [];
Condition50%1/2
$namespace_search_path ||= []
l!l
0796
MC/DC0%0/1
$namespace_search_path ||= []
$namespace_search_path
Truth table50%1/2
A
A= $namespace_search_path
Aresult
11
00
46  
47 796 ▶ if ($engine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine) 754 42 796
48 754 $closure_context = $engine->context->make_closure($expression);
49 754 delete @{ $closure_context->environment }{ @$bindings, map { $_->[0] } @$bindings_with_defaults };
50 754 $closure_environment = $closure_context->environment;
51 754 $closure_namespaces = $closure_context->namespaces;
52 754 $namespace_search_path = $closure_context->namespace_search_path;
53 }
54  
55 796 return bless [
56 $expression, $bindings, $bindings_with_defaults, $options,
57 $closure_environment, $closure_namespaces, $namespace_search_path
58 ] => $class;
59 }
60  
61 51 ▶sub is_lambda { return 1; }
Subroutine is_lambda: called CC=1 SCAR=0.0
Pod undocumented
62  
63 sub id {
64 0 ▶ return '^^Lambda';
Subroutine id: not called CC=1 SCAR=6.9
Pod undocumented
65 }
66  
67 sub to_rdf {
68 0 ▶ my( $self, $parent_model ) = @_;
Subroutine to_rdf: not called CC=2 SCAR=17.9
Pod undocumented
69  
70 my $model = $parent_model -> child_model(
71 0 namespace_search_path => [ @{$self -> [$CLOSURE_NAMESPACE_PATH]} ],
72 prefixes => RDF::Trine::NamespaceMap->new($self -> [$CLOSURE_NAMESPACES]),
73 );
74  
75 0 my $bnode = $model -> bnode;
76 0 $model -> add_type($bnode, 'loc:Algorithm');
77 0 $model -> add_expression($bnode, $self -> [$EXPRESSION]);
78 $model -> add_list(
79 $bnode, 'loc:bindings',
80 0 (map { $self -> _binding_rdf($model, $_) } @{$self->[$BINDINGS]}),
81 0 (map { $self -> _binding_rdf($model, @$_) } @{$self->[$BINDINGS_WITH_DEFAULTS]})
82 );
83 0 foreach my $opt(keys %{$self->[$OPTIONS]}) {
84 $model -> add_option(
85 $bnode,
86 $opt,
87 0 $self->[$OPTIONS]->{$opt}->to_rdf($model)
88 );
89 }
90 }
91  
92 sub as_text {
93 0 ▶ my ($self) = @_;
Subroutine as_text: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ( $min, $max ) = $self->arity;
95 0 ▶ if ( $min < $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $max) { } 0 0 0
96 0 return "(lambda/$min..$max)";
97 }
98 else {
99 0 return "(lambda/$min)";
100 }
101 }
102  
103 sub arity {
104 2699 ▶ my ($self) = @_;
Subroutine arity: called CC=2 SCAR=6.9
Pod undocumented
105 2699 my $min = scalar( @{ $self->[$BINDINGS] } );
106 2699 my $more = scalar( @{ $self->[$BINDINGS_WITH_DEFAULTS] } );
107 2699 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2698 1 2699
108 2698 return ( $min, $min + $more );
109 }
110 else {
111 1 return $min + $more;
112 }
113 }
114  
115 sub min_arity {
116 266 ▶ my ($self) = @_;
Subroutine min_arity: called CC=1 SCAR=0.0
Pod undocumented
117  
118 266 return scalar( @{ $self->[$BINDINGS] } );
119 }
120  
121 sub _arity_in_range {
122 1349 ▶ my ( $self, $arity, $min, $max ) = @_;
Subroutine _arity_in_range: called CC=4 SCAR=21.1
123  
124 1349 ▶ if ( $arity < $min || $arity > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arity < $min or $arity > $max) 0 1349 1349
Condition33%1/3
$arity < $min or $arity > $max
l!l&&r!l&&!r
001349
MC/DC0%0/2
$arity < $min or $arity > $max
$arity < $min $arity > $max
Truth table33%1/3
A or B
A= $arity < $minB= $arity > $max
ABresult
1X1
011
000
125 0 ▶ if ( $min == $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 0 0
126 0 croak "Expected $min but found $arity arguments.";
127 }
128 else {
129 0 croak "Expected $min..$max but found $arity arguments.";
130 }
131 0 return;
132 }
133 1349 return 1;
134 }
135  
136 sub _options_are_good {
137 1349 ▶ my ( $self, $options ) = @_;
Subroutine _options_are_good: called CC=4 SCAR=14.6
138  
139 1349 ▶ if (%$options) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$options) 1 1348 1349
140 1 my @bad_options = grep { not exists ${ $self->[$OPTIONS] }{$_} } keys %$options;
141 1 ▶ if ( @bad_options > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@bad_options > 1) { } 0 1 1
elsif (@bad_options) { } 0 1 1
142 0 croak "Options " . join( ", ", sort(@bad_options) ) . " are not allowed.";
143 }
144 elsif (@bad_options) {
145 0 croak "Option " . $bad_options[0] . " is not allowed.";
146 }
147 }
148 1349 return 1;
149 }
150  
151 sub _is_placeholder {
152 1986 ▶ my ( $self, $obj ) = @_;
Subroutine _is_placeholder: called CC=2 SCAR=7.1
153 1986 ▶ return blessed($obj) && $obj->isa('Dallycot::AST::Placeholder');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Dallycot::AST::Placeholder')
!ll&&!rl&&r
01724262
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
154 }
155  
156 sub _get_bindings {
157 1349 ▶ my ( $self, $engine, @bindings ) = @_;
Subroutine _get_bindings: called CC=9 SCAR=22.0
158  
159 1349 my ( $min_arity, $max_arity ) = $self->arity;
160 1349 my $arity = scalar(@bindings);
161  
162 1349 my ( @new_bindings, @new_bindings_with_defaults, @filled_bindings, @filled_identifiers );
163  
164 1349 foreach my $idx ( 0 .. $min_arity - 1 ) {
165 1985 ▶ if ( $self->_is_placeholder( $bindings[$idx] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_placeholder($bindings[$idx])) { } 262 1723 1985
166 262 push @new_bindings, $self->[$BINDINGS][$idx];
167 }
168 else {
169 1723 push @filled_bindings, $bindings[$idx];
170 1723 push @filled_identifiers, $self->[$BINDINGS][$idx];
171 }
172 }
173 1349 ▶ if ( $arity > $min_arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arity > $min_arity) 1 1348 1349
174 1 foreach my $idx ( $min_arity .. $arity - 1 ) {
175 1 ▶ if ( $self->_is_placeholder( $bindings[$idx] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_placeholder($bindings[$idx])) { } 0 1 1
176 0 push @new_bindings_with_defaults, $self->[$BINDINGS_WITH_DEFAULTS][ $idx - $min_arity ];
177 }
178 else {
179 1 push @filled_bindings, $bindings[$idx];
180 1 push @filled_identifiers, $self->[$BINDINGS_WITH_DEFAULTS][ $idx - $min_arity ]->[0];
181 }
182 }
183 }
184 1349 ▶ if ( $max_arity > 0 && $arity < $max_arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_arity > 0 and $arity < $max_arity) 2 1347 1349
Condition100%3/3
$max_arity > 0 and $arity < $max_arity
!ll&&!rl&&r
26410832
MC/DC100%2/2
$max_arity > 0 and $arity < $max_arity
$max_arity > 0 $arity < $max_arity
Truth table100%3/3
A and B
A= $max_arity > 0B= $arity < $max_arity
ABresult
0X0
100
111
185 2 foreach my $idx ( $arity .. $max_arity - 1 ) {
186 2 push @filled_bindings, $self->[$BINDINGS_WITH_DEFAULTS][ $idx - $min_arity ]->[1];
187 2 push @filled_identifiers, $self->[$BINDINGS_WITH_DEFAULTS][ $idx - $min_arity ]->[0];
188 }
189 }
190  
191 1349 my %bindings;
192 1349 @bindings{@filled_identifiers} = map { $engine->execute($_) } @filled_bindings;
193  
194 1349 return ( \%bindings, \@new_bindings, \@new_bindings_with_defaults );
195 }
196  
197 sub _get_options {
198 1349 ▶ my ( $self, $engine, $options ) = @_;
Subroutine _get_options: called CC=1 SCAR=0.0
199  
200 1349 my @option_names = keys %$options;
201  
202 1349 my %ret_options;
203  
204 1349 @ret_options{ keys %$options } = map { $engine->execute($_) } values %$options;
205  
206 1349 return +{ %{ $self->[$OPTIONS] }, %ret_options };
207 }
208  
209 0 ▶sub child_nodes { return () }
Subroutine child_nodes: not called CC=1 SCAR=6.9
Pod undocumented
210  
211 sub apply {
212 1349 ▶ my ( $self, $engine, $options, @bindings ) = @_;
Subroutine apply: called CC=4 SCAR=13.9
Pod undocumented
213  
214 1349 my ( $min_arity, $max_arity ) = $self->arity;
215  
216 1349 my $arity = scalar(@bindings);
217  
218 1349 $self->_arity_in_range( $arity, $min_arity, $max_arity );
219 1349 $self->_options_are_good($options);
220 1349 my ( $filled_bindings, $new_bindings, $new_bindings_with_defaults )
221 = $self->_get_bindings( $engine, @bindings );
222 1349 my ($filled_options) = $self->_get_options( $engine, $options );
223  
224 1349 ▶ my %environment = ( %{ $self->[$CLOSURE_ENVIRONMENT] || {} }, %$filled_bindings );
Branch: 1 branch
BranchTrueFalseTotal
$self->[$CLOSURE_ENVIRONMENT] or {} 0 1349 1349
225  
226 1349 ▶ if ( @$new_bindings || @$new_bindings_with_defaults ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$new_bindings or @$new_bindings_with_defaults) { } 196 1153 1349
Condition66%2/3
@$new_bindings or @$new_bindings_with_defaults
l!l&&r!l&&!r
19601153
MC/DC50%1/2
@$new_bindings or @$new_bindings_with_defaults
@$new_bindings @$new_bindings_with_defaults
Truth table66%2/3
A or B
A= @$new_bindingsB= @$new_bindings_with_defaults
ABresult
1X1
011
000
227 196 my $promise = deferred;
228 196 $promise->resolve(
229 bless [
230 $self->[$EXPRESSION], $new_bindings, $new_bindings_with_defaults,
231 $filled_options, \%environment, $self->[$CLOSURE_NAMESPACES],
232 $self->[$CLOSURE_NAMESPACE_PATH]
233 ] => __PACKAGE__
234 );
235 196 return $promise->promise;
236 }
237 else {
238 my $new_engine = $engine->with_new_closure(
239 1153 +{ %environment, %{$filled_options} },
240 $self->[$CLOSURE_NAMESPACES],
241 $self->[$CLOSURE_NAMESPACE_PATH]
242 );
243 1153 return $new_engine->execute( $self->[$EXPRESSION] );
244 }
245 }
246  
247 30 1;
« blib/lib/Dallycot/Registry.pm ↑ Summary blib/lib/Dallycot/Value/String.pm »