📚 Phase 12.5 最適化戦略 & Phase 15 セルフホスティング計画
Phase 12.5: MIR15最適化戦略 - コンパイラ丸投げ作戦 - optimization-strategy.txt: 詳細戦略(MIR側は軽量、コンパイラに丸投げ) - implementation-examples.md: 具体的な実装例 - debug-safety-comparison.md: 現在のDebugBox vs ChatGPT5提案の比較分析 Phase 15: Nyashセルフホスティング - 究極の目標 - self-hosting-plan.txt: 内蔵Craneliftによる実現計画 - technical-details.md: CompilerBox設計とブートストラップ手順 - README.md: セルフホスティングのビジョン 重要な知見: - LLVM統合完了済み(Phase 11)だが依存が重すぎる - Craneliftが現実的な選択肢(3-5MB vs LLVM 50-100MB) - 「コンパイラもBox、すべてがBox」の夢へ MASTERロードマップ更新済み
This commit is contained in:
@ -7,19 +7,34 @@
|
||||
*/
|
||||
|
||||
use super::*;
|
||||
use crate::boxes::result::NyashResultBox;
|
||||
use crate::box_trait::StringBox;
|
||||
|
||||
impl NyashInterpreter {
|
||||
/// await式を実行 - 非同期操作の結果を待機
|
||||
pub(super) fn execute_await(&mut self, expression: &ASTNode) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let value = self.execute_expression(expression)?;
|
||||
|
||||
// FutureBoxなら待機して結果を取得
|
||||
// FutureBoxなら協調待機して Result.Ok/Err を返す
|
||||
if let Some(future) = value.as_any().downcast_ref::<FutureBox>() {
|
||||
future.wait_and_get()
|
||||
.map_err(|msg| RuntimeError::InvalidOperation { message: msg })
|
||||
let max_ms: u64 = std::env::var("NYASH_AWAIT_MAX_MS").ok().and_then(|s| s.parse().ok()).unwrap_or(5000);
|
||||
let start = std::time::Instant::now();
|
||||
let mut spins = 0usize;
|
||||
while !future.ready() {
|
||||
crate::runtime::global_hooks::safepoint_and_poll();
|
||||
std::thread::yield_now();
|
||||
spins += 1;
|
||||
if spins % 1024 == 0 { std::thread::sleep(std::time::Duration::from_millis(1)); }
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
|
||||
let err = Box::new(StringBox::new("Timeout"));
|
||||
return Ok(Box::new(NyashResultBox::new_err(err)));
|
||||
}
|
||||
}
|
||||
let v = future.get();
|
||||
Ok(Box::new(NyashResultBox::new_ok(v)))
|
||||
} else {
|
||||
// FutureBoxでなければそのまま返す
|
||||
Ok(value)
|
||||
// FutureBoxでなければ Ok(value) で返す
|
||||
Ok(Box::new(NyashResultBox::new_ok(value)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,17 +153,27 @@ impl NyashInterpreter {
|
||||
}
|
||||
|
||||
|
||||
/// await式を実行 - Execute await expression
|
||||
/// await式を実行 - Execute await expression (Result.Ok/Err統一)
|
||||
pub(super) fn execute_await(&mut self, expression: &ASTNode) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let value = self.execute_expression(expression)?;
|
||||
|
||||
// FutureBoxなら待機して結果を取得
|
||||
if let Some(future) = value.as_any().downcast_ref::<FutureBox>() {
|
||||
future.wait_and_get()
|
||||
.map_err(|msg| RuntimeError::InvalidOperation { message: msg })
|
||||
let max_ms: u64 = std::env::var("NYASH_AWAIT_MAX_MS").ok().and_then(|s| s.parse().ok()).unwrap_or(5000);
|
||||
let start = std::time::Instant::now();
|
||||
let mut spins = 0usize;
|
||||
while !future.ready() {
|
||||
crate::runtime::global_hooks::safepoint_and_poll();
|
||||
std::thread::yield_now();
|
||||
spins += 1;
|
||||
if spins % 1024 == 0 { std::thread::sleep(std::time::Duration::from_millis(1)); }
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
|
||||
let err = Box::new(crate::box_trait::StringBox::new("Timeout"));
|
||||
return Ok(Box::new(crate::boxes::result::NyashResultBox::new_err(err)));
|
||||
}
|
||||
}
|
||||
let v = future.get();
|
||||
Ok(Box::new(crate::boxes::result::NyashResultBox::new_ok(v)))
|
||||
} else {
|
||||
// FutureBoxでなければそのまま返す
|
||||
Ok(value)
|
||||
Ok(Box::new(crate::boxes::result::NyashResultBox::new_ok(value)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +75,8 @@ impl NyashInterpreter {
|
||||
self.declare_local_variable(param, value.clone_or_share());
|
||||
}
|
||||
|
||||
// static関数の本体を実行
|
||||
// static関数の本体を実行(TaskGroupスコープ)
|
||||
crate::runtime::global_hooks::push_task_scope();
|
||||
let mut result = Box::new(VoidBox::new()) as Box<dyn NyashBox>;
|
||||
for statement in &body {
|
||||
result = self.execute_statement(statement)?;
|
||||
@ -89,6 +90,7 @@ impl NyashInterpreter {
|
||||
}
|
||||
|
||||
// local変数スタックを復元
|
||||
crate::runtime::global_hooks::pop_task_scope();
|
||||
self.restore_local_vars(saved_locals);
|
||||
|
||||
// outbox変数スタックを復元
|
||||
@ -204,25 +206,27 @@ impl NyashInterpreter {
|
||||
self.declare_local_variable(param, value.clone_or_share());
|
||||
}
|
||||
|
||||
// メソッドの本体を実行
|
||||
let mut result = Box::new(VoidBox::new()) as Box<dyn NyashBox>;
|
||||
for statement in body {
|
||||
result = self.execute_statement(statement)?;
|
||||
|
||||
// return文チェック
|
||||
if let super::ControlFlow::Return(return_val) = &self.control_flow {
|
||||
result = return_val.clone_box();
|
||||
self.control_flow = super::ControlFlow::None;
|
||||
break;
|
||||
}
|
||||
// メソッドの本体を実行(TaskGroupスコープ)
|
||||
crate::runtime::global_hooks::push_task_scope();
|
||||
let mut result = Box::new(VoidBox::new()) as Box<dyn NyashBox>;
|
||||
for statement in body {
|
||||
result = self.execute_statement(statement)?;
|
||||
|
||||
// return文チェック
|
||||
if let super::ControlFlow::Return(return_val) = &self.control_flow {
|
||||
result = return_val.clone_box();
|
||||
self.control_flow = super::ControlFlow::None;
|
||||
break;
|
||||
}
|
||||
|
||||
// local変数スタックを復元
|
||||
self.restore_local_vars(saved_locals);
|
||||
|
||||
idebug!("✅ Static box method completed: {}.{}", name, method);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// local変数スタックを復元
|
||||
crate::runtime::global_hooks::pop_task_scope();
|
||||
self.restore_local_vars(saved_locals);
|
||||
|
||||
idebug!("✅ Static box method completed: {}.{}", name, method);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -598,7 +602,8 @@ impl NyashInterpreter {
|
||||
self.declare_local_variable(param, value.clone_or_share());
|
||||
}
|
||||
|
||||
// 親メソッドの本体を実行
|
||||
// 親メソッドの本体を実行(TaskGroupスコープ)
|
||||
crate::runtime::global_hooks::push_task_scope();
|
||||
let mut result: Box<dyn NyashBox> = Box::new(VoidBox::new());
|
||||
for statement in &body {
|
||||
result = self.execute_statement(statement)?;
|
||||
@ -615,6 +620,7 @@ impl NyashInterpreter {
|
||||
idebug!("🔍 DEBUG: FromCall {}.{} result: {}", parent, method, result.to_string_box().value);
|
||||
|
||||
// local変数スタックを復元
|
||||
crate::runtime::global_hooks::pop_task_scope();
|
||||
self.restore_local_vars(saved_locals);
|
||||
|
||||
Ok(result)
|
||||
|
||||
@ -74,11 +74,12 @@ impl NyashInterpreter {
|
||||
self.declare_local_variable(param, value.clone_or_share());
|
||||
}
|
||||
|
||||
// 関数本体を実行
|
||||
// 関数本体を実行(TaskGroupスコープをプッシュ)
|
||||
crate::runtime::global_hooks::push_task_scope();
|
||||
let mut result: Box<dyn NyashBox> = Box::new(VoidBox::new());
|
||||
for statement in &body {
|
||||
result = self.execute_statement(statement)?;
|
||||
|
||||
|
||||
// return文チェック
|
||||
if let super::ControlFlow::Return(return_val) = &self.control_flow {
|
||||
result = return_val.clone_box();
|
||||
@ -86,10 +87,11 @@ impl NyashInterpreter {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 🌍 local変数スタックを復元(関数呼び出し終了)
|
||||
crate::runtime::global_hooks::pop_task_scope();
|
||||
self.restore_local_vars(saved_locals);
|
||||
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
Err(RuntimeError::InvalidOperation {
|
||||
|
||||
@ -247,10 +247,12 @@ impl NyashInterpreter {
|
||||
}
|
||||
});
|
||||
|
||||
// FutureBoxを現在のTaskGroupに登録(暗黙グループ best-effort)
|
||||
crate::runtime::global_hooks::register_future_to_current_group(&future_box);
|
||||
// FutureBoxを変数に保存
|
||||
let future_box_instance = Box::new(future_box) as Box<dyn NyashBox>;
|
||||
self.set_variable(variable, future_box_instance)?;
|
||||
|
||||
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user