blib/lib/Lingy/Evaluator.pm

filter: Statementstmt 95.6% 243 / 254 Branchbran 84.0% 121 / 144 Conditioncond 71.4% 45 / 63 MC/DCmcdc 54.5% 18 / 33 Subroutinesub 93.5% 29 / 31 Podpod 0.0% 0 / 26 totaltotal 82.7% 456 / 551 CC 97 SCAR 22.1
CC
97
Cov
89%
special_dot
19.9
evaluate
12.2
special_try
11.8
CRAP 9.1
« lib/Lingy/Fn.pm ↑ Summary lib/Lingy/Util.pm »
1 56 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::Evaluator;
3  
4 56 ▶use Lingy::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 56 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 56 our @EXPORT = qw(
9 evaluate
10 );
11  
12 # Lingy Special Forms:
13 56 my %special_dispatch = (
14 'def' => \&special_def,
15 'defmacro!' => \&special_defmacro,
16 'do' => \&special_do,
17 '.' => \&special_dot,
18 'fn*' => \&special_fn,
19 'if' => \&special_if,
20 'import*' => \&special_import,
21 'let*' => \&special_let,
22 'loop' => \&special_loop,
23 'new' => \&special_new,
24 'recur' => \&special_recur,
25 'quasiquote' => \&special_quasiquote,
26 'quasiquoteexpand' => \&special_quasiquoteexpand,
27 'quote' => \&special_quote,
28 'try*' => \&special_try,
29 'throw' => \&special_throw,
30 'var' => \&special_var,
31 );
32  
33 0 ▶sub special_symbols {
Subroutine special_symbols: not called CC=1 SCAR=6.9
Pod undocumented
34 0 keys %special_dispatch;
35 }
36  
37  
38 # Main eval functions:
39 56 our $ENV;
40 478592 ▶sub evaluate {
Subroutine evaluate: called CC=12 SCAR=25.0
Pod undocumented
41 478592 my ($ast, $env) = @_;
42 478592 $ENV = $env;
43  
44 478592 while (1) {
45 599105 $ast = macroexpand($ast, $env);
46  
47 599105 ▶ return nil unless defined $ast;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ast 1 599104 599105
48  
49 599104 ▶ return evaluate_ast($ast, $env) unless ref($ast) eq LIST;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ast eq "Lingy::List" 329416 269688 599104
50  
51 269688 ▶ return $ast unless @$ast; # Empty list
Branch: 1 branch
BranchTrueFalseTotal
unless @$ast 16269 253419 269688
52  
53 253419 ▶ if (my $fn = $special_dispatch{$ast->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $special_dispatch{$ast->[0]}) { } 106520 146899 253419
54 106520 ($ast, $env) = $fn->($ast, $env);
55 106488 ▶ return $ast unless $env;
Branch: 1 branch
BranchTrueFalseTotal
unless $env 76321 30167 106488
56  
57 } else {
58 146899 my ($fn, @args) = @{evaluate_ast($ast, $env)};
59 146893 ▶ return $fn->(@args) if ref($fn) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fn eq "CODE" 56505 90388 146893
60  
61 90388 ▶ while ((my $ref = ref($fn)) ne FUNCTION) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ref = ref $fn) ne "Lingy::Fn") 40 90351 90391
62 40 ▶ if ($ref eq VAR) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq 'Lingy::Var') { } 3 37 40
elsif ($ref eq 'Lingy::Keyword') { } 30 7 37
elsif ($ref eq 'Lingy::Vector') { } 7 0 7
elsif ($ref eq 'Lingy::Nil') { } 0 0 0
63 3 $fn = $env->get($$fn);
64  
65 } elsif ($ref eq KEYWORD) {
66 30 return special_keyword($env, $fn, @args);
67  
68 } elsif ($ref eq VECTOR) {
69 7 return special_vector($env, $fn, @args);
70  
71 } elsif ($ref eq NIL) {
72 0 return $args[0];
73  
74 } else {
75 0 err "Can't use '$ref' object as function";
76 }
77 }
78 90351 ($ast, $env) = $fn->(@args);
79 }
80 }
81 }
82  
83 476407 ▶sub evaluate_ast {
Subroutine evaluate_ast: called CC=5 SCAR=16.1
Pod undocumented
84 476407 my ($ast, $env) = @_;
85  
86 476407 ▶ $ast->isa(SYMBOL)
Branch: 4 branches
BranchTrueFalseTotal
$ast->isa('Lingy::Symbol') ? : 284926 191481 476407
$ast->isa('Lingy::ListClass') ? : 147114 44367 191481
$ast->isa('Lingy::HashMap') ? : 224 44143 44367
$ast->isa('Lingy::HashSet') ? : 11 44132 44143
87 ? $env->get($$ast) :
88 $ast->isa(LISTTYPE)
89 ? ref($ast)->new([ map evaluate($_, $env), @$ast ]) :
90 $ast->isa(HASHMAP)
91 ? ref($ast)->new([map evaluate($_, $env), %$ast]) :
92 $ast->isa(HASHSET)
93 ? ref($ast)->new([map evaluate($_, $env), %$ast]) :
94 $ast;
95 }
96  
97  
98 # Special form handler functions:
99 7183 ▶sub special_def {
Subroutine special_def: called CC=5 SCAR=16.3
Pod undocumented
100 7183 my ($ast, $env) = @_;
101 7183 my (undef, $sym, $form) = @$ast;
102 7183 ▶ $sym // err "Too few arguments to def";
Condition33%1/3
$sym // err("Too few arguments to def")
l!l
718300
MC/DC0%0/2
$sym // err("Too few arguments to def")
$sym err("Too few arguments to def")
Truth table33%1/3
A // B
A= $symB= err("Too few arguments to def")
ABresult
1X1
011
000
103 7183 ▶ $form //= nil;
Condition66%2/3
$form //= nil()
l!l&&r!l&&!r
717940
MC/DC0%0/2
$form //= nil()
$form nil()
Truth table66%2/3
A //= B
A= $formB= nil()
ABresult
1X1
011
000
104 7183 ▶ err "Can't def a qualified symbol: '$sym'"
Branch: 1 branch
BranchTrueFalseTotal
if $sym =~ m[./.] 1 7182 7183
105 if $sym =~ m{./.};
106  
107 7182 my $new = evaluate($form, $env);
108 7179 ▶ if (my $meta = RT->meta->{$form}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = RT()->meta->{$form}) 9 7170 7179
109 9 $new = $new->clone;
110 9 RT->meta->{"$new"} = $meta;
111 }
112  
113 7179 RT->current_ns->set($$sym, $new);
114 }
115  
116 1088 ▶sub special_defmacro {
Subroutine special_defmacro: called CC=1 SCAR=0.0
Pod undocumented
117 1088 my ($ast, $env) = @_;
118 1088 my (undef, $sym, $form) = @$ast;
119 1088 RT->current_ns->set($$sym, MACRO->new(evaluate($form, $env)));
120 }
121  
122 92 ▶sub special_do {
Subroutine special_do: called CC=1 SCAR=0.0
Pod undocumented
123 92 my ($ast, $env) = @_;
124 92 my (undef, @do) = @$ast;
125 92 $ast = pop @do;
126 92 evaluate_ast(list(\@do), $env);
127 90 return ($ast, $env);
128 }
129  
130 # TODO This function needs more attention and testing.
131 41363 ▶sub special_dot {
Subroutine special_dot: called CC=19 SCAR=29.9
Pod undocumented
132 41363 my ($ast, $env) = @_;
133  
134 41363 my (undef, $target, @args) = @$ast;
135 41363 ▶ if (@args == 1 and ref($args[0]) eq LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "Lingy::List") 41285 78 41363
Condition100%3/3
@args == 1 and ref $args[0] eq "Lingy::List"
!ll&&!rl&&r
364241285
MC/DC100%2/2
@args == 1 and ref $args[0] eq "Lingy::List"
@args == 1 ref $args[0] eq "Lingy::List"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "Lingy::List"
ABresult
0X0
100
111
136 41285 @args = @{$args[0]};
137 }
138  
139 ($target, @args) = map {
140 41363 ▶ $_->isa(LIST)
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Lingy::List') ? : 65 163234 163299
141 ? evaluate($_, $env) : $_;
142 } ($target, @args);
143  
144 41363 ▶ @args > 0 or err "Not enough args for . form";
Branch: 1 branch
BranchTrueFalseTotal
@args > 0 or err("Not enough args for . form") 41363 0 41363
145  
146 41363 ▶ if (my $class = is_class_symbol($target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = is_class_symbol($target)) 41215 148 41363
147 41215 ▶ if (RT->is_lingy_class($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (RT()->is_lingy_class($class)) { } 41205 10 41215
148 41205 $target = "${class}::${shift @args}";
149 @args = map {
150 41205 ▶ $_->isa(SYMBOL) ? $env->get($_) : $_;
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Lingy::Symbol') ? : 80429 10 80439
151 } @args;
152 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 41205 return &{$target}(@args);
154 }
155  
156 else {
157 10 ▶ my $method = shift(@args) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $method = shift @args or die 10 0 10
158 10 ▶ $method->isa(SYMBOL) or die;
Branch: 1 branch
BranchTrueFalseTotal
$method->isa("Lingy::Symbol") or die 10 0 10
159 10 $method = $$method;
160 @args = map {
161 10 ▶ unbox_val( $_->isa(SYMBOL) ? $env->get($_) : $_ );
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Lingy::Symbol') ? : 0 4 4
162 } @args;
163 10 return box_val $class->$method(@args);
164 }
165 }
166  
167 148 ▶ $target = $env->get($target)
Branch: 1 branch
BranchTrueFalseTotal
if $target->isa("Lingy::Symbol") 125 23 148
168 if $target->isa(SYMBOL);
169  
170 148 ▶ $target = $$target if ref($target) eq CLASS;
Branch: 1 branch
BranchTrueFalseTotal
if ref $target eq "Lingy::Class" 110 38 148
171  
172 148 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 148 0 148
173 148 my $member = shift(@args);
174 148 ▶ if ($target->isa(CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->isa("Lingy::Class")) 129 19 148
175 129 ▶ @args = map { $_->isa(SYMBOL)
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Lingy::Symbol') ? : 110 11 121
176 ? $env->get($_) : $_; } @args;
177  
178 129 ▶ if (not $target->can($member)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target->can($member)) 0 129 129
179 0 my $class = $target->NAME;
180 0 err "No matching field found: '$member' " .
181 "for class '$class'";
182 }
183 129 my $method = $$member;
184 129 return $target->$method(@args);
185 }
186  
187 # Test the unboxing of native call args:
188 19 ▶ if ($target->can($member)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->can($member)) 19 0 19
189 19 my $method = $$member;
190 @args = map {
191 19 ▶ unbox_val( $_->isa(SYMBOL) ? $env->get($_) : $_ );
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Lingy::Symbol') ? : 9 0 9
192 } @args;
193 19 return box_val $target->$method(@args);
194 }
195 }
196  
197 # XXX $target->isa(CLASS);
198 0 XXX $ast, "Don't know how to '.' this yet";
199 }
200  
201 7925 ▶sub special_fn {
Subroutine special_fn: called CC=1 SCAR=0.0
Pod undocumented
202 7925 my ($ast, $env) = @_;
203 7925 return FUNCTION->new($ast, $env);
204 }
205  
206 21572 ▶sub special_if {
Subroutine special_if: called CC=3 SCAR=11.0
Pod undocumented
207 21572 my ($ast, $env) = @_;
208 21572 my (undef, $cond, $then, $else) = @$ast;
209 21572 ▶ $ast = ${BOOLEAN->new(evaluate($cond, $env))} ? $then :
Branch: 2 branches
BranchTrueFalseTotal
${'Lingy::Boolean'->new(evaluate($cond, $env));} ? : 1228 20344 21572
defined $else ? : 20328 16 20344
210 defined $else ? $else : nil;
211 21572 return ($ast, $env);
212 }
213  
214 11 ▶sub special_import {
Subroutine special_import: called CC=8 SCAR=21.7
Pod undocumented
215 11 my ($ast, $env) = @_;
216  
217 11 my ($fn, @specs) = @$ast;
218  
219 11 my $return = nil;
220  
221 11 for my $spec (@specs) {
222 11 ▶ if (ref($spec) eq SYMBOL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq "Lingy::Symbol") 11 0 11
223 11 $spec = list([$spec]);
224 }
225  
226 err "Invalid import spec" unless
227 $spec->isa(LIST) and
228 @$spec > 0 and
229 11 ▶ not grep { ref($_) ne SYMBOL } @$spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->isa("Lingy::List") and @$spec > 0 and not grep({ref $_ ne "Lingy::Symbol";} @$spec) 0 11 11
Condition33%2/6
$spec->isa("Lingy::List") and @$spec > 0 and not grep({ref $_ ne "Lingy::Symbol";} @$spec)
!ll&&!rl&&r
0011
$spec->isa("Lingy::List") and @$spec > 0
!ll&&!rl&&r
0011
MC/DC0%0/3
$spec->isa("Lingy::List") and @$spec > 0 and not grep({ref $_ ne "Lingy::Symbol";} @$spec)
$spec->isa("Lingy::List") @$spec > 0 not grep({ref $_ ne "Lingy::Symbol";} @$spec)
Truth table25%1/4
A and B and not(C)
A= $spec->isa("Lingy::List")B= @$spec > 0C= not grep({ref $_ ne "Lingy::Symbol";} @$spec)
ABCresult
0XX0
10X0
1100
1111
230  
231 11 my ($module_name, $imports) = @$spec;
232 11 my $name = $$module_name;
233 11 (my $module = $name) =~ s/lingy\.lang\./Lingy::/;
234 11 $module =~ s/\./::/g;
235 11 ▶ eval "require $module; 1" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1" or die $@ 11 0 11
236 11 my $class = RT->current_ns->{$name} =
237 CLASS->_new($name);
238 11 ▶ if ($module->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->can("new")) 5 6 11
239 5 $return = $class;
240 }
241 # TODO - imports
242 }
243  
244 11 return $return;
245 }
246  
247 30 ▶sub special_keyword {
Subroutine special_keyword: called CC=3 SCAR=11.0
Pod undocumented
248 30 my ($env, $keyword, @args) = @_;
249 30 ▶ err "Wrong number of args (${\ scalar @args}) passed to: '$keyword'"
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 or @args == 2 3 27 30
Condition100%3/3
@args == 1 or @args == 2
l!l&&r!l&&!r
2253
MC/DC100%2/2
@args == 1 or @args == 2
@args == 1 @args == 2
Truth table100%3/3
A or B
A= @args == 1B= @args == 2
ABresult
1X1
011
000
250 unless @args == 1 or @args == 2;
251 27 my $map = shift @args;
252 27 evaluate(
253 list([
254 symbol('get'),
255 $map,
256 $keyword,
257 @args,
258 ]),
259 $env,
260 );
261 }
262  
263 594 ▶sub special_let {
Subroutine special_let: called CC=4 SCAR=13.9
Pod undocumented
264 594 my ($ast, $env) = @_;
265 594 my (undef, $bindings, $body) = @$ast;
266 594 ▶ err "First argument to 'let' must be a vector"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bindings eq "Lingy::Vector" 0 594 594
267 unless ref($bindings) eq VECTOR;
268 594 $env = Lingy::Env->new(outer => $env);
269 594 ▶ for (my $i = 0; $i < @$bindings; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$bindings) 646 594 1240
270 $env->set(
271 646 ${$bindings->[$i]},
272 evaluate($bindings->[$i+1], $env),
273 );
274 }
275 594 ▶ $body = list([symbol('do'), @{$ast}[2..(@$ast-1)] ])
Branch: 1 branch
BranchTrueFalseTotal
if @$ast > 3 5 589 594
276 if @$ast > 3;
277 594 return ($body, $env);
278 }
279  
280 83 ▶sub special_loop {
Subroutine special_loop: called CC=8 SCAR=21.3
Pod undocumented
281 83 my ($ast, $env) = @_;
282 83 my (undef, $bindings, $body) = @$ast;
283 83 ▶ if (not $env->{RECUR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env->{'RECUR'}) { } 24 59 83
284 24 ▶ err "First argument to 'loop' must be a vector"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bindings eq "Lingy::Vector" 0 24 24
285 unless ref($bindings) eq VECTOR;
286 24 $env = Lingy::Env->new(outer => $env);
287 24 my $binds = [];
288 24 ▶ for (my $i = 0; $i < @$bindings; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$bindings) 59 24 83
289 59 push @$binds, ${$bindings->[$i]};
290 $env->set(
291 59 ${$bindings->[$i]},
292 evaluate($bindings->[$i+1], $env),
293 );
294 }
295 24 ▶ $body //= nil;
Condition33%1/3
$body //= nil()
l!l&&r!l&&!r
2400
MC/DC0%0/2
$body //= nil()
$body nil()
Truth table33%1/3
A //= B
A= $bodyB= nil()
ABresult
1X1
011
000
296 0 ▶ $body = list([symbol('do'), @{$ast}[2..(@$ast-1)] ])
Branch: 1 branch
BranchTrueFalseTotal
if @$ast > 3 0 24 24
297 if @$ast > 3;
298 24 $env->{LOOP} = [$binds, $body];
299 } else {
300 59 my $binds;
301 59 ($binds, $body) = @{$env->{LOOP}};
302 59 ▶ if (@$bindings != @$binds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bindings != @$binds) 0 59 59
303 0 err sprintf
304 "Mismatched argument count to recur, " .
305 "expected: %d args, got: %d",
306 scalar(@$binds),
307 scalar(@$bindings);
308 }
309 59 my $i = 0;
310 59 for my $bind (@$binds) {
311 112 $env->set($bind, $bindings->[$i++], $env);
312 }
313 }
314 83 return ($body, $env);
315 }
316  
317 0 ▶sub special_new {
Subroutine special_new: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my ($ast, $env) = @_;
319 0 XXX @_, 'special_new not yet implemented';
320 }
321  
322 7761 ▶sub special_quasiquote {
Subroutine special_quasiquote: called CC=1 SCAR=0.0
Pod undocumented
323 7761 my ($ast, $env) = @_;
324 7761 return (quasiquote($ast->[1]), $env);
325 }
326  
327 34 ▶sub special_quasiquoteexpand {
Subroutine special_quasiquoteexpand: called CC=1 SCAR=0.0
Pod undocumented
328 34 my ($ast, $env) = @_;
329 34 return quasiquote($ast->[1]);
330 }
331  
332 18732 ▶sub special_quote {
Subroutine special_quote: called CC=1 SCAR=0.0
Pod undocumented
333 18732 my ($ast, $env) = @_;
334 18732 return $ast->[1];
335 }
336  
337 59 ▶sub special_recur {
Subroutine special_recur: called CC=1 SCAR=0.0
Pod undocumented
338 59 my ($ast, $env) = @_;
339 59 $env->{RECUR} = 1;
340 return (
341 list([symbol('loop'), VECTOR->new([
342 map evaluate($_, $env),
343 59 @{$ast}[1..(@$ast-1)]])
344 ]),
345 $env,
346 );
347 }
348  
349 11 ▶sub special_try {
Subroutine special_try: called CC=10 SCAR=24.7
Pod undocumented
350 11 my ($ast, $env) = @_;
351 11 my (undef, $body, $catch) = @$ast;
352 11 local $@;
353 11 my $val = eval { evaluate($body, $env) };
354 11 ▶ return $val unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 2 9 11
355 9 my $err = $@;
356 9 ▶ die ref($err) ? RT->printer->pr_str($err) : $err
Branch: 2 branches
BranchTrueFalseTotal
unless defined $catch 1 8 9
ref $err ? : 0 1 1
357 unless defined $catch;
358 err "Invalid 'catch' clause" unless
359 $catch and $catch->isa(LISTTYPE) and
360 @$catch and $catch->[0]->isa(SYMBOL) and
361 8 ▶ ${$catch->[0]} =~ /^catch\*?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $catch and $catch->isa("Lingy::ListClass") and @$catch and $catch->[0]->isa("Lingy::Symbol") and ${$catch->[0];} =~ /^catch\*?$/ 0 8 8
Condition33%4/12
$catch and $catch->isa("Lingy::ListClass") and @$catch and $catch->[0]->isa("Lingy::Symbol") and ${$catch->[0];} =~ /^catch\*?$/
!ll&&!rl&&r
008
$catch and $catch->isa("Lingy::ListClass") and @$catch and $catch->[0]->isa("Lingy::Symbol")
!ll&&!rl&&r
008
$catch and $catch->isa("Lingy::ListClass") and @$catch
!ll&&!rl&&r
008
$catch and $catch->isa("Lingy::ListClass")
!ll&&!rl&&r
008
MC/DC0%0/5
$catch and $catch->isa("Lingy::ListClass") and @$catch and $catch->[0]->isa("Lingy::Symbol") and ${$catch->[0];} =~ /^catch\*?$/
$catch $catch->isa("Lingy::ListClass") @$catch $catch->[0]->isa("Lingy::Symbol") ${$catch->[0];} =~ /^catch\*?$/
Truth table16%1/6
A and B and C and D and E
A= $catchB= $catch->isa("Lingy::ListClass")C= @$catchD= $catch->[0]->isa("Lingy::Symbol")E= ${$catch->[0];} =~ /^catch\*?$/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
362 8 my $e;
363 8 (undef, $e, $ast) = @$catch;
364 8 ▶ if (not ref($err)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $err) 2 6 8
365 2 chomp $err;
366 2 $err = string($err);
367 }
368 return (
369 8 $ast,
370 Lingy::Env->new(
371 outer => $env,
372 binds => [$e],
373 exprs => [$err],
374 ),
375 );
376 }
377  
378 10 ▶sub special_throw {
Subroutine special_throw: called CC=1 SCAR=0.0
Pod undocumented
379 10 my ($ast, $env) = @_;
380 10 require Carp;
381 10 Carp::confess(
382 evaluate($ast->[1], $env),
383 );
384 }
385  
386 2 ▶sub special_var {
Subroutine special_var: called CC=2 SCAR=6.9
Pod undocumented
387 2 my ($ast, $env) = @_;
388 2 my (undef, $name) = @$ast;
389 2 ▶ $env->get($name, 1)
Branch: 1 branch
BranchTrueFalseTotal
$env->get($name, 1) or err("Unable to resolve var: '${name}' in this context") 1 1 2
390 or err "Unable to resolve var: '$name' in this context";
391 1 return VAR->new($name);
392 }
393  
394 7 ▶sub special_vector {
Subroutine special_vector: called CC=2 SCAR=6.9
Pod undocumented
395 7 my ($env, $vector, @args) = @_;
396 7 ▶ err "Wrong number of args (${\ scalar @args}) passed to: " .
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 2 5 7
397 "'lingy.lang.Vector'"
398 unless @args == 1;
399 5 evaluate(
400 list([
401 symbol('nth'),
402 $vector,
403 @args,
404 ]),
405 $env,
406 );
407 }
408  
409  
410 # Helper functions:
411 41363 ▶sub is_class_symbol {
Subroutine is_class_symbol: called CC=5 SCAR=16.6
Pod undocumented
412 41363 my ($symbol) = @_;
413 41363 ▶ return unless ref($_[0]) eq SYMBOL;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "Lingy::Symbol" 23 41340 41363
414 41340 my $class = $$symbol;
415 41340 ▶ return unless $class =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /\./ 125 41215 41340
416 41215 $class =~ s/^lingy\.lang\./Lingy::/;
417 41215 $class =~ s/\./::/g;
418 41215 (my $path = "$class.pm") =~ s/::/\//g;
419 41215 ▶ if (not exists $INC{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$path}) 0 41215 41215
420 0 ▶ eval "require $class; 1" or
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class; 1" or err("Not a Lingy class: $class") 0 0 0
421 err "Not a Lingy class: $class";
422 }
423 41215 return $class;
424 }
425  
426 599123 ▶sub macroexpand {
Subroutine macroexpand: called CC=11 SCAR=24.0
Pod undocumented
427 599123 my ($ast, $env) = @_;
428 599123 my ($sym, $call);
429 # while ast is a macro call form
430 599123 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ast eq "Lingy::List" and $sym = $ast->[0] and ref $sym eq "Lingy::Symbol") 262762 345741 608503
Condition100%6/6
ref $ast eq "Lingy::List" and $sym = $ast->[0] and ref $sym eq "Lingy::Symbol"
!ll&&!rl&&r
34568952262762
ref $ast eq "Lingy::List" and $sym = $ast->[0]
!ll&&!rl&&r
32942016270262813
MC/DC100%3/3
ref $ast eq "Lingy::List" and $sym = $ast->[0] and ref $sym eq "Lingy::Symbol"
ref $ast eq "Lingy::List" $sym = $ast->[0] ref $sym eq "Lingy::Symbol"
Truth table100%4/4
A and B and C
A= ref $ast eq "Lingy::List"B= $sym = $ast->[0]C= ref $sym eq "Lingy::Symbol"
ABCresult
0XX0
10X0
1100
1111
431 ref($ast) eq LIST and
432 $sym = $ast->[0] and
433 ref($sym) eq SYMBOL
434 ) {
435 262762 $sym = $$sym;
436 262762 ▶ if ($sym =~ /^\.(\S+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ /^\.(\S+)$/) 24 262738 262762
437 24 my ($member, $instance, @rest) = @$ast;
438 24 $member = symbol(substr($$member, 1));
439 24 return list([symbol('.'), $instance, $member, @rest]);
440 }
441 262738 ▶ if ($sym =~ /^($namespace_re)\.$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ /^($Lingy::Evaluator::namespace_re)\.$/) 5 262733 262738
442 5 my (undef, @args) = @$ast;
443 # XXX Should expand to (new Foo.Bar 1 2 3)
444 5 return list([
445 symbol('.'),
446 symbol($1),
447 symbol('new'),
448 @args,
449 ]);
450 }
451 262733 ▶ if ($sym =~ m{^($namespace_re)/($symbol_re)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ m[^($Lingy::Evaluator::namespace_re)/($Lingy::Evaluator::symbol_re)$]) 21085 241648 262733
452 21085 my $namespace = $1;
453 21085 my $sym_name = $2;
454 21085 my $class = $namespace;
455 21085 $class =~ s/\./::/g;
456 21085 ▶ if (my $cls = $env->get($namespace, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cls = $env->get($namespace, 1)) 21077 8 21085
457 21077 $class = $$cls;
458 21077 $class =~ s/\./::/g;
459 }
460 21085 ▶ if ($class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new")) 12 21073 21085
461 12 my (undef, @args) = @$ast;
462 12 return list([
463 symbol('.'),
464 symbol($namespace),
465 symbol($sym_name),
466 @args,
467 ]);
468 }
469 }
470 262721 ▶ if (($call = $env->get($sym, 1)) and
Branch: 1 branch
BranchTrueFalseTotal
if ($call = $env->get($sym, 1) and ref $call eq 'Lingy::Macro') { } 9380 253341 262721
Condition100%3/3
$call = $env->get($sym, 1) and ref $call eq "Lingy::Macro"
!ll&&!rl&&r
1064941468479380
MC/DC100%2/2
$call = $env->get($sym, 1) and ref $call eq "Lingy::Macro"
$call = $env->get($sym, 1) ref $call eq "Lingy::Macro"
Truth table100%3/3
A and B
A= $call = $env->get($sym, 1)B= ref $call eq "Lingy::Macro"
ABresult
0X0
100
111
471 ref($call) eq MACRO
472 ) {
473 # expand macro call form
474 9380 $ast = evaluate($call->(@{$ast}[1..(@$ast-1)]));
475 } else {
476 253341 last;
477 }
478 }
479 599082 return $ast;
480 }
481  
482 41136 ▶sub quasiquote {
Subroutine quasiquote: called CC=9 SCAR=22.0
Pod undocumented
483 41136 my ($ast) = @_;
484 41136 ▶ return list([symbol('vec'), quasiquote_loop($ast)])
Branch: 1 branch
BranchTrueFalseTotal
if $ast->isa("Lingy::Vector") 423 40713 41136
485 if $ast->isa(VECTOR);
486 40713 ▶ return list([symbol('quote'), $ast])
Branch: 1 branch
BranchTrueFalseTotal
if $ast->isa("Lingy::HashMap") or $ast->isa("Lingy::HashSet") or $ast->isa("Lingy::Symbol") 16995 23718 40713
Condition83%5/6
$ast->isa("Lingy::HashMap") or $ast->isa("Lingy::HashSet") or $ast->isa("Lingy::Symbol")
l!l&&r!l&&!r
21699323718
$ast->isa("Lingy::HashMap") or $ast->isa("Lingy::HashSet")
l!l&&r!l&&!r
2040711
MC/DC66%2/3
$ast->isa("Lingy::HashMap") or $ast->isa("Lingy::HashSet") or $ast->isa("Lingy::Symbol")
$ast->isa("Lingy::HashMap") $ast->isa("Lingy::HashSet") $ast->isa("Lingy::Symbol")
Truth table75%3/4
A or B or C
A= $ast->isa("Lingy::HashMap")B= $ast->isa("Lingy::HashSet")C= $ast->isa("Lingy::Symbol")
ABCresult
1XX1
01X1
0011
0000
487 if $ast->isa(HASHMAP) or
488 $ast->isa(HASHSET) or
489 $ast->isa(SYMBOL);
490 23718 ▶ return $ast unless $ast->isa(LIST);
Branch: 1 branch
BranchTrueFalseTotal
unless $ast->isa("Lingy::List") 93 23625 23718
491 23625 my ($a0, $a1) = @$ast;
492 23625 ▶ return $a1
Branch: 1 branch
BranchTrueFalseTotal
if $a0 and $a0->isa("Lingy::Symbol") and "$a0" eq "unquote" 7752 15873 23625
Condition100%6/6
$a0 and $a0->isa("Lingy::Symbol") and "$a0" eq "unquote"
!ll&&!rl&&r
78157957752
$a0 and $a0->isa("Lingy::Symbol")
!ll&&!rl&&r
166223547
MC/DC100%3/3
$a0 and $a0->isa("Lingy::Symbol") and "$a0" eq "unquote"
$a0 $a0->isa("Lingy::Symbol") "$a0" eq "unquote"
Truth table100%4/4
A and B and C
A= $a0B= $a0->isa("Lingy::Symbol")C= "$a0" eq "unquote"
ABCresult
0XX0
10X0
1100
1111
493 if $a0 and
494 $a0->isa(SYMBOL) and
495 "$a0" eq 'unquote';
496 15873 return quasiquote_loop($ast);
497 }
498  
499 16296 ▶sub quasiquote_loop {
Subroutine quasiquote_loop: called CC=6 SCAR=17.9
Pod undocumented
500 16296 my ($ast) = @_;
501 16296 my $list = list([]);
502 16296 for my $elt (reverse @$ast) {
503 41039 ▶ if ($elt->isa(LISTTYPE) and
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->isa('Lingy::ListClass') and $elt->[0] and $elt->[0]->isa('Lingy::Symbol') and "$elt->[0]" eq 'splice-unquote') { } 7698 33341 41039
Condition100%9/9
$elt->isa("Lingy::ListClass") and $elt->[0] and $elt->[0]->isa("Lingy::Symbol") and "$elt->[0]" eq "splice-unquote"
!ll&&!rl&&r
17110162317698
$elt->isa("Lingy::ListClass") and $elt->[0] and $elt->[0]->isa("Lingy::Symbol")
!ll&&!rl&&r
170951523929
$elt->isa("Lingy::ListClass") and $elt->[0]
!ll&&!rl&&r
170772423938
MC/DC100%4/4
$elt->isa("Lingy::ListClass") and $elt->[0] and $elt->[0]->isa("Lingy::Symbol") and "$elt->[0]" eq "splice-unquote"
$elt->isa("Lingy::ListClass") $elt->[0] $elt->[0]->isa("Lingy::Symbol") "$elt->[0]" eq "splice-unquote"
Truth table100%5/5
A and B and C and D
A= $elt->isa("Lingy::ListClass")B= $elt->[0]C= $elt->[0]->isa("Lingy::Symbol")D= "$elt->[0]" eq "splice-unquote"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
504 $elt->[0] and
505 $elt->[0]->isa(SYMBOL) and
506 "$elt->[0]" eq 'splice-unquote'
507 ) {
508 7698 $list = list([symbol('concat'), $elt->[1], $list]);
509 } else {
510 33341 $list = list([symbol('cons'), quasiquote($elt), $list]);
511 }
512 }
513 16296 return $list;
514 }
515  
516 56 1;
« lib/Lingy/Fn.pm ↑ Summary lib/Lingy/Util.pm »