runner: move WASM/AOT and common interpreter execution into runner::modes; finalize mode split; keep demo helpers local
This commit is contained in:
128
src/runner.rs
128
src/runner.rs
@ -412,133 +412,9 @@ impl NyashRunner {
|
|||||||
walk(ast, runtime);
|
walk(ast, runtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute WASM compilation mode
|
// execute_wasm_mode moved to runner::modes::wasm
|
||||||
#[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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Parse to AST
|
// execute_aot_mode moved to runner::modes::aot
|
||||||
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 LLVM mode
|
/// Execute LLVM mode
|
||||||
fn execute_llvm_mode(&self, filename: &str) {
|
fn execute_llvm_mode(&self, filename: &str) {
|
||||||
|
|||||||
40
src/runner/modes/aot.rs
Normal file
40
src/runner/modes/aot.rs
Normal 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); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
41
src/runner/modes/common.rs
Normal file
41
src/runner/modes/common.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -2,5 +2,8 @@ pub mod mir;
|
|||||||
pub mod vm;
|
pub mod vm;
|
||||||
pub mod llvm;
|
pub mod llvm;
|
||||||
pub mod bench;
|
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
49
src/runner/modes/wasm.rs
Normal 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); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user