Phase 12.7: Nyash文法革命とANCP 90%圧縮技法の発見 - 文法改革完了とFunctionBox実装

This commit is contained in:
Moe Charm
2025-09-03 20:03:45 +09:00
parent 6d79d7d3ac
commit 7455c9ec97
69 changed files with 3817 additions and 62 deletions

69
src/boxes/function_box.rs Normal file
View File

@ -0,0 +1,69 @@
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
use crate::ast::ASTNode;
use std::collections::HashMap;
use std::sync::{Arc, Weak};
use std::any::Any;
#[derive(Debug)]
pub struct ClosureEnv {
pub me_value: Option<Weak<dyn NyashBox>>, // Weak me (upgrade at call)
pub captures: HashMap<String, Box<dyn NyashBox>>, // P1: by-value captures
}
impl ClosureEnv {
pub fn new() -> Self { Self { me_value: None, captures: HashMap::new() } }
}
#[derive(Debug)]
pub struct FunctionBox {
pub params: Vec<String>,
pub body: Vec<ASTNode>,
pub env: ClosureEnv,
base: BoxBase,
}
impl FunctionBox {
pub fn new(params: Vec<String>, body: Vec<ASTNode>) -> Self {
Self { params, body, env: ClosureEnv::new(), base: BoxBase::new() }
}
pub fn with_env(params: Vec<String>, body: Vec<ASTNode>, env: ClosureEnv) -> Self {
Self { params, body, env, base: BoxBase::new() }
}
}
impl BoxCore for FunctionBox {
fn box_id(&self) -> u64 { self.base.id }
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FunctionBox(params={}, body={})", self.params.len(), self.body.len())
}
fn as_any(&self) -> &dyn Any { self }
fn as_any_mut(&mut self) -> &mut dyn Any { self }
}
impl NyashBox for FunctionBox {
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(self.clone()) }
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
fn to_string_box(&self) -> StringBox { StringBox::new(format!("FunctionBox(params={}, captures={}, body={})", self.params.len(), self.env.captures.len(), self.body.len())) }
fn type_name(&self) -> &'static str { "FunctionBox" }
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(o) = other.as_any().downcast_ref::<FunctionBox>() {
BoolBox::new(self.box_id() == o.box_id())
} else { BoolBox::new(false) }
}
}
impl Clone for ClosureEnv {
fn clone(&self) -> Self {
let me_value = self.me_value.as_ref().map(|w| Weak::clone(w));
let mut captures: HashMap<String, Box<dyn NyashBox>> = HashMap::new();
for (k, v) in &self.captures { captures.insert(k.clone(), v.clone_box()); }
Self { me_value, captures }
}
}
impl Clone for FunctionBox {
fn clone(&self) -> Self {
Self { params: self.params.clone(), body: self.body.clone(), env: self.env.clone(), base: BoxBase::new() }
}
}

View File

@ -86,6 +86,8 @@ pub mod aot_config_box;
pub mod aot_compiler_box;
pub mod task_group_box;
pub mod token_box;
pub mod function_box;
pub mod ref_cell_box;
// Web専用Box群ブラウザ環境でのみ利用可能
#[cfg(target_arch = "wasm32")]

View File

