feat: 搭建闭包捕获基础设施

This commit is contained in:
爱特每羊队
2026-06-30 19:58:58 +08:00
parent caa50afbe6
commit b915f127ca
17 changed files with 660 additions and 12 deletions
+24 -1
View File
@@ -17,6 +17,7 @@ pub(crate) struct Binding {
pub(crate) reg: u8,
pub(crate) initialized: bool,
pub(crate) is_const: bool,
pub(crate) is_captured: bool,
}
pub struct SymbolTable {
@@ -84,6 +85,7 @@ impl SymbolTable {
reg,
initialized: false,
is_const: matches!(kind, VariableDeclarationKind::Const) || is_const,
is_captured: false,
},
);
Ok(())
@@ -101,13 +103,22 @@ impl SymbolTable {
Err(format!("Identifier '{name}' is not defined"))
}
pub fn lookup_any(&self, name: &str) -> Option<u8> {
pub(crate) fn lookup_any(&self, name: &str) -> Option<u8> {
self.scopes
.iter()
.rev()
.find_map(|scope| scope.bindings.get(name).map(|binding| binding.reg))
}
pub(crate) fn lookup_any_binding(&self, name: &str) -> Option<(&Binding, usize)> {
for (i, scope) in self.scopes.iter().enumerate().rev() {
if let Some(b) = scope.bindings.get(name) {
return Some((b, i));
}
}
None
}
pub fn lookup_or_global(&mut self, name: &str, reg_for_new: u8) -> u8 {
for scope in self.scopes.iter().rev() {
if let Some(b) = scope.bindings.get(name) {
@@ -120,6 +131,7 @@ impl SymbolTable {
reg: reg_for_new,
initialized: true,
is_const: false,
is_captured: false,
},
);
reg_for_new
@@ -137,6 +149,15 @@ impl SymbolTable {
false
}
pub fn lookup_is_captured(&self, name: &str) -> bool {
for scope in self.scopes.iter().rev() {
if let Some(b) = scope.bindings.get(name) {
return b.is_captured;
}
}
false
}
pub fn declare_initialized(
&mut self, name: &str, reg: u8, kind: VariableDeclarationKind, is_const: bool,
) -> Result<(), String> {
@@ -157,6 +178,7 @@ impl SymbolTable {
reg,
initialized: true,
is_const: matches!(kind, VariableDeclarationKind::Const) || is_const,
is_captured: false,
},
);
Ok(())
@@ -167,6 +189,7 @@ impl SymbolTable {
reg,
initialized: true,
is_const: false,
is_captured: false,
});
}