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() }
}
}