macro libs: add ControlFlowBuilder (if/match normalization helpers) and PatternBuilder (eq/or/and/type_is/default) for compile-time AST building

This commit is contained in:
Selfhosting Dev
2025-09-20 01:19:50 +09:00
parent c5ab95d082
commit 7d22a931be
2 changed files with 154 additions and 0 deletions

99
apps/lib/cf_builder.nyash Normal file
View File

@ -0,0 +1,99 @@
// ControlFlowBuilder — If/Match 正規化ビルダー(コンパイル時メタ)
// 生成物は AST JSON v0 のノード文字列、またはステートメント配列(文字列配列)。
static box ControlFlowBuilder {
// If 文: then/else は配列(文ノード文字列)
if_stmt(cond_json, then_stmts, else_stmts) {
local JB = include "apps/lib/json_builder.nyash"
return JB.if_(cond_json, then_stmts, else_stmts)
}
// If 式: res_name へ代入して合流([Local(res), If(..)] の配列を返す)
if_expr(cond_json, then_expr_json, else_expr_json, res_name) {
local JB = include "apps/lib/json_builder.nyash"
local res_var = JB.variable(res_name)
local decl = JB.local([res_name], [null])
local then_s = [ JB.assignment(res_var, then_expr_json) ]
local else_s = [ JB.assignment(res_var, else_expr_json) ]
local ifn = JB.if_(cond_json, then_s, else_s)
return [ decl, ifn ]
}
// Match 式(簡易版): scrutinee を一度だけ評価し、res_name に収束。
// arms: array of { cond_json, guard_json|null, body_expr_json }。
// default アームは cond_json == "__NY_PATTERN_DEFAULT" で指定。
match_expr(scrut_json, arms, res_name) {
return this.match_expr_with_names(scrut_json, arms, res_name, "__ny_scrut")
}
match_expr_with_names(scrut_json, arms, res_name, scrut_name) {
local JB = include "apps/lib/json_builder.nyash"
local PT = include "apps/lib/pattern_builder.nyash"
// scrutinee を一度だけ評価
local decl_scrut = JB.local([scrut_name], [scrut_json])
local scrut_var = JB.variable(scrut_name)
// res の宣言
local res_decl = JB.local([res_name], [null])
local res_var = JB.variable(res_name)
// デフォルト(任意)を抽出
local normals = []
local default_arm = null
local i = 0
loop(i < arms.length()) {
local a = arms.get(i)
if a.cond_json == PT.default() {
default_arm = a
} else {
normals.push(a)
}
i = i + 1
}
// 連鎖 If を構築
function cond_fuse(c, g) {
if g == null { return c }
return PT.and_([c, g])
}
// 末尾から畳み込んで If 連鎖を組み立てる
local chain = null
i = normals.length() - 1
loop(i >= 0) {
local a = normals.get(i)
local cond_all = cond_fuse(a.cond_json, a.guard_json)
local then_s = [ JB.assignment(res_var, a.body_expr_json) ]
local else_s = null
if chain != null { else_s = [ chain ] }
local ifn = JB.if_(cond_all, then_s, else_s)
chain = ifn
i = i - 1
}
// デフォルト(無ければ null を代入して収束させる)
if default_arm != null {
if chain == null {
// 単一デフォルト
chain = JB.assignment(res_var, default_arm.body_expr_json)
// Assignment は文なので If で包む必要なし
chain = JB.if_(JB.literal_bool(true), [chain], null)
} else {
local def_s = [ JB.assignment(res_var, default_arm.body_expr_json) ]
chain = JB.if_(JB.literal_bool(true), [chain], def_s) // 常に true の If で else をぶら下げる
}
} else {
// デフォルトなし: 未マッチ経路を res=null へ収束
local set_null = JB.assignment(res_var, JB.literal_null())
if chain == null {
chain = JB.if_(JB.literal_bool(true), [set_null], null)
} else {
chain = JB.if_(JB.literal_bool(true), [chain], [set_null])
}
}
return [ decl_scrut, res_decl, chain ]
}
}

View File

@ -0,0 +1,55 @@
// PatternBuilder — パターン条件ビルダー(コンパイル時メタ)
// 生成物は AST JSON v0 の条件式(文字列)。
static box PatternBuilder {
// eq(lhs, rhs) => lhs == rhs
eq(lhs_json, rhs_json) {
local JB = include "apps/lib/json_builder.nyash"
return JB.binary("==", lhs_json, rhs_json)
}
// or_([c1, c2, ...]) => c1 || c2 || ... (空は false
or_(conds) {
local JB = include "apps/lib/json_builder.nyash"
if conds.length() == 0 { return JB.literal_bool(false) }
if conds.length() == 1 { return conds.get(0) }
local i = 1
local acc = conds.get(0)
loop(i < conds.length()) {
acc = JB.binary("||", acc, conds.get(i))
i = i + 1
}
return acc
}
// and_([g1, g2, ...]) => g1 && g2 && ... (空は true
and_(conds) {
local JB = include "apps/lib/json_builder.nyash"
if conds.length() == 0 { return JB.literal_bool(true) }
if conds.length() == 1 { return conds.get(0) }
local i = 1
local acc = conds.get(0)
loop(i < conds.length()) {
acc = JB.binary("&&", acc, conds.get(i))
i = i + 1
}
return acc
}
// type_is(type_name, expr_json) — MVP: 呼び出し側で適宜拡張。
// ここでは簡易に eq(typeof(expr), type_name) 相当のプレースホルダを返すか、
// プロジェクトの TypeOp 実装に合わせて後で差し替える。
type_is(type_name, expr_json) {
// プレースホルダ(将来の実装点): 常に true にせず、明示的に比較形を構築するのが安全。
// ユーザー側の Lower/Resolver が未対応なら、後で本関数を差し替える。
local JB = include "apps/lib/json_builder.nyash"
// 仮: Call 形式を使わず、ダミーの比較を残す("__ny_type(expr) == type_name" 的イメージ)。
// 実際の採用時は TypeOp(check) の AST 形へ置換する。
local left = JB.binary("__typeof", expr_json, JB.literal_null()) // ダミー演算子(後で置換)
return JB.binary("==", left, JB.literal_string(type_name))
}
// default マーカー(条件式ではない)。
default() { return "__NY_PATTERN_DEFAULT" }
}