blib/lib/Language/Farnsworth/Value/Boolean.pm

filter: Statementstmt 40.4% 36 / 89 Branchbran 6.4% 4 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.8% 11 / 19 Podpod 0.0% 0 / 11 totaltotal 28.1% 51 / 181 CC 32 SCAR 31.5
CC
32
Cov
26%
compare
42.0
add
30.0
subtract
30.0
CRAP 23.4
« blib/lib/Language/Farnsworth/Output.pm ↑ Summary blib/lib/Language/Farnsworth/Units/Currency.pm »
1 package Language::Farnsworth::Value::Boolean;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Language::Farnsworth::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base 'Language::Farnsworth::Value';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = 0.6;
13  
14 use overload
15 4 ▶ '+' => \&add,
Subroutine BEGIN: called CC=1 SCAR=0.0
16 '-' => \&subtract,
17 '*' => \&mult,
18 '/' => \&div,
19 '%' => \&mod,
20 '**' => \&pow,
21 '<=>' => \&compare,
22 4 'bool' => \&bool;
23  
24 4 ▶use base qw(Language::Farnsworth::Value);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #this is the REQUIRED fields for Language::Farnsworth::Value subclasses
27 #
28 #dimen => a Language::Farnsworth::Dimension object
29 #
30 #this is so i can make a -> conforms in Language::Farnsworth::Value, to replace the existing code, i'm also planning on adding some definitions such as, TYPE_PARI, TYPE_STRING, TYPE_LAMBDA, TYPE_DATE, etc. to make certain things easier
31  
32 sub new
33 57 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
34 57 my $class = shift;
35 57 my $value = shift;
36 57 my $outmagic = shift; #i'm still not sure on this one
37  
38 57 my $self = {};
39  
40 57 bless $self, $class;
41  
42 57 $self->{outmagic} = $outmagic;
43  
44 57 ▶ $self->{truthiness} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 32 25 57
45
46 57 return $self;
47 }
48  
49 sub gettruth
50 52 ▶{
Subroutine gettruth: called CC=1 SCAR=0.0
Pod undocumented
51 52 return $_[0]->{truthiness};
52 }
53  
54 sub type
55 0 ▶{
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return "Boolean";
57 }
58  
59 #######
60 #The rest of this code can be GREATLY cleaned up by assuming that $one is of type, Language::Farnsworth::Value::Pari, this means that i can slowly redo a lot of this code
61  
62 sub add
63 0 ▶{
Subroutine add: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my ($one, $two, $rev) = @_;
65  
66 0 ▶ error "Non reference given to addition of Boolean" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
67  
68 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
69 0 ▶ error "Scalar value given to addition of boolean" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
70 0 ▶ return $two->add($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
71 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
72 {
73 0 error "Given non boolean to boolean operation";
74 }
75  
76  
77 #NOTE TO SELF this needs to be more helpful, i'll probably do this by creating an "error" class that'll be captured in ->evalbranch's recursion and use that to add information from the parse tree about WHERE the error occured
78 0 error "Adding booleans is not a good idea\n";
79 }
80  
81 sub subtract
82 0 ▶{
Subroutine subtract: not called CC=5 SCAR=34.0
Pod undocumented
83 0 my ($one, $two, $rev) = @_;
84  
85 0 ▶ error "Non reference given to subtraction of Boolean" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
86  
87 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
88 0 ▶ error "Scalar value given to subtraction to Booleans" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
89 0 ▶ return $two->subtract($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
90 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
91 {
92 0 error "Given non boolean to boolean operation";
93 }
94  
95 0 error "Subtracting Booleans? what did you think this would do, create a black hole?";
96 }
97  
98 sub modulus
99 0 ▶{
Subroutine modulus: not called CC=5 SCAR=34.0
Pod undocumented
100 0 my ($one, $two, $rev) = @_;
101  
102 0 ▶ error "Non reference given to modulus" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
103  
104 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
105 0 ▶ error "Scalar value given to modulus to boolean" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
106 0 ▶ return $two->mod($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
107 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
108 {
109 0 error "Given non boolean to boolean operation";
110 }
111  
112 0 error "Modulusing booleans? what did you think this would do, create a black hole?";
113 }
114  
115 sub mult
116 0 ▶{
Subroutine mult: not called CC=5 SCAR=34.0
Pod undocumented
117 0 my ($one, $two, $rev) = @_;
118  
119 0 ▶ error "Non reference given to multiplication of boolean" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
120  
121 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
122 0 ▶ error "Scalar value given to multiplcation to boolean" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
123 0 ▶ return $two->mult($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
124 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
125 {
126 0 error "Given non boolean to boolean operation";
127 }
128  
129 0 error "Multiplying arrays? what did you think this would do, create a black hole?";
130 }
131  
132 sub div
133 0 ▶{
Subroutine div: not called CC=5 SCAR=34.0
Pod undocumented
134 0 my ($one, $two, $rev) = @_;
135  
136 0 ▶ error "Non reference given to division of booleans" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
137  
138 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
139 0 ▶ error "Scalar value given to division of booleans" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
140 0 ▶ return $two->div($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
141 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
142 {
143 0 error "Given non boolean to boolean operation";
144 }
145  
146 0 error "Dividing booleans? what did you think this would do, create a black hole?";
147 }
148  
149 sub bool
150 52 ▶{
Subroutine bool: called CC=2 SCAR=6.9
Pod undocumented
151 52 my $self = shift;
152  
153 #seems good enough of an idea to me
154 #i have a bug HERE
155 #print "BOOLCONV\n";
156 #print Dumper($self);
157 #print "ENDBOOLCONV\n";
158 52 ▶ return $self->gettruth()?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->gettruth ? : 31 21 52
159 }
160  
161 sub pow
162 0 ▶{
Subroutine pow: not called CC=5 SCAR=34.0
Pod undocumented
163 0 my ($one, $two, $rev) = @_;
164  
165 0 ▶ error "Non reference given to exponentiation of booleans" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
166  
167 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
168 0 ▶ error "Exponentiating booleans? what did you think this would do, create a black hole?" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
169 0 ▶ return $two->pow($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
170 0 ▶ if (!$two->istype("Boolean"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Boolean")) 0 0 0
171 {
172 0 error "Given non boolean to boolean operation";
173 }
174  
175  
176 0 error "Exponentiating arrays? what did you think this would do, create a black hole?";
177 }
178  
179 sub compare
180 0 ▶{
Subroutine compare: not called CC=6 SCAR=37.4
Pod undocumented
181 0 my ($one, $two, $rev) = @_;
182  
183 0 ▶ error "Non reference given to compare of boolean" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
184  
185 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
186 0 ▶ error "Scalar value given to division of boolean" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
187 0 ▶ return $two->compare($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
188  
189 0 ▶ my $rv = $rev ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 0 0
190 #check for $two being a simple value
191 0 my $tv = $two->gettruth();
192 0 my $ov = $one->gettruth();
193  
194 #i also need to check the units, but that will come later
195 #NOTE TO SELF this needs to be more helpful, i'll probably do something by adding stuff in ->new to be able to fetch more about the processing
196 0 ▶ error "Unable to process different types in compare of booleans\n" unless $one->conforms($two); #always call this on one, since $two COULD be some other object
Branch: 1 branch
BranchTrueFalseTotal
unless $one->conforms($two) 0 0 0
197  
198 #moving this down so that i don't do any math i don't have to
199 0 my $new = $tv <=> $ov;
200
201 0 return $new * $rv;
202 }
203  
« blib/lib/Language/Farnsworth/Output.pm ↑ Summary blib/lib/Language/Farnsworth/Units/Currency.pm »