Skip to content

Commit

Permalink
Add sprintfAddHandler and use it.
Browse files Browse the repository at this point in the history
This allows you to register a handler which knows how to convert types NQP doesn't know about to bigints.  (Eventually I hope to expand it to floating point values and strings as well.)

I'm not particularly attached to the name sprintfAddHandler, if someone has a better suggestion.
  • Loading branch information
colomon committed Jul 19, 2013
1 parent 644fc3a commit b16ca35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/HLL/sprintf.nqp
@@ -1,4 +1,6 @@
my module sprintf {
my @handlers;

grammar Syntax {
token TOP {
:my $*ARGS_USED := 0;
Expand Down Expand Up @@ -74,6 +76,12 @@ my module sprintf {
}

sub intify($number_representation) {
for @handlers -> $handler {
if $handler.mine($number_representation) {
return $handler.int($number_representation);
}
}

my $result;
if $number_representation > 0 {
$result := nqp::floor_n($number_representation);
Expand Down Expand Up @@ -316,4 +324,11 @@ my module sprintf {
}

nqp::bindcurhllsym('sprintf', &sprintf);

sub sprintfAddHandler($interface) {
@handlers.push($interface);
}

nqp::bindcurhllsym('sprintfAddHandler', &sprintfAddHandler);

}
14 changes: 14 additions & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2015,6 +2015,20 @@ QAST::OperationsJAST.add_core_op('sprintf', -> $qastcomp, $op {
|@operands )
);
});
QAST::OperationsJAST.add_core_op('sprintfAddHandler', -> $qastcomp, $op {
my @operands := $op.list;
$qastcomp.as_jast(
QAST::Op.new(
:op('call'),
:returns(str),
QAST::Op.new(
:op('gethllsym'),
QAST::SVal.new( :value('nqp') ),
QAST::SVal.new( :value('sprintfAddHandler') )
),
|@operands )
);
});
QAST::OperationsJAST.map_classlib_core_op('escape', $TYPE_OPS, 'escape', [$RT_STR], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('flip', $TYPE_OPS, 'flip', [$RT_STR], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('replace', $TYPE_OPS, 'replace', [$RT_STR, $RT_INT, $RT_INT, $RT_STR], $RT_STR);
Expand Down

0 comments on commit b16ca35

Please sign in to comment.