Add block scoping and statement/expression distinction
Implement strict block scoping and distinguish between statements and expressions. This change introduces hierarchical scopes for functions and blocks, enforces that `def` is a statement with no return value, and prevents statements from appearing in expression positions or as the last element of a block. A new example `design-flaw.myc` is added to demonstrate the uninitialized variable issue solved by these changes.
This commit is contained in:
+49
-32
@@ -21,40 +21,14 @@ struct LocalInfo {
|
||||
#[derive(Debug, Clone)]
|
||||
struct CompilerScope {
|
||||
locals: HashMap<Symbol, LocalInfo>,
|
||||
slot_count: u32,
|
||||
}
|
||||
|
||||
impl CompilerScope {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
locals: HashMap::new(),
|
||||
slot_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn define(&mut self, sym: &Symbol, identity: Identity) -> Result<LocalSlot, String> {
|
||||
if self.locals.contains_key(sym) {
|
||||
return Err(format!(
|
||||
"Variable '{}' is already defined in this scope level.",
|
||||
sym.name
|
||||
));
|
||||
}
|
||||
let slot = LocalSlot(self.slot_count);
|
||||
self.locals.insert(
|
||||
sym.clone(),
|
||||
LocalInfo {
|
||||
slot,
|
||||
identity,
|
||||
_ty: StaticType::Any,
|
||||
},
|
||||
);
|
||||
self.slot_count += 1;
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
fn resolve(&self, sym: &Symbol) -> Option<LocalInfo> {
|
||||
self.locals.get(sym).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -65,7 +39,8 @@ enum ScopeKind {
|
||||
|
||||
struct FunctionCompiler {
|
||||
identity: Identity,
|
||||
scope: CompilerScope,
|
||||
scopes: Vec<CompilerScope>,
|
||||
slot_count: u32,
|
||||
upvalues: Vec<Address>,
|
||||
kind: ScopeKind,
|
||||
}
|
||||
@@ -74,12 +49,21 @@ impl FunctionCompiler {
|
||||
fn new(kind: ScopeKind, identity: Identity) -> Self {
|
||||
Self {
|
||||
identity,
|
||||
scope: CompilerScope::new(),
|
||||
scopes: vec![CompilerScope::new()],
|
||||
slot_count: 0,
|
||||
upvalues: Vec::new(),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
fn push_scope(&mut self) {
|
||||
self.scopes.push(CompilerScope::new());
|
||||
}
|
||||
|
||||
fn pop_scope(&mut self) {
|
||||
self.scopes.pop();
|
||||
}
|
||||
|
||||
fn define_variable(
|
||||
&mut self,
|
||||
name: &Symbol,
|
||||
@@ -98,14 +82,39 @@ impl FunctionCompiler {
|
||||
let idx = GlobalIdx(globals_map.len() as u32);
|
||||
globals_map.insert(name.clone(), (idx, identity));
|
||||
Ok(Address::Global(idx))
|
||||
}
|
||||
}
|
||||
ScopeKind::Local => {
|
||||
let slot = self.scope.define(name, identity)?;
|
||||
let current_scope = self.scopes.last_mut().unwrap();
|
||||
if current_scope.locals.contains_key(name) {
|
||||
return Err(format!(
|
||||
"Variable '{}' is already defined in this scope level.",
|
||||
name.name
|
||||
));
|
||||
}
|
||||
let slot = LocalSlot(self.slot_count);
|
||||
current_scope.locals.insert(
|
||||
name.clone(),
|
||||
LocalInfo {
|
||||
slot,
|
||||
identity,
|
||||
_ty: StaticType::Any,
|
||||
},
|
||||
);
|
||||
self.slot_count += 1;
|
||||
Ok(Address::Local(slot))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_local(&self, sym: &Symbol) -> Option<LocalInfo> {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
if let Some(info) = scope.locals.get(sym) {
|
||||
return Some(info.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn add_upvalue(&mut self, addr: Address) -> UpvalueIdx {
|
||||
if let Some(idx) = self.upvalues.iter().position(|&a| a == addr) {
|
||||
return UpvalueIdx(idx as u32);
|
||||
@@ -207,10 +216,16 @@ impl Binder {
|
||||
else_br,
|
||||
} => {
|
||||
let cond = self.bind(cond, diag);
|
||||
|
||||
self.functions.last_mut().unwrap().push_scope();
|
||||
let then_br = self.bind(then_br, diag);
|
||||
self.functions.last_mut().unwrap().pop_scope();
|
||||
|
||||
let mut else_br_bound = None;
|
||||
if let Some(e) = else_br {
|
||||
self.functions.last_mut().unwrap().push_scope();
|
||||
else_br_bound = Some(Rc::new(self.bind(e, diag)));
|
||||
self.functions.last_mut().unwrap().pop_scope();
|
||||
}
|
||||
|
||||
self.make_node(
|
||||
@@ -384,10 +399,12 @@ impl Binder {
|
||||
}
|
||||
|
||||
UntypedKind::Block { exprs } => {
|
||||
self.functions.last_mut().unwrap().push_scope();
|
||||
let mut bound_exprs = Vec::new();
|
||||
for expr in exprs {
|
||||
bound_exprs.push(Rc::new(self.bind(expr, diag)));
|
||||
}
|
||||
self.functions.last_mut().unwrap().pop_scope();
|
||||
self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Block { exprs: bound_exprs },
|
||||
@@ -485,13 +502,13 @@ impl Binder {
|
||||
let current_fn_idx = self.functions.len() - 1;
|
||||
|
||||
// 1. Try local in current function
|
||||
if let Some(info) = self.functions[current_fn_idx].scope.resolve(sym) {
|
||||
if let Some(info) = self.functions[current_fn_idx].resolve_local(sym) {
|
||||
return Some(Address::Local(info.slot));
|
||||
}
|
||||
|
||||
// 2. Try enclosing scopes (capture chain)
|
||||
for i in (0..current_fn_idx).rev() {
|
||||
if let Some(info) = self.functions[i].scope.resolve(sym) {
|
||||
if let Some(info) = self.functions[i].resolve_local(sym) {
|
||||
let mut addr = Address::Local(info.slot);
|
||||
|
||||
// Record the capture for each lambda level in between
|
||||
|
||||
Reference in New Issue
Block a user