@nudojs/core 2.1.0 → 3.0.0-beta.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.
@@ -1,855 +0,0 @@
1
- import { Node, ImportDeclaration, File, Expression } from '@babel/types';
2
-
3
- /** 项(Term):抽象值的身份。字面量是项的特例。 */
4
- type LiteralValue = string | number | boolean | null | undefined;
5
- type Term = {
6
- op: "lit";
7
- value: LiteralValue;
8
- } | {
9
- op: "var";
10
- id: string;
11
- } | {
12
- op: "app";
13
- fn: string;
14
- args: Term[];
15
- };
16
- declare const lit: (value: LiteralValue) => Term;
17
- declare const v: (id: string) => Term;
18
- declare const app: (fn: string, args: Term[]) => Term;
19
- declare function termEquals(a: Term, b: Term): boolean;
20
- declare function termToString(t: Term): string;
21
- /** 常量折叠 + 简单代数化简 */
22
- declare function simplifyTerm(t: Term): Term;
23
-
24
- /** 约束(Pred):附着在项上的事实,可传播。 */
25
-
26
- type PrimName = "number" | "string" | "boolean" | "bigint" | "symbol";
27
- type Pred = {
28
- op: "true";
29
- } | {
30
- op: "false";
31
- } | {
32
- op: "eq";
33
- a: Term;
34
- b: Term;
35
- } | {
36
- op: "ne";
37
- a: Term;
38
- b: Term;
39
- } | {
40
- op: "lt";
41
- a: Term;
42
- b: Term;
43
- } | {
44
- op: "le";
45
- a: Term;
46
- b: Term;
47
- } | {
48
- op: "gt";
49
- a: Term;
50
- b: Term;
51
- } | {
52
- op: "ge";
53
- a: Term;
54
- b: Term;
55
- } | {
56
- op: "and";
57
- args: Pred[];
58
- } | {
59
- op: "or";
60
- args: Pred[];
61
- } | {
62
- op: "not";
63
- arg: Pred;
64
- } | {
65
- op: "typeof";
66
- t: Term;
67
- type: PrimName;
68
- };
69
- declare const pTrue: Pred;
70
- declare const pFalse: Pred;
71
- declare const eq: (a: Term, b: Term) => Pred;
72
- declare const ne: (a: Term, b: Term) => Pred;
73
- declare const lt: (a: Term, b: Term) => Pred;
74
- declare const le: (a: Term, b: Term) => Pred;
75
- declare const gt: (a: Term, b: Term) => Pred;
76
- declare const ge: (a: Term, b: Term) => Pred;
77
- declare const ptypeof: (t: Term, type: PrimName) => Pred;
78
- declare function and(...preds: Pred[]): Pred;
79
- declare function or(...preds: Pred[]): Pred;
80
- declare function not(p: Pred): Pred;
81
- declare function predEquals(a: Pred, b: Pred): boolean;
82
- declare function predToString(p: Pred): string;
83
- /** 把 pred 里的 Term 用 subst 替换(用于 bound 变量重命名等) */
84
- declare function substPred(p: Pred, subst: (t: Term) => Term): Pred;
85
- /** 收集 pred 中出现的自由变量 */
86
- declare function predVars(p: Pred): Set<string>;
87
- /** 约束环境 Φ:Pred 的合取 */
88
- type Phi = Pred;
89
- declare const emptyPhi: Phi;
90
- declare const phiAnd: typeof and;
91
- /** 简单蕴含:在区间/字面量可判定范围内判断 Φ ⊢ pred */
92
- declare function implies(phi: Phi, pred: Pred): boolean;
93
- /** 便捷:数字下界 */
94
- declare function gtNum(term: Term, n: number): Pred;
95
- declare function geNum(term: Term, n: number): Pred;
96
- declare function ltNum(term: Term, n: number): Pred;
97
- declare function leNum(term: Term, n: number): Pred;
98
-
99
- /** 抽象值 Abs = 形状 × 项 × 约束 × 置信度 */
100
-
101
- type Confidence = "exact" | "path" | "widened" | "mock" | "partial" | "opaque";
102
- type Shape = {
103
- k: "never";
104
- }
105
- /**
106
- * any:JS 里可以是任意值(无约束参数)。
107
- * 运算按真实 JS 语义取并集,不是「分析失败」。
108
- */
109
- | {
110
- k: "any";
111
- }
112
- /** unknown:分析拿不到信息(求值失败/泄漏),不是「任意值」 */
113
- | {
114
- k: "unknown";
115
- } | {
116
- k: "prim";
117
- type: PrimName;
118
- } | {
119
- k: "obj";
120
- slots: Record<string, {
121
- value: Abs;
122
- optional?: boolean;
123
- readonly?: boolean;
124
- }>;
125
- index?: {
126
- key: Abs;
127
- value: Abs;
128
- };
129
- open?: boolean;
130
- } | {
131
- k: "arr";
132
- element: Abs;
133
- } | {
134
- k: "tuple";
135
- elements: Abs[];
136
- rest?: Abs;
137
- } | {
138
- k: "fn";
139
- params: string[];
140
- name?: string;
141
- paramTypes?: Abs[];
142
- returnType?: Abs;
143
- } | {
144
- k: "brand";
145
- name: string;
146
- shape: Abs;
147
- } | {
148
- k: "eff";
149
- eff: "promise" | "generator";
150
- inner: Abs;
151
- } | {
152
- k: "sum";
153
- members: Abs[];
154
- };
155
- type Abs = {
156
- shape: Shape;
157
- term?: Term;
158
- pred?: Pred;
159
- conf: Confidence;
160
- /**
161
- * C2.4:分支 join 可解释标注(如 `join(number | string)`)。
162
- * 仅展示用:不进 leq / 指纹 / check 等价;formatAbs 读取。
163
- */
164
- pathNote?: string;
165
- };
166
- declare const never: Abs;
167
- /** 分析无信息 */
168
- declare const unknown: Abs;
169
- /** 带 term 的 any(generalize type-var) */
170
- declare function anyVar(id: string, conf?: Confidence): Abs;
171
- declare function num(): Abs;
172
- declare function str(): Abs;
173
- declare function bool(): Abs;
174
- declare function numLit(value: number): Abs;
175
- declare function strLit(value: string): Abs;
176
- declare function boolLit(value: boolean): Abs;
177
- /** 带项的符号数,例如参数 x */
178
- declare function numVar(id: string, pred?: Pred, conf?: Confidence): Abs;
179
- declare function obj(slots: Record<string, {
180
- value: Abs;
181
- optional?: boolean;
182
- }>): Abs;
183
- declare function confJoin(a: Confidence, b: Confidence): Confidence;
184
- declare function absToString(a: Abs): string;
185
- declare function shapeToString(s: Shape): string;
186
- /** 从 term 反推 prim shape */
187
- declare function shapeOfTerm(t: Term): Shape;
188
- /** 构造带项的结果 Abs;pred 相对 term */
189
- declare function abs(shape: Shape, term: Term | undefined, pred: Pred | undefined, conf: Confidence): Abs;
190
- declare function isNumPrim(a: Abs): boolean;
191
- declare function isStrPrim(a: Abs): boolean;
192
- /** 置信度:字面量全确定 */
193
- declare function isExactLit(a: Abs): boolean;
194
- declare function litValue(a: Abs): LiteralValue | undefined;
195
-
196
- /**
197
- * HOF 关系 Abs:无 body 的外延关系(paramTypes → returnType)的判定、
198
- * α 替换与应用。类型变量仍是 term var;关系仍是 fn 形状上的外延槽。
199
- */
200
-
201
- /**
202
- * 关系来源标记:P4 豁免与 diagnostics 依赖它,禁止隐式猜。
203
- * - promote:使用驱动提升(generalize symbolic / instantiate 局部)
204
- * - refine:@nudo:refine 契约
205
- * - relationFn:harvest/mock/测试直接写入 fnRels 时的预留来源(P4 error 路径)
206
- */
207
- type RelSource = "promote" | "refine" | "relationFn";
208
- type HofSite = {
209
- /** 形参名(函数形参) */
210
- param: string;
211
- /** 输入侧 term:实参的 term(element 的 var/lit/app);map 1 个、reduce 2 个 */
212
- argTerms: Term[];
213
- /** 输出侧:归纳出的返回 Abs */
214
- result: Abs;
215
- /** 源位置,便于 diagnostics */
216
- loc?: {
217
- line: number;
218
- column: number;
219
- };
220
- };
221
- /**
222
- * run 局部 collector(与 Phi 并列,不进 Φ 合并)。
223
- * symbolic 一次跑:安装并沉淀到 PolyFn;instantiate 重跑:装 throwaway
224
- * 副本——形状提升仍生效,结果不写回共享状态(见 generalize.ts run())。
225
- */
226
- type HofCollectCtx = {
227
- /** 本次归纳的形参名集合(身份判定用) */
228
- paramNames: ReadonlySet<string>;
229
- /** 本次 typeParams 的 α id 集合(term 复用白名单) */
230
- alphaIds: Set<string>;
231
- /** fresh α 计数 */
232
- freshSeq: {
233
- n: number;
234
- };
235
- sites: HofSite[];
236
- fnRels: Map<string, {
237
- abs: Abs;
238
- source: RelSource;
239
- }>;
240
- entryShapes: Map<string, {
241
- abs: Abs;
242
- source: RelSource;
243
- }>;
244
- };
245
- declare function createHofCollectCtx(paramNames: ReadonlySet<string>, alphaIds: Iterable<string>): HofCollectCtx;
246
- /**
247
- * 仅当 term 是 var 且 id ∈ alphaIds(本次 typeParams)时复用;否则 fresh α。
248
- * 绝不把调用点具体值/字面量冻进关系。
249
- */
250
- declare function alphaOf(absOrTerm: Abs | Term | undefined, ctx: HofCollectCtx): Term;
251
- /** 共享输出变量 B:${param}(§5.1 P2 钉死) */
252
- declare function betaOf(param: string): Term;
253
- /**
254
- * 提升写入载体:替换 env.vars map 项,禁止 mutate 共享 Abs。
255
- * arrival-first:已有 fn/arr 形状 → 拒绝新观测。
256
- * 返回是否真正写入。
257
- */
258
- declare function promoteParamShape(env: AstEnv, param: string, promotedShape: Shape, opts?: {
259
- loc?: {
260
- line: number;
261
- column: number;
262
- };
263
- recordSite?: boolean;
264
- }): boolean;
265
- /**
266
- * 挂载点①:方法派发 miss。receiver 是形参 Identifier 且 shape 为 any/未知,
267
- * 方法名为 filter/map/reduce/flatMap → 提升为 arr(自身 var)。
268
- */
269
- declare function tryPromoteReceiverAsArr(env: AstEnv, receiverName: string, method: string, loc?: {
270
- line: number;
271
- column: number;
272
- }): Abs | undefined;
273
- /**
274
- * for-of 迭代对象提升(applyEach 型):`for (const x of items)`,
275
- * items 为形参且仍是 any/unknown → arr(自身 var)。不依赖方法名。
276
- */
277
- declare function tryPromoteForOfIteratee(env: AstEnv, iterateeName: string, loc?: {
278
- line: number;
279
- column: number;
280
- }): Abs | undefined;
281
- /**
282
- * 挂载点②:CallExpression callee = 形参 Identifier 直接调用 p(x) / p(a,b)。
283
- * 提升为 fn(paramTypes=[αOf(args)], returnType=B:param)。
284
- */
285
- declare function tryPromoteDirectCall(env: AstEnv, calleeName: string, args: Abs[], loc?: {
286
- line: number;
287
- column: number;
288
- }): Abs | undefined;
289
- /**
290
- * 挂载点③:HOF 回调实参。回调是 Identifier ∈ paramNames 且尚未有 fn 形状。
291
- * 按方法名提升:map/flatMap → fn([αOf(el)], B:param);filter → fn([αOf(el)], bool);
292
- * reduce → fn([αOf(init), αOf(el)], B:param)。
293
- */
294
- declare function tryPromoteHofCallback(env: AstEnv, cbName: string, method: string, argAbses: Abs[], loc?: {
295
- line: number;
296
- column: number;
297
- }): Abs | undefined;
298
- /** deep-ish copy for snapshot(新对象,不是 env.vars 同一引用) */
299
- declare function snapshotAbs(a: Abs): Abs;
300
- /**
301
- * 「有可用外延签名」判定:唯一权威定义。
302
- *
303
- * 名实说明:isRelFn **不要求** term 是 var。完全单态的具体签名
304
- * (paramTypes=[number], returnType=string)同样满足——map 有槽就用。
305
- * 多态(term=var)只是其中一种形态;名字里的 Rel 指「关系槽可用」。
306
- *
307
- * 收紧条件——仅有 returnType 而 paramTypes 与 arity 不对齐时,不算 rel,
308
- * 防止半截签名在 map 里冒充关系。
309
- */
310
- declare function isRelFn(a: Abs | undefined | null): boolean;
311
- /**
312
- * pred 三条规则:
313
- * 1. var ∈ map + 实参有 term → 换 term,可归约则归约,否则残余保留
314
- * 2. var ∈ map + 实参无 term → pred → true(conf 由调用方降级)
315
- * 3. var ∉ map(自由 α)→ 原样保留
316
- */
317
- declare function substPredAbs(p: Pred, map: ReadonlyMap<string, Abs>): {
318
- pred: Pred;
319
- dropped: boolean;
320
- };
321
- /**
322
- * α 替换:map 的 key 是 term var id,value 是替换 Abs。
323
- * var ∉ map 原样保留(含自由 α 上的 term/pred)。
324
- */
325
- declare function substAbs(a: Abs, map: ReadonlyMap<string, Abs>): Abs;
326
- /**
327
- * relation-only / isRelFn 的应用:按 paramTypes 做 α 替换得到 returnType。
328
- * impl.relation 槽优先于 shape.returnType。重复 α 先绑定保留。
329
- */
330
- declare function instantiateReturn(fn: Abs, args: Abs[]): Abs;
331
- /**
332
- * 回调统一入口(单点定义)。A–F + sum:
333
- * A Node inline | B apply | C body | D relation | E isRelFn | F unknown
334
- *
335
- * 实现委托 ast-eval 的 applyAbsFn(已含 sum / D / E / body 优先)。
336
- * Identifier 解析层:env.vars 有 Abs → B–E;env.fns 有 → callFunction;否则 unknown。
337
- *
338
- * 为避免 hof ↔ ast-eval 循环依赖,本函数由宿主在运行时绑定。
339
- *
340
- * 依赖说明:宿主在 `ast-eval.ts` 模块加载时注册(副作用)。
341
- * 只 import hof.ts 而未加载 ast-eval 时,fallback 仅认 relation/isRelFn。
342
- * 与 §5.1 的「禁止全局 collector」不同——这里是无状态委托钩子,不是 run 局部状态。
343
- */
344
- type ApplyCallbackHost = (cb: Abs | {
345
- type: string;
346
- }, args: Abs[], env: unknown, phi: unknown, budget: unknown) => Abs;
347
- /**
348
- * ast-eval 模块加载时注册(副作用)。
349
- * 必须经 `ast-eval.ts`(或其依赖方:exec/call、exec/class、generalize)加载,
350
- * 才能启用 Identifier/env.fns/inline body 路径;只 import hof.ts 时 fallback
351
- * 仅认 relation/isRelFn。勿在多份 ast-eval 实例下各写各的——双包/双副本会覆盖。
352
- */
353
- declare function setApplyCallbackHost(fn: ApplyCallbackHost): void;
354
- declare function applyCallbackAbs(cb: Abs | {
355
- type: string;
356
- }, args: Abs[], env: unknown, phi: unknown, budget: unknown): Abs;
357
- /** undefined 值的统一 Abs 表示(forEach/find 等) */
358
- declare function undefAbs(): Abs;
359
- /**
360
- * map 元素投影:body 在符号实参上跑出 unknown 时,
361
- * 用 shape.returnType 槽,conf 由调用方按 path/partial 处理。
362
- * rel 回调不会走到这里(instantiateReturn 已给出结果)。
363
- */
364
- declare function mapElementFallback(cbAbs: Abs | undefined, elem: Abs, out: Abs): Abs;
365
- /**
366
- * flatMap 统一结果:展开后的元素 join 成 arr(γ)。
367
- * JS flatMap 永远返回 Array;双路径共用此投影,禁止一边 tuple 一边 arr。
368
- * 同 term 元素 first-wins(joinAbs 会丢 β 身份,见 design §5.1)。
369
- */
370
- declare function projectFlatMapResult(arrConf: Confidence, mapped: Abs[]): Abs;
371
- /** 从 map/filter/reduce 回调实参里取出 Abs(Identifier 已绑定或直接 Abs) */
372
- declare function asAbs(v: unknown): Abs | undefined;
373
-
374
- /**
375
- * AstEnv:抽象求值环境类型。
376
- * 独立成文件:hof/language/leq/abs-fn/abs-modules 等消费方只依赖此类型,
377
- * 不必再为拿一个类型 import ast-eval 巨石(TS 模块环)。
378
- * ast-eval.ts 对本类型做兼容 re-export,外部消费路径不变。
379
- */
380
-
381
- type AstEnv = {
382
- vars: Map<string, Abs>;
383
- /** 用户函数:name → { params, body } */
384
- fns: Map<string, {
385
- params: string[];
386
- body: Node;
387
- async?: boolean;
388
- kind?: string;
389
- }>;
390
- /** class 表(旁路,withVar 必须保留) */
391
- classes?: Map<string, unknown>;
392
- /** 当前正在求值的方法所属类名(super.x() 从它的父类派发) */
393
- currentOwner?: string;
394
- /** P2:generalize symbolic 跑的 HOF collector(run 局部,不进 Φ) */
395
- hofCollect?: HofCollectCtx;
396
- };
397
-
398
- /**
399
- * Abs 模块面(无 fs):import 绑定 + export 收集。
400
- * 解析路径/读文件在 host;core 只吃「specifier → 导出表」。
401
- */
402
-
403
- type AbsModuleExports = {
404
- named: Record<string, Abs>;
405
- default?: Abs;
406
- };
407
- /** 把 import 说明符绑定进 env(宿主已求值依赖) */
408
- declare function bindImports(node: ImportDeclaration, env: AstEnv, modules: Record<string, AbsModuleExports>): void;
409
- /**
410
- * 从已求值 env + AST 收集 ESM 导出。
411
- * 支持:export function/const、export { a, b as c }、export default(具名)、
412
- * 以及带 source 的 re-export(`export { a } from "mod"` / `export * from "mod"`——
413
- * 需 host 传入已求值 modules)。
414
- */
415
- declare function collectAbsExports(file: File, env: AstEnv, modules?: Record<string, AbsModuleExports>): AbsModuleExports;
416
-
417
- /**
418
- * B 路径运行时:transpile 后的程序在 Node 上执行时,值就是 Abs。
419
- * 与 AST 解释器语义同构;TypeValue 不再是求值载体。
420
- */
421
-
422
- declare function currentExecPhi(): Phi;
423
- declare function withExecPhi<T>(p: Phi, body: () => T): T;
424
- declare function $add(a: Abs, b: Abs): Abs;
425
- declare function $sub(a: Abs, b: Abs): Abs;
426
- declare function $mul(a: Abs, b: Abs): Abs;
427
- declare function $div(a: Abs, b: Abs): Abs;
428
- declare function $mod(a: Abs, b: Abs): Abs;
429
- declare function $neg(a: Abs): Abs;
430
- declare function $typeof(a: Abs): Abs;
431
- declare function $not(a: Abs): Abs;
432
- declare function $eq(a: Abs, b: Abs): Abs;
433
- declare function $ne(a: Abs, b: Abs): Abs;
434
- /** `==` / `!=`(C2.3):双字面量 Abstract Equality,否则回落严格判定 */
435
- declare function $eqLoose(a: Abs, b: Abs): Abs;
436
- declare function $neLoose(a: Abs, b: Abs): Abs;
437
- declare function $lt(a: Abs, b: Abs): Abs;
438
- declare function $le(a: Abs, b: Abs): Abs;
439
- declare function $gt(a: Abs, b: Abs): Abs;
440
- declare function $ge(a: Abs, b: Abs): Abs;
441
- declare function $join(a: Abs, b: Abs): Abs;
442
- /**
443
- * transpile 泄漏的 JS 函数值 → 一等 fn Abs。
444
- * B 路径把函数声明/表达式编译成真实 JS 函数;它们流进对象槽、
445
- * 元组、join 等 Abs 结构时不能裸存——下游(bridge/leq/join)读 `.shape`。
446
- * 参数名无法从运行时函数恢复(用 fn.length → argN,与 analyzer 的
447
- * extractParamNames 回退口径一致);带真实参数名走 $fnVal(transpile 侧)。
448
- */
449
- declare function asAbsVal(v: unknown): Abs;
450
- /**
451
- * 函数调用边界:callee 的 loop/early-return 不得冒泡成 caller 结果。
452
- * 每个 B 路径调用帧独立 ALS;NudoReturn 收成该调用的返回值。
453
- */
454
- declare function callAtFunctionBoundary<T>(body: () => T): T;
455
- /** 函数表达式 → 一等 fn Abs(transpile 侧带真实参数名;异步 body 包 $async)
456
- * opts.bindThis:对象方法——$invoke 会把 receiver 作为 impl 首参注入。 */
457
- declare function $fnVal(params: string[], impl: (...args: Abs[]) => Abs, opts?: {
458
- bindThis?: boolean;
459
- }): Abs;
460
- /** 字面量 → Abs(transpile 侧数字/字符串/布尔/null/undefined) */
461
- declare function $lit(v: unknown): Abs;
462
- /** JS 真值:字面量按 Boolean(v);对象形恒真;不可判 → undefined */
463
- declare function litTruth(a: Abs): boolean | undefined;
464
- declare function isDefinitelyTrue(a: Abs): boolean;
465
- declare function isDefinitelyFalse(a: Abs): boolean;
466
- /** 函数求值作用域:收集抽象分支上的 early-return / throw 值;try 标记栈同边界 */
467
- declare function runWithLoopExits<T>(body: () => T): T;
468
- declare function takeLoopExits(): Abs[];
469
- declare function takeThrowExits(): Abs[];
470
- declare function pushLoopExit(v: Abs): void;
471
- declare function pushThrowExit(v: Abs): void;
472
- /** transpile 生成代码用:`$pushLoopExit` */
473
- declare function $pushLoopExit(v: Abs): void;
474
- /** try 块开始:压栈并返回当前 throwExits 长度 */
475
- declare function $tryMark(): number;
476
- /** 当前最内层 try 的 mark(return 时 drain 用;嵌套调用不串栈) */
477
- declare function $tryCurrentMark(): number;
478
- declare function $tryPopMark(): void;
479
- /** 取出 mark 之后新记录的 throw(try 吸收 / catch 合并) */
480
- declare function $tryTakeSince(mark: number): Abs[];
481
- declare function $fork(test: Abs, consequent: () => Abs, alternate?: () => Abs): Abs;
482
- declare const DEFAULT_MAX_LOOP_ITERS = 8;
483
- /**
484
- * for 的惰性展开:生成器只负责「按上限吐状态」。
485
- * 抽象条件无法诚实终止——消费者必须自带 maxIters。
486
- */
487
- declare function $forIter(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, body: (s: Abs) => Abs, maxIters?: number): Generator<{
488
- state: Abs;
489
- test: Abs;
490
- afterBody: Abs;
491
- }, void, void>;
492
- /**
493
- * 有界 for:unroll ≤ maxIters,每步「可能退出」的态 join;
494
- * 相邻态 leq 视为不动点提前停。
495
- */
496
- declare function $for(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, body: (s: Abs) => Abs, maxIters?: number, opts?: {
497
- pack?: () => Abs;
498
- unpack?: (s: Abs) => void;
499
- }): Abs;
500
- /** 数组字面量 → ≤cap tuple(逐元素精确)/ >cap arr;策略与 ast-eval 同源(containers.ts) */
501
- declare function $arr(items: Abs[]): Abs;
502
- declare function isArrMutator(name: string): boolean;
503
- /**
504
- * C1.4 语句重绑:返回**变更后容器** Abs(不是 JS 返回值)。
505
- * `a.pop()` 语句应把 `a` 绑成去掉末元的 tuple,而不是被移除的元素。
506
- */
507
- declare function $arrMutContainer(arr: Abs, method: string, args: Abs[]): Abs;
508
- /** 下标读 a[i];字面量 i 走 tuple 精确投影,否则并所有元素;string[i] → 单字符 */
509
- declare function $idx(a: Abs, i: Abs): Abs;
510
- /** 下标写 a[i]=v → 新 tuple(越界写按 JS 语义增长,空洞为 undefined) */
511
- declare function $idxSet(a: Abs, i: Abs, value: Abs): Abs;
512
- /** 数组/字符串长度 */
513
- declare function $len(a: Abs): Abs;
514
- /** 对象字面量 → Abs obj */
515
- declare function $obj(slots: Record<string, Abs>): Abs;
516
- /** 对象展开 { ...a, b } */
517
- declare function $spread(a: Abs, b: Abs): Abs;
518
- /**
519
- * 解构 rest:`const { a, ...rest } = o` → rest = o 去掉 named keys。
520
- * closed obj:剩余槽 closed;open / optional 键:rest 仍 open(可能有未知键)。
521
- */
522
- declare function $objRest(o: Abs, keys: string[]): Abs;
523
- /** 数组 rest:`const [a, ...rest] = arr` → rest = 从 start 起的尾段 */
524
- declare function $arrRest(a: Abs, start: number): Abs;
525
- /** 数组连接 [...a, ...b] / [...a, x];结果超 cap 时与字面量同策略降 arr */
526
- declare function $concat(a: Abs, b: Abs): Abs;
527
- /** 元素列表(tuple 展开;arr 抽象;C1 Set/Map 逐条目)。
528
- * Map 迭代语义是 entry `[key, value]` 元组,不是裸 value。 */
529
- declare function $elems(a: Abs): Abs[];
530
- /**
531
- * for-of:对 iterable 每个元素跑 body;有界展开。
532
- * body(item, i) 可返回 void;状态由外部 JS 变量承接。
533
- * - 具体空 tuple:体 0 次(不得用 `length || maxIters` 展开成 maxIters)
534
- * - 抽象 arr / 未知长度:0..maxIters 出口与 pack 状态 join
535
- */
536
- declare function $forOf(iterable: Abs, body: (item: Abs, index: Abs) => void, maxIters?: number, opts?: {
537
- pack?: () => Abs;
538
- unpack?: (s: Abs) => void;
539
- }): void;
540
- /**
541
- * 命名空间身份表:transpile 后 `Math.max(0, x)` 的接收者是宿主 JS 全局对象
542
- * (非 Abs)。按对象身份识别命名空间,路由到 Abs builtin 表。
543
- */
544
- declare function namespaceNameOf(v: unknown): string | undefined;
545
- /** 正则字面量 → RegExp brand(source/flags 进 slots,供 exec/test 精确执行) */
546
- declare function $regex(pattern: string, flags?: string): Abs;
547
- /** 成员读:obj.slots[key];缺失 → undefined 字面量;brand 解包内层 */
548
- declare function $get(o: Abs, key: string, opts?: {
549
- silent?: boolean;
550
- }): Abs;
551
- /** 成员写:返回新 obj/brand(不可变更新) */
552
- declare function $set(o: Abs, key: string, value: Abs): Abs;
553
- /**
554
- * 有界 while:state 线程穿 test/step(与 $for 同折叠语义)。
555
- * 抽象条件无法诚实终止——必须 maxIters。
556
- */
557
- declare function $while(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, maxIters?: number): Abs;
558
- /**
559
- * 顺序 while(具体/可变闭包):body 内对 JS 变量赋值。
560
- * 提供 pack/unpack 时,抽象条件的可能出口会 join 回绑定(P0 健全);
561
- * 未 instrument 的调用保持旧语义(预算截断,文档已声明)。
562
- */
563
- declare function $whileSeq(test: () => Abs, body: () => void, maxIters?: number, opts?: {
564
- /** 把当前绑定收成 Abs 状态(transpile 注入) */
565
- pack?: () => Abs;
566
- /** 把 join 后的状态写回绑定 */
567
- unpack?: (s: Abs) => void;
568
- }): void;
569
- /** B 路径「函数提前 return」信号(区别于 throw) */
570
- declare class NudoReturn extends Error {
571
- readonly absValue: Abs;
572
- constructor(absValue: Abs);
573
- }
574
- /** transpile `return x` inside for/while → `$loopReturn(x)` */
575
- declare function $loopReturn(v: Abs): never;
576
- declare function isNudoReturn(e: unknown): e is NudoReturn;
577
- /** catch 转译辅助:控制流信号透传(生成代码只注入 `$` 前缀符号) */
578
- declare function $rethrowIfNudoReturn(e: unknown): void;
579
- /** B 路径 throw 载荷:携带 Abs 抛出值 */
580
- declare class NudoThrow extends Error {
581
- readonly absValue: Abs;
582
- constructor(absValue: Abs);
583
- }
584
- /** transpile `throw x` → `$throw(x)` */
585
- declare function $throw(v: Abs): never;
586
- declare function isNudoThrow(e: unknown): e is NudoThrow;
587
- /**
588
- * fork 臂是否以控制流退出(return / throw)。
589
- * 生成代码用此决定 continue-path free-write 标志:退出臂的写
590
- * 不得污染 join 后的 continue 绑定。
591
- */
592
- declare function $isForkExit(e: unknown): boolean;
593
- /** catch 参数:从 NudoThrow 取出 Abs;宿主 Error 补 name/message;否则 unknown */
594
- declare function $catchVal(e: unknown): Abs;
595
- /**
596
- * async 函数体包进 thunk,返回值经 wrapPromise。
597
- */
598
- declare function $async(thunk: () => Abs): Abs;
599
- /** await → 解包 eff("promise") */
600
- declare function $await(v: Abs): Abs;
601
- /** async 直接 return 的 coerce */
602
- declare function $asyncReturn(v: Abs): Abs;
603
- /** function* 体:收集所有 yield 值为 tuple Abs;抽象分支时降 conf(P0-6) */
604
- declare function $gen(body: () => void): Abs;
605
- /** yield v:压入当前生成器收集器;表达式值用 unknown */
606
- declare function $yield(v: Abs): Abs;
607
- /**
608
- * switch:具体 disc 选中匹配 case;抽象 disc 并所有分支。
609
- * 抽象路径与 $fork 同构:集合 side-table 按臂 overlay,共享 body 只跑一次;
610
- * 臂内 NudoReturn/NudoThrow 不冒泡污染兄弟臂。
611
- * **无 default 时必须隐式 fall-through 臂(undef)**,否则无匹配路径被丢掉(P0-2)。
612
- */
613
- declare function $switch(disc: Abs, cases: Array<{
614
- test: Abs;
615
- run: () => Abs;
616
- }>, dflt?: () => Abs): Abs;
617
- /** `??` / `??=` 测试:确定非 nullish → false;lit nullish → true;否则抽象 boolean */
618
- declare function $nullishTest(v: Abs): Abs;
619
-
620
- /**
621
- * B 路径 transpile:JS AST → 可在 Node 上执行的抽象值程序(源码字符串)。
622
- * 运算符改为 $add/$sub/…;if 改为 $fork;for 改为 $for。
623
- * 值类型是 Abs;副作用与模块仍由 host mock/注入。
624
- */
625
-
626
- type TranspileOptions = {
627
- /** 运行时 import 说明符 */
628
- runtimeImport?: string;
629
- maxLoopIters?: number;
630
- /** 方法体内 this 的绑定名(transpile class 时注入) */
631
- thisParam?: string;
632
- /** 当前类名(super 派发用) */
633
- className?: string;
634
- /** 原始源码(@nudo:replace 按节点文本匹配) */
635
- source?: string;
636
- /** 替换表:归一化目标文本 → 注入变量名;可选语句范围 */
637
- replacements?: Array<{
638
- target: string;
639
- varName: string;
640
- /** 仅该语句范围内生效(1-based 行) */
641
- stmtStart?: number;
642
- stmtEnd?: number;
643
- }>;
644
- /** @nudo:as:覆盖紧随语句的 init / return */
645
- asOverrides?: Array<{
646
- varName: string;
647
- stmtStart: number;
648
- stmtEnd: number;
649
- }>;
650
- /** 循环嵌套深度(>0 时 return → $loopReturn,C2.1) */
651
- inLoop?: number;
652
- /** try 嵌套深度(>0 时 return 前 drain throwExits,使 catch 能吸收抽象 throw) */
653
- inTry?: number;
654
- /** 当前 try 的 mark 变量名(return drain 用;避免全局栈顶污染) */
655
- tryMarkName?: string;
656
- };
657
- declare function transpileSource(source: string, opts?: TranspileOptions): string;
658
- declare function transpileFile(file: File, opts?: TranspileOptions): string;
659
- declare function transpileExpression(expr: Expression, opts?: TranspileOptions): string;
660
- /** 解析 + transpile */
661
- declare function transpile(source: string, opts?: TranspileOptions): string;
662
-
663
- /**
664
- * 调用 Abs 一等函数(absFunction impl)或 mock apply。
665
- * 供 B 路径 import 绑定包装:`(...args) => $call(absFn, args)`。
666
- *
667
- * 统一顺序:apply → body → relation → isRelFn(委托 applyAbsFn)。
668
- * 行为对齐说明(与旧 $call 的差异,均属刻意):
669
- * 1. 带 body 的函数也进 call-budget(与 ast-eval 同轨;递归会 truncated 而非爆栈)
670
- * 2. body 抛错 → never(applyAbsFn 内恢复,不返回中间值)
671
- * 3. 无 impl 时 isRelFn 可走 E 路径(旧版恒 unknown)
672
- */
673
-
674
- declare function $call(fn: Abs, args: Abs[]): Abs;
675
-
676
- /**
677
- * B class 规格表(无 call/ast-eval 依赖,避免循环)。
678
- * Abs-eval registerClassDecl 与 transpile $class 共用。
679
- */
680
-
681
- type BClassSpec = {
682
- name: string;
683
- superName?: string;
684
- ctor?: (thisVal: Abs, ...args: Abs[]) => Abs;
685
- methods?: Record<string, (thisVal: Abs, ...args: Abs[]) => Abs>;
686
- staticMethods?: Record<string, (...args: Abs[]) => Abs>;
687
- statics?: Record<string, Abs>;
688
- };
689
- declare function registerBClass(spec: BClassSpec): void;
690
- declare function getBClass(name: string): BClassSpec | undefined;
691
- declare function clearBClasses(): void;
692
-
693
- /**
694
- * B 路径 class:brand 实例 + ctor/method 闭包 + 继承链。
695
- * 方法内 this 由 transpile 改写为 thisVal 参数。
696
- */
697
-
698
- /** 定义类 → 可 new 的 Abs(brand 标记;静态字段挂在 slots) */
699
- declare function $class(name: string, spec: Omit<BClassSpec, "name"> & {
700
- extends?: string;
701
- }): Abs;
702
- /** new C(...) → 空 brand 实例 + ctor 写字段;非类构造走 impl/$call */
703
- declare function $new(cls: Abs | ((...a: unknown[]) => unknown), args: Abs[]): Abs;
704
- /**
705
- * super(...):父类构造写入字段(this 保持子类 brand)。
706
- * transpile: super(a,b) → __this = $super(__this, "Child", [a,b])
707
- */
708
- declare function $super(thisVal: Abs, childName: string, args: Abs[]): Abs;
709
- /** 实例方法调用:沿继承链;类 Abs 上回落 staticMethods;obj 上回落属性函数 */
710
- declare function $invoke(thisVal: Abs, method: string, args: Abs[], loc?: [number, number]): Abs;
711
- /** super.method():从父类起找(跳过自身覆盖) */
712
- declare function $invokeSuper(thisVal: Abs, childName: string, method: string, args: Abs[]): Abs;
713
- declare function $thisGet(thisVal: Abs, key: string): Abs;
714
- declare function $thisSet(thisVal: Abs, key: string, value: Abs): Abs;
715
- /** 解构默认值:undefined(含 sum 成员 / 可能缺失)时用 default 并入非 undefined 部分 */
716
- declare function $orDefault(v: Abs, dflt: () => Abs): Abs;
717
- /** 可选链 a?.b:nullish 短路为 undefined 字面量 */
718
- declare function $optionalGet(o: Abs, key: string): Abs;
719
- /** 可选链 a?.m():nullish 短路为 undefined */
720
- declare function $optionalInvoke(thisVal: Abs, method: string, args: Abs[]): Abs;
721
- /** 静态方法:cls.staticMethod(args) */
722
- declare function $staticInvoke(cls: Abs, method: string, args: Abs[]): Abs;
723
- /** 计算属性写:o[kAbs] = v。非字面量 key → open + index join(不得写成字面槽 "?") */
724
- declare function $setKey(o: Abs, key: Abs, value: Abs): Abs;
725
-
726
- /**
727
- * 成员缺失诊断(B 路径 + Abs ast-eval 共用)。
728
- * 无 $call / transpile 依赖,避免 ast-eval ↔ calls 循环。
729
- */
730
-
731
- type BMemberDiag = {
732
- kind: "method" | "property";
733
- name: string;
734
- /** 接收者 prim 类型名,或 "unknown" / "object"(C0.5 闭 shape 缺槽) */
735
- receiver: string;
736
- line?: number;
737
- column?: number;
738
- /** 调用点来源(provenance):实参字面量 loc 优先,否则最近 $callNamed loc */
739
- origin?: {
740
- line: number;
741
- column: number;
742
- };
743
- /** 覆盖默认 no-method 码;C0.5 用 nudo:missing-slot */
744
- code?: string;
745
- };
746
- /** 给实参 Abs 打 provenance(调用点参数字面量) */
747
- declare function tagAbsOrigin(a: Abs, loc: {
748
- line: number;
749
- column: number;
750
- }): void;
751
- declare function getAbsOrigin(a: Abs | undefined): {
752
- line: number;
753
- column: number;
754
- } | undefined;
755
- declare function setMemberDiagCollector(c: ((d: BMemberDiag) => void) | null): void;
756
- declare function recordMemberDiag(d: BMemberDiag): void;
757
- /**
758
- * prim 接收者上的未知成员 → 诊断。
759
- * 返回 true 表示确定缺失(number 上任意方法;string 上表外方法)。
760
- */
761
- declare function notePrimMemberMissing(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): boolean;
762
- /**
763
- * unknown 接收者上的成员访问 → unknown-recv(与 TypeValue 口径对齐:
764
- * object/instance/refined/promise 静默;其余记一条)。
765
- */
766
- declare function noteUnknownMemberMissing(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): boolean;
767
- /** 方法分派失败时的统一记账(prim / unknown;obj/brand/promise 静默) */
768
- declare function noteMemberDispatchMiss(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): void;
769
- /** host(service analysisConfig)在 B-path 前设置;默认 false */
770
- declare function setEvalMissingSlotEnabled(enabled: boolean): void;
771
- declare function isEvalMissingSlotEnabled(): boolean;
772
- /** per-analysis 作用域:body 内 flag 隔离,结束后自动恢复外层值 */
773
- declare function runWithEvalMissingSlot<T>(enabled: boolean, body: () => T): T;
774
- /**
775
- * 闭对象 shape 上缺失字段且**求值真实走到**该读取 → `nudo:missing-slot`。
776
- * 仅展示/草稿提示;不参与 handwritten check 义务(C0:禁止 body AST 预扫)。
777
- */
778
- declare function noteObjSlotMissing(recv: Abs | undefined, name: string, loc?: [number, number]): boolean;
779
-
780
- /**
781
- * B 路径调用点记录:transpile 把 `f(args)` 改成 $callNamed,
782
- * 分析时可收集 call@ 所需的 AbsCallRecord。
783
- * 成员缺失诊断见 member-diag.ts(与 ast-eval 共用,避免循环依赖)。
784
- */
785
-
786
- type BCallRecord = {
787
- fnName: string;
788
- args: Abs[];
789
- result: Abs;
790
- callLoc?: {
791
- line: number;
792
- column: number;
793
- };
794
- threw?: boolean;
795
- };
796
- declare function setBCallCollector(collector: ((r: BCallRecord) => void) | null): void;
797
- declare function getBCallCollector(): ((r: BCallRecord) => void) | null;
798
- /**
799
- * 按名调用并记录。
800
- * loc: [line, column](1-based line,0-based column,与 Babel 一致)
801
- * argLocs: 与 args 对齐的实参字面量源位置(provenance;无 loc 用 null)
802
- */
803
- declare function $callNamed(name: string, fn: unknown, args: Abs[], loc?: [number, number], argLocs?: Array<[number, number] | null | undefined>): Abs;
804
-
805
- /**
806
- * 进程内 B 路径执行:transpile 源码 → new Function 跑在 runtime 上。
807
- * 不写临时文件;相对 import 用注入的 AbsModuleExports / JS 导出绑定。
808
- *
809
- * mode:
810
- * - "exec"(默认):执行全部顶层(含副作用)
811
- * - "analyze":只保留函数声明与纯字面量 const;跳过顶层表达式/循环/if
812
- * ——分析入口安全,不触发 fetch 等顶层副作用
813
- */
814
-
815
- type RunTranspiledOptions = {
816
- /** 说明符 → 依赖导出(host 模块图或 runTranspiled 产物) */
817
- modules?: Record<string, AbsModuleExports | Record<string, unknown>>;
818
- maxLoopIters?: number;
819
- /** analyze = 跳过效应性顶层语句 */
820
- mode?: "exec" | "analyze";
821
- /** @nudo:replace 注入值:varName → Abs */
822
- replacements?: Record<string, Abs>;
823
- /** @nudo:replace 匹配表:传给 transpile */
824
- replacementTargets?: Array<{
825
- target: string;
826
- varName: string;
827
- stmtStart?: number;
828
- stmtEnd?: number;
829
- }>;
830
- /** @nudo:as 注入值:varName → Abs */
831
- asOverrides?: Record<string, Abs>;
832
- /** @nudo:as 语句范围表 */
833
- asOverrideTargets?: Array<{
834
- varName: string;
835
- stmtStart: number;
836
- stmtEnd: number;
837
- }>;
838
- /** @nudo:env 全局 Abs(JSON/Math/console…)→ 作用域绑定 */
839
- envGlobals?: Record<string, Abs>;
840
- };
841
- /**
842
- * 执行一段 B 路径程序,返回顶层 `export function` / `export const`。
843
- */
844
- declare function runTranspiled(source: string, opts?: RunTranspiledOptions): Record<string, unknown>;
845
- type TranspiledCallResult = {
846
- result: Abs;
847
- /** 非 never 表示函数可能 throw 该类型 */
848
- throws: Abs;
849
- };
850
- /** 调用 runTranspiled 导出(捕获 $throw) */
851
- declare function callTranspiledExportFull(exports: Record<string, unknown>, name: string, args: Abs[]): TranspiledCallResult;
852
- /** 调用 runTranspiled 导出(仅结果) */
853
- declare function callTranspiledExport(exports: Record<string, unknown>, name: string, args: Abs[]): Abs;
854
-
855
- export { $add as $, type Abs as A, $get as B, type Confidence as C, $gt as D, $idx as E, $idxSet as F, $invoke as G, type HofSite as H, $invokeSuper as I, $isForkExit as J, $join as K, $le as L, $len as M, $lit as N, $loopReturn as O, type Phi as P, $lt as Q, type RelSource as R, type Shape as S, type Term as T, $mod as U, $mul as V, $ne as W, $neLoose as X, $neg as Y, $new as Z, $not as _, type AstEnv as a, getAbsOrigin as a$, $nullishTest as a0, $obj as a1, $objRest as a2, $optionalGet as a3, $optionalInvoke as a4, $orDefault as a5, $pushLoopExit as a6, $regex as a7, $rethrowIfNudoReturn as a8, $set as a9, type TranspileOptions as aA, type TranspiledCallResult as aB, abs as aC, absToString as aD, alphaOf as aE, and as aF, anyVar as aG, app as aH, applyCallbackAbs as aI, asAbs as aJ, asAbsVal as aK, betaOf as aL, bindImports as aM, bool as aN, boolLit as aO, callAtFunctionBoundary as aP, callTranspiledExport as aQ, callTranspiledExportFull as aR, clearBClasses as aS, collectAbsExports as aT, confJoin as aU, createHofCollectCtx as aV, currentExecPhi as aW, emptyPhi as aX, eq as aY, ge as aZ, geNum as a_, $setKey as aa, $spread as ab, $staticInvoke as ac, $sub as ad, $super as ae, $switch as af, $thisGet as ag, $thisSet as ah, $throw as ai, $tryCurrentMark as aj, $tryMark as ak, $tryPopMark as al, $tryTakeSince as am, $typeof as an, $while as ao, $whileSeq as ap, $yield as aq, type BCallRecord as ar, type BClassSpec as as, type BMemberDiag as at, DEFAULT_MAX_LOOP_ITERS as au, type HofCollectCtx as av, type LiteralValue as aw, NudoReturn as ax, NudoThrow as ay, type RunTranspiledOptions as az, type Pred as b, substAbs as b$, getBCallCollector as b0, getBClass as b1, gt as b2, gtNum as b3, implies as b4, instantiateReturn as b5, isArrMutator as b6, isDefinitelyFalse as b7, isDefinitelyTrue as b8, isEvalMissingSlotEnabled as b9, or as bA, pFalse as bB, pTrue as bC, phiAnd as bD, predEquals as bE, predToString as bF, predVars as bG, projectFlatMapResult as bH, promoteParamShape as bI, ptypeof as bJ, pushLoopExit as bK, pushThrowExit as bL, recordMemberDiag as bM, registerBClass as bN, runTranspiled as bO, runWithEvalMissingSlot as bP, runWithLoopExits as bQ, setApplyCallbackHost as bR, setBCallCollector as bS, setEvalMissingSlotEnabled as bT, setMemberDiagCollector as bU, shapeOfTerm as bV, shapeToString as bW, simplifyTerm as bX, snapshotAbs as bY, str as bZ, strLit as b_, isExactLit as ba, isNudoReturn as bb, isNudoThrow as bc, isNumPrim as bd, isRelFn as be, isStrPrim as bf, le as bg, leNum as bh, lit as bi, litTruth as bj, litValue as bk, lt as bl, ltNum as bm, mapElementFallback as bn, namespaceNameOf as bo, ne as bp, never as bq, not as br, noteMemberDispatchMiss as bs, noteObjSlotMissing as bt, notePrimMemberMissing as bu, noteUnknownMemberMissing as bv, num as bw, numLit as bx, numVar as by, obj as bz, type AbsModuleExports as c, substPred as c0, substPredAbs as c1, tagAbsOrigin as c2, takeLoopExits as c3, takeThrowExits as c4, termEquals as c5, termToString as c6, transpile as c7, transpileExpression as c8, transpileFile as c9, transpileSource as ca, tryPromoteDirectCall as cb, tryPromoteForOfIteratee as cc, tryPromoteHofCallback as cd, tryPromoteReceiverAsArr as ce, undefAbs as cf, unknown as cg, v as ch, withExecPhi as ci, type PrimName as d, $arr as e, $arrMutContainer as f, $arrRest as g, $async as h, $asyncReturn as i, $await as j, $call as k, $callNamed as l, $catchVal as m, $class as n, $concat as o, $div as p, $elems as q, $eq as r, $eqLoose as s, $fnVal as t, $for as u, $forIter as v, $forOf as w, $fork as x, $ge as y, $gen as z };