@fraczak/k 8.2.0 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +73 -0
- package/DOCS/CANONICAL_PATTERNS.md +349 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +12 -4
- package/DOCS/REPL.md +30 -3
- package/Examples/arithmetics.k +31 -14
- package/Examples/bnat.k +13 -6
- package/Examples/buda.k +9 -4
- package/Examples/byte.k +23 -9
- package/Examples/ieee.k +102 -34
- package/Examples/karatsuba-mult.k +23 -9
- package/Examples/nat.k +3 -2
- package/Examples/poly.k +30 -16
- package/Examples/vhdl_to_k.k +7 -2
- package/README.md +33 -5
- package/backend-api.mjs +6 -1
- package/backends/llvm/src/cli.mjs +3 -33
- package/backends/wasm/src/cli.mjs +3 -33
- package/backends/wasm/src/wasm.mjs +1 -29
- package/browser/vfs-data.mjs +8 -8
- package/canonical-pattern.mjs +407 -0
- package/k.mjs +12 -29
- package/kvm.mjs +2 -28
- package/named-patterns.mjs +35 -2
- package/object.mjs +137 -20
- package/objects/compile.mjs +2 -29
- package/objects/validate.mjs +13 -1
- package/package.json +1 -1
- package/parser.jison +72 -10
- package/parser.mjs +114 -49
- package/pretty.mjs +19 -4
- package/repl.mjs +319 -104
- package/scripts/build-repl-html.mjs +1 -1
- package/tests/test-k-object.mjs +5 -1
- package/tests/test-named-patterns.mjs +439 -0
- package/tests/test-repl.mjs +163 -12
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// canonical-pattern.mjs
|
|
2
|
+
// Canonical representation, hash resolution, and export helpers.
|
|
3
|
+
|
|
4
|
+
import { annotate } from "./index.mjs";
|
|
5
|
+
import { patternGraphToCanonical, prettyFilter } from "./pretty.mjs";
|
|
6
|
+
import { patternToPropertyList } from "./codecs/runtime/pattern-json.mjs";
|
|
7
|
+
import { exportPatternGraph } from "./codecs/runtime/codec.mjs";
|
|
8
|
+
import { closedPatternToCodeHash } from "./repl-codecs.mjs";
|
|
9
|
+
import { expandFilter } from "./named-patterns.mjs";
|
|
10
|
+
|
|
11
|
+
function collectFilterKnots(filter) {
|
|
12
|
+
const knots = new Set();
|
|
13
|
+
function walk(node) {
|
|
14
|
+
if (!node || typeof node !== "object") return;
|
|
15
|
+
if (node.name && node.type !== "name") {
|
|
16
|
+
knots.add(node.name);
|
|
17
|
+
}
|
|
18
|
+
if (node.fields) {
|
|
19
|
+
for (const child of Object.values(node.fields)) {
|
|
20
|
+
walk(child);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (node.args) {
|
|
24
|
+
for (const arg of node.args) {
|
|
25
|
+
walk(arg);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
walk(filter);
|
|
30
|
+
return knots;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function skeletonizeFilter(filter, knots) {
|
|
34
|
+
let leafIdx = 0;
|
|
35
|
+
function walk(node) {
|
|
36
|
+
if (!node || typeof node !== "object") return node;
|
|
37
|
+
if (node.type === "name" && knots.has(node.name)) {
|
|
38
|
+
return { ...node };
|
|
39
|
+
}
|
|
40
|
+
if (node.name && node.type !== "name") {
|
|
41
|
+
const knotName = node.name;
|
|
42
|
+
if (node.fields) {
|
|
43
|
+
const newFields = {};
|
|
44
|
+
for (const [k, v] of Object.entries(node.fields)) {
|
|
45
|
+
newFields[k] = walk(v);
|
|
46
|
+
}
|
|
47
|
+
return { ...node, name: knotName, fields: newFields };
|
|
48
|
+
}
|
|
49
|
+
return { ...node };
|
|
50
|
+
}
|
|
51
|
+
if (node.type === "product" || node.type === "union") {
|
|
52
|
+
const newFields = {};
|
|
53
|
+
for (const [k, v] of Object.entries(node.fields || {})) {
|
|
54
|
+
newFields[k] = walk(v);
|
|
55
|
+
}
|
|
56
|
+
return { ...node, fields: newFields };
|
|
57
|
+
}
|
|
58
|
+
if (node.type === "code") {
|
|
59
|
+
return { ...node };
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
type: "name",
|
|
63
|
+
name: `__LEAF_${leafIdx++}__`,
|
|
64
|
+
open: true
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return walk(filter);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function collectParamPaths(filter) {
|
|
71
|
+
const map = {};
|
|
72
|
+
function walk(node, path) {
|
|
73
|
+
if (!node || typeof node !== "object") return;
|
|
74
|
+
if (node.type === "name" && typeof node.name === "string") {
|
|
75
|
+
const m = node.name.match(/^__PARAM_(\d+)__$/);
|
|
76
|
+
if (m) {
|
|
77
|
+
const idx = Number(m[1]);
|
|
78
|
+
if (!map[idx]) map[idx] = [];
|
|
79
|
+
map[idx].push(path);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (node.fields) {
|
|
83
|
+
for (const k of Object.keys(node.fields).sort()) {
|
|
84
|
+
walk(node.fields[k], [...path, k]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
walk(filter, []);
|
|
89
|
+
return map;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function findVarAtPath(filter, path) {
|
|
93
|
+
let curr = filter;
|
|
94
|
+
for (let i = 0; i < path.length; i++) {
|
|
95
|
+
const key = path[i];
|
|
96
|
+
if (!curr?.fields?.[key]) return null;
|
|
97
|
+
curr = curr.fields[key];
|
|
98
|
+
}
|
|
99
|
+
return curr?.name || (curr?.type === "name" ? curr.name : null);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function collectCanonicalVarPaths(canonicalFilter) {
|
|
103
|
+
const paths = {};
|
|
104
|
+
function walk(node, path) {
|
|
105
|
+
if (!node || typeof node !== "object") return;
|
|
106
|
+
const name =
|
|
107
|
+
node.name || (node.type === "name" ? node.name : null);
|
|
108
|
+
if (name && /^X\d+$/.test(name) && !paths[name]) {
|
|
109
|
+
paths[name] = path;
|
|
110
|
+
}
|
|
111
|
+
if (node.fields) {
|
|
112
|
+
for (const k of Object.keys(node.fields).sort()) {
|
|
113
|
+
walk(node.fields[k], [...path, k]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
walk(canonicalFilter, []);
|
|
118
|
+
return paths;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function toAstFilter(node) {
|
|
122
|
+
if (!node || typeof node !== "object") return node;
|
|
123
|
+
const res = { ...node };
|
|
124
|
+
if (
|
|
125
|
+
res.name &&
|
|
126
|
+
/^X\d+$/.test(res.name) &&
|
|
127
|
+
(!res.fields || Object.keys(res.fields).length === 0)
|
|
128
|
+
) {
|
|
129
|
+
res.type = "name";
|
|
130
|
+
res.open = true;
|
|
131
|
+
}
|
|
132
|
+
if (res.fields) {
|
|
133
|
+
res.fields = {};
|
|
134
|
+
for (const [k, v] of Object.entries(node.fields)) {
|
|
135
|
+
res.fields[k] = toAstFilter(v);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return res;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function resolvePatternCanonicalInfo(name, pat, options = {}) {
|
|
142
|
+
if (!pat) return { hash: null, def: null, params: null };
|
|
143
|
+
if (
|
|
144
|
+
pat._canonical &&
|
|
145
|
+
pat._canonicalDef &&
|
|
146
|
+
pat._canonicalParams &&
|
|
147
|
+
pat._aliasArgs
|
|
148
|
+
) {
|
|
149
|
+
return {
|
|
150
|
+
hash: pat._canonical,
|
|
151
|
+
def: pat._canonicalDef,
|
|
152
|
+
params: pat._canonicalParams,
|
|
153
|
+
aliasArgs: pat._aliasArgs,
|
|
154
|
+
canonicalParams: pat._graphParams || [],
|
|
155
|
+
canonicalFilterAst: pat._canonicalFilterAst || null
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (typeof name === "string" && name.startsWith("@")) {
|
|
159
|
+
pat._canonical = name;
|
|
160
|
+
}
|
|
161
|
+
const formalParams = pat.params || [];
|
|
162
|
+
const formalCount = formalParams.length;
|
|
163
|
+
const allPatterns = { ...(options.patterns || {}) };
|
|
164
|
+
if (name && pat) allPatterns[name] = pat;
|
|
165
|
+
|
|
166
|
+
let pathMap = {};
|
|
167
|
+
if (formalCount > 0) {
|
|
168
|
+
try {
|
|
169
|
+
const markerArgs = formalParams.map((_, i) => ({
|
|
170
|
+
type: "name",
|
|
171
|
+
name: `__PARAM_${i}__`
|
|
172
|
+
}));
|
|
173
|
+
const expanded = expandFilter(
|
|
174
|
+
{ type: "pattern_apply", pattern: name, args: markerArgs },
|
|
175
|
+
allPatterns
|
|
176
|
+
);
|
|
177
|
+
pathMap = collectParamPaths(expanded);
|
|
178
|
+
} catch {
|
|
179
|
+
pathMap = {};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let expandedFilter = null;
|
|
184
|
+
try {
|
|
185
|
+
expandedFilter = expandFilter(pat.filter, allPatterns);
|
|
186
|
+
} catch {
|
|
187
|
+
expandedFilter = pat.filter;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!pat.filter) {
|
|
191
|
+
return {
|
|
192
|
+
hash: pat._canonical || null,
|
|
193
|
+
def: null,
|
|
194
|
+
params: formalParams
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const knots = collectFilterKnots(expandedFilter);
|
|
199
|
+
const skeletonFilter = skeletonizeFilter(expandedFilter, knots);
|
|
200
|
+
const source = `? ${prettyFilter(skeletonFilter)}`;
|
|
201
|
+
try {
|
|
202
|
+
const annotated = annotate(source, {
|
|
203
|
+
libraries: options.libraries || [],
|
|
204
|
+
patterns: allPatterns
|
|
205
|
+
});
|
|
206
|
+
const mainRel = annotated.rels.__main__;
|
|
207
|
+
let canonicalFilter = null;
|
|
208
|
+
let graphParams = null;
|
|
209
|
+
let codeHash = null;
|
|
210
|
+
if (
|
|
211
|
+
mainRel?.typePatternGraph &&
|
|
212
|
+
mainRel?.def?.patterns?.[0] != null
|
|
213
|
+
) {
|
|
214
|
+
const rootId = mainRel.typePatternGraph.find(
|
|
215
|
+
mainRel.def.patterns[0]
|
|
216
|
+
);
|
|
217
|
+
const res = patternGraphToCanonical(
|
|
218
|
+
mainRel.typePatternGraph,
|
|
219
|
+
rootId,
|
|
220
|
+
null
|
|
221
|
+
);
|
|
222
|
+
canonicalFilter = res.filter;
|
|
223
|
+
graphParams = res.canonicalParams;
|
|
224
|
+
|
|
225
|
+
const propList = patternToPropertyList(
|
|
226
|
+
exportPatternGraph(mainRel.typePatternGraph, rootId)
|
|
227
|
+
);
|
|
228
|
+
codeHash = closedPatternToCodeHash(propList);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (canonicalFilter) {
|
|
232
|
+
pat._canonicalDef = prettyFilter(canonicalFilter);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const varPaths = collectCanonicalVarPaths(canonicalFilter);
|
|
236
|
+
const tokens = (graphParams || []).map((cVar) => {
|
|
237
|
+
const path = varPaths[cVar];
|
|
238
|
+
return path ? findVarAtPath(expandedFilter, path) : null;
|
|
239
|
+
});
|
|
240
|
+
const tokenCounts = {};
|
|
241
|
+
for (const t of tokens) {
|
|
242
|
+
if (t) tokenCounts[t] = (tokenCounts[t] || 0) + 1;
|
|
243
|
+
}
|
|
244
|
+
const aliasArgs = tokens.map((t) => {
|
|
245
|
+
if (!t) return "(...)";
|
|
246
|
+
if (formalParams.includes(t)) return t;
|
|
247
|
+
if (tokenCounts[t] > 1) return t;
|
|
248
|
+
return "(...)";
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const selectedParams = formalParams.map((_, i) => {
|
|
252
|
+
const paths = pathMap[i] || [];
|
|
253
|
+
for (const p of paths) {
|
|
254
|
+
const v = findVarAtPath(canonicalFilter, p);
|
|
255
|
+
if (v) return v;
|
|
256
|
+
}
|
|
257
|
+
return graphParams?.[i] || `X${i}`;
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const relHash = annotated.relAlias?.__main__ || null;
|
|
261
|
+
const finalHash =
|
|
262
|
+
codeHash || relHash || pat._canonical || null;
|
|
263
|
+
|
|
264
|
+
pat._canonical = finalHash;
|
|
265
|
+
pat._canonicalParams =
|
|
266
|
+
formalCount > 0 ? selectedParams : [];
|
|
267
|
+
pat._graphParams = graphParams || [];
|
|
268
|
+
pat._aliasArgs = aliasArgs;
|
|
269
|
+
pat._canonicalFilterAst = toAstFilter(canonicalFilter);
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
hash: pat._canonical,
|
|
273
|
+
def: pat._canonicalDef || null,
|
|
274
|
+
params: pat._canonicalParams,
|
|
275
|
+
aliasArgs: pat._aliasArgs,
|
|
276
|
+
canonicalParams: pat._graphParams,
|
|
277
|
+
canonicalFilterAst: pat._canonicalFilterAst
|
|
278
|
+
};
|
|
279
|
+
} catch {
|
|
280
|
+
return {
|
|
281
|
+
hash: pat._canonical || null,
|
|
282
|
+
def: pat._canonicalDef || null,
|
|
283
|
+
params: pat._canonicalParams || null
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
hash: pat._canonical || null,
|
|
288
|
+
def: pat._canonicalDef || null,
|
|
289
|
+
params: pat._canonicalParams || null
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function canonicalPatternHash(name, pat, options = {}) {
|
|
294
|
+
return resolvePatternCanonicalInfo(name, pat, options).hash;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function buildExportPreamble(exports = [], libraries = []) {
|
|
298
|
+
if (exports.length === 0) return "";
|
|
299
|
+
const relMap = {};
|
|
300
|
+
const patMap = {};
|
|
301
|
+
for (const lib of libraries) {
|
|
302
|
+
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
303
|
+
if (name !== "__main__") relMap[name] = { hash };
|
|
304
|
+
}
|
|
305
|
+
for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
|
|
306
|
+
patMap[name] = {
|
|
307
|
+
hash,
|
|
308
|
+
params:
|
|
309
|
+
lib.patterns?.[name]?.params ||
|
|
310
|
+
lib.patterns?.[name]?._canonicalParams ||
|
|
311
|
+
lib.patterns?.[hash]?.params ||
|
|
312
|
+
[],
|
|
313
|
+
aliasArgs:
|
|
314
|
+
lib.patterns?.[name]?._aliasArgs ||
|
|
315
|
+
lib.meta?.[hash]?.origins?.find((o) => o.name === name)
|
|
316
|
+
?.aliasArgs ||
|
|
317
|
+
lib.patterns?.[name]?.params ||
|
|
318
|
+
[]
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
for (const [name, pat] of Object.entries(lib.patterns || {})) {
|
|
322
|
+
if (!patMap[name]) {
|
|
323
|
+
patMap[name] = {
|
|
324
|
+
hash: pat._canonical || lib.patternAlias?.[name] || name,
|
|
325
|
+
params: pat.params || [],
|
|
326
|
+
aliasArgs: pat._aliasArgs || pat.params || []
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
331
|
+
for (const origin of entry?.origins || []) {
|
|
332
|
+
const type = origin?.type || entry?.type;
|
|
333
|
+
if (type === "rel") {
|
|
334
|
+
if (origin?.name && origin.name !== "__main__") {
|
|
335
|
+
relMap[origin.name] = { hash };
|
|
336
|
+
}
|
|
337
|
+
} else if (type === "pattern") {
|
|
338
|
+
if (origin?.name) {
|
|
339
|
+
patMap[origin.name] = {
|
|
340
|
+
hash,
|
|
341
|
+
params: origin.params || entry.params || [],
|
|
342
|
+
aliasArgs:
|
|
343
|
+
origin.aliasArgs ||
|
|
344
|
+
entry.aliasArgs ||
|
|
345
|
+
origin.params ||
|
|
346
|
+
[]
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const lines = [];
|
|
355
|
+
for (const spec of exports) {
|
|
356
|
+
const [rawLib, rawLocal] = spec.includes(":")
|
|
357
|
+
? spec.split(":", 2)
|
|
358
|
+
: [spec, spec];
|
|
359
|
+
const isExplicitPattern =
|
|
360
|
+
rawLib.includes("[") || rawLocal.includes("[");
|
|
361
|
+
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
362
|
+
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
363
|
+
|
|
364
|
+
let emitted = false;
|
|
365
|
+
if (!isExplicitPattern && relMap[libName]) {
|
|
366
|
+
const target = relMap[libName];
|
|
367
|
+
const body = target.hash.startsWith("@")
|
|
368
|
+
? target.hash.slice(1)
|
|
369
|
+
: target.hash;
|
|
370
|
+
lines.push(`${localName} = @${body};`);
|
|
371
|
+
emitted = true;
|
|
372
|
+
}
|
|
373
|
+
if (patMap[libName]) {
|
|
374
|
+
if (isExplicitPattern || !emitted || relMap[libName]) {
|
|
375
|
+
const target = patMap[libName];
|
|
376
|
+
const body = target.hash.startsWith("@")
|
|
377
|
+
? target.hash.slice(1)
|
|
378
|
+
: target.hash;
|
|
379
|
+
const paramStr =
|
|
380
|
+
target.params && target.params.length > 0
|
|
381
|
+
? `[${target.params.join(", ")}]`
|
|
382
|
+
: "[]";
|
|
383
|
+
const args = target.aliasArgs || target.params;
|
|
384
|
+
const argStr =
|
|
385
|
+
args && args.length > 0
|
|
386
|
+
? `[${args.join(", ")}]`
|
|
387
|
+
: paramStr;
|
|
388
|
+
lines.push(
|
|
389
|
+
`${localName}${paramStr} = @${body}${argStr};`
|
|
390
|
+
);
|
|
391
|
+
emitted = true;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (!emitted) {
|
|
395
|
+
throw new Error(
|
|
396
|
+
`--export: '${rawLib}' not found in loaded libraries`
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return lines.join("\n") + "\n";
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export default {
|
|
404
|
+
resolvePatternCanonicalInfo,
|
|
405
|
+
canonicalPatternHash,
|
|
406
|
+
buildExportPreamble
|
|
407
|
+
};
|
package/k.mjs
CHANGED
|
@@ -12,7 +12,8 @@ import {
|
|
|
12
12
|
compileLibrary,
|
|
13
13
|
decodeObject,
|
|
14
14
|
objectToFunction,
|
|
15
|
-
loadLibrary
|
|
15
|
+
loadLibrary,
|
|
16
|
+
buildExportPreamble
|
|
16
17
|
} from "./object.mjs";
|
|
17
18
|
|
|
18
19
|
let kScript, inputStream;
|
|
@@ -106,42 +107,24 @@ function compileFile(filePath, libraries, preamble) {
|
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
|
|
110
|
-
if (exports.length === 0) return "";
|
|
111
|
-
const aliasMap = {};
|
|
112
|
-
for (const lib of libraries) {
|
|
113
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
114
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
115
|
-
}
|
|
116
|
-
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
117
|
-
if (entry?.type !== "rel") continue;
|
|
118
|
-
for (const origin of entry?.origins || []) {
|
|
119
|
-
if (origin?.name && origin.name !== "__main__") aliasMap[origin.name] = hash;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const lines = [];
|
|
124
|
-
for (const spec of exports) {
|
|
125
|
-
const [libName, localName] = spec.includes(":") ? spec.split(":", 2) : [spec, spec];
|
|
126
|
-
const hash = aliasMap[libName];
|
|
127
|
-
if (!hash) throw new Error(`--export: '${libName}' not found in loaded libraries`);
|
|
128
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
129
|
-
lines.push(`${localName} = @${body};`);
|
|
130
|
-
}
|
|
131
|
-
return lines.join("\n") + "\n";
|
|
132
|
-
}
|
|
110
|
+
const preamble = buildExportPreamble(exports, libraries);
|
|
133
111
|
|
|
134
112
|
let kScriptStr = (function (arg) {
|
|
135
113
|
if (arg == null) {
|
|
136
114
|
throw new Error("Missing script argument");
|
|
137
115
|
}
|
|
138
116
|
if (arg === "-k") {
|
|
139
|
-
throw new Error(
|
|
117
|
+
throw new Error(
|
|
118
|
+
"-k is no longer supported; " +
|
|
119
|
+
"pass the source/object file path directly"
|
|
120
|
+
);
|
|
140
121
|
}
|
|
141
|
-
if (isFile(arg)) return compileFile(arg, libraries,
|
|
142
|
-
return
|
|
122
|
+
if (isFile(arg)) return compileFile(arg, libraries, preamble);
|
|
123
|
+
return preamble + arg;
|
|
143
124
|
})(args.shift());
|
|
144
|
-
let kScript = typeof kScriptStr === "function"
|
|
125
|
+
let kScript = typeof kScriptStr === "function"
|
|
126
|
+
? kScriptStr
|
|
127
|
+
: k.compile(kScriptStr, { libraries });
|
|
145
128
|
inputStream = (function (arg) {
|
|
146
129
|
if (arg == null) {
|
|
147
130
|
return stdin;
|
package/kvm.mjs
CHANGED
|
@@ -13,7 +13,8 @@ import {
|
|
|
13
13
|
compileLibrary,
|
|
14
14
|
compileObjectBuffer,
|
|
15
15
|
decodeObject,
|
|
16
|
-
loadLibrary
|
|
16
|
+
loadLibrary,
|
|
17
|
+
buildExportPreamble
|
|
17
18
|
} from "./object.mjs";
|
|
18
19
|
import codes from "./codes.mjs";
|
|
19
20
|
import { isMainEntrypoint } from "./codecs/runtime/cli-entry.mjs";
|
|
@@ -41,33 +42,6 @@ function isFile(filePath) {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function buildExportPreamble(exports, libraries) {
|
|
45
|
-
if (exports.length === 0) return "";
|
|
46
|
-
const aliasMap = {};
|
|
47
|
-
for (const lib of libraries) {
|
|
48
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
49
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
50
|
-
}
|
|
51
|
-
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
52
|
-
if (entry?.type !== "rel") continue;
|
|
53
|
-
for (const origin of entry?.origins || []) {
|
|
54
|
-
if (origin?.name && origin.name !== "__main__") {
|
|
55
|
-
aliasMap[origin.name] = hash;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const lines = [];
|
|
61
|
-
for (const spec of exports) {
|
|
62
|
-
const [libName, localName] = spec.includes(":") ? spec.split(":", 2) : [spec, spec];
|
|
63
|
-
const hash = aliasMap[libName];
|
|
64
|
-
if (!hash) throw new Error(`--export: '${libName}' not found in loaded libraries`);
|
|
65
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
66
|
-
lines.push(`${localName} = @${body};`);
|
|
67
|
-
}
|
|
68
|
-
return lines.join("\n") + "\n";
|
|
69
|
-
}
|
|
70
|
-
|
|
71
45
|
function kirFindCode(kir) {
|
|
72
46
|
return (hash) => kir.codes?.[hash] || codes.find(hash);
|
|
73
47
|
}
|
package/named-patterns.mjs
CHANGED
|
@@ -26,13 +26,20 @@ export function expandFilter(
|
|
|
26
26
|
|
|
27
27
|
if (filter.type === "pattern_apply") {
|
|
28
28
|
const patternName = filter.pattern;
|
|
29
|
-
|
|
29
|
+
let template = patterns[patternName];
|
|
30
|
+
if (!template && patternName.startsWith("@")) {
|
|
31
|
+
template = patterns[patternName.slice(1)];
|
|
32
|
+
}
|
|
33
|
+
if (!template && !patternName.startsWith("@")) {
|
|
34
|
+
template = patterns["@" + patternName];
|
|
35
|
+
}
|
|
30
36
|
if (!template) {
|
|
31
37
|
const pos = filter.start
|
|
32
38
|
? ` (lines ${filter.start.line}:${filter.start.column})`
|
|
33
39
|
: "";
|
|
40
|
+
const suffix = patternName.startsWith("@") ? "" : "[]";
|
|
34
41
|
throw new Error(
|
|
35
|
-
`Unknown pattern template '${patternName}
|
|
42
|
+
`Unknown pattern template '${patternName}${suffix}'${pos}`
|
|
36
43
|
);
|
|
37
44
|
}
|
|
38
45
|
|
|
@@ -92,12 +99,33 @@ export function expandFilter(
|
|
|
92
99
|
const nextStack = new Set(stack);
|
|
93
100
|
nextStack.add(patternName);
|
|
94
101
|
|
|
102
|
+
const paramWildcardVars = {};
|
|
103
|
+
for (let i = 0; i < expandedArgs.length; i++) {
|
|
104
|
+
const arg = expandedArgs[i];
|
|
105
|
+
if (
|
|
106
|
+
arg &&
|
|
107
|
+
arg.type === null &&
|
|
108
|
+
arg.open === true &&
|
|
109
|
+
(!arg.fields || Object.keys(arg.fields).length === 0) &&
|
|
110
|
+
!arg.name
|
|
111
|
+
) {
|
|
112
|
+
paramWildcardVars[i] = `_wildcard_np${++uniqueIdRef.val}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
95
116
|
function substitute(node) {
|
|
96
117
|
if (!node || typeof node !== "object") return node;
|
|
97
118
|
|
|
98
119
|
if (node.type === "name") {
|
|
99
120
|
const paramIdx = template.params.indexOf(node.name);
|
|
100
121
|
if (paramIdx !== -1) {
|
|
122
|
+
if (paramWildcardVars[paramIdx]) {
|
|
123
|
+
return {
|
|
124
|
+
type: "name",
|
|
125
|
+
name: paramWildcardVars[paramIdx],
|
|
126
|
+
open: true
|
|
127
|
+
};
|
|
128
|
+
}
|
|
101
129
|
const clonedArg = cloneFilter(expandedArgs[paramIdx]);
|
|
102
130
|
if (node.name && node.name !== template.params[paramIdx]) {
|
|
103
131
|
clonedArg.name = varSubst[node.name] || node.name;
|
|
@@ -246,6 +274,11 @@ export function expandPatternsInRels(rels, patterns = {}, options = {}) {
|
|
|
246
274
|
allPatterns[name] = pat;
|
|
247
275
|
}
|
|
248
276
|
}
|
|
277
|
+
for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
|
|
278
|
+
if (libPatterns[name] && !(hash in allPatterns)) {
|
|
279
|
+
allPatterns[hash] = libPatterns[name];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
249
282
|
}
|
|
250
283
|
}
|
|
251
284
|
|