From 2d2540131905dcdaccfe3387c01ea90587ac1d53 Mon Sep 17 00:00:00 2001 From: Kai Schmidt Date: Sun, 6 Oct 2024 11:22:17 -0700 Subject: [PATCH] let imported modules be used as macros --- src/compile/mod.rs | 26 +++++++++++++++++++++++--- src/compile/modifier.rs | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/compile/mod.rs b/src/compile/mod.rs index 67ee125d0..1205f3876 100644 --- a/src/compile/mod.rs +++ b/src/compile/mod.rs @@ -1654,7 +1654,9 @@ code: } // Attempt to look up the identifier as a non-macro let as_non_macro = self.find_name(name.strip_suffix('!')?, skip_local)?; - if let BindingKind::Module(_) = self.asm.bindings[as_non_macro.index].kind { + if let BindingKind::Module(_) | BindingKind::Import(_) = + self.asm.bindings[as_non_macro.index].kind + { // Only allow it if it is a module Some(as_non_macro) } else { @@ -1883,7 +1885,21 @@ code: self.push_instr(Instr::Call(span)); } } - BindingKind::Import { .. } => self.add_error(span, "Cannot import module item here."), + BindingKind::Import(path) => { + if let Some(local) = self.imports.get(&path).and_then(|m| m.names.get("Call")) { + self.code_meta.global_references.remove(&span); + self.code_meta + .global_references + .insert(span.clone(), local.index); + self.global_index(local.index, span, call); + } else { + self.add_error( + span, + "Module cannot be called here as \ + it has no `Call` function.", + ); + } + } BindingKind::Module(m) => { if let Some(local) = m.names.get("Call").or_else(|| m.names.get("New")) { self.code_meta.global_references.remove(&span); @@ -1892,7 +1908,11 @@ code: .insert(span.clone(), local.index); self.global_index(local.index, span, call); } else { - self.add_error(span, "Cannot import module item here."); + self.add_error( + span, + "Module cannot be called here as \ + it has no `Call` or `New` function.", + ); } } BindingKind::IndexMacro(_) | BindingKind::CodeMacro(_) => { diff --git a/src/compile/modifier.rs b/src/compile/modifier.rs index ff111df95..f32b166f3 100644 --- a/src/compile/modifier.rs +++ b/src/compile/modifier.rs @@ -1587,6 +1587,7 @@ impl Compiler { } else if let Some(m) = (self.asm.bindings.get(local.index)).and_then(|binfo| match &binfo.kind { BindingKind::Module(m) => Some(m), + BindingKind::Import(path) => self.imports.get(path), _ => None, }) {