docs(cf/pattern): add ControlFlowBuilder and PatternBuilder guides; reference from If/Match normalization; plan updated in CURRENT_TASK

This commit is contained in:
Selfhosting Dev
2025-09-20 01:15:26 +09:00
parent d185811541
commit c5ab95d082
4 changed files with 97 additions and 0 deletions

View File

@ -56,6 +56,7 @@ Next (short)
- Docs: enrich `docs/guides/loopform.md` with carrier examples and JSON builder snippets.
- If/Match normalization pass: canonical If join with single PHI group and Match→Ifchain (scrutinee once, guard fused), expression results via join var.
- ScopeBox (compile-time meta): design + docs; no-op macro scaffold; MIR hint names (no-op) and plan for zero-cost stripping.
- ControlFlowBuilder/PatternBuilder docs and scaffolding: author APIs for If/Match normalization and pattern conditions; migrate macros to use them.
Action Items (next 48h)
- [x] Enable sugar by default (array/map literals)
@ -64,6 +65,7 @@ Action Items (next 48h)
- [ ] LoopForm MVP2: twovars carrier safe normalization + tests/smokes
- [ ] LLVM PHI hygiene smoke on LoopForm cases
- [ ] ScopeBox docs + macro scaffold (no-op) + MIR hint type sketch
- [ ] ControlFlowBuilder/PatternBuilder docs本commitで追加→ スキャフォールド実装 → If/Matchマクロ置換の最初の1本
## Phase16 Outlook
- MacroCtx (gensym/report/getEnv) and capabilities mapped to `nyash.toml`.

View File

@ -0,0 +1,59 @@
# ControlFlowBuilder — If/Match 正規化ビルダー(コンパイル時メタ)
目的
- If/Match の正規化join 変数導入・scrutinee 一回評価・ガード合成・単一 PHI 群)を安全に一貫化するためのビルダー。
- 生成物は AST JSON v0 の文字列(マクロ内で使用)。実行時コストはゼロ。
設計原則
- 合流点は必ず join 変数gensymで収束空 PHI を避け、PHI は合流ブロック先頭へ誘導)。
- 条件/scrutinee は 1 回だけ評価してローカルへ束ねる。
- 構造は If/Match → If 連鎖(パターン条件は PatternBuilder で構築)。
想定 APIMVP
- if_stmt(cond_json, then_stmts_json[], else_stmts_json[]|null) -> If ノード文字列
- if_expr(cond_json, then_expr_json, else_expr_json, res_name) -> statements_json[]
- [ Local(res), If(assign res in both branches) ] を返す
- match_stmt(scrut_json, arms[]) -> statements_json[]
- match_expr(scrut_json, arms[], res_name) -> statements_json[]
- arms: [{ cond_json, guard_json|null, body_expr_json }]
- cond_json は PatternBuilder で組み立てるOR/型チェック等)
使用例(式 If
```
local JB = include "apps/lib/json_builder.nyash"
local CF = include "apps/lib/cf_builder.nyash" // 実装後に利用
local cond = JB.binary("<", JB.variable("a"), JB.variable("b"))
local then_e = JB.literal_int(10)
local else_e = JB.literal_int(20)
local stmts = CF.if_expr(cond, then_e, else_e, "__res0")
// stmts を元の位置にスプライスProgram 配列へ結合)
```
使用例(式 Match
```
local JB = include "apps/lib/json_builder.nyash"
local CF = include "apps/lib/cf_builder.nyash"
local PT = include "apps/lib/pattern_builder.nyash"
local scrut = JB.variable("x")
local c_small = PT.or_([ PT.eq(JB.literal_int(0)), PT.eq(JB.literal_int(1)) ])
local arms = [
{ cond_json: c_small, guard_json: JB.variable("small"), body_expr_json: JB.literal_string("small") },
{ cond_json: PT.type_is("IntegerBox", scrut), guard_json: null,
body_expr_json: /* toString(as_int(scrut)) を別途構築 */ JB.variable("n_to_string") },
{ cond_json: PT.default(), guard_json: null, body_expr_json: JB.literal_string("other") },
]
local stmts = CF.match_expr(scrut, arms, "__res1")
```
備考
- gensym は MacroCtx から受け取る想定CF 内では与えられた res_name/scrut_name を尊重)。
- 実装は JsonBuilder を用いてノード文字列を生成。
- 将来: cond/guard の短絡は既存の BinaryOp(&&,||) へ委譲し、PyVM/LLVM の規約に従う。
関連
- docs/guides/pattern-builder.md
- docs/guides/if-match-normalize.md
- docs/guides/loopform.md

View File

@ -4,6 +4,10 @@
- If と Match を“合流点が明確な制御フロー”へ正規化し、MIR/LLVM が一貫した PHI 生成を行えるようにする。
- ルールをシンプルに保ち、LoopFormキャリアと相性良く動作させる。
推奨ビルダー(コンパイル時メタ)
- ControlFlowBuilder: If/Match の正規化join 変数・If 連鎖生成)。
- PatternBuilder: パターン条件(==/OR/AND/型チェック/デフォルト)を構築。
適用タイミング
- マクロ前展開パスの中で、If/Match 正規化 →必要に応じてLoopForm 正規化の順で適用するのが基本。
- ループ本体に If/Match が含まれていても、If/Match 正規化は局所的に完結するLoopForm と独立に安全)。

View File

@ -0,0 +1,32 @@
# PatternBuilder — パターン条件ビルダー(コンパイル時メタ)
目的
- Match/If で用いるパターン条件を安全に構築するビルダー。
- 生成物は AST JSON v0 の条件式文字列BinaryOp/TypeOp 等で、ControlFlowBuilder に渡して If 連鎖へ合成する。
想定 APIMVP
- eq(expr_json) … scrut == expr
- or_(conds_json_array) … c1 || c2 || …
- and_(conds_json_array) … g1 && g2 &&
- type_is(type_name, scrut_json) … type_check(scrut, type_name)
- default() … デフォルト用マーカーCF側で末尾へ
使用例
```
local JB = include "apps/lib/json_builder.nyash"
local PT = include "apps/lib/pattern_builder.nyash"
local scrut = JB.variable("x")
local p = PT.or_([ PT.eq(JB.literal_int(0)), PT.eq(JB.literal_int(1)) ])
// guard 付き条件: (p) && small
local cond = PT.and_([ p, JB.variable("small") ])
```
注意
- default() は条件式ではないため、ControlFlowBuilder 側で最後の else に落とす処理を行う。
- 型チェックは TypeOp(check) の JSON を生成するか、既存の JSON 片を組み立てる(実装詳細に依存)。
関連
- docs/guides/controlflow-builder.md
- docs/guides/if-match-normalize.md