blib/lib/Dallycot/Processor.pm

filter: Statementstmt 68.3% 121 / 177 Branchbran 36.1% 13 / 36 Conditioncond 46.6% 7 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 70.4% 31 / 44 Podpod 0.0% 0 / 18 totaltotal 58.5% 175 / 299 CC 26 SCAR 19.4
CC
26
Cov
62%
channel_send
30.0
channel_read
30.0
execute
8.8
CRAP 6.9
« blib/lib/Dallycot/Library/CLI.pm ↑ Summary blib/lib/Dallycot/Library/Linguistics.pm »
1 package Dallycot::Processor;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Run compiled Dallycot code.
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use Dallycot::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Dallycot::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Dallycot::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Dallycot::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use Dallycot::AST;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 9 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 9 ▶use Math::BigRat try => 'GMP';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 BEGIN {
29 9 ▶ $Dallycot::Processor::USING_XS = eval {
Subroutine BEGIN: called CC=2 SCAR=6.9
30 9 require Dallycot::Processor::XS;
31 8 1;
32 };
33  
34 9 ▶ if ($Dallycot::Processor::USING_XS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dallycot::Processor::USING_XS) { } 8 1 9
35 8 extends 'Dallycot::Processor::XS';
36 }
37 else {
38 1 extends 'Dallycot::Processor::PP';
39 }
40 }
41  
42 has context => (
43 is => 'ro',
44 isa => 'Dallycot::Context',
45 handles => [
46 qw[
47 has_assignment
48 get_assignment
49 add_assignment
50 has_namespace
51 get_namespace
52 add_namespace
53 get_namespace_search_path
54 append_namespace_search_path
55 ]
56 ],
57 default => sub {
58 74 ▶ Dallycot::Context->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
59 }
60 9 );
61  
62 has channels => (
63 is => 'ro',
64 isa => 'HashRef',
65 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 9 predicate => 'has_channels',
67 lazy => 1
68 );
69  
70 9 has max_cost => (
71 is => 'ro',
72 isa => 'Int',
73 default => 100_000
74 );
75  
76 9 has ignore_cost => (
77 is => 'ro',
78 isa => 'Bool',
79 default => 0
80 );
81  
82 9 has cost => (
83 is => 'ro',
84 isa => 'Int',
85 writer => '_cost',
86 default => 0
87 );
88  
89 9 has parent => (
90 is => 'ro',
91 predicate => 'has_parent',
92 isa => __PACKAGE__
93 );
94  
95 sub channel_send {
96 0 ▶ my ( $self, $channel, @items ) = @_;
Subroutine channel_send: not called CC=5 SCAR=34.0
Pod undocumented
97  
98 0 ▶ if ( $self->has_channels && exists( $self->channels->{$channel} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_channels and exists $self->channels->{$channel}) { } 0 0 0
elsif ($self->has_parent) { } 0 0 0
Condition0%0/3
$self->has_channels and exists $self->channels->{$channel}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_channels and exists $self->channels->{$channel}
$self->has_channels exists $self->channels->{$channel}
Truth table0%0/3
A and B
A= $self->has_channelsB= exists $self->channels->{$channel}
ABresult
0X0
100
111
99 0 ▶ if ( $self->channels->{$channel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channels->{$channel}) 0 0 0
100 0 $self->channels->{$channel}->send_data(@items);
101 }
102 }
103 elsif ( $self->has_parent ) {
104 0 $self->parent->channel_send( $channel, @items );
105 }
106 0 return;
107 }
108  
109 sub channel_read {
110 0 ▶ my ( $self, $channel, %options ) = @_;
Subroutine channel_read: not called CC=5 SCAR=34.0
Pod undocumented
111  
112 0 ▶ if ( $self->has_channels && exists( $self->channels->{$channel} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_channels and exists $self->channels->{$channel}) { } 0 0 0
elsif ($self->has_parent) { } 0 0 0
Condition0%0/3
$self->has_channels and exists $self->channels->{$channel}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_channels and exists $self->channels->{$channel}
$self->has_channels exists $self->channels->{$channel}
Truth table0%0/3
A and B
A= $self->has_channelsB= exists $self->channels->{$channel}
ABresult
0X0
100
111
113 0 ▶ if ( $self->channels->{$channel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->channels->{$channel}) 0 0 0
114 0 return $self->channels->{$channel}->receive_data(%options);
115 }
116 }
117 elsif ( $self->has_parent ) {
118 0 return $self->parent->channel_read( $channel, %options );
119 }
120 0 my $d = deferred;
121 0 $d->resolve( Dallycot::Value::String->new('') );
122 0 return $d->promise;
123 }
124  
125 sub create_channel {
126 0 ▶ my ( $self, $channel, $object ) = @_;
Subroutine create_channel: not called CC=1 SCAR=6.9
Pod undocumented
127  
128 0 $self->channels->{$channel} = $object;
129 0 return;
130 }
131  
132 sub with_child_scope {
133 698 ▶ my ($self) = @_;
Subroutine with_child_scope: called CC=1 SCAR=0.0
Pod undocumented
134  
135 698 my $ctx = $self->context;
136  
137 return __PACKAGE__->new(
138 parent => $self,
139 max_cost => $self->max_cost - $self->cost,
140 context => Dallycot::Context->new(
141 parent => $ctx,
142 698 namespace_search_path => [ @{ $ctx->namespace_search_path } ]
143 )
144 );
145 }
146  
147 sub with_new_closure {
148 1153 ▶ my ( $self, $environment, $namespaces, $search_path ) = @_;
Subroutine with_new_closure: called CC=2 SCAR=7.6
Pod undocumented
149  
150 return __PACKAGE__->new(
151 parent => $self,
152 max_cost => $self->max_cost - $self->cost,
153 context => Dallycot::Context->new(
154 environment => +{%$environment},
155 namespaces => +{%$namespaces},
156 1153 ▶ namespace_search_path => [ @{ ( $search_path // $self->context->namespace_search_path ) } ]
Condition33%1/3
$search_path // $self->context->namespace_search_path
l!l&&r!l&&!r
115300
MC/DC0%0/2
$search_path // $self->context->namespace_search_path
$search_path $self->context->namespace_search_path
Truth table33%1/3
A // B
A= $search_pathB= $self->context->namespace_search_path
ABresult
1X1
011
000
157 )
158 );
159 }
160  
161 sub _execute_expr {
162 1653 ▶ my ( $self, $expr ) = @_;
Subroutine _execute_expr: called CC=2 SCAR=6.9
163  
164 1653 ▶ if ( 'ARRAY' eq ref $expr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $expr) { } 622 1031 1653
165 622 return $self->execute(@$expr);
166 }
167 else {
168 1031 return $self->execute($expr);
169 }
170 }
171  
172 sub collect {
173 1164 ▶ my ( $self, @exprs ) = @_;
Subroutine collect: called CC=1 SCAR=0.0
Pod undocumented
174  
175 1653 return Promises::collect( map { $self->_execute_expr($_) } @exprs )->then(
176 sub {
177 1141 ▶ map {@$_} @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
178 }
179 1164 );
180 }
181  
182 # for now, just returns the original values
183 sub coerce {
184 84 ▶ my ( $self, $a, $b, $atype, $btype ) = @_;
Subroutine coerce: called CC=1 SCAR=0.0
Pod undocumented
185  
186 84 my $d = deferred;
187  
188 84 $d->resolve( $a, $b );
189  
190 84 return $d->promise;
191 }
192  
193 sub make_lambda {
194 754 ▶ my ( $self, $expression, $bindings, $bindings_with_defaults, $options ) = @_;
Subroutine make_lambda: called CC=4 SCAR=13.9
Pod undocumented
195  
196 754 ▶ $bindings ||= [];
Condition100%2/2
$bindings ||= []
l!l
416338
MC/DC100%1/1
$bindings ||= []
$bindings
Truth table100%2/2
A
A= $bindings
Aresult
11
00
197 754 ▶ $bindings_with_defaults ||= [];
Condition100%2/2
$bindings_with_defaults ||= []
l!l
364390
MC/DC100%1/1
$bindings_with_defaults ||= []
$bindings_with_defaults
Truth table100%2/2
A
A= $bindings_with_defaults
Aresult
11
00
198 754 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
346408
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
199  
200 754 return Dallycot::Value::Lambda->new(
201 expression => $expression,
202 bindings => $bindings,
203 bindings_with_defaults => $bindings_with_defaults,
204 options => $options,
205 engine => $self
206 );
207 }
208  
209 9 Readonly my $TRUE => Dallycot::Value::Boolean->new(1);
210 9 Readonly my $FALSE => Dallycot::Value::Boolean->new();
211 9 Readonly my $UNDEFINED => Dallycot::Value::Undefined->new;
212 9 Readonly my $ZERO => Dallycot::Value::Numeric->new( Math::BigRat->bzero() );
213 9 Readonly my $ONE => Dallycot::Value::Numeric->new( Math::BigRat->bone() );
214  
215 312 ▶sub TRUE () { return $TRUE }
Subroutine TRUE: called CC=1 SCAR=0.0
Pod undocumented
216 63 ▶sub FALSE () { return $FALSE }
Subroutine FALSE: called CC=1 SCAR=0.0
Pod undocumented
217 0 ▶sub UNDEFINED () { return $UNDEFINED }
Subroutine UNDEFINED: not called CC=1 SCAR=6.9
Pod undocumented
218 0 ▶sub ZERO () { return $ZERO }
Subroutine ZERO: not called CC=1 SCAR=6.9
Pod undocumented
219 0 ▶sub ONE () { return $ONE }
Subroutine ONE: not called CC=1 SCAR=6.9
Pod undocumented
220  
221 sub _execute_loop {
222 0 ▶ my ( $self, $deferred, $expected_types, $stmt, @stmts ) = @_;
Subroutine _execute_loop: not called CC=2 SCAR=17.9
223  
224 0 ▶ if ( !@stmts ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stmts) 0 0 0
225 $self->_execute( $expected_types, $stmt )
226 0 ▶ ->done( sub { $deferred->resolve(@_); }, sub { $deferred->reject(@_); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 0 return;
228 }
229 $self->_execute( ['Any'], $stmt )
230 0 ▶ ->done( sub { $self->_execute_loop( $deferred, $expected_types, @stmts ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 ▶ sub { $deferred->reject(@_); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 return;
233 }
234  
235 sub _execute {
236 9032 ▶ my ( $self, $expected_types, $ast ) = @_;
Subroutine _execute: called CC=4 SCAR=20.8
237  
238 9032 my $promise = eval {
239 9032 ▶ if ( $self->add_cost(1) > $self->max_cost ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add_cost(1) > $self->max_cost) { } 0 9032 9032
240 0 my $d = deferred;
241 0 $d->reject("Exceeded maximum evaluation cost");
242 0 $d->promise;
243 }
244 else {
245 9032 $ast->execute($self);
246 }
247 };
248  
249 9032 ▶ return $promise if $promise;
Branch: 1 branch
BranchTrueFalseTotal
if $promise 9032 0 9032
250  
251 0 my $d = deferred;
252 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
253 0 $d->reject($@);
254 }
255 else {
256 0 $d->reject("Unable to evaluate");
257 }
258 0 return $d->promise;
259 }
260  
261 sub execute {
262 9032 ▶ my ( $self, $ast, @ast ) = @_;
Subroutine execute: called CC=6 SCAR=21.8
Pod undocumented
263  
264 9032 ▶ if(!defined $ast) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ast) 0 9032 9032
265 0 my $d = deferred;
266 0 $d -> resolve(UNDEFINED);
267 0 return $d -> promise;
268 }
269  
270 9032 ▶ if ( !blessed $ast) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($ast)) 0 9032 9032
271 0 print STDERR "$ast not blessed at ", join( " ", caller ), "\n";
272 }
273  
274 9032 my @expected_types = ('Any');
275  
276 9032 ▶ if (@ast) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ast) 1164 7868 9032
277 1164 my $potential_types = pop @ast;
278  
279 1164 ▶ if ( 'ARRAY' eq ref $potential_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $potential_types) { } 1164 0 1164
280 1164 @expected_types = @$potential_types;
281 }
282 else {
283 0 push @ast, $potential_types;
284 }
285 }
286  
287 9032 ▶ if (@ast) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ast) { } 0 9032 9032
288 0 my $d = deferred;
289 0 $self->_execute_loop( $d, \@expected_types, $ast, @ast );
290 0 return $d->promise;
291 }
292 else {
293 9032 return $self->_execute( \@expected_types, $ast );
294 }
295 }
296  
297 sub compose_lambdas {
298 52 ▶ my ( $self, @lambdas ) = @_;
Subroutine compose_lambdas: called CC=2 SCAR=6.9
Pod undocumented
299 52 @lambdas = reverse @lambdas;
300  
301 52 my $new_engine = $self->with_child_scope;
302  
303 52 my $expression = Dallycot::AST::Fetch->new('#');
304  
305 52 for my $idx ( 0 .. $#lambdas ) {
306 73 $new_engine->context->add_assignment( "__lambda_" . $idx, $lambdas[$idx] );
307 73 $expression
308 = Dallycot::AST::Apply->new( Dallycot::AST::Fetch->new( '__lambda_' . $idx ), [$expression] );
309 }
310  
311 52 return $new_engine->make_lambda( $expression, ['#'] );
312 }
313  
314 sub _add_filter_to_context {
315 0 ▶ my ( $engine, $idx, $filter, $expression ) = @_;
Subroutine _add_filter_to_context: not called CC=1 SCAR=6.9
316  
317 0 $engine->context->add_assignment( "__lambda_" . $idx, $filter );
318 0 return Dallycot::AST::Apply->new( Dallycot::AST::Fetch->new( '__lambda_' . $idx ), [$expression] );
319 }
320  
321 sub compose_filters {
322 19 ▶ my ( $self, @filters ) = @_;
Subroutine compose_filters: called CC=2 SCAR=11.1
Pod undocumented
323  
324 19 ▶ if ( @filters == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@filters == 1) 19 0 19
325 19 return $filters[0];
326 }
327  
328 0 my $new_engine = $self->with_child_scope;
329  
330 0 my $expression = Dallycot::AST::Fetch->new('#');
331 0 my $idx = 0;
332 0 my @applications = map { _add_filter_to_context( $new_engine, $idx++, $_, $expression ) } @filters;
333  
334 0 return $new_engine->make_lambda( Dallycot::AST::All->new(@applications), ['#'] );
335 }
336  
337 sub make_map {
338 33 ▶ my ( $self, $transform ) = @_;
Subroutine make_map: called CC=1 SCAR=0.0
Pod undocumented
339  
340 33 return $self->execute(
341 Dallycot::AST::Apply->new(
342 Dallycot::Value::URI->new('http://www.dallycot.net/ns/loc/1.0#map'),
343 [ $transform, Dallycot::AST::Placeholder->new ], {}
344 )
345 );
346 }
347  
348 sub make_filter {
349 18 ▶ my ( $self, $selector ) = @_;
Subroutine make_filter: called CC=1 SCAR=0.0
Pod undocumented
350  
351 18 return $self->execute(
352 Dallycot::AST::Apply->new(
353 Dallycot::Value::URI->new('http://www.dallycot.net/ns/loc/1.0#filter'),
354 [ $selector, Dallycot::AST::Placeholder->new ], {}
355 )
356 );
357 }
358  
359 9 __PACKAGE__->meta->make_immutable;
360  
361 9 require Dallycot::Library::Core;
362  
363 9 1;
« blib/lib/Dallycot/Library/CLI.pm ↑ Summary blib/lib/Dallycot/Library/Linguistics.pm »