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:
parent
db110759ab
commit
b4d033ff54
@ -360,24 +360,39 @@ fn types_to_c(types []Type, table &Table) string {
|
|||||||
return sb.str()
|
return sb.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pretty inefficient algo, works fine with N < 1000 (TODO optimize)
|
// sort structs by dependant fields
|
||||||
fn sort_structs(types mut []Type) {
|
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 i := 0; i < types.len; i++ {
|
||||||
for j in 0 .. i {
|
type_names << types[i].name
|
||||||
t := types[i]
|
}
|
||||||
//t2 := types[j]
|
// loop over types
|
||||||
// check if any of the types before `t` reference `t`
|
for i := 0; i < types.len; i++ {
|
||||||
if types[j].contains_field_type(t.name) {
|
t := types[i]
|
||||||
//println('moving up: $t.name len=$types.len')
|
// create list of deps
|
||||||
types.insert(j, t)
|
mut field_types := []string
|
||||||
types.delete(i+1)
|
for field in t.fields {
|
||||||
i = 0 // Start from scratch
|
// skip if not in types list
|
||||||
cnt++
|
if !(field.typ in type_names) {
|
||||||
if cnt > 500 {
|
continue
|
||||||
println('infinite type loop (perhaps you have a recursive struct `$t.name`?)')
|
}
|
||||||
exit(1)
|
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
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
130
compiler/depgraph.v
Normal file
130
compiler/depgraph.v
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -577,7 +577,7 @@ fn (v mut V) add_v_files_to_compile() {
|
|||||||
println(v.table.imports)
|
println(v.table.imports)
|
||||||
}
|
}
|
||||||
// graph deps
|
// graph deps
|
||||||
mut dep_graph := new_mod_dep_graph()
|
mut dep_graph := new_dep_graph()
|
||||||
dep_graph.from_import_tables(v.table.file_imports)
|
dep_graph.from_import_tables(v.table.file_imports)
|
||||||
deps_resolved := dep_graph.resolve()
|
deps_resolved := dep_graph.resolve()
|
||||||
if !deps_resolved.acyclic {
|
if !deps_resolved.acyclic {
|
||||||
|
@ -6,50 +6,8 @@ module main
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
struct ModDepGraphNode {
|
// add a module and its deps (module speficic dag method)
|
||||||
mut:
|
pub fn(graph mut DepGraph) from_import_tables(import_tables []FileImportTable) {
|
||||||
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) {
|
|
||||||
for fit in import_tables {
|
for fit in import_tables {
|
||||||
mut deps := []string
|
mut deps := []string
|
||||||
for _, m in fit.imports {
|
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) {
|
// get ordered imports (module speficic dag method)
|
||||||
graph.nodes << ModDepGraphNode{
|
pub fn(graph &DepGraph) imports() []string {
|
||||||
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 {
|
|
||||||
mut mods := []string
|
mut mods := []string
|
||||||
for node in graph.nodes {
|
for node in graph.nodes {
|
||||||
if node.name == 'main' {
|
if node.name == 'main' {
|
||||||
@ -127,27 +29,6 @@ pub fn(graph &ModDepGraph) imports() []string {
|
|||||||
return mods
|
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'
|
// 'strings' => 'VROOT/vlib/strings'
|
||||||
// 'installed_mod' => '~/.vmodules/installed_mod'
|
// 'installed_mod' => '~/.vmodules/installed_mod'
|
||||||
// 'local_mod' => '/path/to/current/dir/local_mod'
|
// 'local_mod' => '/path/to/current/dir/local_mod'
|
||||||
|
@ -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() {
|
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'
|
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 csv_reader := csv.new_reader(data)
|
||||||
|
|
||||||
// mut row_count := 0
|
mut row_count := 0
|
||||||
// for {
|
for {
|
||||||
// row := csv_reader.read() or {
|
row := csv_reader.read() or {
|
||||||
// break
|
break
|
||||||
// }
|
}
|
||||||
// row_count++
|
row_count++
|
||||||
// if row_count== 1 {
|
if row_count== 1 {
|
||||||
// assert row[0] == 'name'
|
assert row[0] == 'name'
|
||||||
// }
|
}
|
||||||
// if row_count == 2 {
|
if row_count == 2 {
|
||||||
// assert row[0] == 'joe'
|
assert row[0] == 'joe'
|
||||||
// }
|
}
|
||||||
// if row_count == 3 {
|
if row_count == 3 {
|
||||||
// assert row[0] == 'sam'
|
assert row[0] == 'sam'
|
||||||
// // quoted field
|
// quoted field
|
||||||
// assert row[3] == 'test quoted field'
|
assert row[3] == 'test quoted field'
|
||||||
// }
|
}
|
||||||
// if row_count == 4 {
|
if row_count == 4 {
|
||||||
// assert row[0] == 'mike'
|
assert row[0] == 'mike'
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// assert row_count == 4
|
assert row_count == 4
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fn test_encoding_csv_writer() {
|
fn test_encoding_csv_writer() {
|
||||||
// mut csv_writer := csv.new_writer()
|
mut csv_writer := csv.new_writer()
|
||||||
|
|
||||||
// csv_writer.write(['name', 'email', 'phone', 'other'])
|
csv_writer.write(['name', 'email', 'phone', 'other'])
|
||||||
// csv_writer.write(['joe', 'joe@blow.com', '0400000000', 'test'])
|
csv_writer.write(['joe', 'joe@blow.com', '0400000000', 'test'])
|
||||||
// csv_writer.write(['sam', 'sam@likesham.com', '0433000000', 'needs, quoting'])
|
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'
|
||||||
// }
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user