/
Signature.pm
134 lines (115 loc) · 3.85 KB
/
Signature.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
my class Signature { # declared in BOOTSTRAP
# class Signature is Any {
# has Mu $!params; # VM's array of parameters
# has Mu $!returns; # return type
# has Mu $!arity; # arity
# has Mu $!count; # count
# has Mu $!code;
multi method ACCEPTS(Signature:D: Capture $topic) {
nqp::p6bool(nqp::p6isbindable(self, nqp::decont($topic)));
}
multi method ACCEPTS(Signature:D: @topic) {
self.ACCEPTS(@topic.Capture)
}
multi method ACCEPTS(Signature:D: %topic) {
self.ACCEPTS(%topic.Capture)
}
multi method ACCEPTS(Signature:D: Signature:D $topic) {
my $sclass = self.params.classify({.named});
my $tclass = $topic.params.classify({.named});
my @spos := $sclass{False} // ();
my @tpos := $tclass{False} // ();
while @spos {
my $s;
my $t;
last unless $t=@tpos.shift;
$s=@spos.shift;
if $s.slurpy or $s.capture {
@spos=();
@tpos=();
last;
}
if $t.slurpy or $t.capture {
return False unless any(@spos) ~~ {.slurpy or .capture};
@spos=();
@tpos=();
last;
}
if not $s.optional {
return False if $t.optional
}
return False unless $t ~~ $s;
}
return False if @tpos;
if @spos {
return False unless @spos[0].optional or @spos[0].slurpy or @spos[0].capture;
}
for flat ($sclass{True} // ()).grep({!.optional and !.slurpy}) -> $this {
my $other;
return False unless $other=($tclass{True} // ()).grep(
{!.optional and $_ ~~ $this });
return False unless +$other == 1;
}
my $here=$sclass{True}.SetHash;
my $hasslurpy=($sclass{True} // ()).grep({.slurpy}).Bool;
for flat @($tclass{True} // ()) -> $other {
my $this;
if $other.slurpy {
return False if any($here.keys) ~~ { .type !=:= Mu };
return $hasslurpy;
}
if $this=$here.keys.grep( -> $t { $other ~~ $t }) {
$here{$this[0]} :delete;
}
else {
return False unless $hasslurpy;
}
}
True;
}
method arity() {
$!arity
}
method count() {
$!count
}
method params() {
nqp::p6list(nqp::clone($!params), List, Mu);
}
method !gistperl(Signature:D: $perl, Mu:U :$elide-type = Mu) {
# Opening.
my $text = $perl ?? ':(' !! '(';
# Parameters.
if self.params -> @params {
$text ~= @params.shift.perl(:$elide-type) ~ ': '
if @params[0].invocant;
$text ~= ';; '
if !@params[0].multi-invocant;
my $sep = '';
for @params.kv -> $i, $param {
$text ~= $sep ~ $param.perl(:$elide-type);
$text .= subst(/' $'$/,'') unless $perl;
$sep = $param.multi-invocant && !@params[$i+1].?multi-invocant
?? ';; '
!! ', '
}
}
if !nqp::isnull($!returns) && $!returns !=:= Mu {
$text = $text ~ ' --> ' ~ $!returns.perl
}
# Closer.
$text ~ ')'
}
method !deftype(Signature:D:) {
!nqp::isnull($!code) && $!code ~~ Routine ?? Any !! Mu
}
multi method perl(Signature:D:) {
self!gistperl(True, :elide-type(self!deftype))
}
multi method gist(Signature:D:) {
self!gistperl(False, :elide-type(self!deftype))
}
method returns() { $!returns }
}
multi sub infix:<eqv>(Signature $a, Signature $b) { $a.perl eq $b.perl }
# vim: ft=perl6 expandtab sw=4