Files
hakorune/src/mir/builder/exprs_lambda.rs

178 lines
6.3 KiB
Rust
Raw Normal View History

use super::ValueId;
use crate::ast::ASTNode;
impl super::MirBuilder {
// Lambda lowering to NewClosure
pub(super) fn build_lambda_expression(
&mut self,
params: Vec<String>,
body: Vec<ASTNode>,
) -> Result<ValueId, String> {
use std::collections::HashSet;
let mut used: HashSet<String> = HashSet::new();
let mut locals: HashSet<String> = HashSet::new();
for p in &params {
locals.insert(p.clone());
}
fn collect_vars(
ast: &ASTNode,
used: &mut std::collections::HashSet<String>,
locals: &mut std::collections::HashSet<String>,
) {
match ast {
ASTNode::Variable { name, .. } => {
if !locals.contains(name) {
used.insert(name.clone());
}
}
ASTNode::Assignment { target, value, .. } => {
collect_vars(target, used, locals);
collect_vars(value, used, locals);
}
ASTNode::BinaryOp { left, right, .. } => {
collect_vars(left, used, locals);
collect_vars(right, used, locals);
}
ASTNode::UnaryOp { operand, .. } => {
collect_vars(operand, used, locals);
}
ASTNode::MethodCall {
object, arguments, ..
} => {
collect_vars(object, used, locals);
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::FunctionCall { arguments, .. } => {
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::Call {
callee, arguments, ..
} => {
collect_vars(callee, used, locals);
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::FieldAccess { object, .. } => {
collect_vars(object, used, locals);
}
ASTNode::New { arguments, .. } => {
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::If {
condition,
then_body,
else_body,
..
} => {
collect_vars(condition, used, locals);
for st in then_body {
collect_vars(st, used, locals);
}
if let Some(eb) = else_body {
for st in eb {
collect_vars(st, used, locals);
}
}
}
ASTNode::Loop {
condition, body, ..
} => {
collect_vars(condition, used, locals);
for st in body {
collect_vars(st, used, locals);
}
}
ASTNode::TryCatch {
try_body,
catch_clauses,
finally_body,
..
} => {
for st in try_body {
collect_vars(st, used, locals);
}
for c in catch_clauses {
for st in &c.body {
collect_vars(st, used, locals);
}
}
if let Some(fb) = finally_body {
for st in fb {
collect_vars(st, used, locals);
}
}
}
ASTNode::Throw { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::Print { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::Return { value, .. } => {
if let Some(v) = value {
collect_vars(v, used, locals);
}
}
ASTNode::AwaitExpression { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::MatchExpr {
scrutinee,
arms,
else_expr,
..
} => {
collect_vars(scrutinee, used, locals);
for (_, e) in arms {
collect_vars(e, used, locals);
}
collect_vars(else_expr, used, locals);
}
ASTNode::Program { statements, .. } => {
for st in statements {
collect_vars(st, used, locals);
}
}
ASTNode::FunctionDeclaration { params, body, .. } => {
let mut inner = locals.clone();
for p in params {
inner.insert(p.clone());
}
for st in body {
collect_vars(st, used, &mut inner);
}
}
_ => {}
}
}
for st in body.iter() {
collect_vars(st, &mut used, &mut locals);
}
let mut captures: Vec<(String, ValueId)> = Vec::new();
for name in used.into_iter() {
refactor(mir): Remove VariableContext legacy fields (Phase 2-6/7) 完全移行→削除の安全順序(Option C)に従い、VariableContext の deprecated フィールドと sync helpers を完全削除。 ## Changes - Migrated all 66+ access sites to variable_ctx.variable_map - Removed 1 deprecated field (variable_map) from MirBuilder - Removed 2 sync helpers (sync_variable_ctx_to_legacy, sync_legacy_to_variable_ctx) - Fixed BoxCompilationContext.variable_map references (kept as-is, different scope) - Fixed ExitBindingBuilder.variable_map references (kept as-is, local field) - Updated observer.rs to use variable_map() accessor method ## JoinIR Integration Verified - CarrierInfo::from_variable_map() works correctly - ExitLine contract maintained (Phase 132-135) - NYASH_TRACE_VARMAP debug support preserved - Pattern 1-5 lowering all functional ## Tests - cargo test --release --lib: 1033 passed, 0 failed - phase135_trim_mir_verify.sh: PASS (MIR SSA/ValueId OK) - cargo build --release: SUCCESS - Deprecation warnings: reduced (86 remaining, from other contexts) ## Statistics - 27 files changed - 146 insertions(+), 174 deletions(-) - Net: -28 lines Phase 2 Progress: 6/7 contexts complete (86%) - ✅ MetadataContext - ✅ CoreContext - ✅ TypeContext - ✅ ScopeContext - ✅ BindingContext - ✅ VariableContext (this commit) - ⏳ CompilationContext (Phase 2-7 next) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 03:48:44 +09:00
if let Some(&vid) = self.variable_ctx.variable_map.get(&name) {
captures.push((name, vid));
}
}
refactor(mir): Remove VariableContext legacy fields (Phase 2-6/7) 完全移行→削除の安全順序(Option C)に従い、VariableContext の deprecated フィールドと sync helpers を完全削除。 ## Changes - Migrated all 66+ access sites to variable_ctx.variable_map - Removed 1 deprecated field (variable_map) from MirBuilder - Removed 2 sync helpers (sync_variable_ctx_to_legacy, sync_legacy_to_variable_ctx) - Fixed BoxCompilationContext.variable_map references (kept as-is, different scope) - Fixed ExitBindingBuilder.variable_map references (kept as-is, local field) - Updated observer.rs to use variable_map() accessor method ## JoinIR Integration Verified - CarrierInfo::from_variable_map() works correctly - ExitLine contract maintained (Phase 132-135) - NYASH_TRACE_VARMAP debug support preserved - Pattern 1-5 lowering all functional ## Tests - cargo test --release --lib: 1033 passed, 0 failed - phase135_trim_mir_verify.sh: PASS (MIR SSA/ValueId OK) - cargo build --release: SUCCESS - Deprecation warnings: reduced (86 remaining, from other contexts) ## Statistics - 27 files changed - 146 insertions(+), 174 deletions(-) - Net: -28 lines Phase 2 Progress: 6/7 contexts complete (86%) - ✅ MetadataContext - ✅ CoreContext - ✅ TypeContext - ✅ ScopeContext - ✅ BindingContext - ✅ VariableContext (this commit) - ⏳ CompilationContext (Phase 2-7 next) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-16 03:48:44 +09:00
let me = self.variable_ctx.variable_map.get("me").copied();
let dst = self.next_value_id();
self.emit_instruction(super::MirInstruction::NewClosure {
dst,
params: params.clone(),
body: body.clone(),
captures,
me,
})?;
self.type_ctx.value_types
.insert(dst, crate::mir::MirType::Box("FunctionBox".to_string()));
Ok(dst)
}
}