Persist macros after expansion
When expanding macros, the expander may encounter new macro declarations. These need to be stored back into the environment's macro registry so that they are available for subsequent compilation passes or other environments. Persist macros after expansion This commit modifies the `expand` method in `src/ast/environment.rs` to persist macro declarations encountered during expansion. When a macro is expanded, the `Expander` updates its internal registry. This change ensures that the updated registry is then stored back into the `Environment`'s `macro_registry`. This allows subsequent compilations or expansions within the same environment to correctly recognize and utilize these newly defined macros. Additionally, a `NodeKind::Program` case was added to the `CapturePass::transform` method in `src/ast/compiler/captures.rs`. This ensures that expressions within a program node are also recursively transformed, maintaining consistency in the AST processing pipeline.
This commit is contained in:
@@ -70,6 +70,13 @@ impl CapturePass {
|
||||
.collect(),
|
||||
},
|
||||
|
||||
NodeKind::Program { exprs } => NodeKind::Program {
|
||||
exprs: exprs
|
||||
.into_iter()
|
||||
.map(|e| Rc::new(Self::transform(e.as_ref().clone(), capture_map)))
|
||||
.collect(),
|
||||
},
|
||||
|
||||
NodeKind::Tuple { elements } => NodeKind::Tuple {
|
||||
elements: elements
|
||||
.into_iter()
|
||||
|
||||
@@ -390,9 +390,15 @@ impl Environment {
|
||||
}
|
||||
|
||||
/// Expands macros in a syntax AST.
|
||||
/// Any macro declarations encountered during expansion are persisted back
|
||||
/// into the environment's registry so subsequent compilations can use them.
|
||||
fn expand(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<SyntaxNode> {
|
||||
match self.get_expander().expand(syntax_ast) {
|
||||
Ok(ast) => Some(ast),
|
||||
let mut expander = self.get_expander();
|
||||
match expander.expand(syntax_ast) {
|
||||
Ok(ast) => {
|
||||
*self.macro_registry.borrow_mut() = expander.into_registry();
|
||||
Some(ast)
|
||||
}
|
||||
Err(e) => {
|
||||
diagnostics.push_error(e, None);
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user