# PythonParserBox実装計画 - エキスパートフィードバック 日付: 2025-08-27 ## Gemini先生のフィードバック ### 総評 これは非常に野心的で、言語の成熟度を飛躍的に高める可能性を秘めた素晴らしい計画です。Nyashの「Everything is a Box」哲学とPythonエコシステムを融合させるという着眼点に大変興奮しました。 ### 1. 実装計画は技術的に健全か?落とし穴は? **技術的健全性:** はい、計画は全体として技術的に非常に健全です。 * **CPythonパーサーの利用:** `pyo3`経由で`ast.parse()`を利用するのは、Python構文との互換性を100%保証するための最も確実で賢明なアプローチです。 * **JSON中間表現(IR):** Python AST (Pythonメモリ空間) と Nyash AST (Rustメモリ空間) の間にJSONを挟むのは、言語間の境界を明確に分離する良い設計です。 * **段階的実装とフォールバック:** 未実装機能を`exec()`にフォールバックする戦略は、実用性を保ちながら段階的に実装を進めるための極めて現実的なアプローチです。 **潜在的な落とし穴:** * **パフォーマンス:** `Python AST → JSON → Nyash AST → Nyash実行`というパイプラインは、特に初期段階では非常に低速になります。 * **ASTの複雑性の爆発:** PythonのASTは非常に巨大で、言語バージョンの更新も速いです。 * **標準ライブラリの壁:** Pythonの真の力は広範な標準/サードパーティライブラリにあります。`import`文をどう扱うかは最重要課題です。 ### 2. Python AST → Nyash AST変換で注意すべき意味論の違いは? * **型システム:** Pythonは動的型付け、Nyashは静的型付けまたはより厳格な型システムを持つと推測 * **オブジェクトモデルと可変性:** Pythonのオブジェクトは基本的に可変(mutable) * **スコープとクロージャ:** Pythonの`global`や`nonlocal`の挙動は独特 * **特殊メソッド (`__dunder__`):** Pythonの挙動は特殊メソッドで定義される * **組み込み関数:** `len()`, `print()`, `range()`などをどう扱うか ### 3. Nyashパーサーのバグ検証戦略として効果的か? **非常に効果的です。** これは「Differential Testing(差分テスト)」と呼ばれる強力な手法です。 * **巨大なテストスイート:** 事実上、世の中にある無数のPythonコードがNyashのテストケースになります。 * **微妙なバグの発見:** 手書きの単体テストでは見逃しがちなセマンティクスのバグを発見するのに絶大な効果を発揮します。 ### 4. pyo3統合でのGIL管理のベストプラクティスは? * **基本原則:** Pythonオブジェクトに触れるコードは、必ずGILを獲得した状態で実行 * **GILの必要期間を最小化する** * **`py.allow_threads`の活用:** 時間がかかり、かつPythonオブジェクトに触れない処理で使用 ### 5. 段階的実装(Phase 1-4)の優先順位は適切か? はい、提案されている優先順位は非常に適切です。 1. Phase 1: 基本構文 + 変数束縛 + 基本的な式 + プリミティブ型 2. Phase 2: list, dict, tuple 3. Phase 3: 関数とスコープ 4. Phase 4: モジュールとクラス **最優先で並行すべきこと:** `exec()`へのフォールバック機構 ### 6. 言語成熟度向上への貢献度をどう評価する? **定量的指標:** * ライブラリ互換性カバレッジ * パフォーマンステスト * 発見バグ数 **定性的指標:** * 表現力の向上 * 開発者体験 * エコシステムのマイルストーン ### 結論 この`PythonParserBox`計画は、単なる機能追加ではなく、**Nyash言語のテスト、デバッグ、そしてエコシステム獲得を同時に加速させる、極めて戦略的な一手**です。 --- ## Codex先生のフィードバック ### Overall Verdict - Sound approach: Using CPython's parser via pyo3, lowering via a JSON IR, and gradually replacing exec() with Nyash MIR/JIT is feasible and strategically strong. - Biggest risks: Semantic mismatches at statement/expression boundaries, version skew of Python AST, and boundary-costs between CPython and Nyash during phased rollout. ### Architectural Pitfalls - **Python version skew:** `ast` schema changes across minors. Pin and encode `py_version` + `ast_format` in the JSON. - **AST stability vs syntax fidelity:** `ast` loses comments and some token distinctions - **Boundary granularity:** Whole-file fallback wastes partial coverage; per-node fallback is unsafe. **The practical unit is per-function.** - **Import system and environment:** Python imports pull arbitrary code - **Error mapping:** Propagate Python exceptions with full traceback - **Performance overhead:** Python AST→JSON→Nyash→MIR is heavy - **Object model mismatch:** Identity (`is`), mutability, truthiness, numeric tower - **Concurrency:** GIL limits parallel parse/exec ### AST→Nyash Semantics: High-Risk Differences - **Names and scope:** - LEGB resolution; `global`/`nonlocal` behavior; closures and cell variables - Comprehension scopes (separate scope in Python 3) - **Control flow:** - `for` iterates via iterator protocol; `for/else`, `while/else` semantics - Short-circuit truthiness uses Python rules; `__bool__` then `__len__` - **Functions:** - Defaults evaluated at definition time; `*args/**kwargs` - Decorators transform functions at definition time - **Operators and numbers:** - `/` true division; `//` floor division; big integers by default - Operator dispatch via dunder methods; `is` vs `==` For Phase 1, the must-haves are: LEGB + locals/freevars, default args timing, iterator-based `for`, `for/else` + `while/else`, Python truthiness and short-circuiting. ### Fallback Strategy - **Fallback unit: Per-function.** If a function body contains unsupported nodes, compile a "PyThunk" that calls into CPython. - **Boundary types:** Define canonical bridges: `PyAnyBox` in Nyash wrapping `Py` - **Imports and globals:** Execute module top-level in Python; then selectively replace functions ### pyo3/GIL Best Practices - **Initialization:** Call `pyo3::prepare_freethreaded_python()` once - **GIL usage:** - Use `Python::with_gil(|py| { ... })` for all Python calls - Minimize time under GIL; copy data out promptly - For heavy Rust work, drop GIL: `py.allow_threads(|| { ... })` - **Data transfer:** Prefer building JSON on the Python side - **Versioning:** Pin Python minor version; embed version string in the IR ### Phasing and Priorities (Refined) - **Phase 1 (Parser + Minimal Semantics):** - Python→JSON exporter with location info - Nyash IR for expressions and basic statements - Semantics fidelity for: iterator protocol, truthiness, scoping - **Fallback per-function for anything else** - **Phase 2-4:** Coverage expansion → Objects/Runtime → MIR/JIT ### Parser Bug Validation Strategy - **Differential execution:** Curate pairs of semantically equivalent snippets - **Oracle testing:** Run CPython as oracle and compare - **Fuzzing:** Grammar-based fuzzers - **Coverage and gating:** Track node-kind coverage ### IR/JSON Design Tips - Include: `node_type`, children, `lineno/col_offset`, `py_version`, `ast_format`, `support_level` - Canonicalize: normalize forms, operator names - Determinism: maintain stable field ordering ### Concrete Recommendations - **Pin to one Python minor (e.g., 3.11 or 3.12)** - **Choose per-function fallback as the core boundary** - **Implement Python truthiness, iterator protocol, and scoping correctly before optimizing** - **Keep the GIL minimal: build the JSON in Python; parse in Rust** - **Telemetry from day one: log unsupported node kinds and fallback counts** - **Start with JSON; plan migration to a compact binary once stable** --- ## 統合された重要ポイント ### 🎯 両エキスパートが一致した最重要事項 1. **関数単位のフォールバック戦略** - ファイル全体でなく関数レベルでコンパイル/フォールバックを切り替え - 未対応機能を含む関数はCPython exec、対応済み関数はNyash MIR/JIT 2. **Python バージョン固定** - Python 3.11または3.12に固定 - AST安定性の確保とバージョン間の差異回避 3. **意味論の正確な実装が最優先** - 最適化より先にPython互換性を確保 - 特に: イテレータプロトコル、真偽値判定、スコーピング規則 4. **GIL管理の最小化** - Python側でJSON生成、Rust側で解析 - 重い処理はGIL外で実行 5. **テレメトリーの重要性** - 未対応ノードの記録 - フォールバック率の計測 - 実行時の統計情報収集 ### 🚨 特に注意すべき意味論の違い 1. **制御フロー** - for/else, while/else の独特な挙動 - forループのイテレータプロトコル 2. **スコープ規則** - LEGB(Local, Enclosing, Global, Builtins) - global/nonlocal宣言 - 内包表記の独立スコープ(Python 3) 3. **数値演算** - / (true division) vs // (floor division) - デフォルトで大整数 - NaN の扱い 4. **関数定義** - デフォルト引数は定義時に評価 - *args/**kwargs の扱い - デコレータの実行順序 ### 📊 成功の測定指標 1. **カバレッジ**: コンパイル済み vs フォールバック関数の比率 2. **性能向上**: 数値計算ベンチマークでの改善率 3. **バグ発見数**: Differential Testingで発見されたバグ数 4. **エコシステム**: 動作する有名Pythonライブラリの数