vm(hako): add v1 reader/dispatcher (flagged), commonize mir_call handler, share block scan; smokes: add v1 hakovm canary; docs: 20.37/20.38 plans, OOB policy; runner: v1 hakovm toggle; include SKIP summary

This commit is contained in:
nyash-codex
2025-11-03 23:21:48 +09:00
parent a4f30ae827
commit 06a729ff40
67 changed files with 3340 additions and 1520 deletions

View File

@ -42,6 +42,47 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
// Direct v0 bridge when requested via CLI/env
let groups = runner.config.as_groups();
// Diagnostic/Exec: accept MIR JSON file direct (experimental; default OFF)
if let Some(path) = groups.parser.mir_json_file.as_ref() {
match std::fs::read_to_string(path) {
Ok(text) => {
// Try schema v1 first (preferred by emitter)
match crate::runner::json_v1_bridge::try_parse_v1_to_module(&text) {
Ok(Some(module)) => {
crate::cli_v!("[mir-json] schema=v1 executing {} (len={})", path, text.len());
let rc = runner.execute_mir_module_quiet_exit(&module);
std::process::exit(rc);
}
Ok(None) => {
// Not v1 schema; attempt minimal v0 loader
if text.contains("\"functions\"") && text.contains("\"blocks\"") {
match crate::runner::mir_json_v0::parse_mir_v0_to_module(&text) {
Ok(module) => {
crate::cli_v!("[mir-json] schema=v0 executing {} (len={})", path, text.len());
let rc = runner.execute_mir_module_quiet_exit(&module);
std::process::exit(rc);
}
Err(e) => {
eprintln!("❌ MIR JSON v0 parse error: {}", e);
std::process::exit(1);
}
}
}
eprintln!("❌ MIR JSON invalid or unsupported shape: {}", path);
std::process::exit(1);
}
Err(e) => {
eprintln!("❌ MIR JSON parse error (v1): {}", e);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("❌ Error reading MIR JSON {}: {}", path, e);
std::process::exit(1);
}
}
}
let use_ny_parser = groups.parser.parser_ny
|| std::env::var("NYASH_USE_NY_PARSER").ok().as_deref() == Some("1");
if use_ny_parser {
@ -236,10 +277,13 @@ impl NyashRunner {
if let Some(_sb) = result.as_any().downcast_ref::<StringBox>() {
return 0; // strings do not define rc semantics yet
}
0
} else {
0
}
// Global fallbacks when signature is missing or imprecise
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; }
0
}
Err(_) => 1,
}

View File

