diff --git a/docs/development/current/main/phase152b_static_method_cleanup.md b/docs/development/current/main/phase152b_static_method_cleanup.md new file mode 100644 index 00000000..e3de0906 --- /dev/null +++ b/docs/development/current/main/phase152b_static_method_cleanup.md @@ -0,0 +1,371 @@ +# Phase 152-B: Static Method 宣言の整理(Stage-3 仕様への統一) + +## 🎯 ゴール + +**Stage-3 の正式仕様に統一:「静的なエントリポイントは `static box + メソッド定義`だけ」** + +方針: +- パーサに新構文は追加しない +- テスト・ドキュメント・Stage-B ヘルパーを `static box` スタイルに統一 +- `static method` は legacy/非推奨として明記 + +結果: +- Stage-3 仕様がクリーンになる +- 既存コードは継続動作(ヘルパーで両パターン対応) +- セルフホスティングパイプラインが仕様統一 + +## 📋 スコープ(やること・やらないこと) + +### ✅ やること +- `apps/tests/stage1_run_min.hako` を `static box Main { main() { } }` 形式に書き換え +- `compiler_stageb.hako` の `_find_main_body` ロジックを修正 + - `static method main` パターンに加えて + - `static box Main { main() { } }` パターンにも対応 +- `docs/development/selfhosting/quickstart.md` など、legacy `static method` を例示している docs を `static box` に統一 +- Stage-3 言語リファレンスに「`static method` は legacy/非推奨」を明記 + +### ❌ やらないこと +- Rust/Selfhost パーサに `static method` 新構文を追加しない +- Stage-2 向けの言語仕様変更 +- JoinIR/MIR の意味論変更 + +--- + +## 🏗️ 5 つのタスク + +### Task 1: 仕様ドキュメント作成(設計確認) + +**ファイル**: `docs/development/current/main/phase152b_static_method_cleanup.md`(このファイル) + +**内容**: + +#### 仕様確認 + +**Stage-3 正式仕様**: +```nyash +// ✅ 正しい形(静的エントリポイント) +static box Main { + main(args) { + return 0 + } +} +``` + +**Legacy 形式(廃止予定)**: +```nyash +// ❌ Legacy(Stage-3 では廃止方向) +static box Main { + static method main() { + return 0 + } +} +``` + +#### 影響範囲 + +| 対象 | 内容 | 修正方法 | +|-----|------|---------| +| `apps/tests/stage1_run_min.hako` | テストフィクスチャ | `static box Main { main() { } }` に書き換え | +| `compiler_stageb.hako::_find_main_body` | Stage-B ヘルパー | 両パターンに対応するロジック追加 | +| `quickstart.md` など | サンプルコード | `static box` スタイルに統一 | +| 言語リファレンス | 仕様 | "legacy/非推奨" 明記 | + +#### 方針 + +- **パーサ**: 新構文追加なし(既存の `static box` パーサで十分) +- **後方互換性**: Stage-B ヘルパーで `static method` パターンもサポート +- **将来性**: Phase 160+ では `static method` を廃止(ユーザーにはドキュメントで周知) + +--- + +### Task 2: `stage1_run_min.hako` の書き換え + +**対象ファイル**: `apps/tests/stage1_run_min.hako` + +**現状**: +```nyash +// Minimal run path fixture for Stage-1 CLI. +// Produces a trivial Program(JSON v0) with Main.main returning 0. +static box Main { + static method main() { + return 0 + } +} +``` + +**変更後**: +```nyash +// Minimal run path fixture for Stage-1 CLI. +// Produces a trivial Program(JSON v0) with Main.main returning 0. +static box Main { + main() { + return 0 + } +} +``` + +**期待動作**: +- Stage-1 CLI / Stage-B パイプラインで Program(JSON v0) が問題なく生成される +- Selfhost depth-1 / Stage-3 でも構文エラーなし +- すべての既存テスト PASS + +--- + +### Task 3: `compiler_stageb.hako::_find_main_body` ロジック調整 + +**対象ファイル**: `lang/src/compiler/entry/compiler_stageb.hako`(またはその周辺) + +**現在のロジック** (概要): +```nyash +_find_main_body(src) { + if src == null { return "" } + local key = "static method main" + local p = src.indexOf(key) + if p < 0 { return "" } + // ... parse body from position p +} +``` + +**問題**: +- `static method main` にハードコードされている +- `static box Main { main() { } }` パターンに対応できない + +**修正方針**: 段階的フォールバック + +```nyash +_find_main_body(src) { + if src == null { return "" } + + // Pattern 1: static method main (legacy) + local p = src.indexOf("static method main") + if p >= 0 { + return me.extractBodyFromPosition(src, p + 19) // Skip "static method main" + } + + // Pattern 2: static box Main { main() (modern/Stage-3) + p = src.indexOf("static box Main") + if p < 0 { + p = src.indexOf("box Main") + } + + if p >= 0 { + // Find "main(" after "Main" + local start = src.indexOf("main(", p) + if start >= 0 { + return me.extractBodyFromPosition(src, start) + } + } + + // Fallback: no main found + return "" +} + +extractBodyFromPosition(src, pos) { + // Find opening brace { + local bracePos = src.indexOf("{", pos) + if bracePos < 0 { return "" } + + // Find matching closing brace } + local depth = 0 + local i = bracePos + while i < src.length() { + local ch = src.substring(i, i + 1) + if ch == "{" { + depth = depth + 1 + } else if ch == "}" { + depth = depth - 1 + if depth == 0 { + return src.substring(bracePos, i + 1) + } + } + i = i + 1 + } + + return "" +} +``` + +**実装参考**: +- Phase 25.1c の `StageBBodyExtractorBox.build_body_src` に類似ロジックがある +- String スライシング(`substring`)を活用 +- ブレースマッチングは基本的な手動実装で十分 + +**期待動作**: +- `static method main` パターン: 既存どおり動作 +- `static box Main { main() { } }` パターン: 新規対応 +- エラーハンドリング: 両パターン見つからない場合は空文字(無変更) + +--- + +### Task 4: ドキュメント統一 + +**更新対象**: + +1. **`docs/development/selfhosting/quickstart.md`** など quickstart 系 + - 現在: `static box Main { static method main() { } }` を例示 + - 変更: `static box Main { main() { } }` に統一 + - スニペットはすべて書き換え + +2. **`docs/guides/language-guide.md`** など言語ガイド + - Static box の紹介時に「`main()` メソッドがエントリポイント」を明記 + - `static method` は「legacy/非推奨」と注釈 + +3. **`LANGUAGE_REFERENCE_2025.md`** など仕様書 + - static box の定義で「Static method は Stage-3 では廃止予定」を追記 + +**例**: +```markdown +### Static Box(静的ボックス) + +エントリポイント: + +✅ **推奨**: static box スタイル +\`\`\`nyash +static box Main { + main() { + return 0 + } +} +\`\`\` + +❌ **Legacy(Phase 160+ で廃止予定)**: static method スタイル +\`\`\`nyash +static box Main { + static method main() { + return 0 + } +} +\`\`\` +``` + +--- + +### Task 5: テスト・ドキュメント更新・CURRENT_TASK 記録 + +**テスト実行**: + +1. **Stage-1/Stage-B 経路**: + ```bash + # stage1_run_min.hako を使っている既存スモーク + ./tools/smokes/v2/profiles/quick/selfhost/selfhost_stageb_*.sh + + # 期待: Program(JSON v0) 生成エラーなし + ``` + +2. **Selfhost depth-1**: + ```bash + NYASH_FEATURES=stage3 NYASH_USE_NY_COMPILER=1 NYASH_JOINIR_STRICT=1 \ + ./target/release/hakorune apps/tests/stage1_run_min.hako + + # 期待: RC 0, 構文エラーなし + ``` + +3. **既存テスト互換性**: + ```bash + # 全スモークテスト実行 + ./tools/smokes/v2/run.sh --profile quick + + # 期待: 回帰なし(既存テスト全て PASS) + ``` + +**ドキュメント更新**: +- `phase152b_static_method_cleanup.md` に実装結果を追記 +- `CURRENT_TASK.md` に Phase 152-B 完了エントリ追加 + +**CURRENT_TASK.md 追加内容**: +```markdown +### Phase 152-B: Static Method 宣言の整理(Stage-3 仕様統一)✅ + +**完了内容**: +- Static box エントリポイントを `static box Main { main() { } }` に統一 +- Legacy `static method main()` は廃止方向として明記 +- パーサ側は新構文追加なし(既存 static box パーサで対応) + +**修正ファイル**: +- `apps/tests/stage1_run_min.hako` - テストフィクスチャ統一 +- `compiler_stageb.hako` - _find_main_body ロジック調整(両パターン対応) +- `docs/development/selfhosting/quickstart.md` など - サンプルコード統一 +- `LANGUAGE_REFERENCE_2025.md` - "legacy/非推奨" 明記 + +**テスト結果**: +- Stage-1/Stage-B パイプライン: ✅ 問題なし +- Selfhost depth-1: ✅ stage1_run_min.hako PASS +- 全スモークテスト: ✅ 回帰なし + +**成果**: +- Stage-3 仕様がクリーンに(エントリポイント形式の統一) +- 既存コード継続動作(後方互換性维持) +- セルフホスティングパイプラインの仕様統一 + +**次フェーズ**: Phase 160+ - Static method 廃止(ユーザー周知済み) +``` + +**Git commit**: +``` +chore(phase152-b): Static method 宣言の整理(Stage-3 仕様統一) + +- stage1_run_min.hako を static box スタイルに統一 +- compiler_stageb.hako のメイン検出ロジック修正(両パターン対応) +- quickstart 等ドキュメントのサンプルコード統一 +- static method を legacy/非推奨として明記 +- パーサ新構文追加なし(仕様統一性保持) +- テスト結果: 全スモークテスト PASS +``` + +--- + +## ✅ 完成チェックリスト(Phase 152-B) + +- [ ] Task 1: 仕様ドキュメント作成 + - [ ] 正式仕様と legacy 形式を明記 + - [ ] 影響範囲と方針を整理 +- [ ] Task 2: `stage1_run_min.hako` 書き換え + - [ ] `static box Main { main() { } }` に変更 + - [ ] 期待動作確認 +- [ ] Task 3: `compiler_stageb.hako` ロジック調整 + - [ ] `_find_main_body` を両パターン対応に修正 + - [ ] ブレースマッチング実装 + - [ ] ビルド・テスト確認 +- [ ] Task 4: ドキュメント統一 + - [ ] quickstart.md のサンプルコード統一 + - [ ] 言語ガイド更新(legacy 注釈追加) + - [ ] 仕様書に「廃止予定」明記 +- [ ] Task 5: テスト・CURRENT_TASK 更新 + - [ ] Stage-1/Stage-B スモークテスト実行 + - [ ] Selfhost depth-1 テスト実行 + - [ ] 全スモークテスト確認 + - [ ] CURRENT_TASK.md 更新 + - [ ] git commit で記録 + +--- + +## 所要時間 + +**2-3 時間程度** + +- Task 1(仕様確認): 30分 +- Task 2(ファイル書き換え): 15分 +- Task 3(ロジック修正): 1時間 +- Task 4(ドキュメント): 30分 +- Task 5(テスト・記録): 30分 + +--- + +## 次のステップ + +**Phase 200+: Python → Hakorune トランスパイラ構想への準備** +- Stage-3 仕様が確定したので、より大きな野望に向けて土台が固まった +- Phase 160+ の .hako JoinIR/MIR 移植と並行して、Python 統合の準備を進める + +--- + +## 進捗 + +- ✅ Phase 130-134: LLVM Python バックエンド整理 +- ✅ Phase 150: Selfhost Stage-3 Depth-1 ベースライン強化 +- ✅ Phase 151: ConsoleBox Selfhost Support +- ✅ Phase 152-A: 括弧付き代入式(Rust/Selfhost パーサ両対応) +- 🎯 Phase 152-B: Static Method 宣言整理(← **現在のフェーズ**) +- 📋 Phase 160+: .hako JoinIR/MIR 移植章(予定) +- 🌟 Phase 200+: Python → Hakorune トランスパイラ構想(夢) +