From 51ad71ca38c5c7a3c536326a3cc1aa40ae91a428 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Wed, 7 Jan 2026 23:17:08 -0500 Subject: [PATCH] relative imports, properly --- hylo-lang/examples/fib.hylo | 2 +- hylo-lang/hyloc/src/ir.rs | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/hylo-lang/examples/fib.hylo b/hylo-lang/examples/fib.hylo index 9057c6d..25f4205 100644 --- a/hylo-lang/examples/fib.hylo +++ b/hylo-lang/examples/fib.hylo @@ -1,4 +1,4 @@ -import "examples/put2.hylo" +import "./put2.hylo" : fib dup 1 > if diff --git a/hylo-lang/hyloc/src/ir.rs b/hylo-lang/hyloc/src/ir.rs index db95898..f1f5edf 100644 --- a/hylo-lang/hyloc/src/ir.rs +++ b/hylo-lang/hyloc/src/ir.rs @@ -9,16 +9,20 @@ macro_rules! push_num { ($num:ident) => { IR::StackPush(*$num as u64) } } -fn import(specifier: &str, imported: &mut HashSet, is_entrypoint: bool) -> Option { - // TODO paths relative to the files, not just the invocation - let path = PathBuf::from(specifier).canonicalize().unwrap(); +fn import(importer_dir: &PathBuf, specifier: &str, imported: &mut HashSet, is_entrypoint: bool) -> Option { + let mut path = PathBuf::from(specifier); + if path.is_relative() { + let mut new_path = importer_dir.clone(); + new_path.push(path); + path = new_path.canonicalize().unwrap(); + } if imported.contains(&path) { return None; } let contents = std::fs::read_to_string(&path).unwrap(); - Some(generate_internal(&Module::parse(tokenize(&contents), is_entrypoint), imported)) + Some(generate_internal(path, &Module::parse(tokenize(&contents), is_entrypoint), imported)) } fn collapse_module(mut module_w: ModuleWithImports) -> IRModule { @@ -53,17 +57,17 @@ fn collapse_module(mut module_w: ModuleWithImports) -> IRModule { pub fn compile(path: &str) -> IRModule { let mut imported = HashSet::new(); - let module = import(path, &mut imported, true).unwrap(); + let module = import(&std::env::current_dir().unwrap(), path, &mut imported, true).unwrap(); collapse_module(module) // TODO remove unused words } #[derive(Debug)] struct ModuleWithImports { module: Option, - imports: Option> + imports: Option>, } -fn generate_internal(module: &Module, imported: &mut HashSet) -> ModuleWithImports { +fn generate_internal(path: PathBuf, module: &Module, imported: &mut HashSet) -> ModuleWithImports { // Eventually these will end up being sections in assembly let mut text = vec![]; let mut data = vec![]; @@ -100,7 +104,7 @@ fn generate_internal(module: &Module, imported: &mut HashSet) -> Module }, Token::String(text) => { if last_was_import { - if let Some(module) = import(text, imported, false) { + if let Some(module) = import(&path.parent().unwrap().to_path_buf(), text, imported, false) { imports.push(module); } IR::ImportString // This will be elided later @@ -146,6 +150,6 @@ fn generate_internal(module: &Module, imported: &mut HashSet) -> Module text: text.into_iter().flatten().collect::>(), data, }), - imports: Some(imports) + imports: Some(imports), } } -- 2.43.0