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

filter: Statementstmt 90.0% 135 / 150 Branchbran 57.1% 48 / 84 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 18 totaltotal 72.8% 212 / 291 CC 46 SCAR 18.2
CC
46
Cov
77%
mod
30.0
compare
8.5
div
7.1
CRAP 6.2
« blib/lib/Language/Farnsworth.pm ↑ Summary blib/lib/Language/Farnsworth/Value/Types.pm »
1 package Language::Farnsworth::Value::Pari;
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 4 ▶no warnings 'redefine'; #wtf are these coming from?
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Math::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Language::Farnsworth::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use base 'Language::Farnsworth::Value';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = 0.5;
16  
17 use overload
18 4 ▶ '+' => \&add,
Subroutine BEGIN: called CC=1 SCAR=0.0
19 '-' => \&subtract,
20 '*' => \&mult,
21 '/' => \&div,
22 '%' => \&mod,
23 '**' => \&pow,
24 '<=>' => \&compare,
25 'bool' => \&bool,
26 4 '"' => \&toperl;
27  
28 4 ▶use base qw(Language::Farnsworth::Value);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 {
31 4 my $plain;
32 978 ▶ sub TYPE_PLAIN {return $plain if $plain; $plain=new Language::Farnsworth::Value::Pari(0)}
Subroutine TYPE_PLAIN: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $plain 0 978 978
33 }
34  
35 #this is the REQUIRED fields for Language::Farnsworth::Value subclasses
36 #
37 #dimen => a Language::Farnsworth::Dimension object
38 #
39 #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
40  
41 sub new
42 4 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
43 22413 my $class = shift;
44 22413 my $value = shift;
45 22413 my $dimen = shift; #should only really be used internally?
46 22413 my $outmagic = shift; #i'm still not sure on this one
47 22413 my $hex = shift;
48 22413 my $valueone = shift; #sentinal value
49  
50 22413 my $self = {};
51  
52 22413 bless $self, $class;
53  
54 22413 $self->{outmagic} = $outmagic;
55 22413 $self->{valueone} = $valueone; #sentinal value for increment and postcrement, ignores dimens
56  
57 22413 ▶ if (ref($dimen) eq "Language::Farnsworth::Dimension")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dimen eq 'Language::Farnsworth::Dimension') { } 12372 10041 22413
58 {
59 12372 $self->{dimen} = $dimen;
60 }
61 else
62 {
63 10041 ▶ $dimen = {} if !defined($dimen);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimen 10003 38 10041
64 10041 $self->{dimen} = new Language::Farnsworth::Dimension($dimen);
65 }
66  
67 22413 $value =~ s/(ee|E)/e/i; #fixes double ee's, i could probably eventually remove this, but it doesn't do any harm for now
68
69 22413 ▶ if ($hex)
Branch: 1 branch
BranchTrueFalseTotal
if ($hex) { } 3 22410 22413
70 {
71 3 $self->{pari} = parsehex($value);
72 }
73 else
74 {
75 22410 $self->{pari} = PARI $value;
76 }
77  
78 22413 return $self;
79 }
80  
81 sub isvalueone
82 1171 ▶{
Subroutine isvalueone: called CC=1 SCAR=0.0
Pod undocumented
83 1171 return $_[0]->{valueone};
84 }
85  
86 sub type
87 0 ▶{
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self = shift;
89 0 my $scope = shift;
90 0 return $self->{dimen}->Dump($scope); #TODO: This should instead use the output code once it moves here.
91 }
92  
93 #helpers for parsing hex, binary, and octal formats, could also extend support to others
94 sub parsehex
95 3 ▶{
Subroutine parsehex: called CC=4 SCAR=14.0
Pod undocumented
96 3 my $input = shift;
97 3 ▶ if ($input =~ /0x(.*)/i)
Branch: 3 branches
BranchTrueFalseTotal
if ($input =~ /0x(.*)/i) { } 1 2 3
elsif ($input =~ /0b(.*)/i) { } 1 1 2
elsif ($input =~ /0(.*)/i) { } 1 0 1
98 {
99 1 return parsedigits(16, $1, $input);
100 }
101 elsif ($input =~ /0b(.*)/i)
102 {
103 1 return parsedigits(2, $1, $input);
104 }
105 elsif ($input =~ /0(.*)/i)
106 {
107 1 return parsedigits(8, $1, $input);
108 }
109 else
110 {
111 0 error("How did you manage to get here? \$input=$input at Pari.pm");
112 }
113 }
114  
115 sub parsedigits
116 3 ▶{
Subroutine parsedigits: called CC=6 SCAR=18.8
Pod undocumented
117 3 my ($base, $digits, $input) = @_;
118 3 my $value = PARI '0';
119 3 my $div = $value + 1.0;
120 3 my $flag = 0;
121  
122 3 for my $d (split //, $digits)
123 {
124 3 ▶ $flag=1,next if ($d eq '.' && !$flag); #only set $flag once, that way it'll trigger invalid digit properly afterwords
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "." and not $flag 0 3 3
Condition33%1/3
$d eq "." and not $flag
!ll&&!rl&&r
300
MC/DC0%0/2
$d eq "." and not $flag
$d eq "." not $flag
Truth table33%1/3
A and not(B)
A= $d eq "."B= not $flag
ABresult
0X0
100
111
125 3 my $v = checkdigit($d, $base, $input);
126 3 $value = $value * $base + $v; #build it up from left to right we need to multiply by $base each iterartion, starting at 0 this causes no issues at all
127 3 ▶ $div *= $base if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 0 3 3
128 }
129  
130 3 ▶ return $value/$div if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 0 3 3
131 3 return $value;
132 }
133  
134 sub checkdigit
135 3 ▶{
Subroutine checkdigit: called CC=3 SCAR=12.0
Pod undocumented
136 3 my ($digit, $base, $input) = @_;
137 3 my $valid = "0123456789ABCDEF";
138 3 my $v = index($valid, uc $digit);
139  
140 3 print("Invalid digit '$digit' in number '$input' for base $base");
141  
142 3 ▶ if ($v == -1 || $v >= $base)
Branch: 1 branch
BranchTrueFalseTotal
if ($v == -1 or $v >= $base) 0 3 3
Condition33%1/3
$v == -1 or $v >= $base
l!l&&r!l&&!r
003
MC/DC0%0/2
$v == -1 or $v >= $base
$v == -1 $v >= $base
Truth table33%1/3
A or B
A= $v == -1B= $v >= $base
ABresult
1X1
011
000
143 {
144 0 error("Invalid digit '$digit' in number '$input' for base $base");
145 }
146  
147 3 return $v;
148 }
149  
150 ####
151 #THESE FUNCTIONS WILL BE MOVED TO Language::Farnsworth::Value, or somewhere more appropriate
152  
153 sub getdimen
154 61447 ▶{
Subroutine getdimen: called CC=1 SCAR=0.0
Pod undocumented
155 61447 my $self = shift;
156 61447 return $self->{dimen};
157 }
158  
159 #######
160 #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
161  
162 sub getpari
163 27136 ▶{
Subroutine getpari: called CC=1 SCAR=0.0
Pod undocumented
164 27136 my $self = shift;
165 27136 return $self->{pari};
166 }
167  
168 sub toperl
169 58 ▶{
Subroutine toperl: called CC=1 SCAR=0.0
Pod undocumented
170 58 my $self = shift;
171 58 return $self->getpari()."";
172 }
173  
174 sub add
175 50 ▶{
Subroutine add: called CC=4 SCAR=14.1
Pod undocumented
176 50 my ($one, $two, $rev) = @_;
177  
178 50 ▶ error "Non reference given to addition of scalar" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 50 50
179  
180 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
181 50 ▶ return $two->add($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 50 50
182  
183 #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
184 50 ▶ error "Unable to process different units in addition\n" unless ($one->conforms($two));
Branch: 1 branch
BranchTrueFalseTotal
unless $one->conforms($two) 1 49 50
185  
186 #moving this down so that i don't do any math i don't have to
187  
188 #ONLY THIS MODULE SHOULD EVER TOUCH ->{pari} ANYMORE! this might change into, NEVER
189 49 return new Language::Farnsworth::Value::Pari($one->getpari() + $two->getpari(), $one->getdimen());
190 }
191  
192 sub subtract
193 23 ▶{
Subroutine subtract: called CC=5 SCAR=17.8
Pod undocumented
194 23 my ($one, $two, $rev) = @_;
195  
196 23 ▶ error "Non reference given to subtraction of scalar" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 23 23
197  
198 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
199 23 ▶ return $two->subtract($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 23 23
200  
201 #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
202 23 ▶ error "Unable to process different units in subtraction\n" unless ($one->conforms($two));
Branch: 1 branch
BranchTrueFalseTotal
unless $one->conforms($two) 0 23 23
203  
204 #moving this down so that i don't do any math i don't have to
205 23 ▶ if (!$rev)
Branch: 1 branch
BranchTrueFalseTotal
if (not $rev) { } 23 0 23
206 {
207 23 return new Language::Farnsworth::Value::Pari($one->getpari() - $two->getpari(), $one->getdimen()); #if !$rev they are in order
208 }
209 else
210 {
211 #i've never seen this happen, we'll see if it works
212 0 error "some mistake happened here in subtraction\n"; #to test later on
213 }
214 }
215  
216 sub mod
217 0 ▶{
Subroutine mod: not called CC=5 SCAR=34.0
Pod undocumented
218 0 my ($one, $two, $rev) = @_;
219  
220 0 ▶ error "Non reference given to modulus" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
221  
222 #as odd as this seems, we need it in order to allow overloading later on
223 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
224 0 ▶ return $two->mod($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 0 0
225  
226 #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
227 0 ▶ error "Unable to process different units in modulus\n" unless ($one->conforms($two));
Branch: 1 branch
BranchTrueFalseTotal
unless $one->conforms($two) 0 0 0
228  
229 #moving this down so that i don't do any math i don't have to
230 0 ▶ if (!$rev)
Branch: 1 branch
BranchTrueFalseTotal
if (not $rev) { } 0 0 0
231 {
232 0 return new Language::Farnsworth::Value::Pari($one->getpari() % $two->getpari(), $one->getdimen()); #if !$rev they are in order
233 }
234 else
235 {
236 0 return new Language::Farnsworth::Value::Pari($two->getpari() % $one->getpari(), $one->getdimen()); #if !$rev they are in order
237 }
238 }
239  
240 sub mult
241 8524 ▶{
Subroutine mult: called CC=4 SCAR=14.5
Pod undocumented
242 8524 my ($one, $two, $rev) = @_;
243  
244 8524 ▶ error "ARRAY REF WTF?" if (ref($two) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $two eq "ARRAY" 0 8524 8524
245 8524 ▶ error "Non reference given to multiplication of scalar" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 8524 8524
246  
247 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
248 8524 ▶ return $two->mult($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 8524 8524
249  
250 8524 my $nd = $one->getdimen()->merge($two->getdimen()); #merge the dimensions! don't cross the streams though
251  
252 #moving this down so that i don't do any math i don't have to
253 8524 return new Language::Farnsworth::Value::Pari($one->getpari() * $two->getpari(), $nd);
254 }
255  
256 sub div
257 2795 ▶{
Subroutine div: called CC=6 SCAR=19.6
Pod undocumented
258 2795 my ($one, $two, $rev) = @_;
259  
260 2795 ▶ error "Non reference given to division of scalar" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 2795 2795
261  
262 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
263 2795 ▶ return $two->div($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 2795 2795
264  
265 #these are a little screwy SO i'll probably comment them more later
266 #probably after i find out that they're wrong
267 2795 ▶ my $qd = $rev ? $two->getdimen() : $one->getdimen();
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 2795 2795
268 2795 ▶ my $dd = $rev ? $one->getdimen()->invert() : $two->getdimen()->invert();
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 2795 2795
269  
270 2795 my $nd = $qd->merge($dd);
271
272 2795 ▶ if (!$rev)
Branch: 1 branch
BranchTrueFalseTotal
if (not $rev) { } 2795 0 2795
273 {
274 2795 return new Language::Farnsworth::Value::Pari($one->getpari() / $two->getpari(), $nd); #if !$rev they are in order
275 }
276 else
277 {
278 0 return new Language::Farnsworth::Value::Pari($two->getpari() / $one->getpari(), $nd); #if !$rev they are in order
279 }
280 }
281  
282 sub bool
283 1002 ▶{
Subroutine bool: called CC=2 SCAR=6.9
Pod undocumented
284 1002 my $self = shift;
285  
286 #seems good enough of an idea to me
287 #i have a bug HERE
288 # print "PARI BOOLCONV\n";
289 #print Dumper($self);
290 #print "ENDBOOLCONV\n";
291 1002 ▶ return $self->getpari()?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->getpari ? : 11 991 1002
292 }
293  
294 sub pow
295 978 ▶{
Subroutine pow: called CC=5 SCAR=16.6
Pod undocumented
296 978 my ($one, $two, $rev) = @_;
297  
298 978 ▶ error "Non reference given to exponentiation of scalar" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 978 978
299  
300 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
301 978 ▶ return $two->pow($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 978 978
302  
303 978 ▶ if (!$two->conforms($one->TYPE_PLAIN))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->conforms($one->TYPE_PLAIN)) 1 977 978
304 {
305 1 error "A number with units as the exponent doesn't make sense";
306 }
307  
308 #moving this down so that i don't do any math i don't have to
309 977 my $new;
310 977 ▶ if (!$rev)
Branch: 1 branch
BranchTrueFalseTotal
if (not $rev) { } 977 0 977
311 {
312 977 $new = new Language::Farnsworth::Value::Pari($one->getpari() ** $two->getpari(), $one->getdimen()->mult($two->getpari())); #if !$rev they are in order
313 }
314 else
315 {
316 0 error "Wrong order in ->pow()";
317 }
318  
319 977 return $new;
320 }
321  
322 sub compare
323 93 ▶{
Subroutine compare: called CC=8 SCAR=21.3
Pod undocumented
324 93 my ($one, $two, $rev) = @_;
325
326 93 ▶ error "Non reference given to exponentiation" unless (ref($two));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 93 93
327  
328 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
329 93 ▶ return $two->compare($one, !$rev) unless ($two->isa(__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->isa(__PACKAGE__) 0 93 93
330  
331 93 ▶ my $rv = $rev ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 0 93 93
332 #check for $two being a simple value
333 93 my $tv = $two->getpari();
334 93 my $ov = $one->getpari();
335  
336 #i also need to check the units, but that will come later
337 #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
338 93 ▶ error "Unable to process different units in compare\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 93 93
339  
340 #moving this down so that i don't do any math i don't have to
341 93 my $new;
342
343 93 ▶ if ($ov == $tv)
Branch: 3 branches
BranchTrueFalseTotal
if ($ov == $tv) { } 18 75 93
elsif ($ov < $tv) { } 39 36 75
elsif ($ov > $tv) { } 36 0 36
344 {
345 18 return 0;
346 }
347 elsif ($ov < $tv)
348 {
349 39 return -1;
350 }
351 elsif ($ov > $tv)
352 {
353 36 return 1;
354 }
355 }
356  
357 4 1;
« blib/lib/Language/Farnsworth.pm ↑ Summary blib/lib/Language/Farnsworth/Value/Types.pm »