2025-09-20 01:19:50 +09:00
|
|
|
|
// 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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 02:20:02 +09:00
|
|
|
|
// type_is(type_name, expr_json)
|
|
|
|
|
|
// Lowering 規約: MethodCall(object=expr, method="is", arguments=[Literal(String type_name)])
|
|
|
|
|
|
// → MIR::TypeOp(Check, value=expr, ty=map(type_name)) に降下される(src/mir/builder/exprs.rs)。
|
2025-09-20 01:19:50 +09:00
|
|
|
|
type_is(type_name, expr_json) {
|
2025-09-20 02:20:02 +09:00
|
|
|
|
local t = "{\"kind\":\"Literal\",\"value\":{\"type\":\"string\",\"value\":\"" + type_name + "\"}}"
|
|
|
|
|
|
return "{\"kind\":\"MethodCall\",\"object\":" + expr_json + ",\"method\":\"is\",\"arguments\":[" + t + "]}"
|
2025-09-20 01:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// default マーカー(条件式ではない)。
|
|
|
|
|
|
default() { return "__NY_PATTERN_DEFAULT" }
|
|
|
|
|
|
}
|