behavior-contracts 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +430 -115
- package/cli-contract.yaml +49 -51
- package/dist/authoring.d.ts +61 -424
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +53 -1157
- package/dist/authoring.js.map +1 -1
- package/dist/behavior.d.ts +7 -15
- package/dist/behavior.d.ts.map +1 -1
- package/dist/behavior.js.map +1 -1
- package/dist/builder.d.ts +4 -4
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +9 -11
- package/dist/builder.js.map +1 -1
- package/dist/cli.js +5 -5
- package/dist/cli.js.map +1 -1
- package/dist/commands/codegen.d.ts.map +1 -1
- package/dist/commands/codegen.js +12 -56
- package/dist/commands/codegen.js.map +1 -1
- package/dist/commands/ts-compile.d.ts +23 -0
- package/dist/commands/ts-compile.d.ts.map +1 -0
- package/dist/commands/ts-compile.js +724 -0
- package/dist/commands/ts-compile.js.map +1 -0
- package/dist/commands/ts-extract.d.ts +67 -1
- package/dist/commands/ts-extract.d.ts.map +1 -1
- package/dist/commands/ts-extract.js +162 -31
- package/dist/commands/ts-extract.js.map +1 -1
- package/dist/generated/contract.d.ts.map +1 -1
- package/dist/generated/contract.js +2 -2
- package/dist/generated/contract.js.map +1 -1
- package/dist/generated/program.js +4 -4
- package/dist/generated/program.js.map +1 -1
- package/dist/generator/core.d.ts +11 -1
- package/dist/generator/core.d.ts.map +1 -1
- package/dist/generator/core.js +5 -4
- package/dist/generator/core.js.map +1 -1
- package/dist/generator/emit-php.d.ts +0 -17
- package/dist/generator/emit-php.d.ts.map +1 -1
- package/dist/generator/emit-php.js +25 -1
- package/dist/generator/emit-php.js.map +1 -1
- package/dist/generator/emit-python.d.ts +0 -7
- package/dist/generator/emit-python.d.ts.map +1 -1
- package/dist/generator/emit-python.js +34 -1
- package/dist/generator/emit-python.js.map +1 -1
- package/dist/generator/emit-straightline-typed-typescript.js +1 -1
- package/dist/generator/emit-straightline-typed-typescript.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +110 -107
- package/dist/generator/emit-typed-native-go.js.map +1 -1
- package/dist/generator/emit-typed-native-rust.d.ts +3 -2
- package/dist/generator/emit-typed-native-rust.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-rust.js +62 -126
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/dist/generator/emit-typescript-native.js +1 -1
- package/dist/generator/emit-typescript-native.js.map +1 -1
- package/dist/generator/emit-typescript.js +1 -1
- package/dist/generator/emit-typescript.js.map +1 -1
- package/dist/generator/typed.d.ts +5 -1
- package/dist/generator/typed.d.ts.map +1 -1
- package/dist/generator/typed.js +40 -9
- package/dist/generator/typed.js.map +1 -1
- package/dist/guard.d.ts +4 -0
- package/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +26 -8
- package/dist/guard.js.map +1 -1
- package/dist/index.d.ts +19 -55
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -87
- package/dist/index.js.map +1 -1
- package/dist/lowering.d.ts +251 -0
- package/dist/lowering.d.ts.map +1 -0
- package/dist/lowering.js +652 -0
- package/dist/lowering.js.map +1 -0
- package/dist/provenance.js +4 -4
- package/dist/provenance.js.map +1 -1
- package/dist/runtime.d.ts +52 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +77 -0
- package/dist/runtime.js.map +1 -0
- package/dist/spec-versions.d.ts +25 -0
- package/dist/spec-versions.d.ts.map +1 -0
- package/dist/spec-versions.js +26 -0
- package/dist/spec-versions.js.map +1 -0
- package/dist/type-gate.d.ts +12 -0
- package/dist/type-gate.d.ts.map +1 -1
- package/dist/type-gate.js +79 -3
- package/dist/type-gate.js.map +1 -1
- package/package.json +5 -1
- package/dist/bind.d.ts +0 -64
- package/dist/bind.d.ts.map +0 -1
- package/dist/bind.js +0 -131
- package/dist/bind.js.map +0 -1
package/dist/lowering.js
ADDED
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { buildComponentCore, checkPortableEntry, checkPortNames, checkDeclaredPortTypes, collectNodeIdRefs, DEFAULT_PLAN_CONCURRENCY, } from "./builder.js";
|
|
2
|
+
import { assertAllNodeTypesDetermined, canonicalizeType } from "./type-gate.js";
|
|
3
|
+
import { assertPortableTypeNotation, PortabilityError } from "./guard.js";
|
|
4
|
+
export class AuthoringFailure extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.name = "AuthoringFailure";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function afail(code, message) {
|
|
13
|
+
throw new AuthoringFailure(code, message);
|
|
14
|
+
}
|
|
15
|
+
// ── 式の抽象値(reader 共通)────────────────────────────────────────────────────
|
|
16
|
+
/**
|
|
17
|
+
* EXPR_META — 本体中の「式」を表す抽象値の鍵。recorder reader は Proxy に、AST reader は
|
|
18
|
+
* 素のオブジェクトにこの鍵で {@link ExprMeta} を載せる。以降の lowering({@link lowerValue} 他)は
|
|
19
|
+
* **carrier の実装を問わず** meta だけを読むため、両 reader は同じ規則を通る。
|
|
20
|
+
*/
|
|
21
|
+
export const EXPR_META = /* @__PURE__ */ Symbol("behavior-contracts.authoring.expr");
|
|
22
|
+
/** 式の抽象値なら ExprMeta を返す。 */
|
|
23
|
+
export function exprMetaOf(v) {
|
|
24
|
+
if (v === null || (typeof v !== "object" && typeof v !== "function"))
|
|
25
|
+
return undefined;
|
|
26
|
+
return v[EXPR_META];
|
|
27
|
+
}
|
|
28
|
+
/** carrier 非依存の抽象値(AST reader が使う素のオブジェクト carrier)。 */
|
|
29
|
+
export function plainExpr(meta) {
|
|
30
|
+
return { [EXPR_META]: meta };
|
|
31
|
+
}
|
|
32
|
+
/** 演算結果(ref path を持たない)の meta。 */
|
|
33
|
+
export function opMeta(node) {
|
|
34
|
+
return { node, refPath: null, refOp: "ref", isInput: false };
|
|
35
|
+
}
|
|
36
|
+
/** ノード結果(`{ref:[<nodeId>]}` root。プロパティアクセスで path 延長可能)の meta。 */
|
|
37
|
+
export function nodeMeta(id) {
|
|
38
|
+
return { node: { ref: [id] }, refPath: [id], refOp: "ref", isInput: false };
|
|
39
|
+
}
|
|
40
|
+
/** `$`(Input root)の meta。 */
|
|
41
|
+
export function inputRootMeta() {
|
|
42
|
+
return { node: { ref: [] }, refPath: [], refOp: "ref", isInput: true };
|
|
43
|
+
}
|
|
44
|
+
/** 名前付き入力 port(#191 新形: 各引数=port。`{ref:[name]}` root・isInput)の meta。`opt(name)` が
|
|
45
|
+
* `markInputPortOptional` を発火できるよう isInput:true・refPath 単一セグメントにする(`$.name` と同像)。 */
|
|
46
|
+
export function inputPortMeta(name) {
|
|
47
|
+
return { node: { ref: [name] }, refPath: [name], refOp: "ref", isInput: true };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* プロパティアクセス(`$.email` / `row.tagRefs`)を 1 段 lower する。ref path を持たない
|
|
51
|
+
* (演算結果)値へのアクセスは注記先が無いため fail-closed。
|
|
52
|
+
*/
|
|
53
|
+
export function lowerMember(f, meta, prop) {
|
|
54
|
+
if (meta.refPath === null)
|
|
55
|
+
afail("COMPUTED_ACCESS", `property '.${prop}' on a computed expression cannot be lowered (bind the value through a component result or restructure the wiring)`);
|
|
56
|
+
// `xs.length` は property アクセスではなく Expression IR の `len` operator へ lower する
|
|
57
|
+
// (expression-ir.md §3: `xs.length` → `{len:[xs]}`・配列のみ。`{ref:[…,"length"]}` に化かすと
|
|
58
|
+
// len の意味が黙って落ちる — その silent-wrong を塞ぐ)。配列/文字列の判別は型層の len operand 制約
|
|
59
|
+
// (arr のみ・type-gate)が reader 非依存に enforce する。`opt(...).length` も同じ len に落とす。
|
|
60
|
+
if (prop === "length")
|
|
61
|
+
return opMeta({ len: [meta.node] });
|
|
62
|
+
if (meta.isInput && meta.refPath.length === 0)
|
|
63
|
+
declareInputPort(f, prop);
|
|
64
|
+
const path = [...meta.refPath, prop];
|
|
65
|
+
return { node: { [meta.refOp]: path }, refPath: path, refOp: meta.refOp, isInput: meta.isInput };
|
|
66
|
+
}
|
|
67
|
+
// ── 値 → Expression IR lowering ────────────────────────────────────────────────
|
|
68
|
+
/**
|
|
69
|
+
* 本体に現れた値を Expression IR へ lower する。
|
|
70
|
+
* 抽象値はその IR ノードに、リテラルはそのまま(配列 → `{arr:[...]}`、オブジェクト →
|
|
71
|
+
* `{obj:{...}}`、bigint → `{int:"..."}`)に落ちる。関数 / Date / Promise / undefined は
|
|
72
|
+
* Portability 違反として loud に失敗する(fail-closed)。
|
|
73
|
+
*/
|
|
74
|
+
export function lowerValue(v, at) {
|
|
75
|
+
const meta = exprMetaOf(v);
|
|
76
|
+
if (meta) {
|
|
77
|
+
if (meta.refPath !== null && meta.refPath.length === 0)
|
|
78
|
+
afail("WHOLE_INPUT", `${at}: the whole '$' input cannot be wired; reference a field of it`);
|
|
79
|
+
return meta.node;
|
|
80
|
+
}
|
|
81
|
+
if (v === null)
|
|
82
|
+
return null;
|
|
83
|
+
switch (typeof v) {
|
|
84
|
+
case "boolean":
|
|
85
|
+
case "string":
|
|
86
|
+
return v;
|
|
87
|
+
case "number":
|
|
88
|
+
if (Number.isInteger(v) && !Number.isSafeInteger(v))
|
|
89
|
+
afail("BAD_PORT_VALUE", `${at}: integral literal ${v} exceeds the safe range (use a bigint literal)`);
|
|
90
|
+
if (!Number.isFinite(v))
|
|
91
|
+
afail("BAD_PORT_VALUE", `${at}: non-finite number literal`);
|
|
92
|
+
return v;
|
|
93
|
+
case "bigint":
|
|
94
|
+
return { int: v.toString() };
|
|
95
|
+
case "undefined":
|
|
96
|
+
afail("BAD_PORT_VALUE", `${at}: undefined cannot be wired`);
|
|
97
|
+
break;
|
|
98
|
+
case "function":
|
|
99
|
+
afail("BAD_PORT_VALUE", `${at}: functions cannot be placed in IR (Portability Guard)`);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
if (Array.isArray(v))
|
|
103
|
+
return { arr: v.map((e, i) => lowerValue(e, `${at}[${i}]`)) };
|
|
104
|
+
if (v instanceof Date || v instanceof Promise)
|
|
105
|
+
afail("BAD_PORT_VALUE", `${at}: ${v.constructor.name} cannot be placed in IR (Portability Guard)`);
|
|
106
|
+
if (typeof v === "object") {
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const k of Object.keys(v)) {
|
|
109
|
+
if (k === "__proto__")
|
|
110
|
+
afail("BAD_PORT_VALUE", `${at}: object key "__proto__" is forbidden (fail-closed)`);
|
|
111
|
+
out[k] = lowerValue(v[k], `${at}.${k}`);
|
|
112
|
+
}
|
|
113
|
+
return { obj: out };
|
|
114
|
+
}
|
|
115
|
+
afail("BAD_PORT_VALUE", `${at}: value cannot be wired`);
|
|
116
|
+
}
|
|
117
|
+
// ── Expression 演算子(expression-ir.md §3 の閉集合と 1:1)──────────────────────────
|
|
118
|
+
/** 2 項演算子の閉集合(両 reader が読む唯一の語彙表)。 */
|
|
119
|
+
export const BINARY_EXPR_OPS = [
|
|
120
|
+
"add", "sub", "mul", "div", "mod",
|
|
121
|
+
"eq", "ne", "lt", "le", "gt", "ge",
|
|
122
|
+
"and", "or", "coalesce",
|
|
123
|
+
];
|
|
124
|
+
/** 単項演算子の閉集合。 */
|
|
125
|
+
export const UNARY_EXPR_OPS = ["neg", "not", "len"];
|
|
126
|
+
export function lowerBinOp(op, a, b) {
|
|
127
|
+
return opMeta({ [op]: [lowerValue(a, op), lowerValue(b, op)] });
|
|
128
|
+
}
|
|
129
|
+
export function lowerUnOp(op, a) {
|
|
130
|
+
return opMeta({ [op]: [lowerValue(a, op)] });
|
|
131
|
+
}
|
|
132
|
+
/** `+`(文字列連結)。n-ary(min 2)。 */
|
|
133
|
+
export function lowerConcat(parts) {
|
|
134
|
+
if (parts.length < 2)
|
|
135
|
+
afail("BAD_PORT_VALUE", `concat expects >= 2 args, got ${parts.length}`);
|
|
136
|
+
return opMeta({ concat: parts.map((p, i) => lowerValue(p, `concat[${i}]`)) });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* `opt(x)` — `?.`(optional chaining)の lowering。ref を refOpt に変換する
|
|
140
|
+
* (以降のプロパティアクセスは中間 null で null を返す — expression-ir.md `refOpt`)。
|
|
141
|
+
*
|
|
142
|
+
* `opt($.x)` は「その input Port は省略可」という**宣言**でもある。宣言(inputPorts)が
|
|
143
|
+
* optionality の SSoT(runBehavior の束縛規則も native lowering もそこだけを読む)なので、
|
|
144
|
+
* ここでその SSoT を実際に立てる — 立てなければ「式は refOpt・宣言は required:true」という
|
|
145
|
+
* 矛盾した 2 枚の carrier が残り、宣言だけを読む下流は optionality を見失う。
|
|
146
|
+
*
|
|
147
|
+
* 単一セグメント(`opt($.x)`)のときだけ head を optional にする。多段(`opt($.user.name)`)は
|
|
148
|
+
* 「途中/末端が null を取り得る」の宣言であって head Port 自体の省略可性ではないため触らない。
|
|
149
|
+
*/
|
|
150
|
+
export function lowerOpt(f, x) {
|
|
151
|
+
const meta = exprMetaOf(x);
|
|
152
|
+
if (!meta || meta.refPath === null || meta.refPath.length === 0)
|
|
153
|
+
afail("BAD_PORT_VALUE", "opt(...) expects a reference (a recorded '$' field or node result)");
|
|
154
|
+
if (meta.isInput && meta.refPath.length === 1)
|
|
155
|
+
markInputPortOptional(f, meta.refPath[0]);
|
|
156
|
+
return { node: { refOpt: meta.refPath }, refPath: meta.refPath, refOp: "refOpt", isInput: meta.isInput };
|
|
157
|
+
}
|
|
158
|
+
// ── `.map` / `refsConnection` の opts(runtime-enumerable SoT)────────────────────────
|
|
159
|
+
/**
|
|
160
|
+
* `.map` の opts キーの閉集合(**runtime-enumerable SoT**)。{@link MapOptions} の型はここから
|
|
161
|
+
* 導出し、新 opts キーを足すときはここに 1 語追加するのが正。matrix の網羅ゲートはこの配列から
|
|
162
|
+
* 導出するので、SoT にキーを足して matrix にケースが無ければ CI が RED になる。
|
|
163
|
+
* 注: 要素ごと skip の `when` は opts キーではなく callback 内 `when(...)` guard 由来
|
|
164
|
+
* (`map.when` へ lower)なので、ここには含めない。
|
|
165
|
+
*/
|
|
166
|
+
export const MAP_OPTION_KEYS = ["into", "batched"];
|
|
167
|
+
/**
|
|
168
|
+
* `refsConnection`(v3 fanout)の opts キーの閉集合(**runtime-enumerable SoT**)。
|
|
169
|
+
* {@link FanoutOptions} の型はここから導出。新 opts キーはここに追加するのが正。
|
|
170
|
+
*/
|
|
171
|
+
export const FANOUT_OPTION_KEYS = ["dedupeKey", "drop", "implicitSource"];
|
|
172
|
+
const _mapOptsSync = true;
|
|
173
|
+
const _fanoutOptsSync = true;
|
|
174
|
+
void _mapOptsSync;
|
|
175
|
+
void _fanoutOptsSync;
|
|
176
|
+
export function newGraphFrame(method) {
|
|
177
|
+
return {
|
|
178
|
+
method,
|
|
179
|
+
nodes: [],
|
|
180
|
+
nodeIds: new Set(),
|
|
181
|
+
inputPorts: new Map(),
|
|
182
|
+
optionalInputs: new Set(),
|
|
183
|
+
seq: 0,
|
|
184
|
+
mapSeq: 0,
|
|
185
|
+
branchStack: [],
|
|
186
|
+
pendingElement: null,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export function declareInputPort(f, name) {
|
|
190
|
+
if (!f.inputPorts.has(name))
|
|
191
|
+
f.inputPorts.set(name, { type: "unknown", required: true });
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* markInputPortOptional — `opt($.x)` が宣言する「Port x は省略可」を schema に立てる
|
|
195
|
+
* (`required:false`)。`$.x` アクセス時点で {@link declareInputPort} が required で登録済みなので、
|
|
196
|
+
* ここは既存 schema の更新。型(`type`)には触らない(型は `inputPorts` 上書きの担当)。
|
|
197
|
+
*/
|
|
198
|
+
export function markInputPortOptional(f, name) {
|
|
199
|
+
const cur = f.inputPorts.get(name);
|
|
200
|
+
if (cur !== undefined)
|
|
201
|
+
f.inputPorts.set(name, { ...cur, required: false });
|
|
202
|
+
f.optionalInputs.add(name);
|
|
203
|
+
}
|
|
204
|
+
function nextId(f) {
|
|
205
|
+
const id = `n${f.seq++}`;
|
|
206
|
+
f.nodeIds.add(id);
|
|
207
|
+
return id;
|
|
208
|
+
}
|
|
209
|
+
/** 分岐スタックの条件を `and` 連鎖に合成する(gate の `if`)。 */
|
|
210
|
+
function composedCond(f) {
|
|
211
|
+
const conds = f.branchStack.map((b) => b.cond);
|
|
212
|
+
return conds.reduce((acc, c) => (acc === undefined ? c : { and: [acc, c] }), undefined);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 分岐内に Component が初めて現れた時点で gate(CondNode)を遅延生成する。
|
|
216
|
+
* gate は条件成立時 `{ok:true}` / 不成立時 `null` を返し、分岐内ノードは
|
|
217
|
+
* `parent=gate, bindField:"ok"` の null-binding skip(execution-plan.md)で実行選択される。
|
|
218
|
+
*/
|
|
219
|
+
function ensureGate(f) {
|
|
220
|
+
const bf = f.branchStack[f.branchStack.length - 1];
|
|
221
|
+
if (!bf)
|
|
222
|
+
return null;
|
|
223
|
+
if (f.branchStack.length > 1)
|
|
224
|
+
afail("NESTED_BRANCH", `method '${f.method}': a conditional branch containing components cannot nest inside another branch (authoring v1; flatten to sibling gates)`);
|
|
225
|
+
if (bf.gateId === null) {
|
|
226
|
+
const id = nextId(f);
|
|
227
|
+
const gate = { id, cond: { if: composedCond(f), then: { obj: { ok: true } }, else: null } };
|
|
228
|
+
f.nodes.push(gate);
|
|
229
|
+
bf.gateId = id;
|
|
230
|
+
}
|
|
231
|
+
return bf;
|
|
232
|
+
}
|
|
233
|
+
/** ports の deps から parent(Wire)を決める: in-branch データ親 > gate > 先頭データ親。 */
|
|
234
|
+
function chooseParent(f, deps, bf) {
|
|
235
|
+
if (bf) {
|
|
236
|
+
const inBranch = deps.find((d) => {
|
|
237
|
+
const idx = f.nodes.findIndex((n) => n.id === d);
|
|
238
|
+
return idx >= bf.startLen;
|
|
239
|
+
});
|
|
240
|
+
if (inBranch !== undefined)
|
|
241
|
+
return { parent: inBranch }; // gate skip は親経由で伝播する
|
|
242
|
+
return { parent: bf.gateId, bindField: "ok" };
|
|
243
|
+
}
|
|
244
|
+
if (deps.length > 0)
|
|
245
|
+
return { parent: deps[0] };
|
|
246
|
+
return {};
|
|
247
|
+
}
|
|
248
|
+
// ── catalog エントリ由来の型導出 ─────────────────────────────────────────────────
|
|
249
|
+
/**
|
|
250
|
+
* derivePortSchemas — node の `portSchemas`(irVersion 2)を機械導出する **唯一の SSoT**。
|
|
251
|
+
* `node.outType`(出力型契約)の入力側の双子で、port ごとの型契約を焼く(native emitter の port
|
|
252
|
+
* 境界がこれを honor し要素 kind 推論を廃す)。各 port の契約は leaf の宣言 `PortSchema` そのもの
|
|
253
|
+
* (`entry.inputPorts[k]`)。未宣言 port は `checkPortNames` が上流で fail-closed に拒否するので、
|
|
254
|
+
* ここへは宣言済み port だけが届く(`{ type: "value" }` は宣言欠落時の不透明 default)。
|
|
255
|
+
*/
|
|
256
|
+
export function derivePortSchemas(entry, ports) {
|
|
257
|
+
const out = {};
|
|
258
|
+
for (const k of Object.keys(ports))
|
|
259
|
+
out[k] = entry.inputPorts[k] ?? { type: "value" };
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* baseOutTypeFromEntry — catalog エントリの base ノード結果型を導出する。`elemType` は leaf の
|
|
264
|
+
* **要素型**なので、ノード結果の確定型は `output.shape` で包む:
|
|
265
|
+
* - `item`(既定)→ 要素型そのもの。
|
|
266
|
+
* - `items` → `{arr: 要素型}`(リスト結果)。
|
|
267
|
+
* - `connection` → `{obj:{items:{arr:要素型}, cursor:{opt:"string"}}}`(connection 結果)。
|
|
268
|
+
* `elemType` が無いエントリ(手書き catalog 等)は undefined(=未確定 → ゲートが UNTYPED_NODE)。
|
|
269
|
+
*/
|
|
270
|
+
function baseOutTypeFromEntry(entry) {
|
|
271
|
+
if (entry.elemType === undefined)
|
|
272
|
+
return undefined;
|
|
273
|
+
const shape = entry.output?.shape ?? "item";
|
|
274
|
+
if (shape === "items")
|
|
275
|
+
return { arr: entry.elemType };
|
|
276
|
+
if (shape === "connection")
|
|
277
|
+
return { obj: { items: { arr: entry.elemType }, cursor: { opt: "string" } } };
|
|
278
|
+
return entry.elemType; // item(単一)
|
|
279
|
+
}
|
|
280
|
+
// ── leaf 呼び(componentRef ノード / map・fanout 要素の capture)──────────────────────
|
|
281
|
+
/**
|
|
282
|
+
* lowerLeafCall — catalog leaf 1 呼び出しの lowering(両 reader 共通の唯一の実装)。
|
|
283
|
+
*
|
|
284
|
+
* ports は catalog の Port schema に対して build-time 検証され(未知 Port / required 欠落 /
|
|
285
|
+
* 宣言 Port へのスカラリテラル型不一致は fail-closed)、Expression IR へ lower される。
|
|
286
|
+
* map/fanout コールバック内の呼びは body ノードを作らず、単一 Map/Fanout ノードへ畳むために
|
|
287
|
+
* pendingElement へ capture する(戻り値 `null`)。それ以外は componentRef ノードを 1 つ生成し、
|
|
288
|
+
* その id を返す。
|
|
289
|
+
*/
|
|
290
|
+
export function lowerLeafCall(f, name, entry, portsIn) {
|
|
291
|
+
checkPortableEntry(name, entry, afail);
|
|
292
|
+
if (!portsIn || typeof portsIn !== "object" || Array.isArray(portsIn) || exprMetaOf(portsIn))
|
|
293
|
+
afail("BAD_PORT_VALUE", `component '${name}': ports must be a plain object literal`);
|
|
294
|
+
checkPortNames(name, entry, Object.keys(portsIn), afail);
|
|
295
|
+
const ports = {};
|
|
296
|
+
for (const k of Object.keys(portsIn))
|
|
297
|
+
ports[k] = lowerValue(portsIn[k], `component '${name}' port '${k}'`);
|
|
298
|
+
checkDeclaredPortTypes(name, entry, ports, afail);
|
|
299
|
+
// map/fanout コールバック内 → 単一 Map/Fanout ノードの component/ports として capture。
|
|
300
|
+
if (f.pendingElement) {
|
|
301
|
+
if (f.pendingElement.component !== null)
|
|
302
|
+
afail("MAP_MULTI_COMPONENT", `method '${f.method}': a .map callback must invoke exactly one component (got a second call: '${name}')`);
|
|
303
|
+
f.pendingElement.component = name;
|
|
304
|
+
f.pendingElement.entry = entry;
|
|
305
|
+
f.pendingElement.ports = ports;
|
|
306
|
+
f.pendingElement.baseElemType = entry.elemType;
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
const bf = ensureGate(f);
|
|
310
|
+
const deps = collectNodeIdRefs(ports, f.nodeIds);
|
|
311
|
+
const id = nextId(f);
|
|
312
|
+
const wire = chooseParent(f, deps, bf);
|
|
313
|
+
const node = { id, component: name, ports };
|
|
314
|
+
if (wire.parent !== undefined)
|
|
315
|
+
node.parent = wire.parent;
|
|
316
|
+
if (wire.bindField !== undefined)
|
|
317
|
+
node.bindField = wire.bindField;
|
|
318
|
+
// base 出力型を leaf の catalog `elemType` からノードへ焼く(leaf の base 型は signature 由来 = 確定)。
|
|
319
|
+
// projection 等で型が変わる点は後段の `.as<T>()` が上書きする。catalog に elemType が無いノードは
|
|
320
|
+
// outType 未確定のまま残り、全ノード型確定ゲート(type-gate.ts)が `UNTYPED_NODE` で名指して落とす。
|
|
321
|
+
const baseOut = baseOutTypeFromEntry(entry);
|
|
322
|
+
if (baseOut !== undefined)
|
|
323
|
+
node.outType = baseOut;
|
|
324
|
+
node.portSchemas = derivePortSchemas(entry, ports);
|
|
325
|
+
if (entry.leafSymbol !== undefined)
|
|
326
|
+
node.leafSymbol = entry.leafSymbol;
|
|
327
|
+
f.nodes.push(node);
|
|
328
|
+
return id;
|
|
329
|
+
}
|
|
330
|
+
// ── `.as<T>(t)`(inline 確定型注記)────────────────────────────────────────────────
|
|
331
|
+
/**
|
|
332
|
+
* lowerAs — `.as<T>(t)` を lower する。projection / 合成で base 型(leaf signature 由来)から
|
|
333
|
+
* **確定型が変わる点**を consumer が inline で確定させる surface。
|
|
334
|
+
*
|
|
335
|
+
* TS の型は実行時に消去されるため BC core は**推論しない**: `.as` は runtime `PortableType`
|
|
336
|
+
* (可搬型記法の閉集合)だけを読み、それを対象 body ノードの `outType` に書き込む。
|
|
337
|
+
*
|
|
338
|
+
* `.as` は **node 結果(componentRef / map / fanout の結果)** にのみ適用できる。`$` 入力参照・
|
|
339
|
+
* 演算結果は body ノードを持たず注記先が無いため `BAD_AS_TARGET` で fail-closed。map/fanout
|
|
340
|
+
* コールバック内の leaf 結果への `.as` は要素の確定型として pendingElement に記録し、畳まれた
|
|
341
|
+
* MapNode/FanoutNode の `outType` に載せる。
|
|
342
|
+
*/
|
|
343
|
+
export function lowerAs(f, meta, t) {
|
|
344
|
+
// runtime PortableType の閉集合検査(guard と同一強度・fail-closed)。TS の型消去後に core が
|
|
345
|
+
// 読むのはこの runtime 注記だけ(推論しない)。
|
|
346
|
+
try {
|
|
347
|
+
assertPortableTypeNotation(t, `.as`);
|
|
348
|
+
}
|
|
349
|
+
catch (e) {
|
|
350
|
+
if (e instanceof PortabilityError)
|
|
351
|
+
afail("BAD_TYPE_NOTATION", `method '${f.method}': .as<T>(t) — ${e.message}`);
|
|
352
|
+
throw e;
|
|
353
|
+
}
|
|
354
|
+
if (meta.mapResult && f.pendingElement) {
|
|
355
|
+
f.pendingElement.outType = t;
|
|
356
|
+
return meta; // 同じ要素結果(callback 内でさらに配線はしない)。
|
|
357
|
+
}
|
|
358
|
+
const id = meta.refPath !== null && meta.refPath.length === 1 ? meta.refPath[0] : null;
|
|
359
|
+
const node = id !== null ? f.nodes.find((n) => n.id === id) : undefined;
|
|
360
|
+
if (!node)
|
|
361
|
+
afail("BAD_AS_TARGET", `method '${f.method}': .as<T>(t) can only annotate a component/map/fanout result node (not a '$' input reference, a projected field, or a computed expression)`);
|
|
362
|
+
node.outType = t;
|
|
363
|
+
return meta;
|
|
364
|
+
}
|
|
365
|
+
// ── structured control: Map ─────────────────────────────────────────────────────
|
|
366
|
+
/**
|
|
367
|
+
* lowerMap — `xs.map(el => Leaf({...}), opts?)` を単一 MapNode へ lower する。
|
|
368
|
+
* `runCallback` は reader が供給する「コールバック本体の読み取り」(recorder は JS closure 呼び出し、
|
|
369
|
+
* AST reader は arrow 本体の静的評価)。lowering 規則(capture・畳み・outType・parent)は共有。
|
|
370
|
+
*/
|
|
371
|
+
/** `into`-map の augment 済み要素型 = source 要素型 ⊕ `{ <into>: leaf 結果型 }`(applyInto の value
|
|
372
|
+
* zip と同一構造を型で導く。source は over 配列の要素型・leaf 結果は catalog `elemType`=どちらも宣言由来
|
|
373
|
+
* で推論でない)。source が obj でなければ loud reject(into は object 要素を要求)。leaf 要素型が未確定なら
|
|
374
|
+
* `undefined`(=型ゲートが UNTYPED_NODE で拾う)。 */
|
|
375
|
+
function intoElemType(f, sourceElem, intoKey, leafElem) {
|
|
376
|
+
if (leafElem === undefined)
|
|
377
|
+
return undefined;
|
|
378
|
+
if (typeof sourceElem !== "object" || sourceElem === null || !("obj" in sourceElem))
|
|
379
|
+
afail("MAP_NOT_LOWERABLE", `method '${f.method}': .map 'into' requires an object-typed source element`);
|
|
380
|
+
return { obj: { ...sourceElem.obj, [intoKey]: leafElem } };
|
|
381
|
+
}
|
|
382
|
+
export function lowerMap(f, overMeta, runCallback, opts, intoSourceElem) {
|
|
383
|
+
if (f.pendingElement)
|
|
384
|
+
afail("NESTED_MAP", `method '${f.method}': nested .map is not in the v2 vocabulary (chain a second map over the augmented list — scp-ir-architecture.md §5.1)`);
|
|
385
|
+
if (f.branchStack.length > 0)
|
|
386
|
+
afail("MAP_IN_BRANCH", `method '${f.method}': .map inside a conditional branch is not expressible in the IR (guard the 'over' expression instead)`);
|
|
387
|
+
if (overMeta.refPath !== null && overMeta.refPath.length === 0)
|
|
388
|
+
afail("WHOLE_INPUT", `method '${f.method}': the whole '$' input cannot be iterated; reference an array field`);
|
|
389
|
+
if (opts !== undefined) {
|
|
390
|
+
if (opts === null || typeof opts !== "object")
|
|
391
|
+
afail("MAP_NOT_LOWERABLE", ".map options must be an object");
|
|
392
|
+
if (opts.into !== undefined && typeof opts.into !== "string")
|
|
393
|
+
afail("MAP_NOT_LOWERABLE", ".map option 'into' must be a string key");
|
|
394
|
+
if (opts.batched !== undefined && typeof opts.batched !== "boolean")
|
|
395
|
+
afail("MAP_NOT_LOWERABLE", ".map option 'batched' must be a boolean");
|
|
396
|
+
}
|
|
397
|
+
const pm = beginElement(f);
|
|
398
|
+
try {
|
|
399
|
+
runCallback({ node: { ref: [pm.as] }, refPath: [pm.as], refOp: "ref", isInput: false });
|
|
400
|
+
}
|
|
401
|
+
finally {
|
|
402
|
+
f.pendingElement = null;
|
|
403
|
+
}
|
|
404
|
+
if (pm.component === null)
|
|
405
|
+
afail("MAP_NOT_LOWERABLE", `method '${f.method}': a .map callback must invoke a catalog component (a pure-transform map has no operator in the portable Expression IR v2 vocabulary)`);
|
|
406
|
+
const over = overMeta.node;
|
|
407
|
+
const deps = collectNodeIdRefs(over, f.nodeIds);
|
|
408
|
+
collectNodeIdRefs(pm.ports, f.nodeIds, deps);
|
|
409
|
+
if (pm.when !== undefined)
|
|
410
|
+
collectNodeIdRefs(pm.when, f.nodeIds, deps);
|
|
411
|
+
const id = nextId(f);
|
|
412
|
+
const node = { id, map: { over, as: pm.as, component: pm.component, ports: pm.ports } };
|
|
413
|
+
if (pm.when !== undefined)
|
|
414
|
+
node.map.when = pm.when;
|
|
415
|
+
if (opts?.into !== undefined)
|
|
416
|
+
node.map.into = opts.into;
|
|
417
|
+
if (opts?.batched === true)
|
|
418
|
+
node.map.batched = true;
|
|
419
|
+
if (deps.length > 0)
|
|
420
|
+
node.map.parent = deps[0];
|
|
421
|
+
// map ノードの outType は**要素型**(配列ラップ前)で持つ既存規約に従う。要素型の出所は 3 段:
|
|
422
|
+
// callback 内の明示注記(`pm.outType`)→ `into` 指定時は source 要素型 ⊕ `{<into>: leaf 結果型}` を
|
|
423
|
+
// {@link intoElemType} が宣言型から導く → いずれも無ければ leaf の base 要素型(catalog elemType)。
|
|
424
|
+
const mapElem = pm.outType !== undefined
|
|
425
|
+
? pm.outType
|
|
426
|
+
: opts?.into !== undefined
|
|
427
|
+
? intoElemType(f, intoSourceElem, opts.into, pm.baseElemType)
|
|
428
|
+
: pm.baseElemType;
|
|
429
|
+
if (mapElem !== undefined)
|
|
430
|
+
node.outType = mapElem;
|
|
431
|
+
node.map.portSchemas = derivePortSchemas(pm.entry, pm.ports);
|
|
432
|
+
if (pm.entry.leafSymbol !== undefined)
|
|
433
|
+
node.map.leafSymbol = pm.entry.leafSymbol;
|
|
434
|
+
f.nodes.push(node);
|
|
435
|
+
return id;
|
|
436
|
+
}
|
|
437
|
+
// ── structured control: Fanout ───────────────────────────────────────────────────
|
|
438
|
+
/**
|
|
439
|
+
* lowerFanout — `ids.refsConnection(el => Leaf({...}), { dedupeKey, drop, implicitSource })` を
|
|
440
|
+
* 単一 FanoutNode へ lower する。`.map` と同じ single-component capture を使うが、意味は
|
|
441
|
+
* dedup 済み 1 回の batched dispatch → dedupe(first-seen by dedupeKey)/drop(dangling)/strip →
|
|
442
|
+
* connection `{items,cursor:null}`。
|
|
443
|
+
*/
|
|
444
|
+
export function lowerFanout(f, overMeta, runCallback, opts) {
|
|
445
|
+
if (f.pendingElement)
|
|
446
|
+
afail("FANOUT_IN_BRANCH", `method '${f.method}': refsConnection inside a .map callback is not expressible (chain over the connection items instead)`);
|
|
447
|
+
if (f.branchStack.length > 0)
|
|
448
|
+
afail("FANOUT_IN_BRANCH", `method '${f.method}': refsConnection inside a conditional branch is not expressible in the IR (guard the 'over' expression instead)`);
|
|
449
|
+
if (overMeta.refPath !== null && overMeta.refPath.length === 0)
|
|
450
|
+
afail("WHOLE_INPUT", `method '${f.method}': the whole '$' input cannot be fanned out; reference an id-list field`);
|
|
451
|
+
if (opts === null || opts === undefined || typeof opts !== "object")
|
|
452
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': refsConnection requires an options object with a 'dedupeKey'`);
|
|
453
|
+
if (typeof opts.dedupeKey !== "string" || opts.dedupeKey === "")
|
|
454
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': refsConnection 'dedupeKey' must be a non-empty string`);
|
|
455
|
+
const drop = opts.drop ?? "dangling";
|
|
456
|
+
if (drop !== "dangling" && drop !== "none")
|
|
457
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': refsConnection 'drop' must be "dangling" or "none"`);
|
|
458
|
+
if (opts.implicitSource !== undefined && (typeof opts.implicitSource !== "string" || opts.implicitSource === ""))
|
|
459
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': refsConnection 'implicitSource' must be a non-empty string when present`);
|
|
460
|
+
const pm = beginElement(f);
|
|
461
|
+
try {
|
|
462
|
+
runCallback({ node: { ref: [pm.as] }, refPath: [pm.as], refOp: "ref", isInput: false });
|
|
463
|
+
}
|
|
464
|
+
finally {
|
|
465
|
+
f.pendingElement = null;
|
|
466
|
+
}
|
|
467
|
+
if (pm.component === null)
|
|
468
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': a refsConnection callback must invoke exactly one catalog component`);
|
|
469
|
+
if (pm.when !== undefined)
|
|
470
|
+
afail("FANOUT_NOT_LOWERABLE", `method '${f.method}': refsConnection callback cannot carry a when(...) guard (dedupe/drop is the fanout's own definition)`);
|
|
471
|
+
const over = overMeta.node;
|
|
472
|
+
const deps = collectNodeIdRefs(over, f.nodeIds);
|
|
473
|
+
collectNodeIdRefs(pm.ports, f.nodeIds, deps);
|
|
474
|
+
const id = nextId(f);
|
|
475
|
+
const node = {
|
|
476
|
+
id,
|
|
477
|
+
fanout: {
|
|
478
|
+
over,
|
|
479
|
+
as: pm.as,
|
|
480
|
+
component: pm.component,
|
|
481
|
+
ports: pm.ports,
|
|
482
|
+
dedupeKey: opts.dedupeKey,
|
|
483
|
+
drop,
|
|
484
|
+
relationKind: "connection",
|
|
485
|
+
},
|
|
486
|
+
};
|
|
487
|
+
if (opts.implicitSource !== undefined)
|
|
488
|
+
node.fanout.implicitSource = opts.implicitSource;
|
|
489
|
+
if (deps.length > 0)
|
|
490
|
+
node.fanout.parent = deps[0];
|
|
491
|
+
// callback 内 `.as<T>(t)` で確定した**要素型**を connection outType へ包む(fanout は常に
|
|
492
|
+
// connection)。`.as` 未指定時は leaf の base 要素型にフォールバック。`implicitSource` は結果要素
|
|
493
|
+
// から**除去される**ので宣言する要素型からも落とす(型注記は produced value を述べるもの)。
|
|
494
|
+
const fanElem = stripImplicitSource(pm.outType !== undefined ? pm.outType : pm.baseElemType, opts.implicitSource);
|
|
495
|
+
if (fanElem !== undefined)
|
|
496
|
+
node.outType = { obj: { items: { arr: fanElem }, cursor: { opt: "string" } } };
|
|
497
|
+
node.fanout.portSchemas = derivePortSchemas(pm.entry, pm.ports);
|
|
498
|
+
if (pm.entry.leafSymbol !== undefined)
|
|
499
|
+
node.fanout.leafSymbol = pm.entry.leafSymbol;
|
|
500
|
+
f.nodes.push(node);
|
|
501
|
+
return id;
|
|
502
|
+
}
|
|
503
|
+
function beginElement(f) {
|
|
504
|
+
const pm = {
|
|
505
|
+
as: `$e${f.mapSeq++}`,
|
|
506
|
+
component: null,
|
|
507
|
+
ports: null,
|
|
508
|
+
when: undefined,
|
|
509
|
+
outType: undefined,
|
|
510
|
+
baseElemType: undefined,
|
|
511
|
+
entry: null,
|
|
512
|
+
};
|
|
513
|
+
f.pendingElement = pm;
|
|
514
|
+
return pm;
|
|
515
|
+
}
|
|
516
|
+
/** fanout の宣言要素型から `implicitSource` フィールドを落とす(除去されるフィールドを宣言に残すと
|
|
517
|
+
* 「宣言と実物がずれた型」= 偽の注記になり、そのまま codegen へ流れる)。 */
|
|
518
|
+
function stripImplicitSource(elem, implicitSource) {
|
|
519
|
+
if (elem === undefined || implicitSource === undefined)
|
|
520
|
+
return elem;
|
|
521
|
+
if (typeof elem !== "object" || !("obj" in elem))
|
|
522
|
+
return elem;
|
|
523
|
+
if (!Object.prototype.hasOwnProperty.call(elem.obj, implicitSource))
|
|
524
|
+
return elem;
|
|
525
|
+
const rest = {};
|
|
526
|
+
for (const [k, v] of Object.entries(elem.obj))
|
|
527
|
+
if (k !== implicitSource)
|
|
528
|
+
rest[k] = v;
|
|
529
|
+
return { obj: rest };
|
|
530
|
+
}
|
|
531
|
+
// ── structured control: Conditional / Guard ───────────────────────────────────────
|
|
532
|
+
/**
|
|
533
|
+
* lowerConditional — Conditional(`?:`)/ Guard(`&&`)の lowering
|
|
534
|
+
* (semantic-contract-programming.md「構造化制御構文」の Compiler 規則そのもの):
|
|
535
|
+
*
|
|
536
|
+
* - 両分岐(else 省略時は then のみ)が**純式のみ** → Expression IR `{cond:[c,t,e]}` へ
|
|
537
|
+
* inline lower(body ノードは増えない。else 省略は `null`)。
|
|
538
|
+
* - 分岐に **Component 宣言を含む** → 分岐ごとに gate(CondNode + null-binding skip)を立て、
|
|
539
|
+
* 両分岐が値を持つ場合は Φ(CondNode)ノードで合流する(採用側のみ実行される)。
|
|
540
|
+
* Guard 形(else 無し)は then の値をそのまま返す — 条件不成立時は当該ノードが Skip され、
|
|
541
|
+
* 下流へは未生成 Port の Skip 伝播規則で連鎖する。
|
|
542
|
+
* - `.map` コールバック内では **per-element guard**(v2 `map.when`)として lower される
|
|
543
|
+
* (guard 形のみ。false の要素は handler 未呼び出しで結果から除外)。
|
|
544
|
+
*
|
|
545
|
+
* `runThen` / `runElse` は reader が供給する分岐本体の読み取り。戻り値は分岐の値(抽象値 or リテラル)。
|
|
546
|
+
* 戻り値は「この条件式全体の値」(抽象値 / リテラル / node 結果 meta を包んだ carrier)。
|
|
547
|
+
*/
|
|
548
|
+
export function lowerConditional(f, condition, runThen, runElse, wrap) {
|
|
549
|
+
if (!exprMetaOf(condition))
|
|
550
|
+
afail("BAD_CONDITION", `method '${f.method}': the condition must be an Expression over ports (a literal/native comparison of plain values is not part of the graph)`);
|
|
551
|
+
const c = lowerValue(condition, "when condition");
|
|
552
|
+
// map コールバック内: per-element guard(v2 map.when)。
|
|
553
|
+
if (f.pendingElement) {
|
|
554
|
+
if (runElse !== null)
|
|
555
|
+
afail("BAD_BRANCH", `method '${f.method}': a conditional inside a .map callback is guard-form only (no else — v2 'map.when' skips the element)`);
|
|
556
|
+
const before = f.pendingElement.component;
|
|
557
|
+
const result = runThen();
|
|
558
|
+
if (f.pendingElement.component === before) {
|
|
559
|
+
// 純式 guard → inline cond(else は null)。
|
|
560
|
+
return wrap(opMeta({ cond: [c, lowerValue(result, "when then"), null] }));
|
|
561
|
+
}
|
|
562
|
+
if (f.pendingElement.when !== undefined)
|
|
563
|
+
afail("BAD_CONDITION", `method '${f.method}': a .map callback records at most one when(...) guard`);
|
|
564
|
+
f.pendingElement.when = c;
|
|
565
|
+
return result;
|
|
566
|
+
}
|
|
567
|
+
// then 分岐。
|
|
568
|
+
const thenFrame = { cond: c, gateId: null, startLen: f.nodes.length };
|
|
569
|
+
f.branchStack.push(thenFrame);
|
|
570
|
+
let thenVal;
|
|
571
|
+
try {
|
|
572
|
+
thenVal = runThen();
|
|
573
|
+
}
|
|
574
|
+
finally {
|
|
575
|
+
f.branchStack.pop();
|
|
576
|
+
}
|
|
577
|
+
const thenHasComponents = f.nodes.length > thenFrame.startLen;
|
|
578
|
+
// else 分岐。
|
|
579
|
+
let elseVal = null;
|
|
580
|
+
let elseHasComponents = false;
|
|
581
|
+
if (runElse !== null) {
|
|
582
|
+
const elseFrame = { cond: { not: [c] }, gateId: null, startLen: f.nodes.length };
|
|
583
|
+
f.branchStack.push(elseFrame);
|
|
584
|
+
try {
|
|
585
|
+
elseVal = runElse();
|
|
586
|
+
}
|
|
587
|
+
finally {
|
|
588
|
+
f.branchStack.pop();
|
|
589
|
+
}
|
|
590
|
+
elseHasComponents = f.nodes.length > elseFrame.startLen;
|
|
591
|
+
}
|
|
592
|
+
// 純式のみ → Expression IR へ inline lower(Compiler 規則)。
|
|
593
|
+
if (!thenHasComponents && !elseHasComponents)
|
|
594
|
+
return wrap(opMeta({ cond: [c, lowerValue(thenVal, "when then"), lowerValue(elseVal, "when else")] }));
|
|
595
|
+
// Guard 形(else 無し): then の値をそのまま返す(不成立時は gate skip → 未生成 Port)。
|
|
596
|
+
if (runElse === null)
|
|
597
|
+
return thenVal;
|
|
598
|
+
// Φ 合流ノード: 採用側の値を単一 Port にする(cond は採用側のみ評価する)。
|
|
599
|
+
const id = nextId(f);
|
|
600
|
+
const phi = {
|
|
601
|
+
id,
|
|
602
|
+
cond: { if: c, then: lowerValue(thenVal, "when then"), else: lowerValue(elseVal, "when else") },
|
|
603
|
+
};
|
|
604
|
+
f.nodes.push(phi);
|
|
605
|
+
return wrap(nodeMeta(id));
|
|
606
|
+
}
|
|
607
|
+
// ── Component 組み立て(lowering の末尾)──────────────────────────────────────────
|
|
608
|
+
/**
|
|
609
|
+
* assembleComponent — 1 メソッド分の lowering を Component へ畳む(両 reader 共通の末尾)。
|
|
610
|
+
*
|
|
611
|
+
* 上書き schema(`inputPorts`)は **型**の供給点(TS 型はランタイムに残らない reader もある)。
|
|
612
|
+
* optionality は `opt($.x)` が本体に書いた**宣言**なので、上書きは型を足すだけで、それを黙って
|
|
613
|
+
* 取り消さない:
|
|
614
|
+
* - 上書きが `required:true` を **明示** して `opt($.x)` と矛盾 → LOUD reject(2 枚の carrier が
|
|
615
|
+
* 食い違ったまま通ると、宣言だけを読む下流 [runBehavior の束縛 / native lowering] が
|
|
616
|
+
* optionality を見失う — それが silent な誤束縛の入口)。
|
|
617
|
+
* - 上書きが `required` を **述べていない**(型だけ)→ 型は上書き・`required:false` は保存。
|
|
618
|
+
* 逆向き(上書きが `required:false`・本体は素の `$.x`)は矛盾ではない。
|
|
619
|
+
*
|
|
620
|
+
* 検証・plan 導出・正規化・Guard は builder.ts の {@link buildComponentCore} に委譲し、
|
|
621
|
+
* 全ノード型確定ゲート(type-gate.ts)で outputType を導出する。
|
|
622
|
+
*/
|
|
623
|
+
export function assembleComponent(f, returned, overrides, concurrency) {
|
|
624
|
+
if (returned === undefined)
|
|
625
|
+
afail("BAD_OUTPUT", `method '${f.method}' must return its Output Port (got undefined)`);
|
|
626
|
+
if (returned instanceof Promise)
|
|
627
|
+
afail("BAD_OUTPUT", `method '${f.method}' must be synchronous and declarative (async/await is derived by the compiler, not written)`);
|
|
628
|
+
const output = lowerValue(returned, `method '${f.method}' output`);
|
|
629
|
+
const inputPorts = {};
|
|
630
|
+
for (const [k, s] of f.inputPorts) {
|
|
631
|
+
const ov = overrides?.[k];
|
|
632
|
+
if (ov === undefined) {
|
|
633
|
+
inputPorts[k] = s;
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
if (f.optionalInputs.has(k) && ov.required === true)
|
|
637
|
+
afail("PORT_OPTIONALITY_CONFLICT", `method '${f.method}' port '${k}': the body declares it OPTIONAL via opt($.${k}), but opts.inputPorts declares required:true — the two disagree. An optional port's declaration is what runBehavior's binding and the native lowering read, so this contradiction would silently drop the optionality. Supply required:false (or omit required and let opt($.${k}) stand).`);
|
|
638
|
+
inputPorts[k] = f.optionalInputs.has(k) ? { ...ov, required: false } : ov;
|
|
639
|
+
}
|
|
640
|
+
// 上書き schema にしか現れない Port も契約として載せる(型抽出の接続点)。
|
|
641
|
+
for (const [k, s] of Object.entries(overrides ?? {}))
|
|
642
|
+
if (!(k in inputPorts))
|
|
643
|
+
inputPorts[k] = s;
|
|
644
|
+
const component = buildComponentCore({ name: f.method, inputPorts, body: f.nodes, output }, { concurrency });
|
|
645
|
+
// 全ノード型確定ゲート: 全 body ノードの outType と output(Φ 合成)の全フィールド型が一意に確定して
|
|
646
|
+
// いることを網羅検査し(未確定は UNTYPED_NODE で fail-closed)、確定済みノード型から outputType を
|
|
647
|
+
// 構造伝播で導出する(推論ではない)。
|
|
648
|
+
component.outputType = canonicalizeType(assertAllNodeTypesDetermined(component));
|
|
649
|
+
return component;
|
|
650
|
+
}
|
|
651
|
+
export { DEFAULT_PLAN_CONCURRENCY };
|
|
652
|
+
//# sourceMappingURL=lowering.js.map
|