modules: cycle error improvements

pull/1270/head
joe-conigliaro 2019-07-22 20:44:08 +10:00 committed by Alexander Medvednikov
parent a65ae726ee
commit 0ed240e123
1 changed files with 23 additions and 11 deletions

View File

@ -5,8 +5,10 @@
module main module main
struct ModDepGraphNode { struct ModDepGraphNode {
mut:
name string name string
deps []string deps []string
last_cycle string
} }
struct ModDepGraph { struct ModDepGraph {
@ -21,13 +23,13 @@ struct DepSet {
items []string items []string
} }
pub fn(mapset mut DepSet) add(item string) { pub fn(dset mut DepSet) add(item string) {
mapset.items << item dset.items << item
} }
pub fn(mapset &DepSet) diff(otherset DepSet) DepSet { pub fn(dset &DepSet) diff(otherset DepSet) DepSet {
mut diff := DepSet{} mut diff := DepSet{}
for item in mapset.items { for item in dset.items {
if !item in otherset.items { if !item in otherset.items {
diff.items << item diff.items << item
} }
@ -35,8 +37,8 @@ pub fn(mapset &DepSet) diff(otherset DepSet) DepSet {
return diff return diff
} }
pub fn(mapset &DepSet) size() int { pub fn(dset &DepSet) size() int {
return mapset.items.len return dset.items.len
} }
pub fn new_mod_dep_graph() *ModDepGraph { pub fn new_mod_dep_graph() *ModDepGraph {
@ -88,13 +90,18 @@ pub fn(graph &ModDepGraph) resolve() *ModDepGraph {
if ready_set.size() == 0 { if ready_set.size() == 0 {
mut g := new_mod_dep_graph() mut g := new_mod_dep_graph()
g.acyclic = false g.acyclic = false
ndk := node_deps.keys()
for name, _ in node_deps { for name, _ in node_deps {
g.nodes << node_names[name] mut node := node_names[name]
if name == ndk[node_deps.size-1] {
node.last_cycle = node_deps[name].items[node_deps[name].items.len-1]
}
g.nodes << node
} }
return g return g
} }
ready_set.items.len > 0 { ready_set.size() > 0 {
mut new_set := map[string]DepSet{} mut new_set := map[string]DepSet{}
for name in ready_set.items { for name in ready_set.items {
// node_deps.remove(name) // node_deps.remove(name)
@ -133,9 +140,14 @@ pub fn(graph &ModDepGraph) last_node() {
} }
pub fn(graph &ModDepGraph) display() { pub fn(graph &ModDepGraph) display() {
for _, node in graph.nodes { for i:=0; i<graph.nodes.len; i++ {
for _, dep in node.deps { node := graph.nodes[i]
println(' * $node.name -> $dep') for dep in node.deps {
mut out := ' * $node.name -> $dep'
if !graph.acyclic && i == graph.nodes.len-1 && dep == node.last_cycle {
out += ' <-- last cycle'
}
println(out)
} }
} }
} }