Skip to content

Commit

Permalink
Refactor JnthnNQP's package declarators a little to be more STD-ish, …
Browse files Browse the repository at this point in the history
…and then do a first cut of the new code for compiling attribute declarations.
  • Loading branch information
jnthn committed Sep 12, 2010
1 parent b2f3bac commit c5dac2d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
32 changes: 23 additions & 9 deletions dotnet/compiler/Actions.pm
Expand Up @@ -341,8 +341,13 @@ method variable($/) {
make $past;
}

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

sub package($/) {
my $name := ~$<package_def><name>;

# Prefix the class initialization with initial setup. Also install it
Expand Down Expand Up @@ -387,7 +392,7 @@ method package_declarator:sym<class>($/) {

# Just evaluate anything else in the package in-line.
my $past := $<package_def>.ast;
make $past;
return $past;
}

method package_def($/) {
Expand Down Expand Up @@ -427,12 +432,21 @@ method variable_declarator($/) {
$/.CURSOR.panic("Redeclaration of symbol ", $name);
}
if $*SCOPE eq 'has' {
$BLOCK.symbol($name, :scope('attribute') );
unless $BLOCK<attributes> {
$BLOCK<attributes> :=
PAST::Op.new( :pasttype('list'), :named('attr') );
}
$BLOCK<attributes>.push( $name );
# Create and add a meta-attribute.
my $meta-attr-type := %*HOW-METAATTR{$*PKGDECL} || $*DEFAULT-METAATTR;
$*PACKAGE-SETUP.push(PAST::Op.new(
:pasttype('callmethod'), :name('add_attribute'),
PAST::Op.new(
:pasttype('nqpop'), :name('get_how'),
PAST::Var.new( :name('type_obj'), :scope('register') )
),
PAST::Var.new( :name('type_obj'), :scope('register') ),
PAST::Op.new(
:pasttype('callmethod'), :name('new'),
PAST::Var.new( :name($meta-attr-type), :scope('package') ),
PAST::Val.new( :value($name), :named('name') )
)
));
$past := PAST::Stmts.new();
}
else {
Expand Down
23 changes: 19 additions & 4 deletions dotnet/compiler/Grammar.pm
Expand Up @@ -15,7 +15,7 @@ method TOP() {
%*HOW<role> := 'NQPRoleHOW';

# What attribute class to use with what HOW, plus a default.
my %*DEFAULT-METAATTR := 'NQPAttribute';
my $*DEFAULT-METAATTR := 'NQPAttribute';
my %*HOW-METAATTR;
%*HOW-METAATTR<knowhow> := 'KnowHOWAttribute';

Expand Down Expand Up @@ -268,10 +268,25 @@ token twigil { <[*!?]> }

proto token package_declarator { <...> }
token package_declarator:sym<module> { <sym> <package_def> }
token package_declarator:sym<class> {
token package_declarator:sym<knowhow> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
$<sym>=[class|grammar|knowhow|role]
<package_def>
:my $*PKGDECL := 'knowhow';
<sym> <package_def>
}
token package_declarator:sym<class> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
:my $*PKGDECL := 'class';
<sym> <package_def>
}
token package_declarator:sym<grammar> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
:my $*PKGDECL := 'grammar';
<sym> <package_def>
}
token package_declarator:sym<role> {
:my $*PACKAGE-SETUP := PAST::Stmts.new();
:my $*PKGDECL := 'role';
<sym> <package_def>
}

rule package_def {
Expand Down

0 comments on commit c5dac2d

Please sign in to comment.