| 1 | # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>]. | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # This code will be run incredabily fast, hence is tries to avoid copying etc. It | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # is not always optimally readible when your Perl skills are poor. | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | package Math::Formula; | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 28 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 28 | $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 28 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 28 | ▶ | use Log::Report 'math-formula'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 28 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 28 | ▶ | use Math::Formula::Token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 28 | ▶ | use Math::Formula::Type; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | #-------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | sub new(%) | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 68 | ▶ | { my ($class, $name, $expr, %self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 68 | $self{_name} = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 68 | $self{_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 68 | (bless {}, $class)->init(\%self); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | sub init($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 68 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 68 | ▶ | my $name = $self->{MSBE_name} = $args->{_name} or panic "every formular requires a name"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 68 | ▶ | my $expr = $args->{_expr} or panic "every formular requires an expression"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 68 | my $returns = $self->{MSBE_returns} = $args->{returns}; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 68 | ▶ | if(ref $expr eq 'SCALAR') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | { $expr = MF::STRING->new(undef, $$expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ▶ | elsif(! ref $expr && $returns && $returns->isa('MF::STRING')) | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
not ref $expr and $returns and $returns->isa("MF::STRING")
not ref $expr and $returns
MC/DC0%0/3
not ref $expr and $returns and $returns->isa("MF::STRING")
not ref $expr
$returns
$returns->isa("MF::STRING")
Truth table75%3/4
not(A) and B and C
A= not ref $exprB= $returnsC= $returns->isa("MF::STRING")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | { $expr = MF::STRING->new(undef, $expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 68 | $self->{MSBE_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 68 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | #-------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 23 | ▶ | sub name() { $_[0]->{MSBE_name} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 284 | ▶ | sub expression() { $_[0]->{MSBE_expr} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expression: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 260 | ▶ | sub returns() { $_[0]->{MSBE_returns} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
returns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | sub tree($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 264 | ▶ | { my ($self, $expression) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tree: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 264 | ▶ | $self->{MSBE_ast} ||= $self->_build_ast($self->_tokenize($expression), 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"MSBE_ast"} ||= $self->_build_ast($self->_tokenize($expression), 0)
MC/DC0%0/2
$self->{"MSBE_ast"} ||= $self->_build_ast($self->_tokenize($expression), 0)
$self->{"MSBE_ast"}
$self->_build_ast($self->_tokenize($expression), 0)
Truth table66%2/3
A ||= B
A= $self->{"MSBE_ast"}B= $self->_build_ast($self->_tokenize($expression), 0)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # For testing only: to load a new expression without the need to create | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # a new object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | sub _test($$) | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 214 | ▶ | { my ($self, $expr) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_test: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 214 | $self->{MSBE_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 214 | delete $self->{MSBE_ast}; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ### | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | ### PARSER | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | ### | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 28 | my $match_int = MF::INTEGER->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 28 | my $match_float = MF::FLOAT->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 28 | my $match_name = MF::NAME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 28 | my $match_date = MF::DATE->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 28 | my $match_time = MF::TIME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 28 | my $match_tz = MF::TIMEZONE->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 28 | my $match_dt = MF::DATETIME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 28 | my $match_dur = MF::DURATION->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 28 | my $match_op = join '|', | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | qw{ // -> }, '[?*\/+\-#~.%]', | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | qw{ =~ !~ <=> <= >= == != < > }, # order is important | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | qw{ :(?![0-9][0-9]) (?<![0-9][0-9]): }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ( map "$_\\b", qw/ and or not xor exists like unlike cmp lt le eq ne ge gt/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | sub _tokenize($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 297 | ▶ | { my ($self, $s) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 297 | our @t = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 297 | my $parens_open = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 28 | ▶ | use re 'eval'; #XXX needed with newer than 5.16 perls? | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 297 | $s =~ m/ ^ | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | (?: \s* | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | (?| \# (?: \s [^\n\r]+ | $ ) \ | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 47 | | ( true\b | false\b ) (?{ push @t, MF::BOOLEAN->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | | ( \" (?: \\\" | [^"] )* \" ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 100 | (?{ push @t, MF::STRING->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | | ( \' (?: \\\' | [^'] )* \' ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | (?{ push @t, MF::STRING->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 39 | | ( $match_dur ) (?{ push @t, MF::DURATION->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 13 | | ( $match_tz ) (?{ push @t, MF::TIMEZONE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 311 | | ( $match_op ) (?{ push @t, MF::OPERATOR->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 79 | | ( $match_name ) (?{ push @t, MF::NAME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 31 | | ( $match_dt ) (?{ push @t, MF::DATETIME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 23 | | ( $match_date ) (?{ push @t, MF::DATE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 21 | | ( $match_time ) (?{ push @t, MF::TIME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 42 | | ( $match_float ) (?{ push @t, MF::FLOAT->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 163 | | ( $match_int ) (?{ push @t, MF::INTEGER->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | | \( (?{ push @t, MF::PARENS->new('(', ++$parens_open) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | | \) (?{ push @t, MF::PARENS->new(')', $parens_open--) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | | \$ ([1-9][0-9]*) (?{ push @t, MF::CAPTURE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | | $ | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | | (.+) (?{ error __x"expression '{name}', failed at '{where}'", | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | name => $self->name, where => $+ }) | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | )+ \z /sxo; | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 297 | ▶ | ! $parens_open | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | or error __x"expression '{name}', parenthesis do not match", name => $self->name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 297 | \@t; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | sub _build_ast($$) | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 560 | ▶ | { my ($self, $t, $prio) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ast: called CC=23 SCAR=31.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 560 | ▶ | return shift @$t if @$t < 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | PROGRESS: | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 297 | ▶ | while(my $first = shift @$t) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 597 | ▶ | if($first->isa('MF::PARENS')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | { my $level = $first->level; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 5 | my @nodes; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | 5 | ▶ | while(my $node = shift @$t) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 25 | ▶ | { last if $node->isa('MF::PARENS') && $node->level==$level; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->isa("MF::PARENS") and $node->level == $level
MC/DC100%2/2
$node->isa("MF::PARENS") and $node->level == $level
$node->isa("MF::PARENS")
$node->level == $level
Truth table100%3/3
A and B
A= $node->isa("MF::PARENS")B= $node->level == $level
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 20 | push @nodes, $node; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | $first = $self->_build_ast(\@nodes, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 5 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 592 | ▶ | if(ref $first eq 'MF::OPERATOR') # unresolved operator | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 38 | { my $op = $first->token; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 38 | ▶ | if($op eq '#' || $op eq '.') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op eq "#" or $op eq "."
MC/DC100%2/2
$op eq "#" or $op eq "."
$op eq "#"
$op eq "."
Truth table100%3/3
A or B
A= $op eq "#"B= $op eq "."
| |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | { # Fragments and Methods are always infix, but their left-side arg | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # can be left-out. As PREFIX, they would be RTL but we need LTR | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 6 | unshift @$t, $first; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | $first = MF::NAME->new(''); | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 32 | ▶ | my $next = $self->_build_ast($t, $prio) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | or error __x"expression '{name}', monadic '{op}' not followed by anything useful", | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | name => $self->name, op => $op; | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 32 | $first = MF::PREFIX->new($op, $next); | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 32 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 554 | ▶ | my $next = $t->[0] | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | or return $first; # end of expression | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 288 | ▶ | if(ref $next ne 'MF::OPERATOR') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | ▶ | { if($next->isa('MF::TIMEZONE')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | { # Oops, mis-parse | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | unshift @$t, $next->cast('MF::INTEGER'); | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 2 | $next = MF::OPERATOR->new('+'); | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | { error __x"expression '{name}', expected infix operator but found '{type}'", | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | name => $self->name, type => ref $next; | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 288 | my $op = $next->token; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 288 | ▶ | @$t or error __x"expression '{name}', infix operator '{op}' requires right-hand argument", | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | name => $self->name, op => $op; | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 288 | my ($next_prio, $assoc) = MF::OPERATOR->find($op); | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 288 | ▶ | return $first | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$next_prio < $prio or $next_prio == $prio and $assoc == 1
$next_prio == $prio and $assoc == 1
MC/DC66%2/3
$next_prio < $prio or $next_prio == $prio and $assoc == 1
$next_prio < $prio
$next_prio == $prio
$assoc == 1
Truth table75%3/4
A or B and C
A= $next_prio < $prioB= $next_prio == $prioC= $assoc == 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | if $next_prio < $prio | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | || ($next_prio==$prio && $assoc==MF::OPERATOR::LTR); | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 264 | ▶ | if($op eq ':') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 7 | { return $first; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 257 | shift @$t; # apply the operator | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 257 | ▶ | if($op eq '?') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 7 | { my $then = $self->_build_ast($t, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 7 | my $colon = shift @$t; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | 7 | ▶ | $colon && $colon->token eq ':' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$colon and $colon->token eq ":"
MC/DC0%0/2
$colon and $colon->token eq ":"
$colon
$colon->token eq ":"
Truth table33%1/3
A and B
A= $colonB= $colon->token eq ":"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | or error __x"expression '{name}', expected ':' in '?:', but got '{token}'", | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | name => $self->name, token => ($next ? $colon->token : 'end-of-line'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 7 | my $else = $self->_build_ast($t, $next_prio); | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 7 | $first = MF::TERNARY->new($op, $first, $then, $else); | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 7 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 250 | $first = MF::INFIX->new($op, $first, $self->_build_ast($t, $next_prio)); | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 250 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | #-------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | sub evaluate($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 275 | ▶ | { my ($self, $context, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 275 | my $expr = $self->expression; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 275 | ▶ | my $result | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | = ref $expr eq 'CODE' ? $self->toType($expr->($context, $self, %args)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | : ! blessed $expr ? $self->tree($expr)->compute($context) | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | : $expr->isa('Math::Formula::Type') ? $expr | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | : panic; | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # For external evaluation calls, we must follow the request | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 275 | ▶ | my $expect = $args{expect} || $self->returns; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$args{'expect'} || $self->returns
MC/DC100%2/2
$args{'expect'} || $self->returns
$args{'expect'}
$self->returns
Truth table100%3/3
A || B
A= $args{'expect'}B= $self->returns
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 275 | ▶ | $result && $expect && ! $result->isa($expect) ? $result->cast($expect, $context) : $result; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$result and $expect and not $result->isa($expect)
$result and $expect
MC/DC66%2/3
$result and $expect and not $result->isa($expect)
$result
$expect
not $result->isa($expect)
Truth table75%3/4
A and B and not(C)
A= $resultB= $expectC= not $result->isa($expect)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 28 | my %_match = map { my $match = $_->_match; ( $_ => qr/^$match$/x ) } | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | qw/MF::DATETIME MF::TIME MF::DATE MF::DURATION/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | sub toType($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 22 | ▶ | { my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toType: called CC=16 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 22 | ▶ | if(blessed $data) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | ▶ | { return $data if $data->isa('Math::Formula::Type'); # explicit type | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | ▶ | return MF::DATETIME->new(undef, $data) if $data->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 3 | ▶ | return MF::DURATION->new(undef, $data) if $data->isa('DateTime::Duration'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | ▶ | return MF::FRAGMENT->new($data->name, $data) if $data->isa('Math::Formula::Context'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 15 | ▶ | my $match = sub { my $type = shift; my $match = $type->_match; qr/^$match$/ }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | return | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | ref $data eq 'SCALAR' ? MF::STRING->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | : $data =~ /^[+-]?[0-9]+$/ ? MF::INTEGER->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | : $data =~ /^[+-]?[0-9]+\./ ? MF::FLOAT->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | : $data =~ /^(?:true|false)$/ ? MF::BOOLEAN->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | : ref $data eq 'Regexp' ? MF::REGEXP->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | : $data =~ $_match{'MF::DATETIME'} ? MF::DATETIME->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | : $data =~ $_match{'MF::TIME'} ? MF::TIME->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | : $data =~ $_match{'MF::DATE'} ? MF::DATE->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | ▶ | : $data =~ $_match{'MF::DURATION'} ? MF::DURATION->new($data) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | : $data =~ /^(['"]).*\1$/ ? MF::STRING->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | : error __x"not an expression (string needs \\ ) for '{data}'", data => $data; | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | #-------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 28 | 1; | |||||||||||||||||||||||||||||||||||||||||||||