FormulaEngine/Namespace.pm

filter: Statementstmt 80.9% 98 / 121 Branchbran 67.2% 39 / 58 Conditioncond 38.2% 13 / 34 MC/DCmcdc 28.5% 6 / 21 Subroutinesub 86.3% 19 / 22 Podpod 100% 9 / 9 totaltotal 69.4% 184 / 265 CC 43 SCAR 21.3
CC
43
Cov
70%
clone
30.0
find_methods
20.7
simplify_call
10.0
CRAP 8.4
« FormulaEngine/Parser/ContextUtil.pm ↑ Summary FormulaEngine/Formula.pm »
1 package Language::FormulaEngine::Namespace;
2 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶require MRO::Compat if $] lt '5.009005';
Branch: 1 branch
BranchTrueFalseTotal
if $] lt "5.009005" 0 7 7
6 7 ▶use Language::FormulaEngine::Error ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Object holding function and variable names
10 7 our $VERSION = '0.08'; # VERSION
11  
12  
13 7 ▶has variables => ( is => 'rw', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
14 8 ▶has constants => ( is => 'rw', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 7 has die_on_unknown_value => ( is => 'rw' );
16  
17  
18 0 ▶sub clone {
Subroutine clone: not called CC=5 SCAR=34.0
Pod documented
19 0 my $self= shift;
20 0 ▶ my %attrs= @_==1 && ref $_[0] eq 'HASH'? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
21 0 ▶ $attrs{variables} ||= { %{ $self->variables } };
Condition0%0/2
$attrs{"variables"} ||= {%{$self->variables;}}
l!l
00
MC/DC0%0/1
$attrs{"variables"} ||= {%{$self->variables;}}
$attrs{"variables"}
Truth table0%0/2
A
A= $attrs{"variables"}
Aresult
11
00
22 0 ▶ $attrs{constants} ||= { %{ $self->constants } };
Condition0%0/2
$attrs{"constants"} ||= {%{$self->constants;}}
l!l
00
MC/DC0%0/1
$attrs{"constants"} ||= {%{$self->constants;}}
$attrs{"constants"}
Truth table0%0/2
A
A= $attrs{"constants"}
Aresult
11
00
23 0 $self->new( %$self, %attrs );
24 }
25  
26 # potentially hot method
27 130 ▶sub clone_and_merge {
Subroutine clone_and_merge: called CC=5 SCAR=18.3
Pod documented
28 130 my $self= shift;
29 0 ▶ my %attrs= @_==1 && ref $_[0] eq 'HASH'? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 130 130
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
13000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
30 130 ▶ $attrs{variables}= { %{ $self->variables }, ($attrs{variables}? %{ $attrs{variables} } : () ) };
Branch: 1 branch
BranchTrueFalseTotal
$attrs{'variables'} ? : 130 0 130
31 0 ▶ $attrs{constants}= { %{ $self->constants }, ($attrs{constants}? %{ $attrs{constants} } : () ) };
Branch: 1 branch
BranchTrueFalseTotal
$attrs{'constants'} ? : 0 130 130
32 130 $self->new( %$self, %attrs );
33 }
34  
35  
36 80 ▶sub get_constant {
Subroutine get_constant: called CC=1 SCAR=0.0
Pod documented
37 80 my ($self, $name)= @_;
38 80 $name= lc $name;
39 80 $self->constants->{$name};
40 }
41  
42 83 ▶sub get_value {
Subroutine get_value: called CC=4 SCAR=14.0
Pod documented
43 83 my ($self, $name)= @_;
44 83 $name= lc $name;
45 83 my $set= $self->variables;
46 83 ▶ return $set->{$name} if exists $set->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $set->{$name} 77 6 83
47 6 $set= $self->constants;
48 6 ▶ return $set->{$name} if exists $set->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $set->{$name} 0 6 6
49 6 ▶ die ErrREF("Unknown variable or constant '$_[1]'")
Branch: 1 branch
BranchTrueFalseTotal
if $self->die_on_unknown_value 0 6 6
50 if $self->die_on_unknown_value;
51 6 return undef;
52 }
53  
54 7 our %is_pure_function;
55 0 ▶sub FETCH_CODE_ATTRIBUTES {
Subroutine FETCH_CODE_ATTRIBUTES: not called CC=2 SCAR=17.9
56 0 my ($class, $ref)= @_;
57 0 ▶ return $class->maybe::mext::method($ref), ($is_pure_function{0+$ref}? ('Pure') : ());
Branch: 1 branch
BranchTrueFalseTotal
$Language::FormulaEngine::Namespace::is_pure_function{0 + $ref} ? : 0 0 0
58 }
59 372 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=2 SCAR=7.0
60 372 my ($class, $ref, @attr)= @_;
61 372 my $n= @attr;
62 372 @attr= grep $_ ne 'Pure', @attr;
63 372 ▶ $is_pure_function{0+$ref}= 1 if $n > @attr;
Branch: 1 branch
BranchTrueFalseTotal
if $n > @attr 372 0 372
64 372 $class->maybe::next::method($ref, @attr);
65 }
66  
67 256 ▶sub get_function {
Subroutine get_function: called CC=4 SCAR=14.3
Pod documented
68 256 my ($self, $name)= @_;
69 256 $name= lc $name;
70 # The value 0E0 is a placeholder for "no such function"
71 256 ▶ my $info= $self->{_function_cache}{$name} ||= do {
Condition66%2/3
$self->{"_function_cache"}{$name} ||= do { my(%tmp) = $self->_collect_function_info($name); keys %tmp ? \%tmp : "0E0" }
l!l&&r!l&&!r
232330
MC/DC0%0/2
$self->{"_function_cache"}{$name} ||= do { my(%tmp) = $self->_collect_function_info($name); keys %tmp ? \%tmp : "0E0" }
$self->{"_function_cache"}{$name} do { my(%tmp) = $self->_collect_function_info($name); keys %tmp ? \%tmp : "0E0" }
Truth table66%2/3
A ||= B
A= $self->{"_function_cache"}{$name}B= do { my(%tmp) = $self->_collect_function_info($name); keys %tmp ? \%tmp : "0E0" }
ABresult
1X1
011
000
72 233 my %tmp= $self->_collect_function_info($name);
73 233 ▶ keys %tmp? \%tmp : '0E0';
Branch: 1 branch
BranchTrueFalseTotal
keys %tmp ? : 233 0 233
74 };
75 256 ▶ return ref $info? $info : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $info ? : 256 0 256
76 }
77  
78 233 ▶sub _collect_function_info {
Subroutine _collect_function_info: called CC=8 SCAR=20.8
79 233 my ($self, $name)= @_;
80 233 my $fn= $self->can("fn_$name");
81 233 my $sm= $self->can("simplify_$name");
82 233 my $ev= $self->can("nodeval_$name");
83 my $pure= $fn? $is_pure_function{0+$fn}
84 233 ▶ : $ev? $is_pure_function{0+$ev}
Branch: 2 branches
BranchTrueFalseTotal
$fn ? : 213 20 233
$ev ? : 20 0 20
85 : 0;
86 233 my $pl= $self->can("perlgen_$name");
87 return
88 233 ▶ ($pure? ( is_pure_function => $pure ) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$pure ? : 217 16 233
$fn ? : 213 20 233
$sm ? : 35 198 233
$ev ? : 20 213 233
$pl ? : 57 176 233
89 ($fn? ( native => $fn ) : ()),
90 ($sm? ( simplify => $sm ) : ()),
91 ($ev? ( evaluator => $ev ) : ()),
92 ($pl? ( perl_generator => $pl ) : ()),
93 $self->maybe::next::method($name);
94 }
95  
96  
97 171 ▶sub evaluate_call {
Subroutine evaluate_call: called CC=4 SCAR=14.0
Pod documented
98 171 my ($self, $call)= @_;
99 171 my $name= $call->function_name;
100 171 ▶ my $info= $self->get_function($name)
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->get_function($name) or die ErrNAME("Unknown function '${name}'") 171 0 171
101 or die ErrNAME("Unknown function '$name'");
102 # If the namespace supplies a special evaluator method, use that
103 171 ▶ if (my $eval= $info->{evaluator}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $eval = $info->{'evaluator'}) { } 14 157 171
elsif (my $fn = $info->{'native'}) { } 157 0 157
104 14 return $self->$eval($call);
105 }
106 # Else if the namespace supplies a native plain-old-function, convert the parameters
107 # from parse nodes to plain values and then call the function.
108 elsif (my $fn= $info->{native}) {
109 # The function might be a perl builtin, so need to activate the same
110 # warning flags that would be used by the compiled version.
111 7 ▶ use warnings FATAL => 'numeric', 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 157 my @args= map $_->evaluate($self), @{ $call->parameters };
113 157 return $fn->(@args);
114 }
115 # Else the definition of the function is incomplete.
116 0 die ErrNAME("Incomplete function '$name' cannot be evaluated");
117 }
118  
119  
120 15 ▶sub simplify_call {
Subroutine simplify_call: called CC=10 SCAR=23.0
Pod documented
121 15 my ($self, $call)= @_;
122 15 my ($same, $const)= (1,1);
123 15 my @s_params= @{ $call->parameters };
124 15 for (@s_params) {
125 27 my $s= $_->simplify($self);
126 27 ▶ $same &&= ($s == $_);
Condition100%3/3
$same &&= $s == $_
!ll&&!rl&&r
7119
MC/DC100%2/2
$same &&= $s == $_
$same $s == $_
Truth table100%3/3
A &&= B
A= $sameB= $s == $_
ABresult
0X0
100
111
127 27 ▶ $const &&= $s->is_constant;
Condition100%3/3
$const &&= $s->is_constant
!ll&&!rl&&r
5913
MC/DC100%2/2
$const &&= $s->is_constant
$const $s->is_constant
Truth table100%3/3
A &&= B
A= $constB= $s->is_constant
ABresult
0X0
100
111
128 27 $_= $s;
129 }
130 15 ▶ $call= Language::FormulaEngine::Parser::Node::Call->new($call->function_name, \@s_params)
Branch: 1 branch
BranchTrueFalseTotal
unless $same 11 4 15
131 unless $same;
132 15 ▶ if (my $info= $self->get_function($call->function_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->get_function($call->function_name)) 15 0 15
133 15 ▶ if (my $method= $info->{simplify}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $method = $info->{'simplify'}) { } 9 6 15
elsif ($const and $info->{'is_pure_function'}) { } 2 6 8
134 9 return $self->$method($call);
135 }
136 # Are they all constants being passed to a pure function?
137 ▶ elsif ($const && $info->{is_pure_function}) {
Condition100%3/3
$const and $info->{"is_pure_function"}
!ll&&!rl&&r
222
MC/DC100%2/2
$const and $info->{"is_pure_function"}
$const $info->{"is_pure_function"}
Truth table100%3/3
A and B
A= $constB= $info->{"is_pure_function"}
ABresult
0X0
100
111
138 2 my $val= $self->evaluate_call($call);
139 2 ▶ return !defined $val? $call : $self->_parse_node_for_value($val);
Branch: 1 branch
BranchTrueFalseTotal
!defined($val) ? : 0 2 2
140 }
141 }
142 4 return $call;
143 }
144  
145 15 ▶sub simplify_symref {
Subroutine simplify_symref: called CC=2 SCAR=6.9
Pod documented
146 15 my ($self, $symref)= @_;
147 15 local $self->{die_on_unknown_value}= 0;
148 15 my $val= $self->get_value($symref->symbol_name);
149 15 ▶ return !defined $val? $symref : $self->_parse_node_for_value($val);
Branch: 1 branch
BranchTrueFalseTotal
!defined($val) ? : 6 9 15
150 }
151 11 ▶sub _parse_node_for_value {
Subroutine _parse_node_for_value: called CC=3 SCAR=13.3
152 11 my ($self, $val)= @_;
153 # Take a guess at whether this should be a number or string...
154 11 ▶ if (Scalar::Util::looks_like_number($val) && 0+$val eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($val) and 0 + $val eq $val) { } 11 0 11
Condition33%1/3
&Scalar::Util::looks_like_number($val) and 0 + $val eq $val
!ll&&!rl&&r
0011
MC/DC0%0/2
&Scalar::Util::looks_like_number($val) and 0 + $val eq $val
&Scalar::Util::looks_like_number($val) 0 + $val eq $val
Truth table33%1/3
A and B
A= &Scalar::Util::looks_like_number($val)B= 0 + $val eq $val
ABresult
0X0
100
111
155 11 return Language::FormulaEngine::Parser::Node::Number->new($val);
156 } else {
157 0 return Language::FormulaEngine::Parser::Node::String->new($val);
158 }
159 }
160  
161  
162 0 ▶sub find_methods {
Subroutine find_methods: not called CC=5 SCAR=30.3
Pod documented
163 0 my ($self, $pattern)= @_;
164 0 ▶ my $todo= mro::get_linear_isa(ref $self || $self);
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
165 0 my (%seen, @ret);
166 0 for my $pkg (@$todo) {
167 0 ▶ my $stash= do { no strict 'refs'; \%{$pkg.'::'} };
Subroutine BEGIN: called CC=1 SCAR=1.2
168 0 ▶ push @ret, grep +($_ =~ $pattern and defined $stash->{$_}{CODE} and !$seen{$_}++), keys %$stash;
Condition0%0/6
$_ =~ /$pattern/ && defined $stash->{$_}{'CODE'} && !$seen{$_}++
!ll&&!rl&&r
000
$_ =~ /$pattern/ and defined $stash->{$_}{"CODE"}
!ll&&!rl&&r
000
MC/DC0%0/3
$_ =~ /$pattern/ && defined $stash->{$_}{'CODE'} && !$seen{$_}++
$_ =~ /$pattern/ defined $stash->{$_}{"CODE"} !$seen{$_}++
Truth table0%0/4
A and B && not(C)
A= $_ =~ /$pattern/B= defined $stash->{$_}{"CODE"}C= !$seen{$_}++
ABCresult
0XX0
10X0
1100
1111
169 }
170 0 \@ret;
171 }
172  
173  
174  
175 7 1;
176  
177 __END__
« FormulaEngine/Parser/ContextUtil.pm ↑ Summary FormulaEngine/Formula.pm »