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

Commit

Permalink
Browse files Browse the repository at this point in the history
A little cleanup and consistency improvement in the actions to elimin…
…ate some bits left over from the 6model transition, and in prep for roles additions.
  • Loading branch information
jnthn committed Feb 5, 2011
1 parent 8f6ca5a commit ae3ec52
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
54 changes: 26 additions & 28 deletions src/NQP/Actions.pm
Expand Up @@ -365,20 +365,31 @@ method variable($/) {
make $past;
}

method package_declarator:sym<module>($/) { make $<package_def>.ast; }
method package_declarator:sym<knowhow>($/) { make package($/); }
method package_declarator:sym<class>($/) { make package($/) }
method package_declarator:sym<grammar>($/) { make package($/) }
method package_declarator:sym<role>($/) { make package($/); }
method package_declarator:sym<native>($/) { make package($/); }

sub package($/) {
my @ns := pir::clone__PP($<package_def><name><identifier>);
method package_declarator:sym<module>($/) { make $<package_def>.ast }
method package_declarator:sym<knowhow>($/) { make $<package_def>.ast }
method package_declarator:sym<class>($/) { make $<package_def>.ast }
method package_declarator:sym<grammar>($/) { make $<package_def>.ast }
method package_declarator:sym<role>($/) { make $<package_def>.ast }
method package_declarator:sym<native>($/) { make $<package_def>.ast }

method package_def($/) {
# Get name and meta-object.
my @ns := pir::clone__PP($<name><identifier>);
my $name := ~@ns.pop;
my $how := %*HOW{$*PKGDECL};

# Get the body code.
my $past := $<block> ?? $<block>.ast !! $<comp_unit>.ast;
$past.namespace( $<name><identifier> );
$past.blocktype('immediate');

# Evaluate anything else in the package in-line; also give it a $?CLASS
# lexical. XXX Due to Parrot static lexpad fail, it's currently package scoped.
$past.unshift(PAST::Var.new( :name('$?CLASS'), :scope('package'), :isdecl(1) ));
$past.symbol('$?CLASS', :scope('package'));

# Prefix the class initialization with initial setup. Also install it
# in the symbol table right away.
my $how := %*HOW{~$<sym>};
$*PACKAGE-SETUP.unshift(PAST::Stmts.new(
PAST::Op.new( :pasttype('bind'),
PAST::Var.new( :name('type_obj'), :scope('register'), :isdecl(1) ),
Expand All @@ -397,16 +408,16 @@ sub package($/) {
PAST::Var.new( :name('type_obj'), :scope('register') )
)
));
if $<package_def><repr> {
my $repr_name := $<package_def><repr>[0].ast;
if $<repr> {
my $repr_name := $<repr>[0].ast;
$repr_name.named('repr');
$*PACKAGE-SETUP[0][0][1].push($repr_name);
}

# Add call to add_parent if we have one.
# XXX Doesn't handle lexical classes yet.
if $<package_def><parent> {
my @ns := pir::clone__PP($<package_def><parent>[0]<identifier>);
if $<parent> {
my @ns := pir::clone__PP($<parent>[0]<identifier>);
my $name := ~@ns.pop;
$*PACKAGE-SETUP.push(PAST::Op.new(
:pasttype('callmethod'), :name('add_parent'),
Expand All @@ -419,7 +430,7 @@ sub package($/) {
PAST::Var.new( :name(~$name), :namespace(@ns), :scope('package') )
));
}
elsif ~$<sym> eq 'grammar' {
elsif $*PKGDECL eq 'grammar' {
$*PACKAGE-SETUP.push(PAST::Op.new(
:pasttype('callmethod'), :name('add_parent'),
PAST::Op.new(
Expand All @@ -446,22 +457,9 @@ sub package($/) {
# Set up slot for the type object to live in.
@BLOCK[0][0].unshift(PAST::Var.new( :name($name), :namespace(@ns), :scope('package'), :isdecl(1) ));

# Evaluate anything else in the package in-line; also give it a $?CLASS
# lexical. XXX Due to Parrot static lexpad fail, it's currently package scoped.
my $past := $<package_def>.ast;
$past.unshift(PAST::Var.new( :name('$?CLASS'), :scope('package'), :isdecl(1) ));
$past.symbol('$?CLASS', :scope('package'));

# Attach the class code to run at loadinit time.
$past.loadinit.push(PAST::Block.new( :blocktype('immediate'), $*PACKAGE-SETUP ));

return $past;
}

method package_def($/) {
my $past := $<block> ?? $<block>.ast !! $<comp_unit>.ast;
$past.namespace( $<name><identifier> );
$past.blocktype('immediate');
make $past;
}

Expand Down
7 changes: 6 additions & 1 deletion src/NQP/Grammar.pm
Expand Up @@ -11,6 +11,7 @@ method TOP() {
# Package declarator to meta-package mapping.
my %*HOW;
%*HOW<knowhow> := KnowHOW;
%*HOW<module> := NQPModuleHOW;
%*HOW<class> := NQPClassHOW;
%*HOW<grammar> := NQPClassHOW;
%*HOW<role> := NQPParametricRoleHOW;
Expand Down Expand Up @@ -278,7 +279,11 @@ token sigil { <[$@%&]> }
token twigil { <[*!?]> }

proto token package_declarator { <...> }
token package_declarator:sym<module> { <sym> <package_def> }
token package_declarator:sym<module> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
:my $*PKGDECL := 'module';
<sym> <package_def>
}
token package_declarator:sym<knowhow> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
:my $*PKGDECL := 'knowhow';
Expand Down

0 comments on commit ae3ec52

Please sign in to comment.