| 1 | package Language::FormulaEngine::Formula; | ||||||||||||||||||||||||||||||||||||||||
| 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 overload '""' => sub { shift->to_string }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | has engine => ( is => 'rw', required => 1 ); | |||||||||||||||||||||||||||||||||||||||
| 8 | 7 | has orig_text => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||
| 9 | 7 | has parse_tree => ( is => 'rw', required => 1 ); | |||||||||||||||||||||||||||||||||||||||
| 10 | 7 | has functions => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||||||||||
| 11 | 7 | has symbols => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub _count_refs { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_count_refs: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | my $node= shift; | |||||||||||||||||||||||||||||||||||||||
| 15 | 2 | my (%fn_set, %var_set, @todo); | |||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | while (defined $node) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | $var_set{$node->symbol_name}++ if $node->can('symbol_name'); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | if ($node->can('function_name')) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | $fn_set{$node->function_name}++; | |||||||||||||||||||||||||||||||||||||||
| 20 | 2 | push @todo, @{ $node->parameters }; | |||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | $node= pop @todo; | |||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | (\%fn_set, \%var_set); | |||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _build_symbols { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_symbols: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||
| 29 | 0 | @{$self}{'functions','symbols'}= _count_refs( $self->parse_tree ); | |||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $self->{symbols}; | |||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub _build_functions { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_functions: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||
| 35 | 2 | @{$self}{'functions','symbols'}= _count_refs( $self->parse_tree ); | |||||||||||||||||||||||||||||||||||||||
| 36 | 2 | $self->{functions}; | |||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | sub evaluate { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=6 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my ($self, $ns_or_vars)= @_; | |||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $ns= !$ns_or_vars? $self->engine->namespace | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition33%1/3
not ref $ns_or_vars and @_ > 2
MC/DC0%0/2
not ref $ns_or_vars and @_ > 2
not ref $ns_or_vars
@_ > 2
Truth table33%1/3
not(A) and B
A= not ref $ns_or_varsB= @_ > 2
| |||||||||||||||||||||||||||||||||||||||||
| 43 | : !ref $ns_or_vars && @_ > 2? $self->engine->namespace->clone_and_merge(variables => { @_[1..$#_] }) | ||||||||||||||||||||||||||||||||||||||||
| 44 | : ref $ns_or_vars eq 'HASH'? $self->engine->namespace->clone_and_merge(variables => $ns_or_vars) | ||||||||||||||||||||||||||||||||||||||||
| 45 | : ref($ns_or_vars)->can('get_function')? $ns_or_vars | ||||||||||||||||||||||||||||||||||||||||
| 46 | : croak "Can't evaluate for $ns_or_vars"; | ||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | return $self->parse_tree->evaluate($ns); | |||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub simplify { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
simplify: called CC=7 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | my ($self, $ns_or_vars)= @_; | |||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | my $ns= !$ns_or_vars? $self->engine->namespace | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
not ref $ns_or_vars and @_ > 2
MC/DC0%0/2
not ref $ns_or_vars and @_ > 2
not ref $ns_or_vars
@_ > 2
Truth table0%0/3
not(A) and B
A= not ref $ns_or_varsB= @_ > 2
| |||||||||||||||||||||||||||||||||||||||||
| 54 | : !ref $ns_or_vars && @_ > 2? $self->engine->namespace->clone_and_merge(variables => { @_[1..$#_] }) | ||||||||||||||||||||||||||||||||||||||||
| 55 | : ref $ns_or_vars eq 'HASH'? $self->engine->namespace->clone_and_merge(variables => $ns_or_vars) | ||||||||||||||||||||||||||||||||||||||||
| 56 | : ref($ns_or_vars)->can('get_function')? $ns_or_vars | ||||||||||||||||||||||||||||||||||||||||
| 57 | : croak "Can't evaluate for $ns_or_vars"; | ||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | my $parse_tree= $self->parse_tree->simplify($ns); | |||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | return $self if $parse_tree == $self->parse_tree; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | return Language::FormulaEngine::Formula->new( | |||||||||||||||||||||||||||||||||||||||
| 61 | engine => $self->engine, | ||||||||||||||||||||||||||||||||||||||||
| 62 | parse_tree => $parse_tree, | ||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | sub compile { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my ($self, $subname)= @_; | |||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $self->engine->compiler->compile($self->parse_tree, $subname) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 70 | or croak $self->engine->compiler->error; | ||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | ▶ | sub deparse { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
deparse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||
| 76 | 8 | $self->parse_tree->deparse($self->engine->parser); | |||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||
| 80 | 14 | ▶ | sub to_string { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 81 | 14 | my $orig= $_[0]->orig_text; | |||||||||||||||||||||||||||||||||||||||
| 82 | 14 | ▶ | return defined $orig? $orig : $_[0]->deparse; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||||||||||||||