Browse Source

fix: clean up file resolvement

modules
Garrit Franke 3 years ago
parent
commit
aa4fd06ae5
  1. 9
      src/builder/mod.rs
  2. 8
      src/parser/rules.rs
  3. 1
      tests/importable_module/foo/bar.sb
  4. 3
      tests/importable_module/foo/baz/module.sb
  5. 2
      tests/imports.sb

9
src/builder/mod.rs

@ -84,10 +84,17 @@ impl Builder {
)?;
for import in &module.imports {
// Build module relative to the current file
let import_path = resolved_file_path
let mut import_path = resolved_file_path
.parent()
.unwrap()
.join(PathBuf::from(import));
if import_path.is_dir() {
import_path = import_path.join("module.sb");
} else if !import_path.ends_with(".sb") {
import_path.set_extension("sb");
}
self.build_module(import_path)?;
}
self.modules.push(module.clone());

8
src/parser/rules.rs

@ -157,13 +157,7 @@ impl Parser {
chars.next();
chars.next_back();
let import_path = if chars.as_str().ends_with(".sb") {
chars.collect()
} else {
format!("{}.sb", chars.collect::<String>())
};
Ok(import_path)
Ok(chars.collect())
}
fn parse_type(&mut self) -> Result<Type, String> {

1
tests/importable_module/foo/bar.sb

@ -1,3 +1,4 @@
import "baz"
fn nested_module() {
println("A deeply nested function was called!")

3
tests/importable_module/foo/baz/module.sb

@ -0,0 +1,3 @@
fn baz() {
println("Baz was called")
}

2
tests/imports.sb

@ -1,4 +1,4 @@
import "importable_module/module"
import "importable_module"
fn main() {
external_function()

Loading…
Cancel
Save