Skip to content

Commit

Permalink
make the context a package variable and add the number pattern to the…
Browse files Browse the repository at this point in the history
… context.
  • Loading branch information
pstaabp committed Sep 26, 2023
1 parent bbae128 commit 66558ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions macros/contexts/contextNondecimalBase.pl
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,21 @@ =head3 Examples
=cut

my $convertContext;

sub convert {
my ($value, %options) = @_;
my $from = $options{'from'} // 10;
my $to = $options{'to'} // 10;

my $context = $main::context{NondecimalBase}->copy;
$convertContext = $main::context{NondecimalBase}->copy unless $convertContext;
if ($from != 10) {
$context->setBase($from);
$value = $context->fromBase($value);
$convertContext->setBase($from);
$value = $convertContext->fromBase($value);
}
if ($to != 10) {
$context->setBase($to);
$value = $context->toBase($value);
$convertContext->setBase($to);
$value = $convertContext->toBase($value);
}
return $value;
}
Expand All @@ -159,12 +161,13 @@ sub new {
$context->{value}{Real} = 'context::NondecimalBase::Real';
$context->functions->disable('All');
$context->constants->clear();
$self->{pattern}{number} = '[' . join('', 0 .. 9, 'A' .. 'Z') . ']+';
$context->{pattern}{number} = '[' . join('', 0 .. 9, 'A' .. 'Z') . ']+';
$context->{precedence}{NondecimalBase} = $context->{precedence}{special};
$context->flags->set(limits => [ -1000, 1000, 1 ]);
return $context;
}


# set the base of the context. Either base a number >=2 or an arrayref of digits.
sub setBase {
my ($self, $base) = @_;
Expand All @@ -184,6 +187,10 @@ sub setBase {
$self->{base} = $base;
$self->{digits} = $digits;
$self->{digitMap} = { map { ($digits->[$_], $_) } (0 .. $base - 1) };
$self->{pattern}{number} = '[' . join('', @$digits) . ']+';
my $msg = 'Numbers should consist only of the digits: ' . join(',', @$digits);
$self->{error}{msg}{"Variable '%s' is not defined in this context"} = $msg;
$self->{error}{msg}{"'%s' is not defined in this context"} = $msg;
$self->update;
}

Expand Down
4 changes: 2 additions & 2 deletions t/contexts/nondecimal_base.t
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ subtest 'Check that the Context parses number correct' => sub {
};

subtest 'check that non-valid digits return errors' => sub {
like dies { Compute('456'); }, qr/The number should only consist of the digits:/,
like dies { Compute('456'); }, qr/Numbers should consist only of the digits:/,
'Try to build a base-5 number will illegal digits';
};

Expand Down Expand Up @@ -133,7 +133,7 @@ subtest 'Use alternative digits' => sub {

ok my $a2 = Compute("3TE"), "Base 12 number '3TE' with T=ten and E = eleven";
like dies { Compute('A5'); },
qr/The number should only consist of the digits:/,
qr/Numbers should consist only of the digits:/,
'Check that A=10 is not allowed';
};

Expand Down

0 comments on commit 66558ea

Please sign in to comment.