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

fmt builder.v

This commit is contained in:
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 module builder
import ( import os
os import time
time import v.ast
v.ast import v.table
v.table import v.pref
v.pref import v.util
v.util import v.vmod
v.vmod import v.checker
v.checker import v.parser
v.parser import v.scanner
v.scanner import v.gen
v.gen import v.gen.js
v.gen.js import v.gen.x64
v.gen.x64 import v.depgraph
v.depgraph
)
pub struct Builder { pub struct Builder {
pub: pub:
@ -36,7 +34,7 @@ pub fn new_builder(pref &pref.Preferences) Builder {
rdir := os.real_path(pref.path) rdir := os.real_path(pref.path)
compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) } compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
table := table.new_table() table := table.new_table()
return builder.Builder{ return Builder{
pref: pref pref: pref
table: table table: table
checker: checker.new_checker(table, pref) checker: checker.new_checker(table, pref)
@ -49,7 +47,7 @@ pub fn new_builder(pref &pref.Preferences) Builder {
// parse all deps from already parsed files // parse all deps from already parsed files
pub fn (b mut Builder) parse_imports() { pub fn (b mut Builder) parse_imports() {
mut done_imports := []string var done_imports := []string
// NB: b.parsed_files is appended in the loop, // NB: b.parsed_files is appended in the loop,
// so we can not use the shorter `for in` form. // 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++ {
@ -86,12 +84,11 @@ pub fn (b mut Builder) parse_imports() {
b.resolve_deps() b.resolve_deps()
} }
pub fn (b mut Builder) resolve_deps() { pub fn (b mut Builder) resolve_deps() {
graph := b.import_graph() graph := b.import_graph()
deps_resolved := graph.resolve() deps_resolved := graph.resolve()
if !deps_resolved.acyclic { 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 // TODO: error, when v itself does not have v.table -> v.ast -> v.table cycles anymore
return return
} }
@ -100,7 +97,7 @@ pub fn (b mut Builder) resolve_deps() {
eprintln(deps_resolved.display()) eprintln(deps_resolved.display())
eprintln('------------------------------------------') eprintln('------------------------------------------')
} }
mut mods := []string var mods := []string
for node in deps_resolved.nodes { for node in deps_resolved.nodes {
mods << node.name mods << node.name
} }
@ -109,7 +106,7 @@ pub fn (b mut Builder) resolve_deps() {
eprintln(mods.str()) eprintln(mods.str())
eprintln('-------------------------------') eprintln('-------------------------------')
} }
mut reordered_parsed_files := []ast.File var reordered_parsed_files := []ast.File
for m in mods { for m in mods {
for pf in b.parsed_files { for pf in b.parsed_files {
if m == pf.mod.name { if m == pf.mod.name {
@ -123,11 +120,11 @@ pub fn (b mut Builder) resolve_deps() {
// graph of all imported modules // graph of all imported modules
pub fn (b &Builder) import_graph() &depgraph.DepGraph { pub fn (b &Builder) import_graph() &depgraph.DepGraph {
mut builtins := util.builtin_module_parts var builtins := util.builtin_module_parts
builtins << 'builtin' builtins << 'builtin'
mut graph := depgraph.new_dep_graph() var graph := depgraph.new_dep_graph()
for p in b.parsed_files { for p in b.parsed_files {
mut deps := []string var deps := []string
if p.mod.name !in builtins { if p.mod.name !in builtins {
deps << 'builtin' 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 { pub fn (b Builder) v_files_from_dir(dir string) []string {
mut res := []string var res := []string
if !os.exists(dir) { if !os.exists(dir) {
if dir == 'compiler' && os.is_dir('vlib') { if dir == 'compiler' && os.is_dir('vlib') {
println('looks like you are trying to build V with an old command') 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) { } else if !os.is_dir(dir) {
verror("$dir isn't a directory!") verror("$dir isn't a directory!")
} }
mut files := os.ls(dir) or { var files := os.ls(dir) or {
panic(err) panic(err)
} }
if b.pref.is_verbose { if b.pref.is_verbose {
@ -171,7 +168,7 @@ pub fn (b Builder) v_files_from_dir(dir string) []string {
continue continue
} }
if b.pref.compile_defines_all.len > 0 && file.contains('_d_') { if b.pref.compile_defines_all.len > 0 && file.contains('_d_') {
mut allowed := false var allowed := false
for cdefine in b.pref.compile_defines { for cdefine in b.pref.compile_defines {
file_postfix := '_d_${cdefine}.v' file_postfix := '_d_${cdefine}.v'
if file.ends_with(file_postfix) { if file.ends_with(file_postfix) {
@ -245,11 +242,11 @@ fn module_path(mod string) string {
return mod.replace('.', os.path_separator) 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: // 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) 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) { 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 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 { pub fn (p mut Parser) name_expr() ast.Expr {
var node := ast.Expr{} var node := ast.Expr{}
if p.inside_is { if p.inside_is {
p.inside_is = false
return ast.Type{ return ast.Type{
typ: p.parse_type() typ: p.parse_type()
} }
@ -1054,7 +1055,6 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
p.inside_is = true p.inside_is = true
} }
right = p.expr(precedence) right = p.expr(precedence)
p.inside_is = false
var expr := ast.Expr{} var expr := ast.Expr{}
expr = ast.InfixExpr{ expr = ast.InfixExpr{
left: left left: left