diff --git a/src/runner.rs b/src/runner.rs index 83ec0870..b2dae53e 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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) { diff --git a/src/runner/modes/aot.rs b/src/runner/modes/aot.rs new file mode 100644 index 00000000..c3acb03c --- /dev/null +++ b/src/runner/modes/aot.rs @@ -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); } + } + } +} + diff --git a/src/runner/modes/common.rs b/src/runner/modes/common.rs new file mode 100644 index 00000000..01069404 --- /dev/null +++ b/src/runner/modes/common.rs @@ -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); + } + } + } +} + diff --git a/src/runner/modes/mod.rs b/src/runner/modes/mod.rs index 93923b7e..9ec89aad 100644 --- a/src/runner/modes/mod.rs +++ b/src/runner/modes/mod.rs @@ -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; diff --git a/src/runner/modes/wasm.rs b/src/runner/modes/wasm.rs new file mode 100644 index 00000000..d4a40863 --- /dev/null +++ b/src/runner/modes/wasm.rs @@ -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); } + } + } +} +