Skip to content

Commit e7b836c

Browse files
committed
topological sorting of types
1 parent 008d651 commit e7b836c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Perl6/TypeGraph.pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use Perl6::Type;
22
class Perl6::TypeGraph {
33
has %.types;
4+
has @.sorted;
45
my grammar Decl {
56
token ident { <.alpha> \w* }
67
token apostrophe { <[ ' \- ]> }
@@ -56,7 +57,19 @@ class Perl6::TypeGraph {
5657
$t.super.push: $get-type('Any');
5758
}
5859
}
60+
self!topo-sort;
5961
}
62+
method !topo-sort {
63+
my %seen;
64+
sub visit($n) {
65+
return if %seen{$n};
66+
%seen{$n} = True;
67+
visit($_) for $n.super, $n.roles;
68+
@!sorted.push: $n;
69+
}
70+
visit($_) for %.types.values;
71+
}
72+
6073
}
6174

6275
# vim: ft=perl6

t/typegraph.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ is $t.types<Str>.mro, 'Str Cool Any Mu', 'Str mro';
1414
is $t.types<Match>.mro, 'Match Capture Cool Any Mu', 'Match mro';
1515
is $t.types<Stash>.super.any, 'Any', 'Any as default parent works';
1616
is $t.types<Any>.super, 'Mu', 'default-Any did not add a parent to Any';
17+
1718
done;

0 commit comments

Comments
 (0)