blib/lib/Language/Farnsworth/Functions/Standard.pm

filter: Statementstmt 73.4% 122 / 166 Branchbran 50.0% 17 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 20 / 26 Podpod 0.0% 0 / 18 totaltotal 64.2% 160 / 249 CC 21 SCAR 12.6
CC
21
Cov
69%
match
6.0
units
6.0
push
6.0
CRAP 3.5
« blib/lib/Language/Farnsworth/Value/Types.pm ↑ Summary blib/lib/Language/Farnsworth/Error.pm »
1 package Language::Farnsworth::Functions::Standard;
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::Value::Types;
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 utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Math::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub init
15 4 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
16 4 my $env = shift;
17
18 4 $env->eval("push{arr byref isa [], x isa ...} := {arr =arr+x};");
19 4 $env->eval("unshift{arr byref isa [], x isa ...} := {arr =x+arr};");
20  
21 #$env->{funcs}->addfunc("push", [["arr", undef, $array, 0], ["in", undef, "VarArg", 0]],\&push); #actually i might rewrite this in farnsworth now that it can do it
22 4 $env->{funcs}->addfunc("pop", [["arr", undef, TYPE_ARRAY, 0]],\&pop, $env); #eventually this maybe too
23 4 $env->{funcs}->addfunc("shift", [["arr", undef, TYPE_ARRAY, 1]], \&shift, $env);
24 #$env->{funcs}->addfunc("unshift", [["arr", undef, $array, 0], ["in", undef, "VarArg", 0]], \&unshift);
25 4 $env->{funcs}->addfunc("sort", [["arr", undef, "VarArg", 0]],\&sort, $env);
26  
27 4 $env->{funcs}->addfunc("length", [["in", undef, undef, 0]],\&length, $env);
28  
29 4 $env->{funcs}->addfunc("ord", [["in", undef, TYPE_STRING, 0]],\&ord, $env);
30 4 $env->{funcs}->addfunc("chr", [["in", undef, TYPE_PLAIN, 0]],\&chr, $env);
31 4 $env->{funcs}->addfunc("index", [["str", undef, TYPE_STRING, 0],["substr", undef, TYPE_STRING, 0],["pos", TYPE_PLAIN, TYPE_PLAIN, 0]],\&index, $env);
32 4 $env->{funcs}->addfunc("eval", [["str", undef, TYPE_STRING, 0]],\&eval, $env); #needs special case!
33
34 4 $env->eval('map{sub isa {`x`}, x isa ...} := {var xx=[]+x; if (length[xx] == 1 && xx@0$ conforms []) {xx = x@0$}; if (length[xx] == 1 && !(xx conforms [])) {xx = [xx]}; var z=[]+xx; var e; var out=[]; while(length[z]) {e = shift[z]; push[out, (sub)[e]]}; out}');
35  
36 4 $env->{funcs}->addfunc("substrLen", [["str", undef, TYPE_STRING, 0],["left", undef, TYPE_PLAIN, 0],["length", undef, TYPE_PLAIN, 0]],\&substrlen, $env); #this one works like perls
37 4 $env->eval("substr{str,left,right}:={substrLen[str,left,right-left]}");
38 4 $env->eval("left{str,pos}:={substrLen[str,0,pos]}");
39 4 $env->eval("right{str,pos}:={substrLen[str,length[str]-pos,pos]}");
40  
41 4 $env->{funcs}->addfunc("reverse", [["in", undef, undef, 0]],\&reverse, $env);
42  
43 4 $env->eval("now{x = \"UTC\" isa \"\"} := {setzone[#now#, x]}");
44 4 $env->{funcs}->addfunc("setzone", [["date", undef, TYPE_DATE, 0],["zone", undef, TYPE_STRING, 0]], \&setzone, $env);
45  
46 #$env->{funcs}->addfunc("unit", [["in", undef, undef, 0]], \&unit);
47 4 $env->{funcs}->addfunc("units", [["in", undef, undef, 0]], \&units, $env);
48 4 $env->{funcs}->addfunc("error", [["in", undef, TYPE_STRING, 0]], \&doerror, $env);
49 4 $env->{funcs}->addfunc("return", [["in", undef, undef, 0]], \&doreturn, $env);
50 4 $env->{funcs}->addfunc("match", [["regex", undef, TYPE_STRING, 0], ["input", undef, TYPE_STRING, 0], ["options",TYPE_STRING,TYPE_STRING, 0]], \&match, $env);
51  
52 4 $env->eval('max{x isa ...} := {var z; if (length[x] == 1 && x@0$ conforms []) {z = x@0$} else {z=x}; var n = length[z]; var m=z@0$; var q; while((n=n-1)>=0){q=z@n$; q>m?m=q:0}; m}');
53 4 $env->eval('min{x isa ...} := {var z; if (length[x] == 1 && x@0$ conforms []) {z = x@0$} else {z=x}; var n = length[z]; var m=z@0$; var q; while((n=n-1)>=0){q=z@n$; q<m?m=q:0}; m}');
54 }
55  
56 sub doerror
57 0 ▶{
Subroutine doerror: not called CC=1 SCAR=6.9
Pod undocumented
58 #with an array we give the number of elements, with a string we give the length of the string
59 0 my ($args, $eval)= @_;
60  
61 0 my $input = $eval->{vars}->getvar("in"); #i should clean this up more too
62  
63 0 error $input->getstring();
64 }
65  
66 sub doreturn
67 3 ▶{
Subroutine doreturn: called CC=1 SCAR=0.0
Pod undocumented
68 #with an array we give the number of elements, with a string we give the length of the string
69 3 my ($args, $eval)= @_;
70  
71 3 my $input = $eval->{vars}->getvar("in"); #i should clean this up more too
72  
73 3 farnsreturn $input;
74 }
75  
76 sub match
77 0 ▶{
Subroutine match: not called CC=2 SCAR=17.9
Pod undocumented
78 0 my ($args, $eval)= @_;
79  
80 0 my $input = $eval->{vars}->getvar("input");
81 0 my $regex = $eval->{vars}->getvar("regex");
82 0 my $options = $eval->{vars}->getvar("options");
83  
84 0 ▶ error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
85 }
86  
87 sub units
88 0 ▶{
Subroutine units: not called CC=2 SCAR=17.9
Pod undocumented
89 #with an array we give the number of elements, with a string we give the length of the string
90 0 my ($args, $eval)= @_;
91  
92 0 my $input = $eval->{vars}->getvar("in"); #i should clean this up more too
93  
94 0 ▶ error "Need number with units for units[]" unless $input->istype("Pari");
Branch: 1 branch
BranchTrueFalseTotal
unless $input->istype("Pari") 0 0 0
95  
96 0 my $units = $input->getdimen();
97  
98 0 return Language::Farnsworth::Value::Pari->new(1.0, $units);
99 }
100  
101 sub setzone
102 0 ▶{
Subroutine setzone: not called CC=1 SCAR=6.9
Pod undocumented
103 #with an array we give the number of elements, with a string we give the length of the string
104 0 my ($args, $eval)= @_;
105
106 0 my $date = $eval->{vars}->getvar("date"); #i should clean this up more too
107 0 my $zone = $eval->{vars}->getvar("zone"); #i should clean this up more too
108  
109 0 $date->getdate()->set_time_zone($zone->getstring());
110
111 0 return $date;
112 }
113  
114 #sub unit
115 #{
116 #this code needs to be removed, and turned into an operator, its working in the wrong level all together.
117 #args is... a Language::Farnsworth::Value array
118 # my ($args, $eval, $branches)= @_;
119
120 #print Dumper($branches);
121  
122 # if ((ref($branches->[1][0]) ne "Fetch") || (!$eval->{units}->isunit($branches->[1][0][0])))
123 # {
124 # error "First argument to unit[] must be a unit name";
125 # }
126  
127 # my $unitvar = $eval->{units}->getunit($branches->[1][0][0]);
128 #
129 # return $unitvar; #if its undef, its undef! i should really make some kind of error checking here
130 #}
131  
132 sub sort
133 5 ▶{
Subroutine sort: called CC=5 SCAR=16.3
Pod undocumented
134 #args is... a Language::Farnsworth::Value array
135 5 my ($args, $eval)= @_;
136 5 my $arr = $eval->{vars}->getvar("arr");
137
138 5 my $argcount = $arr->getarray();
139  
140 5 my $sortlambda;
141  
142 5 ▶ if (ref($arr->getarrayref()->[0]) eq "Language::Farnsworth::Value::Lambda")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arr->getarrayref->[0] eq 'Language::Farnsworth::Value::Lambda') { } 2 3 5
143 {
144 2 $sortlambda = shift(@{$arr->getarrayref});
145 }
146 else
147 {
148 #i should really do this outside the sub ONCE, but i'm lazy for now
149 3 $sortlambda = $eval->eval("{`a,b` a <=> b}");
150 }
151  
152 my $sortsub = sub
153 47 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 47 my $val = $eval->evalbranch(bless [(bless [$a, $b], 'Array'), $sortlambda], 'LambdaCall');
155
156 47 0+$val->toperl(); #return this, just to make sure the value is right
157 5 };
158  
159 5 my @sorts;
160  
161 5 ▶ if ($arr->getarray() > 1)
Branch: 2 branches
BranchTrueFalseTotal
if ($arr->getarray > 1) { } 2 3 5
elsif ($arr->getarray == 1 and ref $arr->getarrayref->[0] eq 'Language::Farnsworth::Value::Array') { } 3 0 3
162 {
163 #we've been given a bunch of things, assume we need to sort them like that
164 2 push @sorts, $arr->getarray();
165 }
166 ▶ elsif (($arr->getarray() == 1) && (ref($arr->getarrayref()->[0]) eq "Language::Farnsworth::Value::Array"))
Condition33%1/3
$arr->getarray == 1 and ref $arr->getarrayref->[0] eq "Language::Farnsworth::Value::Array"
!ll&&!rl&&r
003
MC/DC0%0/2
$arr->getarray == 1 and ref $arr->getarrayref->[0] eq "Language::Farnsworth::Value::Array"
$arr->getarray == 1 ref $arr->getarrayref->[0] eq "Language::Farnsworth::Value::Array"
Truth table33%1/3
A and B
A= $arr->getarray == 1B= ref $arr->getarrayref->[0] eq "Language::Farnsworth::Value::Array"
ABresult
0X0
100
111
167 {
168 #given an array as a second value, dereference it since its the only thing we've got
169 3 push @sorts, $arr->getarrayref()->[0]->getarray();
170 }
171 else
172 {
173 #ok you want me to sort ONE thing? i'll sort that one thing, in O(1) time!
174 0 return $arr->getarrayref()->[0];
175 }
176  
177 5 my @rets = CORE::sort $sortsub @sorts;
178  
179 #print "SORT RETURNING!\n";
180 #print Dumper(\@rets);
181  
182 5 return new Language::Farnsworth::Value::Array([@rets]);
183 }
184  
185 sub push
186 0 ▶{
Subroutine push: not called CC=2 SCAR=17.9
Pod undocumented
187 #args is... a Language::Farnsworth::Value array
188 0 my ($args, $eval)= @_;
189
190 0 my $array = $eval->{vars}->getvar("arr");
191  
192 0 ▶ unless ($array->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($array->istype("Array")) 0 0 0
193 {
194 0 error "First argument to push must be an array";
195 }
196  
197 #ok type checking is done, do the push!
198
199 0 my @input = $args->getarray();
200 0 shift @input; #remove the original array value
201  
202 #i should probably flatten arrays here so that; a=[1,2,3]; push[a,a]; will result in a = [1,2,3,1,2,3]; instead of a = [1,2,3,[1,2,3]];
203 #no i shouldn't, i'll be adding the ability to make them flatten in the parser
204  
205 0 CORE::push @{$array->getarrayref()}, @input;
206  
207 0 return new Language::Farnsworth::Value::Pari(0+@input); #returns number of items pushed
208 }
209  
210 sub unshift
211 0 ▶{
Subroutine unshift: not called CC=2 SCAR=17.9
Pod undocumented
212 #args is... a Language::Farnsworth::Value array
213 0 my ($args, $eval)= @_;
214
215 0 my $array = $eval->{vars}->getvar("arr");
216  
217 0 ▶ unless ($array->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($array->istype("Array")) 0 0 0
218 {
219 0 error "First argument to push must be an array";
220 }
221  
222 #ok type checking is done, do the push!
223
224 0 my @input = $args->getarray();
225 0 shift @input; #remove the original array value
226  
227 #i should probably flatten arrays here so that; a=[1,2,3]; push[a,a]; will result in a = [1,2,3,1,2,3]; instead of a = [1,2,3,[1,2,3]];
228 #no i shouldn't, i'll be adding the ability to make them flatten in the parser
229  
230 0 CORE::unshift @{$array->getarrayref()}, @input;
231  
232 0 return new Language::Farnsworth::Value::Pari(0+@input); #returns number of items pushed
233 }
234  
235 sub pop
236 1 ▶{
Subroutine pop: called CC=2 SCAR=7.1
Pod undocumented
237 #args is... a Language::Farnsworth::Value array
238 1 my ($args, $eval)= @_;
239
240 1 my $array = $eval->{vars}->getvar("arr");
241
242 1 ▶ unless ($array->istype("Array"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($array->istype("Array")) 0 1 1
243 {
244 0 error "Argument to pop must be an array";
245 }
246  
247 #ok type checking is done, do the pop
248
249 1 my $retval = CORE::pop @{$array->getarrayref()};
250  
251 1 return $retval; #pop returns the value of the element removed
252 }
253  
254 sub shift
255 12 ▶{
Subroutine shift: called CC=3 SCAR=11.3
Pod undocumented
256 #args is... a Language::Farnsworth::Value array
257 12 my ($args, $eval)= @_;
258
259 12 my $var = $eval->{vars}->getvar("arr");
260 12 my $varref = $var->getref();
261  
262 12 ▶ error "Need lvalue for input to shift[]" unless defined $varref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $varref 0 12 12
263  
264 #if ((ref($branches->[1][0]) ne "Fetch") || (!$eval->{vars}->isvar($branches->[1][0][0])))
265 #{
266 # die "Argument to shift must be a variable";
267 #}
268  
269 #my $arrayvar = $eval->{vars}->getvar($branches->[1][0][0]);
270  
271 12 ▶ unless (ref($var) eq "Language::Farnsworth::Value::Array")
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var eq "Language::Farnsworth::Value::Array") 0 12 12
272 {
273 0 error "Argument to shift must be an array";
274 }
275  
276 #ok type checking is done, do the pop
277
278 12 my $retval = CORE::shift @{${$varref}->getarrayref()};
279  
280 12 return $retval; #pop returns the value of the element removed
281 }
282  
283 sub length
284 21 ▶{
Subroutine length: called CC=5 SCAR=16.7
Pod undocumented
285 #with an array we give the number of elements, with a string we give the length of the string
286 21 my ($args, $eval)= @_;
287 21 my @argsarry = $args->getarray();
288  
289 21 my @rets;
290  
291 21 for my $arg (@argsarry)
292 {
293 21 ▶ if (ref($arg) eq "Language::Farnsworth::Value::Array")
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'Language::Farnsworth::Value::Array') { } 19 2 21
elsif (ref $arg eq 'Language::Farnsworth::Value::String') { } 2 0 2
294 {
295 19 CORE::push @rets, Language::Farnsworth::Value::Pari->new(scalar $arg->getarray());
296 }
297 elsif (ref($arg) eq "Language::Farnsworth::Value::String")
298 {
299 2 CORE::push @rets, Language::Farnsworth::Value::Pari->new(length $arg->getstring());
300 }
301 else
302 {
303 #until i decide how this should work on regular numbers, just do this
304 0 CORE::push @rets, Language::Farnsworth::Value::Pari->new(0);
305 }
306 }
307  
308 21 ▶ if (@rets > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@rets > 1) { } 0 21 21
309 {
310 0 return Language::Farnsworth::Value::Array->new(\@rets);
311 }
312 else
313 {
314 21 return $rets[0];
315 }
316 }
317  
318 sub reverse
319 2 ▶{
Subroutine reverse: called CC=5 SCAR=16.7
Pod undocumented
320 #with an array we give the number of elements, with a string we give the length of the string
321 2 my ($args, $eval)= @_;
322 2 my @argsarry = $args->getarray();
323  
324 2 my @rets;
325  
326 2 for my $arg (reverse @argsarry) #this will make reverse[1,2,3,4] return [4,3,2,1]
327 {
328 2 ▶ if (ref($arg) eq "Language::Farnsworth::Value::Array")
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'Language::Farnsworth::Value::Array') { } 1 1 2
elsif (ref $arg eq 'Language::Farnsworth::Value::String') { } 1 0 1
329 {
330 1 CORE::push @rets, Language::Farnsworth::Value::Array->new([reverse $arg->getarray()]);
331 }
332 elsif (ref($arg) eq "Language::Farnsworth::Value::String")
333 {
334 1 CORE::push @rets, Language::Farnsworth::Value::String->new("".reverse($arg->getstring()));
335 }
336 else
337 {
338 0 CORE::push @rets, $arg; #should i make it print the reverse of all its arguments? yes, lets fix that
339 }
340 }
341  
342 2 ▶ if (@rets > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@rets > 1) { } 0 2 2
343 {
344 0 return Language::Farnsworth::Value::Array->new(\@rets);
345 }
346 else
347 {
348 2 return $rets[0];
349 }
350 }
351  
352 sub substrlen
353 4 ▶{
Subroutine substrlen: called CC=2 SCAR=7.2
Pod undocumented
354 #with an array we give the number of elements, with a string we give the length of the string
355 4 my ($args, $eval)= @_;
356 4 my @arg = $args->getarray();
357  
358 4 ▶ if (ref $arg[0] eq "Language::Farnsworth::Value::String")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg[0] eq 'Language::Farnsworth::Value::String') { } 4 0 4
359 {
360 #do i need to do something to convert these to work? (the 1,2 anyway?)
361 4 my $ns = substr($arg[0]->getstring(), $arg[1]->toperl(), $arg[2]->toperl());
362 #print "SUBSTR :: $ns\n";
363 4 return Language::Farnsworth::Value::String->new($ns);
364 }
365 else
366 {
367 0 error "substr and friends only works on strings";
368 }
369 }
370  
371 sub ord
372 2 ▶{
Subroutine ord: called CC=1 SCAR=0.0
Pod undocumented
373 #with an array we give the number of elements, with a string we give the length of the string
374 2 my ($args, $eval)= @_;
375  
376 2 my $input = $eval->{vars}->getvar("in"); #i should clean this up more too
377  
378 2 my $ns = ord($input->getstring());
379 2 return Language::Farnsworth::Value::Pari->new($ns);
380 }
381  
382 sub chr
383 2 ▶{
Subroutine chr: called CC=1 SCAR=0.0
Pod undocumented
384 #with an array we give the number of elements, with a string we give the length of the string
385 2 my ($args, $eval)= @_;
386  
387 2 my $input = $eval->{vars}->getvar("in"); #i should clean this up more too
388  
389 2 my $ns = chr($input->toperl());
390 2 return Language::Farnsworth::Value::String->new($ns);
391 }
392  
393 sub index
394 1 ▶{
Subroutine index: called CC=1 SCAR=0.0
Pod undocumented
395 #with an array we give the number of elements, with a string we give the length of the string
396 1 my ($args, $eval)= @_;
397  
398 1 my $string = $eval->{vars}->getvar("str")->getstring();
399 1 my $substr = $eval->{vars}->getvar("substr")->getstring();
400 1 my $pos = $eval->{vars}->getvar("pos")->toperl();
401  
402 1 my $ns = index $string, $substr, $pos; #substr($arg[0]{pari}, "".$arg[1]{pari}, "".$arg[2]{pari});
403 1 return Language::Farnsworth::Value::Pari->new($ns); #give string flag of 1, since we don't know what language is intended
404 }
405  
406 sub eval
407 2 ▶{
Subroutine eval: called CC=1 SCAR=0.0
Pod undocumented
408 #with an array we give the number of elements, with a string we give the length of the string
409 2 my ($args, $eval, $reval)= @_;
410 2 my $evalstr = $eval->{vars}->getvar("str")->getstring();
411  
412 # my $nvars = new Language::Farnsworth::Variables($eval->{vars});
413 # my %nopts = (vars => $nvars, funcs => $eval->{funcs}, units => $eval->{units}, parser => $eval->{parser});
414 # my $neval = $eval->new(%nopts);
415  
416 2 return $reval->eval($evalstr);
417 }
418  
419 4 1;
« blib/lib/Language/Farnsworth/Value/Types.pm ↑ Summary blib/lib/Language/Farnsworth/Error.pm »