refactor: 大規模なモジュールを分割し、コードの構造を改善

runner, mir/builder, backend/llvm の各モジュールが肥大化していたため、責務ごとにファイルを分割し、見通しを改善するリファクタリングを実施。

### `runner`
- `mod.rs` に集中していたロジックを、以下のモジュールに分割:
    - `tasks.rs`: `nyash.toml` のタスク実行処理
    - `build.rs`: AOTビルドパイプラインの実装
    - `pipeline.rs`: `using` の解決など、パイプライン中のユーティリティ
    - `demos.rs`: デモの実行処理

### `mir/builder`
- `if/else` 文のPHIノード生成ロジックを `stmts.rs` から `phi.rs` へ切り出し。
- `utils.rs` にあったPHI関連のヘルパーも `phi.rs` に集約。
- ASTから自由変数を収集するロジックを `vars.rs` へ切り出し。

### `backend/llvm/compiler/codegen`
- 巨大だった `lower_one_function` 関数を、`function.rs` モジュールとして分離。
- `sanitize_symbol` などのヘルパー関数を `utils.rs` へ移動。
This commit is contained in:
Selfhosting Dev
2025-09-16 01:54:00 +09:00
parent 63717cf590
commit ab76cd35fa
13 changed files with 878 additions and 346 deletions

74
src/mir/builder/vars.rs Normal file
View File

@ -0,0 +1,74 @@
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.
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);
}
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::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::PeekExpr { 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); }
}
_ => {}
}
}