Gate‑C(Core): normalize exit code for JSON file/pipe (rc mirrors return); add quick smokes (file/pipe parity).

This commit is contained in:
nyash-codex
2025-11-01 19:42:20 +09:00
parent e74fe8d3b0
commit aa6cd566e8
4 changed files with 120 additions and 5 deletions

View File

@ -160,6 +160,68 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
}
impl NyashRunner {
/// Execute a MIR module quietly and return an OS exit code derived from the
/// program's return value (Integer/Bool/Float). Strings and other values map to 0.
/// - Integer: value as i64, truncated to 0..=255 (two's complement wrap)
/// - Bool: true=1, false=0
/// - Float: floor toward zero (as i64), truncated to 0..=255
pub(crate) fn execute_mir_module_quiet_exit(&self, module: &crate::mir::MirModule) -> i32 {
use crate::backend::MirInterpreter;
use crate::box_trait::{BoolBox, IntegerBox, StringBox};
use crate::boxes::FloatBox;
use crate::mir::MirType;
fn to_rc(val: i64) -> i32 {
let m = ((val % 256) + 256) % 256; // normalize into 0..=255
m as i32
}
let mut interp = MirInterpreter::new();
match interp.execute_module(module) {
Ok(result) => {
if let Some(func) = module.functions.get("main") {
match &func.signature.return_type {
MirType::Integer => {
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
return to_rc(ib.value);
}
}
MirType::Bool => {
if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
return if bb.value { 1 } else { 0 };
}
}
MirType::Float => {
if let Some(fb) = result.as_any().downcast_ref::<FloatBox>() {
return to_rc(fb.value as i64);
}
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
return to_rc(ib.value);
}
}
_ => {}
}
// Fallbacks by inspecting boxed value kinds (robust to imprecise return types)
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
return to_rc(ib.value);
}
if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
return if bb.value { 1 } else { 0 };
}
if let Some(fb) = result.as_any().downcast_ref::<FloatBox>() {
return to_rc(fb.value as i64);
}
if let Some(_sb) = result.as_any().downcast_ref::<StringBox>() {
return 0; // strings do not define rc semantics yet
}
0
} else {
0
}
}
Err(_) => 1,
}
}
pub(crate) fn execute_mir_module(&self, module: &crate::mir::MirModule) {
// If CLI requested MIR JSON emit, write to file and exit immediately.
let groups = self.config.as_groups();