| 1 | package Dallycot::AST::Apply; | ||||||||||||||||||||||||||||
| 2 | 29 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Apply bindings to lambda | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 29 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 29 | ▶ | use parent 'Dallycot::AST'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 29 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 29 | ▶ | use Promises qw(deferred); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 29 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 29 | Readonly my $EXPRESSION => 0; | |||||||||||||||||||||||||||
| 17 | 29 | Readonly my $BINDINGS => 1; | |||||||||||||||||||||||||||
| 18 | 29 | Readonly my $OPTIONS => 2; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||||||||||
| 21 | 139 | ▶ | my ( $class, $expression, $bindings, $options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 139 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 24 | 139 | ▶ | $bindings //= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$bindings //= []
MC/DC0%0/1
$bindings //= []
$bindings
Truth table50%1/2
A
A= $bindings
| |||||||||||||||||||||||||||||
| 25 | 139 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 26 | 139 | return bless [ $expression, $bindings, $options ] => $class; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub to_rdf { | ||||||||||||||||||||||||||||
| 30 | 4 | ▶ | my($self, $model) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_rdf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # node -> expression_set -> [ ... ] | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | 4 | return $model -> apply( | |||||||||||||||||||||||||||
| 36 | $self -> [0], | ||||||||||||||||||||||||||||
| 37 | $self -> [1], | ||||||||||||||||||||||||||||
| 38 | $self -> [2] | ||||||||||||||||||||||||||||
| 39 | ) | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub simplify { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
simplify: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | return bless [ | ||||||||||||||||||||||||||||
| 46 | $self->[$EXPRESSION]->simplify, | ||||||||||||||||||||||||||||
| 47 | 0 | [ map { $_->simplify } @{ $self->[$BINDINGS] } ], | |||||||||||||||||||||||||||
| 48 | $self->[$OPTIONS] | ||||||||||||||||||||||||||||
| 49 | ] => __PACKAGE__; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub child_nodes { | ||||||||||||||||||||||||||||
| 53 | 811 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
child_nodes: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 811 | ▶ | return $self->[$EXPRESSION], @{ $self->[$BINDINGS] || [] }, values %{ $self->[$OPTIONS] || {} }; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub to_string { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | return | ||||||||||||||||||||||||||||
| 62 | "(" | ||||||||||||||||||||||||||||
| 63 | . $self->[$EXPRESSION]->to_string . ")(" | ||||||||||||||||||||||||||||
| 64 | . join( | ||||||||||||||||||||||||||||
| 65 | ", ", | ||||||||||||||||||||||||||||
| 66 | 0 | ( map { $_->to_string } @{ $self->[$BINDINGS] } ), | |||||||||||||||||||||||||||
| 67 | 0 | ( map { $_ . " -> " . $self->[$OPTIONS]->{$_}->to_string } | |||||||||||||||||||||||||||
| 68 | 0 | sort keys %{ $self->[$OPTIONS] } | |||||||||||||||||||||||||||
| 69 | ) | ||||||||||||||||||||||||||||
| 70 | ) . ")"; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub execute { | ||||||||||||||||||||||||||||
| 74 | 1185 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1185 | my $expr = $self->[$EXPRESSION]; | |||||||||||||||||||||||||||
| 77 | 1185 | ▶ | if ( $expr->isa('Dallycot::Value') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 123 | $expr = bless [$expr] => 'Dallycot::AST::Identity'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | return $engine->execute($expr)->then( | ||||||||||||||||||||||||||||
| 82 | sub { | ||||||||||||||||||||||||||||
| 83 | 1183 | ▶ | my ($lambda) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 84 | 1183 | ▶ | if ( !$lambda ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 0 | croak "Undefined value can not be a function."; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | elsif ( $lambda->can('apply') ) { | ||||||||||||||||||||||||||||
| 88 | 1183 | my @bindings = @{ $self->[$BINDINGS] }; | |||||||||||||||||||||||||||
| 89 | 1183 | ▶ | if ( @bindings && $bindings[-1]->isa('Dallycot::AST::FullPlaceholder') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@bindings and $bindings[-1]->isa("Dallycot::AST::FullPlaceholder")
MC/DC100%2/2
@bindings and $bindings[-1]->isa("Dallycot::AST::FullPlaceholder")
@bindings
$bindings[-1]->isa("Dallycot::AST::FullPlaceholder")
Truth table100%3/3
A and B
A= @bindingsB= $bindings[-1]->isa("Dallycot::AST::FullPlaceholder")
| |||||||||||||||||||||||||||||
| 90 | 90 | ▶ | if ( $lambda->min_arity < @bindings ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # we have something like f(..., ___) indicating we *want* a lambda | ||||||||||||||||||||||||||||
| 93 | # since we don't have room for any placeholders, we'll just create | ||||||||||||||||||||||||||||
| 94 | # a lambda and return it | ||||||||||||||||||||||||||||
| 95 | # we need to evaluate any options or bindings first | ||||||||||||||||||||||||||||
| 96 | 1 | pop @bindings; | |||||||||||||||||||||||||||
| 97 | 1 | return Dallycot::AST::Lambda->new( | |||||||||||||||||||||||||||
| 98 | Dallycot::AST::Apply->new( $self->[$EXPRESSION], \@bindings, $self->[$OPTIONS] ) ) | ||||||||||||||||||||||||||||
| 99 | ->execute($engine); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 89 | pop @bindings; | |||||||||||||||||||||||||||
| 103 | 89 | push @bindings, | |||||||||||||||||||||||||||
| 104 | ( bless [] => 'Dallycot::AST::Placeholder' ) x ( $lambda->min_arity - @bindings ); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 1182 | return $lambda->apply( $engine, { %{ $self->[$OPTIONS] } }, @bindings ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | 0 | croak "Value of type " . $lambda->type . " can not be used as a function."; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 1185 | ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 29 | 1; | |||||||||||||||||||||||||||