docs(repl): record Phase 288.1 contract and results

更新内容:
- docs/reference/language/repl.md
  - Phase 288.1 完了ステータスに更新
  - Implementation Architecture セクション追加(AST rewrite 方式説明)
  - AST Rewriter の動作フロー追加(Variable/Assignment 変換ロジック)
  - ExternCall Bridge の仕組み追加(__repl.get/set → VMValue)
  - Expression Detection ロジック追加(wrapper AST 判定)
  - 動作例を完全に更新(全機能が動作済み)

- docs/development/current/main/phases/phase-288/README.md
  - Phase 288.1 完了セクション追加
  - 変更ファイル一覧(8ファイル, +592行)記録
  - 確認コマンド 4種 記録(変数永続化/式表示/_変数/リセット)
  - 回帰テスト結果記録(154/154 PASS)

- docs/development/current/main/10-Now.md
  - "Phase 288.1 完了" に更新
  - 次の候補(REPL UX 改善 / JoinIR 設計作業)を追記

- CURRENT_TASK.md
  - 1段落サマリー更新(288.1 完了、次の方向性)

Phase 288.1 成果(SSOT記録):
 変数永続化(session → eval bridge)
 式自動表示(pure expression auto-output)
 _ 変数(last displayed value)
 Fail-Fast 未定義エラー + ヒント
 セッションリセット(.reset)
 154/154 smoke tests PASS(file mode 不変)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-25 23:05:20 +09:00
parent 46fbe12ce6
commit 55d30c9845
4 changed files with 160 additions and 46 deletions

View File

@ -1,6 +1,6 @@
# REPL Mode (ReadEvalPrint Loop) — Specification (SSOT)
**Status**: Phase 288 P0P3 complete (2025-12-25, MVP) / Phase 288.1 next (session persistence + auto-display)
**Status**: Phase 288.1 complete (2025-12-25, Session Persistence + Auto-Display)
## Philosophy: Two Execution Contexts, One Language
@ -297,25 +297,53 @@ Recommended file-mode errors:
## 9) Phase 288 MVP Implementation Status
### Completed (Phase 288 P0-P3) - 2025-12-25
### Completed (Phase 288.1) - 2025-12-25
**CLI Entry** (`--repl` / `-i` flags)
**REPL Loop** (`.help`, `.exit`, `.reset` commands)
**ReplSessionBox** (VMValue-based session state)
**ReplSessionBox** (VMValue-based session state with `Rc<RefCell<>>`)
**Implicit Local Binding** (`x = 1` compiles without error)
**print() Output** (ExternCall output displays correctly)
**`_` Variable** (last value stored in session)
**Variable Persistence** (session variables accessible across lines via AST rewrite)
**Expression Auto-Display** (`1+1``2` automatic output)
**`_` Variable** (last displayed value accessible, Void not stored)
**Session Reset** (`.reset` command clears state)
**Main Box Entry Point** (VM execution fixed)
**Fail-Fast Undefined Variables** (clear error messages with hints)
### Deferred to Phase 288.1+
### Implementation Architecture (Phase 288.1)
**Variable Persistence** (session variables accessible across lines)
**Expression Auto-Display** (`1+1``2` automatic output)
**Complex Expression Detection** (distinguish expression vs statement)
**REPL Variable Injection** (session → compilation context bridge)
**AST Rewrite Approach**:
```
User Input: x = 42
Parse → AST (static box Main { main() { x = 42 } })
AST Rewriter (REPL-specific):
- Undeclared Variable: x → __repl.get("x")
- Undeclared Assignment: x = 42 → __repl.set("x", 42)
- Respects: local declarations, reserved names (me/true/false/null)
- Skips: nested scopes (function/method bodies), Main wrapper
Rewritten AST → MIR Compiler
ExternCall("__repl", "get"/"set", args)
VM Execution → ReplSessionBox.get/set
```
### Current Behavior (Phase 288 P3 MVP)
**Key Components**:
- **`src/runner/repl/ast_rewriter.rs`** (~430 lines): AST transformation
- **`src/mir/builder/calls/build.rs`**: `__repl.get/set` → ExternCall MIR lowering
- **`src/backend/mir_interpreter/handlers/externals.rs`**: ExternCall handlers
- **`Rc<RefCell<ReplSessionBox>>`**: Session sharing between REPL runner and VM
**Expression Detection**:
- Checks wrapper AST (`Main.main` body) for single expression node
- Pure expressions: Literal, BinaryOp, MethodCall, New, etc.
- Statements: Assignment, Local, Print, If, Loop, etc.
- Void values not displayed or stored in `_`
### Current Behavior (Phase 288.1)
```nyash
>>> .help
@ -325,33 +353,37 @@ Commands:
.help - Show this help
>>> x = 42
>>> # Silent (implicit local creation)
>>> # Silent (implicit local creation via __repl.set)
>>> print("Hello REPL!")
Hello REPL! # print() output displays
>>> print(42 + 1)
43 # Arithmetic works
>>> print(x)
42 # ✅ Variable persistence works!
>>> 1 + 1
>>> # Silent (expression auto-display in Phase 288.1)
2 # ✅ Expression auto-display works!
>>> _
2 # ✅ Last displayed value accessible
>>> print("Hello")
Hello # Statements don't auto-display
>>> .reset
Session reset
>>> print("After reset")
After reset # REPL continues after reset
>>> print(x)
Error: Runtime error: Invalid instruction: Undefined variable: 'x'
Hint: Variable not defined. Assign a value first.
>>> # ✅ Fail-Fast with clear error
```
**Known Limitations** (Phase 288 MVP):
- Variable persistence not yet implemented (`x = 1` then `print(x)` errors)
- `_` variable stored but not accessible in user code yet
- No expression auto-display (deferred to Phase 288.1)
**Design Philosophy**: Follow 80/20 rule - deliver working REPL first, polish UX later.
**Design Principles**:
- **AST Rewrite**: REPL-specific transformation, parser unchanged
- **ExternCall Bridge**: Clean VM/REPL separation
- **Fail-Fast**: No silent void, clear error messages
- **Box-First**: Complete isolation in `src/runner/repl/`
---
**Document Version**: Phase 288 P0 (2025-12-25)
**Implementation**: Phase 288 P1-P3 (CLI + Session + Basic UX)
**Next Phase**: Phase 288.1 (Expression Auto-Display)
**Document Version**: Phase 288.1 (2025-12-25)
**Implementation**: Complete (CLI + Session + Persistence + Auto-Display)
**Next**: Further UX improvements (multiline input, syntax highlighting, etc.)