2025-09-16 00:01:31 +09:00
|
|
|
/*!
|
|
|
|
|
* 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};
|
|
|
|
|
|
2025-09-16 03:54:44 +09:00
|
|
|
/// Thin file dispatcher: select backend and delegate to mode executors
|
|
|
|
|
pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
|
2025-09-17 07:43:07 +09:00
|
|
|
// Selfhost pipeline (Ny -> JSON v0) behind env gate
|
|
|
|
|
if std::env::var("NYASH_USE_NY_COMPILER").ok().as_deref() == Some("1") {
|
|
|
|
|
if runner.try_run_selfhost_pipeline(filename) {
|
|
|
|
|
return;
|
2025-09-19 02:07:38 +09:00
|
|
|
} else {
|
|
|
|
|
crate::cli_v!("[ny-compiler] fallback to default path (MVP unavailable for this input)");
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Direct v0 bridge when requested via CLI/env
|
2025-09-19 14:29:02 +09:00
|
|
|
let groups = runner.config.as_groups();
|
|
|
|
|
let use_ny_parser = groups.parser.parser_ny
|
2025-09-17 07:43:07 +09:00
|
|
|
|| 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) => {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("🚀 Nyash MIR Interpreter - (parser=ny) Executing file: {} 🚀", filename);
|
2025-09-17 07:43:07 +09:00
|
|
|
runner.execute_mir_module(&module);
|
2025-09-16 00:01:31 +09:00
|
|
|
return;
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ Direct bridge parse error: {}", e);
|
|
|
|
|
process::exit(1);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
// AST dump mode
|
2025-09-19 14:29:02 +09:00
|
|
|
if groups.debug.dump_ast {
|
2025-09-17 07:43:07 +09:00
|
|
|
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);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
};
|
|
|
|
|
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
|
2025-09-19 14:29:02 +09:00
|
|
|
if groups.debug.dump_mir || groups.debug.verify_mir {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("🚀 Nyash MIR Compiler - Processing file: {} 🚀", filename);
|
2025-09-17 07:43:07 +09:00
|
|
|
runner.execute_mir_mode(filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
// WASM / AOT (feature-gated)
|
2025-09-19 14:29:02 +09:00
|
|
|
if groups.compile_wasm {
|
2025-09-17 07:43:07 +09:00
|
|
|
#[cfg(feature = "wasm-backend")]
|
|
|
|
|
{
|
|
|
|
|
super::modes::wasm::execute_wasm_mode(runner, filename);
|
2025-09-16 00:01:31 +09:00
|
|
|
return;
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
#[cfg(not(feature = "wasm-backend"))]
|
|
|
|
|
{
|
|
|
|
|
eprintln!("❌ WASM backend not available. Please rebuild with: cargo build --features wasm-backend");
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-19 14:29:02 +09:00
|
|
|
if groups.compile_native {
|
2025-09-17 07:43:07 +09:00
|
|
|
#[cfg(feature = "cranelift-jit")]
|
|
|
|
|
{
|
|
|
|
|
runner.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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
// Backend selection
|
2025-09-19 14:29:02 +09:00
|
|
|
match groups.backend.backend.as_str() {
|
2025-09-17 07:43:07 +09:00
|
|
|
"mir" => {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("🚀 Nyash MIR Interpreter - Executing file: {} 🚀", filename);
|
2025-09-16 03:54:44 +09:00
|
|
|
runner.execute_mir_mode(filename);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
"vm" => {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("🚀 Nyash VM Backend - Executing file: {} 🚀", filename);
|
2025-09-17 16:11:01 +09:00
|
|
|
#[cfg(feature = "vm-legacy")]
|
|
|
|
|
{
|
|
|
|
|
runner.execute_vm_mode(filename);
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(feature = "vm-legacy"))]
|
|
|
|
|
{
|
|
|
|
|
// Legacy VM is disabled; use PyVM harness instead.
|
|
|
|
|
super::modes::pyvm::execute_pyvm_only(runner, filename);
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 10:58:12 +09:00
|
|
|
"interpreter" => {
|
|
|
|
|
eprintln!("⚠ interpreter backend is legacy and deprecated. Use 'vm' (PyVM/LLVM) instead.");
|
2025-09-17 16:11:01 +09:00
|
|
|
#[cfg(feature = "vm-legacy")]
|
|
|
|
|
{
|
2025-09-17 10:58:12 +09:00
|
|
|
runner.execute_vm_mode(filename);
|
|
|
|
|
}
|
2025-09-17 16:11:01 +09:00
|
|
|
#[cfg(not(feature = "vm-legacy"))]
|
|
|
|
|
{
|
|
|
|
|
// Legacy VM disabled; route to PyVM-only runner
|
|
|
|
|
super::modes::pyvm::execute_pyvm_only(runner, filename);
|
|
|
|
|
}
|
2025-09-17 10:58:12 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
#[cfg(feature = "cranelift-jit")]
|
|
|
|
|
"jit-direct" => {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("⚡ Nyash JIT-Direct Backend - Executing file: {} ⚡", filename);
|
2025-09-16 00:01:31 +09:00
|
|
|
#[cfg(feature = "cranelift-jit")]
|
2025-09-17 07:43:07 +09:00
|
|
|
{
|
|
|
|
|
// Use independent JIT-direct runner method (no VM execute loop)
|
|
|
|
|
runner.run_file_jit_direct(filename);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
#[cfg(not(feature = "cranelift-jit"))]
|
|
|
|
|
{
|
|
|
|
|
eprintln!("❌ Cranelift backend not available. Please rebuild with: cargo build --features cranelift-jit");
|
|
|
|
|
process::exit(1);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
|
|
|
|
"llvm" => {
|
2025-09-19 02:07:38 +09:00
|
|
|
crate::cli_v!("⚡ Nyash LLVM Backend - Executing file: {} ⚡", filename);
|
2025-09-17 07:43:07 +09:00
|
|
|
runner.execute_llvm_mode(filename);
|
|
|
|
|
}
|
2025-09-17 10:58:12 +09:00
|
|
|
other => {
|
|
|
|
|
eprintln!("❌ Unknown backend: {}. Use 'vm' or 'llvm' (or 'interpreter' legacy).", other);
|
|
|
|
|
std::process::exit(2);
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
2025-09-16 03:54:44 +09:00
|
|
|
|
|
|
|
|
impl NyashRunner {
|
2025-09-16 00:01:31 +09:00
|
|
|
pub(crate) fn execute_mir_module(&self, module: &crate::mir::MirModule) {
|
2025-09-17 22:01:29 +09:00
|
|
|
// If CLI requested MIR JSON emit, write to file and exit immediately.
|
2025-09-19 14:29:02 +09:00
|
|
|
let groups = self.config.as_groups();
|
|
|
|
|
if let Some(path) = groups.emit.emit_mir_json.as_ref() {
|
2025-09-17 22:01:29 +09:00
|
|
|
let p = std::path::Path::new(path);
|
|
|
|
|
if let Err(e) = crate::runner::mir_json_emit::emit_mir_json_for_harness_bin(module, p) {
|
|
|
|
|
eprintln!("❌ MIR JSON emit error: {}", e);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
println!("MIR JSON written: {}", p.display());
|
|
|
|
|
std::process::exit(0);
|
|
|
|
|
}
|
2025-09-18 04:23:11 +09:00
|
|
|
// If CLI requested EXE emit, generate JSON then invoke ny-llvmc to link NyRT and exit.
|
2025-09-19 14:29:02 +09:00
|
|
|
if let Some(exe_out) = groups.emit.emit_exe.as_ref() {
|
2025-09-19 02:07:38 +09:00
|
|
|
if let Err(e) = crate::runner::modes::common_util::exec::ny_llvmc_emit_exe_bin(
|
|
|
|
|
module,
|
|
|
|
|
exe_out,
|
2025-09-19 14:29:02 +09:00
|
|
|
groups.emit.emit_exe_nyrt.as_deref(),
|
|
|
|
|
groups.emit.emit_exe_libs.as_deref(),
|
2025-09-19 02:07:38 +09:00
|
|
|
) {
|
|
|
|
|
eprintln!("❌ {}", e);
|
2025-09-18 04:23:11 +09:00
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
println!("EXE written: {}", exe_out);
|
|
|
|
|
std::process::exit(0);
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
use crate::backend::MirInterpreter;
|
2025-09-17 07:43:07 +09:00
|
|
|
use crate::box_trait::{BoolBox, IntegerBox, StringBox};
|
2025-09-16 00:01:31 +09:00
|
|
|
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))
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
("Float", result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
|
|
|
|
MirType::Integer => {
|
|
|
|
|
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
("Integer", ib.value.to_string())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
("Integer", result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
|
|
|
|
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())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
("Bool", result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
|
|
|
|
MirType::String => {
|
|
|
|
|
if let Some(sb) = result.as_any().downcast_ref::<StringBox>() {
|
|
|
|
|
("String", sb.value.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
("String", result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-16 00:01:31 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
_ => (result.type_name(), result.to_string_box().value),
|
2025-09-16 00:01:31 +09:00
|
|
|
};
|
|
|
|
|
println!("ResultType(MIR): {}", ety);
|
|
|
|
|
println!("Result: {}", sval);
|
|
|
|
|
} else {
|
|
|
|
|
println!("Result: {:?}", result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ MIR interpreter error: {}", e);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|