-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper.p6
executable file
·79 lines (68 loc) · 2.22 KB
/
mapper.p6
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
#!/usr/bin/env perl6
constant MAP_FILE = 'map.json'.IO;
use JSON::Tiny;
use nqp;
my @data = unique :as(*.perl), |subs, |methods;
my $total = +@data;
my $unique = @data.map(*.<name>).unique.elems;
say "Writing $total entries to {MAP_FILE}";
say "There are $unique unique routine names";
MAP_FILE.spurt: to-json %(
made-on => ~DateTime.now,
compiler => ~$*PERL.compiler.name,
version => ~$*PERL.compiler.version,
routines => @data.sort(*.<name>),
total => $total,
unique => $unique,
);
sub subs {
find-symbols({
.DEFINITE and nqp::istype($_, Sub)
and .name !~~ /^<['A..Z_-]>+ (':<'.+)? $/
})».&keyit;
}
sub methods {
find-symbols({
!.DEFINITE and try .^can('say')
}).unique.map(*.^methods(:local).Slip).grep({
try {.^can: 'gist'} and .^name ne 'ForeignCode'
}).unique(:as(*.perl))».&keyit
}
sub keyit ($_) {
%(
<type name candidates>
Z=> .WHAT.^name, .name, cand-info .candidates
)
}
sub cand-info (@candidates) {
eager @candidates.map: -> $_ is copy {
my $file = .file;
$file.starts-with: $_ and $file .= substr: .chars with 'SETTING::';
$file .= split('/').=tail if $file.starts-with: '/'; # toss full paths
$_ .= signature;
my %info = :0named, :0pos, :0slurpy, :signature(.gist), :$file,
:count($_ == Inf ?? "Inf" !! $_ with .count), # Inf in JSON => null
:arity($_ == Inf ?? "Inf" !! $_ with .arity); # Inf in JSON => null
for .params {
.named and %info<named>++;
.positional and %info<pos>++;
.slurpy and %info<slurpy>++;
}
%info
}
}
sub find-symbols ($matcher) {
eager grep $matcher, map {*.^name.say; $_}, lazy gather {
my Mu %seen{Mu};
for CORE::.keys.grep(* ne 'IterationEnd') {
try {.^methods} and take $_ given CORE::{$_};
sub dig ($stash) {
for $stash.keys {
try {.^methods} and take $_ given $stash{$_};
next if %seen{ $_ }++;
try { .keys } and .&dig given $stash{$_}.WHO;
}
}(CORE::{$_}.WHO) if try CORE::{$_}.WHO.keys;
}
}
}