Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Parse "multi method" correctly, add basic type-MMD tests
  • Loading branch information
sorear committed Apr 2, 2011
1 parent 2eec5c9 commit d9d73c9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/niecza
Expand Up @@ -27,6 +27,72 @@ use OptRxSimple;
use Sig;

augment class NieczaActions {
method method_def ($/) {
make ::Op::StatementList.new;
my $scope = $*SCOPE // 'has';
my $type = $<type> ?? ~$<type> !! '';
$type = ($type eq '' ?? 'normal' !!
$type eq '^' ?? 'meta' !!
$type eq '!' ?? 'private' !!
(
$/.CURSOR.sorry("Unhandled method decoration $type");
return Nil;
));
$scope = 'anon' if !$<longname>;
my $name = $<longname> ?? self.unqual_longname($<longname>,
"Qualified method definitions not understood") !! Any; #XXX

if $<sigil> {
$/.CURSOR.sorry("Method sgils NYI");
return Nil;
}
if $type eq 'meta' {
$/.CURSOR.sorry("Metamethod mixins NYI");
return Nil;
}
if $<multisig> > 1 {
$/.CURSOR.sorry("Multiple multisigs (what?) NYI");
return Nil;
}

if ($scope eq 'augment' || $scope eq 'supersede' || $scope eq 'state') {
$/.CURSOR.sorry("Illogical scope $scope for method");
return Nil;
}

if ($scope eq 'our') {
$/.CURSOR.sorry("Packages NYI");
return Nil;
}
my $sig = $<multisig> ?? $<multisig>[0].ast !!
self.get_placeholder_sig($/);

my $unsafe = False;
for @( $<trait> ) -> $t {
if $t.ast<nobinder> {
$sig = Any;
} elsif $t.ast<unsafe> {
$unsafe = True;
} else {
$/.CURSOR.sorry("NYI method trait $t");
}
}

my $bl = self.sl_to_block('sub', $<blockoid>.ast,
subname => $name, :$unsafe,
signature => $sig ?? $sig.for_method !! Any);

make self.block_to_closure($/, $bl, bindlex => ($scope eq 'my'),
multiness => ($*MULTINESS || Any),
bindmethod => ($scope ne 'anon' ?? [ $type, $name ] !! Any));
}

method block_to_closure($/, $body, :$bindlex, :$bindmethod, :$once,
:$bindpackages = [], :$multiness) {
::Op::SubDef.new(|node($/), :$bindlex, :$bindmethod, :$body, :$once,
:$bindpackages, :$multiness);
}

method regex_def($/) {
sub _symtext($name) {
($name ~~ /\:sym\<(.*)\>/) ?? ($name.substr(0, $/.from), ~$0) !!
Expand Down
8 changes: 8 additions & 0 deletions test2.pl
Expand Up @@ -67,6 +67,14 @@
multi token foo:b { y }
}
ok "y" ~~ / :lang(G19) <foo> /, "can use multi regex without proto";

my class C20 {
multi method bar(Bool $) { "bool" }
multi method bar(Str $) { "str" }
}

is C20.bar(True), "bool", "multimethods work (1)";
is C20.bar("foo"), "str", "multimethods work (2)";
}

#is $?FILE, 'test.pl', '$?FILE works';
Expand Down

0 comments on commit d9d73c9

Please sign in to comment.