v2: checker & unresolved fixes & small updates

pull/3675/head
joe-conigliaro 2020-02-08 04:46:42 +11:00 committed by GitHub
parent 36e636743b
commit 35bef514b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 51 deletions

View 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) { 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.parsed_files = parser.parse_files(v_files, b.table)
b.parse_imports() 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) 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) 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 // parse all deps from already parsed files
@ -68,20 +74,20 @@ pub fn (b mut Builder) parse_imports() {
continue continue
} }
import_path := b.find_module_path(mod) or { 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)) // v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)', v.parsers[i].import_table.get_import_tok_idx(mod))
//break // break
panic('cannot import module "$mod" (not found)') panic('cannot import module "$mod" (not found)')
} }
v_files := b.v_files_from_dir(import_path) v_files := b.v_files_from_dir(import_path)
if v_files.len == 0 { 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")') panic('cannot import module "$mod" (no .v files in "$import_path")')
} }
// Add all imports referenced by these libs // Add all imports referenced by these libs
parsed_files := parser.parse_files(v_files, b.table) parsed_files := parser.parse_files(v_files, b.table)
for file in parsed_files { for file in parsed_files {
if file.mod.name != mod { 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`') 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) { else if !os.is_dir(dir) {
verror("$dir isn't a directory") verror("$dir isn't a directory")
} }
mut files := os.ls(dir)or{ mut files := os.ls(dir) or {
panic(err) panic(err)
} }
if b.pref.is_verbose { 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) res << filepath.join(dir,file)
} }
return res return res

View File

@ -14,7 +14,6 @@ pub struct Checker {
table &table.Table table &table.Table
mut: mut:
file_name string file_name string
unresolved []ast.Expr
resolved []table.TypeRef resolved []table.TypeRef
} }
@ -26,27 +25,38 @@ pub fn new_checker(table &table.Table) Checker {
pub fn (c mut Checker) check(ast_file ast.File) { pub fn (c mut Checker) check(ast_file ast.File) {
c.file_name = ast_file.path c.file_name = ast_file.path
c.unresolved = ast_file.unresolved // if ast_file.unresolved.len != c.resolved.len {
c.resolve_types() // c.resolve_exprs(file)
// }
c.complete_types(ast_file)
for stmt in ast_file.stmts { for stmt in ast_file.stmts {
c.stmt(stmt) c.stmt(stmt)
} }
} }
pub fn (c mut Checker) check_files(ast_files []ast.File) { 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 { for file in ast_files {
c.check(file) c.check(file)
} }
} }
fn (c mut Checker) resolve_types() { // resolve type of unresolved expressions
// resolve type of unresolved expressions fn (c mut Checker) resolve_expr_types(f ast.File) {
for x in c.unresolved { for x in f.unresolved {
c.resolved << c.expr(x) 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 { for idx, t in c.table.types {
println('Resolve type: $t.name') // println('Resolve type: $t.name')
if t.kind == .array { if t.kind == .array {
mut info := t.array_info() mut info := t.array_info()
if info.elem_type.typ.kind == .unresolved { if info.elem_type.typ.kind == .unresolved {

View File

@ -44,7 +44,7 @@ mut:
builtin_mod bool builtin_mod bool
mod string mod string
unresolved []ast.Expr unresolved []ast.Expr
unresolved_idxs map[string]int unresolved_offset int
} }
// for tests // 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 { fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.TypeRef {
mut idx := p.unresolved.len mut idx := p.unresolved_offset + p.unresolved.len
if key in p.unresolved_idxs { if key in p.table.unresolved_idxs {
idx = p.unresolved_idxs[key] idx = p.table.unresolved_idxs[key]
} }
else { else {
p.unresolved << expr p.unresolved << expr
@ -1235,7 +1235,7 @@ fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.TypeRef {
typ: &table.Type{ typ: &table.Type{
parent: 0 parent: 0
kind: .unresolved kind: .unresolved
name: 'unresolved $idx' name: 'unresolved-$idx'
} }
} }
return t return t

View File

@ -11,6 +11,7 @@ pub mut:
types []Type types []Type
// type_idxs Hashmap // type_idxs Hashmap
type_idxs map[string]int type_idxs map[string]int
unresolved_idxs map[string]int
local_vars []Var local_vars []Var
scope_level int scope_level int
var_idx int var_idx int