🧹 refactor: box_methods.rs大掃除完全成功 - 8モジュールに機能分離

🏗️ アーキテクチャ大幅改善:
• 1822行巨大ファイル → 8つの論理的モジュールに完全分割
• 機能別責任分離でメンテナンス性向上
• ゼロ破壊的変更 - 既存機能すべて正常動作

📂 新モジュール構造:
• basic_methods.rs - StringBox/IntegerBox/BoolBox/FloatBox
• collection_methods.rs - ArrayBox/MapBox
• io_methods.rs - FileBox/ResultBox
• system_methods.rs - TimeBox/DateTimeBox/TimerBox/DebugBox
• math_methods.rs - MathBox/RandomBox
• async_methods.rs - FutureBox/ChannelBox
• web_methods.rs - WebDisplayBox/WebConsoleBox/WebCanvasBox(WASM)
• special_methods.rs - MethodBox/SoundBox

 コード品質向上:
• 可読性 - 機能別分離で理解容易
• 保守性 - 変更影響の局所化
• 拡張性 - 新機能追加が簡単
• テスト性 - 単体テスト作成容易

🎯 プロフェッショナルレベルのコードベース完成\!
Everything is Box哲学の美しい実装構造達成

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-09 16:12:14 +09:00
parent 5d4bae2402
commit 2c5fc374da
12 changed files with 2465 additions and 1714 deletions

View File

@ -0,0 +1,132 @@
/*!
* Async Methods Module
*
* Extracted from interpreter/box_methods.rs
* Contains asynchronous Box type method implementations:
*
* - execute_future_method (FutureBox)
* - execute_channel_method (ChannelBox)
*
* These methods handle asynchronous operations, futures, and
* communication channels in the Nyash interpreter.
*/
use super::*;
use crate::box_trait::StringBox;
use crate::channel_box::{ChannelBox, MessageBox};
impl NyashInterpreter {
/// FutureBoxのメソッド呼び出しを実行
///
/// 非同期計算の結果を管理するFutureBoxの基本操作を提供します。
///
/// サポートメソッド:
/// - get() -> 計算結果を取得 (ブロッキング)
/// - ready() -> 計算完了状態をチェック
/// - equals(other) -> 他のFutureBoxと比較
pub(super) fn execute_future_method(&mut self, future_box: &FutureBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"get" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("get() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(future_box.get())
}
"ready" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("ready() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(future_box.ready())
}
"equals" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("equals() expects 1 argument, got {}", arguments.len()),
});
}
let other = self.execute_expression(&arguments[0])?;
Ok(Box::new(future_box.equals(other.as_ref())))
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for FutureBox", method),
})
}
}
/// ChannelBoxのメソッド呼び出しを実行
///
/// 非同期通信チャンネルを管理するChannelBoxの操作を提供します。
/// プロセス間通信やイベント駆動プログラミングに使用されます。
///
/// サポートメソッド:
/// - sendMessage(content) -> メッセージを送信
/// - announce(content) -> ブロードキャスト送信
/// - toString() -> チャンネル情報を文字列化
/// - sender() -> 送信者情報を取得
/// - receiver() -> 受信者情報を取得
pub(super) fn execute_channel_method(&mut self, channel_box: &ChannelBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
// 引数を評価
let mut arg_values = Vec::new();
for arg in arguments {
arg_values.push(self.execute_expression(arg)?);
}
// メソッドを実行
match method {
"sendMessage" => {
if arg_values.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("sendMessage() expects 1 argument, got {}", arg_values.len()),
});
}
// 簡易実装:メッセージを作成して返す
let content = arg_values[0].to_string_box().value;
let msg = MessageBox::new(&channel_box.sender_name, &content);
Ok(Box::new(msg))
}
"announce" => {
if arg_values.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("announce() expects 1 argument, got {}", arg_values.len()),
});
}
let content = arg_values[0].to_string_box().value;
Ok(Box::new(StringBox::new(&format!("Broadcast from {}: {}", channel_box.sender_name, content))))
}
"toString" => {
if !arg_values.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("toString() expects 0 arguments, got {}", arg_values.len()),
});
}
Ok(Box::new(channel_box.to_string_box()))
}
"sender" => {
if !arg_values.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("sender() expects 0 arguments, got {}", arg_values.len()),
});
}
Ok(channel_box.sender())
}
"receiver" => {
if !arg_values.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("receiver() expects 0 arguments, got {}", arg_values.len()),
});
}
Ok(channel_box.receiver())
}
_ => {
// その他のメソッドはChannelBoxに委譲
Ok(channel_box.invoke(method, arg_values))
}
}
}
}