blib/lib/Math/Logic/Predicate.pm

filter: Statementstmt 88.2% 240 / 272 Branchbran 69.4% 125 / 180 Conditioncond 52.8% 46 / 87 MC/DCmcdc 30.4% 14 / 46 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 11 totaltotal 71.9% 440 / 612 CC 129 SCAR 46.4
CC
129
Cov
76%
match
226.5
lookup
29.4
addproc_h
11.5
CRAP 103.4
↑ Summary
1 package Math::Logic::Predicate;
2  
3 1 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $GRAMMAR = join '', <DATA>;
9 1 our $DEBUG;
10  
11 1 our $VERSION = 0.03;
12  
13 sub new($)
14 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 1 my ($class) = @_;
16 bless {
17 pred => { },
18 125 ▶ nonterm => sub { $_[0] =~ /^[_A-Z]/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 no_code => undef,
20 parser => undef,
21 } => $class
22 }
23  
24 0 ▶sub rules(\$) : lvalue
Subroutine rules: not called CC=1 SCAR=6.9
Pod undocumented
25 {
26 shift->{pred}
27 0 }
28  
29 sub parse(\$$;$)
30 11 ▶{
Subroutine parse: called CC=6 SCAR=18.7
Pod undocumented
31 11 my ($self, $expr, $auto) = @_;
32 11 ▶ $self->{parser} ||= new Parse::RecDescent($GRAMMAR) or confess;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"parser"} ||= "Parse::RecDescent"->new($Math::Logic::Predicate::GRAMMAR) or confess() 11 0 11
Condition66%2/3
$self->{"parser"} ||= "Parse::RecDescent"->new($Math::Logic::Predicate::GRAMMAR)
l!l&&r!l&&!r
1010
MC/DC0%0/2
$self->{"parser"} ||= "Parse::RecDescent"->new($Math::Logic::Predicate::GRAMMAR)
$self->{"parser"} "Parse::RecDescent"->new($Math::Logic::Predicate::GRAMMAR)
Truth table66%2/3
A ||= B
A= $self->{"parser"}B= "Parse::RecDescent"->new($Math::Logic::Predicate::GRAMMAR)
ABresult
1X1
011
000
33 11 $self->{parser}{LG} = $self;
34 11 my $ret;
35 11 ▶ if ($auto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto) { } 3 8 11
36 3 $ret = $self->{parser}->auto($expr);
37 }
38 else {
39 8 $ret = $self->{parser}->statement($expr);
40 }
41 11 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 11 0 11
42 11 ▶ return wantarray ? @$ret : $ret->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 11 11
43 }
44 return
45 0 }
46  
47 sub newproc(\$$$;$$$)
48 72 ▶{
Subroutine newproc: called CC=8 SCAR=23.9
Pod undocumented
49 72 my ($self, $rule, $args, $context, $next, $prev) = @_;
50 72 ▶ $context ||= 'true';
Condition50%1/2
$context ||= "true"
l!l
720
MC/DC0%0/1
$context ||= "true"
$context
Truth table50%1/2
A
A= $context
Aresult
11
00
51  
52 72 ▶ unless (grep { $context == $_ } qw(true false and or sub bind)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$context == $_;} "true", "false", "and", "or", "sub", "bind") 0 72 72
53 0 croak "'$context' is not a valid context type";
54 }
55
56 72 my $ret = {
57 context => $context,
58 rule => $rule,
59 args => $args,
60 };
61  
62 72 $ret->{next} = $next;
63 72 $ret->{fail} = $prev;
64 72 ▶ unless ($context eq 'true' || $context eq 'false' || $context eq 'bind') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context eq "true" or $context eq "false" or $context eq "bind") 66 6 72
Condition50%3/6
$context eq "true" or $context eq "false" or $context eq "bind"
l!l&&r!l&&!r
0666
$context eq "true" or $context eq "false"
l!l&&r!l&&!r
0072
MC/DC33%1/3
$context eq "true" or $context eq "false" or $context eq "bind"
$context eq "true" $context eq "false" $context eq "bind"
Truth table50%2/4
A or B or C
A= $context eq "true"B= $context eq "false"C= $context eq "bind"
ABCresult
1XX1
01X1
0011
0000
65 66 ▶ $ret->{next}{fail} ||= $ret if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 0 66 66
Condition0%0/3
$ret->{"next"}{"fail"} ||= $ret
l!l&&r!l&&!r
000
MC/DC0%0/2
$ret->{"next"}{"fail"} ||= $ret
$ret->{"next"}{"fail"} $ret
Truth table0%0/3
A ||= B
A= $ret->{"next"}{"fail"}B= $ret
ABresult
1X1
011
000
66 }
67 $ret
68 72 }
69  
70 sub add(\$$)
71 13 ▶{
Subroutine add: called CC=8 SCAR=22.2
Pod undocumented
72 13 my ($self, $proc) = @_;
73  
74 13 ▶ if (ref $proc) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proc) { } 10 3 13
75
76 10 ▶ croak "You can't add an undefined rule" unless $proc;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc 0 10 10
77 10 ▶ croak "You can't add queries to the database" if $proc->{rule} eq '^QUERY';
Branch: 1 branch
BranchTrueFalseTotal
if $proc->{"rule"} eq "^QUERY" 0 10 10
78 10 ▶ croak "You can't add variable predicates"if $self->{nonterm}($proc->{rule});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nonterm"}->($proc->{"rule"}) 0 10 10
79  
80 10 ▶ if (@{$proc->{args}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$proc->{'args'};}) { } 10 0 10
81 10 ▶ $self->{pred}{$proc->{rule}} = { } unless $self->{pred}{$proc->{rule}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pred"}{$proc->{"rule"}} 6 4 10
82 }
83 else {
84 0 ▶ $self->{pred}{$proc->{rule}} = [ ] unless $self->{pred}{$proc->{rule}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pred"}{$proc->{"rule"}} 0 0 0
85 }
86 10 $self->addproc_h($proc, 0, $self->{pred}{$proc->{rule}});
87 10 $proc
88
89 }
90 else {
91 3 $self->parse($proc, 'auto')
92 }
93 }
94  
95 sub retract(\$$)
96 2 ▶{
Subroutine retract: called CC=3 SCAR=11.0
Pod undocumented
97 2 my ($self, $proc) = @_;
98
99 2 ▶ $proc = $self->parse($proc) unless ref $proc;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $proc 2 0 2
100
101 2 my ($pad, $frame) = ( {}, {} );
102 2 ▶ while ($self->lookup($proc, $frame, $pad, 'delete')) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lookup($proc, $frame, $pad, "delete")) 3 2 5
103 2 1;
104 }
105  
106 sub addproc_h(\$$$$)
107 25 ▶{
Subroutine addproc_h: called CC=11 SCAR=24.4
Pod undocumented
108 25 my ($self, $proc, $argn, $href) = @_;
109 25 ▶ if (local $_ = $proc->{args}[$argn]) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $_ = $proc->{'args'}[$argn]) { } 15 10 25
110 15 ▶ my $r = $self->{nonterm}($_) ? '_' : $_;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'nonterm'}->($_) ? : 3 12 15
111 15 ▶ unless ($href->{$r}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href->{$r}) 15 0 15
112 15 ▶ if ($argn == $#{$proc->{args}}) { # Last argument
Branch: 1 branch
BranchTrueFalseTotal
if ($argn == $#{$$proc{'args'};}) { } 10 5 15
113 10 $href->{$r} = [ ];
114 }
115 else {
116 5 $href->{$r} = { };
117 }
118 15 delete $href->{'^SORT'};
119 }
120 15 $self->addproc_h($proc, $argn+1, $href->{$r});
121 }
122 else {
123 # Don't duplicate
124 10 ▶ if ($proc->{context} eq 'true' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"context"} eq "true" or $proc->{"context"} eq "false") 8 2 10
Condition66%2/3
$proc->{"context"} eq "true" or $proc->{"context"} eq "false"
l!l&&r!l&&!r
802
MC/DC50%1/2
$proc->{"context"} eq "true" or $proc->{"context"} eq "false"
$proc->{"context"} eq "true" $proc->{"context"} eq "false"
Truth table66%2/3
A or B
A= $proc->{"context"} eq "true"B= $proc->{"context"} eq "false"
ABresult
1X1
011
000
125 $proc->{context} eq 'false') { # Don't duplicate
126 0 ▶ return if grep { $_->{context} eq $proc->{context} } @$href;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"context"} eq $proc->{"context"};} @$href 0 8 8
127 }
128 # Is this rule directly recursive?
129 10 ▶ if ($proc->{context} eq 'bind') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"context"} eq "bind") 2 8 10
130 2 my $cptr = $proc->{next};
131 2 ▶ while ($cptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr) 2 2 4
132 2 ▶ if ($cptr->{rule} eq $proc->{rule}) { #If so...
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{"rule"} eq $proc->{"rule"}) 0 2 2
133 0 push @$href, $proc;
134 0 return 1;
135 }
136 2 $cptr = $cptr->{next};
137 }
138 }
139 10 unshift @$href, $proc;
140 }
141 25 1;
142 }
143  
144 sub lookup(\$$$$;$)
145 17 ▶{
Subroutine lookup: called CC=29 SCAR=33.8
Pod undocumented
146 17 my ($self, $proc, $lse, $pad, $delete) = @_;
147  
148 17 my $rule = $proc->{rule};
149 17 ▶ $rule = $pad->{$rule} if $self->{nonterm}($rule);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nonterm"}->($rule) 0 17 17
150  
151 17 ▶ $lse->{fail}++,return if $lse->{fail};
Branch: 1 branch
BranchTrueFalseTotal
if $lse->{"fail"} 0 17 17
152 17 ▶ $lse->{fail}++,return unless $rule && $self->{pred}{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless $rule and $self->{"pred"}{$rule} 0 17 17
Condition33%1/3
$rule and $self->{"pred"}{$rule}
!ll&&!rl&&r
0017
MC/DC0%0/2
$rule and $self->{"pred"}{$rule}
$rule $self->{"pred"}{$rule}
Truth table33%1/3
A and B
A= $ruleB= $self->{"pred"}{$rule}
ABresult
0X0
100
111
153  
154 17 ▶ $lse->{pred_stack} ||= [ $self->{pred}{$rule} ];
Condition100%2/2
$lse->{"pred_stack"} ||= [$self->{"pred"}{$rule}]
l!l
611
MC/DC100%1/1
$lse->{"pred_stack"} ||= [$self->{"pred"}{$rule}]
$lse->{"pred_stack"}
Truth table100%2/2
A
A= $lse->{"pred_stack"}
Aresult
11
00
155 17 ▶ $lse->{iter_stack} ||= [ 0 ];
Condition100%2/2
$lse->{"iter_stack"} ||= [0]
l!l
611
MC/DC100%1/1
$lse->{"iter_stack"} ||= [0]
$lse->{"iter_stack"}
Truth table100%2/2
A
A= $lse->{"iter_stack"}
Aresult
11
00
156 17 ▶ $lse->{bind_stack} ||= [ 0 ];
Condition100%2/2
$lse->{"bind_stack"} ||= [0]
l!l
611
MC/DC100%1/1
$lse->{"bind_stack"} ||= [0]
$lse->{"bind_stack"}
Truth table100%2/2
A
A= $lse->{"bind_stack"}
Aresult
11
00
157 17 ▶ $lse->{pos} ||= 0;
Condition100%2/2
$lse->{"pos"} ||= 0
l!l
611
MC/DC100%1/1
$lse->{"pos"} ||= 0
$lse->{"pos"}
Truth table100%2/2
A
A= $lse->{"pos"}
Aresult
11
00
158
159 17 my $pred = $lse->{pred_stack};
160 17 my $iter = $lse->{iter_stack};
161 17 my $bind = $lse->{bind_stack};
162
163
164 17 ▶ while (@$pred) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pred) 47 0 47
165 47 my $p;
166 47 ▶ unless ($lse->{pos} == @{$proc->{args}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lse->{"pos"} == @{$proc->{"args"};}) 29 18 47
167 29 $p = $proc->{args}[$lse->{pos}];
168 13 $pred->[0]{'^SORT'} = [ sort keys %{$pred->[0]} ]
169 29 ▶ unless $pred->[0]{'^SORT'};
Branch: 1 branch
BranchTrueFalseTotal
unless $pred->[0]{"^SORT"} 13 16 29
170 }
171 47 my $state = 'push';
172 47 ▶ $p = exists $pad->{$p} ? $pad->{$p} : $p;
Branch: 1 branch
BranchTrueFalseTotal
exists $pad->{$p} ? : 5 42 47
173  
174 47 ▶ if ($lse->{pos} == @{$proc->{args}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lse->{'pos'} == @{$proc->{'args'};}) { } 18 29 47
175 18 ▶ $state = 'pop' if $iter->[0] == @{$pred->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $iter->[0] == @{$pred->[0];} 8 10 18
176 }
177 else {
178 29 ▶ if ($self->{nonterm}($p)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'nonterm'}->($p)) { } 9 20 29
179 9 ▶ $state = 'pop' if $iter->[0] == @{$pred->[0]{'^SORT'}};
Branch: 1 branch
BranchTrueFalseTotal
if $iter->[0] == @{$pred->[0]{"^SORT"};} 2 7 9
180 }
181 else {
182 20 my $len = exists($pred->[0]{$p}) + exists($pred->[0]{_});
183 20 ▶ $state = 'pop' if $iter->[0] >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $iter->[0] >= $len 9 11 20
184 }
185 }
186
187 47 ▶ if ($state eq 'pop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'pop') { } 19 28 47
188 19 my $free = shift @$bind;
189 19 shift @$iter;
190 19 shift @$pred;
191 19 ▶ delete $pad->{$free} if $free;
Branch: 1 branch
BranchTrueFalseTotal
if $free 2 17 19
192 19 $lse->{pos}--;
193
194
195 19 ▶ unless (@$iter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$iter) 7 12 19
196 7 $lse->{fail}++;
197 7 return;
198 }
199 }
200 else {
201 28 my $ind;
202 28 my $pi = $iter->[0]++;
203 28 ▶ unless (defined $p) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $p) { } 18 10 28
if (do { $self->{'nonterm'}->($p) }) { } 7 11 18
204 10 ▶ if ($delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) { } 3 7 10
205 3 delete $pred->[1]{'^SORT'};
206 3 return splice @{$pred->[0]}, --$iter->[0], 1;
207 }
208 else {
209 7 return $pred->[0][$pi];
210 }
211 }
212 elsif ($self->{nonterm}($p)) {
213 7 $ind = $pred->[0]{'^SORT'}[$pi];
214 # No binding to anonymous vars
215 7 ▶ unless ($p eq '_' || $ind eq '_' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq '_' or $ind eq '_' or exists $pad->{$p}) { } 4 3 7
Condition83%5/6
$p eq "_" or $ind eq "_" or exists $pad->{$p}
l!l&&r!l&&!r
403
$p eq "_" or $ind eq "_"
l!l&&r!l&&!r
223
MC/DC66%2/3
$p eq "_" or $ind eq "_" or exists $pad->{$p}
$p eq "_" $ind eq "_" exists $pad->{$p}
Truth table75%3/4
A or B or C
A= $p eq "_"B= $ind eq "_"C= exists $pad->{$p}
ABCresult
1XX1
01X1
0011
0000
216 exists $pad->{$p}) {
217 3 $pad->{$p} = $ind;
218 3 unshift @$bind, $p;
219 }
220 else { # Still need a frame, though
221 4 unshift @$bind, (undef);
222 }
223 }
224 else {
225 11 ▶ if ($pi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pi) { } 0 11 11
226 0 $ind = '_';
227 }
228 else {
229 11 ▶ $ind = exists $pred->[0]{$p} ? $p : '_';
Branch: 1 branch
BranchTrueFalseTotal
exists $pred->[0]{$p} ? : 10 1 11
230 }
231 11 unshift @$bind, 0;
232 }
233 18 unshift @$pred, $pred->[0]{$ind};
234 18 unshift @$iter, 0;
235 18 $lse->{pos}++;
236 }
237 }
238 }
239  
240 16 ▶sub copy_pad(\$$$$$;$) {
Subroutine copy_pad: called CC=10 SCAR=23.0
Pod undocumented
241 16 my ($self, $srule, $scon, $drule, $dcon, $bindtrack) = @_;
242
243 16 ▶ return unless @{$srule->{args}} == @{$drule->{args}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$srule->{"args"};} == @{$drule->{"args"};} 0 16 16
244
245 # I want perl6 parallel iteration!!
246 16 ▶ for (my $i = 0; $i < @{$drule->{args}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$drule->{"args"};}) 22 16 38
247 22 ▶ if ($self->{nonterm}($drule->{args}[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nonterm"}->($drule->{"args"}[$i])) 12 10 22
248 12 my $bind = $srule->{args}[$i];
249 12 ▶ $bind = $scon->{pad}{$bind} if $self->{nonterm}($bind);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nonterm"}->($bind) 6 6 12
250 12 ▶ if (defined $bind && $drule->{args}[$i] ne '_' &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bind and $drule->{"args"}[$i] ne "_" and not exists $dcon->{"pad"}{$drule->{"args"}[$i]}) 4 8 12
Condition83%5/6
defined $bind and $drule->{"args"}[$i] ne "_" and not exists $dcon->{"pad"}{$drule->{"args"}[$i]}
!ll&&!rl&&r
354
defined $bind and $drule->{"args"}[$i] ne "_"
!ll&&!rl&&r
309
MC/DC66%2/3
defined $bind and $drule->{"args"}[$i] ne "_" and not exists $dcon->{"pad"}{$drule->{"args"}[$i]}
defined $bind $drule->{"args"}[$i] ne "_" not exists $dcon->{"pad"}{$drule->{"args"}[$i]}
Truth table75%3/4
A and B and not(C)
A= defined $bindB= $drule->{"args"}[$i] ne "_"C= not exists $dcon->{"pad"}{$drule->{"args"}[$i]}
ABCresult
0XX0
10X0
1100
1111
251 !exists $dcon->{pad}{$drule->{args}[$i]}) {
252 4 $dcon->{pad}{$drule->{args}[$i]} = $bind;
253 4 ▶ $dcon->{stack}[0]{bindings} ||= [ ];
Condition100%2/2
$dcon->{"stack"}[0]{"bindings"} ||= []
l!l
13
MC/DC100%1/1
$dcon->{"stack"}[0]{"bindings"} ||= []
$dcon->{"stack"}[0]{"bindings"}
Truth table100%2/2
A
A= $dcon->{"stack"}[0]{"bindings"}
Aresult
11
00
254 3 push @{$dcon->{stack}[0]{bindings}},
255 4 ▶ $drule->{args}[$i] if $bindtrack;
Branch: 1 branch
BranchTrueFalseTotal
if $bindtrack 3 1 4
256 }
257 }
258 }
259 16 1;
260 }
261  
262 # Returns a context or undef
263 # Changes are reflected in the pad
264 sub match(\$$;$$)
265 17 ▶{
Subroutine match: called CC=60 SCAR=54.2
Pod undocumented
266 17 my ($self, $proc, $state, $indent) = @_;
267  
268 17 ▶ $proc = $self->parse($proc) unless ref $proc;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $proc 1 16 17
269  
270 17 my $cptr; # Pointer to frame of chain
271 17 my $res = 0; # Did the last thing executed succeed (1,0)?
272 17 my $dir = 1; # Are we going forward or backtracking (1,0)?
273
274 17 ▶ $state ||= { pad => {}, stack => [] };
Condition100%2/2
$state ||= {"pad", {}, "stack", []}
l!l
611
MC/DC100%1/1
$state ||= {"pad", {}, "stack", []}
$state
Truth table100%2/2
A
A= $state
Aresult
11
00
275
276 17 ▶ return $state if $proc->{context} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $proc->{"context"} eq "true" 5 12 17
277 12 ▶ return undef if $proc->{context} eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $proc->{"context"} eq "false" 0 12 12
278  
279 12 ▶ if ($proc->{context} eq 'bind' && $proc->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"context"} eq "bind" and $proc->{"code"}) 2 10 12
Condition66%2/3
$proc->{"context"} eq "bind" and $proc->{"code"}
!ll&&!rl&&r
0102
MC/DC50%1/2
$proc->{"context"} eq "bind" and $proc->{"code"}
$proc->{"context"} eq "bind" $proc->{"code"}
Truth table66%2/3
A and B
A= $proc->{"context"} eq "bind"B= $proc->{"code"}
ABresult
0X0
100
111
280 2 ▶ $state->{stack}[0]{bindings} ||= [];
Condition100%2/2
$state->{"stack"}[0]{"bindings"} ||= []
l!l
11
MC/DC100%1/1
$state->{"stack"}[0]{"bindings"} ||= []
$state->{"stack"}[0]{"bindings"}
Truth table100%2/2
A
A= $state->{"stack"}[0]{"bindings"}
Aresult
11
00
281 2 delete $state->{pad}{$_} for @{$state->{stack}[0]{bindings}};
282 2 @{$state->{stack}[0]{bindings}} = ();
283  
284 2 my @nt = grep { $self->{nonterm}($_) } @{$proc->{args}};
285  
286 2 ▶ unless ($proc->{bindcode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proc->{"bindcode"}) 1 1 2
287
288 1 my $ev;
289 1 $ev = "package main; no strict; my \%r;\n";
290 1 $ev .= 'my ($pad, $stack) = @_;';
291 $ev .= "local \$$_ = \$pad->{$_};"
292 1 ."\$r{$_} = \$$_ =~ s/^'//;\n" for @nt;
293 1 $ev .= <<'EOC';
294 local $track = !$stack->{track};
295 $stack->{track} = 1;
296 $stack->{local} ||= { };
297 local $local = $stack->{local};
298 my $res = $proc->{code}();
299 EOC
300 1 for (@nt) {
301 1 $ev .= <<EOC;
302 if (defined \$$_) {
303 \$$_ = q{'}.\$$_ if \$r{$_} || \$$_ =~ /\\W/;
304 push \@{\$stack->{bindings}}, '$_'
305 unless exists \$pad->{$_};
306 \$pad->{$_} = \$$_;
307 } else {
308 delete \$pad->{$_}
309 }
310 EOC
311 }
312 1 $ev .= "\$res\n";
313 1 ▶ $proc->{bindcode} = eval "sub { $ev }";
Subroutine BEGIN: called CC=1 SCAR=0.0
314 1 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
315 }
316  
317 2 $res = $proc->{bindcode}($state->{pad}, $state->{stack}[0]);
318 2 ▶ return $res ? $state : undef
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 2 0 2
319 }
320
321 10 ▶ if ($state->{stack}[0]{ptr}) { # Anything meaningful on the stack?
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'stack'}[0]{'ptr'}) { } 3 7 10
322 3 ▶ print "$indent Loading stack...\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 3 3
323 3 delete $state->{pad}{$_} for @{$state->{stack}[0]{bindings}};
324 3 @{$state->{stack}[0]{bindings}} = ();
325 3 $cptr = $state->{stack}[0]{ptr};
326 3 $dir = 0;
327 }
328 else { # Put something there
329 # $proc is the name of the rule; we want $proc->{next}
330 7 $state->{stack}[0]{ptr} = $cptr = $proc->{next};
331 }
332  
333 10 ▶ while ($cptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr) 13 10 23
334 13 $state->{stack}[0]{ptr} = $cptr; # Tell the stack where we are
335
336 # If we're backtracking, and we skipped on the forward, skip back too
337 goto skip if $cptr->{context} eq 'or' and $state->{stack}[0]{skip}
338 13 ▶ and not $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $cptr->{"context"} eq "or" and $state->{"stack"}[0]{"skip"} and not $dir 0 13 13
Condition33%2/6
$cptr->{"context"} eq "or" and $state->{"stack"}[0]{"skip"} and not $dir
!ll&&!rl&&r
1300
$cptr->{"context"} eq "or" and $state->{"stack"}[0]{"skip"}
!ll&&!rl&&r
1300
MC/DC0%0/3
$cptr->{"context"} eq "or" and $state->{"stack"}[0]{"skip"} and not $dir
$cptr->{"context"} eq "or" $state->{"stack"}[0]{"skip"} not $dir
Truth table25%1/4
A and B and not(C)
A= $cptr->{"context"} eq "or"B= $state->{"stack"}[0]{"skip"}C= not $dir
ABCresult
0XX0
10X0
1100
1111
339
340 goto skip if $cptr->{context} eq 'sub'
341 13 ▶ and not $dir and not $state->{stack}[0]{last};
Branch: 1 branch
BranchTrueFalseTotal
if $cptr->{"context"} eq "sub" and not $dir and not $state->{"stack"}[0]{"last"} 0 13 13
Condition33%2/6
$cptr->{"context"} eq "sub" and not $dir and not $state->{"stack"}[0]{"last"}
!ll&&!rl&&r
1300
$cptr->{"context"} eq "sub" and not $dir
!ll&&!rl&&r
1300
MC/DC0%0/3
$cptr->{"context"} eq "sub" and not $dir and not $state->{"stack"}[0]{"last"}
$cptr->{"context"} eq "sub" not $dir not $state->{"stack"}[0]{"last"}
Truth table25%1/4
A and not(B) and not(C)
A= $cptr->{"context"} eq "sub"B= not $dirC= not $state->{"stack"}[0]{"last"}
ABCresult
0XX0
10X0
1100
1111
342 13 ▶ goto skip if $cptr->{context} eq 'sub' && not $res;
Branch: 1 branch
BranchTrueFalseTotal
if $cptr->{"context"} eq "sub" and not $res 0 13 13
Condition33%1/3
$cptr->{"context"} eq "sub" and not $res
!ll&&!rl&&r
1300
MC/DC0%0/2
$cptr->{"context"} eq "sub" and not $res
$cptr->{"context"} eq "sub" not $res
Truth table33%1/3
A and not(B)
A= $cptr->{"context"} eq "sub"B= not $res
ABresult
0X0
100
111
343  
344 # On forward success in an or chain, skip the current rule
345 goto skip if $cptr->{context} eq 'or' and $res and $dir
346 13 ▶ and $state->{stack}[0]{skip} = 1;
Branch: 1 branch
BranchTrueFalseTotal
if $cptr->{"context"} eq "or" and $res and $dir and $state->{"stack"}[0]{"skip"} = 1 0 13 13
Condition37%3/8
$cptr->{"context"} eq "or" and $res and $dir and $state->{"stack"}[0]{"skip"} = 1
!ll
130
$cptr->{"context"} eq "or" and $res and $dir
!ll&&!rl&&r
1300
$cptr->{"context"} eq "or" and $res
!ll&&!rl&&r
1300
MC/DC0%0/3
$cptr->{"context"} eq "or" and $res and $dir and $state->{"stack"}[0]{"skip"} = 1
$cptr->{"context"} eq "or" $res $dir
Truth table25%1/4
A and B and C
A= $cptr->{"context"} eq "or"B= $resC= $dir
ABCresult
0XX0
10X0
1100
1111
347
348 13 $state->{stack}[0]{skip} = 0;
349
350 # In true context, just go forward
351 13 ▶ if ($cptr->{context} eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{"context"} eq "true") 0 13 13
352 0 $res = $dir = 1;
353 0 goto retry;
354 }
355  
356 # In false context, just go backward (duh)
357 13 ▶ if ($cptr->{context} eq 'false') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{"context"} eq "false") 0 13 13
358 0 $res = $dir = 0;
359 0 goto retry;
360 }
361
362 # If we don't have something to try, try to get something to try
363 13 my $try = $state->{stack}[0]{rule};
364 13 my $frame = $state->{stack}[0]{frame};
365 13 ▶ unless ($frame) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame) 12 1 13
366 print "$indent Look: $cptr->{rule}(",
367 0 ▶ join(', ', map { "$_($state->{pad}{$_})" } @{$cptr->{args}}),
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 12 12
368 ")\n" if $DEBUG;
369 12 $try = $self->lookup($cptr, $state->{stack}[0], $state->{pad});
370 # Fail entirely if we couldn't find anything new
371 12 ▶ unless ($try) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($try) 5 7 12
372 5 ▶ print "$indent Lost\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 5 5
373 5 $res = 0;
374 5 goto retry;
375 }
376 0 ▶ print "$indent Find: $try->{rule}(", join(', ', @{$try->{args}}),
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 7 7
377 ")\n" if $DEBUG;
378  
379 7 ▶ if ($try->{context} eq 'bind') { # Only if it's complex
Branch: 1 branch
BranchTrueFalseTotal
if ($try->{"context"} eq "bind") 2 5 7
380 2 $state->{stack}[0]{rule} = $try;
381 2 $state->{stack}[0]{frame} = $frame = { stack => [], pad => {} };
382 }
383 }
384
385 # Give them variables they need and we have
386 8 $self->copy_pad($cptr, $state => $try, $frame);
387
388 print "$indent Try: $cptr->{rule}(", join(', ',
389 0 ▶ map { $_ . "($state->{pad}{$_})" } @{$cptr->{args}}), ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 8 8
390 if $DEBUG;
391
392 8 ▶ unless ( $res = ! !$self->match($try, $frame, "$indent ") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res = !!$self->match($try, $frame, "$indent ")) 0 8 8
393 0 ▶ print "$indent Fail\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 0 0
394 0 undef $state->{stack}[0]{frame}; # Clear the frame
395 0 next; # Try again
396 }
397
398
399 8 ▶ $dir = 1 if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 8 0 8
400  
401 # Get their variables if they bound any we want
402 8 $self->copy_pad($try, $frame => $cptr, $state, 'bind');
403
404 print "$indent Match: $cptr->{rule}(", join(', ',
405 0 ▶ map { $_ . "($state->{pad}{$_})" } @{$cptr->{args}}), ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::Logic::Predicate::DEBUG 0 8 8
406 if $DEBUG;
407
408 retry:
409 13 ▶ if ($cptr->{context} eq 'sub') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{'context'} eq 'sub') { } 0 13 13
410 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
411 0 ▶ if ($res && $state->{stack}[1]{last}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res and $state->{'stack'}[1]{'last'}) { } 0 0 0
elsif (not $state->{'stack'}[1]{'last'}) { } 0 0 0
Condition0%0/3
$res and $state->{"stack"}[1]{"last"}
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $state->{"stack"}[1]{"last"}
$res $state->{"stack"}[1]{"last"}
Truth table0%0/3
A and B
A= $resB= $state->{"stack"}[1]{"last"}
ABresult
0X0
100
111
412 0 $dir = $res = 0;
413 }
414 elsif (!$state->{stack}[1]{last}) {
415 0 $dir = $res = 0;
416 0 ▶ if ($cptr->{next} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub") 0 0 0
Condition0%0/6
$cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub"
!ll&&!rl&&r
000
$cptr->{"next"}{"context"} eq "or" or $cptr->{"next"}{"context"} eq "sub"
l!l&&r!l&&!r
000
MC/DC0%0/3
$cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub"
$cptr->{"next"} $cptr->{"next"}{"context"} eq "or" $cptr->{"next"}{"context"} eq "sub"
Truth table0%0/4
A and B or C
A= $cptr->{"next"}B= $cptr->{"next"}{"context"} eq "or"C= $cptr->{"next"}{"context"} eq "sub"
ABCresult
0XX0
1000
11X1
1011
417 ($cptr->{next}{context} eq 'or'
418 || $cptr->{next}{context} eq 'sub')) {
419 0 ▶ $dir = 1 unless $state->{stack}[0]{fail} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"stack"}[0]{"fail"} > 1 0 0 0
420 }
421 }
422 else {
423 0 $dir = $res = 1;
424 }
425 }
426 }
427 else {
428 13 $dir = $res;
429 13 ▶ if ($cptr->{next} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub") 0 13 13
Condition50%3/6
$cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub"
!ll&&!rl&&r
1120
$cptr->{"next"}{"context"} eq "or" or $cptr->{"next"}{"context"} eq "sub"
l!l&&r!l&&!r
002
MC/DC0%0/3
$cptr->{"next"} and $cptr->{"next"}{"context"} eq "or" || $cptr->{"next"}{"context"} eq "sub"
$cptr->{"next"} $cptr->{"next"}{"context"} eq "or" $cptr->{"next"}{"context"} eq "sub"
Truth table50%2/4
A and B or C
A= $cptr->{"next"}B= $cptr->{"next"}{"context"} eq "or"C= $cptr->{"next"}{"context"} eq "sub"
ABCresult
0XX0
1000
11X1
1011
430 ($cptr->{next}{context} eq 'or'
431 || $cptr->{next}{context} eq 'sub')) {
432 0 ▶ $dir = 1 unless $state->{stack}[0]{fail} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"stack"}[0]{"fail"} > 1 0 0 0
433 }
434 }
435  
436 skip:
437 13 ▶ if ($dir) { # If we're going forward
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 8 5 13
438 8 $state->{stack}[0]{last} = $res;
439  
440 8 $cptr = $cptr->{next};
441 8 unshift @{$state->{stack}}, { }; # Establish new stack frame
442 }
443 else { # We're going backward
444 5 $cptr = $cptr->{fail};
445 5 for (@{$state->{stack}[0]{bind_stack}}) {
446 0 delete $state->{pad}{$_};
447 }
448 5 @{$state->{stack}[0]{bind_stack}} = ();
449 5 shift @{$state->{stack}}; # Clear the frame
450 # Unbind any variables in the new frame
451 # in order to rebind them this run.
452 5 delete $state->{pad}{$_} for @{$state->{stack}[0]{bindings}};
453 5 @{$state->{stack}[0]{bindings}} = ();
454 }
455 }
456  
457 10 shift @{$state->{stack}};
458 10 ▶ $res ? $state : undef;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 6 4 10
459 }
460  
461 4 ▶sub get(\$$$) {
Subroutine get: called CC=2 SCAR=7.2
Pod undocumented
462 4 my ($self, $iter, $sym) = @_;
463 4 ▶ if (exists $iter->{pad}{$sym}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $iter->{'pad'}{$sym}) { } 4 0 4
464 4 my $ret = $iter->{pad}{$sym};
465 4 $ret =~ s/^'//;
466 4 $ret
467 }
468 else {
469 undef
470 0 }
471 }
472  
473 1 1
474  
475 __DATA__
↑ Summary