Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
start to generate bigints in the actions.
Does not compile the setting yet
  • Loading branch information
moritz committed Nov 11, 2011
1 parent eab4d7a commit 29eeaf8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Perl6/Actions.pm
Expand Up @@ -53,6 +53,14 @@ class Perl6::Actions is HLL::Actions {
$STATEMENT_PRINT := 0;
}

# TODO: inline string_to_bigint?
our sub string_to_bigint($src, $base) {
my $res := nqp::radix_I($base, ~$src, 0, 2, $*ST.find_symbol(['Int']));
$src.CURSOR.panic("'$src' is not a valid number")
unless nqp::iseq_i(nqp::unbox_i(nqp::atkey($res, 2)), nqp::chars($src));
nqp::atkey($res, 0);
}

sub xblock_immediate($xblock) {
$xblock[1] := pblock_immediate($xblock[1]);
$xblock;
Expand Down Expand Up @@ -3335,6 +3343,12 @@ class Perl6::Actions is HLL::Actions {
make $<number>.ast;
}

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 number:sym<complex>($/) {
my $re := $*ST.add_constant('Num', 'num', 0e0);
my $im := $*ST.add_constant('Num', 'num', +~$<im>);
Expand Down
28 changes: 25 additions & 3 deletions src/Perl6/SymbolTable.pm
Expand Up @@ -1042,9 +1042,13 @@ class Perl6::SymbolTable is HLL::Compiler::SerializationContextBuilder {
$namedkey := $namedkey ~ $_.key ~ ',' ~ $_.value ~ ';'
if pir::defined($_.value);
}
$cache_key := "$type,$primitive,"
~ pir::join(',', @value)
~ $namedkey;
if $primitive eq 'bigint' {
$cache_key := "$type,bigint," ~ nqp::tostr_I(@value[0]);
} else {
$cache_key := "$type,$primitive,"
~ pir::join(',', @value)
~ $namedkey;
}
if pir::exists(%!const_cache, $cache_key) {
my $past := self.get_slot_past_for_object(%!const_cache{$cache_key});
$past<has_compile_time_value> := 1;
Expand Down Expand Up @@ -1077,6 +1081,7 @@ class Perl6::SymbolTable is HLL::Compiler::SerializationContextBuilder {
elsif $primitive eq 'type_new' {
$constant := $type_obj.new(|@value, |%named);
if $type eq 'Rat' {
# BIGINT TODO: fix for bigint
my $int_lookup := self.get_object_sc_ref_past(self.find_symbol(['Int']));
my $nu := nqp::unbox_i(nqp::getattr($constant, $type_obj, '$!numerator'));
my $de := nqp::unbox_i(nqp::getattr($constant, $type_obj, '$!denominator'));
Expand Down Expand Up @@ -1128,6 +1133,23 @@ class Perl6::SymbolTable is HLL::Compiler::SerializationContextBuilder {
# Adds a numeric constant value (int or num) to the constants table.
# Returns PAST to do the lookup of the constant.
method add_numeric_constant($type, $value) {
if $type eq 'Int' && pir::typeof__SP($value) eq 'Int' {
if nqp::isbig_I($value) {
# cannot unbox to int without loss of information
my $const := self.add_constant('Int', 'bigint', $value);
my $past := PAST::Op(
:pirop('nqp_bigint_from_str PPs'),
$*ST.get_object_sc_ref_past($*ST.find_symbol(['Int'])),
nqp::tostr_I($value)
);
$past<has_compile_time_value> := 1;
$past<compile_time_value> := $value;
return $past;
}
# since Int doesn't have any vtables yet (at least while compiling
# the setting), it is inconvenient to work with, so unbox
$value := nqp::unbox_i($value);
}
my $const := self.add_constant($type, nqp::lc($type), $value);
my $tflag := $type eq 'Int' ?? 'Ii' !! 'Nn';
my $past := PAST::Want.new($const, $tflag,
Expand Down

0 comments on commit 29eeaf8

Please sign in to comment.