v2: checker & unresolved fixes & small updates
parent
36e636743b
commit
35bef514b0
|
@ -16,11 +16,11 @@ import (
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
pub:
|
pub:
|
||||||
pref &pref.Preferences
|
pref &pref.Preferences
|
||||||
table &table.Table
|
table &table.Table
|
||||||
checker checker.Checker
|
checker checker.Checker
|
||||||
os pref.OS // the OS to build for
|
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 .`
|
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_path string
|
||||||
module_search_paths []string
|
module_search_paths []string
|
||||||
mut:
|
mut:
|
||||||
parsed_files []ast.File
|
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) {
|
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
|
||||||
|
|
|
@ -11,11 +11,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
pub struct Checker {
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_checker(table &table.Table) Checker {
|
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) {
|
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 {
|
||||||
|
|
|
@ -26,25 +26,25 @@ type PostfixParseFn fn()ast.Expr
|
||||||
|
|
||||||
|
|
||||||
struct Parser {
|
struct Parser {
|
||||||
scanner &scanner.Scanner
|
scanner &scanner.Scanner
|
||||||
file_name string
|
file_name string
|
||||||
mut:
|
mut:
|
||||||
tok token.Token
|
tok token.Token
|
||||||
peek_tok token.Token
|
peek_tok token.Token
|
||||||
// vars []string
|
// vars []string
|
||||||
table &table.Table
|
table &table.Table
|
||||||
return_type table.TypeRef // current function's return type
|
return_type table.TypeRef // current function's return type
|
||||||
// scope_level int
|
// scope_level int
|
||||||
// var_idx int
|
// var_idx int
|
||||||
is_c bool
|
is_c bool
|
||||||
//
|
//
|
||||||
// prefix_parse_fns []PrefixParseFn
|
// prefix_parse_fns []PrefixParseFn
|
||||||
inside_if bool
|
inside_if bool
|
||||||
pref &pref.Preferences // Preferences shared from V struct
|
pref &pref.Preferences // Preferences shared from V struct
|
||||||
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
|
||||||
|
|
|
@ -8,17 +8,18 @@ module table
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
// struct_fields map[string][]string
|
// struct_fields map[string][]string
|
||||||
pub mut:
|
pub mut:
|
||||||
types []Type
|
types []Type
|
||||||
// type_idxs Hashmap
|
// type_idxs Hashmap
|
||||||
type_idxs map[string]int
|
type_idxs map[string]int
|
||||||
local_vars []Var
|
unresolved_idxs map[string]int
|
||||||
scope_level int
|
local_vars []Var
|
||||||
var_idx int
|
scope_level int
|
||||||
|
var_idx int
|
||||||
// fns Hashmap
|
// fns Hashmap
|
||||||
fns map[string]Fn
|
fns map[string]Fn
|
||||||
consts map[string]Var
|
consts map[string]Var
|
||||||
tmp_cnt int
|
tmp_cnt int
|
||||||
imports []string
|
imports []string
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Fn {
|
pub struct Fn {
|
||||||
|
|
Loading…
Reference in New Issue