blib/lib/Language/FP.pm

filter: Statementstmt 82.6% 377 / 456 Branchbran 44.3% 78 / 176 Conditioncond 34.6% 9 / 26 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 81.5% 84 / 103 Podpod 8.8% 3 / 34 totaltotal 68.0% 552 / 811 CC 105 SCAR 15.7
CC
105
Cov
71%
fp_eval
21.5
__ANON__
20.5
__ANON__
12.0
CRAP 4.8
↑ Summary
1 #!/usr/bin/env perl
2 package Language::FP;
3 1 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7 1 @EXPORT = qw/fp_eval/;
8 1 @EXPORT_OK = qw/perl2fp fp2perl bottom BOTTOM/;
9 1 %EXPORT_TAGS = (':all' => [@EXPORT_OK, @EXPORT]);
10 1 @ISA = qw(Exporter);
11  
12 1 $VERSION = 0.03;
13  
14 0 ▶sub BOTTOM () { # the universal bad value
Subroutine BOTTOM: not called CC=2 SCAR=10.9
Pod undocumented
15 0 ▶ if ($::FP_DEBUG =~ /b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::FP_DEBUG =~ /b/) 0 0 0
16 1 ▶ use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 confess("Bottom!");
18 }
19 undef
20 0 }
21  
22 2514 ▶sub bottom { # check for bottom
Subroutine bottom: called CC=3 SCAR=14.2
Pod undocumented
23 2514 ▶ (@_ > 0 && !defined($_[0])) ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 && !defined($_[0]) ? : 0 2514 2514
Condition33%1/3
@_ > 0 and CORE::not defined $_[0]
!ll&&!rl&&r
025140
MC/DC0%0/2
@_ > 0 and CORE::not defined $_[0]
@_ > 0 CORE::not defined $_[0]
Truth table33%1/3
A and not(B)
A= @_ > 0B= CORE::not defined $_[0]
ABresult
0X0
100
111
24 }
25  
26 1540 ▶sub numeric { # check for 2 integer args
Subroutine numeric: called CC=2 SCAR=9.2
Pod undocumented
27 1540 ▶ return ($_[0] =~ /$RE{num}{real}/o) && ($_[1] =~ /$RE{num}{real}/o);
Condition33%1/3
$_[0] =~ /$RE{'num'}{'real'}/o && $_[1] =~ /$RE{'num'}{'real'}/o
!ll&&!rl&&r
001540
MC/DC0%0/2
$_[0] =~ /$RE{'num'}{'real'}/o && $_[1] =~ /$RE{'num'}{'real'}/o
$_[0] =~ /$RE{'num'}{'real'}/o $_[1] =~ /$RE{'num'}{'real'}/o
Truth table33%1/3
A && B
A= $_[0] =~ /$RE{'num'}{'real'}/oB= $_[1] =~ /$RE{'num'}{'real'}/o
ABresult
0X0
100
111
28 }
29  
30 ######################################################################
31 ## Parser
32  
33 ##############################
34 # Debugging
35  
36 0 ▶sub info { # pretty debugging output.
Subroutine info: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($pack, $fn, $line, $subr) = caller 1;
38 0 $subr =~ s/^.*:://;
39 0 print STDERR "[$subr] ", @_, "\n";
40 }
41  
42 182 ▶sub Dparse { # parse-time debugging output
Subroutine Dparse: called CC=2 SCAR=7.6
Pod undocumented
43 182 ▶ goto &info if $::FP_DEBUG =~ /p/;
Branch: 1 branch
BranchTrueFalseTotal
if $main::FP_DEBUG =~ /p/ 0 182 182
44 }
45  
46 25222 ▶sub Drun { # run-time debugging.
Subroutine Drun: called CC=2 SCAR=7.6
Pod undocumented
47 25222 ▶ goto &info if $::FP_DEBUG =~ /r/;
Branch: 1 branch
BranchTrueFalseTotal
if $main::FP_DEBUG =~ /r/ 0 25222 25222
48 }
49  
50 ##############################
51 # Utilities
52  
53 # XXX: this shouldn't be needed. It makes <X> behave the same as X
54 # when passed as an argument list. Single-element lists and scalar
55 # values aren't the same kind of thing, but we're trying to pretend as
56 # if they are. Otherwise, perl functions called from FP will all have
57 # to take array-refs.
58 1485 ▶sub as_array($) {
Subroutine as_array: called CC=2 SCAR=6.9
Pod undocumented
59 1485 my $a = shift;
60 1485 ▶ if (ref $a eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'ARRAY') { } 126 1359 1485
61 126 @$a;
62 } else {
63 1359 $a;
64 }
65 }
66  
67 # XXX: this is the disgusting inverse of as_array
68 4390 ▶sub to_arrayref {
Subroutine to_arrayref: called CC=2 SCAR=6.9
Pod undocumented
69 4390 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 4384 6 4390
70 4384 return shift;
71 } else {
72 6 return [@_];
73 }
74 }
75  
76 9395 ▶sub call_it { # call a coderef, with verbosity
Subroutine call_it: called CC=1 SCAR=0.0
Pod undocumented
77 9395 my $f = shift;
78 9395 Drun "Calling $f (@_)";
79 9395 my @res = $f->(@_);
80 9395 Drun "-> (@res)";
81 9395 @res;
82 }
83  
84 15716 ▶sub term { # create a typed parse-tree node.
Subroutine term: called CC=1 SCAR=0.0
Pod undocumented
85 15716 my $type = shift;
86 15716 return { type => $type, val => [@_] };
87 }
88  
89 ##############################
90 # Symbol lookup.
91  
92 # Note: we need to do a bit of magic here to look up functions and
93 # variables in both Language::FP and the calling package.
94  
95 66 ▶sub findsym { # look up a function
Subroutine findsym: called CC=5 SCAR=16.2
Pod undocumented
96 66 my ($sym, $type) = @_;
97 66 ▶ if (ref $sym eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sym eq $type) 0 66 66
98 0 return $sym;
99 }
100 66 my ($where, $thing);
101 66 foreach ('Language::FP', pkg()) {
102 90 my $x = $_.'::'.$sym;
103 90 ▶ if (defined($thing = *{$x}{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($thing = *{$x;}{$type})) 66 24 90
104 66 $where = $x;
105 66 last;
106 }
107 }
108 66 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 33 33 66
109 33 ($thing, $where);
110 } else {
111 33 $thing;
112 }
113 }
114  
115 ######################################################################
116 ## The parser
117  
118 1 my $P = undef;
119 68 ▶sub get_parser {
Subroutine get_parser: called CC=3 SCAR=11.1
Pod undocumented
120 68 ▶ return $P if $P;
Branch: 1 branch
BranchTrueFalseTotal
if $P 67 1 68
121 1 ▶ $P = new Parse::RecDescent <<'EOG' or die "Can't create parser!";
Branch: 1 branch
BranchTrueFalseTotal
$P = "Parse::RecDescent"->new("\n{\nuse Regexp::Common;\n BEGIN {\n\tno strict 'refs';\n\tforeach (qw|term findsym|) {\n\t *{__PACKAGE__.'::'.\$_} = \\&{'Language::FP::'.\$_};\n\t}\n }\n}\n\nthing:\t 'val' <commit> id_undef '=' application\n\t\t{ \$return = term 'val', \@item{qw(id_undef application)} }\n\t| 'def' <commit> id_undef '=' termlist\n\t\t{ \$return = term 'def', \@item{qw(id_undef termlist)} }\n\t| application\n\t\t{ \$return = \$item[1];1; }\n\t| /\\s*/\n\t| <error>\n\napplication: termlist ':' <commit> data\n\t\t{ \$return = term 'application', \@item{qw(termlist data)}; }\n\t| data\n\t\t{ \$return = \$item[1]; }\n\ntermlist: 'while' <commit> complist termlist\n\t\t{ \$return = term 'while', \@item{qw(complist termlist)} }\n \t| complist '->' <commit> complist ';' termlist\n\t\t{ \$return = term 'if', \@item[1,4,6] }\n\t| complist \n\t\t{ \$return = \$item[1];1; }\n\t| <error>\n\ncomplist: <rightop: func '.' func>\n\t\t{ \$return = term 'compose', \@{\$item[1]} }\n\nfunc:\t 'bu' <commit> func data\n\t\t{ \$return = term 'bu', \@item{qw(func data)} }\n\t| '/' func\n\t\t{ \$return = term 'insert', \$item{func} }\n\t| '\@' <commit> func\n\t\t{ \$return = term 'forall', \$item{func} }\n\t| '(' <commit> termlist ')'\n\t\t{ \$return = \$item{termlist} }\n\t| '[' <commit> <rightop: termlist ',' termlist> ']'\n\t\t{ \$return = term 'distribute', \@{\$item[3]} }\n\t| '`' <commit> data\n\t\t{ \$return = term 'constant', \$item{data} }\n\t| sfunc\n\t\t{ \$return = \$item[1];1; }\n\t| id\n\t\t{ \$return = \$item[1];1; }\n\t| <error>\n\ndata:\t datum\n\t\t{ \$return = term 'data', \$item[1] }\n\t| <error>\n\ndatum:\t '<' <commit> datum(s?) '>'\n\t\t{ \$return = \$item[3];1; }\n\t| /\$RE{num}{real}/o\n\t\t{ \$return = \$item[1];1; }\n \t| /\$RE{num}{int}/o\n\t\t{ \$return = \$item[1];1; }\n\t| /\$RE{quoted}/o\n\t\t{ \$return = substr(\$item[1], 1, length(\$item[1]) - 2);1; }\n\t| m{[a-rt-zA-Z_][\\w\\d]*}\n\t <error?: Undefined variable \"\$item[1]\"> <commit> {\n\t\tno strict 'refs';\n\t\t# XXX: actually interpolate variables during parse.\n\t\t\$return = findsym(\$item[1], 'ARRAY') || undef;\n\t}\n\t| <error>\n\nsfunc:\t /\\d+/\n\t\t{ \$return = term 'sfunc', \$item[1]; }\n\nid_undef: m{[a-zA-Z_][\\w\\d]*}\n\t\t{ \$return = term 'id_undef', \$item[1]; }\n\nid:\t m{[a-zA-Z_][\\w\\d]*}\n\t\t{ \$return = term 'id', \$item[1];1; }\n\t| m{([!<>=]=) | [+*/<>-] | ([gln]e) | ([gl]t) | eq}x\n\t\t{ \$return = term 'op', \$item[1]; }\n\n") or die "Can't create parser!" 1 0 1
122  
123 {
124 use Regexp::Common;
125 BEGIN {
126 no strict 'refs';
127 foreach (qw|term findsym|) {
128 *{__PACKAGE__.'::'.$_} = \&{'Language::FP::'.$_};
129 }
130 }
131 }
132  
133 thing: 'val' <commit> id_undef '=' application
134 { $return = term 'val', @item{qw(id_undef application)} }
135 | 'def' <commit> id_undef '=' termlist
136 { $return = term 'def', @item{qw(id_undef termlist)} }
137 | application
138 { $return = $item[1];1; }
139 | /\s*/
140 | <error>
141  
142 application: termlist ':' <commit> data
143 { $return = term 'application', @item{qw(termlist data)}; }
144 | data
145 { $return = $item[1]; }
146  
147 termlist: 'while' <commit> complist termlist
148 { $return = term 'while', @item{qw(complist termlist)} }
149 | complist '->' <commit> complist ';' termlist
150 { $return = term 'if', @item[1,4,6] }
151 | complist
152 { $return = $item[1];1; }
153 | <error>
154  
155 complist: <rightop: func '.' func>
156 { $return = term 'compose', @{$item[1]} }
157  
158 func: 'bu' <commit> func data
159 { $return = term 'bu', @item{qw(func data)} }
160 | '/' func
161 { $return = term 'insert', $item{func} }
162 | '@' <commit> func
163 { $return = term 'forall', $item{func} }
164 | '(' <commit> termlist ')'
165 { $return = $item{termlist} }
166 | '[' <commit> <rightop: termlist ',' termlist> ']'
167 { $return = term 'distribute', @{$item[3]} }
168 | '`' <commit> data
169 { $return = term 'constant', $item{data} }
170 | sfunc
171 { $return = $item[1];1; }
172 | id
173 { $return = $item[1];1; }
174 | <error>
175  
176 data: datum
177 { $return = term 'data', $item[1] }
178 | <error>
179  
180 datum: '<' <commit> datum(s?) '>'
181 { $return = $item[3];1; }
182 | /$RE{num}{real}/o
183 { $return = $item[1];1; }
184 | /$RE{num}{int}/o
185 { $return = $item[1];1; }
186 | /$RE{quoted}/o
187 { $return = substr($item[1], 1, length($item[1]) - 2);1; }
188 | m{[a-rt-zA-Z_][\w\d]*}
189 <error?: Undefined variable "$item[1]"> <commit> {
190 no strict 'refs';
191 # XXX: actually interpolate variables during parse.
192 $return = findsym($item[1], 'ARRAY') || undef;
193 }
194 | <error>
195  
196 sfunc: /\d+/
197 { $return = term 'sfunc', $item[1]; }
198  
199 id_undef: m{[a-zA-Z_][\w\d]*}
200 { $return = term 'id_undef', $item[1]; }
201  
202 id: m{[a-zA-Z_][\w\d]*}
203 { $return = term 'id', $item[1];1; }
204 | m{([!<>=]=) | [+*/<>-] | ([gln]e) | ([gl]t) | eq}x
205 { $return = term 'op', $item[1]; }
206  
207 EOG
208 1 $P;
209 }
210  
211 ######################################################################
212 ## Builtin functions (for both compilers).
213  
214 # FP is supposed to be "bottom-preserving". In other words, once a
215 # single operaation fails, it taints all results that depend on it.
216 # The only way to recover from this is to explicitly recognize the
217 # "bottom" condition using the bottom() test.
218  
219 1 my %op_guts;
220 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1 %op_guts = (
222 ## List ops #####
223 # first/last element of list
224 hd => '@_ ? $_[0] : BOTTOM',
225 hdr => '@_ ? $_[-1] : BOTTOM',
226 # rest of list
227 tl => '@_ ? @_[1..$#_] : BOTTOM',
228 tlr => '@_ ? @_[0..$#_ - 1] : BOTTOM',
229 len => 'return BOTTOM if bottom @_; scalar @_',
230 'reverse' => 'reverse @_',
231 # append
232 apndl => '($_[0], @{$_[1]})',
233 apndr => '(@{$_[0]}, $_[1])',
234 # Rotate
235 rotl => '@_ ? @_[1..$#_,0] : ()',
236 rotr => '@_ ? @_[$#_, 0..$#_ - 1] : ()',
237 # Catenate
238 cat => 'map { as_array $_ } @_',
239 ## Logical ops #####
240 'and' => '$_[0] && $_[1]',
241 'or' => '$_[0] || $_[1]',
242 'not' => '!$_[0]',
243 ## Other ops #####
244 id => '@_',
245 out => 'print STDERR perl2fp(@_), "\n"; @_',
246 iota => '1 .. $_[0]',
247 atom => '@_ == 1 && ref($_[0]) eq "SCALAR"',
248 null => '@_ == 0',
249 ## "shaping" list-ops #####
250 distl => q{
251 my ($a, $b) = @_;
252 return BOTTOM unless !bottom($a) && ref $b eq 'ARRAY';
253 map { [$a, $_] } @$b;
254 },
255  
256 distr => q{
257 my ($a, $b) = @_;
258 return BOTTOM unless !bottom($b) && ref $a eq 'ARRAY';
259 map { [$_, $b] } @$a;
260 },
261  
262 trans => q{
263 my @ret;
264 return () unless @_;
265 my $len = scalar @{$_[0]};
266 foreach (@_[1..$#_]) {
267 return BOTTOM unless ref $_ eq 'ARRAY' && @$_ == $len;
268 }
269 for (my $i = 0; $i < $len; $i++) {
270 push @ret, [ map { $_->[$i] } @_ ];
271 }
272 @ret;
273 },
274 );
275 }
276  
277 ######################################################################
278 ## Closure-based "Compiler"
279  
280 3 ▶sub defun { # 'def' X '=' ...
Subroutine defun: called CC=1 SCAR=0.0
Pod undocumented
281 3 my ($name, $val) = @_;
282 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 3 *{pkg().'::'.$name} = $val;
284 3 Drun "Defined function $name";
285 3 'ok';
286 }
287  
288 0 ▶sub defvar { # 'val' X '=' ...
Subroutine defvar: not called CC=1 SCAR=2.2
Pod undocumented
289 0 my ($name, $val) = @_;
290 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 0 @{pkg().'::'.$name} = as_array $val;
292 0 Drun "Defined value $name";
293 0 'ok';
294 }
295  
296 2 ▶sub do_bu { # bu (i.e. currying)
Subroutine do_bu: called CC=1 SCAR=0.0
Pod undocumented
297 2 my ($f, $o) = @_;
298 2 Dparse "using $f($o, ...)";
299 5 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
300 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301 5 Drun "bu $f ($o, @_)";
302 5 call_it($f, $o, @_);
303 2 };
304 }
305  
306 89 ▶sub compose { # '.' operator
Subroutine compose: called CC=1 SCAR=0.0
Pod undocumented
307 89 my @funcs = @_;
308 89 Dparse "using (@funcs)";
309 4133 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
310 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
311 4133 Drun "compose (@funcs)";
312 4133 foreach my $f (reverse @funcs) {
313 5224 @_ = call_it($f, @_);
314 }
315 4133 @_;
316 89 };
317 }
318  
319 18 ▶sub distribute { # '[...]' list-of-functions
Subroutine distribute: called CC=1 SCAR=0.0
Pod undocumented
320 18 my @xs = @_;
321 18 Dparse "using (@xs)";
322 1053 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
323 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
324 1053 Drun "distribute (@xs) : (@_)";
325 1053 map { to_arrayref call_it $_, @_ } @xs;
326 }
327 18 }
328  
329 4 ▶sub ifelse { # 'a -> b ; c' construct
Subroutine ifelse: called CC=1 SCAR=0.0
Pod undocumented
330 4 my ($if, $then, $else) = @_;
331 4 Dparse "if $if then $then else $else";
332 490 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
333 # XXX: having to call this in array context sucks, but is necessary.
334 490 Drun "if $if then $then else $else";
335 490 my ($test) = call_it $if, @_;
336 490 ▶ if (bottom($test)) {
Branch: 2 branches
BranchTrueFalseTotal
if (bottom($test)) { } 0 490 490
elsif ($test) { } 302 188 490
337 0 BOTTOM;
338 } elsif ($test) {
339 302 call_it $then, @_;
340 } else {
341 188 call_it $else, @_;
342 }
343 4 };
344 }
345  
346 3 ▶sub awhile { # 'while x y'
Subroutine awhile: called CC=1 SCAR=0.0
Pod undocumented
347 3 my ($while, $do) = @_;
348 3 Dparse "while ($while) $do";
349 46 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
350 46 Drun "while ($while) $do -> (@_)";
351 46 my $test;
352 46 ▶ while (!bottom($test = (call_it $while, @_)[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (bottom($test = (call_it($while, @_))[0])) 505 0 505
353 505 ▶ if (!$test) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($test) 46 459 505
354 46 Drun "END while ($while): (@_)";
355 46 return @_;
356 }
357 459 @_ = call_it $do, @_;
358 }
359 # Bottom.
360 0 BOTTOM;
361 }
362 3 }
363  
364 6 ▶sub forall { # '@' operator, i.e. map
Subroutine forall: called CC=1 SCAR=0.0
Pod undocumented
365 6 my $f = shift;
366 6 Dparse "using $f";
367 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
368 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
369 6 Drun "forall $f (@_)";
370 6 map { to_arrayref call_it $f, as_array $_ } @_;
371 6 };
372 }
373  
374 4 ▶sub insert { # '/' operator, i.e. reduce
Subroutine insert: called CC=1 SCAR=0.0
Pod undocumented
375 4 my $f = shift;
376 4 Dparse "using $f";
377 4 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
378 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 4 Drun "insert $f (@_)";
380 4 ▶ return () unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
381 4 my $r = $_[0];
382 4 ▶ return BOTTOM if bottom($r);
Branch: 1 branch
BranchTrueFalseTotal
if bottom($r) 0 4 4
383 4 foreach (@_[1..$#_]) {
384 53 $r = (call_it $f, $r, $_)[0];
385 53 ▶ return BOTTOM if bottom($r);
Branch: 1 branch
BranchTrueFalseTotal
if bottom($r) 0 53 53
386 }
387 4 $r;
388 }
389 4 }
390  
391 19 ▶sub constant { # constant '`' operator
Subroutine constant: called CC=1 SCAR=0.0
Pod undocumented
392 19 my $x = shift;
393 19 Dparse $x;
394 646 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
395 646 Drun "constant $x";
396 646 as_array $x;
397 19 };
398 }
399  
400 31 ▶sub apply { # ':' operator
Subroutine apply: called CC=1 SCAR=0.0
Pod undocumented
401 31 my ($func, $args) = @_;
402 31 return $func->(as_array $args);
403 }
404  
405 1 my %ops = (); # symbol table for binary operators
406 34 ▶sub make_binary_ops {
Subroutine make_binary_ops: called CC=4 SCAR=14.0
Pod undocumented
407 34 ▶ return if keys %ops > 0;
Branch: 1 branch
BranchTrueFalseTotal
if keys %ops > 0 33 1 34
408 # Build binary operator functions.
409 1 foreach my $f (qw|+ - * / ** == != < > <= >=|) {
410 11 ▶ $ops{$f} = eval qq{sub {
Condition50%1/2
eval "sub {\n \t\t\treturn BOTTOM unless numeric(\@_);\n\t\t\t\$_[0] $f \$_[1]\n\t\t}\n\t\t" || die($@)
l!l
110
MC/DC0%0/1
eval "sub {\n \t\t\treturn BOTTOM unless numeric(\@_);\n\t\t\t\$_[0] $f \$_[1]\n\t\t}\n\t\t" || die($@)
eval "sub {\n \t\t\treturn BOTTOM unless numeric(\@_);\n\t\t\t\$_[0] $f \$_[1]\n\t\t}\n\t\t"
Truth table50%1/2
A
A= eval "sub {\n \t\t\treturn BOTTOM unless numeric(\@_);\n\t\t\t\$_[0] $f \$_[1]\n\t\t}\n\t\t"
Aresult
11
00
411 return BOTTOM unless numeric(\@_);
412 \$_[0] $f \$_[1]
413 }
414 } || die $@;
415 }
416 }
417  
418 1 local $::fp_caller = 'Language::FP';
419 72 ▶sub pkg { # package in which to bind functions
Subroutine pkg: called CC=1 SCAR=0.0
Pod undocumented
420 72 $::fp_caller;
421 }
422  
423 my %compile =
424 (
425 val => \&defvar,
426 def => \&defun,
427 application => \&apply,
428 while => \&awhile,
429 if => \&ifelse,
430 compose => \&compose,
431 bu => \&do_bu,
432 insert => \&insert,
433 forall => \&forall,
434 distribute => \&distribute,
435 constant => \&constant,
436 11 ▶ sfunc => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
437 11 ▶ my $x = $_[0] || die "sfunc($#_): (@_)";
Condition50%1/2
$_[0] || die("sfunc($#_): (@_)")
l!l
110
MC/DC0%0/1
$_[0] || die("sfunc($#_): (@_)")
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
438 1026 ▶ sub { $_[$x - 1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
439 11 },
440 33 ▶ id => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
441 33 my $ret = findsym($_[0], 'CODE');
442 33 ▶ unless ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 33 33
443 0 warn "Undefined function $_[0].";
444 0 return \&BOTTOM;
445 }
446 33 $ret;
447 },
448 52 ▶ data => sub { @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
449 3 ▶ id_undef => sub { shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
450 27 ▶ op => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
451 27 ▶ confess "unknown operator '$_[0]'" unless exists $ops{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ops{$_[0]} 0 27 27
452 27 return $ops{$_[0]};
453 }
454 1 );
455  
456 431 ▶sub closure_compile { # internal compiler function
Subroutine closure_compile: called CC=3 SCAR=11.1
Pod undocumented
457 431 my $tree = shift;
458 431 ▶ if (ref $tree ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree ne "HASH") 126 305 431
459 126 return $tree;
460 }
461 305 my $type = $tree->{type};
462 305 ▶ if (exists $compile{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $compile{$type}) { } 305 0 305
463 305 my @args = map { closure_compile($_) } @{$tree->{val}};
464 305 return $compile{$type}->(@args);
465 } else {
466 0 die "Can't handle $tree (type = $type)";
467 }
468 }
469  
470 34 ▶sub CLOSURE_compile { # external compiler function
Subroutine CLOSURE_compile: called CC=1 SCAR=0.0
Pod undocumented
471 34 make_binary_ops;
472 34 closure_compile(@_);
473 }
474  
475 ######################################################################
476 # The "Big Heinous Eval" compiler.
477  
478 =for comment
479  
480 Since Perl's sub calls are slow, I decided to try compiling FP def's
481 down to single, heinous Perl functions. As I suspected, this turns
482 out to be much faster than the other implementation, though debugging
483 is much more of a challenge.
484  
485 Each code generating function should return an expression that will
486 evaluate to its result in list context, and that has enough parens
487 around it to avoid confusing Perl's parser.
488  
489 The functions should use temporaries where necessary to avoid
490 evaluating any of its arguments more than once. These temporaries
491 cannot be references, since the arguments generally won't be real
492 arrays, but expressions producing them.
493  
494 =cut
495  
496 114 ▶sub seq($) { # Turn a sequence into an expression
Subroutine seq: called CC=1 SCAR=0.0
Pod undocumented
497 114 return '(do {
498 '.$_[0].'
499 })';
500 }
501  
502 1 my $gen = 0;
503 62 ▶sub gensym { # yep.
Subroutine gensym: called CC=2 SCAR=6.9
Pod undocumented
504 62 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 7 55 62
505 7 return map { $_.'_'.++$gen } @_;
506 } else {
507 55 return $_[0].'_'.++$gen;
508 }
509 }
510  
511 my %bhe_compile =
512 (
513 0 ▶ val => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
514 0 my ($name, $val, $rhs) = @_;
515 0 $name = bhe_compile($name, undef);
516 0 eval '@'.pkg()."::$name = ".bhe_compile($val, $rhs).";'ok';";
517 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
518 0 ▶ warn "Compilation error in $name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
519 0 BOTTOM;
520 } else {
521 0 q{'ok'};
522 }
523 },
524 3 ▶ def => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
525 3 my ($name, $val, $rhs) = @_;
526 3 my $BODY = bhe_compile($val, $rhs);
527 3 my $NAME = pkg().'::'.bhe_compile($name, undef);
528 3 Dparse "\n---\n", $body, "\n---\n";
529 3 ▶ eval "sub $NAME { $BODY }";
Subroutine f: called CC=3 SCAR=11.0
Subroutine f: called CC=3 SCAR=11.0
Subroutine f2: called CC=3 SCAR=11.0
Branch: 6 branches
BranchTrueFalseTotal
if ((do { my(@OP_28) = do { my(@DISTRIBUTE_27) = @IF_26; to_arrayref(&as_array(scalar do { $VAR1 = '2' })), to_arrayref(&id(@DISTRIBUTE_27)) }; $OP_28[0] < $OP_28[1] })[0]) { } 1 2 3
if ((do { my(@OP_31) = do { my(@DISTRIBUTE_30) = @IF_29; to_arrayref(&as_array(scalar do { $VAR1 = '1' })), to_arrayref(&id(@DISTRIBUTE_30)) }; $OP_31[0] < $OP_31[1] })[0]) { } 1 1 2
if (my $WHILE_33 = (do { my(@OP_36) = do { my(@DISTRIBUTE_35) = @WHILE_34; to_arrayref(&as_array(scalar do { $VAR1 = 0 })), to_arrayref((@DISTRIBUTE_35)[0]) }; $OP_36[0] < $OP_36[1] })[0]) 8 2 10
if bottom(@WHILE_34) 0 8 8
if (my $WHILE_43 = (do { my(@OP_46) = do { my(@DISTRIBUTE_45) = @WHILE_44; to_arrayref(&as_array(scalar do { $VAR1 = 0 })), to_arrayref((@DISTRIBUTE_45)[0]) }; $OP_46[0] < $OP_46[1] })[0]) 8 2 10
if bottom(@WHILE_44) 0 8 8
530 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
531 0 ▶ warn "Compilation error in $NAME: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
532 0 BOTTOM;
533 } else {
534 3 q{'ok'};
535 }
536 },
537 31 ▶ application => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
538 31 my ($func, $args, $rhs) = @_;
539 31 my $arg = bhe_compile($args, undef);
540 31 return bhe_compile($func, $arg);
541 },
542 3 ▶ while => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
543 3 my ($while, $do, $rhs) = @_;
544 3 my ($test, $res) = gensym '$WHILE', '@WHILE';
545 3 my $WHILE = bhe_compile($while, $res);
546 3 my $DO = bhe_compile($do, $res);
547 3 seq <<END;
548 my $res = $rhs;
549 while (my $test = ($WHILE)[0]) {
550 $res = $DO;
551 return BOTTOM if bottom($res);
552 }
553 $res;
554 END
555 },
556 4 ▶ if => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
557 4 my ($if, $then, $else, $rhs) = @_;
558 4 my $x = gensym '@IF';
559 my ($IF, $THEN, $ELSE)
560 4 = map { bhe_compile($_, $x) } ($if, $then, $else);
561 4 seq <<END;
562 my $x = $rhs;
563 if (($IF)[0]) {
564 $THEN;
565 } else {
566 $ELSE;
567 }
568 END
569 },
570 89 ▶ compose => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
571 89 my $rhs = pop;
572 89 my @funcs = @_;
573 89 my $ret = $rhs;
574 89 ▶ while (my $x = pop @funcs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = pop @funcs) 116 89 205
575 116 $ret = bhe_compile($x, $ret);
576 }
577 89 $ret;
578 },
579 2 ▶ bu => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
580 2 my ($f, $a, $rhs) = @_;
581 2 my $A = bhe_compile($a, undef);
582 2 return bhe_compile($f, "($A, $rhs)");
583 },
584 4 ▶ insert => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
585 4 my ($f, $rhs) = @_;
586 4 my ($r, $x, $xs) = gensym '$INSERT', '$INSERT', '@INSERT';
587 4 my $DOIT = bhe_compile($f, "($r, $x)");
588 4 return seq <<END;
589 my $xs = $rhs;
590 if ($xs) {
591 my $r = shift $xs;
592 foreach my $x ($xs) {
593 $r = ($DOIT)[0];
594 return Language::FP::BOTTOM if Language::FP::bottom($r);
595 }
596 $r;
597 } else {
598 (); # nothing to insert
599 }
600 END
601 },
602 6 ▶ forall => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
603 6 my ($f, $rhs) = @_;
604 6 my $v = gensym '@FORALL';
605 6 my $body = bhe_compile($f, $v);
606 6 seq <<ENDS;
607 map {
608 my $v = Language::FP::as_array(\$_);
609 Language::FP::to_arrayref($body)
610 } $rhs
611 ENDS
612 },
613 18 ▶ distribute => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
614 18 my $rhs = pop;
615 18 my $args = gensym '@DISTRIBUTE';
616 18 my $ret = "my $args = $rhs;\n(";
617 $ret .= join ",\n\t", map {
618 18 'Language::FP::to_arrayref('.bhe_compile($_, $args).')'
619 } @_;
620 18 seq ($ret . ');');
621 },
622 19 ▶ constant => sub { return bhe_compile(shift, undef) },
Subroutine __ANON__: called CC=1 SCAR=0.0
623 11 ▶ sfunc => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
624 11 my ($x, $rhs) = @_;
625 11 --$x; # FP indices are one-based.
626 11 "(($rhs)[$x])";
627 },
628 33 ▶ id => sub {
Subroutine __ANON__: called CC=3 SCAR=13.6
629 33 my ($f, $rhs) = @_;
630 33 my ($code, $fullname) = findsym($f, 'CODE');
631 33 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 33 33
632 0 warn "Undefined function $f.";
633 0 return 'return BOTTOM';
634 }
635 33 ▶ unless ($fullname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fullname) 0 33 33
636 0 warn "Anonymous sub not supported\n";
637 0 return 'return BOTTOM';
638 }
639 33 return '&{'.$fullname.'}('.$rhs.')';
640 },
641 52 ▶ data => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
642 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
643 52 pop; # get rid of rhs.
644 52 return 'Language::FP::as_array('.seq(Dumper(to_arrayref @_)).')';
645 },
646 3 ▶ id_undef => sub { shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
647 27 ▶ op => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
648 27 my ($op, $rhs) = @_;
649 27 my $res = gensym 'OP';
650 27 seq "my \@$res = $rhs; \$$res\[0] $op \$$res\[1]";
651 }
652 1 );
653  
654 34 ▶sub BHE_compile { # bootstrap function for BHE
Subroutine BHE_compile: called CC=2 SCAR=7.0
Pod undocumented
655 34 my $compiled = bhe_compile(@_, '@_');
656 34 Dparse "---\n$compiled\n---\n";
657 34 my @ret = eval $compiled;
658 34 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 34 34
659 34 @ret;
660 }
661  
662 305 ▶sub bhe_compile { # Internal BHE compile function
Subroutine bhe_compile: called CC=3 SCAR=11.8
Pod undocumented
663 305 my $tree = shift;
664 305 my $rhs = shift;
665 305 ▶ if (ref $tree ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree ne "HASH") 0 305 305
666 # Terminals should never call bhe_compile
667 0 die;
668 }
669 305 my $type = $tree->{type};
670 305 ▶ if (exists $compile{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $compile{$type}) { } 305 0 305
671 305 return $bhe_compile{$type}->(@{$tree->{val}}, $rhs);
672 } else {
673 0 die "Can't handle $tree (type = $type)";
674 }
675 }
676  
677 ######################################################################
678 ## Exportables:
679  
680 1 ▶sub import {
Subroutine import: called CC=3 SCAR=16.5
681 1 Language::FP->export_to_level(1, @_);
682  
683 # XXX: maybe consider autoloading these?
684  
685 # Build op-functions.
686 1 ▶ while (my ($f, $b) = each %op_guts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %op_guts) 22 1 23
687 0 ▶ *{$f} = eval qq{ sub { return BOTTOM if bottom(\@_); $b }};
Subroutine __ANON__: called CC=7 SCAR=30.2
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=2 SCAR=11.3
Subroutine __ANON__: called CC=4 SCAR=21.3
Subroutine __ANON__: called CC=2 SCAR=11.3
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=2 SCAR=11.3
Subroutine __ANON__: called CC=2 SCAR=11.3
Subroutine __ANON__: called CC=2 SCAR=11.3
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: called CC=2 SCAR=11.3
Subroutine __ANON__: called CC=2 SCAR=11.3
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=2 SCAR=11.3
Subroutine __ANON__: called CC=4 SCAR=21.3
Subroutine __ANON__: not called CC=2 SCAR=11.3
Subroutine __ANON__: not called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Branch: 37 branches
BranchTrueFalseTotal
if bottom(@_) 0 2 2
unless @_ 0 2 2
unless ref $_ eq "ARRAY" and @$_ == $len 0 2 2
if ($i < $len) 6 2 8
if bottom(@_) 0 0 0
@_ ? : 0 0 0
if bottom(@_) 0 0 0
@_ == 1 and ref $_[0] eq "SCALAR" 0 0 0
if bottom(@_) 0 0 0
@_ ? : 0 0 0
if bottom(@_) 0 0 0
@_ ? : 0 0 0
if bottom(@_) 0 0 0
if bottom(@_) 0 4 4
unless CORE::not bottom($b) and ref $a eq "ARRAY" 0 4 4
if bottom(@_) 0 342 342
if bottom(@_) 0 0 0
$_[0] and $_[1] 0 0 0
if bottom(@_) 0 0 0
if bottom(@_) 0 576 576
if bottom(@_) 0 2 2
if bottom(@_) 0 0 0
@_ ? : 0 0 0
if bottom(@_) 0 0 0
if bottom(@_) 0 0 0
if bottom(@_) 0 4 4
if bottom(@_) 0 4 4
if bottom(@_) 0 0 0
@_ ? : 0 0 0
if bottom(@_) 0 0 0
if bottom(@_) 0 6 6
unless CORE::not bottom($a) and ref $b eq "ARRAY" 0 6 6
if bottom(@_) 0 0 0
if bottom(@_) 0 0 0
$_[0] or $_[1] 0 0 0
if bottom(@_) 0 0 0
@_ ? : 0 0 0
Condition33%3/9
ref $_ eq "ARRAY" and @$_ == $len
!ll&&!rl&&r
002
CORE::not bottom($b) and ref $a eq "ARRAY"
!ll&&!rl&&r
004
CORE::not bottom($a) and ref $b eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $_ eq "ARRAY" and @$_ == $len
ref $_ eq "ARRAY" @$_ == $len
MC/DC0%0/2
CORE::not bottom($b) and ref $a eq "ARRAY"
CORE::not bottom($b) ref $a eq "ARRAY"
MC/DC0%0/2
CORE::not bottom($a) and ref $b eq "ARRAY"
CORE::not bottom($a) ref $b eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_ eq "ARRAY"B= @$_ == $len
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= CORE::not bottom($b)B= ref $a eq "ARRAY"
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= CORE::not bottom($a)B= ref $b eq "ARRAY"
ABresult
0X0
100
111
688 22 ▶ die "$f: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
689 }
690 1 1;
691 }
692  
693 164 ▶sub perl2fp {
Subroutine perl2fp: called CC=7 SCAR=20.5
Pod documented
694 164 my @ret;
695 164 foreach (@_) {
696 224 ▶ if (ref eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 96 128 224
elsif (ref $_) { } 0 128 128
elsif (/$RE{'num'}{'int'}/o or /$RE{'num'}{'real'}/o) { } 98 30 128
elsif (defined $_) { } 30 0 30
697 96 push @ret, '<'.perl2fp(@$_).'>';
698 } elsif (ref) {
699 0 die "Expecting ARRAY, got ".ref;
700 ▶ } elsif (/$RE{num}{int}/o || /$RE{num}{real}/o) {
Condition66%2/3
/$RE{"num"}{"int"}/o or /$RE{"num"}{"real"}/o
l!l&&r!l&&!r
98030
MC/DC50%1/2
/$RE{"num"}{"int"}/o or /$RE{"num"}{"real"}/o
/$RE{"num"}{"int"}/o /$RE{"num"}{"real"}/o
Truth table66%2/3
A or B
A= /$RE{"num"}{"int"}/oB= /$RE{"num"}{"real"}/o
ABresult
1X1
011
000
701 98 push @ret, $_;
702 } elsif (defined) {
703 30 push @ret, qq{"$_"};
704 } else {
705 0 push @ret, '_|_';
706 }
707 }
708 164 join(' ', @ret);
709 }
710  
711 0 ▶sub fp2perl {
Subroutine fp2perl: not called CC=1 SCAR=6.9
Pod documented
712 0 my $str = shift;
713 0 return to_arrayref($P->data($str));
714 }
715  
716 68 ▶sub fp_eval {
Subroutine fp_eval: called CC=8 SCAR=30.7
Pod documented
717 68 local $::fp_caller = caller;
718 68 my $p = get_parser;
719 68 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 68 0 68
720 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
721 68 my $parsed = $P->thing(shift);
722 68 ▶ unless ($parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 68 68
723 0 warn "Parse error";
724 0 return undef;
725 }
726 68 ▶ if ($::FP_DEBUG =~ /C/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::FP_DEBUG =~ /C/) { } 34 34 68
727 34 return [CLOSURE_compile($parsed)];
728 } else {
729 34 return [BHE_compile($parsed)];
730 }
731 }
732  
733 0 my %o = @_;
734 0 ▶ my $in = $o{in} || 'STDIN';
Condition0%0/2
$o{'in'} || 'STDIN'
l!l
00
MC/DC0%0/1
$o{'in'} || 'STDIN'
$o{'in'}
Truth table0%0/2
A
A= $o{'in'}
Aresult
11
00
735 0 ▶ my $out = $o{out} || 'STDOUT';
Condition0%0/2
$o{'out'} || 'STDOUT'
l!l
00
MC/DC0%0/1
$o{'out'} || 'STDOUT'
$o{'out'}
Truth table0%0/2
A
A= $o{'out'}
Aresult
11
00
736 0 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
737 0 chomp;
738 0 my $res = $P->thing($_);
739 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
740 0 warn;
741 0 next;
742 }
743 0 print $out perl2fp($res), "\n";
744 }
745 }
746  
747 1 1;
748  
749 __END__
↑ Summary