@kernlang/core 3.4.0 → 3.4.1
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/dist/codegen/body-ts.d.ts +68 -0
- package/dist/codegen/body-ts.js +214 -0
- package/dist/codegen/body-ts.js.map +1 -0
- package/dist/codegen/functions.js +19 -2
- package/dist/codegen/functions.js.map +1 -1
- package/dist/codegen/kern-stdlib.d.ts +63 -0
- package/dist/codegen/kern-stdlib.js +160 -0
- package/dist/codegen/kern-stdlib.js.map +1 -0
- package/dist/codegen/stdlib-preamble.d.ts +19 -0
- package/dist/codegen/stdlib-preamble.js +62 -4
- package/dist/codegen/stdlib-preamble.js.map +1 -1
- package/dist/codegen/type-system.js +15 -1
- package/dist/codegen/type-system.js.map +1 -1
- package/dist/codegen-core.js +7 -0
- package/dist/codegen-core.js.map +1 -1
- package/dist/codegen-expression.d.ts +8 -0
- package/dist/codegen-expression.js +111 -5
- package/dist/codegen-expression.js.map +1 -1
- package/dist/decompiler.js +219 -0
- package/dist/decompiler.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/node-props.d.ts +4 -0
- package/dist/node-props.js.map +1 -1
- package/dist/parser-core.d.ts +7 -1
- package/dist/parser-core.js +3 -2
- package/dist/parser-core.js.map +1 -1
- package/dist/parser-diagnostics.js +5 -2
- package/dist/parser-diagnostics.js.map +1 -1
- package/dist/parser-expression.d.ts +8 -3
- package/dist/parser-expression.js +281 -5
- package/dist/parser-expression.js.map +1 -1
- package/dist/parser-keywords.js +16 -0
- package/dist/parser-keywords.js.map +1 -1
- package/dist/parser-validate-propagation.d.ts +105 -0
- package/dist/parser-validate-propagation.js +684 -0
- package/dist/parser-validate-propagation.js.map +1 -0
- package/dist/parser.d.ts +10 -3
- package/dist/parser.js +11 -5
- package/dist/parser.js.map +1 -1
- package/dist/schema.js +199 -13
- package/dist/schema.js.map +1 -1
- package/dist/semantic-validator.js +9 -5
- package/dist/semantic-validator.js.map +1 -1
- package/dist/spec.d.ts +2 -2
- package/dist/spec.js +13 -1
- package/dist/spec.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/value-ir.d.ts +27 -0
- package/dist/value-ir.js +6 -1
- package/dist/value-ir.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Native KERN handler-body codegen — TypeScript target (slices 1–3).
|
|
2
|
+
*
|
|
3
|
+
* Walks the children of a handler with `lang=kern` and emits a TypeScript
|
|
4
|
+
* body string. Recognized statements:
|
|
5
|
+
*
|
|
6
|
+
* - `let name=X value="EXPR"` — `const X = EXPR;` (slice 1)
|
|
7
|
+
* - `return value="EXPR"` / bare `return` — `return EXPR;` (slice 1)
|
|
8
|
+
* - `if cond="EXPR"` / sibling `else` — `if (EXPR) { … } else { … }` (slice 2c)
|
|
9
|
+
*
|
|
10
|
+
* Statement-level propagation `?` lowers to the same hoisted shape that
|
|
11
|
+
* slice 7 established for raw-body propagation:
|
|
12
|
+
*
|
|
13
|
+
* const __k_t1 = await call();
|
|
14
|
+
* if (__k_t1.kind === 'err') return __k_t1;
|
|
15
|
+
* const u = __k_t1.value;
|
|
16
|
+
*
|
|
17
|
+
* Slice 3 — symmetric `{ code, imports }` shape with the Python target so
|
|
18
|
+
* body-emitter callers have a uniform signature regardless of language.
|
|
19
|
+
* TS's KERN-stdlib lowerings don't currently demand any imports (`Math` is
|
|
20
|
+
* global, `Set`/`Map` are global), so `imports` is typically empty. The
|
|
21
|
+
* `BodyEmitOptions.symbolMap` parameter is currently unused on the TS
|
|
22
|
+
* target — TS preserves the camelCase identifier shape end-to-end — but
|
|
23
|
+
* is plumbed through for parity with the Python emitter (and for any
|
|
24
|
+
* future TS-only renames such as reserved-word collision handling).
|
|
25
|
+
*
|
|
26
|
+
* Slice scope:
|
|
27
|
+
* - Result-flavored propagation only (`'err'` discriminant). Option
|
|
28
|
+
* propagation in native bodies is deferred to slice 8 (typecheck-driven).
|
|
29
|
+
* - `if` requires `cond="EXPR"`. `else` is a sibling node (no condition).
|
|
30
|
+
* `else if` chains land in slice 3 — for slice 2c users nest `if` inside
|
|
31
|
+
* the `else` branch.
|
|
32
|
+
*
|
|
33
|
+
* `gensymCounter` is local to each emit call — every handler gets its own
|
|
34
|
+
* fresh `__k_t1`, `__k_t2`, … sequence (same convention as slice 7).
|
|
35
|
+
*
|
|
36
|
+
* Indentation: the recursive walk threads an `indent` string so nested
|
|
37
|
+
* `if`/`else` branches indent correctly. The caller adds the leading indent
|
|
38
|
+
* for the surrounding function body. */
|
|
39
|
+
import type { IRNode } from '../types.js';
|
|
40
|
+
/** Slice 3e — caller-provided options, parity with the Python body emitter.
|
|
41
|
+
* `symbolMap` is currently unused on the TS target; reserved for future
|
|
42
|
+
* use (e.g., reserved-word renames). */
|
|
43
|
+
export interface BodyEmitOptions {
|
|
44
|
+
symbolMap?: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
/** Slice 3e — public return shape, parity with the Python body emitter.
|
|
47
|
+
* TS's KERN-stdlib lowerings don't currently demand any imports; the
|
|
48
|
+
* `imports` set will typically be empty until a future slice introduces
|
|
49
|
+
* TS-stdlib entries with `requires.ts` (e.g., a `node:crypto` import). */
|
|
50
|
+
export interface BodyEmitResult {
|
|
51
|
+
code: string;
|
|
52
|
+
imports: Set<string>;
|
|
53
|
+
}
|
|
54
|
+
/** Emit the body of a native KERN handler as TypeScript source. Returns
|
|
55
|
+
* the joined body text. Each top-level line is unindented; nested
|
|
56
|
+
* branches indent by 2 spaces per level.
|
|
57
|
+
*
|
|
58
|
+
* Legacy slice 1/2 signature — returns just the code string. Callers
|
|
59
|
+
* that also need the import set (slice 3b parity with Python) should
|
|
60
|
+
* use `emitNativeKernBodyTSWithImports`. */
|
|
61
|
+
export declare function emitNativeKernBodyTS(handlerNode: IRNode, options?: BodyEmitOptions): string;
|
|
62
|
+
/** Slice 3e — context-aware variant returning `{ code, imports }`.
|
|
63
|
+
* TS's KERN-stdlib lowerings don't currently demand any imports; the
|
|
64
|
+
* `imports` set will typically be empty until a future slice introduces
|
|
65
|
+
* TS-stdlib entries with `requires.ts` (e.g., a `node:crypto` import).
|
|
66
|
+
* Provided for symmetry with the Python target so generators that drive
|
|
67
|
+
* both languages have a uniform call shape. */
|
|
68
|
+
export declare function emitNativeKernBodyTSWithImports(handlerNode: IRNode, _options?: BodyEmitOptions): BodyEmitResult;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/** Native KERN handler-body codegen — TypeScript target (slices 1–3).
|
|
2
|
+
*
|
|
3
|
+
* Walks the children of a handler with `lang=kern` and emits a TypeScript
|
|
4
|
+
* body string. Recognized statements:
|
|
5
|
+
*
|
|
6
|
+
* - `let name=X value="EXPR"` — `const X = EXPR;` (slice 1)
|
|
7
|
+
* - `return value="EXPR"` / bare `return` — `return EXPR;` (slice 1)
|
|
8
|
+
* - `if cond="EXPR"` / sibling `else` — `if (EXPR) { … } else { … }` (slice 2c)
|
|
9
|
+
*
|
|
10
|
+
* Statement-level propagation `?` lowers to the same hoisted shape that
|
|
11
|
+
* slice 7 established for raw-body propagation:
|
|
12
|
+
*
|
|
13
|
+
* const __k_t1 = await call();
|
|
14
|
+
* if (__k_t1.kind === 'err') return __k_t1;
|
|
15
|
+
* const u = __k_t1.value;
|
|
16
|
+
*
|
|
17
|
+
* Slice 3 — symmetric `{ code, imports }` shape with the Python target so
|
|
18
|
+
* body-emitter callers have a uniform signature regardless of language.
|
|
19
|
+
* TS's KERN-stdlib lowerings don't currently demand any imports (`Math` is
|
|
20
|
+
* global, `Set`/`Map` are global), so `imports` is typically empty. The
|
|
21
|
+
* `BodyEmitOptions.symbolMap` parameter is currently unused on the TS
|
|
22
|
+
* target — TS preserves the camelCase identifier shape end-to-end — but
|
|
23
|
+
* is plumbed through for parity with the Python emitter (and for any
|
|
24
|
+
* future TS-only renames such as reserved-word collision handling).
|
|
25
|
+
*
|
|
26
|
+
* Slice scope:
|
|
27
|
+
* - Result-flavored propagation only (`'err'` discriminant). Option
|
|
28
|
+
* propagation in native bodies is deferred to slice 8 (typecheck-driven).
|
|
29
|
+
* - `if` requires `cond="EXPR"`. `else` is a sibling node (no condition).
|
|
30
|
+
* `else if` chains land in slice 3 — for slice 2c users nest `if` inside
|
|
31
|
+
* the `else` branch.
|
|
32
|
+
*
|
|
33
|
+
* `gensymCounter` is local to each emit call — every handler gets its own
|
|
34
|
+
* fresh `__k_t1`, `__k_t2`, … sequence (same convention as slice 7).
|
|
35
|
+
*
|
|
36
|
+
* Indentation: the recursive walk threads an `indent` string so nested
|
|
37
|
+
* `if`/`else` branches indent correctly. The caller adds the leading indent
|
|
38
|
+
* for the surrounding function body. */
|
|
39
|
+
import { emitExpression } from '../codegen-expression.js';
|
|
40
|
+
import { parseExpression } from '../parser-expression.js';
|
|
41
|
+
const INDENT_STEP = ' ';
|
|
42
|
+
/** Emit the body of a native KERN handler as TypeScript source. Returns
|
|
43
|
+
* the joined body text. Each top-level line is unindented; nested
|
|
44
|
+
* branches indent by 2 spaces per level.
|
|
45
|
+
*
|
|
46
|
+
* Legacy slice 1/2 signature — returns just the code string. Callers
|
|
47
|
+
* that also need the import set (slice 3b parity with Python) should
|
|
48
|
+
* use `emitNativeKernBodyTSWithImports`. */
|
|
49
|
+
export function emitNativeKernBodyTS(handlerNode, options) {
|
|
50
|
+
return emitNativeKernBodyTSWithImports(handlerNode, options).code;
|
|
51
|
+
}
|
|
52
|
+
/** Slice 3e — context-aware variant returning `{ code, imports }`.
|
|
53
|
+
* TS's KERN-stdlib lowerings don't currently demand any imports; the
|
|
54
|
+
* `imports` set will typically be empty until a future slice introduces
|
|
55
|
+
* TS-stdlib entries with `requires.ts` (e.g., a `node:crypto` import).
|
|
56
|
+
* Provided for symmetry with the Python target so generators that drive
|
|
57
|
+
* both languages have a uniform call shape. */
|
|
58
|
+
export function emitNativeKernBodyTSWithImports(handlerNode, _options) {
|
|
59
|
+
const ctx = { gensymCounter: 0, tryDepth: 0 };
|
|
60
|
+
const code = emitChildrenTS(handlerNode.children ?? [], ctx, '').join('\n');
|
|
61
|
+
return { code, imports: new Set() };
|
|
62
|
+
}
|
|
63
|
+
function emitChildrenTS(children, ctx, indent) {
|
|
64
|
+
const lines = [];
|
|
65
|
+
for (let i = 0; i < children.length; i++) {
|
|
66
|
+
const child = children[i];
|
|
67
|
+
if (child.type === 'let') {
|
|
68
|
+
for (const line of emitLetTS(child, ctx))
|
|
69
|
+
lines.push(`${indent}${line}`);
|
|
70
|
+
}
|
|
71
|
+
else if (child.type === 'return') {
|
|
72
|
+
for (const line of emitReturnTS(child, ctx))
|
|
73
|
+
lines.push(`${indent}${line}`);
|
|
74
|
+
}
|
|
75
|
+
else if (child.type === 'if') {
|
|
76
|
+
const condRaw = String(child.props?.cond ?? '');
|
|
77
|
+
const condIR = parseExpression(condRaw);
|
|
78
|
+
// Slice-2 review fix: propagation `?` in an `if` condition has no
|
|
79
|
+
// sensible single-line lowering; reject early with a clear message
|
|
80
|
+
// pointing users at the let-bind workaround.
|
|
81
|
+
if (condIR.kind === 'propagate') {
|
|
82
|
+
throw new Error("Propagation '?' is not allowed in `if cond=` — bind the call to a `let` first, then test the bound name.");
|
|
83
|
+
}
|
|
84
|
+
lines.push(`${indent}if (${emitExpression(condIR)}) {`);
|
|
85
|
+
for (const sl of emitChildrenTS(child.children ?? [], ctx, indent + INDENT_STEP))
|
|
86
|
+
lines.push(sl);
|
|
87
|
+
const next = children[i + 1];
|
|
88
|
+
if (next && next.type === 'else') {
|
|
89
|
+
lines.push(`${indent}} else {`);
|
|
90
|
+
for (const el of emitChildrenTS(next.children ?? [], ctx, indent + INDENT_STEP))
|
|
91
|
+
lines.push(el);
|
|
92
|
+
i++;
|
|
93
|
+
}
|
|
94
|
+
lines.push(`${indent}}`);
|
|
95
|
+
}
|
|
96
|
+
else if (child.type === 'else') {
|
|
97
|
+
// Slice-2 review fix: orphan `else` (without a preceding `if` sibling)
|
|
98
|
+
// is a structural error — silently dropping it produced confusing
|
|
99
|
+
// miscompiles. The `if` arm above consumes its paired `else` via i++,
|
|
100
|
+
// so reaching one here means it was orphaned.
|
|
101
|
+
throw new Error('`else` must immediately follow an `if` sibling. Found orphan `else` in handler body.');
|
|
102
|
+
}
|
|
103
|
+
else if (child.type === 'try') {
|
|
104
|
+
// Slice 4c — try/catch control flow.
|
|
105
|
+
// Slice 4c review fix (OpenCode + Gemini high): orphan `try` produces
|
|
106
|
+
// a syntactically-incomplete body. Both TS (no-catch try is legal but
|
|
107
|
+
// semantically wrong) and Python (SyntaxError) need a paired catch;
|
|
108
|
+
// require the sibling and fail loud if missing.
|
|
109
|
+
const next = children[i + 1];
|
|
110
|
+
if (!next || next.type !== 'catch') {
|
|
111
|
+
throw new Error('`try` must be immediately followed by a `catch` sibling. Found orphan `try` in handler body.');
|
|
112
|
+
}
|
|
113
|
+
lines.push(`${indent}try {`);
|
|
114
|
+
ctx.tryDepth++;
|
|
115
|
+
for (const sl of emitChildrenTS(child.children ?? [], ctx, indent + INDENT_STEP))
|
|
116
|
+
lines.push(sl);
|
|
117
|
+
ctx.tryDepth--;
|
|
118
|
+
const errName = String(next.props?.name ?? 'e');
|
|
119
|
+
lines.push(`${indent}} catch (${errName}) {`);
|
|
120
|
+
for (const cl of emitChildrenTS(next.children ?? [], ctx, indent + INDENT_STEP))
|
|
121
|
+
lines.push(cl);
|
|
122
|
+
i++;
|
|
123
|
+
lines.push(`${indent}}`);
|
|
124
|
+
}
|
|
125
|
+
else if (child.type === 'catch') {
|
|
126
|
+
throw new Error('`catch` must immediately follow a `try` sibling. Found orphan `catch` in handler body.');
|
|
127
|
+
}
|
|
128
|
+
else if (child.type === 'throw') {
|
|
129
|
+
// Slice 4c — throw statement.
|
|
130
|
+
for (const line of emitThrowTS(child, ctx))
|
|
131
|
+
lines.push(`${indent}${line}`);
|
|
132
|
+
}
|
|
133
|
+
else if (child.type === 'each') {
|
|
134
|
+
// Slice 4d — each loop.
|
|
135
|
+
// Slice 4c+4d review fix (Codex P1): the schema's `each` already
|
|
136
|
+
// declares `name` (binding) and `in` (iterable expression). The
|
|
137
|
+
// earlier slice-4d body-emit read `list`/`as` instead, which meant
|
|
138
|
+
// (a) schema-validated source `each name=x in=items` fell back to
|
|
139
|
+
// `for (const item of [])` (empty list, wrong binding) and
|
|
140
|
+
// (b) tests that used `list`/`as` failed schema validation.
|
|
141
|
+
// Read schema-compliant `name`/`in` first; accept legacy
|
|
142
|
+
// `list`/`as` as a fallback for tests that pre-date this fix.
|
|
143
|
+
const listRaw = String(child.props?.in ?? child.props?.list ?? '[]');
|
|
144
|
+
const asName = String(child.props?.name ?? child.props?.as ?? 'item');
|
|
145
|
+
const listIR = parseExpression(listRaw);
|
|
146
|
+
lines.push(`${indent}for (const ${asName} of ${emitExpression(listIR)}) {`);
|
|
147
|
+
for (const sl of emitChildrenTS(child.children ?? [], ctx, indent + INDENT_STEP))
|
|
148
|
+
lines.push(sl);
|
|
149
|
+
lines.push(`${indent}}`);
|
|
150
|
+
}
|
|
151
|
+
// Other child types fall through silently — slice 3 adds more.
|
|
152
|
+
}
|
|
153
|
+
return lines;
|
|
154
|
+
}
|
|
155
|
+
/** Slice 4c review fix (OpenCode + Gemini critical) — propagation `?`
|
|
156
|
+
* inside a `try` block has no clean lowering. The hoisted err-branch
|
|
157
|
+
* emits `return tmp` which exits the function entirely, BYPASSING the
|
|
158
|
+
* enclosing `catch`. That's almost never what users mean — they wrote
|
|
159
|
+
* `?` to flag a Result.err and (presumably) to let the catch handle
|
|
160
|
+
* it. Reject at codegen with a let-bind hint. Same shape as
|
|
161
|
+
* slice-2's reject-`?`-in-`if-cond` rule. */
|
|
162
|
+
function rejectPropagationInsideTry(ctx) {
|
|
163
|
+
if (ctx.tryDepth > 0) {
|
|
164
|
+
throw new Error("Propagation '?' is not allowed inside a `try` block — `return` from the err branch exits the function and bypasses the enclosing `catch`. " +
|
|
165
|
+
'Bind the call to a `let` outside the try, then use `if x.kind === "err" throw new Error(...)` inside the try, OR use raw `lang=ts`/`lang=python` for the affected handler.');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function emitLetTS(node, ctx) {
|
|
169
|
+
const props = (node.props ?? {});
|
|
170
|
+
const name = String(props.name ?? '_');
|
|
171
|
+
const rawValue = props.value;
|
|
172
|
+
if (rawValue === undefined || rawValue === '') {
|
|
173
|
+
return [`const ${name} = undefined;`];
|
|
174
|
+
}
|
|
175
|
+
const valueIR = parseExpression(String(rawValue));
|
|
176
|
+
if (valueIR.kind === 'propagate' && valueIR.op === '?') {
|
|
177
|
+
rejectPropagationInsideTry(ctx);
|
|
178
|
+
const tmp = `__k_t${++ctx.gensymCounter}`;
|
|
179
|
+
const inner = emitExpression(valueIR.argument);
|
|
180
|
+
return [`const ${tmp} = ${inner};`, `if (${tmp}.kind === 'err') return ${tmp};`, `const ${name} = ${tmp}.value;`];
|
|
181
|
+
}
|
|
182
|
+
return [`const ${name} = ${emitExpression(valueIR)};`];
|
|
183
|
+
}
|
|
184
|
+
function emitReturnTS(node, ctx) {
|
|
185
|
+
const props = (node.props ?? {});
|
|
186
|
+
const rawValue = props.value;
|
|
187
|
+
if (rawValue === undefined || rawValue === '') {
|
|
188
|
+
return [`return;`];
|
|
189
|
+
}
|
|
190
|
+
const valueIR = parseExpression(String(rawValue));
|
|
191
|
+
if (valueIR.kind === 'propagate' && valueIR.op === '?') {
|
|
192
|
+
rejectPropagationInsideTry(ctx);
|
|
193
|
+
const tmp = `__k_t${++ctx.gensymCounter}`;
|
|
194
|
+
const inner = emitExpression(valueIR.argument);
|
|
195
|
+
return [`const ${tmp} = ${inner};`, `if (${tmp}.kind === 'err') return ${tmp};`, `return ${tmp}.value;`];
|
|
196
|
+
}
|
|
197
|
+
return [`return ${emitExpression(valueIR)};`];
|
|
198
|
+
}
|
|
199
|
+
function emitThrowTS(node, ctx) {
|
|
200
|
+
const props = (node.props ?? {});
|
|
201
|
+
const rawValue = props.value;
|
|
202
|
+
if (rawValue === undefined || rawValue === '') {
|
|
203
|
+
return [`throw new Error();`];
|
|
204
|
+
}
|
|
205
|
+
const valueIR = parseExpression(String(rawValue));
|
|
206
|
+
if (valueIR.kind === 'propagate' && valueIR.op === '?') {
|
|
207
|
+
rejectPropagationInsideTry(ctx);
|
|
208
|
+
const tmp = `__k_t${++ctx.gensymCounter}`;
|
|
209
|
+
const inner = emitExpression(valueIR.argument);
|
|
210
|
+
return [`const ${tmp} = ${inner};`, `if (${tmp}.kind === 'err') return ${tmp};`, `throw ${tmp}.value;`];
|
|
211
|
+
}
|
|
212
|
+
return [`throw ${emitExpression(valueIR)};`];
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=body-ts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-ts.js","sourceRoot":"","sources":["../../src/codegen/body-ts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yCAqCyC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AA8B1D,MAAM,WAAW,GAAG,IAAI,CAAC;AAEzB;;;;;;6CAM6C;AAC7C,MAAM,UAAU,oBAAoB,CAAC,WAAmB,EAAE,OAAyB;IACjF,OAAO,+BAA+B,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;AACpE,CAAC;AAED;;;;;gDAKgD;AAChD,MAAM,UAAU,+BAA+B,CAAC,WAAmB,EAAE,QAA0B;IAC7F,MAAM,GAAG,GAAoB,EAAE,aAAa,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC;IAC/D,MAAM,IAAI,GAAG,cAAc,CAAC,WAAW,CAAC,QAAQ,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5E,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,EAA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,OAAO,CAAC,UAAU,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,WAAW,CAAC,IAAY,EAAE,GAAoB;IACrD,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAA4B,CAAC;IAC5D,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC;IAC7B,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,EAAE,EAAE,CAAC;QAC9C,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAChC,CAAC;IACD,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;IAClD,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,IAAI,OAAO,CAAC,EAAE,KAAK,GAAG,EAAE,CAAC;QACvD,0BAA0B,CAAC,GAAG,CAAC,CAAC;QAChC,MAAM,GAAG,GAAG,QAAQ,EAAE,GAAG,CAAC,aAAa,EAAE,CAAC;QAC1C,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC/C,OAAO,CAAC,SAAS,GAAG,MAAM,KAAK,GAAG,EAAE,OAAO,GAAG,2BAA2B,GAAG,GAAG,EAAE,SAAS,GAAG,SAAS,CAAC,CAAC;IAC1G,CAAC;IACD,OAAO,CAAC,SAAS,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -4,9 +4,26 @@
|
|
|
4
4
|
* Extracted from codegen-core.ts for modular codegen architecture.
|
|
5
5
|
*/
|
|
6
6
|
import { propsOf } from '../node-props.js';
|
|
7
|
+
import { emitNativeKernBodyTS } from './body-ts.js';
|
|
7
8
|
import { emitIdentifier, emitTypeAnnotation } from './emitters.js';
|
|
8
9
|
import { dedent, emitDocComment, exportPrefix, getChildren, getFirstChild, getProps, handlerCode } from './helpers.js';
|
|
9
10
|
import { emitParamList } from './type-system.js';
|
|
11
|
+
/** Slice 1 — native KERN handler bodies (`handler lang=kern`).
|
|
12
|
+
* Returns the emitted body when the fn's handler child opts in via `lang=kern`,
|
|
13
|
+
* otherwise returns the legacy raw `<<<…>>>` body via `handlerCode`.
|
|
14
|
+
*
|
|
15
|
+
* Slice 3e — `emitNativeKernBodyTS` returns `{ code, imports }` for parity
|
|
16
|
+
* with the Python target. TS body emit currently never populates `imports`
|
|
17
|
+
* (Math/Map/Set are global, no module-level wiring needed yet), so we just
|
|
18
|
+
* unwrap `code`. Future TS-stdlib entries declaring `requires.ts` would
|
|
19
|
+
* need this caller to inject the imports above the function declaration. */
|
|
20
|
+
function fnBodyCode(node) {
|
|
21
|
+
const handler = getFirstChild(node, 'handler');
|
|
22
|
+
if (handler && getProps(handler).lang === 'kern') {
|
|
23
|
+
return emitNativeKernBodyTS(handler);
|
|
24
|
+
}
|
|
25
|
+
return handlerCode(node);
|
|
26
|
+
}
|
|
10
27
|
const p = getProps;
|
|
11
28
|
const kids = getChildren;
|
|
12
29
|
const firstChild = getFirstChild;
|
|
@@ -52,7 +69,7 @@ export function generateFunction(node) {
|
|
|
52
69
|
const yieldType = emitTypeAnnotation(returns, 'unknown', node);
|
|
53
70
|
// If user already declared AsyncGenerator<...>, use as-is to avoid double-wrapping
|
|
54
71
|
const retClause = yieldType.startsWith('AsyncGenerator<') ? `: ${yieldType}` : `: AsyncGenerator<${yieldType}>`;
|
|
55
|
-
const code =
|
|
72
|
+
const code = fnBodyCode(node);
|
|
56
73
|
lines.push(`${exp}async function* ${name}${generics}(${paramList})${retClause} {`);
|
|
57
74
|
if (code) {
|
|
58
75
|
for (const line of code.split('\n')) {
|
|
@@ -74,7 +91,7 @@ export function generateFunction(node) {
|
|
|
74
91
|
? `: ${emitTypeAnnotation(returns, 'unknown', node)}`
|
|
75
92
|
: '';
|
|
76
93
|
const asyncKw = isAsync ? 'async ' : '';
|
|
77
|
-
const code =
|
|
94
|
+
const code = fnBodyCode(node);
|
|
78
95
|
const signalNode = firstChild(node, 'signal');
|
|
79
96
|
const cleanupNode = firstChild(node, 'cleanup');
|
|
80
97
|
const hasSignal = !!signalNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/codegen/functions.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AACvH,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,MAAM,CAAC,GAAG,QAAQ,CAAC;AACnB,MAAM,IAAI,GAAG,WAAW,CAAC;AACzB,MAAM,UAAU,GAAG,aAAa,CAAC;AAEjC,4EAA4E;AAE5E,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAO,IAAI,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;IAC3D,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;IAC9B,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,KAAK,MAAM,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC;IAC/D,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,KAAK,MAAM,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC;IAClE,MAAM,WAAW,GAAG,KAAK,CAAC,SAAS,KAAK,MAAM,IAAI,KAAK,CAAC,SAAS,KAAK,IAAI,CAAC;IAC3E,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAC/B,gFAAgF;IAChF,4EAA4E;IAC5E,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,CAAC,CAAC;IACpB,CAAC;SAAM,CAAC;QACN,KAAK,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;QAC/C,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;QAClC,KAAK,CAAC,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC,CAAC;QACzC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAChB,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** KERN-stdlib lowering table — slices 2a + 2b.
|
|
2
|
+
*
|
|
3
|
+
* Per the brainstorm-locked design, KERN handler bodies use module-prefixed
|
|
4
|
+
* function calls (`Text.upper(s)`) instead of method dispatch (`s.upper()`).
|
|
5
|
+
* This table maps each KERN-stdlib operation to its per-target template so
|
|
6
|
+
* the SAME KERN source emits idiomatic TS and idiomatic Python.
|
|
7
|
+
*
|
|
8
|
+
* Template shape: each entry's `ts` and `py` fields are template strings
|
|
9
|
+
* with `$0`, `$1`, … placeholders that reference call args by zero-based
|
|
10
|
+
* position. The template is a string (not a structured shape) because the
|
|
11
|
+
* cross-target divergence is irregular enough that any structured shape
|
|
12
|
+
* ends up being "string with knobs". Concrete divergence cases:
|
|
13
|
+
* - `Text.includes(s, sub)` → TS `s.includes(sub)` vs Python `sub in s`
|
|
14
|
+
* (operator, not method)
|
|
15
|
+
* - `List.isEmpty(xs)` → TS `xs.length === 0` vs Python `len(xs) == 0`
|
|
16
|
+
* (compound binop expressions, not method/prop)
|
|
17
|
+
* - `List.join(xs, sep)` → TS `xs.join(sep)` vs Python `sep.join(xs)`
|
|
18
|
+
* (receiver inverted)
|
|
19
|
+
* - `List.last(xs)` → TS `xs[xs.length - 1]` vs Python `xs[-1]`
|
|
20
|
+
* (different subscript expressions)
|
|
21
|
+
* - `Number.floor(n)` → TS `Math.floor(n)` vs Python `math.floor(n)`
|
|
22
|
+
* (different module qualification)
|
|
23
|
+
* Templates handle all of these uniformly.
|
|
24
|
+
*
|
|
25
|
+
* Slices in this table:
|
|
26
|
+
* - 2a: Text upper/lower/length/trim
|
|
27
|
+
* - 2b: Text+ (includes, startsWith, endsWith, split, replace);
|
|
28
|
+
* List (length, isEmpty, includes, first, last, indexOf, join);
|
|
29
|
+
* Map (has, get, size); Number (round, floor, ceil, abs).
|
|
30
|
+
* Future slices may extend further (List.map / List.filter need closures,
|
|
31
|
+
* so they're deferred until closure support — currently never).
|
|
32
|
+
*
|
|
33
|
+
* Diagnostic: when codegen sees `<KnownModule>.<unknownMethod>(...)`, it
|
|
34
|
+
* throws with a Levenshtein did-you-mean. Calls into modules NOT in this
|
|
35
|
+
* table fall through to the default emit path (passthrough). */
|
|
36
|
+
export interface StdlibEntry {
|
|
37
|
+
arity: number;
|
|
38
|
+
ts: string;
|
|
39
|
+
py: string;
|
|
40
|
+
/** Slice 3b — per-target imports required when this lowering is used.
|
|
41
|
+
* The body emitter collects these into a per-handler import set so the
|
|
42
|
+
* generator can emit `import math` (etc.) at the top of the function
|
|
43
|
+
* body. Keys are target names ('ts' / 'py'); values are the import
|
|
44
|
+
* identifier (`'math'` ⇒ `import math`). Undefined when none required. */
|
|
45
|
+
requires?: {
|
|
46
|
+
ts?: string;
|
|
47
|
+
py?: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export declare const KERN_STDLIB: Record<string, Record<string, StdlibEntry>>;
|
|
51
|
+
export declare const KERN_STDLIB_MODULES: Set<string>;
|
|
52
|
+
/** Look up a stdlib lowering by module + method name.
|
|
53
|
+
* Returns null if the module is unknown OR the method is unknown on a known
|
|
54
|
+
* module — callers should distinguish via `KERN_STDLIB_MODULES.has(module)`
|
|
55
|
+
* to surface the right diagnostic. */
|
|
56
|
+
export declare function lookupStdlib(module: string, method: string): StdlibEntry | null;
|
|
57
|
+
/** Suggest the closest method name on a known module via simple Levenshtein
|
|
58
|
+
* membership. Used in error messages. Returns null if no close match exists. */
|
|
59
|
+
export declare function suggestStdlibMethod(module: string, method: string): string | null;
|
|
60
|
+
/** Substitute `$0`, `$1`, … placeholders in a template with the corresponding
|
|
61
|
+
* args. Throws on out-of-range index — that's a programming error in the
|
|
62
|
+
* KERN_STDLIB table, not user input. */
|
|
63
|
+
export declare function applyTemplate(template: string, args: string[]): string;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** KERN-stdlib lowering table — slices 2a + 2b.
|
|
2
|
+
*
|
|
3
|
+
* Per the brainstorm-locked design, KERN handler bodies use module-prefixed
|
|
4
|
+
* function calls (`Text.upper(s)`) instead of method dispatch (`s.upper()`).
|
|
5
|
+
* This table maps each KERN-stdlib operation to its per-target template so
|
|
6
|
+
* the SAME KERN source emits idiomatic TS and idiomatic Python.
|
|
7
|
+
*
|
|
8
|
+
* Template shape: each entry's `ts` and `py` fields are template strings
|
|
9
|
+
* with `$0`, `$1`, … placeholders that reference call args by zero-based
|
|
10
|
+
* position. The template is a string (not a structured shape) because the
|
|
11
|
+
* cross-target divergence is irregular enough that any structured shape
|
|
12
|
+
* ends up being "string with knobs". Concrete divergence cases:
|
|
13
|
+
* - `Text.includes(s, sub)` → TS `s.includes(sub)` vs Python `sub in s`
|
|
14
|
+
* (operator, not method)
|
|
15
|
+
* - `List.isEmpty(xs)` → TS `xs.length === 0` vs Python `len(xs) == 0`
|
|
16
|
+
* (compound binop expressions, not method/prop)
|
|
17
|
+
* - `List.join(xs, sep)` → TS `xs.join(sep)` vs Python `sep.join(xs)`
|
|
18
|
+
* (receiver inverted)
|
|
19
|
+
* - `List.last(xs)` → TS `xs[xs.length - 1]` vs Python `xs[-1]`
|
|
20
|
+
* (different subscript expressions)
|
|
21
|
+
* - `Number.floor(n)` → TS `Math.floor(n)` vs Python `math.floor(n)`
|
|
22
|
+
* (different module qualification)
|
|
23
|
+
* Templates handle all of these uniformly.
|
|
24
|
+
*
|
|
25
|
+
* Slices in this table:
|
|
26
|
+
* - 2a: Text upper/lower/length/trim
|
|
27
|
+
* - 2b: Text+ (includes, startsWith, endsWith, split, replace);
|
|
28
|
+
* List (length, isEmpty, includes, first, last, indexOf, join);
|
|
29
|
+
* Map (has, get, size); Number (round, floor, ceil, abs).
|
|
30
|
+
* Future slices may extend further (List.map / List.filter need closures,
|
|
31
|
+
* so they're deferred until closure support — currently never).
|
|
32
|
+
*
|
|
33
|
+
* Diagnostic: when codegen sees `<KnownModule>.<unknownMethod>(...)`, it
|
|
34
|
+
* throws with a Levenshtein did-you-mean. Calls into modules NOT in this
|
|
35
|
+
* table fall through to the default emit path (passthrough). */
|
|
36
|
+
export const KERN_STDLIB = {
|
|
37
|
+
Text: {
|
|
38
|
+
upper: { arity: 1, ts: '$0.toUpperCase()', py: '$0.upper()' },
|
|
39
|
+
lower: { arity: 1, ts: '$0.toLowerCase()', py: '$0.lower()' },
|
|
40
|
+
length: { arity: 1, ts: '$0.length', py: 'len($0)' },
|
|
41
|
+
trim: { arity: 1, ts: '$0.trim()', py: '$0.strip()' },
|
|
42
|
+
includes: { arity: 2, ts: '$0.includes($1)', py: '$1 in $0' },
|
|
43
|
+
startsWith: { arity: 2, ts: '$0.startsWith($1)', py: '$0.startswith($1)' },
|
|
44
|
+
endsWith: { arity: 2, ts: '$0.endsWith($1)', py: '$0.endswith($1)' },
|
|
45
|
+
split: { arity: 2, ts: '$0.split($1)', py: '$0.split($1)' },
|
|
46
|
+
// Slice-2 review fix: replace-all is the canonical KERN semantics. JS
|
|
47
|
+
// `replace` only swaps the first match; KERN normalizes to TS
|
|
48
|
+
// `replaceAll` (ES2021+) and Python `replace` (default replace-all).
|
|
49
|
+
replace: { arity: 3, ts: '$0.replaceAll($1, $2)', py: '$0.replace($1, $2)' },
|
|
50
|
+
},
|
|
51
|
+
List: {
|
|
52
|
+
length: { arity: 1, ts: '$0.length', py: 'len($0)' },
|
|
53
|
+
isEmpty: { arity: 1, ts: '$0.length === 0', py: 'len($0) == 0' },
|
|
54
|
+
includes: { arity: 2, ts: '$0.includes($1)', py: '$1 in $0' },
|
|
55
|
+
first: { arity: 1, ts: '$0[0]', py: '$0[0]' },
|
|
56
|
+
// Slice-2 review fix: `$0[$0.length - 1]` evaluated `$0` twice; if `$0`
|
|
57
|
+
// is a function call, that's a double-evaluation bug. `.at(-1)` is
|
|
58
|
+
// ES2022+ and matches Python's `[-1]` semantics (single eval, supports
|
|
59
|
+
// negative index).
|
|
60
|
+
last: { arity: 1, ts: '$0.at(-1)', py: '$0[-1]' },
|
|
61
|
+
// Slice-2 review fix: Python `list.index` raises ValueError when the
|
|
62
|
+
// item isn't found; TS `indexOf` returns -1. Match TS by guarding with
|
|
63
|
+
// a containment check.
|
|
64
|
+
indexOf: { arity: 2, ts: '$0.indexOf($1)', py: '($0.index($1) if $1 in $0 else -1)' },
|
|
65
|
+
// Slice-2 review fix: Python `str.join` requires string elements. Wrap
|
|
66
|
+
// with `map(str, …)` so non-string KERN values stringify like JS does.
|
|
67
|
+
join: { arity: 2, ts: '$0.join($1)', py: '$1.join(map(str, $0))' },
|
|
68
|
+
},
|
|
69
|
+
Map: {
|
|
70
|
+
has: { arity: 2, ts: '$0.has($1)', py: '$1 in $0' },
|
|
71
|
+
// Slice-2 review fix: TS `Map.get(k)` returns `undefined` for missing
|
|
72
|
+
// keys. Python `dict[k]` raises KeyError. Use `.get($1)` (Python dicts'
|
|
73
|
+
// safe-access, returns None) for parity.
|
|
74
|
+
get: { arity: 2, ts: '$0.get($1)', py: '$0.get($1)' },
|
|
75
|
+
size: { arity: 1, ts: '$0.size', py: 'len($0)' },
|
|
76
|
+
},
|
|
77
|
+
Number: {
|
|
78
|
+
// Slice 3c — JS `Math.round` rounds half toward +∞ (so Math.round(-1.5) === -1
|
|
79
|
+
// and Math.round(2.5) === 3). Python's built-in `round` is banker's rounding
|
|
80
|
+
// (half-to-even), which diverges on `.5` values. To preserve the JS-flavored
|
|
81
|
+
// KERN AST semantics on the Python target, lower to `math.floor($0 + 0.5)`
|
|
82
|
+
// — a one-line identity that matches JS `Math.round` parity for both
|
|
83
|
+
// positive and negative half-cases. Single-eval because `$0` is substituted
|
|
84
|
+
// once.
|
|
85
|
+
// Slice 3 review fix (Gemini): use `__k_math` alias to avoid shadowing
|
|
86
|
+
// when the user has a body-local binding or param named `math`. The
|
|
87
|
+
// FastAPI generator emits `import math as __k_math` for any handler
|
|
88
|
+
// that references these.
|
|
89
|
+
round: { arity: 1, ts: 'Math.round($0)', py: '__k_math.floor($0 + 0.5)', requires: { py: 'math' } },
|
|
90
|
+
floor: { arity: 1, ts: 'Math.floor($0)', py: '__k_math.floor($0)', requires: { py: 'math' } },
|
|
91
|
+
ceil: { arity: 1, ts: 'Math.ceil($0)', py: '__k_math.ceil($0)', requires: { py: 'math' } },
|
|
92
|
+
abs: { arity: 1, ts: 'Math.abs($0)', py: 'abs($0)' },
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
export const KERN_STDLIB_MODULES = new Set(Object.keys(KERN_STDLIB));
|
|
96
|
+
/** Look up a stdlib lowering by module + method name.
|
|
97
|
+
* Returns null if the module is unknown OR the method is unknown on a known
|
|
98
|
+
* module — callers should distinguish via `KERN_STDLIB_MODULES.has(module)`
|
|
99
|
+
* to surface the right diagnostic. */
|
|
100
|
+
export function lookupStdlib(module, method) {
|
|
101
|
+
const moduleEntries = KERN_STDLIB[module];
|
|
102
|
+
if (!moduleEntries)
|
|
103
|
+
return null;
|
|
104
|
+
return moduleEntries[method] ?? null;
|
|
105
|
+
}
|
|
106
|
+
/** Suggest the closest method name on a known module via simple Levenshtein
|
|
107
|
+
* membership. Used in error messages. Returns null if no close match exists. */
|
|
108
|
+
export function suggestStdlibMethod(module, method) {
|
|
109
|
+
const moduleEntries = KERN_STDLIB[module];
|
|
110
|
+
if (!moduleEntries)
|
|
111
|
+
return null;
|
|
112
|
+
const candidates = Object.keys(moduleEntries);
|
|
113
|
+
let best = null;
|
|
114
|
+
let bestDist = Infinity;
|
|
115
|
+
for (const c of candidates) {
|
|
116
|
+
const d = levenshtein(method, c);
|
|
117
|
+
if (d < bestDist) {
|
|
118
|
+
bestDist = d;
|
|
119
|
+
best = c;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return bestDist <= 2 ? best : null;
|
|
123
|
+
}
|
|
124
|
+
/** Substitute `$0`, `$1`, … placeholders in a template with the corresponding
|
|
125
|
+
* args. Throws on out-of-range index — that's a programming error in the
|
|
126
|
+
* KERN_STDLIB table, not user input. */
|
|
127
|
+
export function applyTemplate(template, args) {
|
|
128
|
+
return template.replace(/\$(\d+)/g, (_match, idxStr) => {
|
|
129
|
+
const idx = Number.parseInt(idxStr, 10);
|
|
130
|
+
if (idx < 0 || idx >= args.length) {
|
|
131
|
+
throw new Error(`KERN-stdlib template references arg index $${idx} but only ${args.length} args provided. Template: ${template}`);
|
|
132
|
+
}
|
|
133
|
+
return args[idx];
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function levenshtein(a, b) {
|
|
137
|
+
if (a === b)
|
|
138
|
+
return 0;
|
|
139
|
+
const m = a.length;
|
|
140
|
+
const n = b.length;
|
|
141
|
+
if (m === 0)
|
|
142
|
+
return n;
|
|
143
|
+
if (n === 0)
|
|
144
|
+
return m;
|
|
145
|
+
const dp = new Array(n + 1);
|
|
146
|
+
for (let j = 0; j <= n; j++)
|
|
147
|
+
dp[j] = j;
|
|
148
|
+
for (let i = 1; i <= m; i++) {
|
|
149
|
+
let prev = dp[0];
|
|
150
|
+
dp[0] = i;
|
|
151
|
+
for (let j = 1; j <= n; j++) {
|
|
152
|
+
const cur = dp[j];
|
|
153
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
154
|
+
dp[j] = Math.min(dp[j] + 1, dp[j - 1] + 1, prev + cost);
|
|
155
|
+
prev = cur;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return dp[n];
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=kern-stdlib.js.map
|
|
@@ -0,0 +1 @@
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CAAC,CAAC,CAAC;IACtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE;QAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,IAAI,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACjB,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5B,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;YAClB,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3C,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC,CAAC;YACxD,IAAI,GAAG,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IACD,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AACf,CAAC"}
|
|
@@ -33,7 +33,26 @@ export interface KernStdlibUsage {
|
|
|
33
33
|
result: boolean;
|
|
34
34
|
/** Module references `Option<…>` somewhere in a type annotation. */
|
|
35
35
|
option: boolean;
|
|
36
|
+
/** Module uses `KernUnwrapError` — auto-emitted by the slice 7 `!`
|
|
37
|
+
* rewriter when a user wrote `expr!`. Optional for back-compat with
|
|
38
|
+
* callers who only construct the result/option flags. */
|
|
39
|
+
unwrap?: boolean;
|
|
36
40
|
}
|
|
37
41
|
export declare function detectKernStdlibUsage(root: IRNode): KernStdlibUsage;
|
|
38
42
|
export declare function kernStdlibPreamble(usage: KernStdlibUsage): string[];
|
|
43
|
+
/** Slice 4 follow-up — inject the preamble INSIDE a Vue/Nuxt SFC's first
|
|
44
|
+
* `<script[ setup]? lang="ts">` block instead of before the SFC. The plain
|
|
45
|
+
* `injectKernStdlibPreamble` would prepend `type Result<…>` text ahead of
|
|
46
|
+
* the SFC, breaking the file's parse.
|
|
47
|
+
*
|
|
48
|
+
* Only matches a TS-language script block. JavaScript script blocks
|
|
49
|
+
* (`lang="js"` or no `lang`) are left alone — the preamble emits TS
|
|
50
|
+
* syntax (generics, type aliases) which JS can't host.
|
|
51
|
+
*
|
|
52
|
+
* When no matching script tag exists (template-only SFC, JS-only SFC),
|
|
53
|
+
* the preamble is dropped entirely — silently injecting it elsewhere
|
|
54
|
+
* would corrupt the file. The handler bodies in such SFCs cannot use
|
|
55
|
+
* `Result<…>` / `Option<…>` types anyway, so the dropped preamble is
|
|
56
|
+
* not load-bearing. */
|
|
57
|
+
export declare function injectKernStdlibPreambleIntoSFC(code: string, preamble: string[]): string;
|
|
39
58
|
export declare function injectKernStdlibPreamble(code: string, preamble: string[]): string;
|