@ -246,6 +246,7 @@ impl P2PBox {
// 可能ならTransportにハンドラ登録InProcessなど
if let Ok(mut t) = self.transport.write() {
// MethodBox ハンドラー
if let Some(method_box) = handler.as_any().downcast_ref::<MethodBox>() {
let method_clone = method_box.clone();
let intent_name = intent_str.to_string();
@ -253,18 +254,54 @@ impl P2PBox {
let last_from = Arc::clone(&self.last_from);
let last_intent = Arc::clone(&self.last_intent_name);
t.register_intent_handler(&intent_name, Box::new(move |env| {
// flagがtrueのときのみ実行
if flag.load(Ordering::SeqCst) {
// Update receive-side traces for E2E visibility
if let Ok(mut lf) = last_from.write() { *lf = Some(env.from.clone()); }
if let Ok(mut li) = last_intent.write() { *li = Some(env.intent.get_name().to_string_box().value); }
let _ = method_clone.invoke(vec![
Box::new(env.intent.clone()),
Box::new(StringBox::new(env.from.clone())),
]);
if once {
flag.store(false, Ordering::SeqCst);
if once { flag.store(false, Ordering::SeqCst); }
}
}));
// FunctionBox ハンドラー(関数値)
} else if let Some(func_box) = handler.as_any().downcast_ref::<crate::boxes::function_box::FunctionBox>() {
let func_clone = func_box.clone();
let intent_name = intent_str.to_string();
let last_from = Arc::clone(&self.last_from);
let last_intent = Arc::clone(&self.last_intent_name);
t.register_intent_handler(&intent_name, Box::new(move |env| {
if flag.load(Ordering::SeqCst) {
if let Ok(mut lf) = last_from.write() { *lf = Some(env.from.clone()); }
if let Ok(mut li) = last_intent.write() { *li = Some(env.intent.get_name().to_string_box().value); }
// 最小インタープリタで FunctionBox を実行
let mut interp = crate::interpreter::NyashInterpreter::new();
// キャプチャ注入
for (k, v) in func_clone.env.captures.iter() {
interp.declare_local_variable(k, v.clone_or_share());
}
if let Some(me_w) = &func_clone.env.me_value {
if let Some(me_arc) = me_w.upgrade() {
interp.declare_local_variable("me", (*me_arc).clone_or_share());
}
}
// 引数束縛: intent, from必要数だけ
let args: Vec<Box<dyn NyashBox>> = vec![
Box::new(env.intent.clone()),
Box::new(StringBox::new(env.from.clone())),
];
for (i, p) in func_clone.params.iter().enumerate() {
if let Some(av) = args.get(i) {
interp.declare_local_variable(p, av.clone_or_share());
}
}
// 本体実行
crate::runtime::global_hooks::push_task_scope();
for st in &func_clone.body {
let _ = interp.execute_statement(st);
}
crate::runtime::global_hooks::pop_task_scope();
if once { flag.store(false, Ordering::SeqCst); }
}
}));
}

52
src/boxes/ref_cell_box.rs Normal file
View File

@ -0,0 +1,52 @@
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
use std::any::Any;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct RefCellBox {
inner: Arc<Mutex<Box<dyn NyashBox>>>,
base: BoxBase,
}
impl RefCellBox {
pub fn new(initial: Box<dyn NyashBox>) -> Self {
Self { inner: Arc::new(Mutex::new(initial)), base: BoxBase::new() }
}
pub fn with_inner(inner: Arc<Mutex<Box<dyn NyashBox>>>) -> Self {
Self { inner, base: BoxBase::new() }
}
pub fn borrow(&self) -> Box<dyn NyashBox> {
self.inner.lock().unwrap().clone_box()
}
pub fn replace(&self, value: Box<dyn NyashBox>) {
let mut guard = self.inner.lock().unwrap();
*guard = value;
}
pub fn inner_arc(&self) -> Arc<Mutex<Box<dyn NyashBox>>> { Arc::clone(&self.inner) }
}
impl BoxCore for RefCellBox {
fn box_id(&self) -> u64 { self.base.id }
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "RefCellBox(..)")
}
fn as_any(&self) -> &dyn Any { self }
fn as_any_mut(&mut self) -> &mut dyn Any { self }
}
impl NyashBox for RefCellBox {
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self::with_inner(self.inner_arc())) }
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
fn to_string_box(&self) -> StringBox {
let inner = self.inner.lock().unwrap();
StringBox::new(format!("RefCell({})", inner.to_string_box().value))
}
fn type_name(&self) -> &'static str { "RefCellBox" }
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(o) = other.as_any().downcast_ref::<RefCellBox>() {
BoolBox::new(Arc::ptr_eq(&self.inner, &o.inner))
} else { BoolBox::new(false) }
}
}