diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 859288bf20..c49caa4558 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -16,11 +16,11 @@ import ( pub struct Builder { pub: pref &pref.Preferences - table &table.Table - checker checker.Checker - os pref.OS // the OS to build for - compiled_dir string // contains os.realpath() of the dir of the final file beeing compiled, or the dir itself when doing `v .` - module_path string + table &table.Table + checker checker.Checker + os pref.OS // the OS to build for + compiled_dir string // contains os.realpath() of the dir of the final file beeing compiled, or the dir itself when doing `v .` + module_path string module_search_paths []string mut: parsed_files []ast.File @@ -47,14 +47,20 @@ pub fn (b mut Builder) build_c(v_files []string, out_file string) { } pub fn (b mut Builder) build_x64(v_files []string, out_file string) { - ticks := time.ticks() + t0 := time.ticks() b.parsed_files = parser.parse_files(v_files, b.table) b.parse_imports() - println('PARSE: ${time.ticks() - ticks}ms') + t1 := time.ticks() + parse_time := t1 - t0 + println('PARSE: ${parse_time}ms') b.checker.check_files(b.parsed_files) - println('CHECK: ${time.ticks() - ticks}ms') + t2 := time.ticks() + check_time := t2 - t1 + println('CHECK: ${check_time}ms') x64.gen(b.parsed_files, out_file) - println('x64 GEN: ${time.ticks() - ticks}ms') + t3 := time.ticks() + gen_time := t3 - t2 + println('x64 GEN: ${gen_time}ms') } // parse all deps from already parsed files @@ -68,20 +74,20 @@ pub fn (b mut Builder) parse_imports() { continue } import_path := b.find_module_path(mod) or { - //v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)', v.parsers[i].import_table.get_import_tok_idx(mod)) - //break + // v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)', v.parsers[i].import_table.get_import_tok_idx(mod)) + // break panic('cannot import module "$mod" (not found)') } v_files := b.v_files_from_dir(import_path) if v_files.len == 0 { - //v.parsers[i].error_with_token_index('cannot import module "$mod" (no .v files in "$import_path")', v.parsers[i].import_table.get_import_tok_idx(mod)) + // v.parsers[i].error_with_token_index('cannot import module "$mod" (no .v files in "$import_path")', v.parsers[i].import_table.get_import_tok_idx(mod)) panic('cannot import module "$mod" (no .v files in "$import_path")') } // Add all imports referenced by these libs parsed_files := parser.parse_files(v_files, b.table) for file in parsed_files { if file.mod.name != mod { - //v.parsers[pidx].error_with_token_index('bad module definition: ${v.parsers[pidx].file_path} imports module "$mod" but $file is defined as module `$p_mod`', 1 + // v.parsers[pidx].error_with_token_index('bad module definition: ${v.parsers[pidx].file_path} imports module "$mod" but $file is defined as module `$p_mod`', 1 panic('bad module definition: ${ast_file.path} imports module "$mod" but $file.path is defined as module `$ast_file.mod.name`') } } @@ -103,7 +109,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{ + mut files := os.ls(dir) or { panic(err) } if b.pref.is_verbose { @@ -150,6 +156,7 @@ pub fn (b &Builder) v_files_from_dir(dir string) []string { } } */ + res << filepath.join(dir,file) } return res diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4ea7642968..31338aae02 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -11,11 +11,10 @@ import ( ) pub struct Checker { - table &table.Table + table &table.Table mut: - file_name string - unresolved []ast.Expr - resolved []table.TypeRef + file_name string + resolved []table.TypeRef } pub fn new_checker(table &table.Table) Checker { @@ -26,27 +25,38 @@ pub fn new_checker(table &table.Table) Checker { pub fn (c mut Checker) check(ast_file ast.File) { c.file_name = ast_file.path - c.unresolved = ast_file.unresolved - c.resolve_types() + // if ast_file.unresolved.len != c.resolved.len { + // c.resolve_exprs(file) + // } + c.complete_types(ast_file) for stmt in ast_file.stmts { c.stmt(stmt) } } pub fn (c mut Checker) check_files(ast_files []ast.File) { + // this cant be moved to check() for multiple + // files this muse be done first. TODO: optimize + for file in ast_files { + c.file_name = file.path + c.resolve_expr_types(file) + } for file in ast_files { c.check(file) } } -fn (c mut Checker) resolve_types() { - // resolve type of unresolved expressions - for x in c.unresolved { +// resolve type of unresolved expressions +fn (c mut Checker) resolve_expr_types(f ast.File) { + for x in f.unresolved { c.resolved << c.expr(x) } - // update any types with unresolved sub types +} + +// update any types chich contain unresolved sub types +fn (c &Checker) complete_types(f ast.File) { for idx, t in c.table.types { - println('Resolve type: $t.name') + // println('Resolve type: $t.name') if t.kind == .array { mut info := t.array_info() if info.elem_type.typ.kind == .unresolved { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d25fff11a2..3d8fa0e54c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -26,25 +26,25 @@ type PostfixParseFn fn()ast.Expr struct Parser { - scanner &scanner.Scanner - file_name string + scanner &scanner.Scanner + file_name string mut: - tok token.Token - peek_tok token.Token + tok token.Token + peek_tok token.Token // vars []string - table &table.Table - return_type table.TypeRef // current function's return type + table &table.Table + return_type table.TypeRef // current function's return type // scope_level int // var_idx int - is_c bool + is_c bool // // prefix_parse_fns []PrefixParseFn - inside_if bool - pref &pref.Preferences // Preferences shared from V struct - builtin_mod bool - mod string - unresolved []ast.Expr - unresolved_idxs map[string]int + inside_if bool + pref &pref.Preferences // Preferences shared from V struct + builtin_mod bool + mod string + unresolved []ast.Expr + unresolved_offset int } // for tests @@ -1223,9 +1223,9 @@ fn (p mut Parser) match_expr() (ast.Expr,table.TypeRef) { } fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.TypeRef { - mut idx := p.unresolved.len - if key in p.unresolved_idxs { - idx = p.unresolved_idxs[key] + mut idx := p.unresolved_offset + p.unresolved.len + if key in p.table.unresolved_idxs { + idx = p.table.unresolved_idxs[key] } else { p.unresolved << expr @@ -1235,7 +1235,7 @@ fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.TypeRef { typ: &table.Type{ parent: 0 kind: .unresolved - name: 'unresolved $idx' + name: 'unresolved-$idx' } } return t diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index b3f6e570cc..398e24287b 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -8,17 +8,18 @@ module table pub struct Table { // struct_fields map[string][]string pub mut: - types []Type + types []Type // type_idxs Hashmap - type_idxs map[string]int - local_vars []Var - scope_level int - var_idx int + type_idxs map[string]int + unresolved_idxs map[string]int + local_vars []Var + scope_level int + var_idx int // fns Hashmap - fns map[string]Fn - consts map[string]Var - tmp_cnt int - imports []string + fns map[string]Fn + consts map[string]Var + tmp_cnt int + imports []string } pub struct Fn {