50 lines
1.7 KiB
Plaintext
50 lines
1.7 KiB
Plaintext
// PatternBuilder — パターン条件ビルダー(コンパイル時メタ)
|
||
// 生成物は AST JSON v0 の条件式(文字列)。
|
||
|
||
static box PatternBuilder {
|
||
// eq(lhs, rhs) => lhs == rhs
|
||
eq(lhs_json, rhs_json) {
|
||
using "apps/lib/json_builder.hako" as JB
|
||
return JB.binary("==", lhs_json, rhs_json)
|
||
}
|
||
|
||
// or_([c1, c2, ...]) => c1 || c2 || ... (空は false)
|
||
or_(conds) {
|
||
using "apps/lib/json_builder.hako" as JB
|
||
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) {
|
||
using "apps/lib/json_builder.hako" as JB
|
||
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)
|
||
// 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)。
|
||
type_is(type_name, expr_json) {
|
||
local t = "{\"kind\":\"Literal\",\"value\":{\"type\":\"string\",\"value\":\"" + type_name + "\"}}"
|
||
return "{\"kind\":\"MethodCall\",\"object\":" + expr_json + ",\"method\":\"is\",\"arguments\":[" + t + "]}"
|
||
}
|
||
|
||
// default マーカー(条件式ではない)。
|
||
default() { return "__NY_PATTERN_DEFAULT" }
|
||
}
|