blib/lib/Math/Expression.pm

filter: Statementstmt 78.7% 348 / 442 Branchbran 80.3% 323 / 402 Conditioncond 79.4% 155 / 195 MC/DCmcdc 63.3% 69 / 109 Subroutinesub 80.9% 17 / 21 Podpod 82.3% 14 / 17 totaltotal 78.0% 926 / 1186 CC 267 SCAR 47.4
CC
267
Cov
79%
EvalTree
196.4
ParseString
100.0
CheckTreeInt
72.2
CRAP 114.9
↑ Summary
1 #!/usr/bin/perl -w
2 # /\
3 # / \ (C) Copyright 2003 Parliament Hill Computers Ltd.
4 # \ / All rights reserved.
5 # \/
6 # . Author: Alain Williams, First written January 2003; last update July 2016
7 # . addw@phcomp.co.uk
8 # .
9 # .
10 #
11 # SCCS: @(#)Expression.pm 1.48 09/06/16 10:48:57
12 #
13 # This module is free software; you can redistribute it and/or modify
14 # it under the same terms as Perl itself. You must preserve this entire copyright
15 # notice in any use or distribution.
16 # The author makes no warranty what so ever that this code works or is fit
17 # for purpose: you are free to use this code on the understanding that any problems
18 # are your responsibility.
19  
20 # Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is
21 # hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and
22 # this permission notice appear in supporting documentation.
23  
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 package Math::Expression;
27  
28 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use POSIX qw(strftime mktime);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # What local variables - visible elsewhere
32 2 ▶use vars qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
33 @ISA @EXPORT
34 2 /;
35  
36 2 @ISA = ('Exporter');
37  
38 2 @EXPORT = qw(
39 &CheckTree
40 &Eval
41 &EvalToScalar
42 &EvalTree
43 &FuncValue
44 &Parse
45 &ParseString
46 &ParseToScalar
47 &SetOpts
48 &VarSetFun
49 &VarSetScalar
50 $Version
51 );
52  
53 2 our $VERSION = "1.48";
54  
55 # Fundamental to this is a tree of nodes.
56 # Nodes are hashes with members:
57 # oper (var, *, >, ...)
58 # left & right (refs to nodes)
59 # monop (boolean)
60 # name (on var nodes)
61 # fname (on func nodes)
62 # val (on const nodes)
63 # flow (on flow nodes)
64  
65 # Within ParseString() there are 2 stacks:
66 # @tree (of nodes) - this is what is eventually returned
67 # Terminals (var, const) are pushed here as they are read in
68 # @operators all non terminals with JR-precedence > TOS-precedence start off being pushed here.
69 # Where JR-precedence <= TOS-precedence do 'reduce', ie move from @operators to @tree as a tree,
70 # with left/right children coming from @tree and the operator pushed to @tree.
71 # It is interesting to print the tree with the Data::Dumper or PrintTree().
72  
73  
74 # Operator precedence, higher means bind more tightly to operands - ie evaluate first.
75 # If precedence values are the same associate to the left.
76 # 2 values, depending on if it is the TopOfStack or JustRead operator - [TOS, JR]. See ':=' which right associates.
77 # Just binary operators makes life easier as well.
78 # Getting the precedence values right is a pain and for things like close paren, non obvious.
79 # Far apart numbers makes adding new ones easier.
80  
81 2 my %OperPrec = (
82 'var' => [240, 240],
83 'const' => [240, 240],
84 '[' => [70, 230],
85 '++' => [220, 220],
86 '--' => [220, 220],
87 'M-' => [200, 210], # Monadic -
88 'M+' => [200, 210], # Monadic +
89 'M!' => [200, 210],
90 'M~' => [200, 210],
91 '**' => [190, 190],
92 '*' => [180, 180],
93 '/' => [180, 180],
94 '%' => [180, 180],
95 '+' => [170, 170],
96 '-' => [170, 170],
97 '.' => [160, 160],
98 '>' => [150, 150],
99 '<' => [150, 150],
100 '>=' => [150, 150],
101 '<=' => [150, 150],
102 '==' => [150, 150],
103 '!=' => [150, 150],
104 '<>' => [150, 150],
105 'lt' => [150, 150],
106 'gt' => [150, 150],
107 'le' => [150, 150],
108 'ge' => [150, 150],
109 'eq' => [150, 150],
110 'ne' => [150, 150],
111 '&&' => [140, 140],
112 '||' => [130, 130],
113 ':' => [120, 120],
114 '?' => [110, 110],
115 ',' => [100, 101], # Build list 1,2,3,4 as ,L[1]R[,L[2]R[,L[3]R[4]]]
116 '(' => [90, 220],
117 ')' => [90, 90],
118 'func' => [210, 220],
119 ']' => [70, 70],
120 ':=' => [50, 60], # 6 to make := right assosc
121 '}' => [40, 00],
122 'flow' => [30, 40],
123 ';' => [20, 20],
124 '{' => [10, 0],
125 'EOF' => [-50, -50],
126 );
127 # TOS, JR
128 # Nothing special about -ve precedence, just saves renumbering when I got to zero.
129  
130 # Monadic/Unary operators:
131 2 my %MonOp = (
132 '-' => 20,
133 '+' => 20,
134 '!' => 20,
135 '~' => 20,
136 );
137  
138 # MonVarOp - operate on variables, but treat much like monops:
139 2 my %MonVarOp = (
140 '++' => 22,
141 '--' => 22,
142 );
143  
144 # Closing operators on opening ones. NOT [ ]
145 2 my %MatchOp = (
146 '(' => ')',
147 '{' => '}',
148 );
149  
150 2 my %MatchOpClose = reverse %MatchOp; # Reverse lookup
151  
152 # Inbuilt functions, copied to property Functions
153 2 my %InFuns = map { $_ => 1} qw/ abs aindex count defined int join localtime mktime pop printf push round shift split strftime strlen unshift /;
154 # Inbuilt functions that must be given a L value
155 # This does not need to be externally visible with our, any ExtraFuncEval that adds to it will cope
156 2 my %InFunLV = map { $_ => 1} qw / defined pop push shift unshift /;
157  
158 # Escape chars recognised:
159 2 my %escapes = ( n => "\n", r => "\r", t => "\t", '\\' => '\\' );
160  
161 # Default error output function
162 18 ▶sub PrintError {
Subroutine PrintError: called CC=3 SCAR=13.5
Pod undocumented
163 18 my $self = shift;
164 18 ▶ if(defined $self->{PrintErrFunc} && $self->{PrintErrFunc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"PrintErrFunc"} and $self->{"PrintErrFunc"}) 18 0 18
Condition33%1/3
defined $self->{"PrintErrFunc"} and $self->{"PrintErrFunc"}
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $self->{"PrintErrFunc"} and $self->{"PrintErrFunc"}
defined $self->{"PrintErrFunc"} $self->{"PrintErrFunc"}
Truth table33%1/3
A and B
A= defined $self->{"PrintErrFunc"}B= $self->{"PrintErrFunc"}
ABresult
0X0
100
111
165 18 $self->{PrintErrFunc}(@_);
166 18 return;
167 }
168  
169 0 printf STDERR @_;
170 0 print STDERR "\n";
171 }
172  
173 # Default function to set a variable value, store as a reference to an array.
174 # Assign to a variable. (Default function.) Args:
175 # 0 Self
176 # 1 Variable name, might look like a[2] in which case set element with last value in arg 2
177 # Don't make an array bigger it already is, except to make it 1 element bigger
178 # 2 Value - an array
179 # Return the value;
180 1000 ▶sub VarSetFun {
Subroutine VarSetFun: called CC=7 SCAR=19.9
Pod documented
181 1000 my ($self, $name, @value) = @_;
182  
183 1000 ▶ unless(defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 1000 0 1000
184 0 $self->PrintError("Undefined variable name '$name' - need () to force left to right assignment ?");
185 } else {
186 1000 ▶ if($name =~ /^(.+)\[(\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(.+)\[(\d+)\]$/) { } 25 975 1000
187 25 ▶ unless(defined($self->{VarHash}->{$1})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $self->{'VarHash'}{$1}) { } 23 2 25
if (do { $2 > $self->{'ArrayMaxIndex'} }) { } 1 22 23
elsif ($2 > @{$self->{'VarHash'}{$1};}) { } 0 22 22
188 2 ▶ if($2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 == 0) { } 1 1 2
189 1 $self->{VarHash}->{$1} = $value[-1];
190 } else {
191 1 $self->PrintError("Can only create variable '%s' by setting element 0", $1);
192 }
193 } elsif($2 > $self->{ArrayMaxIndex}) {
194 1 $self->PrintError("Array index %d is too large. Max is %d", $2, $self->{ArrayMaxIndex});
195 22 } elsif($2 > @{$self->{VarHash}->{$1}}) {
196 0 $self->PrintError("Extending array too much, '%s' has %d elements, trying to set element %d", $1, scalar @{$self->{VarHash}->{$1}}, $2);
197
198 } else {
199 22 $self->{VarHash}->{$1}[$2] = $value[-1];
200 }
201 } else {
202 975 $self->{VarHash}->{$name} = \@value;
203 }
204 }
205  
206 1000 return @value;
207 }
208  
209 # Set a scalar variable function
210 # 0 Self
211 # 1 Variable name
212 # 2 Value - a scalar
213 # Return the value;
214 0 ▶sub VarSetScalar {
Subroutine VarSetScalar: not called CC=1 SCAR=6.9
Pod documented
215 0 my ($self, $name, $value) = @_;
216 0 my @arr;
217 0 $arr[0] = $value;
218 0 $self->{VarSetFun}($self, $name, @arr);
219 0 return $value;
220 }
221  
222 # Return the value of a variable - return an array
223 # 0 Self
224 # 1 Variable name
225 0 ▶sub VarGetFun {
Subroutine VarGetFun: not called CC=2 SCAR=17.9
Pod documented
226 0 my ($self, $name) = @_;
227  
228 0 ▶ return '' unless(exists($self->{VarHash}->{$name}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"VarHash"}{$name} 0 0 0
229 0 return @{$self->{VarHash}->{$name}};
230 }
231  
232 # Return 1 if a variable is defined - ie has been assigned to
233 # 0 Self
234 # 1 Variable name
235 0 ▶sub VarIsDefFun {
Subroutine VarIsDefFun: not called CC=2 SCAR=17.9
Pod documented
236 0 my ($self, $name) = @_;
237  
238 0 ▶ return exists($self->{VarHash}->{$name}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'VarHash'}{$name} ? : 0 0 0
239 }
240  
241 # Parse a string argument, return a tree that can be evaluated.
242 # Report errors with $ErrFunc.
243 # 0 Self
244 # 1 String argument
245 527 ▶sub ParseString {
Subroutine ParseString: called CC=87 SCAR=46.0
Pod documented
246 527 my ($self, $expr) = @_;
247  
248 527 my @operators = (); # Operators stacked here until needed
249 527 my @tree; # Parsed tree ends up here
250 my $newt; # New Token
251 527 my $ln = ''; # Last $newt->{oper}
252  
253 527 my $operlast = 1; # Operator was last, ie not: var, const, ; ) string flow. Used to idenify monadic operators
254 527 my $endAlready = 0;
255 527 my $GenSemiColon = 0; # Need to generate a ';'. Always do so after a '}'
256  
257 527 while(1) {
258 4815 my $semi = 0;
259 4815 $newt = {};
260  
261 # Lexical part:
262  
263 4815 $expr =~ s/^\s*//;
264 4815 my $EndInput = $expr eq '';
265  
266 4815 ▶ if($GenSemiColon) {
Branch: 10 branches
BranchTrueFalseTotal
if ($GenSemiColon) { } 45 4770 4815
elsif ($EndInput) { } 1551 3219 4770
elsif ($expr =~ s/^(((\d+(\.\d*)?)|(\.\d+))([ed][-+]?\d+)?)//i) { } 755 2464 3219
elsif ($expr =~ /^(['"])/ and $expr =~ s/^($1)([^$1]*)$1//) { } 107 2357 2464
elsif ($expr =~ s/^}//) { } 45 2312 2357
elsif ($expr =~ s"^(\+\+|--|:=|>=|<=|==|<>|!=|&&|\|\||lt\b|gt\b|le\b|ge\b|eq\b|ne\b|\*\*|[-~!./*%+,<>\?:\(\)\[\]{])"") { } 1387 925 2312
elsif ($expr =~ s/^(if|while)//) { } 51 874 925
elsif ($expr =~ s/^;//) { } 167 707 874
elsif ($expr =~ s/^([_a-z][\w]*)\(/(/i) { } 72 635 707
elsif ($expr =~ s/^\$?([_a-z]\w*)//i) { } 634 1 635
267 # Generate an extra semicolon - after a close brace
268 45 $newt->{oper} = ';';
269 45 $operlast = 0;
270 45 $EndInput = $GenSemiColon = 0;
271 } # End of input string:
272 elsif($EndInput) {
273 1551 $operlast = 0;
274 # First time generate a ';' to terminate a set of statements:
275 1551 ▶ if($endAlready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endAlready) { } 1028 523 1551
276 1028 undef $newt;
277 } else {
278 523 $newt->{oper} = 'EOF';
279 523 $EndInput = 0;
280 }
281 1551 $endAlready = 1;
282 } # Match integer/float constant:
283 elsif($expr =~ s/^(((\d+(\.\d*)?)|(\.\d+))([ed][-+]?\d+)?)//i) {
284 755 $newt->{oper} = 'const';
285 755 $newt->{val} = $1;
286 755 $newt->{type} = 'num'; # Used in debug/tree-print
287 755 $operlast = 0;
288 } # Match string bounded by ' or "
289 ▶ elsif($expr =~ /^(['"])/ and $expr =~ s/^($1)([^$1]*)$1//) {
Condition66%2/3
$expr =~ /^(['"])/ and $expr =~ s/^($1)([^$1]*)$1//
!ll&&!rl&&r
23570107
MC/DC50%1/2
$expr =~ /^(['"])/ and $expr =~ s/^($1)([^$1]*)$1//
$expr =~ /^(['"])/ $expr =~ s/^($1)([^$1]*)$1//
Truth table66%2/3
A and B
A= $expr =~ /^(['"])/B= $expr =~ s/^($1)([^$1]*)$1//
ABresult
0X0
100
111
290 107 $newt->{oper} = 'const';
291 107 $newt->{val} = $2;
292 # Double quoted, understand some escapes:
293 107 ▶ $newt->{val} =~ s/\\([nrt\\]|x[\da-fA-F]{2}|u\{([\da-fA-F]+)\})/length($1) == 1 ? $escapes{$1} : defined($2) ? (chr hex $2) : (chr hex '0'.$1)/ge if($1 eq '"');
Branch: 3 branches
BranchTrueFalseTotal
if $1 eq "\"" 88 19 107
length $1 == 1 ? : 3 4 7
defined $2 ? : 3 1 4
294 107 $newt->{type} = 'str';
295 107 $operlast = 0;
296 } elsif($expr =~ s/^}//) {
297 # Always need a ';' after this - magic one up to be sure
298 # If not then flow operators screw up.
299 45 $newt->{oper} = '}';
300 45 $GenSemiColon = 1;
301 45 $operlast = 1;
302 } # Match (operators). Need \b after things like 'ne' so that it is not start of var name:
303 elsif($expr =~ s@^(\+\+|--|:=|>=|<=|==|<>|!=|&&|\|\||lt\b|gt\b|le\b|ge\b|eq\b|ne\b|\*\*|[-~!./*%+,<>\?:\(\)\[\]{])@@) {
304 1387 $newt->{oper} = $1;
305 # Monadic if the previous token was an operator and this one can be monadic:
306 1387 ▶ if($operlast && defined($MonOp{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operlast and defined $MonOp{$1}) 69 1318 1387
Condition100%3/3
$operlast and defined $MonOp{$1}
!ll&&!rl&&r
113917969
MC/DC100%2/2
$operlast and defined $MonOp{$1}
$operlast defined $MonOp{$1}
Truth table100%3/3
A and B
A= $operlastB= defined $MonOp{$1}
ABresult
0X0
100
111
307 69 $newt->{oper} = 'M' . $1;
308 69 $newt->{monop} = $1; # Monop flag & for error reporting
309 }
310 1387 ▶ if(defined($MonVarOp{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $MonVarOp{$1}) 25 1362 1387
311 25 $newt->{monop} = $1; # Monop flag & for error reporting
312 }
313  
314 # If we see '()' push the empty list as '()' will just be eliminated - as if never there.
315 1387 ▶ if($ln eq '(' && $1 eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln eq '(' and $1 eq ')') { } 5 1382 1387
Condition100%3/3
$ln eq "(" and $1 eq ")"
!ll&&!rl&&r
1365175
MC/DC100%2/2
$ln eq "(" and $1 eq ")"
$ln eq "(" $1 eq ")"
Truth table100%3/3
A and B
A= $ln eq "("B= $1 eq ")"
ABresult
0X0
100
111
316 5 push @tree, {oper => 'var', name => 'EmptyList'};
317 } else {
318 1382 ▶ $operlast = 1 unless($1 eq ')' or $1 eq ']');
Branch: 1 branch
BranchTrueFalseTotal
unless $1 eq ")" or $1 eq "]" 1140 242 1382
Condition100%3/3
$1 eq ")" or $1 eq "]"
l!l&&r!l&&!r
197451140
MC/DC100%2/2
$1 eq ")" or $1 eq "]"
$1 eq ")" $1 eq "]"
Truth table100%3/3
A or B
A= $1 eq ")"B= $1 eq "]"
ABresult
1X1
011
000
319 }
320 } # Flow: if/while:
321 elsif($expr =~ s@^(if|while)@@) {
322 51 $newt->{oper} = 'flow';
323 51 $newt->{flow} = $1;
324 51 $operlast = 0;
325 } # Semi-colon:
326 elsif($expr =~ s@^;@@) {
327 167 $newt->{oper} = ';';
328 167 $operlast = 0;
329 } # Match 'function(', leave '(' in input:
330 elsif($expr =~ s/^([_a-z][\w]*)\(/(/i) {
331 72 ▶ unless($self->{Functions}->{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Functions"}{$1}) 1 71 72
332 1 $self->PrintError("When parsing: found unknown function '%s'", $1);
333 1 return;
334 }
335 71 $newt->{oper} = 'func';
336 71 $newt->{fname} = $1;
337 71 $operlast = 1; # So that argument can be monadic
338 } # Match VarName or $VarName or $123
339 elsif($expr =~ s/^\$?([_a-z]\w*)//i) {
340 634 $newt->{oper} = 'var';
341 634 ▶ $newt->{name} = defined($1) ? $1 : defined($2) ? $2 : $3;
Branch: 2 branches
BranchTrueFalseTotal
defined $1 ? : 634 0 634
defined $2 ? : 0 0 0
342 634 $operlast = 0;
343 } else {
344 1 $self->PrintError("Unrecognised input in expression at '%s'", $expr);
345 1 return;
346 }
347  
348 # Processed everything ?
349 4813 ▶ if(!@operators && $EndInput) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @operators and $EndInput) 514 4299 4813
Condition100%3/3
not @operators and $EndInput
!ll&&!rl&&r
32921007514
MC/DC100%2/2
not @operators and $EndInput
not @operators $EndInput
Truth table100%3/3
not(A) and B
A= not @operatorsB= $EndInput
ABresult
0X0
100
111
350 514 return pop @tree;
351 }
352  
353 # What is new token ?
354 4299 ▶ $ln = $newt ? $newt->{oper} : '';
Branch: 1 branch
BranchTrueFalseTotal
$newt ? : 3785 514 4299
355  
356 # Grammatical part
357 # Move what we can from @operators to @tree
358  
359 4299 my $loopb = 0; # Loop buster
360 4299 ▶ while(@operators || $newt) {
Branch: 1 branch
BranchTrueFalseTotal
if (@operators or $newt) 5436 511 5947
Condition100%3/3
@operators or $newt
l!l&&r!l&&!r
38881548511
MC/DC100%2/2
@operators or $newt
@operators $newt
Truth table100%3/3
A or B
A= @operatorsB= $newt
ABresult
1X1
011
000
361  
362 # End of input ?
363 5436 ▶ if($EndInput and @operators == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EndInput and @operators == 0) 0 5436 5436
Condition66%2/3
$EndInput and @operators == 0
!ll&&!rl&&r
49225140
MC/DC0%0/2
$EndInput and @operators == 0
$EndInput @operators == 0
Truth table66%2/3
A and B
A= $EndInputB= @operators == 0
ABresult
0X0
100
111
364 0 ▶ if(@tree != 1) { # There should be one node left - the root
Branch: 1 branch
BranchTrueFalseTotal
if (@tree != 1) 0 0 0
365 0 ▶ $self->PrintError("Expression error - %s",
Branch: 1 branch
BranchTrueFalseTotal
$#tree == -1 ? : 0 0 0
366 $#tree == -1 ? "it's incomplete" : "missing operator");
367 0 return;
368 }
369 0 return pop @tree;
370 }
371  
372 # Terminal (var/const). Shift: push it onto the tree:
373 5436 ▶ if($newt and ($newt->{oper} eq 'var' or $newt->{oper} eq 'const')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newt and $newt->{"oper"} eq "var" || $newt->{"oper"} eq "const") 1496 3940 5436
Condition100%6/6
$newt and $newt->{"oper"} eq "var" || $newt->{"oper"} eq "const"
!ll&&!rl&&r
51434261496
$newt->{"oper"} eq "var" or $newt->{"oper"} eq "const"
l!l&&r!l&&!r
6348623426
MC/DC100%3/3
$newt and $newt->{"oper"} eq "var" || $newt->{"oper"} eq "const"
$newt $newt->{"oper"} eq "var" $newt->{"oper"} eq "const"
Truth table100%4/4
A and B or C
A= $newtB= $newt->{"oper"} eq "var"C= $newt->{"oper"} eq "const"
ABCresult
0XX0
1000
11X1
1011
374 1496 ▶ $operators[-1]->{after} = 1 if(@operators);
Branch: 1 branch
BranchTrueFalseTotal
if @operators 1074 422 1496
375  
376 1496 push @tree, $newt;
377 1496 last; # get next token
378 } # It must be an operator, which must have a terminal to it's left side:
379  
380 # Eliminate () - where current node is a close bracket
381 3940 ▶ if($newt and @operators and $operators[-1]->{oper} eq '(' and $newt->{oper} eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($newt and @operators and $operators[-1]{"oper"} eq "(" and $newt->{"oper"} eq ")") 200 3740 3940
Condition100%9/9
$newt and @operators and $operators[-1]{"oper"} eq "(" and $newt->{"oper"} eq ")"
!ll&&!rl&&r
3582158200
$newt and @operators and $operators[-1]{"oper"} eq "("
!ll&&!rl&&r
16401942358
$newt and @operators
!ll&&!rl&&r
51411262300
MC/DC100%4/4
$newt and @operators and $operators[-1]{"oper"} eq "(" and $newt->{"oper"} eq ")"
$newt @operators $operators[-1]{"oper"} eq "(" $newt->{"oper"} eq ")"
Truth table100%5/5
A and B and C and D
A= $newtB= @operatorsC= $operators[-1]{"oper"} eq "("D= $newt->{"oper"} eq ")"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
382 200 ▶ if($EndInput and $#operators != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EndInput and $#operators != 0) 0 200 200
Condition33%1/3
$EndInput and $#operators != 0
!ll&&!rl&&r
20000
MC/DC0%0/2
$EndInput and $#operators != 0
$EndInput $#operators != 0
Truth table33%1/3
A and B
A= $EndInputB= $#operators != 0
ABresult
0X0
100
111
383 0 $self->PrintError("Unexpected end of expression with unmatched '$operators[-1]->{oper}'");
384 0 return;
385 }
386  
387 200 pop @operators;
388 200 last; # get next token
389 }
390  
391 # Should have a new node to play with - unless end of string
392 3740 ▶ if(!$newt && !$EndInput) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newt or $EndInput) 0 3740 3740
Condition66%2/3
$newt or $EndInput
l!l&&r!l&&!r
32265140
MC/DC0%0/2
$newt or $EndInput
$newt $EndInput
Truth table66%2/3
A or B
A= $newtB= $EndInput
ABresult
1X1
011
000
393 0 ▶ if($loopb++ > 40) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loopb++ > 40) 0 0 0
394 0 $self->PrintError("Internal error, infinite loop at: $expr");
395 0 return;
396 }
397 0 next;
398 }
399  
400 3740 my $NewOpPrec; # EOF is ultra low precedence
401 3740 ▶ $NewOpPrec = ($newt) ? $OperPrec{$newt->{oper}}[1] : -100; # Just read precedence
Branch: 1 branch
BranchTrueFalseTotal
$newt ? : 3226 514 3740
402
403  
404 # If there is a new operator & it is higher precedence than the one at the top of @operators, push it
405 # Also put if @operators is empty
406 3740 ▶ if($newt && @operators) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newt and @operators) 2100 1640 3740
Condition100%3/3
$newt and @operators
!ll&&!rl&&r
51411262100
MC/DC100%2/2
$newt and @operators
$newt @operators
Truth table100%3/3
A and B
A= $newtB= @operators
ABresult
0X0
100
111
407 2100 ▶ print "Undefined NEWOPrec\n" unless defined $NewOpPrec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $NewOpPrec 0 2100 2100
408 2100 ▶ print "undefeined op-1 oper '$operators[-1]->{oper}'\n" unless(defined $OperPrec{$operators[-1]->{oper}}[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $OperPrec{$operators[-1]{"oper"}}[0] 0 2100 2100
409 }
410 3740 ▶ if($newt && (!@operators or (@operators && $NewOpPrec > $OperPrec{$operators[-1]->{oper}}[0]))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newt and !@operators || @operators && $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]) 1954 1786 3740
Condition88%8/9
$newt and !@operators || @operators && $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
!ll&&!rl&&r
51412721954
not @operators or @operators and $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
l!l&&r!l&&!r
11268281272
@operators and $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
!ll&&!rl&&r
01272828
MC/DC75%3/4
$newt and !@operators || @operators && $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
$newt not @operators @operators $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
Truth table80%4/5
A and not(B) or C and D
A= $newtB= not @operatorsC= @operatorsD= $NewOpPrec > $OperPrec{$operators[-1]{"oper"}}[0]
ABCDresult
0XXX0
100X0
10100
11XX1
10111
411 1954 ▶ $operators[-1]->{after} = 1 if(@operators);
Branch: 1 branch
BranchTrueFalseTotal
if @operators 828 1126 1954
412 1954 push @operators, $newt;
413 1954 last; # get next token
414 }
415  
416 # Flows (if/while) must not be reduced unless the newop is ';' '}' 'EOF' - ALSO PUSH
417 1786 ▶ if(@operators && $operators[-1]->{oper} eq 'flow' && $newt && $newt->{oper} ne ';' && $newt->{oper} ne 'EOF' && $newt->{oper} ne '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (@operators and $operators[-1]{"oper"} eq "flow" and $newt and $newt->{"oper"} ne ";" and $newt->{"oper"} ne "EOF" and $newt->{"oper"} ne "}") 35 1751 1786
Condition86%13/15
@operators and $operators[-1]{"oper"} eq "flow" and $newt and $newt->{"oper"} ne ";" and $newt->{"oper"} ne "EOF" and $newt->{"oper"} ne "}"
!ll&&!rl&&r
1749235
@operators and $operators[-1]{"oper"} eq "flow" and $newt and $newt->{"oper"} ne ";" and $newt->{"oper"} ne "EOF"
!ll&&!rl&&r
1743637
@operators and $operators[-1]{"oper"} eq "flow" and $newt and $newt->{"oper"} ne ";"
!ll&&!rl&&r
1735843
@operators and $operators[-1]{"oper"} eq "flow" and $newt
!ll&&!rl&&r
1735051
@operators and $operators[-1]{"oper"} eq "flow"
!ll&&!rl&&r
0173551
MC/DC66%4/6
@operators and $operators[-1]{"oper"} eq "flow" and $newt and $newt->{"oper"} ne ";" and $newt->{"oper"} ne "EOF" and $newt->{"oper"} ne "}"
@operators $operators[-1]{"oper"} eq "flow" $newt $newt->{"oper"} ne ";" $newt->{"oper"} ne "EOF" $newt->{"oper"} ne "}"
Truth table71%5/7
A and B and C and D and E and F
A= @operatorsB= $operators[-1]{"oper"} eq "flow"C= $newtD= $newt->{"oper"} ne ";"E= $newt->{"oper"} ne "EOF"F= $newt->{"oper"} ne "}"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
418 35 ▶ $operators[-1]->{after} = 1 if(@operators);
Branch: 1 branch
BranchTrueFalseTotal
if @operators 35 0 35
419 35 push @operators, $newt;
420 35 last;
421 }
422  
423 # Reduce, ie where we have everything move operators from @operators to @tree, their operands will be on @tree
424 # Reduce when the new operator precedence is lower than or equal to the one at the top of @operators
425 1751 ▶ if(@operators && $NewOpPrec <= $OperPrec{$operators[-1]->{oper}}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@operators and $NewOpPrec <= $OperPrec{$operators[-1]{"oper"}}[0]) 1751 0 1751
Condition33%1/3
@operators and $NewOpPrec <= $OperPrec{$operators[-1]{"oper"}}[0]
!ll&&!rl&&r
001751
MC/DC0%0/2
@operators and $NewOpPrec <= $OperPrec{$operators[-1]{"oper"}}[0]
@operators $NewOpPrec <= $OperPrec{$operators[-1]{"oper"}}[0]
Truth table33%1/3
A and B
A= @operatorsB= $NewOpPrec <= $OperPrec{$operators[-1]{"oper"}}[0]
ABresult
0X0
100
111
426  
427 # One of the pains is a trailing ';', ie nothing following it.
428 # Detect it and junk it
429 1751 ▶ if($operators[-1]->{oper} eq ';' && !defined $operators[-1]->{after}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operators[-1]{"oper"} eq ";" and not defined $operators[-1]{"after"}) 79 1672 1751
Condition100%3/3
$operators[-1]{"oper"} eq ";" and not defined $operators[-1]{"after"}
!ll&&!rl&&r
154013279
MC/DC100%2/2
$operators[-1]{"oper"} eq ";" and not defined $operators[-1]{"after"}
$operators[-1]{"oper"} eq ";" not defined $operators[-1]{"after"}
Truth table100%3/3
A and not(B)
A= $operators[-1]{"oper"} eq ";"B= not defined $operators[-1]{"after"}
ABresult
0X0
100
111
430 79 pop @operators;
431 79 next;
432 }
433  
434 # If top op is { & new op is } - pop them:
435 1672 ▶ if(@operators && $newt && $operators[-1]->{oper} eq '{' && $newt->{oper} eq '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (@operators and $newt and $operators[-1]{"oper"} eq "{" and $newt->{"oper"} eq "}") 44 1628 1672
Condition88%8/9
@operators and $newt and $operators[-1]{"oper"} eq "{" and $newt->{"oper"} eq "}"
!ll&&!rl&&r
1627144
@operators and $newt and $operators[-1]{"oper"} eq "{"
!ll&&!rl&&r
514111345
@operators and $newt
!ll&&!rl&&r
05141158
MC/DC75%3/4
@operators and $newt and $operators[-1]{"oper"} eq "{" and $newt->{"oper"} eq "}"
@operators $newt $operators[-1]{"oper"} eq "{" $newt->{"oper"} eq "}"
Truth table80%4/5
A and B and C and D
A= @operatorsB= $newtC= $operators[-1]{"oper"} eq "{"D= $newt->{"oper"} eq "}"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
436 44 pop @operators; # Lose the open curly
437  
438 # Unless we uncovered a flow - get next token
439 44 ▶ last unless(@operators && $operators[-1]->{oper} eq 'flow');
Branch: 1 branch
BranchTrueFalseTotal
unless @operators and $operators[-1]{"oper"} eq "flow" 9 35 44
Condition66%2/3
@operators and $operators[-1]{"oper"} eq "flow"
!ll&&!rl&&r
9035
MC/DC50%1/2
@operators and $operators[-1]{"oper"} eq "flow"
@operators $operators[-1]{"oper"} eq "flow"
Truth table66%2/3
A and B
A= @operatorsB= $operators[-1]{"oper"} eq "flow"
ABresult
0X0
100
111
440  
441 35 $newt = undef; # So that we do a last below
442 }
443 1663 my $op = pop @operators;
444 1663 my $func = $op->{oper} eq 'func';
445 1663 my $flow = $op->{oper} eq 'flow';
446 1663 my $monop = defined($op->{monop});
447  
448 # Enough on the tree ?
449 1663 ▶ unless(@tree >= (($func | $monop | $flow) ? 1 : 2)) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@tree >= ($func | $monop | $flow ? 1 : 2)) 523 1140 1663
$func | $monop | $flow ? : 215 1448 1663
450 # ';' are special, don't need operands, also can lose empty ';' nodes
451 523 ▶ next if($op->{oper} eq ';' or $op->{oper} eq 'EOF');
Branch: 1 branch
BranchTrueFalseTotal
if $op->{"oper"} eq ";" or $op->{"oper"} eq "EOF" 512 11 523
Condition100%3/3
$op->{"oper"} eq ";" or $op->{"oper"} eq "EOF"
l!l&&r!l&&!r
151111
MC/DC100%2/2
$op->{"oper"} eq ";" or $op->{"oper"} eq "EOF"
$op->{"oper"} eq ";" $op->{"oper"} eq "EOF"
Truth table100%3/3
A or B
A= $op->{"oper"} eq ";"B= $op->{"oper"} eq "EOF"
ABresult
1X1
011
000
452  
453 $self->PrintError("Missing operand to operator '%s' at %s", $op->{oper},
454 11 ▶ ($expr ne '' ? "'$expr'" : 'end'));
Branch: 1 branch
BranchTrueFalseTotal
$expr ne '' ? : 1 10 11
455  
456 11 return;
457 }
458  
459 # Push $op to @tree, first give it right & left children taken from the top of @tree
460 1140 $op->{right} = pop @tree;
461 1140 ▶ unless($monop or $func) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($monop or $func) 978 162 1140
Condition100%3/3
$monop or $func
l!l&&r!l&&!r
9270978
MC/DC100%2/2
$monop or $func
$monop $func
Truth table100%3/3
A or B
A= $monopB= $func
ABresult
1X1
011
000
462 # Monadic operators & functions do not have a 'left' child.
463 978 $op->{left} = pop @tree;
464 }
465  
466 1140 ▶ $op->{oper} = ';' if($op->{oper} eq 'EOF'); # ie join to previous
Branch: 1 branch
BranchTrueFalseTotal
if $op->{"oper"} eq "EOF" 3 1137 1140
467 1140 push @tree, $op;
468  
469 $newt = undef
470 1140 ▶ if($newt && $op->{oper} eq '[' && $newt->{oper} eq ']');
Branch: 1 branch
BranchTrueFalseTotal
if $newt and $op->{"oper"} eq "[" and $newt->{"oper"} eq "]" 45 1095 1140
Condition83%5/6
$newt and $op->{"oper"} eq "[" and $newt->{"oper"} eq "]"
!ll&&!rl&&r
1095045
$newt and $op->{"oper"} eq "["
!ll&&!rl&&r
38105745
MC/DC66%2/3
$newt and $op->{"oper"} eq "[" and $newt->{"oper"} eq "]"
$newt $op->{"oper"} eq "[" $newt->{"oper"} eq "]"
Truth table75%3/4
A and B and C
A= $newtB= $op->{"oper"} eq "["C= $newt->{"oper"} eq "]"
ABCresult
0XX0
10X0
1100
1111
471  
472 1140 ▶ last unless($newt); # get next token
Branch: 1 branch
BranchTrueFalseTotal
unless $newt 83 1057 1140
473 }
474 }
475 }
476 }
477  
478 # Check the tree for problems, args:
479 # 0 Self
480 # 1 a tree, return that tree, return undef on error.
481 # Report errors with $ErrFunc.
482 # To prevent a cascade of errors all due to one fault, use $ChkErrs to only print the first one.
483 2 my $ChkErrs;
484 2634 ▶sub CheckTree {
Subroutine CheckTree: called CC=1 SCAR=0.0
Pod documented
485 2634 $ChkErrs = 0;
486 2634 return &CheckTreeInt(@_);
487 }
488  
489 # Internal CheckTree
490 2634 ▶sub CheckTreeInt {
Subroutine CheckTreeInt: called CC=31 SCAR=42.8
Pod undocumented
491 2634 my ($self, $tree) = @_;
492 2634 ▶ return unless(defined($tree));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 13 2621 2634
493  
494 2621 ▶ return $tree if($tree->{oper} eq 'var' or $tree->{oper} eq 'const');
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"oper"} eq "var" or $tree->{"oper"} eq "const" 1487 1134 2621
Condition100%3/3
$tree->{"oper"} eq "var" or $tree->{"oper"} eq "const"
l!l&&r!l&&!r
6378501134
MC/DC100%2/2
$tree->{"oper"} eq "var" or $tree->{"oper"} eq "const"
$tree->{"oper"} eq "var" $tree->{"oper"} eq "const"
Truth table100%3/3
A or B
A= $tree->{"oper"} eq "var"B= $tree->{"oper"} eq "const"
ABresult
1X1
011
000
495  
496 1134 my $ok = 1;
497  
498 1134 ▶ if(defined($MatchOp{$tree->{oper}}) or defined($MatchOpClose{$tree->{oper}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $MatchOp{$tree->{"oper"}} or defined $MatchOpClose{$tree->{"oper"}}) 0 1134 1134
Condition33%1/3
defined $MatchOp{$tree->{"oper"}} or defined $MatchOpClose{$tree->{"oper"}}
l!l&&r!l&&!r
001134
MC/DC0%0/2
defined $MatchOp{$tree->{"oper"}} or defined $MatchOpClose{$tree->{"oper"}}
defined $MatchOp{$tree->{"oper"}} defined $MatchOpClose{$tree->{"oper"}}
Truth table33%1/3
A or B
A= defined $MatchOp{$tree->{"oper"}}B= defined $MatchOpClose{$tree->{"oper"}}
ABresult
1X1
011
000
499 0 $self->PrintError("Unmatched bracket '%s'", $tree->{oper});
500 0 $ok = 0;
501 }
502  
503 1134 ▶ if(defined($MonVarOp{$tree->{oper}}) and (!defined($tree->{right}) or ($tree->{right}{oper} ne '[' and $tree->{right}{oper} ne 'var'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $MonVarOp{$tree->{"oper"}} and !defined($tree->{"right"}) || $tree->{"right"}{"oper"} ne "[" && $tree->{"right"}{"oper"} ne "var") 1 1133 1134
Condition88%8/9
defined $MonVarOp{$tree->{"oper"}} and !defined($tree->{"right"}) || $tree->{"right"}{"oper"} ne "[" && $tree->{"right"}{"oper"} ne "var"
!ll&&!rl&&r
1110231
not defined $tree->{"right"} or $tree->{"right"}{"oper"} ne "[" and $tree->{"right"}{"oper"} ne "var"
l!l&&r!l&&!r
0123
$tree->{"right"}{"oper"} ne "[" and $tree->{"right"}{"oper"} ne "var"
!ll&&!rl&&r
7161
MC/DC75%3/4
defined $MonVarOp{$tree->{"oper"}} and !defined($tree->{"right"}) || $tree->{"right"}{"oper"} ne "[" && $tree->{"right"}{"oper"} ne "var"
defined $MonVarOp{$tree->{"oper"}} not defined $tree->{"right"} $tree->{"right"}{"oper"} ne "[" $tree->{"right"}{"oper"} ne "var"
Truth table80%4/5
A and not(B) or C and D
A= defined $MonVarOp{$tree->{"oper"}}B= not defined $tree->{"right"}C= $tree->{"right"}{"oper"} ne "["D= $tree->{"right"}{"oper"} ne "var"
ABCDresult
0XXX0
100X0
10100
11XX1
10111
504 1 $self->PrintError("Operand to '%s' must be a variable or indexed array element", $tree->{oper});
505 1 $ok = 0;
506 }
507  
508 1134 ▶ if($tree->{oper} eq '?' and $tree->{right}{oper} ne ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"oper"} eq "?" and $tree->{"right"}{"oper"} ne ":") 1 1133 1134
Condition100%3/3
$tree->{"oper"} eq "?" and $tree->{"right"}{"oper"} ne ":"
!ll&&!rl&&r
1121121
MC/DC100%2/2
$tree->{"oper"} eq "?" and $tree->{"right"}{"oper"} ne ":"
$tree->{"oper"} eq "?" $tree->{"right"}{"oper"} ne ":"
Truth table100%3/3
A and B
A= $tree->{"oper"} eq "?"B= $tree->{"right"}{"oper"} ne ":"
ABresult
0X0
100
111
509 1 ▶ $self->PrintError("Missing ':' operator after '?' operator") unless($ChkErrs);
Branch: 1 branch
BranchTrueFalseTotal
unless $ChkErrs 1 0 1
510 1 $ok = 0;
511 }
512  
513 1134 ▶ if($tree->{oper} ne 'func') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"oper"} ne "func") 1064 70 1134
514 1064 ▶ unless((!defined($tree->{left}) and defined($tree->{monop})) or $self->CheckTree($tree->{left})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $tree->{"left"} and defined $tree->{"monop"} or $self->CheckTree($tree->{"left"})) 0 1064 1064
Condition66%4/6
not defined $tree->{"left"} and defined $tree->{"monop"} or $self->CheckTree($tree->{"left"})
l!l&&r!l&&!r
919730
not defined $tree->{"left"} and defined $tree->{"monop"}
!ll&&!rl&&r
973091
MC/DC0%0/3
not defined $tree->{"left"} and defined $tree->{"monop"} or $self->CheckTree($tree->{"left"})
not defined $tree->{"left"} defined $tree->{"monop"} $self->CheckTree($tree->{"left"})
Truth table40%2/5
not(A) and B or C
A= not defined $tree->{"left"}B= defined $tree->{"monop"}C= $self->CheckTree($tree->{"left"})
ABCresult
11X1
0X11
1011
0X00
1000
515 0 ▶ $self->PrintError("Missing LH expression to '%s'", defined($tree->{monop}) ? $tree->{monop} : $tree->{oper}) unless($ChkErrs);
Branch: 2 branches
BranchTrueFalseTotal
unless $ChkErrs 0 0 0
defined $tree->{'monop'} ? : 0 0 0
516 0 $ok = 0;
517 }
518 }
519 1134 ▶ unless(&CheckTree($self, $tree->{right})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&CheckTree($self, $tree->{"right"})) 0 1134 1134
520 0 ▶ $self->PrintError("Missing RH expression to '%s'", defined($tree->{monop}) ? $tree->{monop} : $tree->{oper}) unless($ChkErrs);
Branch: 2 branches
BranchTrueFalseTotal
unless $ChkErrs 0 0 0
defined $tree->{'monop'} ? : 0 0 0
521 0 $ok = 0;
522 }
523  
524 1134 ▶ if($tree->{oper} eq 'func') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"oper"} eq "func") 70 1064 1134
525 70 my $fname = $tree->{fname};
526 70 ▶ if($InFunLV{$fname} and
Branch: 1 branch
BranchTrueFalseTotal
if ($InFunLV{$fname} and !defined($tree->{"right"}{"oper"}) || $tree->{"right"}{"oper"} ne "var" && $tree->{"right"}{"oper"} ne "," && (!defined($tree->{"right"}{"left"}{"oper"}) || $tree->{"right"}{"left"}{"oper"} ne "var")) 0 70 70
Condition40%6/15
$InFunLV{$fname} and !defined($tree->{"right"}{"oper"}) || $tree->{"right"}{"oper"} ne "var" && $tree->{"right"}{"oper"} ne "," && (!defined($tree->{"right"}{"left"}{"oper"}) || $tree->{"right"}{"left"}{"oper"} ne "var")
!ll&&!rl&&r
56140
not defined $tree->{"right"}{"oper"} or $tree->{"right"}{"oper"} ne "var" and $tree->{"right"}{"oper"} ne "," and !defined($tree->{"right"}{"left"}{"oper"}) || $tree->{"right"}{"left"}{"oper"} ne "var"
l!l&&r!l&&!r
0014
$tree->{"right"}{"oper"} ne "var" and $tree->{"right"}{"oper"} ne "," and !defined($tree->{"right"}{"left"}{"oper"}) || $tree->{"right"}{"left"}{"oper"} ne "var"
!ll&&!rl&&r
1400
$tree->{"right"}{"oper"} ne "var" and $tree->{"right"}{"oper"} ne ","
!ll&&!rl&&r
950
not defined $tree->{"right"}{"left"}{"oper"} or $tree->{"right"}{"left"}{"oper"} ne "var"
l!l&&r!l&&!r
000
MC/DC0%0/6
$InFunLV{$fname} and !defined($tree->{"right"}{"oper"}) || $tree->{"right"}{"oper"} ne "var" && $tree->{"right"}{"oper"} ne "," && (!defined($tree->{"right"}{"left"}{"oper"}) || $tree->{"right"}{"left"}{"oper"} ne "var")
$InFunLV{$fname} not defined $tree->{"right"}{"oper"} $tree->{"right"}{"oper"} ne "var" $tree->{"right"}{"oper"} ne "," not defined $tree->{"right"}{"left"}{"oper"} $tree->{"right"}{"left"}{"oper"} ne "var"
Truth table42%3/7
A and not(B) or C and D and not(E) or F
A= $InFunLV{$fname}B= not defined $tree->{"right"}{"oper"}C= $tree->{"right"}{"oper"} ne "var"D= $tree->{"right"}{"oper"} ne ","E= not defined $tree->{"right"}{"left"}{"oper"}F= $tree->{"right"}{"left"}{"oper"} ne "var"
ABCDEFresult
0XXXXX0
100XXX0
1010XX0
1011000
11XXXX1
10111X1
1011011
527 (!defined($tree->{right}->{oper}) or (($tree->{right}->{oper} ne 'var' and $tree->{right}->{oper} ne ',') and (!defined($tree->{right}->{left}->{oper}) or $tree->{right}->{left}->{oper} ne 'var')))) {
528 0 $self->PrintError("First argument to $fname must be a variable");
529 0 $ok = 0;
530 }
531 }
532  
533 1134 ▶ $ChkErrs = 1 unless($ok);
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 1132 1134
534 1134 ▶ return $ok ? $tree : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 1132 2 1134
535 }
536  
537 # Parse & check an argument string, return the parsed tree.
538 # Report errors with $ErrFunc.
539 # 0 Self
540 # 1 an expression
541 527 ▶sub Parse {
Subroutine Parse: called CC=1 SCAR=0.0
Pod documented
542 527 my ($self, $expr) = @_;
543  
544 527 return $self->CheckTree($self->ParseString($expr));
545 }
546  
547 # Print a tree - for debugging purposes. Args:
548 # 0 Self
549 # 1 A tree
550 # Hidden second argument is the initial indent level.
551 0 ▶sub PrintTree {
Subroutine PrintTree: not called CC=5 SCAR=34.0
Pod undocumented
552 0 my ($self, $nodp, $dl) = @_;
553  
554 0 ▶ $dl = 0 unless(defined($dl));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dl 0 0 0
555 0 $dl++;
556  
557 0 ▶ unless(defined($nodp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodp) 0 0 0
558 0 print " " x $dl . "UNDEF\n";
559 0 return;
560 }
561  
562 0 print " " x $dl;
563 0 print "nod=$nodp [$nodp->{oper}] P-JR $OperPrec{$nodp->{oper}}[1] ";
564  
565 0 ▶ if($nodp->{oper} eq 'var') {
Branch: 2 branches
BranchTrueFalseTotal
if ($nodp->{'oper'} eq 'var') { } 0 0 0
elsif ($nodp->{'oper'} eq 'const') { } 0 0 0
566 0 print "var($nodp->{name}) \n";
567 } elsif($nodp->{oper} eq 'const') {
568 0 print "const($nodp->{val}) \n";
569 } else {
570 0 print "\n";
571 0 print " " x $dl;print "Desc L \n";
572 0 $self->PrintTree($nodp->{left}, $dl);
573  
574 0 print " " x $dl;print "op '$nodp->{oper}' P-TOS $OperPrec{$nodp->{oper}}[0] at $nodp\n";
575  
576 0 print " " x $dl;print "Desc R \n";
577 0 $self->PrintTree($nodp->{right}, $dl);
578 }
579 }
580  
581 # Evaluate a tree. Return a scalar.
582 # Args:
583 # 0 Self
584 # 1 The root of a tree.
585 9 ▶sub EvalToScalar {
Subroutine EvalToScalar: called CC=1 SCAR=0.0
Pod documented
586 9 my ($self, $tree) = @_;
587 9 my @res = $self->Eval($tree);
588  
589 9 return $res[$#res];
590 }
591  
592 # Parse a string, check and evaluate it, return a scalar
593 # Args:
594 # 0 Self
595 # 1 String to evaluate.
596 # Return undef on error.
597 1 ▶sub ParseToScalar {
Subroutine ParseToScalar: called CC=2 SCAR=7.0
Pod documented
598 1 my ($self, $expr) = @_;
599  
600 1 my $tree = $self->Parse($expr);
601 1 ▶ return undef unless($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless $tree 0 1 1
602 1 return $self->EvalToScalar($tree);
603 }
604  
605 # Evaluate a tree. The result is an array, if you are expecting a single value it is the last (probably $#'th) element.
606 # Args:
607 # 0 Self
608 # 1 The root of a tree.
609 512 ▶sub Eval {
Subroutine Eval: called CC=1 SCAR=0.0
Pod documented
610 512 my ($self, $tree) = @_;
611  
612 512 $self->{LoopCount} = 0; # Count all loops
613 512 $self->{VarSetFun}($self, '_TIME', time);
614  
615 512 return $self->EvalTree($tree, 0);
616 }
617  
618 # Evaluate a tree. The result is an array, if you are expecting a single value it is the last (probably $#'th) element.
619 # Args:
620 # 0 Self
621 # 1 The root of a tree.
622 # 2 Want Lvalue flag -- return variable name rather than it's value
623 # Report errors with the function $PrintErrFunc
624 # Checking undefined values is a pain, assignment of undef & concat undef is OK.
625 3571 ▶sub EvalTree {
Subroutine EvalTree: called CC=107 SCAR=52.8
Pod documented
626 3571 my ($self, $tree, $wantlv) = @_;
627  
628 3571 ▶ return unless(defined($tree));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tree 0 3571 3571
629  
630 3571 my $oper = $tree->{oper};
631  
632 3571 ▶ return $tree->{val} if($oper eq 'const');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "const" 1041 2530 3571
633 2530 ▶ return $wantlv ? $tree->{name} : $self->{VarGetFun}($self, $tree->{name}) if($oper eq 'var');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "var" 956 1574 2530
$wantlv ? : 381 575 956
634  
635 # Some functions need to be given a lvalue
636 return $self->{FuncEval}($self, $tree, $tree->{fname},
637 1574 ▶ $self->EvalTree($tree->{right}, defined($InFunLV{$tree->{fname}}))) if($oper eq 'func');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "func" 70 1504 1574
638  
639 1504 ▶ if($oper eq '++' or $oper eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq "++" or $oper eq "--") 113 1391 1504
Condition100%3/3
$oper eq "++" or $oper eq "--"
l!l&&r!l&&!r
72411391
MC/DC100%2/2
$oper eq "++" or $oper eq "--"
$oper eq "++" $oper eq "--"
Truth table100%3/3
A or B
A= $oper eq "++"B= $oper eq "--"
ABresult
1X1
011
000
640 113 my ($right, @right, @left, $index, $name);
641 # The variable is either a simple variable or an indexed array
642 113 ▶ if($tree->{right}->{oper} eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{'right'}{'oper'} eq '[') { } 7 106 113
643 7 $name = $tree->{right}->{left}->{name};
644 7 $index = 1;
645  
646 7 @left = $self->EvalTree($tree->{right}->{left}, 0);
647  
648 7 @right = $self->EvalTree($tree->{right}->{right}, 0);
649 7 $index = $right[-1];
650  
651 7 ▶ unless($index =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index =~ /^-?\d+$/) 0 7 7
652 0 $self->PrintError("Array '%s' index is not integer '%s'", $name, $index);
653 0 return undef;
654 }
655  
656 7 ▶ $index += @left if($index < 0); # Convert -ve index to a +ve one, will still be -ve if it was very -ve to start with
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 4 3 7
657  
658 7 ▶ return undef if($index < 0 or $index > @left); # Out of bounds
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index > @left 2 5 7
Condition100%3/3
$index < 0 or $index > @left
l!l&&r!l&&!r
115
MC/DC100%2/2
$index < 0 or $index > @left
$index < 0 $index > @left
Truth table100%3/3
A or B
A= $index < 0B= $index > @left
ABresult
1X1
011
000
659  
660 5 $right = $left[$index];
661 5 $name = "$name\[$index\]";
662 } else {
663 106 @right = $self->EvalTree($tree->{right}, 0);
664 106 $right = $right[-1];
665 106 $name = $tree->{right}{name};
666 }
667  
668 111 ▶ $oper eq '++' ? $right++ : $right--;
Branch: 1 branch
BranchTrueFalseTotal
$oper eq '++' ? : 70 41 111
669  
670 111 $self->{VarSetFun}($self, $name, ($right));
671  
672 111 return $right;
673 }
674  
675 # Monadic operators:
676 1391 ▶ if(!defined($tree->{left}) and defined($tree->{monop})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tree->{"left"} and defined $tree->{"monop"}) 68 1323 1391
Condition66%2/3
not defined $tree->{"left"} and defined $tree->{"monop"}
!ll&&!rl&&r
1323068
MC/DC50%1/2
not defined $tree->{"left"} and defined $tree->{"monop"}
not defined $tree->{"left"} defined $tree->{"monop"}
Truth table66%2/3
not(A) and B
A= not defined $tree->{"left"}B= defined $tree->{"monop"}
ABresult
0X0
100
111
677 68 $oper = $tree->{monop};
678  
679 # Evaluate the (RH) operand
680 68 my @right = $self->EvalTree($tree->{right}, 0);
681 68 my $right = $right[$#right];
682 68 ▶ unless(defined($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $right) 0 68 68
683 0 ▶ unless($self->{AutoInit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"AutoInit"}) 0 0 0
684 0 $self->PrintError("Operand to mondaic operator '%s' is not defined", $oper);
685 0 return;
686 }
687 0 $right = 0; # Monadics are all numeric
688 }
689  
690 68 ▶ unless($right =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($right =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)$/i) 0 68 68
691 0 $self->PrintError("Operand to monadic '%s' is not numeric '%s'", $oper, $right);
692 0 return;
693 }
694 68 $right = "$1$2$3";
695  
696 68 ▶ return -$right if($oper eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "-" 57 11 68
697 11 ▶ return $right if($oper eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "+" 11 0 11
698 0 ▶ return !$right if($oper eq '!');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "!" 0 0 0
699 0 ▶ return ~$right if($oper eq '~');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "~" 0 0 0
700  
701 0 $self->PrintError("Unknown monadic operator when evaluating: '%s'", $oper);
702 0 return;
703 }
704  
705 # This is complicated by multiple assignment: (a, b, c) := (1, 2, 3, 4). 'c' is given '(3, 4)'.
706 # Assign the right value to the left node
707 # Where the values list is shorter, leave vars alone: (a, b, c) := (1, 2) does not change c.
708 1323 ▶ if($oper eq ':=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq ":=") 360 963 1323
709 360 my @left = $self->EvalTree($tree->{left}, 1);
710 360 my @right = $self->EvalTree($tree->{right}, $wantlv);
711  
712 # Easy case, assigning to one variable, assign the whole array:
713 360 ▶ return $self->{VarSetFun}($self, @left, @right) if($#right <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#right <= 0 314 46 360
714  
715 # Assign conseq values to conseq variables. The last var gets the rest of the values.
716 # Ignore too many vars.
717 46 ▶ for(my $i = 0; $i <= $#left; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#left) 52 1 53
718 52 ▶ last if($i > $#right);
Branch: 1 branch
BranchTrueFalseTotal
if $i > $#right 1 51 52
719  
720 51 ▶ if($i == $#left and $i != $#right) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#left and $i != $#right) 44 7 51
Condition100%3/3
$i == $#left and $i != $#right
!ll&&!rl&&r
6144
MC/DC100%2/2
$i == $#left and $i != $#right
$i == $#left $i != $#right
Truth table100%3/3
A and B
A= $i == $#leftB= $i != $#right
ABresult
0X0
100
111
721 44 $self->{VarSetFun}($self, $left[$i], @right[$i ... $#right]);
722 44 last;
723 }
724 7 $self->{VarSetFun}($self, $left[$i], $right[$i]);
725 }
726  
727 46 return @right;
728 }
729  
730 # Flow control: if/while
731 963 ▶ if($oper eq 'flow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq "flow") 78 885 963
732 78 ▶ if($tree->{flow} eq 'if') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"flow"} eq "if") 56 22 78
733 # left is condition, right is body when true
734 56 my @left = $self->EvalTree($tree->{left}, 0);
735 56 ▶ return ($left[-1]) ? ($self->EvalTree($tree->{right}, 0))[-1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$left[-1] ? : 34 22 56
736 }
737 22 ▶ if($tree->{flow} eq 'while') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->{"flow"} eq "while") 22 0 22
738 22 my $ret = 0; # Return val, until get something better
739 22 ▶ if( !$self->{PermitLoops}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"PermitLoops"}) 0 22 22
740 0 $self->PrintError("Loops not enabled, set property PermitLoops to do so");
741 0 return;
742 }
743 22 while(1) {
744 121 ▶ if($self->{MaxLoopCount} && ++$self->{LoopCount} > $self->{MaxLoopCount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MaxLoopCount"} and ++$self->{"LoopCount"} > $self->{"MaxLoopCount"}) 0 121 121
Condition33%1/3
$self->{"MaxLoopCount"} and ++$self->{"LoopCount"} > $self->{"MaxLoopCount"}
!ll&&!rl&&r
01210
MC/DC0%0/2
$self->{"MaxLoopCount"} and ++$self->{"LoopCount"} > $self->{"MaxLoopCount"}
$self->{"MaxLoopCount"} ++$self->{"LoopCount"} > $self->{"MaxLoopCount"}
Truth table33%1/3
A and B
A= $self->{"MaxLoopCount"}B= ++$self->{"LoopCount"} > $self->{"MaxLoopCount"}
ABresult
0X0
100
111
745 0 $self->PrintError("Loop exceeded maximum iterations: MaxLoopCount = $self->{MaxLoopCount}");
746 0 return;
747 }
748 # left is loop condition, right is body:
749 121 my @left = $self->EvalTree($tree->{left}, 0);
750 121 ▶ return $ret unless($left[-1]);
Branch: 1 branch
BranchTrueFalseTotal
unless $left[-1] 22 99 121
751 99 $ret = ($self->EvalTree($tree->{right}, 0))[-1];
752 }
753 0 return $ret;
754 }
755 }
756  
757 # Evaluate left - may be able to avoid evaluating right.
758 # Take care to avoid evaluating a tree twice, not just inefficient but nasty side effects with ++ & -- operators
759 885 my @left = $self->EvalTree($tree->{left}, $wantlv);
760 885 my $left = $left[$#left];
761 885 ▶ if(!defined($left) and $oper ne ',' and $oper ne '.' and $oper ne ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $left and $oper ne "," and $oper ne "." and $oper ne ";") 0 885 885
Condition66%6/9
not defined $left and $oper ne "," and $oper ne "." and $oper ne ";"
!ll&&!rl&&r
88500
not defined $left and $oper ne "," and $oper ne "."
!ll&&!rl&&r
88320
not defined $left and $oper ne ","
!ll&&!rl&&r
88122
MC/DC0%0/4
not defined $left and $oper ne "," and $oper ne "." and $oper ne ";"
not defined $left $oper ne "," $oper ne "." $oper ne ";"
Truth table60%3/5
not(A) and B and C and D
A= not defined $leftB= $oper ne ","C= $oper ne "."D= $oper ne ";"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
762 0 ▶ unless($self->{AutoInit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"AutoInit"}) 0 0 0
763 0 $self->PrintError("Left value to operator '%s' is not defined", $oper);
764 0 return;
765 }
766 0 $left = ''; # Set to the empty string
767 }
768  
769 # Lazy evaluation:
770 return $left ? $self->EvalTree($tree->{right}{left}, $wantlv) :
771 885 ▶ $self->EvalTree($tree->{right}{right}, $wantlv) if($oper eq '?');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "?" 12 873 885
$left ? : 5 7 12
772  
773 # Constructing a list of variable names (for assignment):
774 873 ▶ return (@left, $self->EvalTree($tree->{right}, 1)) if($oper eq ',' and $wantlv);
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "," and $wantlv 12 861 873
Condition100%3/3
$oper eq "," and $wantlv
!ll&&!rl&&r
74012112
MC/DC100%2/2
$oper eq "," and $wantlv
$oper eq "," $wantlv
Truth table100%3/3
A and B
A= $oper eq ","B= $wantlv
ABresult
0X0
100
111
775  
776 # More lazy evaluation:
777 861 ▶ if($oper eq '&&' or $oper eq '||') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq "&&" or $oper eq "||") 10 851 861
Condition100%3/3
$oper eq "&&" or $oper eq "||"
l!l&&r!l&&!r
64851
MC/DC100%2/2
$oper eq "&&" or $oper eq "||"
$oper eq "&&" $oper eq "||"
Truth table100%3/3
A or B
A= $oper eq "&&"B= $oper eq "||"
ABresult
1X1
011
000
778 10 ▶ return 0 if($oper eq '&&' and !$left);
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "&&" and not $left 2 8 10
Condition100%3/3
$oper eq "&&" and not $left
!ll&&!rl&&r
442
MC/DC100%2/2
$oper eq "&&" and not $left
$oper eq "&&" not $left
Truth table100%3/3
A and not(B)
A= $oper eq "&&"B= not $left
ABresult
0X0
100
111
779 8 ▶ return 1 if($oper eq '||' and $left);
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "||" and $left 2 6 8
Condition100%3/3
$oper eq "||" and $left
!ll&&!rl&&r
422
MC/DC100%2/2
$oper eq "||" and $left
$oper eq "||" $left
Truth table100%3/3
A and B
A= $oper eq "||"B= $left
ABresult
0X0
100
111
780  
781 6 my @right = $self->EvalTree($tree->{right}, 0);
782  
783 6 ▶ return($right[$#right] ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$right[$#right] ? : 3 3 6
784 }
785  
786 # Everything else is a binary operator, get right side - value(s):
787 851 my @right = $self->EvalTree($tree->{right}, 0);
788 851 my $right = $right[-1];
789  
790 851 ▶ return (@left, @right) if($oper eq ',');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "," 121 730 851
791 730 ▶ return @right if($oper eq ';');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq ";" 211 519 730
792  
793 # Array index. Beware: works differently depending on $wantlv.
794 # Because when $wantlv it is the array name, not its contents
795 519 ▶ if($oper eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq "[") 38 481 519
796 return undef # Check if the array member could exist; ie have index
797 38 ▶ if($right !~ /^-?\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $right =~ /^-?\d+$/ 0 38 38
798  
799 38 ▶ @left = $self->{VarGetFun}($self, $left[0]) if($wantlv);
Branch: 1 branch
BranchTrueFalseTotal
if $wantlv 20 18 38
800  
801 38 my $index = $right[-1];
802 38 ▶ $index += @left if($index < 0); # Convert -ve index to a +ve one
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 11 27 38
803  
804 38 ▶ return "$left\[$index]" # Return var[index] for assignment
Branch: 1 branch
BranchTrueFalseTotal
if $wantlv 20 18 38
805 if($wantlv);
806  
807 return undef # Check if the array member exists
808 18 ▶ if($index < 0 || $index > @left);
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index > @left 2 16 18
Condition100%3/3
$index < 0 or $index > @left
l!l&&r!l&&!r
1116
MC/DC100%2/2
$index < 0 or $index > @left
$index < 0 $index > @left
Truth table100%3/3
A or B
A= $index < 0B= $index > @left
ABresult
1X1
011
000
809  
810 16 return $left[$index];
811 }
812  
813  
814 # Everything else just takes a simple (non array) value, use last value in a list which is in $right.
815 # It is OK to concat undef.
816  
817 481 ▶ if($oper eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq ".") 14 467 481
818 # If one side is undef, treat as empty:
819 14 ▶ $left = "" unless(defined($left));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left 2 12 14
820 14 ▶ $right = "" unless(defined($right));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $right 0 14 14
821 14 ▶ if(length($left) + length($right) > $self->{StringMaxLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($left) + length($right) > $self->{"StringMaxLength"}) 0 14 14
822 0 $self->PrintError("Joined string would exceed maximum allowed %d", $self->{StringMaxLength});
823 0 return "";
824 }
825 14 return $left . $right;
826 }
827  
828 467 ▶ unless(defined($right)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $right) 0 467 467
829 0 ▶ unless($self->{AutoInit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"AutoInit"}) 0 0 0
830 0 $self->PrintError("Right value to operator '%s' is not defined", $oper);
831 0 return;
832 }
833 0 $right = '';
834 }
835  
836 467 ▶ return $left lt $right ? 1 : 0 if($oper eq 'lt');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "lt" 4 463 467
$left lt $right ? : 2 2 4
837 463 ▶ return $left gt $right ? 1 : 0 if($oper eq 'gt');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "gt" 6 457 463
$left gt $right ? : 1 5 6
838 457 ▶ return $left le $right ? 1 : 0 if($oper eq 'le');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "le" 3 454 457
$left le $right ? : 2 1 3
839 454 ▶ return $left ge $right ? 1 : 0 if($oper eq 'ge');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "ge" 3 451 454
$left ge $right ? : 2 1 3
840 451 ▶ return $left eq $right ? 1 : 0 if($oper eq 'eq');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "eq" 2 449 451
$left eq $right ? : 1 1 2
841 449 ▶ return $left ne $right ? 1 : 0 if($oper eq 'ne');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "ne" 4 445 449
$left ne $right ? : 2 2 4
842  
843 445 ▶ return ($left, $right) if($oper eq ':'); # Should not be used, done in '?'
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq ":" 0 445 445
844 # return $left ? $right[0] : $right[1] if($oper eq '?'); # Non lazy version
845  
846 # Everthing else is an arithmetic operator, check for left & right being numeric. NB: '-' 'cos may be -ve.
847 # Returning undef may result in a cascade of errors.
848 # Perl would treat 012 as an octal number, that would confuse most people, convert to a decimal interpretation.
849 445 ▶ unless($left =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)/i) { } 445 0 445
850 0 ▶ unless($self->{AutoInit} and $left eq '') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"AutoInit"} and $left eq "") 0 0 0
Condition0%0/3
$self->{"AutoInit"} and $left eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"AutoInit"} and $left eq ""
$self->{"AutoInit"} $left eq ""
Truth table0%0/3
A and B
A= $self->{"AutoInit"}B= $left eq ""
ABresult
0X0
100
111
851 0 $self->PrintError("Left hand operator to '%s' is not numeric '%s'", $oper, $left);
852 0 return;
853 }
854 0 $left = 0;
855 } else {
856 445 $left = "$1$2$3";
857 }
858  
859 445 ▶ unless($right =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right =~ /^([-+]?)0*([\d.]+)([ef][-+]?\d*|)/i) { } 444 1 445
860 1 ▶ unless($self->{AutoInit} and $right eq '') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"AutoInit"} and $right eq "") 1 0 1
Condition33%1/3
$self->{"AutoInit"} and $right eq ""
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"AutoInit"} and $right eq ""
$self->{"AutoInit"} $right eq ""
Truth table33%1/3
A and B
A= $self->{"AutoInit"}B= $right eq ""
ABresult
0X0
100
111
861 1 $self->PrintError("Right hand operator to '%s' is not numeric '%s'", $oper, $right);
862 1 return;
863 }
864 0 $right = 0;
865 } else {
866 444 $right = "$1$2$3";
867 }
868  
869 444 ▶ return $left * $right if($oper eq '*');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "*" 69 375 444
870 375 ▶ return $left / $right if($oper eq '/');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "/" 12 363 375
871 363 ▶ return $left % $right if($oper eq '%');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "%" 22 341 363
872 341 ▶ return $left + $right if($oper eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "+" 120 221 341
873 221 ▶ return $left - $right if($oper eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "-" 36 185 221
874 185 ▶ return $left ** $right if($oper eq '**');
Branch: 1 branch
BranchTrueFalseTotal
if $oper eq "**" 4 181 185
875  
876 # Force return of true/false -- NOT undef
877 181 ▶ return $left > $right ? 1 : 0 if($oper eq '>');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq ">" 81 100 181
$left > $right ? : 64 17 81
878 100 ▶ return $left < $right ? 1 : 0 if($oper eq '<');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "<" 52 48 100
$left < $right ? : 40 12 52
879 48 ▶ return $left >= $right ? 1 : 0 if($oper eq '>=');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq ">=" 3 45 48
$left >= $right ? : 2 1 3
880 45 ▶ return $left <= $right ? 1 : 0 if($oper eq '<=');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "<=" 3 42 45
$left <= $right ? : 2 1 3
881 42 ▶ return $left == $right ? 1 : 0 if($oper eq '==');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "==" 37 5 42
$left == $right ? : 21 16 37
882 5 ▶ return $left != $right ? 1 : 0 if($oper eq '!=');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "!=" 3 2 5
$left != $right ? : 2 1 3
883 2 ▶ return $left != $right ? 1 : 0 if($oper eq '<>');
Branch: 2 branches
BranchTrueFalseTotal
if $oper eq "<>" 2 0 2
$left != $right ? : 1 1 2
884  
885 0 $self->PrintError("Unknown operator when evaluating: '%s'", $oper);
886 0 return;
887 }
888  
889 # Evaluate a function:
890 70 ▶sub FuncValue {
Subroutine FuncValue: called CC=27 SCAR=33.2
Pod documented
891 70 my ($self, $tree, $fname, @arglist) = @_;
892  
893 # If there is a user supplied extra function evaluator, try that first:
894 70 my $res;
895 70 ▶ return $res if(defined($self->{ExtraFuncEval}) && defined($res = $self->{ExtraFuncEval}(@_)));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ExtraFuncEval"} and defined($res = $self->{"ExtraFuncEval"}->(@_)) 8 62 70
Condition100%3/3
defined $self->{"ExtraFuncEval"} and defined($res = $self->{"ExtraFuncEval"}->(@_))
!ll&&!rl&&r
8548
MC/DC100%2/2
defined $self->{"ExtraFuncEval"} and defined($res = $self->{"ExtraFuncEval"}->(@_))
defined $self->{"ExtraFuncEval"} defined($res = $self->{"ExtraFuncEval"}->(@_))
Truth table100%3/3
A and B
A= defined $self->{"ExtraFuncEval"}B= defined($res = $self->{"ExtraFuncEval"}->(@_))
ABresult
0X0
100
111
896  
897 62 my $last = $arglist[$#arglist];
898  
899 62 ▶ return int($last) if($fname eq 'int');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "int" 6 56 62
900 56 ▶ return abs($last) if($fname eq 'abs');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "abs" 6 50 56
901  
902 # Round in a +ve direction unless RoundNegatives when round away from zero:
903 50 ▶ return int($last + 0.5 * ($self->{RoundNegatives} ? $last <=> 0 : 1)) if($fname eq 'round');
Branch: 2 branches
BranchTrueFalseTotal
if $fname eq "round" 11 39 50
$self->{'RoundNegatives'} ? : 4 7 11
904  
905 39 ▶ return split $arglist[0], $arglist[$#arglist] if($fname eq 'split');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "split" 4 35 39
906 35 ▶ return join $arglist[0], @arglist[1 ... $#arglist] if($fname eq 'join');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "join" 3 32 35
907  
908 # Beware: could exceed max length with: printf("%2000s", "foo");
909 32 ▶ if($fname eq 'printf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "printf") 1 31 32
910 1 ▶ unless($self->{EnablePrintf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"EnablePrintf"}) 0 1 1
911 0 $self->PrintError("Function 'printf' not enabled");
912 0 return "";
913 }
914 1 my $s = sprintf $arglist[0], @arglist[1 ... $#arglist];
915 1 ▶ return $s if(length($s) <= $self->{StringMaxLength});
Branch: 1 branch
BranchTrueFalseTotal
if length $s <= $self->{"StringMaxLength"} 1 0 1
916 0 $self->PrintError("String would exceed maximum allowed %d", $self->{StringMaxLength});
917 0 return "";
918 }
919  
920 31 ▶ return mktime(@arglist) if($fname eq 'mktime');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "mktime" 1 30 31
921 30 ▶ return strftime($arglist[0], @arglist[1 ... $#arglist]) if($fname eq 'strftime');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "strftime" 2 28 30
922 28 ▶ return localtime($last) if($fname eq 'localtime');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "localtime" 2 26 28
923  
924 26 ▶ return $self->{VarIsDefFun}($self, $last) if($fname eq 'defined');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "defined" 2 24 26
925  
926 24 ▶ if($fname eq 'pop' or $fname eq 'shift') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "pop" or $fname eq "shift") 7 17 24
Condition100%3/3
$fname eq "pop" or $fname eq "shift"
l!l&&r!l&&!r
6117
MC/DC100%2/2
$fname eq "pop" or $fname eq "shift"
$fname eq "pop" $fname eq "shift"
Truth table100%3/3
A or B
A= $fname eq "pop"B= $fname eq "shift"
ABresult
1X1
011
000
927 7 my @a = $self->{VarGetFun}($self, $arglist[0]);
928 7 ▶ my $p = $fname eq 'pop' ? pop(@a) : shift(@a);
Branch: 1 branch
BranchTrueFalseTotal
$fname eq 'pop' ? : 6 1 7
929 7 $self->{VarSetFun}($self, $last, @a);
930  
931 7 return $p;
932 }
933  
934 17 ▶ if($fname eq 'push' or $fname eq 'unshift') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "push" or $fname eq "unshift") 5 12 17
Condition100%3/3
$fname eq "push" or $fname eq "unshift"
l!l&&r!l&&!r
2312
MC/DC100%2/2
$fname eq "push" or $fname eq "unshift"
$fname eq "push" $fname eq "unshift"
Truth table100%3/3
A or B
A= $fname eq "push"B= $fname eq "unshift"
ABresult
1X1
011
000
935 # Evaluate right->right and push/unshift that
936 5 my $vn = shift @arglist; # var name
937  
938 5 my @vv = $self->{VarGetFun}($self, $vn);# var value
939 5 my @vp = $self->EvalTree($tree->{right}->{right}, 0); # var to push/unshift
940  
941 5 ▶ $fname eq 'push' ? push(@vv, @vp) : unshift(@vv, @vp);
Branch: 1 branch
BranchTrueFalseTotal
$fname eq 'push' ? : 2 3 5
942 5 $self->{VarSetFun}($self, $vn, @vv);
943  
944 5 return scalar @vv;
945 }
946  
947 12 ▶ return length($last) if($fname eq 'strlen');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "strlen" 1 11 12
948 11 ▶ return scalar @arglist if($fname eq 'count');
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "count" 7 4 11
949  
950 # aindex(array, val) returns index (from 0) of val in array, -1 on error
951 4 ▶ if($fname eq 'aindex') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "aindex") 4 0 4
952 4 my $val = $arglist[$#arglist];
953 4 ▶ for( my $inx = 0; $inx <= $#arglist - 1; $inx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx <= $#arglist - 1) 25 1 26
954 25 ▶ return $inx if($val eq $arglist[$inx]);
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $arglist[$inx] 3 22 25
955 }
956 1 return -1;
957 }
958  
959 0 $self->PrintError("Unknown Function '$fname'");
960  
961 0 return '';
962 }
963  
964 # Create a new parse/evalutation object.
965 # Initialise default options.
966 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
967 2 my $class = shift;
968  
969 # What we store about this evaluation environment, default values:
970 2 my %ExprVars = (
971 PrintErrFunc => '', # Printf errors
972 VarHash => {( # Variable hash
973 EmptyArray => [()],
974 EmptyList => [()],
975 )},
976 VarGetFun => \&VarGetFun, # Get a variable - function
977 VarIsDefFun => \&VarIsDefFun, # Is a variable defined - function
978 VarSetFun => \&VarSetFun, # Set an array variable - function
979 VarSetScalar => \&VarSetScalar, # Set a scalar variable - function
980 FuncEval => \&FuncValue, # Evaluate - function
981 AutoInit => 0, # If true auto initialise variables
982 ExtraFuncEval => undef, # User supplied extra function evaluator function
983 RoundNegatives => 0, # Round behaves differently with -ve numbers
984 PermitLoops => 0, # Are loops allowed
985 MaxLoopCount => 50, # Max # all loops
986 ArrayMaxIndex => 100, # Max index of an array
987 StringMaxLength => 1000, # Max string length
988 EnablePrintf => 0, # Enable printf function
989 Functions => {%InFuns}, # Known functions, initialise to builtins
990  
991 );
992  
993 2 my $self = bless \%ExprVars => $class;
994 2 $self->SetOpt(@_); # Process new options
995  
996 2 return $self;
997 }
998  
999 # Set an option in the %template.
1000 5 ▶sub SetOpt {
Subroutine SetOpt: called CC=4 SCAR=14.0
Pod documented
1001 5 my $self = shift @_;
1002  
1003 5 ▶ while($#_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) 9 5 14
1004 9 ▶ $self->PrintError("Unknown option '$_[0]'") unless(exists($self->{$_[0]}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self{$_[0]} 0 9 9
1005 9 ▶ $self->PrintError("No value to option '$_[0]'") unless(defined($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 9 9
1006 9 $self->{$_[0]} = $_[1];
1007 9 shift;shift;
1008 }
1009 }
1010  
1011 2 1;
1012  
1013 __END__
↑ Summary