| 1 | package Language::FormulaEngine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | use Module::Runtime 'require_module'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Parser/Interpreter/Compiler for simple spreadsheet formula language | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | has parser => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | builder => sub {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_parser: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | coerce => sub { _coerce_instance($_[0], 'parse', 'Language::FormulaEngine::Parser') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | has namespace => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | builder => sub {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_namespace: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | coerce => sub { _coerce_instance($_[0], 'get_function', 'Language::FormulaEngine::Namespace::Default') }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | trigger => sub { my ($self, $val)= @_; $self->compiler->namespace($val) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | has compiler => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | builder => sub {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | coerce => sub { _coerce_instance($_[0], 'compile', 'Language::FormulaEngine::Compiler') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | my $self= shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | $self->compiler->namespace($self->namespace); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | ▶ | sub _coerce_instance { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_instance: called CC=12 SCAR=28.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 21 | my ($thing, $req_method, $default_class)= @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 21 | ▶ | return $thing if ref $thing and ref($thing)->can($req_method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $thing and ref($thing)->can($req_method)
MC/DC100%2/2
ref $thing and ref($thing)->can($req_method)
ref $thing
ref($thing)->can($req_method)
Truth table100%3/3
A and B
A= ref $thingB= ref($thing)->can($req_method)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | my $class= !defined $thing? $default_class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 20 | ▶ | : ref $thing eq 'HASH'? $thing->{CLASS} || $default_class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition33%2/6
$thing->{"CLASS"} or $default_class
$req_method eq "get_function" and $thing =~ /^[0-9]+$/
MC/DC0%0/2
$thing->{"CLASS"} or $default_class
$thing->{"CLASS"}
$default_class
MC/DC0%0/2
$req_method eq "get_function" and $thing =~ /^[0-9]+$/
$req_method eq "get_function"
$thing =~ /^[0-9]+$/
Truth table66%2/3
A or B
A= $thing->{"CLASS"}B= $default_class
Truth table0%0/3
A and B
A= $req_method eq "get_function"B= $thing =~ /^[0-9]+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | : ref $thing? $default_class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | : ($req_method eq 'get_function' && $thing =~ /^[0-9]+$/)? "Language::FormulaEngine::Namespace::Default::V$thing" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | : $thing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 20 | ▶ | require_module($class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | unless $class->can($req_method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | my @args= !ref $thing? () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | : (ref $thing eq 'ARRAY')? @$thing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | : $thing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 20 | return $class->new(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | my ($self, $text, $error_ref)= @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | unless ($self->parser->parse($text)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | die $self->parser->error unless $error_ref; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $$error_ref= $self->parser->error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | return Language::FormulaEngine::Formula->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | engine => $self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | orig_text => $text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | parse_tree => $self->parser->parse_tree, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | functions => $self->parser->functions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | symbols => $self->parser->symbols, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 126 | ▶ | sub evaluate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 126 | my ($self, $text, $vars)= @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 126 | ▶ | $self->parser->parse($text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | or die $self->parser->error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 126 | my $ns= $self->namespace; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 126 | ▶ | $ns= $ns->clone_and_merge(variables => $vars) if $vars && %$vars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$vars and %$vars
MC/DC0%0/2
$vars and %$vars
$vars
%$vars
Truth table33%1/3
A and B
A= $varsB= %$vars
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 126 | return $self->parser->parse_tree->evaluate($ns); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 129 | ▶ | sub compile { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 129 | my ($self, $text)= @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 129 | ▶ | $self->parser->parse($text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | or die $self->parser->error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 129 | $self->compiler->namespace($self->namespace); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 129 | ▶ | $self->compiler->compile($self->parser->parse_tree) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | or die $self->compiler->error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | require Language::FormulaEngine::Formula; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||