From 42379a3694ef5d4c5a79f236d9ef6799f05c87df Mon Sep 17 00:00:00 2001 From: Moe Charm Date: Wed, 13 Aug 2025 21:06:37 +0900 Subject: [PATCH] docs: update Phase 7 planning and add correct Nyash philosophy test files - Update CLAUDE.md with 32-thread build option - Enhance phase7_async_mir.md with detailed implementation plan - Add Obj class definition to phase6_ref_set_get.nyash - Create phase6_ref_set_get_correct.nyash following Everything is Box philosophy --- CLAUDE.md | 4 +- .../native-plan/issues/phase7_async_mir.md | 100 +++++++++++++++--- local_tests/phase6_ref_set_get.nyash | 5 + local_tests/phase6_ref_set_get_correct.nyash | 14 +++ 4 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 local_tests/phase6_ref_set_get_correct.nyash diff --git a/CLAUDE.md b/CLAUDE.md index 2d9e0e7a..84b3b28d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -10,8 +10,8 @@ Nyashプログラミング言語開発に必要な情報をまとめたクイッ ### 🐧 Linux/WSL版 ```bash -# ビルドと実行 -cargo build --release +# ビルドと実行(32スレッド並列ビルド) +cargo build --release -j32 ./target/release/nyash program.nyash ``` diff --git a/docs/予定/native-plan/issues/phase7_async_mir.md b/docs/予定/native-plan/issues/phase7_async_mir.md index 34225a45..ad7777b2 100644 --- a/docs/予定/native-plan/issues/phase7_async_mir.md +++ b/docs/予定/native-plan/issues/phase7_async_mir.md @@ -1,20 +1,96 @@ # Phase 7: Async model in MIR (nowait/await) -Summary: +## Summary - nowait/await を MIR に薄く導入(Future 表現)。スレッドベース実装と整合。 +- 既存のFutureBox実装を活用し、MIR/VMレイヤーで非同期処理を表現。 -Scope: -- MIR: FutureNew / FutureSet / Await の導入(Scheduling は現状 thread::spawn 準拠) -- Lowering: nowait → Future 作成 + スケジューリング、await → wait_and_get -- VM: 今の FutureBox 実装を利用 +## Background +- Nyashでは既にFutureBoxが実装済み(`src/boxes/future/mod.rs`) +- nowait/awaitはトークン・ASTノードとして定義済み +- 現在のインタープリターではthread::spawnベースの実装 -Tasks: -- [ ] 命令・builder・vm 実装 -- [ ] サンプル/スナップショット +## Scope +### MIR命令の追加 +- `FutureNew { dst, value }` - 新しいFuture作成(初期値付き) +- `FutureSet { future, value }` - Futureに値を設定 +- `Await { dst, future }` - Futureの完了を待って値を取得 -Acceptance Criteria: -- 代表ケースで VM 実行が期待通り(順不同完了→await で正しい結果) +### Lowering実装 +- `ASTNode::Nowait { variable, expression }` → + 1. expressionを評価 + 2. FutureNew命令でFuture作成 + 3. 別スレッドでの実行をスケジュール +- `ASTNode::AwaitExpression { expression }` → + 1. expressionを評価(Future値を期待) + 2. Await命令で値取得 -References: -- docs/nyash_core_concepts.md(nowait/await + FutureBox) +### VM実装 +- FutureNew: 新しいVMValue::Future作成 +- FutureSet: Future値の更新(is_readyフラグも設定) +- Await: Future完了まで待機してから値を返す + +## Tasks +- [ ] Phase 7.1: MIR命令定義 + - [ ] `src/mir/instruction.rs`にFutureNew/FutureSet/Await追加 + - [ ] Effect maskの設定(FutureNewはPURE、AwaitはREAD) + - [ ] printer/verificationサポート +- [ ] Phase 7.2: AST→MIR lowering + - [ ] `src/mir/builder.rs`にnowait/awaitの処理追加 + - [ ] 適切なbasic block分割(awaitは制御フローに影響) +- [ ] Phase 7.3: VM実装 + - [ ] `src/backend/vm.rs`にVMValue::Future追加 + - [ ] 各命令の実行ロジック実装 + - [ ] FutureBoxとの統合 +- [ ] Phase 7.4: テスト・検証 + - [ ] 基本的なnowait/awaitのテストケース + - [ ] 複数のnowait実行順序テスト + - [ ] エラーケース(Future未完了時の扱い等) + +## Test Cases +```nyash +// 基本的なnowait/await +static box Main { + main() { + nowait f1 = compute(10) + nowait f2 = compute(20) + local result1 = await f1 + local result2 = await f2 + print(result1 + result2) + } +} + +// ネストしたnowait +static box Main { + main() { + nowait outer = { + nowait inner = compute(5) + await inner * 2 + } + print(await outer) + } +} +``` + +## Acceptance Criteria +- 上記テストケースがMIRダンプで正しい命令列を生成 +- VM実行で期待通りの結果(並行実行→正しい順序で結果取得) +- 既存のFutureBox実装との整合性維持 +- verifierがFuture関連の不正を検出 + +## Implementation Notes +- 初期実装ではシンプルにthread::spawnベース継続 +- Futureの型情報は当面VMValue内で管理(型システムは後続フェーズ) +- エラー処理は最小限(Future未完了時のawaitはブロック) + +## Out of Scope (Phase 7) +- async/await構文(Rustライク) +- Promise chain / then構文 +- 取り消し可能なFuture +- 複雑なスケジューリング戦略 +- Future型の静的型チェック + +## References +- `docs/nyash_core_concepts.md`(nowait/await + FutureBox) +- `src/boxes/future/mod.rs`(既存FutureBox実装) +- `src/interpreter/async_methods.rs`(現在のnowait/await実装) diff --git a/local_tests/phase6_ref_set_get.nyash b/local_tests/phase6_ref_set_get.nyash index 1f3f0c84..6bbe4033 100644 --- a/local_tests/phase6_ref_set_get.nyash +++ b/local_tests/phase6_ref_set_get.nyash @@ -1,3 +1,8 @@ +// Phase 6.1 test: Object creation and field access +box Obj { + init { x } +} + static box Main { main() { local o diff --git a/local_tests/phase6_ref_set_get_correct.nyash b/local_tests/phase6_ref_set_get_correct.nyash new file mode 100644 index 00000000..716e0ac0 --- /dev/null +++ b/local_tests/phase6_ref_set_get_correct.nyash @@ -0,0 +1,14 @@ +// Phase 6.1 test: Object creation and field access (Nyash哲学準拠版) +box DataBox { + init { value } +} + +static box Main { + main() { + local data + data = new DataBox() + data.value = 1 + print(data.value) + return data.value + } +} \ No newline at end of file