Add purity to LocalInfo and initialize in binder

The `LocalInfo` struct now includes a `purity` field to track the purity
of local variables and function arguments. This is initialized to
`Purity::Impure` for local variables and arguments within functions and
when registering native functions. Additionally, the `Environment`
struct is updated to include `root_scopes` and `root_slot_count` to
support parallel mode compilation by populating the root scope with
initial function and native function information.
This commit is contained in:
Michael Schimmel
2026-03-12 14:07:27 +01:00
parent 3780674eb1
commit 08b5bba2c4
2 changed files with 43 additions and 6 deletions
+4 -1
View File
@@ -3,7 +3,7 @@ use crate::ast::compiler::bound_nodes::{
};
use crate::ast::diagnostics::Diagnostics;
use crate::ast::nodes::{Node, Symbol, UntypedKind};
use crate::ast::types::{Identity, StaticType};
use crate::ast::types::{Identity, StaticType, Purity};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
@@ -13,6 +13,7 @@ pub struct LocalInfo {
pub addr: Address,
pub identity: Identity,
pub _ty: StaticType,
pub purity: Purity,
}
#[derive(Debug, Clone)]
@@ -69,6 +70,7 @@ impl FunctionCompiler {
addr: Address::Local(slot),
identity,
_ty: StaticType::Any,
purity: Purity::Impure,
},
);
self.slot_count += 1;
@@ -191,6 +193,7 @@ impl Binder {
addr,
identity,
_ty: StaticType::Any,
purity: Purity::Impure,
},
);
return Some(addr);
+39 -5
View File
@@ -76,6 +76,8 @@ pub struct Environment {
pub global_purity: Rc<RefCell<HashMap<GlobalIdx, Purity>>>,
pub global_values: Rc<RefCell<Vec<Value>>>,
pub fixed_scope_idx: i32,
pub root_scopes: Rc<RefCell<Vec<crate::ast::compiler::binder::CompilerScope>>>,
pub root_slot_count: Rc<RefCell<u32>>,
pub function_registry: Rc<RefCell<GlobalFunctionRegistry>>,
pub typed_function_registry: Rc<RefCell<GlobalAnalyzedRegistry>>,
pub monomorph_cache: Rc<RefCell<MonoCache>>,
@@ -164,6 +166,8 @@ impl Environment {
global_purity: Rc::new(RefCell::new(HashMap::new())),
global_values: Rc::new(RefCell::new(Vec::new())),
fixed_scope_idx: -1,
root_scopes: Rc::new(RefCell::new(vec![crate::ast::compiler::binder::CompilerScope::new()])),
root_slot_count: Rc::new(RefCell::new(0)),
function_registry: Rc::new(RefCell::new(HashMap::new())),
typed_function_registry: Rc::new(RefCell::new(HashMap::new())),
monomorph_cache: Rc::new(RefCell::new(HashMap::new())),
@@ -504,10 +508,25 @@ impl Environment {
let idx = GlobalIdx(values.len() as u32);
let identity = NodeIdentity::new(SourceLocation { line: 0, col: 0 });
names.insert(Symbol::from(name), (idx, identity));
types.insert(idx, ty);
names.insert(Symbol::from(name), (idx, identity.clone()));
types.insert(idx, ty.clone());
purity.insert(idx, func.purity);
values.push(Value::Function(func));
values.push(Value::Function(func.clone()));
// Parallel mode: populate root_scopes[0]
let mut root_scopes = self.root_scopes.borrow_mut();
let mut slot_count = self.root_slot_count.borrow_mut();
let slot = crate::ast::compiler::bound_nodes::LocalSlot(*slot_count);
root_scopes[0].locals.insert(
Symbol::from(name),
crate::ast::compiler::binder::LocalInfo {
addr: Address::Local(slot),
identity,
_ty: ty,
purity: func.purity,
},
);
*slot_count += 1;
}
pub fn register_native_fn(
@@ -535,10 +554,25 @@ impl Environment {
let idx = GlobalIdx(values.len() as u32);
let identity = NodeIdentity::new(SourceLocation { line: 0, col: 0 });
names.insert(Symbol::from(name), (idx, identity));
types.insert(idx, ty);
names.insert(Symbol::from(name), (idx, identity.clone()));
types.insert(idx, ty.clone());
purity.insert(idx, Purity::Pure);
values.push(val);
// Parallel mode: populate root_scopes[0]
let mut root_scopes = self.root_scopes.borrow_mut();
let mut slot_count = self.root_slot_count.borrow_mut();
let slot = crate::ast::compiler::bound_nodes::LocalSlot(*slot_count);
root_scopes[0].locals.insert(
Symbol::from(name),
crate::ast::compiler::binder::LocalInfo {
addr: Address::Local(slot),
identity,
_ty: ty,
purity: Purity::Pure,
},
);
*slot_count += 1;
}