| 1 | package Ordeal::Model::Evaluator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # vim: ts=3 sts=3 sw=3 et ai : | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; # redundant, but still useful to document | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | { our $VERSION = '0.002'; } | |||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Mo qw< build default >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Ouch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Ordeal::Model::Deck; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Ordeal::Model::Shuffle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Exporter qw< import >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | our @EXPORT_OK = qw< EVALUATE >; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use experimental qw< signatures postderef >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | no warnings qw< experimental::signatures experimental::postderef >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | has _dc => (); # deck cache | |||||||||||||||||||||||||||
| 22 | 5 | has _model => (); | |||||||||||||||||||||||||||
| 23 | has _rs => ( | ||||||||||||||||||||||||||||
| 24 | default => sub { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | require Ordeal::Model::ChaCha20; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | return Ordeal::Model::ChaCha20->new; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 5 | ); | |||||||||||||||||||||||||||
| 29 | 5 | has _stack => (); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ▶ | sub BUILD ($self) { | |||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | my $m = delete($self->{model}) or ouch 400, 'no model provided'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 2 | $self->_model($m); | |||||||||||||||||||||||||||
| 34 | $self->_rs(delete $self->{random_source}) | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if exists $self->{random_source}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | $self->_dc({}); | |||||||||||||||||||||||||||
| 37 | 2 | $self->_stack([]); | |||||||||||||||||||||||||||
| 38 | 2 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ▶ | sub EVALUATE (%args) { | |||||||||||||||||||||||||||
|
Subroutine
EVALUATE: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $ast = delete($args{ast}) or ouch 400, 'no ast provided'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | return __PACKAGE__->new(%args)->_eval($ast); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | ▶ | sub _eval ($self, $ast) { | |||||||||||||||||||||||||||
|
Subroutine
_eval: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 47 | 10 | my ($op, @params) = $ast->@*; | |||||||||||||||||||||||||||
| 48 | 10 | ▶ | my $method = eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 10 | ▶ | die '' if substr($op, 0, 1) eq '_'; # no "private" stuff | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 10 | ▶ | die '' if lc($op) ne $op; # no "uppercase" stuff | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 10 | $self->can($op); | |||||||||||||||||||||||||||
| 52 | } or ouch 400, 'unknown op', $op; | ||||||||||||||||||||||||||||
| 53 | 10 | return $self->$method(@params); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ▶ | sub _get_integer ($self, $n) { | |||||||||||||||||||||||||||
|
Subroutine
_get_integer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 4 | push $self->_stack->@*, 0; | |||||||||||||||||||||||||||
| 58 | 4 | ($n) = $self->_unroll($n); | |||||||||||||||||||||||||||
| 59 | 4 | pop $self->_stack->@*; | |||||||||||||||||||||||||||
| 60 | 4 | return $n; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ▶ | sub _shuffle ($self, $deck) { | |||||||||||||||||||||||||||
|
Subroutine
_shuffle: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | $deck = Ordeal::Model::Deck->new(cards => $deck) unless blessed $deck; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 4 | return Ordeal::Model::Shuffle->new( | |||||||||||||||||||||||||||
| 66 | auto_reshuffle => 0, | ||||||||||||||||||||||||||||
| 67 | deck => $deck, | ||||||||||||||||||||||||||||
| 68 | default_n_draw => $deck->n_cards, | ||||||||||||||||||||||||||||
| 69 | random_source => $self->_rs, | ||||||||||||||||||||||||||||
| 70 | )->sort; | ||||||||||||||||||||||||||||
| 71 | } ## end sub _shuffle ($self, $deck) | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ▶ | sub _unroll ($self, @potentials) { | |||||||||||||||||||||||||||
|
Subroutine
_unroll: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 74 | 8 | my $N = $self->_stack->[-1]; | |||||||||||||||||||||||||||
| 75 | 18 | ▶ | return map { $N ? ($_ % $N) : $_ } map { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 8 | ▶ | ref($_) ? $self->_eval($_) : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } @potentials; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ▶ | sub math_subtract ($self, $t1, $t2) { | |||||||||||||||||||||||||||
|
Subroutine
math_subtract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 2 | return $self->_get_integer($t1) - $self->_get_integer($t2); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ▶ | sub random ($self, @potentials) { | |||||||||||||||||||||||||||
|
Subroutine
random: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my @candidates = $self->_unroll(@potentials); | |||||||||||||||||||||||||||
| 86 | 0 | return $candidates[$self->_rs->int_rand(0, $#candidates)]; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | ▶ | sub range ($self, $lo, $hi) { | |||||||||||||||||||||||||||
|
Subroutine
range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 2 | ($lo, $hi) = $self->_unroll($lo, $hi); | |||||||||||||||||||||||||||
| 91 | 2 | return $lo .. $hi; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | ▶ | sub repeat ($self, $s_ast, $n) { | |||||||||||||||||||||||||||
|
Subroutine
repeat: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | $n = $self->_get_integer($n); | |||||||||||||||||||||||||||
| 96 | 0 | my @cards; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | while ($n-- > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | my $s = $self->_eval($s_ast); | |||||||||||||||||||||||||||
| 99 | 0 | push @cards, $s->draw; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | return $self->_shuffle(\@cards); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ▶ | sub replicate ($self, $s_ast, $n) { | |||||||||||||||||||||||||||
|
Subroutine
replicate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 0 | $n = $self->_get_integer($n); | |||||||||||||||||||||||||||
| 106 | 0 | my $s = $self->_eval($s_ast); | |||||||||||||||||||||||||||
| 107 | 0 | my @cards = $s->draw; | |||||||||||||||||||||||||||
| 108 | 0 | return $self->_shuffle([(@cards) x $n]); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ▶ | sub resolve ($self, $shuffle) { | |||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=4 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | return $shuffle | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($shuffle) and $shuffle->isa("Ordeal::Model::Shuffle")
MC/DC0%0/2
builtin::blessed($shuffle) and $shuffle->isa("Ordeal::Model::Shuffle")
builtin::blessed($shuffle)
$shuffle->isa("Ordeal::Model::Shuffle")
Truth table33%1/3
A and B
A= builtin::blessed($shuffle)B= $shuffle->isa("Ordeal::Model::Shuffle")
| |||||||||||||||||||||||||||||
| 113 | if blessed($shuffle) && $shuffle->isa('Ordeal::Model::Shuffle'); | ||||||||||||||||||||||||||||
| 114 | 2 | ▶ | my $deck = $self->_dc->{$shuffle} //= $self->_model->get_deck($shuffle); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_dc->{$shuffle} //= $self->_model->get_deck($shuffle)
MC/DC0%0/2
$self->_dc->{$shuffle} //= $self->_model->get_deck($shuffle)
$self->_dc->{$shuffle}
$self->_model->get_deck($shuffle)
Truth table33%1/3
A //= B
A= $self->_dc->{$shuffle}B= $self->_model->get_deck($shuffle)
| |||||||||||||||||||||||||||||
| 115 | 2 | return $self->_shuffle($deck); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | sub shuffle ($self, $s_ast) { return $self->_eval($s_ast)->shuffle } | ||||||||||||||||||||||||||
|
Subroutine
shuffle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | ▶ | sub slice ($self, $s_ast, @slices) { | |||||||||||||||||||||||||||
|
Subroutine
slice: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | my $s = $self->_eval($s_ast) # 400's upon error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | or ouch 500, 'slice: invalid AST', $s_ast; # "my" error => 500 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | push $self->_stack->@*, $s->deck->n_cards; | |||||||||||||||||||||||||||
| 125 | 2 | my @indexes = $self->_unroll(@slices); | |||||||||||||||||||||||||||
| 126 | 2 | pop $self->_stack->@*; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | my $max = 0; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | $max = ($max < $_ ? $_ : $max) for @indexes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 2 | my @cards = $s->draw($max + 1); | |||||||||||||||||||||||||||
| 131 | 2 | return $self->_shuffle([@cards[@indexes]]); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub sort ($self, $s_ast) { return $self->_eval($s_ast)->sort } | ||||||||||||||||||||||||||
|
Subroutine
sort: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ▶ | sub subtract ($self, $s1_ast, $s2_ast) { | |||||||||||||||||||||||||||
|
Subroutine
subtract: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 0 | my $s1 = $self->_eval($s1_ast); | |||||||||||||||||||||||||||
| 138 | 0 | my $s2 = $self->_eval($s2_ast); | |||||||||||||||||||||||||||
| 139 | 0 | my @cards = $s1->draw; | |||||||||||||||||||||||||||
| 140 | 0 | for my $deleted ($s2->draw) { | |||||||||||||||||||||||||||
| 141 | 0 | for my $i (0 .. $#cards) { | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | next if $cards[$i] ne $deleted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | splice @cards, $i, 1; | |||||||||||||||||||||||||||
| 144 | 0 | last; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | return $self->_shuffle(\@cards); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ▶ | sub sum ($self, $s1_ast, $s2_ast) { | |||||||||||||||||||||||||||
|
Subroutine
sum: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $s1 = $self->_eval($s1_ast); | |||||||||||||||||||||||||||
| 152 | 0 | my $s2 = $self->_eval($s2_ast); | |||||||||||||||||||||||||||
| 153 | 0 | return $self->_shuffle([$s1->draw, $s2->draw]); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 5 | 1; | |||||||||||||||||||||||||||