Ternary/Expression.pm

filter: Statementstmt 100% 91 / 91 Branchbran 100% 48 / 48 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 100% 181 / 181 CC 29 SCAR 13.8
CC
29
Cov
100%
and3
6.0
not3
5.0
xor3
5.0
CRAP 4.0
« Ternary/Calculator.pm ↑ Summary Ternary/Trit.pm »
1 # Copyright (c) 2012-2017 Martin Becker, Blaubeuren. All rights reserved.
2 # This package is free software; you can redistribute it and/or modify it
3 # under the same terms as Perl itself.
4  
5 package Math::Logic::Ternary::Expression;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK = qw(not3 and3 or3 xor3 eqv3 bool3 val3);
14 1 our %EXPORT_TAGS = ('all' => \@EXPORT_OK);
15 1 our $VERSION = '0.004';
16  
17 # ----- object definition -----
18  
19 # Math::Logic::Ternary::Expression=ARRAY(...)
20  
21 # M::L::T::Expression ARRAY
22 # +----------+---------+ +----------+----------+
23 # | VALUEREF | o----------->| VALUE | anything |
24 # +----------+---------+ +----------+----------+
25 # | NEGATED | boolean | | DEFERRED | boolean |
26 # +----------+---------+ +----------+----------+
27  
28 # .......... index .......... # .............. value ..............
29 1 ▶use constant VALUEREF => 0; # ref of lazily accessible value
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant NEGATED => 1; # boolean
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # lazily accessible value: ARRAY(...)
33  
34 # .......... index .......... # .............. value ..............
35 1 ▶use constant VALUE => 0; # coderef while deferred, then value
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant DEFERRED => 1; # boolean
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 my $undef = bless [[undef, ''], ''];
39  
40 # turn any value into an MLTE object
41 70 ▶sub _object {
Subroutine _object: called CC=3 SCAR=11.0
42 70 my ($this) = @_;
43 70 ▶ return $undef if !defined $this;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 1 69 70
44 69 ▶ return $this if __PACKAGE__ eq ref $this;
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__ eq ref $this 63 6 69
45 6 return bless [[$this, 'CODE' eq ref $this], ''];
46 }
47  
48 # get the actual value from an MLTE object
49 151 ▶sub _evaluate {
Subroutine _evaluate: called CC=4 SCAR=13.9
50 151 my ($this) = @_;
51 151 my $vr = $this->[VALUEREF];
52 151 my $v = $vr->[VALUE];
53 151 my $n = $this->[NEGATED];
54 151 ▶ if ($vr->[DEFERRED]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vr->[1]) 10 141 151
55 10 $vr->[DEFERRED] = '';
56 10 $v = $vr->[VALUE] = $v->();
57 10 ▶ if (!defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 3 7 10
58 3 $n = $this->[NEGATED] = '';
59 }
60 }
61 # die "assertion failed" unless defined($v) || !$n;
62 151 ▶ return $n? !$v: $v;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 52 99 151
63 }
64  
65 # get the actual value from anything
66 97 ▶sub val3 {
Subroutine val3: called CC=3 SCAR=11.0
Pod documented
67 97 my ($this) = @_;
68 97 ▶ return $this if !defined($this) || __PACKAGE__ ne ref $this;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $this or __PACKAGE__ ne ref $this 4 93 97
Condition100%3/3
not defined $this or __PACKAGE__ ne ref $this
l!l&&r!l&&!r
1393
MC/DC100%2/2
not defined $this or __PACKAGE__ ne ref $this
not defined $this __PACKAGE__ ne ref $this
Truth table100%3/3
not(A) or B
A= not defined $thisB= __PACKAGE__ ne ref $this
ABresult
1X1
011
000
69 93 return _evaluate($this);
70 }
71  
72 # replace actual value by a truth value
73 10 ▶sub bool3 {
Subroutine bool3: called CC=2 SCAR=6.9
Pod documented
74 10 my ($this) = @_;
75 10 my $val = _evaluate(_object($this));
76 10 ▶ return $undef if !defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 7 10
77 7 return bless [[!!$val, ''], ''];
78 }
79  
80 # negate without evaluating
81 53 ▶sub not3 {
Subroutine not3: called CC=5 SCAR=16.1
Pod documented
82 53 my ($this) = @_;
83 53 ▶ return $undef if !defined $this;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 2 51 53
84 51 ▶ if (__PACKAGE__ eq ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__ eq ref $this) 41 10 51
85 41 my $vr = $this->[VALUEREF];
86 41 ▶ return $undef if !($vr->[DEFERRED] || defined $vr->[VALUE]);
Branch: 1 branch
BranchTrueFalseTotal
unless $vr->[1] or defined $vr->[0] 1 40 41
Condition100%3/3
$vr->[1] or defined $vr->[0]
l!l&&r!l&&!r
1391
MC/DC100%2/2
$vr->[1] or defined $vr->[0]
$vr->[1] defined $vr->[0]
Truth table100%3/3
A or B
A= $vr->[1]B= defined $vr->[0]
ABresult
1X1
011
000
87 40 return bless [$vr, !$this->[NEGATED]];
88 }
89 10 return bless [[$this, 'CODE' eq ref $this], 1];
90 }
91  
92 # a && b: tt => b, tf => b, ft => a, ff => a
93 9 ▶sub and3 {
Subroutine and3: called CC=6 SCAR=17.9
Pod documented
94 9 my ($this, $that) = @_;
95 9 my $obj = _object($this);
96 9 my $val = _evaluate($obj);
97 9 ▶ return $obj if defined($val) && !$val; # false, * => LHS
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and not $val 3 6 9
Condition100%3/3
defined $val and not $val
!ll&&!rl&&r
333
MC/DC100%2/2
defined $val and not $val
defined $val not $val
Truth table100%3/3
A and not(B)
A= defined $valB= not $val
ABresult
0X0
100
111
98 6 $obj = _object($that);
99 6 ▶ return $obj if $val; # true, * => RHS
Branch: 1 branch
BranchTrueFalseTotal
if $val 3 3 6
100 3 $val = _evaluate($obj);
101 3 ▶ return $obj if defined($val) && !$val; # undef, false => RHS
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and not $val 1 2 3
Condition100%3/3
defined $val and not $val
!ll&&!rl&&r
111
MC/DC100%2/2
defined $val and not $val
defined $val not $val
Truth table100%3/3
A and not(B)
A= defined $valB= not $val
ABresult
0X0
100
111
102 2 return $undef; # else => undef
103 }
104  
105 # a || b: tt => a, tf => a, ft => b, ff => b
106 9 ▶sub or3 {
Subroutine or3: called CC=4 SCAR=13.9
Pod documented
107 9 my ($this, $that) = @_;
108 9 my $obj = _object($this);
109 9 my $val = _evaluate($obj);
110 9 ▶ return $obj if $val; # true, * => LHS
Branch: 1 branch
BranchTrueFalseTotal
if $val 3 6 9
111 6 $obj = _object($that);
112 6 ▶ return $obj if defined $val; # false, * => RHS
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 3 3 6
113 3 $val = _evaluate($obj);
114 3 ▶ return $obj if $val; # undef, true => RHS
Branch: 1 branch
BranchTrueFalseTotal
if $val 1 2 3
115 2 return $undef; # else => undef
116 }
117  
118 # a? !b: b tt => !b, tf => !b, ft => b, ff => b
119 # !a && b || !b && a tt => !b, tf => a, ft => b, ff => a
120 # !b && a || !a && b tt => !a, tf => a, ft => b, ff => b
121 # !(a && b) && (a || b) tt => !b, tf => a, ft => b, ff => b (implemented)
122  
123 9 ▶sub xor3 {
Subroutine xor3: called CC=5 SCAR=16.1
Pod documented
124 9 my ($this, $that) = @_;
125 9 my $obj1 = _object($this);
126 9 my $val = _evaluate($obj1);
127 9 ▶ return $undef if !defined $val; # undef, * => undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 6 9
128 6 my $obj2 = _object($that);
129 6 ▶ return $obj2 if !$val; # false, * => RHS
Branch: 1 branch
BranchTrueFalseTotal
unless $val 3 3 6
130 3 $val = _evaluate($obj2);
131 3 ▶ return $undef if !defined $val; # true, undef => undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 2 3
132 2 ▶ return $obj1 if !$val; # true, false => LHS
Branch: 1 branch
BranchTrueFalseTotal
unless $val 1 1 2
133 1 return not3($obj2); # true, true => not RHS
134 }
135  
136 # !(a || b) || (a && b) tt => b, tf => b, ft => a, ff => !b
137  
138 9 ▶sub eqv3 {
Subroutine eqv3: called CC=5 SCAR=16.1
Pod documented
139 9 my ($this, $that) = @_;
140 9 my $obj1 = _object($this);
141 9 my $val = _evaluate($obj1);
142 9 ▶ return $undef if !defined $val; # undef, * => undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 6 9
143 6 my $obj2 = _object($that);
144 6 ▶ return $obj2 if $val; # true, * => RHS
Branch: 1 branch
BranchTrueFalseTotal
if $val 3 3 6
145 3 $val = _evaluate($obj2);
146 3 ▶ return $undef if !defined $val; # false, undef => undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 2 3
147 2 ▶ return $obj1 if $val; # false, true => LHS
Branch: 1 branch
BranchTrueFalseTotal
if $val 1 1 2
148 1 return not3($obj2); # false, false => not RHS
149 }
150  
151 1 1;
152 __END__
« Ternary/Calculator.pm ↑ Summary Ternary/Trit.pm »