feat: 大規模リファクタリング - SRP原則に基づくモジュール分割

## MIR builder_calls.rs リファクタリング
- 879行 → 629行 (28%削減) + 7専門モジュール
- calls/ ディレクトリに機能別分割:
  - call_target.rs: CallTarget型定義
  - method_resolution.rs: メソッド解決ロジック
  - extern_calls.rs: 外部呼び出し処理
  - special_handlers.rs: 特殊ハンドラー
  - function_lowering.rs: 関数変換ユーティリティ
  - call_unified.rs: 統一Call実装
  - mod.rs: モジュール統合

## Parser statements.rs リファクタリング
- 723行 → 8専門モジュール
- statements/ ディレクトリに機能別分割:
  - control_flow.rs: if/loop/break/continue/return
  - declarations.rs: 宣言系ディスパッチャー
  - exceptions.rs: try/throw/catch/cleanup
  - helpers.rs: ヘルパー関数
  - io_async.rs: print/nowait
  - modules.rs: import/using/from
  - variables.rs: local/outbox/assignments
  - mod.rs: 統合モジュール

## 効果
 単一責任原則(SRP)の達成
 保守性・再利用性の向上
 ChatGPT5 Pro設計の型安全Call解決システム実装
 スモークテスト通過確認済み

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-25 09:01:55 +09:00
parent 26d181cac3
commit 2f306dd6a5
23 changed files with 1819 additions and 1616 deletions

View File

@ -0,0 +1,114 @@
/*!
* Method Resolution System
*
* Type-safe function and method resolution at compile-time
* ChatGPT5 Pro design for preventing runtime string-based resolution
*/
use crate::mir::{Callee, ValueId};
use std::collections::HashMap;
/// Resolve function call target to type-safe Callee
/// Implements the core logic of compile-time function resolution
pub fn resolve_call_target(
name: &str,
current_static_box: &Option<String>,
variable_map: &HashMap<String, ValueId>,
) -> Result<Callee, String> {
// 1. Check for built-in/global functions first
if is_builtin_function(name) {
return Ok(Callee::Global(name.to_string()));
}
// 2. Check for static box method in current context
if let Some(box_name) = current_static_box {
if has_method(box_name, name) {
// Warn about potential self-recursion
if is_commonly_shadowed_method(name) {
eprintln!("{}", generate_self_recursion_warning(box_name, name));
}
return Ok(Callee::Method {
box_name: box_name.clone(),
method: name.to_string(),
receiver: None, // Static method call
});
}
}
// 3. Check for local variable containing function value
if let Some(&value_id) = variable_map.get(name) {
return Ok(Callee::Value(value_id));
}
// 4. Check for external/host functions
if is_extern_function(name) {
return Ok(Callee::Extern(name.to_string()));
}
// 5. Resolution failed - this prevents runtime string-based resolution
Err(format!("Unresolved function: '{}'. {}", name, suggest_resolution(name)))
}
/// Check if function name is a built-in global function
pub fn is_builtin_function(name: &str) -> bool {
matches!(name,
"print" | "error" | "panic" | "exit" | "now" |
"gc_collect" | "gc_stats" |
// Math functions (handled specially)
"sin" | "cos" | "abs" | "min" | "max"
)
}
/// Check if function name is an external/host function
pub fn is_extern_function(name: &str) -> bool {
name.starts_with("nyash.") ||
name.starts_with("env.") ||
name.starts_with("system.")
}
/// Check if method is commonly shadowed (for warning generation)
pub fn is_commonly_shadowed_method(name: &str) -> bool {
matches!(name, "print" | "log" | "error" | "toString")
}
/// Generate warning about potential self-recursion
pub fn generate_self_recursion_warning(box_name: &str, method: &str) -> String {
format!(
"[Warning] Calling '{}' in static box '{}' context. \
This resolves to '{}.{}' which may cause self-recursion if called from within the same method.",
method, box_name, box_name, method
)
}
/// Suggest resolution for unresolved function
pub fn suggest_resolution(name: &str) -> String {
match name {
n if n.starts_with("console") => {
"Did you mean 'env.console.log' or 'print'?".to_string()
}
"log" | "println" => {
"Did you mean 'print' or 'env.console.log'?".to_string()
}
n if n.contains('.') => {
"Qualified names should use 'env.' prefix for external calls.".to_string()
}
_ => {
"Check function name or ensure it's in scope.".to_string()
}
}
}
/// Check if current static box has the specified method
/// TODO: Replace with proper method registry lookup
pub fn has_method(box_name: &str, method: &str) -> bool {
match box_name {
"ConsoleStd" => matches!(method, "print" | "println" | "log"),
"StringBox" => matches!(method, "upper" | "lower" | "length" | "concat" | "slice"),
"IntegerBox" => matches!(method, "add" | "sub" | "mul" | "div"),
"ArrayBox" => matches!(method, "push" | "pop" | "get" | "set" | "length"),
"MapBox" => matches!(method, "get" | "set" | "has" | "delete"),
"MathBox" => matches!(method, "sin" | "cos" | "abs" | "min" | "max"),
_ => false, // Conservative: assume no method unless explicitly known
}
}