| 1 | ## no critic (RcsKeywords,PodSections,InterpolationOfMetachars,EmptyQuotes,ConstantPragma,InitializationForLocalVars,LocalVars,PunctuationVars) | ||||||||||||||||||||||||||||
| 2 | package AI::Prolog::Engine::Primitives; | ||||||||||||||||||||||||||||
| 3 | 13 | $REVISION = '$Id: Primitives.pm,v 1.1 2005/08/06 23:28:40 ovid Exp $'; | |||||||||||||||||||||||||||
| 4 | 13 | $VERSION = '0.3'; | |||||||||||||||||||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use base 'AI::Prolog::Engine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use aliased 'AI::Prolog::Term'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use aliased 'AI::Prolog::Term::Cut'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use aliased 'AI::Prolog::Term::Number'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use aliased 'AI::Prolog::TermList'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use aliased 'AI::Prolog::TermList::Step'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use aliased 'AI::Prolog::ChoicePoint'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | my %DESCRIPTION_FOR; | |||||||||||||||||||||||||||
| 19 | 13 | my $LONGEST_PREDICATE = ''; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub _load_builtins { | ||||||||||||||||||||||||||
|
Subroutine
_load_builtins: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | return if keys %DESCRIPTION_FOR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | require Pod::Simple::Text; | |||||||||||||||||||||||||||
| 24 | 0 | require Pod::Perldoc; | |||||||||||||||||||||||||||
| 25 | 0 | my $perldoc = Pod::Perldoc->new; | |||||||||||||||||||||||||||
| 26 | 0 | my $builtin_pod = 'AI::Prolog::Builtins'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my ($found) = $perldoc->grand_search_init( [$builtin_pod] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | or die "Help failed. Cannot find documentation for $builtin_pod: $!"; | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | open my $fh, '<', $found | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | or die "Cannot open $found for reading: ($!)"; | ||||||||||||||||||||||||||||
| 32 | 0 | my @lines = <$fh>; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | close $fh or die "Cannot close $found: ($!)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | while (@lines) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | my $line = shift @lines; | |||||||||||||||||||||||||||
| 37 | 0 | my $predicate; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( $line =~ /\A=item\s*(\S+)/mx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | $predicate = $1; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( $predicate =~ m{.*/\d+}mx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | my @pod = "=head1 $predicate"; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( length $predicate > length $LONGEST_PREDICATE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | $LONGEST_PREDICATE = $predicate; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | while ( $line = shift @lines ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( $line =~ /\A=(?:item|back)/mx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | unshift @lines => $line; | |||||||||||||||||||||||||||
| 48 | 0 | last; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | push @pod => $line; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | push @pod => '=cut'; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # XXX I hate instantiating this here, but there | ||||||||||||||||||||||||||||
| 55 | # appears to be a bug in parsing if I don't :( | ||||||||||||||||||||||||||||
| 56 | 0 | my $parser = Pod::Simple::Text->new; | |||||||||||||||||||||||||||
| 57 | 0 | my $output; | |||||||||||||||||||||||||||
| 58 | 0 | $parser->output_string( \$output ); | |||||||||||||||||||||||||||
| 59 | 0 | $parser->parse_lines( @pod, undef ); | |||||||||||||||||||||||||||
| 60 | 0 | $DESCRIPTION_FOR{$predicate} = $output; | |||||||||||||||||||||||||||
| 61 | 0 | $output = ''; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub _remove_choices { | ||||||||||||||||||||||||||
|
Subroutine
_remove_choices: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # this implements the cut operator | ||||||||||||||||||||||||||||
| 72 | 1 | my ( $self, $varid ) = @_; | |||||||||||||||||||||||||||
| 73 | 1 | my @stack; | |||||||||||||||||||||||||||
| 74 | 1 | my $i = @{ $self->{_stack} }; | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | while ( $i > $varid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 3 | my $o = pop @{ $self->{_stack} }; | |||||||||||||||||||||||||||
| 77 | 3 | ▶ | if ( not $o->isa(ChoicePoint) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 2 | push @stack => $o; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 3 | $i--; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | while (@stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | push @{ $self->{_stack} } => pop @stack; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _splice_goal_list { | ||||||||||||||||||||||||||
|
Subroutine
_splice_goal_list: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | my ( $t2, $p, $p1, $ptail ); | |||||||||||||||||||||||||||
| 92 | 0 | my @vars; | |||||||||||||||||||||||||||
| 93 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 94 | 0 | $term = $term->getarg(0); | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | while ( $term && $term->getfunctor ne 'null' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$term and $term->getfunctor ne "null"
MC/DC0%0/2
$term and $term->getfunctor ne "null"
$term
$term->getfunctor ne "null"
Truth table0%0/3
A and B
A= $termB= $term->getfunctor ne "null"
| |||||||||||||||||||||||||||||
| 96 | 0 | $t2 = $term->getarg(0); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $t2 eq Term->CUT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | $p = TermList->new( Cut->new( scalar @{ $self->{_stack} } ) ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | 0 | $p = TermList->new($t2); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $i++ == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $p1 = $ptail = $p; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | $ptail->next($p); | |||||||||||||||||||||||||||
| 108 | 0 | $ptail = $p; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | $term = $term->getarg(1); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | $ptail->next( $self->{_goal}->next ); | |||||||||||||||||||||||||||
| 113 | 0 | $self->{_goal} = $p1; | |||||||||||||||||||||||||||
| 114 | 0 | $self->{_goal}->resolve( $self->{_db} ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 13 | ▶ | use constant CONTINUE => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 13 | ▶ | use constant RETURN => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 13 | ▶ | use constant FAIL => (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 13 | my @PRIMITIVES; # we'll fix this later | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | $PRIMITIVES[1] = sub { # !/0 (cut) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 1 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 126 | 1 | _remove_choices( $self, $term->varid ); | |||||||||||||||||||||||||||
| 127 | 1 | CONTINUE; | |||||||||||||||||||||||||||
| 128 | 13 | }; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $PRIMITIVES[2] = sub { # call/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 132 | 0 | $self->{_goal} = TermList->new( $term->getarg(0), $self->{_goal}->next ); | |||||||||||||||||||||||||||
| 133 | 0 | $self->{_goal}->resolve( $self->{_db} ); | |||||||||||||||||||||||||||
| 134 | 0 | RETURN; | |||||||||||||||||||||||||||
| 135 | 13 | }; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | $PRIMITIVES[3] = sub { # fail/0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 1 | FAIL; | |||||||||||||||||||||||||||
| 139 | 13 | }; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $PRIMITIVES[4] = sub { # consult/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 142 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 143 | 0 | my $file = $term->getarg(0)->getfunctor; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ( open my $fh, '<', $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Avoid do { local $/; <$fh> }. This triggers a bug where | ||||||||||||||||||||||||||||
| 147 | # *two* copies of the string are made. Double space is | ||||||||||||||||||||||||||||
| 148 | # required. | ||||||||||||||||||||||||||||
| 149 | 0 | my $prolog; | |||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 0 | local $/; | |||||||||||||||||||||||||||
| 152 | 0 | $prolog = <$fh>; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | $self->{_db}->consult($prolog); | |||||||||||||||||||||||||||
| 155 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 0 | warn "Could not open ($file) for reading: $!"; | |||||||||||||||||||||||||||
| 159 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 13 | }; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $PRIMITIVES[5] = sub { # assert/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 165 | 0 | $self->{_db}->assert( $term->getarg(0) ); | |||||||||||||||||||||||||||
| 166 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 167 | 13 | }; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $PRIMITIVES[7] = sub { # retract/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 170 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ( not $self->{_db}->retract( $term->getarg(0), $self->{_stack} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | $self->backtrack; | |||||||||||||||||||||||||||
| 173 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | $self->{_cp}->clause( $self->{_retract_clause} ) | ||||||||||||||||||||||||||||
| 176 | 0 | ; # if $self->{_cp}; # doesn't work | |||||||||||||||||||||||||||
| 177 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 178 | 13 | }; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | $PRIMITIVES[8] = sub { # listing/0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 181 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | 0 | $self->{_db}->dump(0); | |||||||||||||||||||||||||||
| 183 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 184 | 13 | }; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | $PRIMITIVES[9] = sub { # listing/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 187 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 188 | 0 | my $predicate = $term->getarg(0)->getfunctor; | |||||||||||||||||||||||||||
| 189 | 0 | $self->{_db}->list($predicate); | |||||||||||||||||||||||||||
| 190 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 191 | 13 | }; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $PRIMITIVES[10] = sub { # print/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 194 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 195 | 0 | AI::Prolog::Engine::_print( $term->getarg(0)->to_string ); | |||||||||||||||||||||||||||
| 196 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 197 | 13 | }; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $PRIMITIVES[11] = sub { # println/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 200 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 201 | 0 | AI::Prolog::Engine::_print( $term->getarg(0)->to_string . "\n" ); | |||||||||||||||||||||||||||
| 202 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 203 | 13 | }; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $PRIMITIVES[12] = sub { AI::Prolog::Engine::_print("\n"); CONTINUE }; # nl | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $PRIMITIVES[13] = sub { # trace. notrace. | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 208 | 0 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 209 | 0 | $self->{_trace} = $term->getfunctor eq 'trace'; | |||||||||||||||||||||||||||
| 210 | AI::Prolog::Engine::_print( | ||||||||||||||||||||||||||||
| 211 | 0 | ▶ | 'Trace ' . ( $self->{_trace} ? 'ON' : 'OFF' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 213 | 13 | }; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 27 | ▶ | $PRIMITIVES[15] = sub { # is/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 216 | 27 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 217 | 27 | my $rhs = $term->getarg(0)->deref; | |||||||||||||||||||||||||||
| 218 | 27 | my $lhs = $term->getarg(1)->value; | |||||||||||||||||||||||||||
| 219 | 26 | ▶ | if ( $rhs->is_bound ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 2 | my $value = $rhs->value; | |||||||||||||||||||||||||||
| 221 | 2 | ▶ | if ( not looks_like_number($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 2 | return $value == $lhs; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 24 | $rhs->bind( Number->new($lhs) ); | |||||||||||||||||||||||||||
| 227 | 24 | push @{ $self->{_stack} } => $rhs; | |||||||||||||||||||||||||||
| 228 | 24 | return CONTINUE; | |||||||||||||||||||||||||||
| 229 | 13 | }; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 11 | ▶ | $PRIMITIVES[16] = sub { # gt/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 232 | 11 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 233 | 11 | ▶ | return ( $term->getarg(0)->value > $term->getarg(1)->value ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | ? CONTINUE | ||||||||||||||||||||||||||||
| 235 | : FAIL; | ||||||||||||||||||||||||||||
| 236 | 13 | }; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 3 | ▶ | $PRIMITIVES[17] = sub { # lt/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 239 | 3 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 240 | 3 | ▶ | return ( $term->getarg(0)->value < $term->getarg(1)->value ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | ? CONTINUE | ||||||||||||||||||||||||||||
| 242 | : FAIL; | ||||||||||||||||||||||||||||
| 243 | 13 | }; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | ▶ | $PRIMITIVES[19] = sub { # ge/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 3 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 247 | 3 | ▶ | return ( $term->getarg(0)->value >= $term->getarg(1)->value ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | ? CONTINUE | ||||||||||||||||||||||||||||
| 249 | : FAIL; | ||||||||||||||||||||||||||||
| 250 | 13 | }; | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 3 | ▶ | $PRIMITIVES[20] = sub { # le/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 253 | 3 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 254 | 3 | ▶ | return ( $term->getarg(0)->value <= $term->getarg(1)->value ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | ? CONTINUE | ||||||||||||||||||||||||||||
| 256 | : FAIL; | ||||||||||||||||||||||||||||
| 257 | 13 | }; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $PRIMITIVES[22] = sub { # halt/0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 260 | 0 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 261 | 0 | $self->halt(1); | |||||||||||||||||||||||||||
| 262 | 0 | CONTINUE; | |||||||||||||||||||||||||||
| 263 | 13 | }; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $PRIMITIVES[23] = sub { # var/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 266 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 267 | 0 | ▶ | return $term->getarg(0)->bound() ? FAIL : CONTINUE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 13 | }; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # plus(X,Y) := 25. | ||||||||||||||||||||||||||||
| 271 | # minux(X,Y) := 26. | ||||||||||||||||||||||||||||
| 272 | # mult(X,Y) := 27. | ||||||||||||||||||||||||||||
| 273 | # div(X,Y) := 28. | ||||||||||||||||||||||||||||
| 274 | # mod(X,Y) := 29. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | ▶ | $PRIMITIVES[30] = sub { # seq/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 277 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 278 | 0 | $self->_splice_goal_list($term); | |||||||||||||||||||||||||||
| 279 | 0 | CONTINUE; | |||||||||||||||||||||||||||
| 280 | 13 | }; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 13 | my $HELP_OUTPUT; | |||||||||||||||||||||||||||
| 283 | 0 | ▶ | $PRIMITIVES[31] = sub { # help/0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 284 | 0 | _load_builtins(); | |||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ( not $HELP_OUTPUT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | $HELP_OUTPUT = "Help is available for the following builtins:\n\n"; | |||||||||||||||||||||||||||
| 287 | 0 | my @predicates = sort keys %DESCRIPTION_FOR; | |||||||||||||||||||||||||||
| 288 | 0 | my $length = length $LONGEST_PREDICATE; | |||||||||||||||||||||||||||
| 289 | 0 | my $columns = 5; | |||||||||||||||||||||||||||
| 290 | 0 | my $format = join ' ' => ("%-${length}s") x $columns; | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | while (@predicates) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | my @row; | |||||||||||||||||||||||||||
| 293 | 0 | for ( 1 .. $columns ) { | |||||||||||||||||||||||||||
| 294 | 0 | ▶ | push @row => @predicates | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | ? shift @predicates | ||||||||||||||||||||||||||||
| 296 | : ''; | ||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | 0 | $HELP_OUTPUT .= sprintf $format => @row; | |||||||||||||||||||||||||||
| 299 | 0 | $HELP_OUTPUT .= "\n"; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | 0 | $HELP_OUTPUT .= "\n"; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | 0 | AI::Prolog::Engine::_print($HELP_OUTPUT); | |||||||||||||||||||||||||||
| 304 | 0 | CONTINUE; | |||||||||||||||||||||||||||
| 305 | 13 | }; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $PRIMITIVES[32] = sub { # help/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 308 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 309 | 0 | my $predicate = $term->getarg(0)->to_string; | |||||||||||||||||||||||||||
| 310 | 0 | _load_builtins(); | |||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ( my $description = $DESCRIPTION_FOR{$predicate} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | AI::Prolog::Engine::_print($description); | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | else { | ||||||||||||||||||||||||||||
| 315 | 0 | AI::Prolog::Engine::_print("No help available for ($predicate)\n\n"); | |||||||||||||||||||||||||||
| 316 | 0 | $PRIMITIVES[31]->(); | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | 0 | CONTINUE; | |||||||||||||||||||||||||||
| 319 | 13 | }; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 13 | my $gensym_int = 0; | |||||||||||||||||||||||||||
| 322 | 0 | ▶ | $PRIMITIVES[33] = sub { # gemsym/1 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 323 | 0 | my ( $self, $term, $c ) = @_; | |||||||||||||||||||||||||||
| 324 | 0 | my $t2 = Term->new( 'v' . $gensym_int++, 0 ); | |||||||||||||||||||||||||||
| 325 | return $t2->unify( $term->getarg(0), $self->{_stack} ) | ||||||||||||||||||||||||||||
| 326 | 0 | ▶ | ? CONTINUE | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | : FAIL; | ||||||||||||||||||||||||||||
| 328 | 13 | }; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 13 | ▶ | use constant UNDEFINED_SUBROUTINE_ERROR => do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 331 | 13 | eval { | |||||||||||||||||||||||||||
| 332 | 13 | ▶ | no strict 'refs'; ## no critic NoStrict | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 333 | 13 | &{'---'}; | |||||||||||||||||||||||||||
| 334 | }; | ||||||||||||||||||||||||||||
| 335 | 13 | my $e = $@; | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # Undefined subroutine &main::--- called at .../Primitives.pm line 12. | ||||||||||||||||||||||||||||
| 338 | 13 | ▶ | my ($msg) = $e =~ / \A | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | (.+) # 'Undefined subroutine' | ||||||||||||||||||||||||||||
| 340 | (?<=\s) # ' ' | ||||||||||||||||||||||||||||
| 341 | \S* # &main:: | ||||||||||||||||||||||||||||
| 342 | ---/mx | ||||||||||||||||||||||||||||
| 343 | or die q[Perl's error message changed! Damn! Fix this regex.]; | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 13 | $msg; | |||||||||||||||||||||||||||
| 346 | 13 | }; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | $PRIMITIVES[34] = sub { # perlcall2/2 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=17 SCAR=54.2
| |||||||||||||||||||||||||||||
| 349 | 0 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Get a function name... | ||||||||||||||||||||||||||||
| 352 | 0 | my $function_term = $term->getarg(0); | |||||||||||||||||||||||||||
| 353 | 0 | ▶ | if ( not $function_term->is_bound ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | 0 | my $function_name = $function_term->to_string; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Lookup a fully qualified function name... | ||||||||||||||||||||||||||||
| 359 | 0 | my $function_ref; | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | if ( $function_name =~ /[:\']/mx ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 361 | 0 | $function_ref = $function_name; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | elsif ( defined( my $package = $self->{_perlpackage} ) ) { | ||||||||||||||||||||||||||||
| 364 | 0 | $function_name = "$package\::$function_name"; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | # Search the call stack... | ||||||||||||||||||||||||||||
| 368 | 0 | ▶ | if ( not defined $function_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | my $cx = 1; | |||||||||||||||||||||||||||
| 370 | 0 | my %packages; | |||||||||||||||||||||||||||
| 371 | CX: | ||||||||||||||||||||||||||||
| 372 | 0 | ▶ | while ( my $package = caller $cx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # Don't retry packages... | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | next if exists $packages{$package}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | $packages{$package} = undef; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # AUTOLOAD using packages are expected to provide a | ||||||||||||||||||||||||||||
| 379 | # ->can() that works. I don't know if that's a widely | ||||||||||||||||||||||||||||
| 380 | # known expectation but it's what I'm going to go | ||||||||||||||||||||||||||||
| 381 | # with. Hash::AsObject gets this wrong. | ||||||||||||||||||||||||||||
| 382 | 0 | ▶ | if (do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
do {
no strict 'refs';
defined &{"${package}::$function_name";}
} or $package->can($function_name)
MC/DC0%0/2
do {
no strict 'refs';
defined &{"${package}::$function_name";}
} or $package->can($function_name)
do {
no strict 'refs';
defined &{"${package}::$function_name";}
}
$package->can($function_name)
Truth table0%0/3
A or B
A= do {
no strict 'refs';
defined &{"${package}::$function_name";}
}B= $package->can($function_name)
| |||||||||||||||||||||||||||||
| 383 | 13 | ▶ | no strict 'refs'; ## no critic NoStrict | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 384 | 0 | defined &{"$package\::$function_name"}; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | or $package->can($function_name) | ||||||||||||||||||||||||||||
| 387 | ) | ||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||
| 389 | 0 | $function_ref = "$package\::$function_name"; | |||||||||||||||||||||||||||
| 390 | 0 | last CX; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | continue { | ||||||||||||||||||||||||||||
| 394 | 0 | ++$cx; | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | # We got nuthin! Damn! I'll try for the first AUTOLOAD. | ||||||||||||||||||||||||||||
| 399 | 0 | ▶ | if ( not defined $function_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 0 | my $cx = 1; | |||||||||||||||||||||||||||
| 401 | 0 | my %packages; | |||||||||||||||||||||||||||
| 402 | AUTOLOAD_CX: | ||||||||||||||||||||||||||||
| 403 | 0 | ▶ | while ( my ($package) = caller $cx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 0 | ▶ | next if exists $packages{$package}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 0 | $packages{$package} = undef; | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 0 | ▶ | if (do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
do {
no strict 'refs';
defined &{"${package}::AUTOLOAD";}
} or $package->can("AUTOLOAD")
MC/DC0%0/2
do {
no strict 'refs';
defined &{"${package}::AUTOLOAD";}
} or $package->can("AUTOLOAD")
do {
no strict 'refs';
defined &{"${package}::AUTOLOAD";}
}
$package->can("AUTOLOAD")
Truth table0%0/3
A or B
A= do {
no strict 'refs';
defined &{"${package}::AUTOLOAD";}
}B= $package->can("AUTOLOAD")
| |||||||||||||||||||||||||||||
| 408 | 13 | ▶ | no strict 'refs'; ## no critic NoStrict | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 409 | 0 | defined &{"$package\::AUTOLOAD"}; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | or $package->can('AUTOLOAD') | ||||||||||||||||||||||||||||
| 412 | ) | ||||||||||||||||||||||||||||
| 413 | { | ||||||||||||||||||||||||||||
| 414 | 0 | $function_ref = "$package\::$function_name"; | |||||||||||||||||||||||||||
| 415 | 0 | last AUTOLOAD_CX; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | continue { | ||||||||||||||||||||||||||||
| 419 | 0 | ++$cx; | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | ▶ | if ( not defined $function_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # XXX What do to with the first arg? | ||||||||||||||||||||||||||||
| 428 | 0 | my ( undef, $results_ref ) = $term->getarg(1)->to_data; | |||||||||||||||||||||||||||
| 429 | 0 | my @results = @{ $results_ref->[0] }; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 0 | eval { | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 13 | ▶ | no strict 'refs'; ## no critic NoStrict | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 434 | 0 | $function_ref->(@results); | |||||||||||||||||||||||||||
| 435 | }; | ||||||||||||||||||||||||||||
| 436 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | # Extreme caution here. | ||||||||||||||||||||||||||||
| 439 | 0 | ▶ | if ( $e =~ UNDEFINED_SUBROUTINE_ERROR ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | 0 | return FAIL; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 0 | return CONTINUE; | |||||||||||||||||||||||||||
| 445 | 13 | }; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 49 | ▶ | sub find { return $PRIMITIVES[ $_[1] ] } | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 13 | 1; | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | __END__ | ||||||||||||||||||||||||||||