App-EvalServerAdvanced-ConstantCalc
view release on metacpan or search on metacpan
lib/App/EvalServerAdvanced/ConstantCalc.pm view on Meta::CPAN
package App::EvalServerAdvanced::ConstantCalc;
our $VERSION = '0.06';
# ABSTRACT: turns strings and constants into values
use v5.24;
use Moo;
use Function::Parameters;
use Data::Dumper;
has constants => (is => 'ro', default => sub {+{}});
has _parser => (is => 'ro', default => sub {App::EvalServerAdvanced::ConstantCalc::Parser->new(consts => $_[0])});
method get_value($key) {
die "Missing constant [$key]" unless exists($self->constants->{$key});
return $self->constants->{$key};
}
method add_constant($key, $value) {
die "Invalid key [$key]" if ($key =~ /\s/ || $key =~ /^\s*\d/);
if (exists($self->constants->{$key}) && defined(my $eval = $self->constants->{$key})) {
die "Cannot redefine a constant [$key]. Existing value [$eval] new value [$value]"
}
die "Value undefined for [$key]" unless defined($value);
die "Value [$value] for [$key] must be an integer" if ($value =~ /[^xob\d\-+_]/i);
$self->constants->{$key} = App::EvalServerAdvanced::ConstantCalc::Parser::_to_int($value);
}
method calculate($string) {
return $self->_parser->from_string($string);
}
package
App::EvalServerAdvanced::ConstantCalc::Parser;
use strict;
use warnings;
# Ensure we can't accidentally turn to strings, or floats, or anything other than an integer
use integer;
no warnings 'experimental::bitwise';
use feature 'bitwise';
use parent qw/Parser::MGC/;
use Function::Parameters;
method new($class: %args) {
my $consts = delete $args{consts};
my $self = $class->SUPER::new(%args);
$self->{_private}{consts} = $consts;
return $self;
}
method consts() {
return $self->{_private}{consts};
}
method parse_upper() {
my $val = $self->parse_term();
1 while $self->any_of(
sub {$self->expect("&"); $val &= $self->parse_term(); 1},
( run in 1.276 second using v1.01-cache-2.11-cpan-39bf76dae61 )