Formula.pm

filter: Statementstmt 89.3% 109 / 122 Branchbran 40.3% 25 / 62 Conditioncond 79.1% 19 / 24 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 85.7% 18 / 21 Podpod 87.5% 7 / 8 totaltotal 74.1% 186 / 251 CC 40 SCAR 41.9
CC
40
Cov
74%
toType
240.0
_build_ast
17.4
evaluate
6.2
CRAP 66.1
« Formula/Context.pm ↑ Summary Formula/Type.pm »
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 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 $VERSION = '0.10';
13  
14  
15 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 ▶use Log::Report 'math-formula';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 13 ▶use Math::Formula::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use Math::Formula::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 #--------------------------
27  
28 sub new(%)
29 12 ▶{ my ($class, $name, $expr, %self) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
30 12 $self{_name} = $name;
31 12 $self{_expr} = $expr;
32 12 (bless {}, $class)->init(\%self) }
33  
34 sub init($)
35 12 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.3
Pod undocumented
36 12 ▶ $self->{MSBE_name} = $args->{_name} or panic "every formular requires a name";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MSBE_name"} = $args->{"_name"} or &panic("every formular requires a name") 0 12 12
37 12 ▶ $self->{MSBE_expr} = $args->{_expr} or panic "every formular requires an expression";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MSBE_expr"} = $args->{"_expr"} or &panic("every formular requires an expression") 0 12 12
38 12 $self->{MSBE_returns} = $args->{returns};
39 12 $self;
40 }
41  
42 #--------------------------
43  
44 0 ▶sub name() { $_[0]->{MSBE_name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
45 114 ▶sub expression() { $_[0]->{MSBE_expr} }
Subroutine expression: called CC=1 SCAR=0.0
Pod documented
46 109 ▶sub returns() { $_[0]->{MSBE_returns} }
Subroutine returns: called CC=1 SCAR=0.0
Pod documented
47  
48  
49 sub tree($)
50 114 ▶{ my ($self, $expression) = @_;
Subroutine tree: called CC=2 SCAR=7.1
Pod documented
51 114 ▶ $self->{MSBE_ast} ||= $self->_build_ast($self->_tokenize($expression), 0);
Condition66%2/3
$self->{"MSBE_ast"} ||= $self->_build_ast($self->_tokenize($expression), 0)
l!l&&r!l&&!r
11130
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)
ABresult
1X1
011
000
52 }
53  
54 # For testing only: to load a new expression without the need to create
55 # a new object.
56 sub _test($$)
57 112 ▶{ my ($self, $expr) = @_;
Subroutine _test: called CC=1 SCAR=0.0
58 112 $self->{MSBE_expr} = $expr;
59 112 delete $self->{MSBE_ast};
60 }
61  
62 ###
63 ### PARSER
64 ###
65  
66 13 my $multipliers = MF::INTEGER->_multipliers;
67 13 my $match_float = MF::FLOAT->_match;
68 13 my $match_name = MF::NAME->_match;
69 13 my $match_date = MF::DATE->_match;
70 13 my $match_time = MF::TIME->_match;
71 13 my $match_dt = MF::DATETIME->_match;
72 13 my $match_dur = MF::DURATION->_match;
73  
74 13 my $match_op = join '|',
75 '[*\/+\-#~.%]',
76 qw/=~ !~
77 <=> <= >= == != < > /, # order is important
78 ( map "$_\\b", qw/
79 and or not xor
80 like unlike
81 cmp lt le eq ne ge gt/
82 );
83  
84 sub _tokenize($)
85 149 ▶{ my ($self, $s) = @_;
Subroutine _tokenize: called CC=2 SCAR=6.9
86 149 our @t = ();
87 149 my $parens_open = 0;
88  
89 13 ▶ use re 'eval'; #XXX needed with newer than 5.16 perls?
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 149 $s =~ m/ ^
92 (?: \s*
93 (?| \# (?: \s [^\n\r]+ | $ ) \
94 37 | ( true | false ) (?{ push @t, MF::BOOLEAN->new($+) })
95 | ( \" (?: \\\" | [^"] )* \" )
96 29 (?{ push @t, MF::STRING->new($+) })
97 | ( \' (?: \\\' | [^'] )* \' )
98 2 (?{ push @t, MF::STRING->new($+) })
99 22 | ( $match_dur ) (?{ push @t, MF::DURATION->new($+) })
100 150 | ( $match_op ) (?{ push @t, MF::OPERATOR->new($+) })
101 34 | ( $match_name ) (?{ push @t, MF::NAME->new($+) })
102 31 | ( $match_dt ) (?{ push @t, MF::DATETIME->new($+) })
103 23 | ( $match_date ) (?{ push @t, MF::DATE->new($+) })
104 16 | ( $match_time ) (?{ push @t, MF::TIME->new($+) })
105 7 | ( $match_float ) (?{ push @t, MF::FLOAT->new($+) })
106 | ( [0-9][0-9_]*(?:$multipliers)? )
107 79 (?{ push @t, MF::INTEGER->new($+) })
108 5 | \( (?{ push @t, MF::PARENS->new('(', ++$parens_open) })
109 5 | \) (?{ push @t, MF::PARENS->new(')', $parens_open--) })
110 | $
111 0 | (.+) (?{ error __x"expression '{name}', failed at '{where}'",
112 name => $self->name, where => $+ })
113 )
114 )+ \z /sxo;
115  
116 149 ▶ ! $parens_open
Branch: 1 branch
BranchTrueFalseTotal
$parens_open and &error(&__x("expression '{name}', parenthesis do not match", "name", $self->name)) 0 149 149
117 or error __x"expression '{name}', parenthesis do not match", name => $self->name;
118  
119 149 \@t;
120 }
121  
122 sub _build_ast($$)
123 268 ▶{ my ($self, $t, $prio) = @_;
Subroutine _build_ast: called CC=17 SCAR=28.5
124 268 ▶ return shift @$t if @$t < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$t < 2 126 142 268
125  
126 PROGRESS:
127 142 ▶ while(my $first = shift @$t)
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = shift @$t) 142 0 142
128 {
129 #warn "LOOP FIRST ", Dumper $first,
130 #warn " MORE ", Dumper $t;
131 289 ▶ if($first->isa('MF::PARENS'))
Branch: 1 branch
BranchTrueFalseTotal
if ($first->isa("MF::PARENS")) 5 284 289
132 5 { my $level = $first->level;
133  
134 5 my @nodes;
135 5 ▶ while(my $node = shift @$t)
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = shift @$t) 25 0 25
136 25 ▶ { last if $node->isa('MF::PARENS') && $node->level==$level;
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("MF::PARENS") and $node->level == $level 5 20 25
Condition100%3/3
$node->isa("MF::PARENS") and $node->level == $level
!ll&&!rl&&r
1825
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
ABresult
0X0
100
111
137 20 push @nodes, $node;
138 }
139 5 $first = $self->_build_ast(\@nodes, 0);
140 5 redo PROGRESS;
141 }
142  
143 284 ▶ if(ref $first eq 'MF::OPERATOR') # unresolved operator
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first eq "MF::OPERATOR") 14 270 284
144 14 { my $op = $first->token;
145  
146 14 ▶ if($op eq '#' || $op eq '.')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "#" or $op eq ".") 1 13 14
Condition66%2/3
$op eq "#" or $op eq "."
l!l&&r!l&&!r
0113
MC/DC50%1/2
$op eq "#" or $op eq "."
$op eq "#" $op eq "."
Truth table66%2/3
A or B
A= $op eq "#"B= $op eq "."
ABresult
1X1
011
000
147 { # Fragments and Methods are always infix, but their left-side arg
148 # can be left-out. As PREFIX, they would be RTL but we need LTR
149 1 unshift @$t, $first;
150 1 $first = MF::NAME->new('');
151 1 redo PROGRESS;
152 }
153  
154 13 ▶ my $next = $self->_build_ast($t, $prio)
Branch: 1 branch
BranchTrueFalseTotal
my $next = $self->_build_ast($t, $prio) or &error(&__x("expression '{name}', monadic '{op}' not followed by anything useful", "name", $self->name, "op", $op)) 0 13 13
155 or error __x"expression '{name}', monadic '{op}' not followed by anything useful",
156 name => $self->name, op => $op;
157 #warn "HERE";
158  
159 13 $first = MF::PREFIX->new($op, $next);
160 13 redo PROGRESS;
161 }
162  
163 270 ▶ my $next = $t->[0]
Branch: 1 branch
BranchTrueFalseTotal
my $next = $t->[0] or return $first 132 138 270
164 or return $first; # end of expression
165  
166 138 ▶ ref $next eq 'MF::OPERATOR'
Branch: 1 branch
BranchTrueFalseTotal
ref $next eq "MF::OPERATOR" or &error(&__x("expression '{name}', expected infix operator but found '{type}'", "name", $self->name, "type", ref $next)) 0 138 138
167 or error __x"expression '{name}', expected infix operator but found '{type}'",
168 name => $self->name, type => ref $next;
169  
170 138 my $op = $next->token;
171 138 ▶ @$t or error __x"expression '{name}', infix operator '{op}' requires right-hand argument",
Branch: 1 branch
BranchTrueFalseTotal
@$t or &error(&__x("expression '{name}', infix operator '{op}' requires right-hand argument", "name", $self->name, "op", $op)) 0 138 138
172 name => $self->name, op => $op;
173  
174 138 my ($next_prio, $assoc) = MF::OPERATOR->find($op);
175  
176 138 ▶ return $first
Branch: 1 branch
BranchTrueFalseTotal
if $next_prio < $prio or $next_prio == $prio and $assoc == 1 10 128 138
Condition83%5/6
$next_prio < $prio or $next_prio == $prio and $assoc == 1
l!l&&r!l&&!r
420114
$next_prio == $prio and $assoc == 1
!ll&&!rl&&r
12806
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
ABCresult
1XX1
0111
00X0
0100
177 if $next_prio < $prio
178 || ($next_prio==$prio && $assoc==MF::OPERATOR::LTR);
179  
180 128 shift @$t; # apply the operator
181 128 $first = MF::INFIX->new($op, $first, $self->_build_ast($t, $next_prio));
182 128 redo PROGRESS;
183 }
184 }
185  
186 #--------------------------
187  
188 sub evaluate($)
189 114 ▶{ my ($self, $context, %args) = @_;
Subroutine evaluate: called CC=6 SCAR=18.2
Pod documented
190 114 my $expr = $self->expression;
191  
192 114 ▶ my $result = ref $expr eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $expr eq 'CODE' ? : 0 114 114
193 ? $self->toType($expr->($context, $self, %args))
194 : $self->tree($expr)->_compute($context, $self);
195  
196 # For external evaluation calls, we must follow the request
197 114 ▶ my $expect = $args{expect} || $self->returns;
Condition66%2/3
$args{'expect'} || $self->returns
l!l&&r!l&&!r
50109
MC/DC50%1/2
$args{'expect'} || $self->returns
$args{'expect'} $self->returns
Truth table66%2/3
A || B
A= $args{'expect'}B= $self->returns
ABresult
1X1
011
000
198 114 ▶ $result && $expect && ! $result->isa($expect) ? $result->cast($expect) : $result;
Branch: 1 branch
BranchTrueFalseTotal
$result && $expect && !$result->isa($expect) ? : 1 113 114
Condition83%5/6
$result and $expect and not $result->isa($expect)
!ll&&!rl&&r
10941
$result and $expect
!ll&&!rl&&r
01095
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)
ABCresult
0XX0
10X0
1100
1111
199 }
200  
201  
202 13 my %_match = map { my $match = $_->_match; ( $_ => qr/^$match$/x ) }
203 qw/MF::DATETIME MF::TIME MF::DATE MF::DURATION/;
204  
205 sub toType($)
206 0 ▶{ my ($self, $data) = @_;
Subroutine toType: not called CC=15 SCAR=54.8
Pod documented
207 0 ▶ if(blessed $data)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data)) 0 0 0
208 0 ▶ { return $data if $data->isa('Math::Formula::Type'); # explicit type
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("Math::Formula::Type") 0 0 0
209 0 ▶ return MF::DATETIME->new(undef, $data) if $data->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("DateTime") 0 0 0
210 0 ▶ return MF::DURATION->new(undef, $data) if $data->isa('DateTime::Duration');
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("DateTime::Duration") 0 0 0
211 0 ▶ return MF::FRAGMENT->new($data->name, $data) if $data->isa('Math::Formula::Context');
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("Math::Formula::Context") 0 0 0
212 }
213  
214 0 ▶ my $match = sub { my $type = shift; my $match = $type->_match; qr/^$match$/ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
215  
216 return
217 $data =~ /^[+-]?[0-9]+$/ ? MF::INTEGER->new(undef, $data)
218 : $data =~ /^[+-]?[0-9]+\./ ? MF::FLOAT->new(undef, $data)
219 : $data =~ /^(?:true|false)$/ ? MF::BOOLEAN->new($data)
220 : ref $data eq 'Regexp' ? MF::REGEXP->new(undef, $data)
221 : $data =~ $_match{'MF::DATETIME'} ? MF::DATETIME->new($data)
222 : $data =~ $_match{'MF::TIME'} ? MF::TIME->new($data)
223 : $data =~ $_match{'MF::DATE'} ? MF::DATE->new($data)
224 0 ▶ : $data =~ $_match{'MF::DURATION'} ? MF::DURATION->new($data)
Branch: 9 branches
BranchTrueFalseTotal
$data =~ /^[+-]?[0-9]+$/ ? : 0 0 0
$data =~ /^[+-]?[0-9]+\./ ? : 0 0 0
$data =~ /^(?:true|false)$/ ? : 0 0 0
ref $data eq 'Regexp' ? : 0 0 0
$data =~ /$_match{'MF::DATETIME'}/ ? : 0 0 0
$data =~ /$_match{'MF::TIME'}/ ? : 0 0 0
$data =~ /$_match{'MF::DATE'}/ ? : 0 0 0
$data =~ /$_match{'MF::DURATION'}/ ? : 0 0 0
$data =~ /^(['"]).*\1$/ ? : 0 0 0
225 : $data =~ /^(['"]).*\1$/ ? MF::STRING->new($data)
226 : MF::STRING->new(undef, $data);
227 }
228  
229 #--------------------------
230  
231 13 1;
« Formula/Context.pm ↑ Summary Formula/Type.pm »