Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Actions.(dec|hex|oct|bin)int() #1021

Merged
merged 2 commits into from Feb 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/Perl6/Actions.nqp
Expand Up @@ -7,6 +7,8 @@ use QAST;

my $wantwant := Mu;

my int $?BITS := nqp::iseq_i(0x1ffffffff,8589934591) ?? 64 !! 32;

sub block_closure($code) {
my $closure := QAST::Op.new(
:op('callmethod'), :name('clone'),
Expand Down Expand Up @@ -631,10 +633,17 @@ class Perl6::Actions is HLL::Actions does STDActions {
}
}

sub string_to_bigint($src, $base) {
sub string_to_int($src, int $base, int $chars) {
my $res := nqp::radix($base, ~$src, 0, 2);
$src.CURSOR.panic("'$src' is not a valid number")
unless nqp::iseq_i(nqp::atpos($res, 2), $chars);
nqp::box_i(nqp::atpos($res, 0), $*W.find_symbol(['Int']));
}

sub string_to_bigint($src, int $base, int $chars) {
my $res := nqp::radix_I($base, ~$src, 0, 2, $*W.find_symbol(['Int']));
$src.CURSOR.panic("'$src' is not a valid number")
unless nqp::iseq_i(nqp::unbox_i(nqp::atpos($res, 2)), nqp::chars($src));
unless nqp::iseq_i(nqp::unbox_i(nqp::atpos($res, 2)), $chars);
nqp::atpos($res, 0);
}

Expand Down Expand Up @@ -7258,10 +7267,30 @@ class Perl6::Actions is HLL::Actions does STDActions {
make QAST::WVal.new( :value($v) );
}

method decint($/) { make string_to_bigint( $/, 10); }
method hexint($/) { make string_to_bigint( $/, 16); }
method octint($/) { make string_to_bigint( $/, 8 ); }
method binint($/) { make string_to_bigint( $/, 2 ); }
method decint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 16 !! 9)
?? string_to_bigint($/, 10, $chars)
!! string_to_int($/, 10, $chars);
}
method hexint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 14 !! 8)
?? string_to_bigint($/, 16, $chars)
!! string_to_int($/, 16, $chars);
}
method octint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 20 !! 10)
?? string_to_bigint($/, 8, $chars)
!! string_to_int($/, 8, $chars);
}
method binint($/) {
my int $chars := nqp::chars($/);
make $chars > ($?BITS == 64 ?? 62 !! 30)
?? string_to_bigint($/, 2, $chars)
!! string_to_int($/, 2, $chars);
}

method number:sym<numish>($/) {
make $<numish>.ast;
Expand Down