From 38de6c98fce373e5f857cdf4ec0fa9b658661153 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 1 Mar 2020 21:56:07 +0100 Subject: [PATCH] cgen: test local modules --- vlib/v/builder/builder.v | 27 ++++++++++++++++++++++++++- vlib/v/builder/modules.v | 23 ----------------------- vlib/v/checker/checker.v | 2 +- vlib/v/gen/cgen.v | 11 +++++++---- vlib/v/gen/cgen_test.v | 3 ++- vlib/v/gen/tests/1.c | 6 ++++++ vlib/v/gen/tests/1.vv | 3 +++ vlib/v/gen/tests/localmod/localmod.v | 5 +++++ 8 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 vlib/v/gen/tests/localmod/localmod.v diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index a025b9328b..f5a82cc742 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -21,8 +21,8 @@ pub: 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: + module_search_paths []string parsed_files []ast.File } @@ -76,6 +76,8 @@ pub fn (b mut Builder) parse_imports() { 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 + // println('module_search_paths:') + // println(b.module_search_paths) panic('cannot import module "$mod" (not found)') } v_files := b.v_files_from_dir(import_path) @@ -171,3 +173,26 @@ pub fn (b &Builder) log(s string) { 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') +} diff --git a/vlib/v/builder/modules.v b/vlib/v/builder/modules.v index 0a83839589..d32ec3db36 100644 --- a/vlib/v/builder/modules.v +++ b/vlib/v/builder/modules.v @@ -4,26 +4,3 @@ import ( os 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') -} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 68ac4f0197..90cba0a0f3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -12,7 +12,7 @@ import ( ) const ( - max_nr_errors = 150 + max_nr_errors = 350 ) pub struct Checker { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c62c96c2d1..dd26400c0a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -85,8 +85,9 @@ fn (g mut Gen) stmt(node ast.Stmt) { } else { type_sym := g.table.get_type_symbol(it.typ) - g.write('$type_sym.name ${it.name}(') - g.definitions.write('$type_sym.name ${it.name}(') + name := it.name.replace('.', '__') + g.write('$type_sym.name ${name}(') + g.definitions.write('$type_sym.name ${name}(') } for i, arg in it.args { arg_type_sym := g.table.get_type_symbol(arg.typ) @@ -283,7 +284,8 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('}') } ast.CallExpr { - g.write('${it.name}(') + name := it.name.replace('.', '__') + g.write('${name}(') g.call_args(it.args) g.write(')') /* @@ -298,7 +300,8 @@ fn (g mut Gen) expr(node ast.Expr) { } ast.MethodCallExpr { typ := 'TODO' - g.write('${typ}_${it.name}(') + name := it.name.replace('.', '__') + g.write('${typ}_${name}(') g.expr(it.expr) if it.args.len > 0 { g.write(', ') diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 65df7b3d2f..dd65a4d1e0 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -16,13 +16,14 @@ fn test_c_files() { vroot := filepath.dir(vexe) term_ok := term.ok_message('OK') 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' mut ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { panic(err) } ctext = ctext // unused warn mut b := builder.new_builder(pref.Preferences{}) + b.module_search_paths = ['$vroot/vlib/v/gen/tests/'] res := b.gen_c([path]) if compare_texts(res, ctext) { eprintln('${term_ok} ${i}') diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 3a9880ff16..ddbb7ff212 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -9,6 +9,7 @@ void ensure_cap(int required, int cap); void println(string s); void matches(); void end(); +void localmod__pub_foo(); int pi = 3; int pi2 = pi; @@ -33,6 +34,7 @@ int main() { int ak = 10; int mypi = pi; Color color = Color_red; + localmod__pub_foo(); return 0; } @@ -141,3 +143,7 @@ void end() { bool x = i != -1 && key == 10; int e = 2 + 3 * 4; } + +void localmod__pub_foo() { + int a = 10; +} diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index d1a0b50772..c51e34b8b8 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -1,6 +1,8 @@ //import moda //import modb as mb +import localmod + const ( pi = 3 pi2 = pi @@ -39,6 +41,7 @@ fn main() { ak := 10 mypi := pi color := Color.red + localmod.pub_foo() } /* user := User{} diff --git a/vlib/v/gen/tests/localmod/localmod.v b/vlib/v/gen/tests/localmod/localmod.v new file mode 100644 index 0000000000..fd4cd5f56f --- /dev/null +++ b/vlib/v/gen/tests/localmod/localmod.v @@ -0,0 +1,5 @@ +module localmod + +pub fn pub_foo() { + a := 10 +}