Refactor discover_globals to handle patterns

The `discover_globals` function has been refactored to better handle
pattern matching within definitions. It now uses a recursive helper
function `register_pattern` to correctly process identifiers and tuple
patterns. This ensures that all declared variables within a `Def` node
are registered with their corresponding scope and virtual ID.
Additionally, support for `Expansion` nodes has been added to ensure
that any nested global definitions within them are also discovered.
This commit is contained in:
2026-03-27 08:27:31 +01:00
parent 903c77a665
commit cb4162e420
+38 -18
View File
@@ -458,26 +458,43 @@ impl Environment {
fn discover_globals(&self, node: &SyntaxNode) { fn discover_globals(&self, node: &SyntaxNode) {
match &node.kind { match &node.kind {
SyntaxKind::Def { pattern, .. } => { SyntaxKind::Def { pattern, .. } => {
if let SyntaxKind::Identifier { symbol: sym, .. } = &pattern.kind { let mut root_scopes = self.root_scopes.borrow_mut();
let mut root_scopes = self.root_scopes.borrow_mut(); let last_idx = root_scopes.len() - 1;
let last_idx = root_scopes.len() - 1; let current_scope = &mut root_scopes[last_idx];
let current_scope = &mut root_scopes[last_idx];
if !current_scope.locals.contains_key(sym) { fn register_pattern(
let mut slot_count = self.root_slot_count.borrow_mut(); pattern: &SyntaxNode,
let slot = VirtualId(*slot_count); scope: &mut CompilerScope,
current_scope.locals.insert( slot_count: &mut u32,
sym.clone(), identity: &crate::ast::types::NodeIdentity,
LocalInfo { ) {
addr: Address::Local(slot), match &pattern.kind {
identity: node.identity.clone(), SyntaxKind::Identifier { symbol: sym, .. } => {
_ty: StaticType::Any, if !scope.locals.contains_key(sym) {
purity: Purity::Impure, let slot = VirtualId(*slot_count);
}, scope.locals.insert(
); sym.clone(),
*slot_count += 1; LocalInfo {
addr: Address::Local(slot),
identity: Rc::new(identity.clone()),
_ty: StaticType::Any,
purity: Purity::Impure,
},
);
*slot_count += 1;
}
}
SyntaxKind::Tuple { elements } => {
for el in elements {
register_pattern(el, scope, slot_count, identity);
}
}
_ => {}
} }
} }
let mut slot_count = self.root_slot_count.borrow_mut();
register_pattern(pattern, current_scope, &mut slot_count, &node.identity);
} }
SyntaxKind::MacroDecl { name, params, body } => { SyntaxKind::MacroDecl { name, params, body } => {
let mut registry = self.macro_registry.borrow_mut(); let mut registry = self.macro_registry.borrow_mut();
@@ -491,7 +508,7 @@ impl Environment {
_ => vec![], _ => vec![],
} }
} }
let p_names = extract_names(params); let p_names = extract_names(params);
registry.define(name.name.clone(), p_names, body.as_ref().clone()); registry.define(name.name.clone(), p_names, body.as_ref().clone());
} }
@@ -500,6 +517,9 @@ impl Environment {
self.discover_globals(expr); self.discover_globals(expr);
} }
} }
SyntaxKind::Expansion { expanded, .. } => {
self.discover_globals(expanded);
}
_ => {} _ => {}
} }
} }