runner: move WASM/AOT and common interpreter execution into runner::modes; finalize mode split; keep demo helpers local

This commit is contained in:
Moe Charm
2025-08-26 04:39:16 +09:00
parent 1e735d7717
commit abdf9fb5cd
5 changed files with 137 additions and 128 deletions

40
src/runner/modes/aot.rs Normal file
View File

@ -0,0 +1,40 @@
use super::super::NyashRunner;
#[cfg(feature = "wasm-backend")]
use nyash_rust::{parser::NyashParser, mir::MirCompiler, backend::aot::AotBackend};
#[cfg(feature = "wasm-backend")]
use std::{fs, process};
impl NyashRunner {
/// Execute AOT compilation mode (split)
#[cfg(feature = "wasm-backend")]
pub(crate) fn execute_aot_mode(&self, filename: &str) {
// Read the file
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
// Parse to AST
let ast = match NyashParser::parse_from_string(&code) {
Ok(ast) => ast,
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
// Compile to MIR
let mut mir_compiler = MirCompiler::new();
let compile_result = match mir_compiler.compile(ast) {
Ok(result) => result,
Err(e) => { eprintln!("❌ MIR compilation error: {}", e); process::exit(1); }
};
// Determine output file (no extension change)
let output = self.config.output_file.as_deref().unwrap_or(filename);
let mut aot_backend = AotBackend::new();
match aot_backend.compile_to_executable(compile_result.module, output) {
Ok(()) => { println!("✅ AOT compilation successful!\nExecutable written to: {}", output); },
Err(e) => { eprintln!("❌ AOT compilation error: {}", e); process::exit(1); }
}
}
}

View File

@ -0,0 +1,41 @@
use super::super::NyashRunner;
use nyash_rust::{parser::NyashParser, interpreter::NyashInterpreter, box_factory::builtin::BuiltinGroups};
use std::{fs, process};
impl NyashRunner {
/// Execute Nyash file with interpreter (common helper)
pub(crate) fn execute_nyash_file(&self, filename: &str) {
// Read the file
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
println!("📝 File contents:\n{}", code);
println!("\n🚀 Parsing and executing...\n");
// Parse the code with debug fuel limit
eprintln!("🔍 DEBUG: Starting parse with fuel: {:?}...", self.config.debug_fuel);
let ast = match NyashParser::parse_from_string_with_fuel(&code, self.config.debug_fuel) {
Ok(ast) => { eprintln!("🔍 DEBUG: Parse completed, AST created"); ast },
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
println!("✅ Parse successful!");
// Execute the AST
let mut interpreter = NyashInterpreter::new_with_groups(BuiltinGroups::native_full());
eprintln!("🔍 DEBUG: Starting execution...");
match interpreter.execute(ast) {
Ok(result) => {
println!("✅ Execution completed successfully!");
println!("Result: {}", result.to_string_box().value);
},
Err(e) => {
eprintln!("❌ Runtime error:\n{}", e.detailed_message(Some(&code)));
process::exit(1);
}
}
}
}

View File

@ -2,5 +2,8 @@ pub mod mir;
pub mod vm;
pub mod llvm;
pub mod bench;
// WASM/AOT modes remain in runner.rs for now (feature-gated)
pub mod common;
#[cfg(feature = "wasm-backend")]
pub mod wasm;
#[cfg(feature = "wasm-backend")]
pub mod aot;

49
src/runner/modes/wasm.rs Normal file
View File

@ -0,0 +1,49 @@
use super::super::NyashRunner;
#[cfg(feature = "wasm-backend")]
use nyash_rust::{parser::NyashParser, mir::MirCompiler, backend::wasm::WasmBackend};
#[cfg(feature = "wasm-backend")]
use std::{fs, process};
impl NyashRunner {
/// Execute WASM compilation mode (split)
#[cfg(feature = "wasm-backend")]
pub(crate) fn execute_wasm_mode(&self, filename: &str) {
// Read the file
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
// Parse to AST
let ast = match NyashParser::parse_from_string(&code) {
Ok(ast) => ast,
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
// Compile to MIR
let mut mir_compiler = MirCompiler::new();
let compile_result = match mir_compiler.compile(ast) {
Ok(result) => result,
Err(e) => { eprintln!("❌ MIR compilation error: {}", e); process::exit(1); }
};
// Compile to WAT text
let mut wasm_backend = WasmBackend::new();
let wat_text = match wasm_backend.compile_to_wat(compile_result.module) {
Ok(wat) => wat,
Err(e) => { eprintln!("❌ WASM compilation error: {}", e); process::exit(1); }
};
// Determine output file
let output = self.config.output_file.as_deref().unwrap_or_else(|| {
if filename.ends_with(".nyash") { filename.strip_suffix(".nyash").unwrap_or(filename) } else { filename }
});
let output_file = format!("{}.wat", output);
match fs::write(&output_file, wat_text) {
Ok(()) => { println!("✅ WASM compilation successful!\nOutput written to: {}", output_file); },
Err(e) => { eprintln!("❌ Error writing WASM file {}: {}", output_file, e); process::exit(1); }
}
}
}