Skip to content

Commit 9c4dfa5

Browse files
committed
Implement and test resultchild, which allows something other than the final statement of a QAST::Stmt or QAST::Stmts to be used as the result of the overall operation.
1 parent 58acfd1 commit 9c4dfa5

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/QAST/Compiler.nqp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,25 +365,30 @@ class QAST::Compiler is HLL::Compiler {
365365
}
366366

367367
multi method as_post(QAST::Stmts $node) {
368-
self.compile_all_the_stmts($node.list)
368+
self.compile_all_the_stmts($node.list, $node.resultchild)
369369
}
370370

371371
multi method as_post(QAST::Stmt $node) {
372372
my $orig_reg := $*REGALLOC;
373373
{
374374
my $*REGALLOC := RegAlloc.new($orig_reg);
375-
self.compile_all_the_stmts($node.list)
375+
self.compile_all_the_stmts($node.list, $node.resultchild)
376376
}
377377
}
378378

379-
method compile_all_the_stmts(@stmts) {
379+
method compile_all_the_stmts(@stmts, $resultchild?) {
380380
my $last;
381381
my $ops := self.post_new('Ops');
382+
my $i := 0;
382383
for @stmts {
383384
$last := self.as_post($_);
384385
$ops.push($last);
386+
if nqp::defined($resultchild) && $resultchild == $i {
387+
$ops.result($last.result);
388+
}
389+
$i := $i + 1;
385390
}
386-
if $last {
391+
if $last && !nqp::defined($resultchild) {
387392
$ops.result($last.result);
388393
}
389394
$ops

src/QAST/Stmt.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class QAST::Stmt is QAST::Node {
2+
has $!resultchild;
3+
4+
method resultchild(*@value) { $!resultchild := @value[0] if @value; $!resultchild }
25
}

src/QAST/Stmts.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class QAST::Stmts is QAST::Node {
2+
has $!resultchild;
3+
4+
method resultchild(*@value) { $!resultchild := @value[0] if @value; $!resultchild }
25
}

t/qast/qast.t

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use QAST;
22

3-
plan(68);
3+
plan(70);
44

55
# Following a test infrastructure.
66
sub compile_qast($qast) {
@@ -970,3 +970,26 @@ is_qast(
970970
),
971971
115,
972972
'QAST::VM with pirop and signature after a space');
973+
974+
is_qast(
975+
QAST::Block.new(
976+
QAST::Stmts.new(
977+
:resultchild(0),
978+
QAST::IVal.new( :value(1) ),
979+
QAST::IVal.new( :value(2) )
980+
)
981+
),
982+
1,
983+
'resultchild works on QAST::Stmts');
984+
985+
is_qast(
986+
QAST::Block.new(
987+
QAST::Stmt.new(
988+
:resultchild(1),
989+
QAST::IVal.new( :value(3) ),
990+
QAST::IVal.new( :value(4) ),
991+
QAST::IVal.new( :value(5) )
992+
)
993+
),
994+
4,
995+
'resultchild works on QAST::Stmt');

0 commit comments

Comments
 (0)