#!/usr/bin/perl

use strict;
use warnings;

use lib 'lib';

use Storable   ();
use File::Spec ();
use Parrot::Pmc2c::Pmc2cMain ();

my $dump_pattern = File::Spec->catfile(qw( src pmc *.dump ));

my %dependencies =
(
    default => [],
);

for my $dump (glob $dump_pattern)
{
    my $pmc     = Storable::retrieve($dump);
    my $parents = $pmc->parents();
    my $name    = $pmc->name();
    my $parent  = $parents->[0];
    next if $name eq 'default';

    push @{ $dependencies{$parent} }, $name;
    $dependencies{$name} ||= [];
}

sub gather_deps
{
    my ($deps, $name, $order) = @_;

    my @deps_at_level         = @{ $deps->{$name} };
    push @$order, @deps_at_level;
    gather_deps($deps, $_, $order) for @deps_at_level;
}

my $dep_list = [];
gather_deps( \%dependencies, 'default', $dep_list );
print STDERR join("\n", 'default', @$dep_list, '');
