blib/lib/Lingy/Common.pm

filter: Statementstmt 87.4% 160 / 183 Branchbran 45.6% 21 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 56 / 60 Podpod 0.0% 0 / 19 totaltotal 76.9% 237 / 308 CC 25 SCAR 27.0
CC
25
Cov
79%
comp_pair
110.0
box_val
7.1
YYY
6.0
CRAP 14.9
« blib/lib/Lingy/Boolean.pm ↑ Summary lib/Lingy/String.pm »
1 60 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::Common;
3  
4 60 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 60 ▶use Scalar::Util qw'refaddr reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # RT is the RunTime class accessor function.
9 # 'Lingy::RT' or a subclass like 'YAMLScript::RT'.
10 BEGIN {
11 60 ▶ *RT = sub { 'Lingy::RT' } unless defined &RT;
Subroutine BEGIN: called CC=2 SCAR=7.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Lingy::Common::RT 60 0 60
12 }
13  
14 # Base type classes:
15 60 ▶use constant LISTTYPE => 'Lingy::ListClass';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 60 ▶use constant SCALARTYPE => 'Lingy::ScalarClass';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 60 ▶use constant SEQUENTIAL => 'Lingy::Sequential';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Type classes:
20 60 ▶use constant ATOM => 'Lingy::Atom';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 60 ▶use constant BOOLEAN => 'Lingy::Boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 60 ▶use constant CHARACTER => 'Lingy::Character';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 60 ▶use constant CLASS => 'Lingy::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 60 ▶use constant CLOJURE => 'Lingy::Clojure';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 60 ▶use constant COMPILER => 'Lingy::Compiler';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 60 ▶use constant FUNCTION => 'Lingy::Fn';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 60 ▶use constant HASHMAP => 'Lingy::HashMap';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 60 ▶use constant HASHSET => 'Lingy::HashSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 60 ▶use constant KEYWORD => 'Lingy::Keyword';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 60 ▶use constant LIST => 'Lingy::List';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 60 ▶use constant MACRO => 'Lingy::Macro';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 60 ▶use constant NIL => 'Lingy::Nil';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 60 ▶use constant NUMBER => 'Lingy::Number';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 60 ▶use constant REGEX => 'Lingy::Regex';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 60 ▶use constant STRBUILD => 'Lingy::StringBuilder';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 60 ▶use constant STRING => 'Lingy::String';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 60 ▶use constant SYMBOL => 'Lingy::Symbol';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 60 ▶use constant SYSTEM => 'Lingy::System';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 60 ▶use constant UTIL => 'Lingy::Util';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 60 ▶use constant VECTOR => 'Lingy::Vector';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 60 ▶use constant VAR => 'Lingy::Var';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Exception classes:
44 60 ▶use constant EXCEPTION => 'Lingy::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 60 use constant ILLEGALARGUMENTEXCEPTION =>
46 60 ▶ 'Lingy::IllegalArgumentException';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Functionality classes:
49 60 ▶use constant NAMESPACE => 'Lingy::Namespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 60 ▶use constant NUMBERS => 'Lingy::Numbers';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 60 ▶use constant TERM => 'Lingy::Term';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 60 ▶use constant THREAD => 'Lingy::Thread';
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 BEGIN {
55 60 ▶ our @EXPORT = qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
56 OK
57  
58 $symbol_re
59 $namespace_re
60  
61 refaddr
62 reftype
63  
64 RT
65  
66 LISTTYPE
67 SCALARTYPE
68 SEQUENTIAL
69  
70 COMPILER
71 NAMESPACE
72 NUMBERS
73 TERM
74 THREAD
75 UTIL
76  
77 ATOM
78 BOOLEAN
79 CHARACTER
80 CLASS
81 CLOJURE
82 FUNCTION
83 HASHMAP
84 HASHSET
85 KEYWORD
86 LIST
87 MACRO
88 NIL
89 NUMBER
90 REGEX
91 STRBUILD
92 STRING
93 SYMBOL
94 SYSTEM
95 VAR
96 VECTOR
97  
98 EXCEPTION
99 ILLEGALARGUMENTEXCEPTION
100  
101 list
102 string
103 symbol
104  
105 has
106 err
107 box_val
108 unbox_val
109 assert_args
110 comp_pair
111 nil
112 false
113 true
114  
115 Dump
116 PPP
117 WWW
118 XXX
119 YYY
120 ZZZ
121 >;
122 }
123  
124 {
125 60 my ($n, $t, $f);
126 60 ($n, $t, $f) = (1, 1, 0);
127 60 my $nil = bless \$n, 'Lingy::Nil';
128 60 my $true = bless \$t, BOOLEAN;
129 60 my $false = bless \$f, BOOLEAN;
130 1857 ▶ sub nil { $nil }
Subroutine nil: called CC=1 SCAR=0.0
Pod undocumented
131 4576 ▶ sub true { $true }
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
132 21157 ▶ sub false { $false }
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
133 }
134  
135 60 our $namespace_re = qr{(?:
136 \w+
137 (?:\.\w+)*
138 )}x;
139  
140 60 our $symbol_re = qr{(
141 \*?[-\w]+[\?\!\*\#\=]? |
142 [-+*/<>] |
143 ==? |
144 <= |
145 >=|
146 ->>?
147 )}x;
148  
149 0 ▶sub OK { $Lingy::RT::OK }
Subroutine OK: not called CC=1 SCAR=6.9
Pod undocumented
150  
151 166529 ▶sub list { LIST->new(@_) }
Subroutine list: called CC=1 SCAR=0.0
Pod undocumented
152 504 ▶sub string { STRING->new(@_) }
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
153 186833 ▶sub symbol { SYMBOL->new(@_) }
Subroutine symbol: called CC=1 SCAR=0.0
Pod undocumented
154  
155 sub has {
156 60 ▶ my ($caller) = caller;
Subroutine has: called CC=1 SCAR=0.0
Pod undocumented
157 60 my $name = shift;
158 my $method =
159 sub {
160 $#_
161 ? $_[0]{$name} = $_[1]
162 9509 ▶ : $_[0]{$name};
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 0 9509 9509
163 60 };
164 60 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
165 60 *{"${caller}::$name"} = $method;
166 };
167  
168 60 our $error_prefix = '';
169 sub err {
170 50 ▶ my $msg = shift;
Subroutine err: called CC=4 SCAR=13.9
Pod undocumented
171 50 $msg = sprintf $msg, @_;
172  
173 # XXX This is needed to keep the mal tests passing for now.
174 50 ▶ $error_prefix = 'Error:' if $ENV{LINGY_TEST};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LINGY_TEST"} 47 3 50
175  
176 50 ▶ if ($error_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lingy::Common::error_prefix) 49 1 50
177 49 ▶ $msg = $error_prefix .
Branch: 1 branch
BranchTrueFalseTotal
$msg =~ /\n./ ? : 0 49 49
178 ($msg =~ /\n./ ? "\n" : ' ') .
179 $msg;
180 }
181  
182 50 die "$msg\n";
183 }
184  
185 sub box_val {
186 map {
187 28 ▶ my $o = $_;
Subroutine box_val: called CC=6 SCAR=19.6
Pod undocumented
188 28 my $type = ref($o);
189 28 ▶ if (not($type)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $type) { } 7 21 28
elsif ($type eq 'HASH') { } 0 21 21
elsif ($type eq 'ARRAY') { } 0 21 21
elsif ($type =~ /^(?:SCALAR|REF|Regexp)$/) { } 0 21 21
190 7 ▶ /^\-?\d+$/ ? NUMBER->new($o) : STRING->new($o);
Branch: 1 branch
BranchTrueFalseTotal
/^\-?\d+$/ ? : 6 1 7
191 }
192 elsif ($type eq 'HASH') {
193 0 HASHMAP->new([
194 map box_val($_), %$o
195 ]);
196 }
197 elsif ($type eq 'ARRAY') {
198 0 VECTOR->new([
199 map box_val($_), %$o
200 ]);
201 }
202 elsif ($type =~ /^(?:SCALAR|REF|Regexp)$/) {
203 0 XXX($o, "Lingy can't box this object yet");
204 } else {
205 21 $o;
206 }
207 } @_;
208 }
209  
210 sub unbox_val {
211 13 ▶ my ($obj) = @_;
Subroutine unbox_val: called CC=2 SCAR=6.9
Pod undocumented
212 13 ▶ ref($obj) =~ /^
Branch: 1 branch
BranchTrueFalseTotal
ref($obj) =~ /^ Lingy::( String|Number|Boolean|Nil|HashMap|Vector|Fn ) $/x ? : 4 9 13
213 Lingy::(
214 String|Number|Boolean|Nil|HashMap|Vector|Fn
215 )
216 $/x ? $obj->unbox : $obj;
217 }
218  
219 sub assert_args {
220 8 ▶ my $args = shift;
Subroutine assert_args: called CC=3 SCAR=11.0
Pod undocumented
221 8 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 8 7 15
222 8 ▶ if (ref($args->[$i]) ne $_[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[$i] ne $_[$i]) 1 7 8
223 1 my (undef, undef, undef, $fn) = caller(1);
224 1 err "Arg %d for '%s' must be '%s', not '%s'",
225 $i, $fn, $_[$i], ref($args->[$i]);
226 }
227 }
228 }
229  
230 sub comp_pair {
231 0 ▶ my ($x, $y) = @_;
Subroutine comp_pair: not called CC=10 SCAR=47.0
Pod undocumented
232 0 ▶ if (ref($x) eq NIL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq "Lingy::Nil") 0 0 0
233 0 ▶ return ref($y) eq NIL ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
ref $y eq 'Lingy::Nil' ? : 0 0 0
234 }
235 0 ▶ return 1 if ref($y) eq NIL;
Branch: 1 branch
BranchTrueFalseTotal
if ref $y eq "Lingy::Nil" 0 0 0
236 0 ▶ ref($x) eq ref($y) or
Branch: 1 branch
BranchTrueFalseTotal
ref $x eq ref $y or err("Can't compare values of type '%s' and '%s'", ref $x, ref $y) 0 0 0
237 err "Can't compare values of type '%s' and '%s'",
238 ref($x), ref($y);
239 0 ▶ if (ref($x) eq VECTOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq "Lingy::Vector") 0 0 0
240 0 ▶ return @$x cmp @$y unless @$x == @$y;
Branch: 1 branch
BranchTrueFalseTotal
unless @$x == @$y 0 0 0
241 0 my $i = 0;
242 0 for my $e (@$x) {
243 0 ▶ return 1 if $i > @$y;
Branch: 1 branch
BranchTrueFalseTotal
if $i > @$y 0 0 0
244 0 my $r = comp_pair($x->[$i], $y->[$i]);
245 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
246 0 $i++;
247 }
248 0 return 0;
249 }
250 0 "$x" cmp "$y";
251 }
252  
253 sub Dump {
254 3 ▶ _dump(@_);
Subroutine Dump: called CC=1 SCAR=0.0
Pod undocumented
255 }
256 sub PPP {
257 1 ▶ require Lingy::Printer;
Subroutine PPP: called CC=1 SCAR=0.0
Pod undocumented
258 1 die _dump(RT->printer->pr_str(@_));
259 }
260 sub WWW {
261 1 ▶ warn _dump(@_);
Subroutine WWW: called CC=2 SCAR=7.2
Pod undocumented
262 1 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
263 }
264 sub XXX {
265 2 ▶ die _dump(@_);
Subroutine XXX: called CC=1 SCAR=0.0
Pod undocumented
266 }
267 sub YYY {
268 0 ▶ print _dump(@_);
Subroutine YYY: not called CC=2 SCAR=17.9
Pod undocumented
269 0 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
270 }
271 sub ZZZ {
272 0 ▶ require Carp;
Subroutine ZZZ: not called CC=1 SCAR=6.9
Pod undocumented
273 0 Carp::confess _dump(@_);
274 }
275 sub _dump {
276 7 ▶ require YAML::PP;
Subroutine _dump: called CC=1 SCAR=0.0
277 7 YAML::PP->new(
278 schema => ['Core', 'Perl', '-dumpcode']
279 )->dump_string(@_) . "...\n";
280 }
281  
282 60 1;
« blib/lib/Lingy/Boolean.pm ↑ Summary lib/Lingy/String.pm »