Refactor Binder initialization and root binding

The `Binder` and `FunctionCompiler` initialization has been refactored
to accept initial scopes and slot counts. This allows for more flexible
management of the compiler's state, particularly for the root scope.

The `bind_root` function now returns the final scopes and slot count of
the root function compiler, enabling the `Environment` to update its
state with these new bindings.

The global variable handling has been integrated into the scope
management, removing the direct reliance on a shared `HashMap` for
globals. This promotes a more consistent approach to symbol resolution.
This commit is contained in:
Michael Schimmel
2026-03-12 14:40:13 +01:00
parent 08b5bba2c4
commit a220815bd6
3 changed files with 79 additions and 91 deletions
+45 -72
View File
@@ -37,11 +37,15 @@ struct FunctionCompiler {
}
impl FunctionCompiler {
fn new(identity: Identity) -> Self {
fn new(identity: Identity, initial_scopes: Vec<CompilerScope>, initial_slot_count: u32) -> Self {
Self {
identity,
scopes: vec![CompilerScope::new()],
slot_count: 0,
scopes: if initial_scopes.is_empty() {
vec![CompilerScope::new()]
} else {
initial_scopes
},
slot_count: initial_slot_count,
upvalues: Vec::new(),
}
}
@@ -51,7 +55,9 @@ impl FunctionCompiler {
}
fn pop_scope(&mut self) {
self.scopes.pop();
if self.scopes.len() > 1 {
self.scopes.pop();
}
}
fn define_variable(&mut self, name: &Symbol, identity: Identity) -> Result<Address, String> {
@@ -104,19 +110,18 @@ pub enum ExprContext {
pub struct Binder {
functions: Vec<FunctionCompiler>,
globals: Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
capture_map: HashMap<Identity, std::collections::HashSet<Identity>>,
fixed_scope_idx: i32,
}
impl Binder {
pub fn new(
globals: Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32,
fixed_scope_idx: i32,
) -> Self {
let mut binder = Self {
functions: Vec::new(),
globals,
capture_map: HashMap::new(),
fixed_scope_idx,
};
@@ -125,17 +130,20 @@ impl Binder {
line: 0,
col: 0,
}),
initial_scopes,
initial_slot_count,
));
binder
}
pub fn bind_root(
globals: Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
initial_scopes: Vec<CompilerScope>,
initial_slot_count: u32,
fixed_scope_idx: i32,
node: &Node<UntypedKind>,
diagnostics: &mut Diagnostics,
) -> Result<(BoundNode, HashMap<Identity, Vec<Identity>>), String> {
let mut binder = Self::new(globals, fixed_scope_idx);
) -> Result<(BoundNode, HashMap<Identity, Vec<Identity>>, Vec<CompilerScope>, u32), String> {
let mut binder = Self::new(initial_scopes, initial_slot_count, fixed_scope_idx);
let bound = binder.bind(node, ExprContext::Expression, diagnostics);
let final_captures = binder
@@ -144,7 +152,13 @@ impl Binder {
.map(|(k, v)| (k, v.into_iter().collect()))
.collect();
Ok((bound, final_captures))
let root_compiler = binder.functions.pop().unwrap();
Ok((
bound,
final_captures,
root_compiler.scopes,
root_compiler.slot_count,
))
}
fn declare_variable(
@@ -168,36 +182,6 @@ impl Binder {
);
return None;
}
if current_scope_idx == 0 {
let mut globals_map = self.globals.borrow_mut();
let addr = if let Some((idx, existing_id)) = globals_map.get(name) {
if *existing_id != identity {
diag.push_error(
format!("Variable '{}' is already defined in global scope.", name.name),
None,
);
return None;
}
Address::Global(*idx)
} else {
let idx = GlobalIdx(globals_map.len() as u32);
globals_map.insert(name.clone(), (idx, identity.clone()));
Address::Global(idx)
};
let current_scope = self.functions[0].scopes.last_mut().unwrap();
current_scope.locals.insert(
name.clone(),
LocalInfo {
addr,
identity,
_ty: StaticType::Any,
purity: Purity::Impure,
},
);
return Some(addr);
}
}
let current_fn = self.functions.last_mut().unwrap();
@@ -359,7 +343,7 @@ impl Binder {
UntypedKind::Lambda { params, body } => {
let identity = node.identity.clone();
self.functions
.push(FunctionCompiler::new(identity.clone()));
.push(FunctionCompiler::new(identity.clone(), vec![], 0));
let params_bound = self.bind_pattern(params, DeclarationKind::Parameter, diag);
let body_bound = self.bind(body, ExprContext::Expression, diag);
@@ -540,30 +524,22 @@ impl Binder {
) -> Option<Address> {
let current_fn_idx = self.functions.len() - 1;
// 1. Try local in current function
if let Some((info, _)) = self.functions[current_fn_idx].resolve_local(sym) {
return Some(info.addr);
}
// 2. Try enclosing scopes (capture chain)
for i in (0..current_fn_idx).rev() {
if let Some((info, scope_idx)) = self.functions[i].resolve_local(sym) {
let is_frozen_root = i == 0 && (scope_idx as i32) <= self.fixed_scope_idx;
if let Some((info, _)) = self.functions[i].resolve_local(sym) {
// If the resolved address is already Global, we don't need to capture it as an upvalue
if let Address::Global(_) = info.addr {
return Some(info.addr);
}
let mut addr = info.addr;
if is_frozen_root {
if let Address::Local(slot) = addr {
addr = Address::Global(GlobalIdx(slot.0));
}
}
if let Address::Global(_) = addr {
return Some(addr);
}
// Record the capture for each lambda level in between
for k in (i + 1)..=current_fn_idx {
let lambda_id = self.functions[k].identity.clone();
self.capture_map
@@ -576,18 +552,19 @@ impl Binder {
}
}
let globals = self.globals.borrow();
if let Some((idx, _)) = globals.get(sym) {
return Some(Address::Global(*idx));
}
// 3. Global Fallback (search in root level with context removed)
if sym.context.is_some() {
let fallback_sym = Symbol {
name: sym.name.clone(),
context: None,
};
if let Some((idx, _)) = globals.get(&fallback_sym) {
return Some(Address::Global(*idx));
// Search again in all functions, primarily we care about Root Scopes
for i in (0..=current_fn_idx).rev() {
if let Some((info, _)) = self.functions[i].resolve_local(&fallback_sym) {
if let Address::Global(_) = info.addr {
return Some(info.addr);
}
}
}
}
@@ -705,9 +682,8 @@ mod tests {
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let globals = Rc::new(RefCell::new(HashMap::new()));
let mut diagnostics = Diagnostics::new();
let (bound, captures) = Binder::bind_root(globals, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -735,9 +711,8 @@ mod tests {
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let globals = Rc::new(RefCell::new(HashMap::new()));
let mut diagnostics = Diagnostics::new();
let (bound, captures) = Binder::bind_root(globals, 0, &untyped, &mut diagnostics).unwrap();
let (bound, captures, _scopes, _slots) = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics).unwrap();
if let BoundKind::Lambda { body, .. } = &bound.kind {
if let BoundKind::Block { exprs } = &body.kind {
@@ -765,9 +740,8 @@ mod tests {
let mut parser = Parser::new(source);
let untyped = parser.parse_expression();
let globals = Rc::new(RefCell::new(HashMap::new()));
let mut diagnostics = Diagnostics::new();
let _ = Binder::bind_root(globals, 0, &untyped, &mut diagnostics);
let _ = Binder::bind_root(vec![], 0, 0, &untyped, &mut diagnostics);
assert!(diagnostics.has_errors());
assert!(diagnostics.items.iter().any(|i| i.message.contains("already defined")));
@@ -775,17 +749,16 @@ mod tests {
#[test]
fn test_repro_global_redefinition() {
let globals = Rc::new(RefCell::new(HashMap::new()));
let source1 = "(def x 1) 1";
let untyped1 = Parser::new(source1).parse_expression();
let mut diagnostics = Diagnostics::new();
assert!(Binder::bind_root(globals.clone(), 0, &untyped1, &mut diagnostics).is_ok());
let (_, _, scopes1, slots1) = Binder::bind_root(vec![], 0, -1, &untyped1, &mut diagnostics).unwrap();
let source2 = "(def x 2) 2";
let untyped2 = Parser::new(source2).parse_expression();
let mut diagnostics2 = Diagnostics::new();
let _ = Binder::bind_root(globals.clone(), 0, &untyped2, &mut diagnostics2);
// Here we simulate frozen scope by passing fixed_scope_idx = 0
let _ = Binder::bind_root(scopes1, slots1, 0, &untyped2, &mut diagnostics2);
assert!(diagnostics2.has_errors());
assert!(diagnostics2.items.iter().any(|i| i.message.contains("frozen/immutable")));