🎉 Phase 11.8/12.7: MIR Core-13 完全実装 + 糖衣構文ドキュメント更新

主要な変更:
- MIR Core-13命令セット確定(Load/Store削除の革命的設計)
  - Const, BinOp, Compare(値・計算)
  - Jump, Branch, Return, Phi(制御)
  - Call, BoxCall, ExternCall(呼び出し)
  - TypeOp, Safepoint, Barrier(メタ)
- Phase 12.7糖衣構文ドキュメント整理(超圧縮重視、可逆変換保証)
- MIRビルダーのモジュール分割完了
- vtableテストスイート拡充
- AI協調開発ツール追加(並列リファクタリング支援)

詳細:
- src/mir/instruction_introspection.rs: core13_instruction_names()追加
- MIRビルダー分割: decls.rs, exprs_*.rs, fields.rs
- plugin_loader_v2: errors.rs, host_bridge.rs分離
- 論文用データ: mir13-final.md作成

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-09-04 11:34:15 +09:00
parent 4e824fa00e
commit fb2d8e37d5
62 changed files with 3632 additions and 835 deletions

View File

@ -29,156 +29,8 @@ use super::control_flow;
// #[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
// use crate::runtime::plugin_loader_v2::PluginLoaderV2;
/// VM execution error
#[derive(Debug)]
pub enum VMError {
InvalidValue(String),
InvalidInstruction(String),
InvalidBasicBlock(String),
DivisionByZero,
StackUnderflow,
TypeError(String),
}
impl std::fmt::Display for VMError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VMError::InvalidValue(msg) => write!(f, "Invalid value: {}", msg),
VMError::InvalidInstruction(msg) => write!(f, "Invalid instruction: {}", msg),
VMError::InvalidBasicBlock(msg) => write!(f, "Invalid basic block: {}", msg),
VMError::DivisionByZero => write!(f, "Division by zero"),
VMError::StackUnderflow => write!(f, "Stack underflow"),
VMError::TypeError(msg) => write!(f, "Type error: {}", msg),
}
}
}
impl std::error::Error for VMError {}
/// VM value representation
#[derive(Debug, Clone)]
pub enum VMValue {
Integer(i64),
Float(f64),
Bool(bool),
String(String),
Future(crate::boxes::future::FutureBox),
Void,
// Phase 9.78a: Add BoxRef for complex Box types
BoxRef(Arc<dyn NyashBox>),
}
// Manual PartialEq implementation to avoid requiring PartialEq on FutureBox
impl PartialEq for VMValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(VMValue::Integer(a), VMValue::Integer(b)) => a == b,
(VMValue::Float(a), VMValue::Float(b)) => a == b,
(VMValue::Bool(a), VMValue::Bool(b)) => a == b,
(VMValue::String(a), VMValue::String(b)) => a == b,
(VMValue::Void, VMValue::Void) => true,
// Future equality semantics are not defined; treat distinct futures as not equal
(VMValue::Future(_), VMValue::Future(_)) => false,
// BoxRef equality by reference
(VMValue::BoxRef(_), VMValue::BoxRef(_)) => false,
_ => false,
}
}
}
impl VMValue {
/// Convert to NyashBox for output
pub fn to_nyash_box(&self) -> Box<dyn NyashBox> {
match self {
VMValue::Integer(i) => Box::new(IntegerBox::new(*i)),
VMValue::Float(f) => Box::new(crate::boxes::FloatBox::new(*f)),
VMValue::Bool(b) => Box::new(BoolBox::new(*b)),
VMValue::String(s) => Box::new(StringBox::new(s)),
VMValue::Future(f) => Box::new(f.clone()),
VMValue::Void => Box::new(VoidBox::new()),
// BoxRef returns a shared handle (do NOT birth a new instance)
VMValue::BoxRef(arc_box) => arc_box.share_box(),
}
}
/// Get string representation for printing
pub fn to_string(&self) -> String {
match self {
VMValue::Integer(i) => i.to_string(),
VMValue::Float(f) => f.to_string(),
VMValue::Bool(b) => b.to_string(),
VMValue::String(s) => s.clone(),
VMValue::Future(f) => f.to_string_box().value,
VMValue::Void => "void".to_string(),
VMValue::BoxRef(arc_box) => arc_box.to_string_box().value,
}
}
/// Attempt to convert to integer
pub fn as_integer(&self) -> Result<i64, VMError> {
match self {
VMValue::Integer(i) => Ok(*i),
_ => Err(VMError::TypeError(format!("Expected integer, got {:?}", self))),
}
}
/// Attempt to convert to bool
pub fn as_bool(&self) -> Result<bool, VMError> {
match self {
VMValue::Bool(b) => Ok(*b),
VMValue::Integer(i) => Ok(*i != 0),
// Pragmatic coercions for dynamic boxes
VMValue::BoxRef(b) => {
// BoolBox → bool
if let Some(bb) = b.as_any().downcast_ref::<BoolBox>() {
return Ok(bb.value);
}
// IntegerBox → truthy if non-zero (legacy and new)
if let Some(ib) = b.as_any().downcast_ref::<IntegerBox>() { return Ok(ib.value != 0); }
if let Some(ib) = b.as_any().downcast_ref::<crate::boxes::integer_box::IntegerBox>() { return Ok(ib.value != 0); }
// VoidBox → false (nullish false)
if b.as_any().downcast_ref::<VoidBox>().is_some() {
return Ok(false);
}
Err(VMError::TypeError(format!("Expected bool, got BoxRef({})", b.type_name())))
}
// Treat plain Void as false for logical contexts
VMValue::Void => Ok(false),
_ => Err(VMError::TypeError(format!("Expected bool, got {:?}", self))),
}
}
/// Convert from NyashBox to VMValue
pub fn from_nyash_box(nyash_box: Box<dyn crate::box_trait::NyashBox>) -> VMValue {
// Try to downcast to known types for optimization
if let Some(int_box) = nyash_box.as_any().downcast_ref::<IntegerBox>() {
VMValue::Integer(int_box.value)
} else if let Some(bool_box) = nyash_box.as_any().downcast_ref::<BoolBox>() {
VMValue::Bool(bool_box.value)
} else if let Some(string_box) = nyash_box.as_any().downcast_ref::<StringBox>() {
VMValue::String(string_box.value.clone())
} else if let Some(future_box) = nyash_box.as_any().downcast_ref::<crate::boxes::future::FutureBox>() {
VMValue::Future(future_box.clone())
} else {
// Phase 9.78a: For all other Box types (user-defined, plugin), store as BoxRef
VMValue::BoxRef(Arc::from(nyash_box))
}
}
}
impl From<&ConstValue> for VMValue {
fn from(const_val: &ConstValue) -> Self {
match const_val {
ConstValue::Integer(i) => VMValue::Integer(*i),
ConstValue::Float(f) => VMValue::Float(*f),
ConstValue::Bool(b) => VMValue::Bool(*b),
ConstValue::String(s) => VMValue::String(s.clone()),
ConstValue::Null => VMValue::Void, // Simplified
ConstValue::Void => VMValue::Void,
}
}
}
// Thinned: core types moved to vm_types.rs
pub use super::vm_types::{VMError, VMValue};
/// Virtual Machine state
pub struct VM {