Skip to content

Commit

Permalink
Rip out existing traits handling, as it needs to be done very differe…
Browse files Browse the repository at this point in the history
…ntly. Get natives to parse/compile.
  • Loading branch information
jnthn committed May 8, 2011
1 parent 7767bf4 commit fc60b90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 87 deletions.
101 changes: 15 additions & 86 deletions src/Perl6/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1757,118 +1757,47 @@ class Perl6::Actions is HLL::Actions {
}

method trait($/) {
my $past;
if $<trait_mod> {
$past := $<trait_mod>.ast;
}
elsif $<colonpair> {
$/.CURSOR.panic('traits specified as colon pairs not yet understood');
}
make $past;

}

method trait_mod:sym<is>($/) {
my $trait := PAST::Op.new( :pasttype('call'), :name('&trait_mod:<is>') );
if $<circumfix> { $trait.push($<circumfix>[0].ast); }

if $/.CURSOR.is_name(~$<longname>) {
# It's a type - look it up and send it in as a positional, before
# the parameter.
my @name := Perl6::Grammar::parse_name(~$<longname>);
$trait.unshift(PAST::Var.new(
:scope(is_lexical(~$<longname>) ?? 'lexical' !! 'package'),
:name(@name.pop()),
:namespace(@name)
));
}
else {
# Not a type name, so construct a named parameter with this name; it
# is a named param so it has to go on the end.
$trait.push(PAST::Var.new(
:name('True'),
:namespace('Bool'),
:scope('package'),
:named(~$<longname>)
));
# Handle is repr specially.
if ~$<longname> eq 'repr' {
if $<circumfix> {
$*REPR := $<circumfix>[0].ast[0].value;
}
else {
$/.cursor.panic("is repr(...) trait needs a parameter");
}
}

$trait<is_name> := ~$<longname>;
make $trait;
}

method trait_mod:sym<hides>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<hides>'),
$<module_name>.ast
);

}

method trait_mod:sym<does>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<does>'),
$<module_name>.ast
);

}

method trait_mod:sym<will>($/) {
my $trait := PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:will'),
$<pblock>.ast
);

if $/.CURSOR.is_name(~$<identifier>) {
# It's a type - look it up and send it in as a positional, before
# the parameter.
$trait.unshift(PAST::Var.new(
:scope('package'),
:name(~$<identifier>)
));
}
else {
# Not a type name, so construct a named parameter with this name; it
# is a named param so it has to go on the end.
$trait.push(PAST::Val.new(
:value(PAST::Var.new( :name('True'), :namespace('Bool'), :scope('package') )),
:named(~$<identifier>)
));
}

make $trait;
}

method trait_mod:sym<of>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<of>'),
$<typename>.ast
);

}

method trait_mod:sym<as>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<as>'),
$<typename>.ast
);

}

method trait_mod:sym<returns>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<returns>'),
$<typename>.ast
);

}

method trait_mod:sym<handles>($/) {
make PAST::Op.new(
:pasttype('call'),
:name('&trait_mod:<handles>'),
$<term>.ast
);

}

method postop($/) {
Expand Down
14 changes: 13 additions & 1 deletion src/Perl6/Grammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ grammar Perl6::Grammar is HLL::Grammar {
:my $*IN_DECL; # what declaration we're in
:my $*MONKEY_TYPING := 0; # whether augment/supersede are allowed
:my $*begin_compunit := 1; # whether we're at start of a compilation unit
:my $*DECLARAND; # the current thingy we're declaring, and subject of traits

# Extras.
:my %*METAOPGEN; # hash of generated metaops
Expand Down Expand Up @@ -841,6 +842,11 @@ grammar Perl6::Grammar is HLL::Grammar {
:my $*PKGDECL := 'knowhow';
<sym> <package_def>
}
token package_declarator:sym<native> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'native';
<sym> <package_def>
}
token package_declarator:sym<slang> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'slang';
Expand All @@ -858,6 +864,7 @@ grammar Perl6::Grammar is HLL::Grammar {
rule package_def {<.end_keyword>
:my $longname;
:my $outer := $*ST.cur_lexpad();
:my $*DECLARAND;
:my $*IN_DECL := 'package';
:my $*CURPAD;

Expand Down Expand Up @@ -904,6 +911,9 @@ grammar Perl6::Grammar is HLL::Grammar {
$/.CURSOR.panic("Cannot use $*SCOPE scope with $*PKGDECL");
}
}

# Set declarand as the package.
$*DECLARAND := $*PACKAGE;
}

[
Expand Down Expand Up @@ -1310,7 +1320,9 @@ grammar Perl6::Grammar is HLL::Grammar {
}

proto token trait_mod { <...> }
token trait_mod:sym<is> { <sym>:s <longname><circumfix>? }
token trait_mod:sym<is> {
<sym>:s <longname><circumfix>?
}
token trait_mod:sym<hides> {
<sym>:s <module_name>
[
Expand Down

0 comments on commit fc60b90

Please sign in to comment.