Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[nqp]: Add return statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 27, 2009
1 parent 533eee8 commit e8e8388
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -115,6 +115,10 @@ method statement_control:sym<for>($/) {
make $past;
}

method statement_control:sym<return>($/) {
make PAST::Op.new( $<EXPR>.ast, :pasttype('return'), :node($/) );
}

## Terms

method noun:sym<variable>($/) { make $<variable>.ast; }
Expand Down Expand Up @@ -168,6 +172,7 @@ method routine_declarator:sym<sub>($/) { make $<routine_def>.ast; }
method routine_def($/) {
my $past := $<blockoid>.ast;
$past.blocktype('declaration');
$past.control('return_pir');
if $<deflongname> {
my $name := ~$<deflongname>[0];
$past.name($name);
Expand Down
5 changes: 5 additions & 0 deletions src/NQP/Grammar.pm
Expand Up @@ -89,6 +89,11 @@ token statement_control:sym<for> {
<xblock>
}

token statement_control:sym<return> {
<sym> :s
[ <EXPR> || <.panic: 'return requires an expression argument'> ]
}

## Terms

token noun:sym<variable> { <variable> }
Expand Down
23 changes: 23 additions & 0 deletions t/nqp/20-return.t
@@ -0,0 +1,23 @@
#! nqp

# implicit and explicit returns from subs

plan(3);

sub foo() { 1; }


sub bar() {
return 2;
0;
}

sub baz() {
if (1) { return 3; }
0;
}

ok( foo() == 1 , 'last value in block' );
ok( bar() == 2 , 'explicit return value in block');
ok( baz() == 3 , 'explicit return from nested block');

0 comments on commit e8e8388

Please sign in to comment.