TPath/AttributeTest.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« TPath/Index.pm ↑ Summary TPath/Compiler.pm »
1 package TPath::AttributeTest;
2 $TPath::AttributeTest::VERSION = '1.007';
3 # ABSTRACT: compares an attribute value to another value
4  
5  
6 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no if $] >= 5.018, warnings => "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Scalar::Util qw(refaddr looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MooseX::SingletonMethod;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use TPath::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use overload;
13 use namespace::autoclean;
14  
15  
16 with 'TPath::Stringifiable';
17  
18  
19 has op => ( is => 'ro', isa => 'Str', required => 1 );
20  
21  
22 has left => ( is => 'ro', isa => 'ATArg', required => 1 );
23  
24  
25 has right => ( is => 'ro', isa => 'ATArg', required => 1 );
26  
27 sub BUILD {
28 my $self = shift;
29 my ( $l, $r ) = $self->_types;
30 my $func;
31  
32 # some coderefs to turn operators into functions
33 state $ge_n = sub { $_[0] >= $_[1] or undef };
34 state $ge_s = sub { ( $_[0] cmp $_[1] ) >= 0 or undef };
35 state $le_n = sub { $_[0] <= $_[1] or undef };
36 state $le_s = sub { ( $_[0] cmp $_[1] ) <= 0 or undef };
37 state $g_n = sub { $_[0] > $_[1] or undef };
38 state $g_s = sub { ( $_[0] cmp $_[1] ) > 0 or undef };
39 state $l_n = sub { $_[0] < $_[1] or undef };
40 state $l_s = sub { ( $_[0] cmp $_[1] ) < 0 or undef };
41 state $ne_n = sub { $_[0] != $_[1] or undef };
42 state $ne_s = sub { $_[0] ne $_[1] or undef };
43  
44 # construct the appropriate function
45 for ( $self->op ) {
46 when ('=') { $func = $self->_e_func( $l, $r, \&_se ) }
47 when ('==') { $func = $self->_e_func( $l, $r, \&_de ) }
48 when ('<=') { $func = $self->_c_func( $l, $r, $le_s, $le_n ) }
49 when ('<') { $func = $self->_c_func( $l, $r, $l_s, $l_n ) }
50 when ('>=') { $func = $self->_c_func( $l, $r, $ge_s, $ge_n ) }
51 when ('>') { $func = $self->_c_func( $l, $r, $g_s, $g_n ) }
52 when ('!=') { $func = $self->_c_func( $l, $r, $ne_s, $ne_n ) }
53 when ('=~') { $func = $self->_m_func( $r, 1 ) }
54 when ('!~') { $func = $self->_m_func( $r, 0 ) }
55 when ('|=') { $func = $self->_i_func( $l, $r, 0 ) }
56 when ('=|=') { $func = $self->_i_func( $l, $r, 1 ) }
57 when ('=|') { $func = $self->_i_func( $l, $r, 2 ) }
58 }
59  
60 # store it
61 $self->add_singleton_method( test => $func );
62 }
63  
64 # a bunch of private methods that construct custom test methods based on string indices
65 sub _i_func {
66  
67 # type of right value; whether the match is positive (=~)
68 my ( $self, $l_type, $r_type, $i_type ) = @_;
69 if ( $r_type =~ /n|s/ ) {
70 my $v = $self->right;
71 my $s_func = _s_func( $self->left );
72 for ($i_type) {
73 when (0) {
74 my $l = length $v;
75 return sub {
76 my ( $self, $ctx ) = @_;
77 my $lv = $self->$s_func($ctx);
78 return undef unless length $lv >= $l;
79 $lv = substr $lv, 0, $l;
80 return $lv eq $v ? 1 : undef;
81 };
82 }
83 when (1) {
84 return sub {
85 my ( $self, $ctx ) = @_;
86 my $lv = $self->$s_func($ctx);
87 return undef unless defined $lv;
88 my $index = index $lv, $v;
89 return $index > -1 ? 1 : undef;
90 };
91 }
92 when (2) {
93 my $l = length $v;
94 return sub {
95 my ( $self, $ctx ) = @_;
96 my $lv = $self->$s_func($ctx);
97 return undef unless defined $lv;
98 return undef unless length $lv >= $l;
99 $lv = substr $lv, -$l;
100 return $lv eq $v ? 1 : undef;
101 };
102 }
103 }
104 }
105 elsif ( $l_type =~ /n|s/ ) {
106 my $v = $self->left;
107 my $s_func = _s_func( $self->right );
108 for ($i_type) {
109 when (0) {
110 return sub {
111 my ( $self, $ctx ) = @_;
112 my $rv = $self->$s_func($ctx);
113 my $index = index $v, $rv;
114 return $index == 0 ? 1 : undef;
115 };
116 }
117 when (1) {
118 return sub {
119 my ( $self, $ctx ) = @_;
120 my $rv = $self->$s_func($ctx);
121 my $index = index $v, $rv;
122 return $index > -1 ? 1 : undef;
123 };
124 }
125 when (2) {
126 my $ll = length $v;
127 return sub {
128 my ( $self, $ctx ) = @_;
129 my $rv = $self->$s_func($ctx);
130 my $index = index $v, $rv;
131 return $index > -1
132 && $index == $ll - length($rv) ? 1 : undef;
133 };
134 }
135 }
136 }
137 else {
138 my $ls_func = _s_func( $self->left );
139 my $rs_func = _s_func( $self->right );
140 for ($i_type) {
141 when (0) {
142 return sub {
143 my ( $self, $ctx ) = @_;
144 my $lv = $self->$ls_func($ctx);
145 my $rv = $self->$rs_func($ctx);
146 my $index = index $lv, $rv;
147 return $index == 0 ? 1 : undef;
148 };
149 }
150 when (1) {
151 return sub {
152 my ( $self, $ctx ) = @_;
153 my $lv = $self->$ls_func($ctx);
154 my $rv = $self->$rs_func($ctx);
155 my $index = index $lv, $rv;
156 return $index > -1 ? 1 : undef;
157 };
158 }
159 when (2) {
160 return sub {
161 my ( $self, $ctx ) = @_;
162 my $lv = $self->$ls_func($ctx);
163 my $rv = $self->$rs_func($ctx);
164 my $index = index $lv, $rv;
165 return $index > -1
166 && $index == length($lv) - length($rv) ? 1 : undef;
167 };
168 }
169 }
170 }
171 }
172  
173 # a bunch of private methods that construct custom test methods
174 sub _m_func {
175  
176 # type of right value; whether the match is positive (=~)
177 my ( $self, $r_type, $positive ) = @_;
178 for ($r_type) {
179 when (/n|s/) {
180 my $v = $self->right;
181 my $re = qr/$v/;
182 my $s_func = _s_func( $self->left );
183 return $positive
184 ? sub {
185 my ( $self, $ctx ) = @_;
186 my $lv = $self->$s_func($ctx);
187 $lv =~ $re ? 1 : undef;
188 }
189 : sub {
190 my ( $self, $ctx ) = @_;
191 my $lv = $self->$s_func($ctx);
192 $lv =~ $re ? undef : 1;
193 };
194 }
195 default {
196 my $ls_func = _s_func( $self->left );
197 my $rs_func = _s_func( $self->right );
198 return $positive
199 ? sub {
200 my ( $self, $ctx ) = @_;
201 my $lv = $self->$ls_func($ctx);
202 my $rv = $self->$rs_func($ctx);
203 ( $lv // '' ) =~ /$rv/ ? 1 : undef;
204 }
205 : sub {
206 my ( $self, $ctx ) = @_;
207 my $lv = $self->$ls_func($ctx);
208 my $rv = $self->$rs_func($ctx);
209 $lv =~ /$rv/ ? undef : 1;
210 };
211 }
212 }
213 }
214  
215 # generates a stringification function for a value
216 sub _s_func {
217 my $v = shift;
218 for ( _type($v) ) {
219 when ('a') {
220 return sub {
221 my ( undef, $ctx ) = @_;
222 $v->apply($ctx);
223 };
224 }
225 when ('c') {
226 return sub {
227 my ( undef, $ctx ) = @_;
228 $v->concatenate($ctx);
229 };
230 }
231 when ('e') {
232 return sub {
233 my ( undef, $ctx ) = @_;
234 my $c = $v->_select( $ctx, 1 );
235 join '', map { $_->n } @$c;
236 };
237 }
238 when ('t') {
239 return sub {
240 my ( undef, $ctx ) = @_;
241 $v->test($ctx);
242 };
243 }
244 when ('f') {
245 return sub {
246 my ( undef, $ctx ) = @_;
247 $v->to_num($ctx);
248 };
249 }
250 when (/[ns]/) {
251 return sub { $v };
252 }
253 }
254 }
255  
256 # generate = test
257 sub _e_func {
258  
259 # left type, right type, the equality function
260 my ( $self, $l, $r, $ef ) = @_;
261  
262 my $lv = $self->left;
263 my $rv = $self->right;
264  
265 # non-silly functions
266 for ($l) {
267 when ('n') {
268 for ($r) {
269 when ('a') {
270 return sub {
271 my ( undef, $ctx ) = @_;
272 my $v = $rv->apply($ctx);
273 return $ef->( $lv, $v );
274 };
275 }
276 when ('t') {
277 return sub {
278 my ( undef, $ctx ) = @_;
279 my $v = $rv->test($ctx);
280 return $ef->( $lv, $v );
281 }
282 }
283 when ('e') {
284 return sub {
285 my ( undef, $ctx ) = @_;
286 my $c = [ map { $_->n } @{ $rv->_select( $ctx, 1 ) } ];
287 return $ef->( $lv, $c );
288 }
289 }
290 when ('f') {
291 return sub {
292 my ( undef, $ctx ) = @_;
293 return $ef->( $lv, $rv->to_num($ctx) );
294 }
295 }
296 when ('c') {
297 return sub {
298 my ( undef, $ctx ) = @_;
299 my $v = $rv->concatenate($ctx);
300 return $ef->( $lv, $v );
301 };
302 }
303 default {
304 confess "fatal logic error! unexpected argument type $r"
305 }
306 }
307 }
308 when ('s') {
309 for ($r) {
310 when ('a') {
311 return sub {
312 my ( undef, $ctx ) = @_;
313 my $v = $rv->apply($ctx);
314 return $ef->( $lv, $v );
315 };
316 }
317 when ('c') {
318 return sub {
319 my ( undef, $ctx ) = @_;
320 my $v = $rv->concatenate($ctx);
321 return $ef->( $lv, $v );
322 };
323 }
324 when ('t') {
325 return sub {
326 my ( undef, $ctx ) = @_;
327 my $v = $rv->test($ctx);
328 return $ef->( $lv, $v );
329 }
330 }
331 when ('e') {
332 my ( undef, $ctx ) = @_;
333 my $c = $rv->_select( $ctx, 1 );
334 return $ef->( $lv, [ map { $_->n } @$c ] );
335 }
336 when ('f') {
337 my ( undef, $ctx ) = @_;
338 return $ef->( $lv, $rv->to_num($ctx) );
339 }
340 default {
341 confess "fatal logic error! unexpected argument type $r"
342 }
343 }
344 }
345 when ('a') {
346 for ($r) {
347 when (/[ns]/) {
348 return sub {
349 my ( undef, $ctx ) = @_;
350 my $v = $lv->apply($ctx);
351 return $ef->( $rv, $v );
352 };
353 }
354 when ('a') {
355 return sub {
356 my ( undef, $ctx ) = @_;
357 my $v1 = $lv->apply($ctx);
358 my $v2 = $rv->apply($ctx);
359 return $ef->( $v1, $v2 );
360 };
361 }
362 when ('c') {
363 return sub {
364 my ( undef, $ctx ) = @_;
365 my $v1 = $lv->apply($ctx);
366 my $v2 = $rv->concatenate($ctx);
367 return $ef->( $v1, $v2 );
368 };
369 }
370 when ('t') {
371 return sub {
372 my ( undef, $ctx ) = @_;
373 my $v1 = $lv->apply($ctx);
374 my $v2 = $rv->test($ctx);
375 return $ef->( $v1, $v2 );
376 }
377 }
378 when ('e') {
379 return sub {
380 my ( undef, $ctx ) = @_;
381 my $v1 = $lv->apply($ctx);
382 my $c = $rv->_select($ctx);
383 return $ef->( $v1, $c );
384 };
385 }
386 when ('f') {
387 return sub {
388 my ( undef, $ctx ) = @_;
389 my $v1 = $lv->apply($ctx);
390 my $n = $rv->to_num($ctx);
391 return $ef->( $v1, $n );
392 };
393 }
394 default {
395 confess "fatal logic error! unexpected argument type $r"
396 }
397 }
398 }
399 when ('t') {
400 for ($r) {
401 when ('n') {
402 return sub {
403 my ( undef, $ctx ) = @_;
404 my $v1 = $lv->test($ctx);
405 return $ef->( $v1, $rv );
406 };
407 }
408 when ('s') {
409 return sub {
410 my ( undef, $ctx ) = @_;
411 my $v1 = $lv->test($ctx);
412 return $ef->( $v1, $rv );
413 };
414 }
415 when ('a') {
416 return sub {
417 my ( undef, $ctx ) = @_;
418 my $v1 = $lv->test($ctx);
419 my $v2 = $rv->apply($ctx);
420 return $ef->( $v1, $v2 );
421 };
422 }
423 when ('c') {
424 return sub {
425 my ( undef, $ctx ) = @_;
426 my $v1 = $lv->test($ctx);
427 my $v2 = $rv->concatenate($ctx);
428 return $ef->( $v1, $v2 );
429 };
430 }
431 when ('t') {
432 return sub {
433 my ( undef, $ctx ) = @_;
434 my $v1 = $lv->test($ctx);
435 my $v2 = $rv->test($ctx);
436 return $ef->( $v1, $v2 );
437 }
438 }
439 when ('e') {
440 return sub {
441 my ( undef, $ctx ) = @_;
442 my $v1 = $lv->test($ctx);
443 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
444 return $ef->( $v1, $c );
445 }
446 }
447 when ('f') {
448 return sub {
449 my ( undef, $ctx ) = @_;
450 my $v1 = $lv->test($ctx);
451 my $n = $lv->to_num($ctx);
452 return $ef->( $v1, $n );
453 }
454 }
455 default {
456 confess "fatal logic error! unexpected argument type $r"
457 }
458 }
459 }
460 when ('e') {
461 for ($r) {
462 when ('n') {
463 return sub {
464 my ( undef, $ctx ) = @_;
465 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
466 return $ef->( $c, $rv );
467 };
468 }
469 when ('s') {
470 return sub {
471 my ( undef, $ctx ) = @_;
472 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
473 return $ef->( $rv, $c );
474 };
475 }
476 when ('a') {
477 return sub {
478 my ( undef, $ctx ) = @_;
479 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
480 my $v2 = $rv->apply($ctx);
481 return $ef->( $c, $v2 );
482 };
483 }
484 when ('c') {
485 return sub {
486 my ( undef, $ctx ) = @_;
487 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
488 my $v2 = $rv->concatenate($ctx);
489 return $ef->( $c, $v2 );
490 };
491 }
492 when ('t') {
493 return sub {
494 my ( undef, $ctx ) = @_;
495 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
496 my $v2 = $rv->test($ctx);
497 return $ef->( $c, $v2 );
498 }
499 }
500 when ('e') {
501 return sub {
502 my ( undef, $ctx ) = @_;
503 my $c1 = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
504 my $c2 = [ map { $_->n } @{ $rv->_select( $ctx, 1 ) } ];
505 return $ef->( $c1, $c2 );
506 }
507 }
508 when ('f') {
509 return sub {
510 my ( undef, $ctx ) = @_;
511 my $c = [ map { $_->n } @{ $lv->_select( $ctx, 1 ) } ];
512 my $n = $rv->to_num($ctx);
513 return $ef->( $c, $n );
514 }
515 }
516 default {
517 confess "fatal logic error! unexpected argument type $r"
518 }
519 }
520 }
521 when ('f') {
522 for ($r) {
523 when (/[ns]/) {
524 return sub {
525 my ( undef, $ctx ) = @_;
526 return $ef->( $lv->to_num($ctx), $rv );
527 };
528 }
529 when ('a') {
530 return sub {
531 my ( undef, $ctx ) = @_;
532 my $n = $lv->to_num($ctx);
533 my $v2 = $rv->apply($ctx);
534 return $ef->( $n, $v2 );
535 };
536 }
537 when ('c') {
538 return sub {
539 my ( undef, $ctx ) = @_;
540 my $n = $lv->to_num($ctx);
541 my $v2 = $rv->concatenate($ctx);
542 return $ef->( $n, $v2 );
543 };
544 }
545 when ('t') {
546 return sub {
547 my ( undef, $ctx ) = @_;
548 my $n = $lv->to_num($ctx);
549 my $v2 = $rv->test($ctx);
550 return $ef->( $n, $v2 );
551 }
552 }
553 when ('e') {
554 return sub {
555 my ( undef, $ctx ) = @_;
556 my $n = $lv->to_num($ctx);
557 my $c = [ map { $_->n } @{ $rv->_select($ctx) } ];
558 return $ef->( $n, $c );
559 };
560 }
561 when ('f') {
562 return sub {
563 my ( undef, $ctx ) = @_;
564 my $n1 = $lv->to_num($ctx);
565 my $n2 = $rv->to_num($ctx);
566 return $ef->( $n1, $n2 );
567 };
568 }
569 default {
570 confess "fatal logic error! unexpected argument type $r"
571 }
572 }
573 }
574 default { confess "fatal logic error! unexpected argument type $l" }
575 }
576 }
577  
578 sub _c_func {
579  
580 # left type, right type, the string comparison function, the number comparison function
581 my ( $self, $l, $r, $sf, $nf ) = @_;
582  
583 my $lv = $self->left;
584 my $rv = $self->right;
585  
586 for ($l) {
587 when ('n') {
588 for ($r) {
589 when ('a') {
590 return sub {
591 my ( undef, $ctx ) = @_;
592 my $v = $rv->apply($ctx);
593 return unless defined $v;
594 if ( my $type = ref $v ) {
595 for ($type) {
596 when ('ARRAY') {
597 return $nf->( $rv, scalar @$v );
598 }
599 default { return }
600 }
601 }
602 $nf->( $lv, $v );
603 };
604 }
605 when ('t') {
606 return sub {
607 my ( undef, $ctx ) = @_;
608 my $v = $rv->test($ctx);
609 $nf->( $lv, $v );
610 }
611 }
612 when ('e') {
613 return sub {
614 my ( undef, $ctx ) = @_;
615 my $c = $rv->_select( $ctx, 1 );
616 $nf->( $lv, scalar @$c );
617 }
618 }
619 when ('f') {
620 return sub {
621 my ( undef, $ctx ) = @_;
622 my $n = $rv->to_num($ctx);
623 $nf->( $lv, $n );
624 }
625 }
626 when ('c') {
627 return sub {
628 my ( undef, $ctx ) = @_;
629 my $n = $rv->concatenate($ctx);
630 $nf->( $lv, $n );
631 }
632 }
633 default {
634 confess "fatal logic error! unexpected argument type $r"
635 }
636 }
637 }
638 when ('s') {
639 for ($r) {
640 when ('a') {
641 return sub {
642 my ( undef, $ctx ) = @_;
643 my $v = $rv->apply($ctx);
644 return unless defined $v;
645 return $sf->( $lv, $v );
646 };
647 }
648 when ('t') {
649 return sub {
650 my ( undef, $ctx ) = @_;
651 my $v = $rv->test($ctx);
652 $sf->( $lv, $v );
653 }
654 }
655 when ('e') {
656 return sub {
657 my ( undef, $ctx ) = @_;
658 my $c = $rv->_select( $ctx, 1 );
659 $sf->( $lv, join '', map { $_->n } @$c );
660 };
661 }
662 when ('f') {
663 return sub {
664 my ( undef, $ctx ) = @_;
665 my $n = $rv->to_num($ctx);
666 $sf->( $lv, $n );
667 };
668 }
669 when ('c') {
670 return sub {
671 my ( undef, $ctx ) = @_;
672 my $n = $rv->concatenate($ctx);
673 $sf->( $lv, $n );
674 };
675 }
676 default {
677 confess "fatal logic error! unexpected argument type $r"
678 }
679 }
680 }
681 when ('a') {
682 for ($r) {
683 when ('n') {
684 return sub {
685 my ( undef, $ctx ) = @_;
686 my $v = $lv->apply($ctx);
687 return unless defined $v;
688 if ( my $type = ref $v ) {
689 for ($type) {
690 when ('ARRAY') {
691 return $nf->( scalar @$v, $rv );
692 }
693 default { return }
694 }
695 }
696 $nf->( $v, $rv );
697 };
698 }
699 when ('s') {
700 return sub {
701 my ( undef, $ctx ) = @_;
702 my $v = $lv->apply($ctx);
703 return unless defined $v;
704 return $sf->( $v, $rv );
705 };
706 }
707 when ('c') {
708 return sub {
709 my ( undef, $ctx ) = @_;
710 my $v1 = $lv->apply($ctx);
711 my $v2 = $rv->concatenate($ctx);
712 return $sf->( $v1, $v2 );
713 };
714 }
715 when ('a') {
716 return sub {
717 my ( undef, $ctx ) = @_;
718 my $v1 = $lv->apply($ctx);
719 my $v2 = $rv->apply($ctx);
720 return _reduce( $v1, $v2, $sf, $nf, $ctx );
721 };
722 }
723 when ('t') {
724 return sub {
725 my ( undef, $ctx ) = @_;
726 my $v1 = $lv->apply($ctx);
727 my $v2 = $rv->test($ctx);
728 return _reduce( $v1, $v2, $sf, $nf, $ctx );
729 };
730 }
731 when ('e') {
732 return sub {
733 my ( undef, $ctx ) = @_;
734 my $v1 = $lv->apply($ctx);
735 my $c = $rv->_select( $ctx, 1 );
736 return _reduce( $v1, [ map { $_->n } @$c ], $sf, $nf,
737 $ctx );
738 };
739 }
740 when ('f') {
741 return sub {
742 my ( undef, $ctx ) = @_;
743 my $v1 = $lv->apply($ctx);
744 my $n = $rv->to_num($ctx);
745 return _reduce( $v1, $n, $sf, $nf, $ctx );
746 };
747 }
748 default {
749 confess "fatal logic error! unexpected argument type $r"
750 }
751 }
752 }
753 when ('t') {
754 for ($r) {
755 when ('n') {
756 return sub {
757 my ( undef, $ctx ) = @_;
758 my $v1 = $lv->test($ctx);
759 return $nf->( $v1, $rv );
760 };
761 }
762 when ('s') {
763 return sub {
764 my ( undef, $ctx ) = @_;
765 my $v1 = $lv->test($ctx);
766 return $sf->( $v1, $rv );
767 };
768 }
769 when ('c') {
770 return sub {
771 my ( undef, $ctx ) = @_;
772 my $v1 = $lv->test($ctx);
773 my $v2 = $rv->concatenate($ctx);
774 return $sf->( $v1, $v2 );
775 };
776 }
777 when ('a') {
778 return sub {
779 my ( undef, $ctx ) = @_;
780 my $v1 = $lv->test($ctx);
781 my $v2 = $rv->apply($ctx);
782 return _reduce( $v1, $v2, $sf, $nf, $ctx );
783 };
784 }
785 when ('t') {
786 return sub {
787 my ( undef, $ctx ) = @_;
788 my $v1 = $lv->test($ctx);
789 my $v2 = $rv->test($ctx);
790 return $nf->( $v1, $v2 );
791 }
792 }
793 when ('e') {
794 return sub {
795 my ( undef, $ctx ) = @_;
796 my $v1 = $lv->test($ctx);
797 my $c = $rv->_select( $ctx, 1 );
798 return $nf->( $v1, scalar @$c );
799 };
800 }
801 when ('f') {
802 return sub {
803 my ( undef, $ctx ) = @_;
804 my $v1 = $lv->test($ctx);
805 my $n = $rv->to_num($ctx);
806 return $nf->( $v1, $n );
807 };
808 }
809 default {
810 confess "fatal logic error! unexpected argument type $r"
811 }
812 }
813 }
814 when ('e') {
815 for ($r) {
816 when ('n') {
817 return sub {
818 my ( undef, $ctx ) = @_;
819 my $c = $lv->_select( $ctx, 1 );
820 return $nf->( scalar @$c, $rv );
821 };
822 }
823 when ('s') {
824 return sub {
825 my ( undef, $ctx ) = @_;
826 my $c = $lv->_select( $ctx, 1 );
827 return $sf->( join( '', @$c ), $rv );
828 };
829 }
830 when ('c') {
831 return sub {
832 my ( undef, $ctx ) = @_;
833 my $c = $lv->_select( $ctx, 1 );
834 my $v2 = $rv->concatenate($ctx);
835 return $sf->( join( '', @$c ), $v2 );
836 };
837 }
838 when ('a') {
839 return sub {
840 my ( undef, $ctx ) = @_;
841 my $c = $lv->_select( $ctx, 1 );
842 my $v2 = $rv->apply($ctx);
843 return _reduce( [ map { $_->n } @$c ], $v2, $sf, $nf,
844 $ctx );
845 };
846 }
847 when ('t') {
848 return sub {
849 my ( undef, $ctx ) = @_;
850 my $c = $lv->_select( $ctx, 1 );
851 my $v2 = $rv->test($ctx);
852 return $nf->( scalar @$c, $v2 );
853 }
854 }
855 when ('e') {
856 return sub {
857 my ( undef, $ctx ) = @_;
858 my $c1 = $lv->_select( $ctx, 1 );
859 my $c2 = $rv->_select( $ctx, 1 );
860 return $nf->( scalar @$c1, scalar @$c2 );
861 };
862 }
863 when ('f') {
864 return sub {
865 my ( undef, $ctx ) = @_;
866 my $c = $lv->_select( $ctx, 1 );
867 my $n = $rv->to_num($ctx);
868 return $nf->( scalar @$c, $n );
869 };
870 }
871 default {
872 confess "fatal logic error! unexpected argument type $r"
873 }
874 }
875 }
876 when ('f') {
877 for ($r) {
878 when ('n') {
879 return sub {
880 my ( undef, $ctx ) = @_;
881 my $n = $lv->to_num($ctx);
882 return $nf->( $n, $rv );
883 };
884 }
885 when ('s') {
886 return sub {
887 my ( undef, $ctx ) = @_;
888 my $n = $lv->to_num($ctx);
889 return $nf->( $n, $rv );
890 };
891 }
892 when ('c') {
893 return sub {
894 my ( undef, $ctx ) = @_;
895 my $n = $lv->to_num($ctx);
896 my $v2 = $rv->concatenate($ctx);
897 return $nf->( $n, $v2 );
898 };
899 }
900 when ('a') {
901 return sub {
902 my ( undef, $ctx ) = @_;
903 my $n = $lv->to_num($ctx);
904 my $v2 = $rv->apply($ctx);
905 return _reduce( $n, $v2, $sf, $nf, $ctx );
906 };
907 }
908 when ('t') {
909 return sub {
910 my ( undef, $ctx ) = @_;
911 my $n = $lv->to_num($ctx);
912 my $v2 = $rv->test($ctx);
913 return $nf->( $n, $v2 );
914 }
915 }
916 when ('e') {
917 return sub {
918 my ( undef, $ctx ) = @_;
919 my $n = $lv->to_num($ctx);
920 my $c = $rv->_select( $ctx, 1 );
921 return $nf->( $n, scalar @$c );
922 };
923 }
924 when ('f') {
925 return sub {
926 my ( undef, $ctx ) = @_;
927 my $n1 = $lv->to_num($ctx);
928 my $n2 = $rv->to_num($ctx);
929 return $nf->( $n1, $n2 );
930 };
931 }
932 default {
933 confess "fatal logic error! unexpected argument type $r"
934 }
935 }
936 }
937 default { confess "fatal logic error! unexpected argument type $l" }
938 }
939 }
940  
941 sub _reduce {
942 my ( $v1, $v2, $sf, $nf, $ctx ) = @_;
943 my ( $l, $r ) = map { _type($_) } $v1, $v2;
944 for ("$l$r") {
945 when ('nn') { return $nf->( $v1, $v2 ) }
946 when (/[sn]{2}/) { return $sf->( $v1, $v2 ) }
947 when ('nh') { return $nf->( $v1, scalar keys %$v2 ) }
948 when ('hn') { return $nf->( scalar keys %$v1, $v2 ) }
949 when ('nr') { return $nf->( $v1, scalar @$v2 ) }
950 when ('rn') { return $nf->( scalar @$v1, $v2 ) }
951 when ('sr') { return $sf->( $v1, join '', @$v2 ) }
952 when ('rs') { return $sf->( join( '', @$v1 ), $v2 ) }
953 when (/[etafc].|.[etafc]/) {
954 my ( $v3, $v4 ) = ( $v1, $v2 );
955 for ($l) {
956 when ('e') {
957 $v3 = [ map { $_->n } @{ $v1->_select( $ctx, 1 ) } ];
958 }
959 when ('t') { $v3 = $v1->test($ctx) }
960 when ('a') { $v3 = $v1->apply($ctx) }
961 when ('f') { $v3 = $v1->to_num($ctx) }
962 when ('c') { $v3 = $v1->concatenate($ctx) }
963 }
964 for ($r) {
965 when ('e') {
966 $v4 = [ map { $_->n } @{ $v2->_select( $ctx, 1 ) } ];
967 }
968 when ('t') { $v4 = $v2->test($ctx) }
969 when ('a') { $v4 = $v2->apply($ctx) }
970 when ('f') { $v4 = $v2->to_num($ctx) }
971 when ('c') { $v4 = $v2->concatenate($ctx) }
972 }
973 return _reduce( $v3, $v4, $sf, $nf, $ctx );
974 }
975 default { return $sf->( $v1, $v2 ) }
976 }
977 }
978  
979 # single equals
980 sub _se {
981 my ( $v1, $v2 ) = @_;
982  
983 return undef if defined $v1 ^ defined $v2;
984 return 1 unless defined $v1;
985 my ( $l, $r ) = map { _type($_) } $v1, $v2;
986 my $lr = "$l$r";
987 for ($lr) {
988 when (/so|os|ss/) { return $v1 eq $v2 ? 1 : undef }
989 when ('nn') { return $v1 == $v2 ? 1 : undef }
990 when ('nr') { return $v1 == @$v2 ? 1 : undef }
991 when ('rn') { return @$v1 == $v2 ? 1 : undef }
992 when ('sr') { return $v1 eq join( '', @$v2 ) ? 1 : undef }
993 when ('rs') { return join( '', @$v1 ) eq $v2 ? 1 : undef }
994 when ('rr') {
995 my @a1 = @$v1;
996 my @a2 = @$v2;
997 return undef unless @a1 == @a2;
998 for my $i ( 0 .. $#a1 ) {
999 return undef unless _se( $a1[$i], $a2[$i] );
1000 }
1001 return 1;
1002 }
1003 when ('hh') {
1004 my @keys = keys %$v1;
1005 return undef unless @keys == ( keys %$v2 );
1006 for my $k (@keys) {
1007 my ( $o1, $o2 ) = ( $v1->{$k}, $v2->{$k} );
1008 return undef unless _se( $o1, $o2 );
1009 }
1010 return 1;
1011 }
1012 when ('oo') {
1013 my $f = $v1->can('equals') || overload::Method( $v1, '==' );
1014 return $v1->$f($v2) ? 1 : undef if $f;
1015 $f = $v2->can('equals') || overload::Method( $v2, '==' );
1016 return $v2->$f($v1) ? 1 : undef if $f;
1017 return refaddr $v1 == refaddr $v2 ? 1 : undef;
1018 }
1019 when (/o./) {
1020 my $f = $v1->can('equals') || overload::Method( $v1, '==' );
1021 return $v1->$f->($v2) ? 1 : undef if $f;
1022 return undef;
1023 }
1024 when (/.o/) {
1025 my $f = $v2->can('equals') || overload::Method( $v2, '==' );
1026 return $v2->$f($v1) ? 1 : undef if $f;
1027 return undef;
1028 }
1029 default { return undef }
1030 }
1031 }
1032  
1033 # double equals
1034 sub _de {
1035 my ( $v1, $v2 ) = @_;
1036 return if defined $v1 ^ defined $v2;
1037 return 1 unless defined $v1;
1038 my ( $l, $r ) = map { _type($_) } $v1, $v2;
1039 my $lr = "$l$r";
1040 for ($lr) {
1041 when ('ss') { return $v1 eq $v2 ? 1 : undef }
1042 when ('nn') { return $v1 == $v2 ? 1 : undef }
1043 when ('hn') { return keys %$v1 == $v2 ? 1 : undef }
1044 when ('nh') { return $v1 == keys %$v2 ? 1 : undef }
1045 when ('nr') { return $v1 == @$v2 ? 1 : undef }
1046 when ('rn') { return @$v1 == $v2 ? 1 : undef }
1047 default {
1048 return ( refaddr $v1 || 0 ) == ( refaddr $v2 || 0 )
1049 ? 1
1050 : undef;
1051 }
1052 }
1053 }
1054  
1055 sub _types {
1056 my $self = shift;
1057 _type( $self->left ), _type( $self->right );
1058 }
1059  
1060 # tests type of argument
1061 sub _type {
1062 my $arg = shift;
1063 if ( my $type = ref $arg ) {
1064 return 'h' if $type eq 'HASH';
1065 return 'r' if $type eq 'ARRAY';
1066 return 'a' if $arg->isa('TPath::Attribute');
1067 return 'e' if $arg->isa('TPath::Expression');
1068 return 't' if $arg->isa('TPath::AttributeTest');
1069 return 'c' if $arg->isa('TPath::Concatenation');
1070 return 'f' if $arg->DOES('TPath::Numifiable');
1071 return 'o';
1072 }
1073 return 'n' if looks_like_number $arg;
1074 return 's';
1075 }
1076  
1077 sub to_string {
1078 my $self = shift;
1079 $self->_stringify( $self->left, 1 ) . ' '
1080 . $self->op . ' '
1081 . $self->_stringify( $self->right, 1 );
1082 }
1083  
1084 __PACKAGE__->meta->make_immutable;
1085  
1086 1;
1087  
1088 __END__
« TPath/Index.pm ↑ Summary TPath/Compiler.pm »