Skip to content

Commit b16ca35

Browse files
committed
Add sprintfAddHandler and use it.
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.
1 parent 644fc3a commit b16ca35

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/HLL/sprintf.nqp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
my module sprintf {
2+
my @handlers;
3+
24
grammar Syntax {
35
token TOP {
46
:my $*ARGS_USED := 0;
@@ -74,6 +76,12 @@ my module sprintf {
7476
}
7577

7678
sub intify($number_representation) {
79+
for @handlers -> $handler {
80+
if $handler.mine($number_representation) {
81+
return $handler.int($number_representation);
82+
}
83+
}
84+
7785
my $result;
7886
if $number_representation > 0 {
7987
$result := nqp::floor_n($number_representation);
@@ -316,4 +324,11 @@ my module sprintf {
316324
}
317325

318326
nqp::bindcurhllsym('sprintf', &sprintf);
327+
328+
sub sprintfAddHandler($interface) {
329+
@handlers.push($interface);
330+
}
331+
332+
nqp::bindcurhllsym('sprintfAddHandler', &sprintfAddHandler);
333+
319334
}

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,20 @@ QAST::OperationsJAST.add_core_op('sprintf', -> $qastcomp, $op {
20152015
|@operands )
20162016
);
20172017
});
2018+
QAST::OperationsJAST.add_core_op('sprintfAddHandler', -> $qastcomp, $op {
2019+
my @operands := $op.list;
2020+
$qastcomp.as_jast(
2021+
QAST::Op.new(
2022+
:op('call'),
2023+
:returns(str),
2024+
QAST::Op.new(
2025+
:op('gethllsym'),
2026+
QAST::SVal.new( :value('nqp') ),
2027+
QAST::SVal.new( :value('sprintfAddHandler') )
2028+
),
2029+
|@operands )
2030+
);
2031+
});
20182032
QAST::OperationsJAST.map_classlib_core_op('escape', $TYPE_OPS, 'escape', [$RT_STR], $RT_STR);
20192033
QAST::OperationsJAST.map_classlib_core_op('flip', $TYPE_OPS, 'flip', [$RT_STR], $RT_STR);
20202034
QAST::OperationsJAST.map_classlib_core_op('replace', $TYPE_OPS, 'replace', [$RT_STR, $RT_INT, $RT_INT, $RT_STR], $RT_STR);

0 commit comments

Comments
 (0)