Files
hakorune/src/mir/builder/vars.rs
nyash-codex c70e76ff57 feat(parser): Phase 152-A - Grouped assignment expression (箱化モジュール化)
Implement Stage-3 grouped assignment expression `(x = expr)` following
the 箱化モジュール化 (modular box) pattern established in Phase 133/134.

**Implementation**:
- AssignmentExprParser module (Rust: 183 lines)
  - src/parser/stage3/assignment_expr_parser.rs (+183 lines)
  - src/parser/stage3/mod.rs (+9 lines)
- AST node addition: GroupedAssignmentExpr
  - src/ast.rs (+7 lines)
  - src/ast/utils.rs (+9 lines)
- MIR lowering via 1-line delegation
  - src/mir/builder/exprs.rs (+5 lines)
  - src/mir/builder/vars.rs (+4 lines)
- Parser integration via 1-line delegation
  - src/parser/expr/primary.rs (+6 lines)
  - src/parser/mod.rs (+1 line)

**Test Results**: 3/3 PASS
- assignment_expr_simple.hako: RC 1 
- assignment_expr_shortcircuit.hako: RC 1 
- shortcircuit_and_phi_skip.hako: RC 1  (updated to use expression context)

**Stage-3 Gate**: No impact on Stage-2/legacy
- NYASH_FEATURES=stage3 required
- Pattern: '(' IDENT '=' expr ')'
- Value/type same as rhs, side effect assigns to lhs

**箱化モジュール化パターン**:
- Dedicated module for assignment expression parsing
- Clear responsibility separation
- 1-line delegation for integration
- Testability improvement
- Follows Phase 133/134-A/134-B pattern

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 13:32:58 +09:00

158 lines
4.9 KiB
Rust

use crate::ast::ASTNode;
use std::collections::HashSet;
/// Collect free variables used in `node` into `used`, excluding names present in `locals`.
/// `locals` is updated as new local declarations are encountered.
#[allow(dead_code)]
pub(super) fn collect_free_vars(
node: &ASTNode,
used: &mut HashSet<String>,
locals: &mut HashSet<String>,
) {
match node {
ASTNode::Variable { name, .. } => {
if name != "me" && name != "this" && !locals.contains(name) {
used.insert(name.clone());
}
}
ASTNode::Local { variables, .. } => {
for v in variables {
locals.insert(v.clone());
}
}
ASTNode::Assignment { target, value, .. } => {
collect_free_vars(target, used, locals);
collect_free_vars(value, used, locals);
}
// Phase 152-A: Grouped assignment expression
ASTNode::GroupedAssignmentExpr { rhs, .. } => {
collect_free_vars(rhs, used, locals);
}
ASTNode::BinaryOp { left, right, .. } => {
collect_free_vars(left, used, locals);
collect_free_vars(right, used, locals);
}
ASTNode::UnaryOp { operand, .. } => {
collect_free_vars(operand, used, locals);
}
ASTNode::MethodCall {
object, arguments, ..
} => {
collect_free_vars(object, used, locals);
for a in arguments {
collect_free_vars(a, used, locals);
}
}
ASTNode::FunctionCall { arguments, .. } => {
for a in arguments {
collect_free_vars(a, used, locals);
}
}
ASTNode::Call {
callee, arguments, ..
} => {
collect_free_vars(callee, used, locals);
for a in arguments {
collect_free_vars(a, used, locals);
}
}
ASTNode::FieldAccess { object, .. } => {
collect_free_vars(object, used, locals);
}
ASTNode::Index { target, index, .. } => {
collect_free_vars(target, used, locals);
collect_free_vars(index, used, locals);
}
ASTNode::New { arguments, .. } => {
for a in arguments {
collect_free_vars(a, used, locals);
}
}
ASTNode::If {
condition,
then_body,
else_body,
..
} => {
collect_free_vars(condition, used, locals);
for st in then_body {
collect_free_vars(st, used, locals);
}
if let Some(eb) = else_body {
for st in eb {
collect_free_vars(st, used, locals);
}
}
}
ASTNode::Loop {
condition, body, ..
} => {
collect_free_vars(condition, used, locals);
for st in body {
collect_free_vars(st, used, locals);
}
}
ASTNode::TryCatch {
try_body,
catch_clauses,
finally_body,
..
} => {
for st in try_body {
collect_free_vars(st, used, locals);
}
for c in catch_clauses {
for st in &c.body {
collect_free_vars(st, used, locals);
}
}
if let Some(fb) = finally_body {
for st in fb {
collect_free_vars(st, used, locals);
}
}
}
ASTNode::Throw { expression, .. } => {
collect_free_vars(expression, used, locals);
}
ASTNode::Print { expression, .. } => {
collect_free_vars(expression, used, locals);
}
ASTNode::Return { value, .. } => {
if let Some(v) = value {
collect_free_vars(v, used, locals);
}
}
ASTNode::AwaitExpression { expression, .. } => {
collect_free_vars(expression, used, locals);
}
ASTNode::MatchExpr {
scrutinee,
arms,
else_expr,
..
} => {
collect_free_vars(scrutinee, used, locals);
for (_, e) in arms {
collect_free_vars(e, used, locals);
}
collect_free_vars(else_expr, used, locals);
}
ASTNode::Program { statements, .. } => {
for st in statements {
collect_free_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_free_vars(st, used, &mut inner);
}
}
_ => {}
}
}