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:
2026-03-31 17:11:57 +02:00
parent 90f9afa0fd
commit 8d14da82c5
2 changed files with 15 additions and 2 deletions
+7
View File
@@ -70,6 +70,13 @@ impl CapturePass {
.collect(), .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 { NodeKind::Tuple { elements } => NodeKind::Tuple {
elements: elements elements: elements
.into_iter() .into_iter()
+8 -2
View File
@@ -390,9 +390,15 @@ impl Environment {
} }
/// Expands macros in a syntax AST. /// 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> { fn expand(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<SyntaxNode> {
match self.get_expander().expand(syntax_ast) { let mut expander = self.get_expander();
Ok(ast) => Some(ast), match expander.expand(syntax_ast) {
Ok(ast) => {
*self.macro_registry.borrow_mut() = expander.into_registry();
Some(ast)
}
Err(e) => { Err(e) => {
diagnostics.push_error(e, None); diagnostics.push_error(e, None);
None None