diff --git a/src/ast/compiler/macros.rs b/src/ast/compiler/macros.rs index 098d23f..1b02ab0 100644 --- a/src/ast/compiler/macros.rs +++ b/src/ast/compiler/macros.rs @@ -24,7 +24,7 @@ type MacroMap = HashMap, (Vec>, Node)>; /// A registry for macro declarations. #[derive(Clone)] pub struct MacroRegistry { - scopes: Vec, + scopes: Vec>, } impl Default for MacroRegistry { @@ -36,12 +36,12 @@ impl Default for MacroRegistry { impl MacroRegistry { pub fn new() -> Self { Self { - scopes: vec![HashMap::new()], + scopes: vec![Rc::new(HashMap::new())], } } pub fn push(&mut self) { - self.scopes.push(HashMap::new()); + self.scopes.push(Rc::new(HashMap::new())); } pub fn pop(&mut self) { @@ -51,7 +51,9 @@ impl MacroRegistry { } pub fn define(&mut self, name: Rc, params: Vec>, body: Node) { - if let Some(current) = self.scopes.last_mut() { + if let Some(current_rc) = self.scopes.last_mut() { + // Copy-on-Write: If the Rc is shared, clone the map before modifying. + let current = Rc::make_mut(current_rc); current.insert(name, (params, body)); } }