blib/lib/Lingy/Fn.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 100 CC 17 SCAR 53.8
CC
17
Cov
0%
new
306.0
clone
2.0
unbox
2.0
CRAP 217.3
1 use strict; use warnings;
2 package Lingy::Fn;
3  
4 use base 'Lingy::Class';
5  
6 use Lingy::Common;
7  
8 use Sub::Name 'subname';
9  
10 *list = \&Lingy::Common::list;
11 *symbol = \&Lingy::Common::symbol;
12 sub err;
13 *err = \&Lingy::Common::err;
14  
15 sub new {
16 my ($class, $ast, $env) = @_;
17  
18 my (undef, @exprs) = @$ast;
19  
20 my $name = \'AFn';
21 $name = shift @exprs if ref($exprs[0]) eq SYMBOL;
22  
23 shift @exprs if ref($exprs[0]) eq STRING;
24 shift @exprs if ref($exprs[0]) eq HASHMAP;
25  
26 @exprs = (list([@exprs]))
27 if ref($exprs[0]) eq VECTOR;
28  
29 my $functions = [];
30 my $variadic = '';
31  
32 for my $expr (@exprs) {
33 err "fn expr is not a list"
34 unless ref($expr) eq LIST;
35 my ($sig, @body) = @$expr;
36 err "fn signature not a vector"
37 unless ref($sig) eq VECTOR;
38 my $arity = (grep {$$_ eq '&'} @$sig) ? -1 : @$sig;
39 if ($arity == -1) {
40 $variadic = @$sig - 1;
41 } elsif ($variadic) {
42 err "Can't have fixed arity function " .
43 "with more params than variadic function"
44 if @$sig > $variadic;
45 }
46 @body = (list([ symbol('do'), @body ]))
47 if @body > 1;
48 if (exists $functions->[$arity+1]) {
49 err $arity == -1
50 ? "Can't have more than 1 variadic overload"
51 : "Can't have 2 overloads with same arity";
52 }
53 $functions->[$arity+1] = [$sig, @body];
54 }
55  
56 subname $name => bless sub {
57 my $arity = @_;
58 my $function =
59 $functions->[$arity+1] ? $functions->[$arity+1] :
60 $arity >= (@$functions-1) ? $functions->[0] : undef;
61 err "Wrong number of args ($arity) passed to function"
62 unless defined $function;
63 my ($sig, $ast) = @$function;
64  
65 return (
66 $ast,
67 Lingy::Env->new(
68 outer => $env,
69 binds => $sig,
70 exprs => \@_,
71 ),
72 );
73 }, $class;
74 }
75  
76 sub clone {
77 my ($fn) = @_;
78 bless sub { goto &$fn }, ref($fn);
79 }
80  
81 sub unbox { $_[0] }
82  
83 1;