blib/lib/Language/Farnsworth/FunctionDispatch.pm

filter: Statementstmt 83.2% 124 / 149 Branchbran 65.3% 34 / 52 Conditioncond 51.2% 20 / 39 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 11 totaltotal 69.6% 202 / 290 CC 43 SCAR 24.2
CC
43
Cov
74%
setupargs
23.0
checkparams
14.1
getref
12.0
CRAP 11.3
« blib/lib/Language/Farnsworth/Dimension.pm ↑ Summary blib/lib/Language/Farnsworth/Variables.pm »
1 package Language::Farnsworth::FunctionDispatch;
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Language::Farnsworth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Language::Farnsworth::Value::Lambda;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Language::Farnsworth::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new
14 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 4 my $self = {};
16 4 bless $self, (shift);
17 }
18  
19 sub addfunc
20 348 ▶{
Subroutine addfunc: called CC=3 SCAR=11.0
Pod undocumented
21 # debug 3, "ADDFUNC", Dumper(\@_);
22 348 my $self = shift;
23 348 my $name = shift;
24 348 my $args = shift;
25 348 my $value = shift;
26 348 my $scope = shift;
27
28 348 ▶ error "No scope given for function $name" unless defined($scope);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope 0 348 348
29
30 #generate a "false" lambda tree for this, this will go away
31 #bless [ @_[2,4] ], 'Lambda'
32 348 my $argbranch = bless [], 'Arglist';
33
34 348 for (@$args)
35 {
36 439 push @$argbranch, bless $_, 'Argele';
37 }
38
39 348 my $branch = bless [$argbranch, $value], 'Lambda';
40  
41 #i should really have some error checking here
42 #warn "Depreciated function definition encoutered";
43 # debug 3, "--------------------------", "FUNCTION: ".$name;
44 # debug 3, Dumper($branch);
45 # debug 3, Dumper($value);
46 # debug 3, Dumper($args);
47
48 348 my $lambda = new Language::Farnsworth::Value::Lambda($scope, $args, $value, $branch, $name);
49
50 348 $self->{funcs}{$name} = {name=>$name, lambda=>$lambda};
51 }
52  
53 sub addfunclamb
54 2 ▶{
Subroutine addfunclamb: called CC=1 SCAR=0.0
Pod undocumented
55 2 my $self = shift;
56 2 my $name = shift;
57 2 my $lambda = shift;
58
59 2 $lambda->setname($name);
60
61 2 $self->{funcs}{$name} = {name => $name, lambda => $lambda};
62 }
63  
64 sub getfunc
65 0 ▶{
Subroutine getfunc: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my $self = shift;
67 0 my $name = shift; #which one to get, we return the hashref
68 0 return $self->{funcs}{$name};
69 }
70  
71 sub isfunc
72 196 ▶{
Subroutine isfunc: called CC=1 SCAR=0.0
Pod undocumented
73 196 my $self = shift;
74 196 my $name = shift;
75  
76 196 return exists($self->{funcs}{$name});
77 }
78  
79 sub setupargs
80 166 ▶{
Subroutine setupargs: called CC=17 SCAR=31.4
Pod undocumented
81 166 my $self = shift;
82 166 my $eval = shift;
83 166 my $args = shift;
84 166 my $argtypes = shift;
85 166 my $name = shift; #name to display
86 #my $branch = shift;
87  
88 166 my $vars = $eval->{vars}; #get the scope we need
89  
90 166 ARG:for my $argc (0..$#$argtypes)
91 {
92 251 my $n = $argtypes->[$argc][0]; #the rest are defaults and constraints
93 251 my $v = $args->getarrayref()->[$argc];
94  
95 251 my $const = $argtypes->[$argc][2];
96  
97 251 ▶ if (ref($const) eq "VarArg")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $const eq "VarArg") 0 251 251
98 {
99 0 warn "Working around bug in lambdas!";
100 0 $const = "VarArg";
101 }
102  
103 251 ▶ if (!defined($v))# || ($v->{dimen}{dimen}{"undef"})) #uncomment for undef== default value
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 8 243 251
104 {
105 #i need a default value!
106 8 ▶ if (!defined($argtypes->[$argc][1]) && defined($argtypes->[$argc][0]) && (defined($const) && ref($const) !~ /Language::Farnsworth::Value/ && $const ne "VarArg"))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $argtypes->[$argc][1] and defined $argtypes->[$argc][0] and (defined $const and not ref($const) =~ /Language::Farnsworth::Value/ and $const ne "VarArg")) 0 8 8
Condition16%2/12
not defined $argtypes->[$argc][1] and defined $argtypes->[$argc][0] and (defined $const and not ref($const) =~ /Language::Farnsworth::Value/ and $const ne "VarArg")
!ll&&!rl&&r
800
not defined $argtypes->[$argc][1] and defined $argtypes->[$argc][0]
!ll&&!rl&&r
800
defined $const and not ref($const) =~ /Language::Farnsworth::Value/ and $const ne "VarArg"
!ll&&!rl&&r
000
defined $const and not ref($const) =~ /Language::Farnsworth::Value/
!ll&&!rl&&r
000
MC/DC0%0/5
not defined $argtypes->[$argc][1] and defined $argtypes->[$argc][0] and (defined $const and not ref($const) =~ /Language::Farnsworth::Value/ and $const ne "VarArg")
not defined $argtypes->[$argc][1] defined $argtypes->[$argc][0] defined $const not ref($const) =~ /Language::Farnsworth::Value/ $const ne "VarArg"
Truth table16%1/6
not(A) and B and C and not(D) and E
A= not defined $argtypes->[$argc][1]B= defined $argtypes->[$argc][0]C= defined $constD= not ref($const) =~ /Language::Farnsworth::Value/E= $const ne "VarArg"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
107 {
108 0 error "Required argument $argc to function $name\[\] missing\n";
109 }
110  
111 8 $v = $argtypes->[$argc][1];
112 }
113  
114 251 ▶ if (defined($const) && ref($const) =~ /Language::Farnsworth::Value/)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $const and ref($const) =~ /Language::Farnsworth::Value/) { } 61 190 251
elsif (defined $const and $const eq 'VarArg') { } 21 338 359
Condition100%3/3
defined $const and ref($const) =~ /Language::Farnsworth::Value/
!ll&&!rl&&r
1692161
MC/DC100%2/2
defined $const and ref($const) =~ /Language::Farnsworth::Value/
defined $const ref($const) =~ /Language::Farnsworth::Value/
Truth table100%3/3
A and B
A= defined $constB= ref($const) =~ /Language::Farnsworth::Value/
ABresult
0X0
100
111
115 {
116 #we have a constraint
117 61 ▶ if (!$v->conforms($const))
Branch: 1 branch
BranchTrueFalseTotal
unless ($v->conforms($const)) 3 58 61
118 {
119 3 error "Constraint not met on argument $argc to $name\[\]\n";
120 }
121 }
122 ▶ elsif (defined($const) && $const eq "VarArg")
Condition66%2/3
defined $const and $const eq "VarArg"
!ll&&!rl&&r
169021
MC/DC50%1/2
defined $const and $const eq "VarArg"
defined $const $const eq "VarArg"
Truth table66%2/3
A and B
A= defined $constB= $const eq "VarArg"
ABresult
0X0
100
111
123 {
124 #we've got a variable argument, it needs to slurp all the rest of the arguments into an array!
125 21 my $last = $#{$args->getarrayref()};
126 21 my @vargs = @{$args->getarrayref()}[$argc..$last];
127 21 my $v = new Language::Farnsworth::Value::Array(\@vargs);
128 21 $vars->declare($n, $v); #set the variable
129 21 last ARG; #don't parse ANY more arguments
130 }
131  
132 227 ▶ if (defined $n) #happens when no arguments! so we check if the name is defined
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 227 0 227
133 {
134 #print "SETVAR $n: ";
135 #print Dumper($argtypes->[$argc]);
136 #print Dumper($vars->{vars});
137 227 ▶ if (!$argtypes->[$argc][3]) #make sure that it shouldn't be byref
Branch: 1 branch
BranchTrueFalseTotal
if (not $argtypes->[$argc][3]) { } 203 24 227
138 {
139 203 $vars->declare($n, $v);
140 }
141 else
142 {
143 #it should be by ref
144 24 ▶ if ($v->getref())
Branch: 1 branch
BranchTrueFalseTotal
if ($v->getref) { } 24 0 24
145 {
146 24 $vars->setref($n, $v->getref());
147 }
148 else
149 {
150 0 error "Can't get reference from expression for argument $argc";
151 }
152 }
153  
154 #print Dumper($vars->{vars});
155 }
156 }
157 }
158  
159 sub callfunc
160 96 ▶{
Subroutine callfunc: called CC=4 SCAR=13.9
Pod undocumented
161 96 my $self = shift;
162 96 my $eval = shift;
163 96 my $name = shift;
164 96 my $args = shift;
165 96 my $branches = shift;
166  
167 96 ▶ error "Given object as function name, check should happen before this" if (ref($name));
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 96 96
168 96 ▶ error "Function $name is not defined" unless $self->isfunc($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isfunc($name) 0 96 96
169  
170 96 my $lambda = $self->{funcs}{$name}{lambda};
171  
172 # warn "-------------ATTEMPTING TO CALL FUNCTION!-------------\n";
173 # warn "FUNCTION NAME : $name\n";
174 # warn "Dumper of func: ".Dumper($lambda->{code});
175 # warn "$eval";
176 # warn "".$lambda->getscope();
177 # warn "--------------------THAT IS ALL\n";
178  
179 96 ▶ if ($name eq "eval")
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'eval') { } 2 94 96
180 {
181 2 return $lambda->eval($args, $eval);
182 }
183 else
184 {
185 94 return $lambda * $args;
186 }
187 }
188  
189 sub calllambda
190 166 ▶{
Subroutine calllambda: called CC=5 SCAR=16.6
Pod undocumented
191 166 my $self = shift;
192 166 my $lambda = shift;
193 166 my $args = shift;
194 166 my $eval = shift;
195  
196 166 ▶ $eval = $lambda->getscope() unless defined($eval);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eval 164 2 166
197  
198 166 my $argtypes = $lambda->getargs();
199 166 my $fval = $lambda->getcode();
200 166 my $name = $lambda->getname();
201  
202 # warn "LAMBDA---------------\n";
203 # warn Dumper($argtypes, $args, $fval);
204  
205 166 my $nvars = new Language::Farnsworth::Variables($eval->{vars});
206  
207 166 my %nopts = (vars => $nvars, funcs => $self, units => $eval->{units}, parser => $eval->{parser});
208 166 my $neval = $eval->new(%nopts);
209  
210 166 ▶ unless($self->checkparams($args, $argtypes))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->checkparams($args, $argtypes)) 0 166 166
211 {
212 0 ▶ if ($lambda->getname())
Branch: 1 branch
BranchTrueFalseTotal
if ($lambda->getname) { } 0 0 0
213 {
214 0 error "Number of arguments not correct to ".$lambda->getname();
215 }
216 else
217 {
218 0 error "Number of arguments not correct to lambda";
219 }
220 }
221
222  
223 166 $self->setupargs($neval, $args, $argtypes, $name);
224  
225 # warn ref($fval);
226  
227 163 ▶ if (ref($fval) ne "CODE")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fval ne 'CODE') { } 100 63 163
228 {
229 # warn "-------------ATTEMPTING TO CALL LAMBDA!-------------\n";
230 #print "FUNCTION NAME : $name\n";
231 # warn "Dumper of lambda: ".Dumper($fval);
232 # warn "--------------------THAT IS ALL\n";
233  
234 100 return $self->callbranch($neval, $fval);
235 }
236 else
237 {
238 #we have a code ref, so we need to call it, we use perlwrap{} to capture
239 63 ▶ return perlwrap {$fval->($args, $neval, $eval)};
Subroutine __ANON__: called CC=1 SCAR=0.0
240 }
241 # return $self->callbranch($neval, $fval);
242 }
243  
244 sub callbranch
245 100 ▶{
Subroutine callbranch: called CC=5 SCAR=17.2
Pod undocumented
246 100 my $self = shift;
247 100 my $eval = shift;
248 100 my $branches = shift;
249 # my $name = shift; #unused
250  
251  
252 # print "CALLBRANCHES :: ";
253 # print $name if defined $name;
254 # print " :: $eval\n";
255  
256 100 my $return = eval {$eval->evalbranch($branches)};
257 #warn Dumper($@);
258 100 ▶ if (ref($@) && $@->isa("Language::Farnsworth::Error"))
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and $@->isa('Language::Farnsworth::Error')) { } 2 98 100
elsif ($@) { } 0 98 98
Condition66%2/3
ref $@ and $@->isa("Language::Farnsworth::Error")
!ll&&!rl&&r
9802
MC/DC50%1/2
ref $@ and $@->isa("Language::Farnsworth::Error")
ref $@ $@->isa("Language::Farnsworth::Error")
Truth table66%2/3
A and B
A= ref $@B= $@->isa("Language::Farnsworth::Error")
ABresult
0X0
100
111
259 {
260 #warn Dumper($@->isreturn);
261 2 ▶ if ($@->isreturn)
Branch: 1 branch
BranchTrueFalseTotal
if ($@->isreturn) { } 2 0 2
262 {
263 2 return $@->getmsg();
264 }
265 else
266 { #redie the error
267 0 die $@;
268 }
269 }
270 elsif ($@)
271 {
272 0 warn "Unhandled perl exception!!!!!!";
273 0 error EPERL, $@;
274 }
275
276 98 return $return;
277 }
278  
279 #this was supposed to be the checks for types and such, but now its something else entirely, mostly
280 sub checkparams
281 166 ▶{
Subroutine checkparams: called CC=12 SCAR=26.5
Pod undocumented
282 166 my $self = shift;
283 166 my $args = shift;
284 166 my $argtypes = shift;
285  
286 166 my $vararg = 0;
287  
288 166 my $neededargs = 0;
289 166 my $badargs = 0;
290  
291 166 for my $argt (@$argtypes)
292 {
293 251 ▶ $neededargs++ unless (defined($argt->[1]) || !defined($argt->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argt->[1] or not defined $argt->[0] 233 18 251
Condition66%2/3
defined $argt->[1] or not defined $argt->[0]
l!l&&r!l&&!r
180233
MC/DC50%1/2
defined $argt->[1] or not defined $argt->[0]
defined $argt->[1] not defined $argt->[0]
Truth table66%2/3
A or not(B)
A= defined $argt->[1]B= not defined $argt->[0]
ABresult
1X1
011
000
294 251 ▶ $badargs++ if (!defined($argt->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argt->[0] 0 251 251
295 }
296  
297 #might want to change the !~ to something else?
298 #warn "Strange bug here to investigate, lambdas produce blessed array refs for vararg... wtf";
299 166 ▶ $vararg = 1 if (grep {defined($_->[2]) && ref($_->[2]) !~ /Language::Farnsworth::Value/ && (($_->[2] eq "VarArg") || (ref($_->[2]) eq "VarArg"))} @{$argtypes}); #find out if there is a vararg arg
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_->[2] and not ref($_->[2]) =~ /Language::Farnsworth::Value/ and $_->[2] eq "VarArg" || ref $_->[2] eq "VarArg";} @{$argtypes;} 21 145 166
Condition66%6/9
defined $_->[2] && !(ref($_->[2]) =~ /Language::Farnsworth::Value/) && ($_->[2] eq 'VarArg' || ref $_->[2] eq 'VarArg')
!ll&&!rl&&r
230021
defined $_->[2] and not ref($_->[2]) =~ /Language::Farnsworth::Value/
!ll&&!rl&&r
1696121
$_->[2] eq "VarArg" or ref $_->[2] eq "VarArg"
l!l&&r!l&&!r
2100
MC/DC50%2/4
defined $_->[2] && !(ref($_->[2]) =~ /Language::Farnsworth::Value/) && ($_->[2] eq 'VarArg' || ref $_->[2] eq 'VarArg')
defined $_->[2] not ref($_->[2]) =~ /Language::Farnsworth::Value/ $_->[2] eq "VarArg" ref $_->[2] eq "VarArg"
Truth table60%3/5
A and not(B) && C or D
A= defined $_->[2]B= not ref($_->[2]) =~ /Language::Farnsworth::Value/C= $_->[2] eq "VarArg"D= ref $_->[2] eq "VarArg"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
300  
301 #print "NEEDED: $neededargs :: $vararg\n";
302 #print Data::Dumper->Dump([$argtypes, $args->getarrayref()], [qw(argtypes args)]);
303  
304 166 ▶ return 1 if ($vararg || ($args->getarray() <= (@{$argtypes}-$badargs) && $args->getarray() >= $neededargs));
Branch: 1 branch
BranchTrueFalseTotal
if $vararg or $args->getarray <= @{$argtypes;} - $badargs and $args->getarray >= $neededargs 166 0 166
Condition50%3/6
$vararg or $args->getarray <= @{$argtypes;} - $badargs and $args->getarray >= $neededargs
l!l&&r!l&&!r
211450
$args->getarray <= @{$argtypes;} - $badargs and $args->getarray >= $neededargs
!ll&&!rl&&r
00145
MC/DC0%0/3
$vararg or $args->getarray <= @{$argtypes;} - $badargs and $args->getarray >= $neededargs
$vararg $args->getarray <= @{$argtypes;} - $badargs $args->getarray >= $neededargs
Truth table50%2/4
A or B and C
A= $varargB= $args->getarray <= @{$argtypes;} - $badargsC= $args->getarray >= $neededargs
ABCresult
1XX1
0111
00X0
0100
305  
306 #return 0 unless (ref($args) eq "Language::Farnsworth::Value") && ($args->{dimen}->compare({dimen=>{array=>1}}));
307  
308 0 return 0;
309 }
310  
311 sub getref
312 0 ▶{
Subroutine getref: not called CC=3 SCAR=24.8
Pod undocumented
313 0 my $self = shift;
314 0 my $argc = shift;
315 0 my $branch = shift;
316 0 my $name = shift;
317  
318 #print "\n\nGETREF\n";
319 #print Dumper($branch);
320  
321 0 ▶ if (ref $branch->[1] ne "Array")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $branch->[1] ne "Array") 0 0 0
322 {
323 #this should add support for some other stuff
324 0 error "Cannot get a reference if function/lambda is called without []";
325 }
326  
327 0 my $argexpr = $branch->[1][$argc];
328
329 #print Dumper($argbranches->[$argc]);
330
331 0 ▶ if (ref $argexpr ne "Fetch")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argexpr ne "Fetch") 0 0 0
332 {
333 0 error "Argument $argc to $name\[\] is not referencable";
334 }
335  
336 0 my $ref = $self->{funcs}{$name}->{scope}{vars}->getref($argexpr->[0]);
337  
338 #warn Dumper($argexpr, $ref);
339  
340 0 return $ref;
341 }
342  
343 4 1;
« blib/lib/Language/Farnsworth/Dimension.pm ↑ Summary blib/lib/Language/Farnsworth/Variables.pm »