From fc2ee9c7b6a850ac616c3bafdf1b7a0e7a08b06b Mon Sep 17 00:00:00 2001 From: Bryan English Date: Mon, 26 Jan 2026 14:45:30 -0500 Subject: [PATCH] imports are now direct from specific module, so there can be name collisions --- hylo-lang/hyloc/src/ir.rs | 43 +++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/hylo-lang/hyloc/src/ir.rs b/hylo-lang/hyloc/src/ir.rs index 0860ced..179566d 100644 --- a/hylo-lang/hyloc/src/ir.rs +++ b/hylo-lang/hyloc/src/ir.rs @@ -20,6 +20,29 @@ struct IRModule { imports: Vec>, exports: Vec, source_file: PathBuf, + number: usize, +} + +impl IRModule { + fn get_label_for_call(&self, name: &String) -> String { + let mut found: Option = None; + for imported in &self.imports { + if imported.exports.contains(name) { + found = Some(imported.number); + // Don't break here, since the last one should win. + } + } + if let Some(found) = found { + format!("_m{}_{}", found, name) + } else { + // TODO check if it's even a word locally. If not, bail. + format!("_m{}_{}", self.number, name) + } + } + + fn get_label(&self, name: &String) -> String { + format!("_m{}_{}", self.number, name) + } } #[derive(Default)] @@ -142,6 +165,8 @@ impl ImportTree { result }).flatten().collect::>()); + let number = self.module_count; + self.module_count += 1; IRModule { text: text.into_iter().flatten().collect::>(), @@ -149,6 +174,7 @@ impl ImportTree { imports, exports, source_file: path, + number, } } @@ -163,8 +189,7 @@ impl ImportTree { let is_entrypoint = module.source_file == self.entrypoint.source_file; - let module_number = self.module_count; - self.module_count += 1; + let module_number = module.number; for string in &module.data { if let IR::StringDef(name, val) = string { @@ -182,20 +207,14 @@ impl ImportTree { IR::StackPushString(new_name) }, IR::Label(name) => { - if is_entrypoint || module.exports.contains(name) { + if is_entrypoint && name == "main" { instruction.clone() } else { - let new_name = format!("_m{}_{}", module_number, name); - IR::Label(new_name) + IR::Label(module.get_label(name)) } }, IR::Call(name) => { - if is_entrypoint || self.all_exports.contains(name) { - instruction.clone() - } else { - let new_name = format!("_m{}_{}", module_number, name); - IR::Label(new_name) - } + IR::Call(module.get_label_for_call(name)) }, _ => instruction.clone() }; @@ -206,8 +225,6 @@ impl ImportTree { } } - - pub fn compile(path: &str) -> Result { let dir = std::env::current_dir()?; let mut tree: ImportTree = Default::default(); -- 2.43.0