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

Commit

Permalink
[nqp] Initial subroutine declaration code (still missing params).
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 26, 2009
1 parent caa7f04 commit 4c4a0e2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/PARROT_REVISION
@@ -1 +1 @@
42113
42115
32 changes: 30 additions & 2 deletions src/NQP/Actions.pm
Expand Up @@ -87,6 +87,7 @@ method statement_control:sym<unless>($/) {

method noun:sym<variable>($/) { make $<variable>.ast; }
method noun:sym<scope_declarator>($/) { make $<scope_declarator>.ast; }
method noun:sym<routine_declarator>($/) { make $<routine_declarator>.ast; }

method variable($/) {
make PAST::Var.new( :name(~$/) );
Expand All @@ -95,13 +96,23 @@ method variable($/) {
method scope_declarator:sym<my>($/) { make $<scoped>.ast; }
method scope_declarator:sym<our>($/) {
my $past := $<scoped>.ast;
$past.scope('package');
@BLOCK[0].symbol( $past.name, :scope('package') );
$past.scope('package');

# If we're modifying the scope of a block, then eliminate the
# variable assignment and just return the (named) block itself.
if $past<XXXroutine> {
$past := $past.viviself;
$past.nsentry($past.name);
}

make $past;
}

method scoped($/) {
make $<variable_declarator>.ast;
make $<routine_declarator>
?? $<routine_declarator>.ast
!! $<variable_declarator>.ast;
}

method variable_declarator($/) {
Expand All @@ -117,6 +128,23 @@ method variable_declarator($/) {
make $past;
}

method routine_declarator:sym<sub>($/) { make $<routine_def>.ast; }

method routine_def($/) {
my $past := $<blockoid>.ast;
$past.blocktype('declaration');
if $<deflongname> {
my $name := ~$<deflongname>[0];
$past.name($name);
$past.nsentry('');
$past := PAST::Var.new( :name($name), :isdecl(1), :viviself($past),
:scope('lexical') );
$past<XXXroutine> := 1;
@BLOCK[0].symbol( $name, :scope('lexical') );
}
make $past;
}

method term:sym<identifier>($/) {
my $past := $<args>.ast;
$past.name(~$<identifier>);
Expand Down
15 changes: 13 additions & 2 deletions src/NQP/Grammar.pm
Expand Up @@ -71,8 +71,9 @@ token statement_control:sym<unless> {

## Terms

token noun:sym<variable> { <variable> }
token noun:sym<scope_declarator> { <scope_declarator> }
token noun:sym<variable> { <variable> }
token noun:sym<scope_declarator> { <scope_declarator> }
token noun:sym<routine_declarator> { <routine_declarator> }

token variable {
<sigil> <twigil>? <desigilname=ident>
Expand All @@ -88,10 +89,20 @@ token scope_declarator:sym<our> { <sym> <scoped> }

rule scoped {
| <variable_declarator>
| <routine_declarator>
}

token variable_declarator { <variable> }

proto token routine_declarator { <...> }
token routine_declarator:sym<sub> { <sym> <routine_def> }

rule routine_def {
<deflongname=ident>?
<.newpad>
<blockoid>
}

proto token term { <...> }

token term:sym<identifier> {
Expand Down

0 comments on commit 4c4a0e2

Please sign in to comment.