@ -13,6 +13,14 @@ pub(super) fn lower_loop_stmt(
loop_stack: &mut Vec<LoopContext>,
env: &BridgeEnv,
) -> Result<BasicBlockId, String> {
// Unification toggle (default ON). For now legacy path is removed; when OFF, warn and proceed unified.
let unify_on = std::env::var("NYASH_MIR_UNIFY_LOOPFORM")
.ok()
.map(|v| matches!(v.trim().to_ascii_lowercase().as_str(), "1"|"true"|"on"))
.unwrap_or(true);
if !unify_on {
crate::cli_v!("[loopform] NYASH_MIR_UNIFY_LOOPFORM=0 requested, but legacy path is unavailable; using unified phi_core path");
}
let cond_bb = new_block(f);
let body_bb = new_block(f);
let exit_bb = new_block(f);

View File

@ -3,6 +3,26 @@ use crate::mir::{
BasicBlock, BasicBlockId, ConstValue, EffectMask, MirInstruction, MirType, ValueId,
};
use serde_json::Value;
use super::mir_json::common as mirjson_common;
fn parse_effects_from(node: &Value) -> EffectMask {
if let Some(arr) = node.get("effects").and_then(Value::as_array) {
let mut m = EffectMask::PURE;
for e in arr {
if let Some(s) = e.as_str() {
match s {
"write" | "mut" | "WriteHeap" => { m = m.union(EffectMask::WRITE); }
"read" | "ReadHeap" => { m = m.union(EffectMask::READ); }
"io" | "IO" | "ffi" | "FFI" | "debug" => { m = m.union(EffectMask::IO); }
"control" | "Control" => { m = m.union(EffectMask::CONTROL); }
_ => {}
}
}
}
return m;
}
EffectMask::PURE
}
/// Try to parse MIR JSON v1 schema into a MIR module.
/// Returns Ok(None) when the input is not v1 (schema_version missing).
@ -126,7 +146,7 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
func_name
)
})?;
let const_val = parse_const_value(value_obj)?;
let const_val = mirjson_common::parse_const_value_generic(value_obj)?;
block_ref.add_instruction(MirInstruction::Const {
dst: ValueId::new(dst),
value: const_val,
@ -255,11 +275,19 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
}
"mir_call" => {
// Minimal v1 mir_call support (Global/Method/Constructor/Extern/Value + Closure creation)
// dst: optional
// Accept both shapes:
// - flat: { op:"mir_call", callee:{...}, args:[...], effects:[] }
// - nested: { op:"mir_call", mir_call:{ callee:{...}, args:[...], effects:[] } }
// dst remains at the instruction root level in both forms.
let dst_opt = inst.get("dst").and_then(|d| d.as_u64()).map(|v| ValueId::new(v as u32));
// args: array of value ids
let effects = if let Some(sub) = inst.get("mir_call") { parse_effects_from(sub) } else { parse_effects_from(inst) };
// args: support both flat/nested placement
let mut argv: Vec<ValueId> = Vec::new();
if let Some(arr) = inst.get("args").and_then(|a| a.as_array()) {
if let Some(arr) = inst
.get("args")
.and_then(|a| a.as_array())
.or_else(|| inst.get("mir_call").and_then(|m| m.get("args").and_then(|a| a.as_array())))
{
for a in arr {
let id = a.as_u64().ok_or_else(|| format!(
"mir_call arg must be integer value id in function '{}'",
@ -268,8 +296,11 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
argv.push(ValueId::new(id));
}
}
// callee: only Global(name) supported here
let callee_obj = inst.get("callee").ok_or_else(|| {
// callee: support Global/Method/Extern/Value/Closure/Constructor (minimal)
let callee_obj = inst
.get("callee")
.or_else(|| inst.get("mir_call").and_then(|m| m.get("callee")))
.ok_or_else(|| {
format!("mir_call missing callee in function '{}'", func_name)
})?;
let ctype = callee_obj
@ -306,10 +337,31 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
func: ValueId::new(0),
callee: Some(crate::mir::definitions::Callee::Global(mapped)),
args: argv,
effects: EffectMask::PURE,
effects,
});
if let Some(d) = dst_opt { max_value_id = max_value_id.max(d.as_u32() + 1); }
}
"Constructor" => {
// new box instance: box_type required
let bt = callee_obj
.get("box_type")
.and_then(Value::as_str)
.ok_or_else(|| format!(
"mir_call callee Constructor missing box_type in function '{}'",
func_name
))?;
// dst required for Constructor
let dst = dst_opt.ok_or_else(|| format!(
"mir_call Constructor requires dst in function '{}'",
func_name
))?;
block_ref.add_instruction(MirInstruction::NewBox {
dst,
box_type: bt.to_string(),
args: argv.clone(),
});
max_value_id = max_value_id.max(dst.as_u32() + 1);
}
"Method" => {
// receiver: required u64, method: string, box_name: optional
let method = callee_obj
@ -342,63 +394,96 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
certainty: crate::mir::definitions::call_unified::TypeCertainty::Known,
}),
args: argv,
effects: EffectMask::PURE,
effects,
});
if let Some(d) = dst_opt { max_value_id = max_value_id.max(d.as_u32() + 1); }
}
"Closure" => {
// Closure creation (NewClosure equivalent)
// Requires dst; accepts optional params[], captures[[name, id]...], me_capture
let dst = dst_opt.ok_or_else(|| format!(
"mir_call Closure requires dst in function '{}'",
func_name
))?;
// params: array of strings (optional)
let mut params: Vec<String> = Vec::new();
if let Some(arr) = callee_obj.get("params").and_then(Value::as_array) {
for p in arr {
let s = p.as_str().ok_or_else(|| format!(
"mir_call Closure params must be strings in function '{}'",
func_name
))?;
params.push(s.to_string());
}
}
// captures: array of [name, id]
let mut captures: Vec<(String, ValueId)> = Vec::new();
if let Some(arr) = callee_obj.get("captures").and_then(Value::as_array) {
for e in arr {
let pair = e.as_array().ok_or_else(|| format!(
"mir_call Closure capture entry must be array in function '{}'",
func_name
))?;
if pair.len() != 2 {
return Err("mir_call Closure capture entry must have 2 elements".into());
// Two shapes are seen in the wild:
// 1) NewClosure-style descriptor (params/captures/me_capture present) → NewClosure
// 2) Value-style descriptor (func present, optionally captures array) → Call(Callee::Value)
let has_new_fields = callee_obj.get("params").is_some()
|| callee_obj.get("captures").is_some()
|| callee_obj.get("me_capture").is_some();
if has_new_fields {
// Closure creation (NewClosure equivalent)
let dst = dst_opt.ok_or_else(|| format!(
"mir_call Closure requires dst in function '{}'",
func_name
))?;
// params: array of strings (optional)
let mut params: Vec<String> = Vec::new();
if let Some(arr) = callee_obj.get("params").and_then(Value::as_array) {
for p in arr {
let s = p.as_str().ok_or_else(|| format!(
"mir_call Closure params must be strings in function '{}'",
func_name
))?;
params.push(s.to_string());
}
let name = pair[0].as_str().ok_or_else(|| {
"mir_call Closure capture[0] must be string".to_string()
})?;
let id = pair[1].as_u64().ok_or_else(|| {
"mir_call Closure capture[1] must be integer".to_string()
})? as u32;
captures.push((name.to_string(), ValueId::new(id)));
}
// captures: array of [name, id]
let mut captures: Vec<(String, ValueId)> = Vec::new();
if let Some(arr) = callee_obj.get("captures").and_then(Value::as_array) {
for e in arr {
let pair = e.as_array().ok_or_else(|| format!(
"mir_call Closure capture entry must be array in function '{}'",
func_name
))?;
if pair.len() != 2 {
return Err("mir_call Closure capture entry must have 2 elements".into());
}
let name = pair[0].as_str().ok_or_else(|| {
"mir_call Closure capture[0] must be string".to_string()
})?;
let id = pair[1].as_u64().ok_or_else(|| {
"mir_call Closure capture[1] must be integer".to_string()
})? as u32;
captures.push((name.to_string(), ValueId::new(id)));
}
}
// me_capture: optional u64
let me_capture = callee_obj
.get("me_capture")
.and_then(Value::as_u64)
.map(|v| ValueId::new(v as u32));
// Body is not carried in v1; create empty body vector as placeholder
block_ref.add_instruction(MirInstruction::NewClosure {
dst,
params,
body: Vec::new(),
captures,
me: me_capture,
});
max_value_id = max_value_id.max(dst.as_u32() + 1);
} else {
// Value-style closure: treat like Value(func id)
let fid = callee_obj
.get("func")
.and_then(Value::as_u64)
.ok_or_else(|| format!(
"mir_call callee Closure missing func in function '{}'",
func_name
))? as u32;
// Captures array (if present) are appended to argv for minimal parity
if let Some(caps) = callee_obj.get("captures").and_then(Value::as_array) {
for c in caps {
let id = c.as_u64().ok_or_else(|| format!(
"mir_call Closure capture must be integer in function '{}'",
func_name
))? as u32;
argv.push(ValueId::new(id));
}
}
block_ref.add_instruction(MirInstruction::Call {
dst: dst_opt,
func: ValueId::new(0),
callee: Some(crate::mir::definitions::Callee::Value(ValueId::new(fid))),
args: argv,
effects,
});
if let Some(d) = dst_opt { max_value_id = max_value_id.max(d.as_u32() + 1); }
}
// me_capture: optional u64
let me_capture = callee_obj
.get("me_capture")
.and_then(Value::as_u64)
.map(|v| ValueId::new(v as u32));
// Body is not carried in v1; create empty body vector as placeholder
block_ref.add_instruction(MirInstruction::NewClosure {
dst,
params,
body: Vec::new(),
captures,
me: me_capture,
});
max_value_id = max_value_id.max(dst.as_u32() + 1);
}
"Constructor" => {
// box_type: string, dst: required
@ -453,41 +538,11 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
func: ValueId::new(0),
callee: Some(crate::mir::definitions::Callee::Value(ValueId::new(fid))),
args: argv,
effects: EffectMask::PURE,
});
if let Some(d) = dst_opt { max_value_id = max_value_id.max(d.as_u32() + 1); }
}
"Closure" => {
// Minimal closure support: treat as Value(func id) and ignore captures here.
// Schema: { type: "Closure", func: <u64>, captures?: [u64, ...] }
let fid = callee_obj
.get("func")
.and_then(Value::as_u64)
.ok_or_else(|| format!(
"mir_call callee Closure missing func in function '{}'",
func_name
))? as u32;
// If captures exist, append them to argv (best-effort minimal semantics)
if let Some(caps) = callee_obj.get("captures").and_then(Value::as_array) {
for c in caps {
let id = c.as_u64().ok_or_else(|| format!(
"mir_call Closure capture must be integer in function '{}'",
func_name
))? as u32;
argv.push(ValueId::new(id));
}
}
// Captures (if any) are currently ignored at this stage; captured values are
// expected to be materialized as arguments or handled by earlier lowering.
block_ref.add_instruction(MirInstruction::Call {
dst: dst_opt,
func: ValueId::new(0),
callee: Some(crate::mir::definitions::Callee::Value(ValueId::new(fid))),
args: argv,
effects: EffectMask::PURE,
effects,
});
if let Some(d) = dst_opt { max_value_id = max_value_id.max(d.as_u32() + 1); }
}
// (no duplicate Closure arm; handled above)
other => {
return Err(format!(
"unsupported callee type '{}' in mir_call (Gate-C v1 bridge)",
@ -515,44 +570,101 @@ pub fn try_parse_v1_to_module(json: &str) -> Result<Option<MirModule>, String> {
#[allow(dead_code)]
fn parse_const_value(value_obj: &Value) -> Result<ConstValue, String> {
let (type_str, raw_val) = if let Some(t) = value_obj.get("type") {
// Accept both shapes:
// 1) { "type": "i64", "value": 123 }
// 2) { "type": {"kind":"handle","box_type":"StringBox"}, "value": "str" }
// 3) Minimal fallback: when "type" is omitted, assume integer/string directly
let (type_desc, raw_val) = if let Some(t) = value_obj.get("type") {
(
t.clone(),
Some(t.clone()),
value_obj
.get("value")
.cloned()
.ok_or_else(|| "const value missing numeric value".to_string())?,
.ok_or_else(|| "const value missing 'value' field".to_string())?,
)
} else {
(Value::String("i64".to_string()), value_obj.clone())
(None, value_obj.clone())
};
match type_str {
Value::String(s) => match s.as_str() {
// String type descriptor
if let Some(Value::String(s)) = type_desc.as_ref() {
match s.as_str() {
// Integer
"i64" | "int" => {
let val = raw_val
.as_i64()
.ok_or_else(|| "const value expected integer".to_string())?;
Ok(ConstValue::Integer(val))
return Ok(ConstValue::Integer(val));
}
other => Err(format!(
"unsupported const type '{}' in Gate-C v1 bridge",
other
)),
},
Value::Object(obj) => {
if let Some(Value::String(kind)) = obj.get("kind") {
if kind == "handle" {
if let Some(Value::String(box_type)) = obj.get("box_type") {
return Err(format!(
"unsupported const handle type '{}' in Gate-C v1 bridge",
box_type
));
// Float
"f64" | "float" => {
let val = raw_val
.as_f64()
.ok_or_else(|| "const value expected float".to_string())?;
return Ok(ConstValue::Float(val));
}
// Bool (allow explicit bool schema even if current emitter uses i64)
"i1" | "bool" => {
let b = match raw_val {
Value::Bool(v) => v,
Value::Number(n) => n.as_i64().unwrap_or(0) != 0,
Value::String(ref s) => s == "true" || s == "1",
_ => false,
};
return Ok(ConstValue::Bool(b));
}
// String explicit
"string" | "String" => {
let s = raw_val
.as_str()
.ok_or_else(|| "const value expected string".to_string())?;
return Ok(ConstValue::String(s.to_string()));
}
// Void/Null
"void" => {
return Ok(ConstValue::Void);
}
other => {
return Err(format!(
"unsupported const type '{}' in Gate-C v1 bridge",
other
));
}
}
}
// Object descriptor (e.g., handle/StringBox)
if let Some(Value::Object(map)) = type_desc.as_ref() {
if let Some(Value::String(kind)) = map.get("kind") {
if kind == "handle" {
if let Some(Value::String(box_type)) = map.get("box_type") {
match box_type.as_str() {
// StringBox handle is serialized with raw string payload
"StringBox" => {
let s = raw_val
.as_str()
.ok_or_else(|| "StringBox const expects string value".to_string())?;
return Ok(ConstValue::String(s.to_string()));
}
// Other handle kinds are not yet supported in the bridge
other => {
return Err(format!(
"unsupported const handle type '{}' in Gate-C v1 bridge",
other
));
}
}
}
}
Err("unsupported const type object in Gate-C v1 bridge".to_string())
}
return Err("unsupported const type object in Gate-C v1 bridge".to_string());
}
// No explicit type: heuristics
match raw_val {
Value::Number(n) => Ok(ConstValue::Integer(n.as_i64().ok_or_else(|| "integer expected".to_string())?)),
Value::Bool(b) => Ok(ConstValue::Bool(b)),
Value::String(s) => Ok(ConstValue::String(s)),
_ => Err("const value has unsupported type descriptor".to_string()),
}
}

View File

@ -0,0 +1,63 @@
use crate::mir::ConstValue;
use serde_json::Value;
/// Generic const parser used by MIR JSON loaders (v0/v1).
/// Supports minimal set: i64/f64/bool/string and handle(StringBox)->String.
pub fn parse_const_value_generic(value_obj: &Value) -> Result<ConstValue, String> {
// Shapes:
// 1) { "type": "i64", "value": 123 }
// 2) { "type": {"kind":"handle","box_type":"StringBox"}, "value": "str" }
// 3) When "type" is omitted, infer from value
let (type_desc, raw_val) = if let Some(t) = value_obj.get("type") {
(
Some(t.clone()),
value_obj
.get("value")
.cloned()
.ok_or_else(|| "const value missing 'value' field".to_string())?,
)
} else {
(None, value_obj.clone())
};
if let Some(Value::String(s)) = type_desc.as_ref() {
return match s.as_str() {
"i64" | "int" => raw_val.as_i64().map(ConstValue::Integer).ok_or_else(|| "const value expected integer".to_string()),
"f64" | "float" => raw_val.as_f64().map(ConstValue::Float).ok_or_else(|| "const value expected float".to_string()),
"i1" | "bool" => Ok(match raw_val {
Value::Bool(b) => ConstValue::Bool(b),
Value::Number(n) => ConstValue::Bool(n.as_i64().unwrap_or(0) != 0),
Value::String(ref s) => ConstValue::Bool(s == "true" || s == "1"),
_ => ConstValue::Bool(false),
}),
"string" | "String" => raw_val.as_str().map(|s| ConstValue::String(s.to_string())).ok_or_else(|| "const value expected string".to_string()),
"void" => Ok(ConstValue::Void),
other => Err(format!("unsupported const type '{}' in MIR JSON bridge", other)),
};
}
if let Some(Value::Object(map)) = type_desc.as_ref() {
if let Some(Value::String(kind)) = map.get("kind") {
if kind == "handle" {
if let Some(Value::String(box_type)) = map.get("box_type") {
return match box_type.as_str() {
"StringBox" => raw_val
.as_str()
.map(|s| ConstValue::String(s.to_string()))
.ok_or_else(|| "StringBox const expects string value".to_string()),
other => Err(format!("unsupported const handle type '{}' in MIR JSON bridge", other)),
};
}
}
}
return Err("unsupported const type object in MIR JSON bridge".to_string());
}
match raw_val {
Value::Number(n) => n.as_i64().map(ConstValue::Integer).ok_or_else(|| "integer expected".to_string()),
Value::Bool(b) => Ok(ConstValue::Bool(b)),
Value::String(s) => Ok(ConstValue::String(s)),
_ => Err("const value has unsupported type descriptor".to_string()),
}
}

162
src/runner/mir_json_v0.rs Normal file
View File

@ -0,0 +1,162 @@
use crate::mir::{
function::{FunctionSignature, MirFunction, MirModule},
BasicBlock, BasicBlockId, ConstValue, MirInstruction, MirType, ValueId,
};
use serde_json::Value;
use super::mir_json::common as mirjson_common;
/// Parse minimal MIR JSON v0 (no schema_version, root has `functions` and each function has `blocks`).
/// Supported ops (minimal): const(i64), copy, compare(op/lhs/rhs), branch(cond/then/else), jump(target), phi(dst,incoming), ret(value?).
pub fn parse_mir_v0_to_module(json: &str) -> Result<MirModule, String> {
let value: Value = serde_json::from_str(json).map_err(|e| format!("invalid JSON: {}", e))?;
let functions = value
.get("functions")
.and_then(|f| f.as_array())
.ok_or_else(|| "JSON missing functions array".to_string())?;
let mut module = MirModule::new("mir_json_v0".to_string());
for func in functions {
let func_name = func
.get("name")
.and_then(|n| n.as_str())
.unwrap_or("main")
.to_string();
let blocks = func
.get("blocks")
.and_then(|b| b.as_array())
.ok_or_else(|| format!("function '{}' missing blocks array", func_name))?;
if blocks.is_empty() {
return Err(format!("function '{}' has no blocks", func_name));
}
let entry_id = blocks
.get(0)
.and_then(|b| b.get("id"))
.and_then(|id| id.as_u64())
.ok_or_else(|| format!("function '{}' entry block missing id", func_name))? as u32;
let entry_bb = BasicBlockId::new(entry_id);
let mut signature = FunctionSignature {
name: func_name.clone(),
params: Vec::new(),
return_type: MirType::Unknown,
effects: crate::mir::EffectMask::PURE,
};
let mut mir_fn = MirFunction::new(signature.clone(), entry_bb);
let mut max_value_id: u32 = 0;
for block in blocks {
let block_id = block
.get("id")
.and_then(|id| id.as_u64())
.ok_or_else(|| format!("function '{}' block missing id", func_name))? as u32;
let bb_id = BasicBlockId::new(block_id);
if mir_fn.get_block(bb_id).is_none() {
mir_fn.add_block(BasicBlock::new(bb_id));
}
let block_ref = mir_fn
.get_block_mut(bb_id)
.expect("block must exist after insertion");
let instructions = block
.get("instructions")
.and_then(|insts| insts.as_array())
.ok_or_else(|| format!("function '{}' block {} missing instructions array", func_name, block_id))?;
for inst in instructions {
let op = inst
.get("op")
.and_then(|o| o.as_str())
.ok_or_else(|| format!("function '{}' block {} missing op field", func_name, block_id))?;
match op {
"const" => {
let dst = require_u64(inst, "dst", "const dst")? as u32;
let vobj = inst.get("value").ok_or_else(|| "const missing value".to_string())?;
let val = parse_const_value(vobj)?;
block_ref.add_instruction(MirInstruction::Const { dst: ValueId::new(dst), value: val });
max_value_id = max_value_id.max(dst + 1);
}
"copy" => {
let dst = require_u64(inst, "dst", "copy dst")? as u32;
let src = require_u64(inst, "src", "copy src")? as u32;
block_ref.add_instruction(MirInstruction::Copy { dst: ValueId::new(dst), src: ValueId::new(src) });
max_value_id = max_value_id.max(dst + 1);
}
"compare" => {
let dst = require_u64(inst, "dst", "compare dst")? as u32;
let lhs = require_u64(inst, "lhs", "compare lhs")? as u32;
let rhs = require_u64(inst, "rhs", "compare rhs")? as u32;
let operation = inst.get("operation").and_then(Value::as_str).unwrap_or("==");
let cop = parse_compare(operation)?;
block_ref.add_instruction(MirInstruction::Compare { dst: ValueId::new(dst), op: cop, lhs: ValueId::new(lhs), rhs: ValueId::new(rhs) });
max_value_id = max_value_id.max(dst + 1);
}
"branch" => {
let cond = require_u64(inst, "cond", "branch cond")? as u32;
let then_bb = require_u64(inst, "then", "branch then")? as u32;
let else_bb = require_u64(inst, "else", "branch else")? as u32;
block_ref.add_instruction(MirInstruction::Branch { condition: ValueId::new(cond), then_bb: BasicBlockId::new(then_bb), else_bb: BasicBlockId::new(else_bb) });
}
"jump" => {
let target = require_u64(inst, "target", "jump target")? as u32;
block_ref.add_instruction(MirInstruction::Jump { target: BasicBlockId::new(target) });
}
"phi" => {
let dst = require_u64(inst, "dst", "phi dst")? as u32;
let incoming = inst.get("incoming").and_then(Value::as_array).ok_or_else(|| "phi incoming missing".to_string())?;
let mut pairs = Vec::with_capacity(incoming.len());
for entry in incoming {
let pair = entry.as_array().ok_or_else(|| "phi incoming entry must be array".to_string())?;
if pair.len() != 2 { return Err("phi incoming entry must have 2 elements".into()); }
let val = pair[0].as_u64().ok_or_else(|| "phi incoming value must be integer".to_string())? as u32;
let bb = pair[1].as_u64().ok_or_else(|| "phi incoming block must be integer".to_string())? as u32;
pairs.push((BasicBlockId::new(bb), ValueId::new(val)));
}
block_ref.add_instruction(MirInstruction::Phi { dst: ValueId::new(dst), inputs: pairs });
max_value_id = max_value_id.max(dst + 1);
}
"ret" => {
let value = inst.get("value").and_then(|v| v.as_u64()).map(|v| ValueId::new(v as u32));
block_ref.add_instruction(MirInstruction::Return { value });
if let Some(val) = value { signature.return_type = MirType::Integer; max_value_id = max_value_id.max(val.as_u32() + 1); } else { signature.return_type = MirType::Void; }
}
other => {
return Err(format!("unsupported op '{}' in mir_json_v0 loader", other));
}
}
}
}
// Set max value id (best effort)
mir_fn.next_value_id = max_value_id;
module.functions.insert(func_name.clone(), mir_fn);
}
Ok(module)
}
fn require_u64(node: &Value, key: &str, context: &str) -> Result<u64, String> {
node.get(key).and_then(Value::as_u64).ok_or_else(|| format!("{} missing field '{}'", context, key))
}
fn parse_const_value(value_obj: &Value) -> Result<ConstValue, String> {
// Delegate to common generic parser (int/float/bool/string/handle(StringBox))
// Keeps behavior superset of previous (int-only) without changing existing callers.
mirjson_common::parse_const_value_generic(value_obj).map_err(|e| format!("{}", e))
}
fn parse_compare(op: &str) -> Result<crate::mir::types::CompareOp, String> {
use crate::mir::types::CompareOp;
Ok(match op {
"==" => CompareOp::Eq,
"!=" => CompareOp::Ne,
"<" => CompareOp::Lt,
"<=" => CompareOp::Le,
">" => CompareOp::Gt,
">=" => CompareOp::Ge,
s => return Err(format!("unsupported compare op '{}'", s)),
})
}

View File

@ -23,6 +23,8 @@ mod demos;
mod dispatch;
pub mod json_v0_bridge;
mod json_v1_bridge;
pub mod mir_json { pub mod common; }
mod mir_json_v0;
pub mod mir_json_emit;
pub mod modes;
mod pipe_io;
@ -87,6 +89,29 @@ impl NyashRunner {
return;
}
let groups = self.config.as_groups();
// Early: direct MIR JSON execution (no source file). Experimental diagnostics/exec.
if let Some(path) = groups.parser.mir_json_file.as_ref() {
match std::fs::read_to_string(path) {
Ok(text) => {
match crate::runner::json_v1_bridge::try_parse_v1_to_module(&text) {
Ok(Some(module)) => { let rc = self.execute_mir_module_quiet_exit(&module); std::process::exit(rc); }
Ok(None) => {
if text.contains("\"functions\"") && text.contains("\"blocks\"") {
match crate::runner::mir_json_v0::parse_mir_v0_to_module(&text) {
Ok(module) => { let rc = self.execute_mir_module_quiet_exit(&module); std::process::exit(rc); }
Err(e) => { eprintln!("❌ MIR JSON v0 parse error: {}", e); std::process::exit(1); }
}
} else {
eprintln!("❌ MIR JSON invalid or unsupported shape: {}", path);
std::process::exit(1);
}
}
Err(e) => { eprintln!("❌ MIR JSON parse error (v1): {}", e); std::process::exit(1); }
}
}
Err(e) => { eprintln!("❌ Error reading MIR JSON {}: {}", path, e); std::process::exit(1); }
}
}
// Early: build
if let Some(cfg_path) = groups.build.path.clone() {
if let Err(e) = self.run_build_mvp(&cfg_path) {

View File

@ -496,11 +496,38 @@ pub fn parse_preludes_to_asts(
eprintln!("[strip-debug] Parse FAILED for: {} (debug={})", prelude_path, debug);
if debug {
eprintln!("[strip-debug] Error: {}", e);
let es = format!("{}", e);
let lines: Vec<&str> = clean_src.lines().collect();
eprintln!("[strip-debug] Total lines: {}", lines.len());
eprintln!("[strip-debug] Lines 15-25:");
for (idx, line) in lines.iter().enumerate().skip(14).take(11) {
eprintln!(" {:3}: {}", idx + 1, line);
// Try to extract error line number (e.g., "at line 451") and show local context
let mut printed = false;
if let Some(pos) = es.rfind("line ") {
let mut j = pos + 5; // after "line "
let bytes = es.as_bytes();
let mut n: usize = 0; let mut had = false;
while j < bytes.len() {
let c = bytes[j];
if c >= b'0' && c <= b'9' { n = n * 10 + (c - b'0') as usize; j += 1; had = true; } else { break; }
}
if had {
let ln = if n == 0 { 1 } else { n };
let from = ln.saturating_sub(3);
let to = std::cmp::min(lines.len(), ln + 3);
eprintln!("[strip-debug] Context around line {} ({}..={}):", ln, from.max(1), to);
for i in from.max(1)..=to {
let mark = if i == ln { ">>" } else { " " };
if let Some(line) = lines.get(i-1) {
eprintln!("{} {:4}: {}", mark, i, line);
}
}
printed = true;
}
}
if !printed {
eprintln!("[strip-debug] Lines 15-25:");
for (idx, line) in lines.iter().enumerate().skip(14).take(11) {
eprintln!(" {:3}: {}", idx + 1, line);
}
}
eprintln!("[strip-debug] Full clean_src:\n{}\n---", clean_src);
}

View File

@ -180,13 +180,29 @@ pub(super) fn resolve_using_target(
}
return Ok(hit);
}
// Resolve aliases early (provided map) — and then recursively resolve the target
if let Some(v) = aliases.get(tgt) {
if trace {
crate::runner::trace::log(format!("[using/resolve] alias '{}' -> '{}'", tgt, v));
// Resolve aliases early(推移的に)
// - ループ/循環を検出して早期エラー
// - 10段まで防衛的
if let Some(_) = aliases.get(tgt) {
use std::collections::HashSet;
let mut seen: HashSet<String> = HashSet::new();
let mut cur = tgt.to_string();
let mut depth = 0usize;
while let Some(next) = aliases.get(&cur).cloned() {
if trace { crate::runner::trace::log(format!("[using/resolve] alias '{}' -> '{}'", cur, next)); }
if !seen.insert(cur.clone()) {
return Err(format!("alias cycle detected at '{}'", cur));
}
cur = next;
depth += 1;
if depth > 10 {
return Err(format!("alias resolution too deep starting at '{}'", tgt));
}
// Continue while next is also an alias; break when concrete
if !aliases.contains_key(&cur) { break; }
}
// Recurse to resolve the alias target into a concrete path/token
let rec = resolve_using_target(v, false, modules, using_paths, aliases, packages, context_dir, strict, verbose)?;
// Recurse once into final target to materialize path/token
let rec = resolve_using_target(&cur, false, modules, using_paths, aliases, packages, context_dir, strict, verbose)?;
crate::runner::box_index::cache_put(&key, rec.clone());
return Ok(rec);
}