| 1 | package App::EvalServerAdvanced::ConstantCalc; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: turns strings and constants into values | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.24; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Function::Parameters; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | has constants => (is => 'ro', default => sub {+{}}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | has _parser => (is => 'ro', default => sub {App::EvalServerAdvanced::ConstantCalc::Parser->new(consts => $_[0])}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | method get_value($key) { | ||||||||||||||||||||||||||||||
|
Subroutine
get_value: called CC=4 SCAR=14.3
Pod
documented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | die "Missing constant [$key]" unless exists($self->constants->{$key}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 2 | return $self->constants->{$key}; | |||||||||||||||||||||||||||||||
| 19 | 1 | } | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | method add_constant($key, $value) { | ||||||||||||||||||||||||||||||
|
Subroutine
add_constant: called CC=9 SCAR=26.0
Pod
documented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | die "Invalid key [$key]" if ($key =~ /\s/ || $key =~ /^\s*\d/); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key =~ /\s/u or $key =~ /^\s*\d/u
MC/DC0%0/2
$key =~ /\s/u or $key =~ /^\s*\d/u
$key =~ /\s/u
$key =~ /^\s*\d/u
Truth table33%1/3
A or B
A= $key =~ /\s/uB= $key =~ /^\s*\d/u
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | if (exists($self->constants->{$key}) && defined(my $eval = $self->constants->{$key})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->constants->{$key} and defined(my $eval = $self->constants->{$key})
MC/DC0%0/2
exists $self->constants->{$key} and defined(my $eval = $self->constants->{$key})
exists $self->constants->{$key}
defined(my $eval = $self->constants->{$key})
Truth table33%1/3
A and B
A= exists $self->constants->{$key}B= defined(my $eval = $self->constants->{$key})
| |||||||||||||||||||||||||||||||||
| 25 | 0 | die "Cannot redefine a constant [$key]. Existing value [$eval] new value [$value]" | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | die "Value undefined for [$key]" unless defined($value); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | die "Value [$value] for [$key] must be an integer" if ($value =~ /[^xob\d\-+_]/i); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 3 | $self->constants->{$key} = App::EvalServerAdvanced::ConstantCalc::Parser::_to_int($value); | |||||||||||||||||||||||||||||||
| 32 | 1 | } | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | method calculate($string) { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 35 | 7 | return $self->_parser->from_string($string); | |||||||||||||||||||||||||||||||
| 36 | 1 | } | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | package | ||||||||||||||||||||||||||||||||
| 39 | App::EvalServerAdvanced::ConstantCalc::Parser; | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # Ensure we can't accidentally turn to strings, or floats, or anything other than an integer | ||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | no warnings 'experimental::bitwise'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use feature 'bitwise'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use parent qw/Parser::MGC/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use Function::Parameters; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | method new($class: %args) { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Branch: 2 branches
Condition33%1/3
@_ > 1 and (@_ - 1) % 2
MC/DC0%0/2
@_ > 1 and (@_ - 1) % 2
@_ > 1
(@_ - 1) % 2
Truth table33%1/3
A and B
A= @_ > 1B= (@_ - 1) % 2
| |||||||||||||||||||||||||||||||||
| 53 | 1 | my $consts = delete $args{consts}; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 1 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 1 | $self->{_private}{consts} = $consts; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 60 | 1 | } | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | method consts() { | ||||||||||||||||||||||||||||||
|
Subroutine
consts: called CC=3 SCAR=11.3
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 63 | 2 | return $self->{_private}{consts}; | |||||||||||||||||||||||||||||||
| 64 | 1 | } | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 20 | ▶ | method parse_upper() { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_upper: called CC=4 SCAR=13.9
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 67 | 20 | my $val = $self->parse_term(); | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 1 while $self->any_of( | ||||||||||||||||||||||||||||||||
| 70 | 26 | ▶ | sub {$self->expect("&"); $val &= $self->parse_term(); 1}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 71 | 20 | ▶ | sub {0} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 72 | 20 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 20 | return $val; | |||||||||||||||||||||||||||||||
| 75 | 1 | } | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 11 | ▶ | method parse() { | ||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 78 | 11 | my $val = $self->parse_upper(); | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 1 while $self->any_of( | ||||||||||||||||||||||||||||||||
| 81 | 20 | ▶ | sub {$self->expect("^"); $val ^= $self->parse_upper(); 1 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 82 | 16 | ▶ | sub {$self->expect("|"); $val |= $self->parse_upper(); 1 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 83 | 11 | ▶ | sub {0} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 84 | 11 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 11 | return $val; | |||||||||||||||||||||||||||||||
| 87 | 1 | } | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 28 | ▶ | method parse_term() { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_term: called CC=3 SCAR=11.1
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 90 | $self->any_of( | ||||||||||||||||||||||||||||||||
| 91 | 28 | ▶ | sub { $self->scope_of( "(", sub { $self->parse }, ")" ) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 92 | 24 | ▶ | sub { $self->expect('~['); my $bitdepth=$self->token_int; $self->expect(']'); my $val = $self->parse_term; (~ ($val & _get_mask($bitdepth))) & _get_mask($bitdepth)}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub { $self->expect('~'); ~$self->parse_term}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 94 | 22 | ▶ | sub { $self->token_constant }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 95 | 20 | ▶ | sub { $self->token_int }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 96 | 28 | ); | |||||||||||||||||||||||||||||||
| 97 | 1 | } | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 22 | ▶ | method token_int() { | ||||||||||||||||||||||||||||||
|
Subroutine
token_int: called CC=3 SCAR=11.1
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 100 | 0+$self->any_of( | ||||||||||||||||||||||||||||||||
| 101 | 22 | ▶ | sub {_to_int($self->expect(qr/0x[0-9A-F_]+/i));}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 102 | 21 | ▶ | sub {_to_int($self->expect(qr/0b[0-7_]+/i));}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 103 | 20 | ▶ | sub {_to_int($self->expect(qr/0o?[0-7_]+/i));}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 104 | 19 | ▶ | sub {$self->expect(qr/\d+/)} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 105 | 22 | ); | |||||||||||||||||||||||||||||||
| 106 | 1 | } | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 22 | ▶ | method token_constant() { | ||||||||||||||||||||||||||||||
|
Subroutine
token_constant: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 109 | 22 | my $const = $self->expect(qr/[a-z_][a-z_0-9]+/i); | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 2 | $self->consts->get_value($const); | |||||||||||||||||||||||||||||||
| 112 | 1 | } | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | fun _get_mask($size) { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_mask: called CC=3 SCAR=11.3
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 115 | 4 | return 2**($size)-1; | |||||||||||||||||||||||||||||||
| 116 | 1 | } | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 6 | ▶ | fun _to_int($val) { | ||||||||||||||||||||||||||||||
|
Subroutine
_to_int: called CC=4 SCAR=14.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 120 | 6 | $val =~ s/^0o/0/i; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | if ($val =~ /^0/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | 4 | return oct $val; | |||||||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||||||
| 125 | 2 | return 0+$val; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | 1 | } | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||