🏗️ Runner モジュール分割とセルフホスティング基盤整備

## 主な変更点

### 🔧 src/runner/ のモジュール分割実装
- mod.rs(1358行)を機能別に分割:
  - pipeline.rs: 入力正規化、using解決、環境注入
  - pipe_io.rs: stdin/fileのJSON v0受理・整形
  - selfhost.rs: セルフホストEXE/VM/Pythonフォールバック
  - dispatch.rs: backend選択と実行、PyVM委譲
- 責務の明確化と保守性向上

### 🚀 セルフホスティング改善
- selfhost.rs: 過渡的なshimから本格実装へ移行開始
- Result出力の調和(PyVM経路でも"Result: N"出力)
- タイムアウト処理とプロセス管理の改善

### 🐛 テストスクリプトの暴走対策
- selfhost_stage2_smoke.sh: 36,783プロセス生成問題への対応
- プロセス管理とタイムアウト処理の見直し

## 技術的詳細
- common.rsのrun_file_legacy()として旧実装を保持
- 新しいモジュール構造で循環依存を回避
- Phase 15.3のセルフホスティング基盤を強化

次のステップ:各モジュールの実装を段階的に移行

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-16 00:01:31 +09:00
parent 94d95dfbcd
commit 63717cf590
7 changed files with 718 additions and 204 deletions

172
src/runner/dispatch.rs Normal file
View File

@ -0,0 +1,172 @@
/*!
* Runner dispatch helpers — execute MIR module and report result
*/
use super::*;
use crate::runner::json_v0_bridge;
use nyash_rust::parser::NyashParser;
use std::{fs, process};
impl NyashRunner {
/// Thin file dispatcher: select backend and delegate to mode executors
pub(crate) fn run_file(&self, filename: &str) {
// Selfhost pipeline (Ny -> JSON v0) behind env gate
if std::env::var("NYASH_USE_NY_COMPILER").ok().as_deref() == Some("1") {
if self.try_run_selfhost_pipeline(filename) {
return;
} else if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[ny-compiler] fallback to default path (MVP unavailable for this input)");
}
}
// Direct v0 bridge when requested via CLI/env
let use_ny_parser = self.config.parser_ny || std::env::var("NYASH_USE_NY_PARSER").ok().as_deref() == Some("1");
if use_ny_parser {
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
match json_v0_bridge::parse_source_v0_to_module(&code) {
Ok(module) => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🚀 Nyash MIR Interpreter - (parser=ny) Executing file: {} 🚀", filename);
}
self.execute_mir_module(&module);
return;
}
Err(e) => { eprintln!("❌ Direct bridge parse error: {}", e); process::exit(1); }
}
}
// AST dump mode
if self.config.dump_ast {
println!("🧠 Nyash AST Dump - Processing file: {}", filename);
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
let ast = match NyashParser::parse_from_string(&code) {
Ok(ast) => ast,
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
println!("{:#?}", ast);
return;
}
// MIR dump/verify
if self.config.dump_mir || self.config.verify_mir {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🚀 Nyash MIR Compiler - Processing file: {} 🚀", filename);
}
self.execute_mir_mode(filename);
return;
}
// WASM / AOT (feature-gated)
if self.config.compile_wasm {
#[cfg(feature = "wasm-backend")]
{ self.execute_wasm_mode(filename); return; }
#[cfg(not(feature = "wasm-backend"))]
{ eprintln!("❌ WASM backend not available. Please rebuild with: cargo build --features wasm-backend"); process::exit(1); }
}
if self.config.compile_native {
#[cfg(feature = "cranelift-jit")]
{ self.execute_aot_mode(filename); return; }
#[cfg(not(feature = "cranelift-jit"))]
{ eprintln!("❌ Native AOT compilation requires Cranelift. Please rebuild: cargo build --features cranelift-jit"); process::exit(1); }
}
// Backend selection
match self.config.backend.as_str() {
"mir" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🚀 Nyash MIR Interpreter - Executing file: {} 🚀", filename);
}
self.execute_mir_mode(filename);
}
#[cfg(feature = "cranelift-jit")]
"jit-direct" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("⚡ Nyash JIT-Direct Backend - Executing file: {}", filename);
}
#[cfg(feature = "cranelift-jit")]
{
crate::runner::modes::cranelift::execute_jit_direct_mode(self, filename);
}
#[cfg(not(feature = "cranelift-jit"))]
{
eprintln!("❌ Cranelift backend not available. Please rebuild with: cargo build --features cranelift-jit");
process::exit(1);
}
}
"llvm" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("⚡ Nyash LLVM Backend - Executing file: {}", filename);
}
self.execute_llvm_mode(filename);
}
_ => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🦀 Nyash Rust Implementation - Executing file: {} 🦀", filename);
if let Some(fuel) = self.config.debug_fuel {
println!("🔥 Debug fuel limit: {} iterations", fuel);
} else {
println!("🔥 Debug fuel limit: unlimited");
}
println!("====================================================");
}
self.execute_nyash_file(filename);
}
}
}
pub(crate) fn execute_mir_module(&self, module: &crate::mir::MirModule) {
use crate::backend::MirInterpreter;
use crate::box_trait::{IntegerBox, BoolBox, StringBox};
use crate::boxes::FloatBox;
use crate::mir::MirType;
let mut interp = MirInterpreter::new();
match interp.execute_module(module) {
Ok(result) => {
println!("✅ MIR interpreter execution completed!");
if let Some(func) = module.functions.get("main") {
let (ety, sval) = match &func.signature.return_type {
MirType::Float => {
if let Some(fb) = result.as_any().downcast_ref::<FloatBox>() {
("Float", format!("{}", fb.value))
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
("Float", format!("{}", ib.value as f64))
} else { ("Float", result.to_string_box().value) }
}
MirType::Integer => {
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
("Integer", ib.value.to_string())
} else { ("Integer", result.to_string_box().value) }
}
MirType::Bool => {
if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
("Bool", bb.value.to_string())
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
("Bool", (ib.value != 0).to_string())
} else { ("Bool", result.to_string_box().value) }
}
MirType::String => {
if let Some(sb) = result.as_any().downcast_ref::<StringBox>() {
("String", sb.value.clone())
} else { ("String", result.to_string_box().value) }
}
_ => { (result.type_name(), result.to_string_box().value) }
};
println!("ResultType(MIR): {}", ety);
println!("Result: {}", sval);
} else {
println!("Result: {:?}", result);
}
}
Err(e) => {
eprintln!("❌ MIR interpreter error: {}", e);
std::process::exit(1);
}
}
}
}