Skip to content

Commit

Permalink
make sprintf pass under parrot backend
Browse files Browse the repository at this point in the history
Also added two tests about accessing args by index.
  • Loading branch information
FROGGS committed Aug 20, 2013
1 parent e5f1cd4 commit 04fe91c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/HLL/sprintf.nqp
Expand Up @@ -53,6 +53,8 @@ my module sprintf {
}

class Actions {
my $knowhow := nqp::knowhow().new_type(:repr("P6bigint"));
my $zero := nqp::box_i(0, $knowhow);
method TOP($/) {
my @statements;
@statements.push( $_.ast ) for $<statement>;
Expand Down Expand Up @@ -93,13 +95,11 @@ my module sprintf {
}
}

my $knowhow := nqp::knowhow().new_type(:repr("P6bigint"));

if nqp::eqaddr($number_representation.WHAT, 1.WHAT) {
if nqp::isint($number_representation) {
nqp::box_i($number_representation, $knowhow);
} else {
if nqp::eqaddr($number_representation.WHAT, 1.01.WHAT)
|| nqp::eqaddr($number_representation.WHAT, "1.01".WHAT) {
if nqp::isnum($number_representation)
|| nqp::isstr($number_representation) {
if $number_representation > 0 {
nqp::fromnum_I(nqp::floor_n($number_representation), $knowhow);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ my module sprintf {
my $pre := ($<sym> eq 'b' ?? '0b' !! '0B') if $int && has_flag($/, 'hash');
if nqp::chars($<precision>) {
$int := '' if $<precision>.ast == 0 && $int == 0;
$int := $pre ~ infix_x('0', intify($<precision>.ast) - nqp::chars($int)) ~ $int;
$int := $pre ~ infix_x('0', $<precision>.ast - nqp::chars($int)) ~ $int;
}
else {
$int := $pre ~ $int
Expand All @@ -161,9 +161,8 @@ my module sprintf {

method directive:sym<d>($/) {
my $int := intify(next_argument($/));
my $knowhow := nqp::knowhow().new_type(:repr("P6bigint"));
my $pad := padding_char($/);
my $sign := nqp::islt_I($int, nqp::box_i(0, $knowhow)) ?? '-'
my $sign := nqp::islt_I($int, $zero) ?? '-'
!! has_flag($/, 'plus')
?? '+' !! '';
$int := nqp::tostr_I(nqp::abs_I($int, $knowhow));
Expand All @@ -189,7 +188,6 @@ my module sprintf {
my $lhs := nqp::floor_n($float);
my $rhs := $float - $lhs;

my $knowhow := nqp::knowhow().new_type(:repr("P6bigint"));
my $int := nqp::fromnum_I($lhs, $knowhow);
$lhs := nqp::tostr_I($int);

Expand Down Expand Up @@ -298,16 +296,14 @@ my module sprintf {
# XXX: Should we emulate p5 behaviour for negative values passed to %u ?
method directive:sym<u>($/) {
my $int := intify(next_argument($/));
if $int < 0 {
if nqp::islt_I($int, $zero) {
my $err := nqp::getstderr();
nqp::printfh($err, "negative value '"
~ $int
~ "' for %u in sprintf");
$int := 0;
}

my $chars := nqp::chars($int);

# Go through tostr_I to avoid scientific notation.
make nqp::tostr_I($int)
}
Expand All @@ -317,7 +313,7 @@ my module sprintf {
my $pre := '0X' if $int && has_flag($/, 'hash');
if nqp::chars($<precision>) {
$int := '' if $<precision>.ast == 0 && $int == 0;
$int := $pre ~ infix_x('0', intify($<precision>.ast) - nqp::chars($int)) ~ $int;
$int := $pre ~ infix_x('0', $<precision>.ast - nqp::chars($int)) ~ $int;
}
else {
$int := $pre ~ $int
Expand Down
12 changes: 11 additions & 1 deletion t/jvm/06-sprintf.t
Expand Up @@ -24,7 +24,7 @@ sub is($actual, $expected, $description) {
}
}

plan(255);
plan(257);

is(nqp::sprintf('Walter Bishop', []), 'Walter Bishop', 'no directives' );

Expand All @@ -49,9 +49,16 @@ dies_ok({ nqp::sprintf('%a', 'Science') }, 'unknown directive' );
is($die_message, "'a' is not valid in sprintf format sequence '%a'",
'unknown directive error message' );

my class SprintfHandler {
method mine($x) { try nqp::reprname($x) eq "P6bigint" }
method int($x) { $x }
}

my $knowhow := nqp::knowhow().new_type(:name('TestBigInt'), :repr("P6bigint"));
nqp::sprintfaddargumenthandler(SprintfHandler.new);
my $large-positive-int := nqp::pow_I(nqp::box_i(33, $knowhow), nqp::box_i(21, $knowhow), $knowhow, $knowhow);
# my $large-negative-int := fromnum_I(-nqp::pow_n(2.42, 42), $knowhow);


is(nqp::sprintf('<%6s>', [12]), '< 12>', 'right-justified %s with space padding');
is(nqp::sprintf('<%6%>', []), '< %>', 'right-justified %% with space padding');
Expand Down Expand Up @@ -310,3 +317,6 @@ is(nqp::sprintf('%17.3g', [3.000000000000e+09]), ' 3e+09', '%17.3g 3.
is(nqp::sprintf('%17.3g', [3.000000000000e+10]), ' 3e+10', '%17.3g 3.000000000000e+10');
is(nqp::sprintf('%17.3g', [3.000000000000e+11]), ' 3e+11', '%17.3g 3.000000000000e+11');
is(nqp::sprintf('%17.3g', [3.000000000000e+12]), ' 3e+12', '%17.3g 3.000000000000e+12');

is(nqp::sprintf('%2$d %1$d', [12, 34]), '34 12', 'parameter index');
is(nqp::sprintf('%3$d %d %1$d', [1, 2, 3]), '3 1 1', 'parameter index');

0 comments on commit 04fe91c

Please sign in to comment.