@nudojs/core 1.0.0 → 1.1.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,761 +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
- declare const never: Abs;
162
- /** 分析无信息 */
163
- declare const unknown: Abs;
164
- /** 带 term 的 any(generalize type-var) */
165
- declare function anyVar(id: string, conf?: Confidence): Abs;
166
- declare function num(): Abs;
167
- declare function str(): Abs;
168
- declare function bool(): Abs;
169
- declare function numLit(value: number): Abs;
170
- declare function strLit(value: string): Abs;
171
- declare function boolLit(value: boolean): Abs;
172
- /** 带项的符号数,例如参数 x */
173
- declare function numVar(id: string, pred?: Pred, conf?: Confidence): Abs;
174
- declare function obj(slots: Record<string, {
175
- value: Abs;
176
- optional?: boolean;
177
- }>): Abs;
178
- declare function confJoin(a: Confidence, b: Confidence): Confidence;
179
- declare function absToString(a: Abs): string;
180
- declare function shapeToString(s: Shape): string;
181
- /** 从 term 反推 prim shape */
182
- declare function shapeOfTerm(t: Term): Shape;
183
- /** 构造带项的结果 Abs;pred 相对 term */
184
- declare function abs(shape: Shape, term: Term | undefined, pred: Pred | undefined, conf: Confidence): Abs;
185
- declare function isNumPrim(a: Abs): boolean;
186
- declare function isStrPrim(a: Abs): boolean;
187
- /** 置信度:字面量全确定 */
188
- declare function isExactLit(a: Abs): boolean;
189
- declare function litValue(a: Abs): LiteralValue | undefined;
190
-
191
- /**
192
- * HOF 关系 Abs:无 body 的外延关系(paramTypes → returnType)的判定、
193
- * α 替换与应用。类型变量仍是 term var;关系仍是 fn 形状上的外延槽。
194
- */
195
-
196
- /**
197
- * 关系来源标记:P4 豁免与 diagnostics 依赖它,禁止隐式猜。
198
- * - promote:使用驱动提升(generalize symbolic / instantiate 局部)
199
- * - refine:@nudo:refine 契约
200
- * - relationFn:harvest/mock/测试直接写入 fnRels 时的预留来源(P4 error 路径)
201
- */
202
- type RelSource = "promote" | "refine" | "relationFn";
203
- type HofSite = {
204
- /** 形参名(函数形参) */
205
- param: string;
206
- /** 输入侧 term:实参的 term(element 的 var/lit/app);map 1 个、reduce 2 个 */
207
- argTerms: Term[];
208
- /** 输出侧:归纳出的返回 Abs */
209
- result: Abs;
210
- /** 源位置,便于 diagnostics */
211
- loc?: {
212
- line: number;
213
- column: number;
214
- };
215
- };
216
- /**
217
- * run 局部 collector(与 Phi 并列,不进 Φ 合并)。
218
- * symbolic 一次跑:安装并沉淀到 PolyFn;instantiate 重跑:装 throwaway
219
- * 副本——形状提升仍生效,结果不写回共享状态(见 generalize.ts run())。
220
- */
221
- type HofCollectCtx = {
222
- /** 本次归纳的形参名集合(身份判定用) */
223
- paramNames: ReadonlySet<string>;
224
- /** 本次 typeParams 的 α id 集合(term 复用白名单) */
225
- alphaIds: Set<string>;
226
- /** fresh α 计数 */
227
- freshSeq: {
228
- n: number;
229
- };
230
- sites: HofSite[];
231
- fnRels: Map<string, {
232
- abs: Abs;
233
- source: RelSource;
234
- }>;
235
- entryShapes: Map<string, {
236
- abs: Abs;
237
- source: RelSource;
238
- }>;
239
- };
240
- declare function createHofCollectCtx(paramNames: ReadonlySet<string>, alphaIds: Iterable<string>): HofCollectCtx;
241
- /**
242
- * 仅当 term 是 var 且 id ∈ alphaIds(本次 typeParams)时复用;否则 fresh α。
243
- * 绝不把调用点具体值/字面量冻进关系。
244
- */
245
- declare function alphaOf(absOrTerm: Abs | Term | undefined, ctx: HofCollectCtx): Term;
246
- /** 共享输出变量 B:${param}(§5.1 P2 钉死) */
247
- declare function betaOf(param: string): Term;
248
- /**
249
- * 提升写入载体:替换 env.vars map 项,禁止 mutate 共享 Abs。
250
- * arrival-first:已有 fn/arr 形状 → 拒绝新观测。
251
- * 返回是否真正写入。
252
- */
253
- declare function promoteParamShape(env: AstEnv, param: string, promotedShape: Shape, opts?: {
254
- loc?: {
255
- line: number;
256
- column: number;
257
- };
258
- recordSite?: boolean;
259
- }): boolean;
260
- /**
261
- * 挂载点①:方法派发 miss。receiver 是形参 Identifier 且 shape 为 any/未知,
262
- * 方法名为 filter/map/reduce/flatMap → 提升为 arr(自身 var)。
263
- */
264
- declare function tryPromoteReceiverAsArr(env: AstEnv, receiverName: string, method: string, loc?: {
265
- line: number;
266
- column: number;
267
- }): Abs | undefined;
268
- /**
269
- * for-of 迭代对象提升(applyEach 型):`for (const x of items)`,
270
- * items 为形参且仍是 any/unknown → arr(自身 var)。不依赖方法名。
271
- */
272
- declare function tryPromoteForOfIteratee(env: AstEnv, iterateeName: string, loc?: {
273
- line: number;
274
- column: number;
275
- }): Abs | undefined;
276
- /**
277
- * 挂载点②:CallExpression callee = 形参 Identifier 直接调用 p(x) / p(a,b)。
278
- * 提升为 fn(paramTypes=[αOf(args)], returnType=B:param)。
279
- */
280
- declare function tryPromoteDirectCall(env: AstEnv, calleeName: string, args: Abs[], loc?: {
281
- line: number;
282
- column: number;
283
- }): Abs | undefined;
284
- /**
285
- * 挂载点③:HOF 回调实参。回调是 Identifier ∈ paramNames 且尚未有 fn 形状。
286
- * 按方法名提升:map/flatMap → fn([αOf(el)], B:param);filter → fn([αOf(el)], bool);
287
- * reduce → fn([αOf(init), αOf(el)], B:param)。
288
- */
289
- declare function tryPromoteHofCallback(env: AstEnv, cbName: string, method: string, argAbses: Abs[], loc?: {
290
- line: number;
291
- column: number;
292
- }): Abs | undefined;
293
- /** deep-ish copy for snapshot(新对象,不是 env.vars 同一引用) */
294
- declare function snapshotAbs(a: Abs): Abs;
295
- /**
296
- * 「有可用外延签名」判定:唯一权威定义。
297
- *
298
- * 名实说明:isRelFn **不要求** term 是 var。完全单态的具体签名
299
- * (paramTypes=[number], returnType=string)同样满足——map 有槽就用。
300
- * 多态(term=var)只是其中一种形态;名字里的 Rel 指「关系槽可用」。
301
- *
302
- * 收紧条件——仅有 returnType 而 paramTypes 与 arity 不对齐时,不算 rel,
303
- * 防止半截签名在 map 里冒充关系。
304
- */
305
- declare function isRelFn(a: Abs | undefined | null): boolean;
306
- /**
307
- * pred 三条规则:
308
- * 1. var ∈ map + 实参有 term → 换 term,可归约则归约,否则残余保留
309
- * 2. var ∈ map + 实参无 term → pred → true(conf 由调用方降级)
310
- * 3. var ∉ map(自由 α)→ 原样保留
311
- */
312
- declare function substPredAbs(p: Pred, map: ReadonlyMap<string, Abs>): {
313
- pred: Pred;
314
- dropped: boolean;
315
- };
316
- /**
317
- * α 替换:map 的 key 是 term var id,value 是替换 Abs。
318
- * var ∉ map 原样保留(含自由 α 上的 term/pred)。
319
- */
320
- declare function substAbs(a: Abs, map: ReadonlyMap<string, Abs>): Abs;
321
- /**
322
- * relation-only / isRelFn 的应用:按 paramTypes 做 α 替换得到 returnType。
323
- * impl.relation 槽优先于 shape.returnType。重复 α 先绑定保留。
324
- */
325
- declare function instantiateReturn(fn: Abs, args: Abs[]): Abs;
326
- /**
327
- * 回调统一入口(单点定义)。A–F + sum:
328
- * A Node inline | B apply | C body | D relation | E isRelFn | F unknown
329
- *
330
- * 实现委托 ast-eval 的 applyAbsFn(已含 sum / D / E / body 优先)。
331
- * Identifier 解析层:env.vars 有 Abs → B–E;env.fns 有 → callFunction;否则 unknown。
332
- *
333
- * 为避免 hof ↔ ast-eval 循环依赖,本函数由宿主在运行时绑定。
334
- *
335
- * 依赖说明:宿主在 `ast-eval.ts` 模块加载时注册(副作用)。
336
- * 只 import hof.ts 而未加载 ast-eval 时,fallback 仅认 relation/isRelFn。
337
- * 与 §5.1 的「禁止全局 collector」不同——这里是无状态委托钩子,不是 run 局部状态。
338
- */
339
- type ApplyCallbackHost = (cb: Abs | {
340
- type: string;
341
- }, args: Abs[], env: unknown, phi: unknown, budget: unknown) => Abs;
342
- /**
343
- * ast-eval 模块加载时注册(副作用)。
344
- * 必须经 `ast-eval.ts`(或其依赖方:exec/call、exec/class、generalize)加载,
345
- * 才能启用 Identifier/env.fns/inline body 路径;只 import hof.ts 时 fallback
346
- * 仅认 relation/isRelFn。勿在多份 ast-eval 实例下各写各的——双包/双副本会覆盖。
347
- */
348
- declare function setApplyCallbackHost(fn: ApplyCallbackHost): void;
349
- declare function applyCallbackAbs(cb: Abs | {
350
- type: string;
351
- }, args: Abs[], env: unknown, phi: unknown, budget: unknown): Abs;
352
- /** undefined 值的统一 Abs 表示(forEach/find 等) */
353
- declare function undefAbs(): Abs;
354
- /**
355
- * map 元素投影:body 在符号实参上跑出 unknown 时,
356
- * 用 shape.returnType 槽,conf 由调用方按 path/partial 处理。
357
- * rel 回调不会走到这里(instantiateReturn 已给出结果)。
358
- */
359
- declare function mapElementFallback(cbAbs: Abs | undefined, elem: Abs, out: Abs): Abs;
360
- /**
361
- * flatMap 统一结果:展开后的元素 join 成 arr(γ)。
362
- * JS flatMap 永远返回 Array;双路径共用此投影,禁止一边 tuple 一边 arr。
363
- * 同 term 元素 first-wins(joinAbs 会丢 β 身份,见 design §5.1)。
364
- */
365
- declare function projectFlatMapResult(arrConf: Confidence, mapped: Abs[]): Abs;
366
- /** 从 map/filter/reduce 回调实参里取出 Abs(Identifier 已绑定或直接 Abs) */
367
- declare function asAbs(v: unknown): Abs | undefined;
368
-
369
- /**
370
- * AstEnv:抽象求值环境类型。
371
- * 独立成文件:hof/language/leq/abs-fn/abs-modules 等消费方只依赖此类型,
372
- * 不必再为拿一个类型 import ast-eval 巨石(TS 模块环)。
373
- * ast-eval.ts 对本类型做兼容 re-export,外部消费路径不变。
374
- */
375
-
376
- type AstEnv = {
377
- vars: Map<string, Abs>;
378
- /** 用户函数:name → { params, body } */
379
- fns: Map<string, {
380
- params: string[];
381
- body: Node;
382
- async?: boolean;
383
- kind?: string;
384
- }>;
385
- /** class 表(旁路,withVar 必须保留) */
386
- classes?: Map<string, unknown>;
387
- /** 当前正在求值的方法所属类名(super.x() 从它的父类派发) */
388
- currentOwner?: string;
389
- /** P2:generalize symbolic 跑的 HOF collector(run 局部,不进 Φ) */
390
- hofCollect?: HofCollectCtx;
391
- };
392
-
393
- /**
394
- * Abs 模块面(无 fs):import 绑定 + export 收集。
395
- * 解析路径/读文件在 host;core 只吃「specifier → 导出表」。
396
- */
397
-
398
- type AbsModuleExports = {
399
- named: Record<string, Abs>;
400
- default?: Abs;
401
- };
402
- /** 把 import 说明符绑定进 env(宿主已求值依赖) */
403
- declare function bindImports(node: ImportDeclaration, env: AstEnv, modules: Record<string, AbsModuleExports>): void;
404
- /**
405
- * 从已求值 env + AST 收集 ESM 导出。
406
- * 支持:export function/const、export { a, b as c }、export default(具名)、
407
- * 以及带 source 的 re-export(`export { a } from "mod"` / `export * from "mod"`——
408
- * 需 host 传入已求值 modules)。
409
- */
410
- declare function collectAbsExports(file: File, env: AstEnv, modules?: Record<string, AbsModuleExports>): AbsModuleExports;
411
-
412
- /**
413
- * B 路径运行时:transpile 后的程序在 Node 上执行时,值就是 Abs。
414
- * 与 AST 解释器语义同构;TypeValue 不再是求值载体。
415
- */
416
-
417
- declare function currentExecPhi(): Phi;
418
- declare function withExecPhi<T>(p: Phi, body: () => T): T;
419
- declare function $add(a: Abs, b: Abs): Abs;
420
- declare function $sub(a: Abs, b: Abs): Abs;
421
- declare function $mul(a: Abs, b: Abs): Abs;
422
- declare function $div(a: Abs, b: Abs): Abs;
423
- declare function $mod(a: Abs, b: Abs): Abs;
424
- declare function $neg(a: Abs): Abs;
425
- declare function $typeof(a: Abs): Abs;
426
- declare function $not(a: Abs): Abs;
427
- declare function $eq(a: Abs, b: Abs): Abs;
428
- declare function $ne(a: Abs, b: Abs): Abs;
429
- declare function $lt(a: Abs, b: Abs): Abs;
430
- declare function $le(a: Abs, b: Abs): Abs;
431
- declare function $gt(a: Abs, b: Abs): Abs;
432
- declare function $ge(a: Abs, b: Abs): Abs;
433
- declare function $join(a: Abs, b: Abs): Abs;
434
- /**
435
- * transpile 泄漏的 JS 函数值 → 一等 fn Abs。
436
- * B 路径把函数声明/表达式编译成真实 JS 函数;它们流进对象槽、
437
- * 元组、join 等 Abs 结构时不能裸存——下游(bridge/leq/join)读 `.shape`。
438
- * 参数名无法从运行时函数恢复(用 fn.length → argN,与 analyzer 的
439
- * extractParamNames 回退口径一致);带真实参数名走 $fnVal(transpile 侧)。
440
- */
441
- declare function asAbsVal(v: unknown): Abs;
442
- /** 函数表达式 → 一等 fn Abs(transpile 侧带真实参数名;异步 body 包 $async) */
443
- declare function $fnVal(params: string[], impl: (...args: Abs[]) => Abs): Abs;
444
- /** 字面量 → Abs(transpile 侧数字/字符串/布尔/null/undefined) */
445
- declare function $lit(v: unknown): Abs;
446
- /** JS 真值:字面量按 Boolean(v);对象形恒真;不可判 → undefined */
447
- declare function litTruth(a: Abs): boolean | undefined;
448
- declare function isDefinitelyTrue(a: Abs): boolean;
449
- declare function isDefinitelyFalse(a: Abs): boolean;
450
- /**
451
- * if:两侧都探索(抽象条件),具体条件短路。
452
- */
453
- declare function $fork(test: Abs, consequent: () => Abs, alternate?: () => Abs): Abs;
454
- declare const DEFAULT_MAX_LOOP_ITERS = 8;
455
- /**
456
- * for 的惰性展开:生成器只负责「按上限吐状态」。
457
- * 抽象条件无法诚实终止——消费者必须自带 maxIters。
458
- */
459
- declare function $forIter(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, body: (s: Abs) => Abs, maxIters?: number): Generator<{
460
- state: Abs;
461
- test: Abs;
462
- afterBody: Abs;
463
- }, void, void>;
464
- /**
465
- * 有界 for:unroll ≤ maxIters,每步「可能退出」的态 join;
466
- * 相邻态 leq 视为不动点提前停。
467
- */
468
- declare function $for(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, body: (s: Abs) => Abs, maxIters?: number): Abs;
469
- /** 数组字面量 → ≤cap tuple(逐元素精确)/ >cap arr;策略与 ast-eval 同源(containers.ts) */
470
- declare function $arr(items: Abs[]): Abs;
471
- /** 下标读 a[i];字面量 i 走 tuple 精确投影,否则并所有元素;string[i] → 单字符 */
472
- declare function $idx(a: Abs, i: Abs): Abs;
473
- /** 下标写 a[i]=v → 新 tuple(越界写按 JS 语义增长,空洞为 undefined) */
474
- declare function $idxSet(a: Abs, i: Abs, value: Abs): Abs;
475
- /** 数组/字符串长度 */
476
- declare function $len(a: Abs): Abs;
477
- /** 对象字面量 → Abs obj */
478
- declare function $obj(slots: Record<string, Abs>): Abs;
479
- /** 对象展开 { ...a, b } */
480
- declare function $spread(a: Abs, b: Abs): Abs;
481
- /** 数组连接 [...a, ...b] / [...a, x];结果超 cap 时与字面量同策略降 arr */
482
- declare function $concat(a: Abs, b: Abs): Abs;
483
- /** 元素列表(tuple 展开;arr 抽象) */
484
- declare function $elems(a: Abs): Abs[];
485
- /**
486
- * for-of:对 iterable 每个元素跑 body;有界展开。
487
- * body(item, i) 可返回 void;状态由外部 JS 变量承接。
488
- */
489
- declare function $forOf(iterable: Abs, body: (item: Abs, index: Abs) => void, maxIters?: number): void;
490
- /**
491
- * 命名空间身份表:transpile 后 `Math.max(0, x)` 的接收者是宿主 JS 全局对象
492
- * (非 Abs)。按对象身份识别命名空间,路由到 Abs builtin 表。
493
- */
494
- declare function namespaceNameOf(v: unknown): string | undefined;
495
- /** 正则字面量 → RegExp brand(source/flags 进 slots,供 exec/test 精确执行) */
496
- declare function $regex(pattern: string, flags?: string): Abs;
497
- /** 成员读:obj.slots[key];缺失 → undefined 字面量;brand 解包内层 */
498
- declare function $get(o: Abs, key: string, opts?: {
499
- silent?: boolean;
500
- }): Abs;
501
- /** 成员写:返回新 obj/brand(不可变更新) */
502
- declare function $set(o: Abs, key: string, value: Abs): Abs;
503
- /**
504
- * 有界 while:state 线程穿 test/step(与 $for 同折叠语义)。
505
- * 抽象条件无法诚实终止——必须 maxIters。
506
- */
507
- declare function $while(init: Abs, test: (s: Abs) => Abs, step: (s: Abs) => Abs, maxIters?: number): Abs;
508
- /**
509
- * 顺序 while(具体/可变闭包):body 内对 JS 变量赋值。
510
- * 适合 transpile `let i; while (…) { i = … }`;抽象条件仍靠预算截断,
511
- * 不保证 exit-join 健全——健全路径用 $while/$for + 状态对象。
512
- */
513
- declare function $whileSeq(test: () => Abs, body: () => void, maxIters?: number): void;
514
- /** B 路径 throw 载荷:携带 Abs 抛出值 */
515
- declare class NudoThrow extends Error {
516
- readonly absValue: Abs;
517
- constructor(absValue: Abs);
518
- }
519
- /** transpile `throw x` → `$throw(x)` */
520
- declare function $throw(v: Abs): never;
521
- declare function isNudoThrow(e: unknown): e is NudoThrow;
522
- /** catch 参数:从 NudoThrow 取出 Abs,否则 unknown */
523
- declare function $catchVal(e: unknown): Abs;
524
- /**
525
- * async 函数体包进 thunk,返回值经 wrapPromise。
526
- */
527
- declare function $async(thunk: () => Abs): Abs;
528
- /** await → 解包 eff("promise") */
529
- declare function $await(v: Abs): Abs;
530
- /** async 直接 return 的 coerce */
531
- declare function $asyncReturn(v: Abs): Abs;
532
- /** function* 体:收集所有 yield 值为 tuple Abs */
533
- declare function $gen(body: () => void): Abs;
534
- /** yield v:压入当前生成器收集器;表达式值用 unknown */
535
- declare function $yield(v: Abs): Abs;
536
- /**
537
- * switch:具体 disc 选中匹配 case;抽象 disc 并所有分支。
538
- */
539
- declare function $switch(disc: Abs, cases: Array<{
540
- test: Abs;
541
- run: () => Abs;
542
- }>, dflt?: () => Abs): Abs;
543
-
544
- /**
545
- * B 路径 transpile:JS AST → 可在 Node 上执行的抽象值程序(源码字符串)。
546
- * 运算符改为 $add/$sub/…;if 改为 $fork;for 改为 $for。
547
- * 值类型是 Abs;副作用与模块仍由 host mock/注入。
548
- */
549
-
550
- type TranspileOptions = {
551
- /** 运行时 import 说明符 */
552
- runtimeImport?: string;
553
- maxLoopIters?: number;
554
- /** 方法体内 this 的绑定名(transpile class 时注入) */
555
- thisParam?: string;
556
- /** 当前类名(super 派发用) */
557
- className?: string;
558
- /** 原始源码(@nudo:replace 按节点文本匹配) */
559
- source?: string;
560
- /** 替换表:归一化目标文本 → 注入变量名;可选语句范围 */
561
- replacements?: Array<{
562
- target: string;
563
- varName: string;
564
- /** 仅该语句范围内生效(1-based 行) */
565
- stmtStart?: number;
566
- stmtEnd?: number;
567
- }>;
568
- /** @nudo:as:覆盖紧随语句的 init / return */
569
- asOverrides?: Array<{
570
- varName: string;
571
- stmtStart: number;
572
- stmtEnd: number;
573
- }>;
574
- };
575
- declare function transpileSource(source: string, opts?: TranspileOptions): string;
576
- declare function transpileFile(file: File, opts?: TranspileOptions): string;
577
- declare function transpileExpression(expr: Expression, opts?: TranspileOptions): string;
578
- /** 解析 + transpile */
579
- declare function transpile(source: string, opts?: TranspileOptions): string;
580
-
581
- /**
582
- * 调用 Abs 一等函数(absFunction impl)或 mock apply。
583
- * 供 B 路径 import 绑定包装:`(...args) => $call(absFn, args)`。
584
- *
585
- * 统一顺序:apply → body → relation → isRelFn(委托 applyAbsFn)。
586
- * 行为对齐说明(与旧 $call 的差异,均属刻意):
587
- * 1. 带 body 的函数也进 call-budget(与 ast-eval 同轨;递归会 truncated 而非爆栈)
588
- * 2. body 抛错 → never(applyAbsFn 内恢复,不返回中间值)
589
- * 3. 无 impl 时 isRelFn 可走 E 路径(旧版恒 unknown)
590
- */
591
-
592
- declare function $call(fn: Abs, args: Abs[]): Abs;
593
-
594
- /**
595
- * B class 规格表(无 call/ast-eval 依赖,避免循环)。
596
- * Abs-eval registerClassDecl 与 transpile $class 共用。
597
- */
598
-
599
- type BClassSpec = {
600
- name: string;
601
- superName?: string;
602
- ctor?: (thisVal: Abs, ...args: Abs[]) => Abs;
603
- methods?: Record<string, (thisVal: Abs, ...args: Abs[]) => Abs>;
604
- staticMethods?: Record<string, (...args: Abs[]) => Abs>;
605
- statics?: Record<string, Abs>;
606
- };
607
- declare function registerBClass(spec: BClassSpec): void;
608
- declare function getBClass(name: string): BClassSpec | undefined;
609
- declare function clearBClasses(): void;
610
-
611
- /**
612
- * B 路径 class:brand 实例 + ctor/method 闭包 + 继承链。
613
- * 方法内 this 由 transpile 改写为 thisVal 参数。
614
- */
615
-
616
- /** 定义类 → 可 new 的 Abs(brand 标记;静态字段挂在 slots) */
617
- declare function $class(name: string, spec: Omit<BClassSpec, "name"> & {
618
- extends?: string;
619
- }): Abs;
620
- /** new C(...) → 空 brand 实例 + ctor 写字段;非类构造走 impl/$call */
621
- declare function $new(cls: Abs | ((...a: unknown[]) => unknown), args: Abs[]): Abs;
622
- /**
623
- * super(...):父类构造写入字段(this 保持子类 brand)。
624
- * transpile: super(a,b) → __this = $super(__this, "Child", [a,b])
625
- */
626
- declare function $super(thisVal: Abs, childName: string, args: Abs[]): Abs;
627
- /** 实例方法调用:沿继承链;类 Abs 上回落 staticMethods;obj 上回落属性函数 */
628
- declare function $invoke(thisVal: Abs, method: string, args: Abs[], loc?: [number, number]): Abs;
629
- /** super.method():从父类起找(跳过自身覆盖) */
630
- declare function $invokeSuper(thisVal: Abs, childName: string, method: string, args: Abs[]): Abs;
631
- declare function $thisGet(thisVal: Abs, key: string): Abs;
632
- declare function $thisSet(thisVal: Abs, key: string, value: Abs): Abs;
633
- /** 解构默认值:undefined 时用 default */
634
- declare function $orDefault(v: Abs, dflt: () => Abs): Abs;
635
- /** 可选链 a?.b:nullish 短路为 undefined 字面量 */
636
- declare function $optionalGet(o: Abs, key: string): Abs;
637
- /** 可选链 a?.m():nullish 短路为 undefined */
638
- declare function $optionalInvoke(thisVal: Abs, method: string, args: Abs[]): Abs;
639
- /** 静态方法:cls.staticMethod(args) */
640
- declare function $staticInvoke(cls: Abs, method: string, args: Abs[]): Abs;
641
- /** 计算属性写:o[kAbs] = v */
642
- declare function $setKey(o: Abs, key: Abs, value: Abs): Abs;
643
-
644
- /**
645
- * 成员缺失诊断(B 路径 + Abs ast-eval 共用)。
646
- * 无 $call / transpile 依赖,避免 ast-eval ↔ calls 循环。
647
- */
648
-
649
- type BMemberDiag = {
650
- kind: "method" | "property";
651
- name: string;
652
- /** 接收者 prim 类型名,或 "unknown" */
653
- receiver: string;
654
- line?: number;
655
- column?: number;
656
- /** 调用点来源(provenance):实参字面量 loc 优先,否则最近 $callNamed loc */
657
- origin?: {
658
- line: number;
659
- column: number;
660
- };
661
- };
662
- /** 给实参 Abs 打 provenance(调用点参数字面量) */
663
- declare function tagAbsOrigin(a: Abs, loc: {
664
- line: number;
665
- column: number;
666
- }): void;
667
- declare function getAbsOrigin(a: Abs | undefined): {
668
- line: number;
669
- column: number;
670
- } | undefined;
671
- declare function setMemberDiagCollector(c: ((d: BMemberDiag) => void) | null): void;
672
- declare function recordMemberDiag(d: BMemberDiag): void;
673
- /**
674
- * prim 接收者上的未知成员 → 诊断。
675
- * 返回 true 表示确定缺失(number 上任意方法;string 上表外方法)。
676
- */
677
- declare function notePrimMemberMissing(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): boolean;
678
- /**
679
- * unknown 接收者上的成员访问 → unknown-recv(与 TypeValue 口径对齐:
680
- * object/instance/refined/promise 静默;其余记一条)。
681
- */
682
- declare function noteUnknownMemberMissing(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): boolean;
683
- /** 方法分派失败时的统一记账(prim / unknown;obj/brand/promise 静默) */
684
- declare function noteMemberDispatchMiss(recv: Abs | undefined, name: string, kind: "method" | "property", loc?: [number, number]): void;
685
-
686
- /**
687
- * B 路径调用点记录:transpile 把 `f(args)` 改成 $callNamed,
688
- * 分析时可收集 call@ 所需的 AbsCallRecord。
689
- * 成员缺失诊断见 member-diag.ts(与 ast-eval 共用,避免循环依赖)。
690
- */
691
-
692
- type BCallRecord = {
693
- fnName: string;
694
- args: Abs[];
695
- result: Abs;
696
- callLoc?: {
697
- line: number;
698
- column: number;
699
- };
700
- threw?: boolean;
701
- };
702
- declare function setBCallCollector(collector: ((r: BCallRecord) => void) | null): void;
703
- declare function getBCallCollector(): ((r: BCallRecord) => void) | null;
704
- /**
705
- * 按名调用并记录。
706
- * loc: [line, column](1-based line,0-based column,与 Babel 一致)
707
- * argLocs: 与 args 对齐的实参字面量源位置(provenance;无 loc 用 null)
708
- */
709
- declare function $callNamed(name: string, fn: unknown, args: Abs[], loc?: [number, number], argLocs?: Array<[number, number] | null | undefined>): Abs;
710
-
711
- /**
712
- * 进程内 B 路径执行:transpile 源码 → new Function 跑在 runtime 上。
713
- * 不写临时文件;相对 import 用注入的 AbsModuleExports / JS 导出绑定。
714
- *
715
- * mode:
716
- * - "exec"(默认):执行全部顶层(含副作用)
717
- * - "analyze":只保留函数声明与纯字面量 const;跳过顶层表达式/循环/if
718
- * ——分析入口安全,不触发 fetch 等顶层副作用
719
- */
720
-
721
- type RunTranspiledOptions = {
722
- /** 说明符 → 依赖导出(host 模块图或 runTranspiled 产物) */
723
- modules?: Record<string, AbsModuleExports | Record<string, unknown>>;
724
- maxLoopIters?: number;
725
- /** analyze = 跳过效应性顶层语句 */
726
- mode?: "exec" | "analyze";
727
- /** @nudo:replace 注入值:varName → Abs */
728
- replacements?: Record<string, Abs>;
729
- /** @nudo:replace 匹配表:传给 transpile */
730
- replacementTargets?: Array<{
731
- target: string;
732
- varName: string;
733
- stmtStart?: number;
734
- stmtEnd?: number;
735
- }>;
736
- /** @nudo:as 注入值:varName → Abs */
737
- asOverrides?: Record<string, Abs>;
738
- /** @nudo:as 语句范围表 */
739
- asOverrideTargets?: Array<{
740
- varName: string;
741
- stmtStart: number;
742
- stmtEnd: number;
743
- }>;
744
- /** @nudo:env 全局 Abs(JSON/Math/console…)→ 作用域绑定 */
745
- envGlobals?: Record<string, Abs>;
746
- };
747
- /**
748
- * 执行一段 B 路径程序,返回顶层 `export function` / `export const`。
749
- */
750
- declare function runTranspiled(source: string, opts?: RunTranspiledOptions): Record<string, unknown>;
751
- type TranspiledCallResult = {
752
- result: Abs;
753
- /** 非 never 表示函数可能 throw 该类型 */
754
- throws: Abs;
755
- };
756
- /** 调用 runTranspiled 导出(捕获 $throw) */
757
- declare function callTranspiledExportFull(exports: Record<string, unknown>, name: string, args: Abs[]): TranspiledCallResult;
758
- /** 调用 runTranspiled 导出(仅结果) */
759
- declare function callTranspiledExport(exports: Record<string, unknown>, name: string, args: Abs[]): Abs;
760
-
761
- export { $add as $, type AstEnv as A, $idxSet as B, type Confidence as C, $invoke as D, $invokeSuper as E, $join as F, $le as G, type HofSite as H, $len as I, $lit as J, $lt as K, $mod as L, $mul as M, $ne as N, $neg as O, type Phi as P, $new as Q, type RelSource as R, type Shape as S, type Term as T, $not as U, $obj as V, $optionalGet as W, $optionalInvoke as X, $orDefault as Y, $regex as Z, $set as _, type Abs as a, litTruth as a$, $setKey as a0, $spread as a1, $staticInvoke as a2, $sub as a3, $super as a4, $switch as a5, $thisGet as a6, $thisSet as a7, $throw as a8, $typeof as a9, callTranspiledExportFull as aA, clearBClasses as aB, collectAbsExports as aC, confJoin as aD, createHofCollectCtx as aE, currentExecPhi as aF, emptyPhi as aG, eq as aH, ge as aI, geNum as aJ, getAbsOrigin as aK, getBCallCollector as aL, getBClass as aM, gt as aN, gtNum as aO, implies as aP, instantiateReturn as aQ, isDefinitelyFalse as aR, isDefinitelyTrue as aS, isExactLit as aT, isNudoThrow as aU, isNumPrim as aV, isRelFn as aW, isStrPrim as aX, le as aY, leNum as aZ, lit as a_, $while as aa, $whileSeq as ab, $yield as ac, type BCallRecord as ad, type BClassSpec as ae, type BMemberDiag as af, DEFAULT_MAX_LOOP_ITERS as ag, type HofCollectCtx as ah, NudoThrow as ai, type RunTranspiledOptions as aj, type TranspileOptions as ak, type TranspiledCallResult as al, abs as am, absToString as an, alphaOf as ao, and as ap, anyVar as aq, app as ar, applyCallbackAbs as as, asAbs as at, asAbsVal as au, betaOf as av, bindImports as aw, bool as ax, boolLit as ay, callTranspiledExport as az, type Pred as b, litValue as b0, lt as b1, ltNum as b2, mapElementFallback as b3, namespaceNameOf as b4, ne as b5, never as b6, not as b7, noteMemberDispatchMiss as b8, notePrimMemberMissing as b9, strLit as bA, substAbs as bB, substPred as bC, substPredAbs as bD, tagAbsOrigin as bE, termEquals as bF, termToString as bG, transpile as bH, transpileExpression as bI, transpileFile as bJ, transpileSource as bK, tryPromoteDirectCall as bL, tryPromoteForOfIteratee as bM, tryPromoteHofCallback as bN, tryPromoteReceiverAsArr as bO, undefAbs as bP, unknown as bQ, v as bR, withExecPhi as bS, noteUnknownMemberMissing as ba, num as bb, numLit as bc, numVar as bd, obj as be, or as bf, pFalse as bg, pTrue as bh, phiAnd as bi, predEquals as bj, predToString as bk, predVars as bl, projectFlatMapResult as bm, promoteParamShape as bn, ptypeof as bo, recordMemberDiag as bp, registerBClass as bq, runTranspiled as br, setApplyCallbackHost as bs, setBCallCollector as bt, setMemberDiagCollector as bu, shapeOfTerm as bv, shapeToString as bw, simplifyTerm as bx, snapshotAbs as by, str as bz, type AbsModuleExports as c, type PrimName as d, $arr as e, $async as f, $asyncReturn as g, $await as h, $call as i, $callNamed as j, $catchVal as k, $class as l, $concat as m, $div as n, $elems as o, $eq as p, $fnVal as q, $for as r, $forIter as s, $forOf as t, $fork as u, $ge as v, $gen as w, $get as x, $gt as y, $idx as z };