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

View File

@ -412,133 +412,9 @@ impl NyashRunner {
walk(ast, runtime);
}
/// Execute WASM compilation mode
#[cfg(feature = "wasm-backend")]
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);
}
};
// execute_wasm_mode moved to runner::modes::wasm
// 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 WASM (Phase 9.77a fix: use compile_to_wat instead of compile_module)
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);
// Write WAT output (already a string)
let output_str = wat_text;
match fs::write(&output_file, output_str) {
Ok(()) => {
println!("✅ WASM compilation successful!");
println!("Output written to: {}", output_file);
},
Err(e) => {
eprintln!("❌ Error writing WASM file {}: {}", output_file, e);
process::exit(1);
}
}
}
/// Execute AOT compilation mode
#[cfg(feature = "wasm-backend")]
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);
}
};
// Compile via AOT backend
let mut aot_backend = match AotBackend::new() {
Ok(backend) => backend,
Err(e) => {
eprintln!("❌ Failed to create AOT backend: {}", e);
process::exit(1);
}
};
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
}
});
match aot_backend.compile_to_executable(compile_result.module, output) {
Ok(()) => {
println!("✅ AOT compilation successful!");
println!("Executable written to: {}", output);
},
Err(e) => {
eprintln!("❌ AOT compilation error: {}", e);
process::exit(1);
}
}
}
// execute_aot_mode moved to runner::modes::aot
/// Execute LLVM mode
fn execute_llvm_mode(&self, filename: &str) {