]> rethought.computer Git - sorel-lang.git/commitdiff
imports are now direct from specific module, so there can be name collisions keep/fc2ee9c7b6a850ac616c3bafdf1b7a0e7a08b06b
authorBryan English <bryan@rethought.computer>
Mon, 26 Jan 2026 19:45:30 +0000 (14:45 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:54 +0000 (04:08 +0000)
hylo-lang/hyloc/src/ir.rs

index 0860ced4f8a349dff9e867ddd9a899dfb98daa2c..179566df67527a3d796cc21f3c148fb0fb3e60d8 100644 (file)
@@ -20,6 +20,29 @@ struct IRModule {
     imports: Vec<Rc<IRModule>>,
     exports: Vec<String>,
     source_file: PathBuf,
+    number: usize,
+}
+
+impl IRModule {
+    fn get_label_for_call(&self, name: &String) -> String {
+        let mut found: Option<usize> = 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::<Vec<_>>());
 
+        let number = self.module_count;
+        self.module_count += 1;
 
         IRModule {
             text: text.into_iter().flatten().collect::<Vec<_>>(),
@@ -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<IRObject> {
     let dir = std::env::current_dir()?;
     let mut tree: ImportTree = Default::default();