1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: generalize mod dag & use for sorting structs also

This commit is contained in:
joe-conigliaro 2019-09-04 02:11:21 +10:00 committed by Alexander Medvednikov
parent db110759ab
commit b4d033ff54
5 changed files with 200 additions and 175 deletions

View File

@ -360,24 +360,39 @@ fn types_to_c(types []Type, table &Table) string {
return sb.str()
}
// pretty inefficient algo, works fine with N < 1000 (TODO optimize)
// sort structs by dependant fields
fn sort_structs(types mut []Type) {
mut cnt := 0
mut graph := new_dep_graph()
// types list
mut type_names := []string
for i := 0; i < types.len; i++ {
for j in 0 .. i {
t := types[i]
//t2 := types[j]
// check if any of the types before `t` reference `t`
if types[j].contains_field_type(t.name) {
//println('moving up: $t.name len=$types.len')
types.insert(j, t)
types.delete(i+1)
i = 0 // Start from scratch
cnt++
if cnt > 500 {
println('infinite type loop (perhaps you have a recursive struct `$t.name`?)')
exit(1)
}
type_names << types[i].name
}
// loop over types
for i := 0; i < types.len; i++ {
t := types[i]
// create list of deps
mut field_types := []string
for field in t.fields {
// skip if not in types list
if !(field.typ in type_names) {
continue
}
field_types << field.typ
}
// add type and dependant types to graph
graph.add(t.name, field_types)
}
// sort
sorted := graph.resolve()
// reorder types
old_types := types.clone()
for i:=0; i<sorted.nodes.len; i++ {
node := sorted.nodes[i]
for j := 0; j < old_types.len; j++ {
t := old_types[j]
if t.name == node.name {
types[i] = t
continue
}
}

130
compiler/depgraph.v Normal file
View File

@ -0,0 +1,130 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Directed acyclic graph
// this implementation is specifically suited to ordering dependencies
module main
struct DepGraphNode {
mut:
name string
deps []string
last_cycle string
}
struct DepGraph {
pub:
mut:
acyclic bool
nodes []DepGraphNode
}
struct DepSet {
mut:
items []string
}
pub fn(dset mut DepSet) add(item string) {
dset.items << item
}
pub fn(dset &DepSet) diff(otherset DepSet) DepSet {
mut diff := DepSet{}
for item in dset.items {
if !item in otherset.items {
diff.items << item
}
}
return diff
}
pub fn(dset &DepSet) size() int {
return dset.items.len
}
pub fn new_dep_graph() &DepGraph {
return &DepGraph{
acyclic: true
}
}
pub fn(graph mut DepGraph) add(mod string, deps []string) {
graph.nodes << DepGraphNode{
name: mod,
deps: deps
}
}
pub fn(graph &DepGraph) resolve() &DepGraph {
mut node_names := map[string]DepGraphNode
mut node_deps := map[string]DepSet
for _, node in graph.nodes {
node_names[node.name] = node
mut dep_set := DepSet{}
for _, dep in node.deps {
dep_set.add(dep)
}
node_deps[node.name] = dep_set
}
mut resolved := new_dep_graph()
for node_deps.size != 0 {
mut ready_set := DepSet{}
for name, deps in node_deps {
if deps.size() == 0 {
ready_set.add(name)
}
}
if ready_set.size() == 0 {
mut g := new_dep_graph()
g.acyclic = false
ndk := node_deps.keys()
for name, _ in node_deps {
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
}
for name in ready_set.items {
node_deps.delete(name)
resolved.nodes << node_names[name]
}
for name, deps in node_deps {
node_deps[name] = deps.diff(ready_set)
}
}
return resolved
}
pub fn(graph &DepGraph) last_node() DepGraphNode {
return graph.nodes[graph.nodes.len-1]
}
pub fn(graph &DepGraph) last_cycle() string {
return graph.last_node().last_cycle
}
pub fn(graph &DepGraph) display() {
for i:=0; i<graph.nodes.len; i++ {
node := graph.nodes[i]
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)
}
}
}

View File

@ -577,7 +577,7 @@ fn (v mut V) add_v_files_to_compile() {
println(v.table.imports)
}
// graph deps
mut dep_graph := new_mod_dep_graph()
mut dep_graph := new_dep_graph()
dep_graph.from_import_tables(v.table.file_imports)
deps_resolved := dep_graph.resolve()
if !deps_resolved.acyclic {

View File

@ -6,50 +6,8 @@ module main
import os
struct ModDepGraphNode {
mut:
name string
deps []string
last_cycle string
}
struct ModDepGraph {
pub:
mut:
acyclic bool
nodes []ModDepGraphNode
}
struct DepSet {
mut:
items []string
}
pub fn(dset mut DepSet) add(item string) {
dset.items << item
}
pub fn(dset &DepSet) diff(otherset DepSet) DepSet {
mut diff := DepSet{}
for item in dset.items {
if !item in otherset.items {
diff.items << item
}
}
return diff
}
pub fn(dset &DepSet) size() int {
return dset.items.len
}
pub fn new_mod_dep_graph() &ModDepGraph {
return &ModDepGraph{
acyclic: true
}
}
pub fn(graph mut ModDepGraph) from_import_tables(import_tables []FileImportTable) {
// add a module and its deps (module speficic dag method)
pub fn(graph mut DepGraph) from_import_tables(import_tables []FileImportTable) {
for fit in import_tables {
mut deps := []string
for _, m in fit.imports {
@ -59,64 +17,8 @@ pub fn(graph mut ModDepGraph) from_import_tables(import_tables []FileImportTable
}
}
pub fn(graph mut ModDepGraph) add(mod string, deps []string) {
graph.nodes << ModDepGraphNode{
name: mod,
deps: deps
}
}
pub fn(graph &ModDepGraph) resolve() &ModDepGraph {
mut node_names := map[string]ModDepGraphNode
mut node_deps := map[string]DepSet
for _, node in graph.nodes {
node_names[node.name] = node
mut dep_set := DepSet{}
for _, dep in node.deps {
dep_set.add(dep)
}
node_deps[node.name] = dep_set
}
mut resolved := new_mod_dep_graph()
for node_deps.size != 0 {
mut ready_set := DepSet{}
for name, deps in node_deps {
if deps.size() == 0 {
ready_set.add(name)
}
}
if ready_set.size() == 0 {
mut g := new_mod_dep_graph()
g.acyclic = false
ndk := node_deps.keys()
for name, _ in node_deps {
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
}
for name in ready_set.items {
node_deps.delete(name)
resolved.nodes << node_names[name]
}
for name, deps in node_deps {
node_deps[name] = deps.diff(ready_set)
}
}
return resolved
}
pub fn(graph &ModDepGraph) imports() []string {
// get ordered imports (module speficic dag method)
pub fn(graph &DepGraph) imports() []string {
mut mods := []string
for node in graph.nodes {
if node.name == 'main' {
@ -127,27 +29,6 @@ pub fn(graph &ModDepGraph) imports() []string {
return mods
}
pub fn(graph &ModDepGraph) last_node() ModDepGraphNode {
return graph.nodes[graph.nodes.len-1]
}
pub fn(graph &ModDepGraph) last_cycle() string {
return graph.last_node().last_cycle
}
pub fn(graph &ModDepGraph) display() {
for i:=0; i<graph.nodes.len; i++ {
node := graph.nodes[i]
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)
}
}
}
// 'strings' => 'VROOT/vlib/strings'
// 'installed_mod' => '~/.vmodules/installed_mod'
// 'local_mod' => '/path/to/current/dir/local_mod'

View File

@ -1,41 +1,40 @@
// Skip this test until struct field order bug is fixed
// import encoding.csv
import encoding.csv
// fn test_encoding_csv_reader() {
// data := 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"test quoted field"\n#chris,chris@nomail.com,94444444,"commented row"\nmike,mike@mikesbikes.com,98888888,"bike store"\n'
// mut csv_reader := csv.new_reader(data)
fn test_encoding_csv_reader() {
data := 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"test quoted field"\n#chris,chris@nomail.com,94444444,"commented row"\nmike,mike@mikesbikes.com,98888888,"bike store"\n'
mut csv_reader := csv.new_reader(data)
// mut row_count := 0
// for {
// row := csv_reader.read() or {
// break
// }
// row_count++
// if row_count== 1 {
// assert row[0] == 'name'
// }
// if row_count == 2 {
// assert row[0] == 'joe'
// }
// if row_count == 3 {
// assert row[0] == 'sam'
// // quoted field
// assert row[3] == 'test quoted field'
// }
// if row_count == 4 {
// assert row[0] == 'mike'
// }
// }
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row_count++
if row_count== 1 {
assert row[0] == 'name'
}
if row_count == 2 {
assert row[0] == 'joe'
}
if row_count == 3 {
assert row[0] == 'sam'
// quoted field
assert row[3] == 'test quoted field'
}
if row_count == 4 {
assert row[0] == 'mike'
}
}
// assert row_count == 4
// }
assert row_count == 4
}
// fn test_encoding_csv_writer() {
// mut csv_writer := csv.new_writer()
fn test_encoding_csv_writer() {
mut csv_writer := csv.new_writer()
// csv_writer.write(['name', 'email', 'phone', 'other'])
// csv_writer.write(['joe', 'joe@blow.com', '0400000000', 'test'])
// csv_writer.write(['sam', 'sam@likesham.com', '0433000000', 'needs, quoting'])
csv_writer.write(['name', 'email', 'phone', 'other'])
csv_writer.write(['joe', 'joe@blow.com', '0400000000', 'test'])
csv_writer.write(['sam', 'sam@likesham.com', '0433000000', 'needs, quoting'])
// assert csv_writer.str() == 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"needs, quoting"\n'
// }
assert csv_writer.str() == 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"needs, quoting"\n'
}