compiler: import modules relative to v.mod

This commit is contained in:
Delyan Angelov 2020-03-01 16:49:39 +02:00 committed by GitHub
parent 615a4b3452
commit 1066ec5cd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 133 additions and 10 deletions

View file

@ -0,0 +1,2 @@
/bin/main
/tests/submodule_test

View file

@ -0,0 +1,16 @@
This projects demonstrates how v.mod lookup can be used so that
a project/module can be as selfcontained as possible.
The programs under bin/ can find the modules mod1,
because the project has a 'v.mod' file, so v module lookup for
the programs under bin/ can still find the parent sibling folder
mod1/ through relation to the parent 'v.mod' file.
Note also that mod1/ also has its own 'v.mod' file.
This allows mod1 submodules to find and import themselves
in relation to it too.
Finally, there is a test/ folder, so you can put all your tests
in there, without cluttering your top level folder, or your module
folders if you so desire.

View file

@ -0,0 +1,12 @@
import mod1.submodule as m
fn test_mod1_can_still_be_found_through_parent_project_vmod(){
assert 1051 == m.f()
}
/*
NB: this main program is under bin/ , but it still
can find mod1, because the parent project has v.mod,
so v module lookup for this program will find mod1 through
relation to the parent v.mod file
*/

View file

@ -0,0 +1,14 @@
#!/usr/local/bin/v run
import mod1.submodule as m
println('This script is located inside: ' + resource_abs_path(''))
println('The result of calling m.f is: ' + m.f().str() )
/*
NB: this main program v script is under bin/ ,
but it *still* can find mod1, because the parent project has v.mod,
so v module lookup for this bin/main.vsh file will find mod1 through
relation to the parent ../v.mod file
*/

View file

@ -0,0 +1,5 @@
module mod1
pub fn f() int {
return 1
}

View file

@ -0,0 +1,5 @@
module mod11
pub fn f() int {
return 11
}

View file

@ -0,0 +1,5 @@
module mod12
pub fn f() int {
return 12
}

View file

@ -0,0 +1,5 @@
module mod13
pub fn f() int {
return 13
}

View file

@ -0,0 +1,7 @@
module mod14
import math
pub fn f() int {
return 14 + int(math.cos(0))
}

View file

@ -0,0 +1,16 @@
module submodule
/*
This submodule just imports its sibling submodules.
Note that they are NOT under 'submodule' itself,
but are in its parent mod1 , and mod1 has a 'v.mod' file.
*/
import mod11
import mod12
import mod13
import mod14
pub fn f() int {
return 1000 + mod11.f() + mod12.f() + mod13.f() + mod14.f()
}

View file

@ -0,0 +1,7 @@
#V Module#
Module {
name: 'mod1',
description: 'A module with several submodules.',
dependencies: []
}

View file

@ -0,0 +1,11 @@
import mod1
import mod1.submodule
fn test_mod1(){
assert 1 == mod1.f()
}
fn test_mod1_submodule_can_find_and_use_all_its_sibling_submodules(){
assert 1051 == submodule.f()
}

View file

@ -0,0 +1,7 @@
#V Project#
Module {
name: 'project_with_modules_having_submodules',
description: 'This project was created with `v create` to prevent regressions with the way V module import lookup works.',
dependencies: []
}