cgen: test local modules

pull/3903/head
Alexander Medvednikov 2020-03-01 21:56:07 +01:00
parent a707ffc04a
commit 38de6c98fc
8 changed files with 50 additions and 30 deletions

View File

@ -21,8 +21,8 @@ pub:
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
mut: mut:
module_search_paths []string
parsed_files []ast.File parsed_files []ast.File
} }
@ -76,6 +76,8 @@ pub fn (b mut Builder) parse_imports() {
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
// println('module_search_paths:')
// println(b.module_search_paths)
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)
@ -171,3 +173,26 @@ pub fn (b &Builder) log(s string) {
println(s) println(s)
} }
} }
[inline]
fn module_path(mod string) string {
// submodule support
return mod.replace('.', filepath.separator)
}
pub fn (b &Builder) find_module_path(mod string) ?string {
mod_path := module_path(mod)
for search_path in b.module_search_paths {
try_path := filepath.join(search_path,mod_path)
if b.pref.is_verbose {
println(' >> trying to find $mod in $try_path ..')
}
if os.is_dir(try_path) {
if b.pref.is_verbose {
println(' << found $try_path .')
}
return try_path
}
}
return error('module "$mod" not found')
}

View File

@ -4,26 +4,3 @@ import (
os os
filepath filepath
) )
[inline]
fn module_path(mod string) string {
// submodule support
return mod.replace('.', filepath.separator)
}
pub fn (b &Builder) find_module_path(mod string) ?string {
mod_path := module_path(mod)
for search_path in b.module_search_paths {
try_path := filepath.join(search_path,mod_path)
if b.pref.is_verbose {
println(' >> trying to find $mod in $try_path ..')
}
if os.is_dir(try_path) {
if b.pref.is_verbose {
println(' << found $try_path .')
}
return try_path
}
}
return error('module "$mod" not found')
}

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
max_nr_errors = 150 max_nr_errors = 350
) )
pub struct Checker { pub struct Checker {

View File

@ -85,8 +85,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
else { else {
type_sym := g.table.get_type_symbol(it.typ) type_sym := g.table.get_type_symbol(it.typ)
g.write('$type_sym.name ${it.name}(') name := it.name.replace('.', '__')
g.definitions.write('$type_sym.name ${it.name}(') g.write('$type_sym.name ${name}(')
g.definitions.write('$type_sym.name ${name}(')
} }
for i, arg in it.args { for i, arg in it.args {
arg_type_sym := g.table.get_type_symbol(arg.typ) arg_type_sym := g.table.get_type_symbol(arg.typ)
@ -283,7 +284,8 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('}') g.write('}')
} }
ast.CallExpr { ast.CallExpr {
g.write('${it.name}(') name := it.name.replace('.', '__')
g.write('${name}(')
g.call_args(it.args) g.call_args(it.args)
g.write(')') g.write(')')
/* /*
@ -298,7 +300,8 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
ast.MethodCallExpr { ast.MethodCallExpr {
typ := 'TODO' typ := 'TODO'
g.write('${typ}_${it.name}(') name := it.name.replace('.', '__')
g.write('${typ}_${name}(')
g.expr(it.expr) g.expr(it.expr)
if it.args.len > 0 { if it.args.len > 0 {
g.write(', ') g.write(', ')

View File

@ -16,13 +16,14 @@ fn test_c_files() {
vroot := filepath.dir(vexe) vroot := filepath.dir(vexe)
term_ok := term.ok_message('OK') term_ok := term.ok_message('OK')
term_fail := term.fail_message('FAIL') term_fail := term.fail_message('FAIL')
for i in 1..(nr_tests + 1) { for i in 1 .. (nr_tests + 1) {
path := '$vroot/vlib/v/gen/tests/${i}.vv' path := '$vroot/vlib/v/gen/tests/${i}.vv'
mut ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { mut ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or {
panic(err) panic(err)
} }
ctext = ctext // unused warn ctext = ctext // unused warn
mut b := builder.new_builder(pref.Preferences{}) mut b := builder.new_builder(pref.Preferences{})
b.module_search_paths = ['$vroot/vlib/v/gen/tests/']
res := b.gen_c([path]) res := b.gen_c([path])
if compare_texts(res, ctext) { if compare_texts(res, ctext) {
eprintln('${term_ok} ${i}') eprintln('${term_ok} ${i}')

View File

@ -9,6 +9,7 @@ void ensure_cap(int required, int cap);
void println(string s); void println(string s);
void matches(); void matches();
void end(); void end();
void localmod__pub_foo();
int pi = 3; int pi = 3;
int pi2 = pi; int pi2 = pi;
@ -33,6 +34,7 @@ int main() {
int ak = 10; int ak = 10;
int mypi = pi; int mypi = pi;
Color color = Color_red; Color color = Color_red;
localmod__pub_foo();
return 0; return 0;
} }
@ -141,3 +143,7 @@ void end() {
bool x = i != -1 && key == 10; bool x = i != -1 && key == 10;
int e = 2 + 3 * 4; int e = 2 + 3 * 4;
} }
void localmod__pub_foo() {
int a = 10;
}

View File

@ -1,6 +1,8 @@
//import moda //import moda
//import modb as mb //import modb as mb
import localmod
const ( const (
pi = 3 pi = 3
pi2 = pi pi2 = pi
@ -39,6 +41,7 @@ fn main() {
ak := 10 ak := 10
mypi := pi mypi := pi
color := Color.red color := Color.red
localmod.pub_foo()
} }
/* /*
user := User{} user := User{}

View File

@ -0,0 +1,5 @@
module localmod
pub fn pub_foo() {
a := 10
}