fmt builder.v

pull/4447/head
Alexander Medvednikov 2020-04-16 15:35:19 +02:00
parent af224b4933
commit 8760313ce5
2 changed files with 32 additions and 35 deletions

View File

@ -1,21 +1,19 @@
module builder
import (
os
time
v.ast
v.table
v.pref
v.util
v.vmod
v.checker
v.parser
v.scanner
v.gen
v.gen.js
v.gen.x64
v.depgraph
)
import os
import time
import v.ast
import v.table
import v.pref
import v.util
import v.vmod
import v.checker
import v.parser
import v.scanner
import v.gen
import v.gen.js
import v.gen.x64
import v.depgraph
pub struct Builder {
pub:
@ -29,14 +27,14 @@ mut:
parsed_files []ast.File
global_scope &ast.Scope
out_name_c string
out_name_js string
out_name_js string
}
pub fn new_builder(pref &pref.Preferences) Builder {
rdir := os.real_path(pref.path)
compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
table := table.new_table()
return builder.Builder{
return Builder{
pref: pref
table: table
checker: checker.new_checker(table, pref)
@ -49,10 +47,10 @@ pub fn new_builder(pref &pref.Preferences) Builder {
// parse all deps from already parsed files
pub fn (b mut Builder) parse_imports() {
mut done_imports := []string
var done_imports := []string
// NB: b.parsed_files is appended in the loop,
// so we can not use the shorter `for in` form.
for i := 0; i<b.parsed_files.len; i++ {
for i := 0; i < b.parsed_files.len; i++ {
ast_file := b.parsed_files[i]
for _, imp in ast_file.imports {
mod := imp.mod
@ -86,12 +84,11 @@ pub fn (b mut Builder) parse_imports() {
b.resolve_deps()
}
pub fn (b mut Builder) resolve_deps() {
graph := b.import_graph()
deps_resolved := graph.resolve()
if !deps_resolved.acyclic {
eprintln('import cycle detected between the following modules: \n' + deps_resolved.display_cycles())
eprintln('warning: import cycle detected between the following modules: \n' + deps_resolved.display_cycles())
// TODO: error, when v itself does not have v.table -> v.ast -> v.table cycles anymore
return
}
@ -100,7 +97,7 @@ pub fn (b mut Builder) resolve_deps() {
eprintln(deps_resolved.display())
eprintln('------------------------------------------')
}
mut mods := []string
var mods := []string
for node in deps_resolved.nodes {
mods << node.name
}
@ -109,12 +106,12 @@ pub fn (b mut Builder) resolve_deps() {
eprintln(mods.str())
eprintln('-------------------------------')
}
mut reordered_parsed_files := []ast.File
var reordered_parsed_files := []ast.File
for m in mods {
for pf in b.parsed_files {
if m == pf.mod.name {
reordered_parsed_files << pf
//eprintln('pf.mod.name: $pf.mod.name | pf.path: $pf.path')
// eprintln('pf.mod.name: $pf.mod.name | pf.path: $pf.path')
}
}
}
@ -123,11 +120,11 @@ pub fn (b mut Builder) resolve_deps() {
// graph of all imported modules
pub fn (b &Builder) import_graph() &depgraph.DepGraph {
mut builtins := util.builtin_module_parts
var builtins := util.builtin_module_parts
builtins << 'builtin'
mut graph := depgraph.new_dep_graph()
var graph := depgraph.new_dep_graph()
for p in b.parsed_files {
mut deps := []string
var deps := []string
if p.mod.name !in builtins {
deps << 'builtin'
}
@ -140,7 +137,7 @@ pub fn (b &Builder) import_graph() &depgraph.DepGraph {
}
pub fn (b Builder) v_files_from_dir(dir string) []string {
mut res := []string
var res := []string
if !os.exists(dir) {
if dir == 'compiler' && os.is_dir('vlib') {
println('looks like you are trying to build V with an old command')
@ -150,7 +147,7 @@ pub fn (b Builder) v_files_from_dir(dir string) []string {
} else if !os.is_dir(dir) {
verror("$dir isn't a directory!")
}
mut files := os.ls(dir) or {
var files := os.ls(dir) or {
panic(err)
}
if b.pref.is_verbose {
@ -171,7 +168,7 @@ pub fn (b Builder) v_files_from_dir(dir string) []string {
continue
}
if b.pref.compile_defines_all.len > 0 && file.contains('_d_') {
mut allowed := false
var allowed := false
for cdefine in b.pref.compile_defines {
file_postfix := '_d_${cdefine}.v'
if file.ends_with(file_postfix) {
@ -245,11 +242,11 @@ fn module_path(mod string) string {
return mod.replace('.', os.path_separator)
}
pub fn (b Builder) find_module_path(mod string, fpath string) ?string {
pub fn (b Builder) find_module_path(mod, fpath string) ?string {
// support @VROOT/v.mod relative paths:
vmod_file_location := vmod.mod_file_cacher.get( fpath )
vmod_file_location := vmod.mod_file_cacher.get(fpath)
mod_path := module_path(mod)
mut module_lookup_paths := []string
var module_lookup_paths := []string
if vmod_file_location.vmod_file.len != 0 && !(vmod_file_location.vmod_folder in b.module_search_paths) {
module_lookup_paths << vmod_file_location.vmod_folder
}

View File

@ -618,6 +618,7 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
pub fn (p mut Parser) name_expr() ast.Expr {
var node := ast.Expr{}
if p.inside_is {
p.inside_is = false
return ast.Type{
typ: p.parse_type()
}
@ -1054,7 +1055,6 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
p.inside_is = true
}
right = p.expr(precedence)
p.inside_is = false
var expr := ast.Expr{}
expr = ast.InfixExpr{
left: left