@fraczak/k 8.1.0 → 8.3.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 +63 -0
- package/DOCS/CANONICAL_PATTERNS.md +252 -0
- package/DOCS/NAMED_PATTERNS.md +23 -0
- package/DOCS/PATTERNS.md +5 -0
- package/DOCS/REPL.md +73 -2
- 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 +350 -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 +125 -2
- package/repl.mjs +334 -82
- package/scripts/build-repl-html.mjs +1 -1
- package/tests/test-k-object.mjs +5 -1
- package/tests/test-named-patterns.mjs +363 -0
- package/tests/test-repl.mjs +193 -16
package/repl.mjs
CHANGED
|
@@ -21,7 +21,15 @@ if (typeof globalThis !== "undefined") {
|
|
|
21
21
|
globalThis.isVariant = isVariant;
|
|
22
22
|
globalThis.patternFromFilter = patternFromFilter;
|
|
23
23
|
}
|
|
24
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
patternGraphToCanonical,
|
|
26
|
+
patterns2filters,
|
|
27
|
+
prettyCode,
|
|
28
|
+
prettyFilter
|
|
29
|
+
} from "./pretty.mjs";
|
|
30
|
+
import {
|
|
31
|
+
resolvePatternCanonicalInfo as canonicalPatternInfo
|
|
32
|
+
} from "./canonical-pattern.mjs";
|
|
25
33
|
import {
|
|
26
34
|
compileObject,
|
|
27
35
|
compileLibrary,
|
|
@@ -90,6 +98,7 @@ function createState(options = {}) {
|
|
|
90
98
|
codes: codes.dump(),
|
|
91
99
|
rels: {},
|
|
92
100
|
relAliases: {},
|
|
101
|
+
patternAliases: {},
|
|
93
102
|
typeAliases: {},
|
|
94
103
|
patterns: {},
|
|
95
104
|
codecs: {},
|
|
@@ -111,6 +120,7 @@ function stateLibrary(state) {
|
|
|
111
120
|
codes: state.codes,
|
|
112
121
|
rels: state.rels,
|
|
113
122
|
relAlias: state.relAliases,
|
|
123
|
+
patternAlias: state.patternAliases || {},
|
|
114
124
|
patterns: state.patterns,
|
|
115
125
|
meta: state.meta,
|
|
116
126
|
main: null
|
|
@@ -132,6 +142,27 @@ function aliasPreamble(state, omitName = null) {
|
|
|
132
142
|
for (const [name, hash] of Object.entries(state.relAliases).sort()) {
|
|
133
143
|
if (name !== omitName) lines.push(aliasLine("rel", name, hash));
|
|
134
144
|
}
|
|
145
|
+
for (const [name, hash] of Object.entries(
|
|
146
|
+
state.patternAliases || {}
|
|
147
|
+
).sort()) {
|
|
148
|
+
if (
|
|
149
|
+
name.startsWith("@") ||
|
|
150
|
+
name === hash ||
|
|
151
|
+
name === omitName
|
|
152
|
+
) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const pat = state.patterns?.[name] || state.patterns?.[hash];
|
|
156
|
+
const params = pat?.params || [];
|
|
157
|
+
const paramStr =
|
|
158
|
+
params.length > 0 ? `[${params.join(", ")}]` : "[]";
|
|
159
|
+
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
160
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
161
|
+
const args = info?.aliasArgs || pat?._aliasArgs || params;
|
|
162
|
+
const argStr =
|
|
163
|
+
args && args.length > 0 ? `[${args.join(", ")}]` : paramStr;
|
|
164
|
+
lines.push(`${name}${paramStr} = @${body}${argStr};`);
|
|
165
|
+
}
|
|
135
166
|
return lines.filter(Boolean).join("\n");
|
|
136
167
|
}
|
|
137
168
|
|
|
@@ -203,10 +234,10 @@ function mergeMeta(state, meta = {}) {
|
|
|
203
234
|
function rememberOrigin(state, hash, name, type, source = "<repl>") {
|
|
204
235
|
if (!state.meta[hash]) state.meta[hash] = { type, origins: [] };
|
|
205
236
|
if (state.meta[hash].type == null) state.meta[hash].type = type;
|
|
206
|
-
const
|
|
207
|
-
origin.name === name
|
|
237
|
+
const existing = state.meta[hash].origins.find((origin) =>
|
|
238
|
+
origin.name === name
|
|
208
239
|
);
|
|
209
|
-
if (!
|
|
240
|
+
if (!existing) {
|
|
210
241
|
state.meta[hash].origins.push({
|
|
211
242
|
source,
|
|
212
243
|
name,
|
|
@@ -220,8 +251,13 @@ function recoverAliasesFromMeta(state, lib) {
|
|
|
220
251
|
for (const origin of entry?.origins || []) {
|
|
221
252
|
if (!origin?.name || !NAME_RE.test(origin.name)) continue;
|
|
222
253
|
const type = entry?.type;
|
|
223
|
-
if (
|
|
254
|
+
if (
|
|
255
|
+
type === "code" ||
|
|
256
|
+
(!type && hash in state.codes && !(hash in state.rels))
|
|
257
|
+
) {
|
|
224
258
|
state.typeAliases[origin.name] = hash;
|
|
259
|
+
} else if (type === "pattern") {
|
|
260
|
+
state.patternAliases[origin.name] = hash;
|
|
225
261
|
} else if (type === "rel" || hash in state.rels) {
|
|
226
262
|
state.relAliases[origin.name] = hash;
|
|
227
263
|
}
|
|
@@ -238,16 +274,30 @@ function mergeLibrary(state, lib, source = "<load>", options = {}) {
|
|
|
238
274
|
...(lib.patterns || lib.sourceDefs?.patterns ||
|
|
239
275
|
lib.defs?.patterns || {})
|
|
240
276
|
};
|
|
277
|
+
state.patternAliases = {
|
|
278
|
+
...state.patternAliases,
|
|
279
|
+
...(lib.patternAlias || {})
|
|
280
|
+
};
|
|
241
281
|
mergeMeta(state, lib.meta);
|
|
242
282
|
|
|
243
283
|
if (loadAliases) {
|
|
244
284
|
state.relAliases = { ...state.relAliases, ...(lib.relAlias || {}) };
|
|
245
285
|
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
246
|
-
if (
|
|
286
|
+
if (
|
|
287
|
+
name !== "__main__" &&
|
|
288
|
+
NAME_RE.test(name) &&
|
|
289
|
+
hash in state.rels
|
|
290
|
+
) {
|
|
247
291
|
state.relAliases[name] = hash;
|
|
248
292
|
rememberOrigin(state, hash, name, "rel", source);
|
|
249
293
|
}
|
|
250
294
|
}
|
|
295
|
+
for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
|
|
296
|
+
if (NAME_RE.test(name)) {
|
|
297
|
+
state.patternAliases[name] = hash;
|
|
298
|
+
rememberOrigin(state, hash, name, "pattern", source);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
251
301
|
recoverAliasesFromMeta(state, lib);
|
|
252
302
|
if (state.typeAliases.string && !state.typeAliases.utf8) {
|
|
253
303
|
state.typeAliases.utf8 = state.typeAliases.string;
|
|
@@ -303,23 +353,71 @@ function savedLibrary(state) {
|
|
|
303
353
|
for (const [name, hash] of Object.entries(state.typeAliases)) {
|
|
304
354
|
if (!meta[hash]) meta[hash] = { type: "code", origins: [] };
|
|
305
355
|
if (meta[hash].type == null) meta[hash].type = "code";
|
|
306
|
-
meta[hash].origins.
|
|
356
|
+
if (!meta[hash].origins.some((o) => o.name === name)) {
|
|
357
|
+
meta[hash].origins.push({
|
|
358
|
+
source: "<repl>",
|
|
359
|
+
name,
|
|
360
|
+
compiledAt: now
|
|
361
|
+
});
|
|
362
|
+
}
|
|
307
363
|
}
|
|
308
364
|
for (const [name, hash] of Object.entries(state.relAliases)) {
|
|
309
365
|
if (!meta[hash]) meta[hash] = { type: "rel", origins: [] };
|
|
310
366
|
if (meta[hash].type == null) meta[hash].type = "rel";
|
|
311
|
-
meta[hash].origins.
|
|
367
|
+
if (!meta[hash].origins.some((o) => o.name === name)) {
|
|
368
|
+
meta[hash].origins.push({
|
|
369
|
+
source: "<repl>",
|
|
370
|
+
name,
|
|
371
|
+
compiledAt: now
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
for (const [name, hash] of Object.entries(
|
|
376
|
+
state.patternAliases || {}
|
|
377
|
+
)) {
|
|
378
|
+
const pat = state.patterns?.[name] || state.patterns?.[hash];
|
|
379
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
380
|
+
if (!meta[hash]) {
|
|
381
|
+
meta[hash] = {
|
|
382
|
+
type: "pattern",
|
|
383
|
+
params: info.canonicalParams || pat?.params || [],
|
|
384
|
+
origins: []
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
if (meta[hash].type == null) meta[hash].type = "pattern";
|
|
388
|
+
if (!meta[hash].params && info.canonicalParams) {
|
|
389
|
+
meta[hash].params = info.canonicalParams;
|
|
390
|
+
}
|
|
391
|
+
const existing = meta[hash].origins.find((o) => o.name === name);
|
|
392
|
+
if (!existing) {
|
|
393
|
+
meta[hash].origins.push({
|
|
394
|
+
source: "<repl>",
|
|
395
|
+
name,
|
|
396
|
+
params: pat?.params || [],
|
|
397
|
+
aliasArgs: info.aliasArgs || pat?.params || [],
|
|
398
|
+
compiledAt: now
|
|
399
|
+
});
|
|
400
|
+
} else {
|
|
401
|
+
if (!existing.params && pat?.params) {
|
|
402
|
+
existing.params = pat.params;
|
|
403
|
+
}
|
|
404
|
+
if (!existing.aliasArgs && info.aliasArgs) {
|
|
405
|
+
existing.aliasArgs = info.aliasArgs;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
312
408
|
}
|
|
313
409
|
const rels = reachableRelations(state.rels, Object.values(state.relAliases));
|
|
314
410
|
const storedHashes = new Set([
|
|
315
411
|
...Object.keys(state.codes),
|
|
316
|
-
...Object.keys(rels)
|
|
412
|
+
...Object.keys(rels),
|
|
413
|
+
...Object.values(state.patternAliases || {})
|
|
317
414
|
]);
|
|
318
415
|
return {
|
|
319
416
|
format: "k-object",
|
|
320
417
|
codes: state.codes,
|
|
321
418
|
rels,
|
|
322
419
|
relAlias: state.relAliases,
|
|
420
|
+
patternAlias: state.patternAliases || {},
|
|
323
421
|
compileStats: { sccs: [], sccCount: 0 },
|
|
324
422
|
patterns: state.patterns || {},
|
|
325
423
|
meta: Object.fromEntries(
|
|
@@ -366,63 +464,80 @@ function listAliases(aliases) {
|
|
|
366
464
|
return entries.map(([name, hash]) => `${name} = ${hash}`).join("\n");
|
|
367
465
|
}
|
|
368
466
|
|
|
467
|
+
function resolvePatternCanonicalInfo(state, name, pat) {
|
|
468
|
+
if (!state || !pat) return { hash: null, def: null, params: null };
|
|
469
|
+
const res = canonicalPatternInfo(name, pat, {
|
|
470
|
+
libraries: [stateLibrary(state)],
|
|
471
|
+
patterns: state.patterns
|
|
472
|
+
});
|
|
473
|
+
state.codes = codes.dump();
|
|
474
|
+
return res;
|
|
475
|
+
}
|
|
476
|
+
|
|
369
477
|
function resolvePatternCanonicalName(state, name, pat) {
|
|
370
|
-
|
|
371
|
-
if (pat._canonical) return pat._canonical;
|
|
372
|
-
const params = pat.params || [];
|
|
373
|
-
const source = `? ${name}[${params.join(", ")}]`;
|
|
374
|
-
try {
|
|
375
|
-
restoreCodes(state);
|
|
376
|
-
const annotated = annotate(source, {
|
|
377
|
-
libraries: [stateLibrary(state)],
|
|
378
|
-
patterns: state.patterns
|
|
379
|
-
});
|
|
380
|
-
const mainRel = annotated.rels.__main__;
|
|
381
|
-
if (
|
|
382
|
-
mainRel?.typePatternGraph &&
|
|
383
|
-
mainRel?.def?.patterns?.[0] != null
|
|
384
|
-
) {
|
|
385
|
-
const rootId = mainRel.typePatternGraph.find(
|
|
386
|
-
mainRel.def.patterns[0]
|
|
387
|
-
);
|
|
388
|
-
const propList = patternToPropertyList(
|
|
389
|
-
exportPatternGraph(mainRel.typePatternGraph, rootId)
|
|
390
|
-
);
|
|
391
|
-
const codeHash = closedPatternToCodeHash(propList);
|
|
392
|
-
if (codeHash) {
|
|
393
|
-
state.codes = codes.dump();
|
|
394
|
-
pat._canonical = codeHash;
|
|
395
|
-
return codeHash;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
const relHash = annotated.relAlias?.__main__ || null;
|
|
399
|
-
if (relHash) {
|
|
400
|
-
pat._canonical = relHash;
|
|
401
|
-
return relHash;
|
|
402
|
-
}
|
|
403
|
-
} catch {
|
|
404
|
-
restoreCodes(state);
|
|
405
|
-
return null;
|
|
406
|
-
}
|
|
407
|
-
return null;
|
|
478
|
+
return resolvePatternCanonicalInfo(state, name, pat).hash;
|
|
408
479
|
}
|
|
409
480
|
|
|
410
481
|
function prettyPattern(name, pat, state = null) {
|
|
411
|
-
const
|
|
412
|
-
const
|
|
413
|
-
const
|
|
414
|
-
|
|
482
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
483
|
+
const isHash = typeof name === "string" && name.startsWith("@");
|
|
484
|
+
const paramList =
|
|
485
|
+
isHash && (info.canonicalParams || pat?.params)
|
|
486
|
+
? (info.canonicalParams || pat.params)
|
|
487
|
+
: info.params != null
|
|
488
|
+
? info.params
|
|
489
|
+
: (pat?.params || []);
|
|
490
|
+
const params = paramList.join(", ");
|
|
491
|
+
const filterStr = info.def ||
|
|
492
|
+
(pat?.filter ? prettyFilter(pat.filter) : "()");
|
|
493
|
+
const suffix =
|
|
494
|
+
info.hash && !isHash ? ` -- ${info.hash}` : "";
|
|
415
495
|
return `${name}[${params}] = ${filterStr};${suffix}`;
|
|
416
496
|
}
|
|
417
497
|
|
|
418
|
-
function
|
|
419
|
-
const
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
498
|
+
function listPatternAliases(state) {
|
|
499
|
+
const aliases = [];
|
|
500
|
+
const names = [
|
|
501
|
+
...new Set([
|
|
502
|
+
...Object.keys(state?.patternAliases || {}),
|
|
503
|
+
...Object.keys(state?.patterns || {})
|
|
504
|
+
])
|
|
505
|
+
].filter((name) => !name.startsWith("@")).sort();
|
|
506
|
+
|
|
507
|
+
for (const name of names) {
|
|
508
|
+
const pat =
|
|
509
|
+
state?.patterns?.[name] ||
|
|
510
|
+
(state?.patternAliases?.[name]
|
|
511
|
+
? state?.patterns?.[state.patternAliases[name]]
|
|
512
|
+
: null);
|
|
513
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
514
|
+
const hash =
|
|
515
|
+
info?.hash || state?.patternAliases?.[name] || null;
|
|
516
|
+
if (!hash) continue;
|
|
517
|
+
const formalParams = pat?.params || [];
|
|
518
|
+
const paramStr =
|
|
519
|
+
formalParams.length > 0
|
|
520
|
+
? `[${formalParams.join(",")}]`
|
|
521
|
+
: "[]";
|
|
522
|
+
const args = info?.aliasArgs || formalParams;
|
|
523
|
+
const argStr =
|
|
524
|
+
args.length > 0
|
|
525
|
+
? `[${args.join(",")}]`
|
|
526
|
+
: "[]";
|
|
527
|
+
aliases.push(`${name}${paramStr} = ${hash}${argStr}`);
|
|
528
|
+
}
|
|
529
|
+
if (aliases.length === 0) return "(none)";
|
|
530
|
+
return aliases.join("\n");
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function listPatterns(stateOrPatterns) {
|
|
534
|
+
if (
|
|
535
|
+
stateOrPatterns &&
|
|
536
|
+
(stateOrPatterns.patterns || stateOrPatterns.patternAliases)
|
|
537
|
+
) {
|
|
538
|
+
return listPatternAliases(stateOrPatterns);
|
|
539
|
+
}
|
|
540
|
+
return listPatternAliases({ patterns: stateOrPatterns || {} });
|
|
426
541
|
}
|
|
427
542
|
|
|
428
543
|
function formatCodecOutput(output) {
|
|
@@ -479,6 +594,11 @@ function userDefinedNames(source) {
|
|
|
479
594
|
),
|
|
480
595
|
relNames: Object.keys(parsed.defs.rels).filter((name) =>
|
|
481
596
|
NAME_RE.test(name) && !name.startsWith("@") && name !== "__main__"
|
|
597
|
+
),
|
|
598
|
+
patternNames: Object.keys(parsed.defs.patterns || {}).filter(
|
|
599
|
+
(name) =>
|
|
600
|
+
(NAME_RE.test(name) && !name.startsWith("@")) ||
|
|
601
|
+
(name.startsWith("@") && NAME_RE.test(name.slice(1)))
|
|
482
602
|
)
|
|
483
603
|
};
|
|
484
604
|
}
|
|
@@ -494,7 +614,7 @@ function originFromSourceNode(source, name, compiledAt, node) {
|
|
|
494
614
|
}
|
|
495
615
|
|
|
496
616
|
function libraryOriginsFromSource(source, fullSource, lib, options) {
|
|
497
|
-
const { typeNames, relNames } = userDefinedNames(source);
|
|
617
|
+
const { typeNames, relNames, patternNames } = userDefinedNames(source);
|
|
498
618
|
const annotated = annotateWithOptionalIdentity(fullSource, options);
|
|
499
619
|
const meta = {};
|
|
500
620
|
const now = new Date().toISOString();
|
|
@@ -527,7 +647,40 @@ function libraryOriginsFromSource(source, fullSource, lib, options) {
|
|
|
527
647
|
));
|
|
528
648
|
}
|
|
529
649
|
|
|
530
|
-
|
|
650
|
+
const parsed = parse(`${source}\n()`);
|
|
651
|
+
const patternAlias = {};
|
|
652
|
+
for (const name of patternNames) {
|
|
653
|
+
const pat = parsed.defs?.patterns?.[name];
|
|
654
|
+
if (!pat) continue;
|
|
655
|
+
const info = canonicalPatternInfo(name, pat, {
|
|
656
|
+
libraries: options.libraries,
|
|
657
|
+
patterns: {
|
|
658
|
+
...(options.patterns || {}),
|
|
659
|
+
...(parsed.defs?.patterns || {})
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
if (info.hash) {
|
|
663
|
+
patternAlias[name] = info.hash;
|
|
664
|
+
if (!meta[info.hash]) {
|
|
665
|
+
meta[info.hash] = {
|
|
666
|
+
type: "pattern",
|
|
667
|
+
params: info.canonicalParams || pat.params || [],
|
|
668
|
+
origins: []
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
meta[info.hash].origins.push({
|
|
672
|
+
source: options.source || null,
|
|
673
|
+
name,
|
|
674
|
+
params: pat.params || [],
|
|
675
|
+
aliasArgs: info.aliasArgs || pat.params || [],
|
|
676
|
+
compiledAt: now,
|
|
677
|
+
...(pat.filter?.start ? { start: pat.filter.start } : {}),
|
|
678
|
+
...(pat.filter?.end ? { end: pat.filter.end } : {})
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return { relAlias, patternAlias, meta };
|
|
531
684
|
}
|
|
532
685
|
|
|
533
686
|
function canonicalNames(state) {
|
|
@@ -535,14 +688,16 @@ function canonicalNames(state) {
|
|
|
535
688
|
...Object.keys(state.codes),
|
|
536
689
|
...Object.keys(state.rels),
|
|
537
690
|
...Object.values(state.typeAliases),
|
|
538
|
-
...Object.values(state.relAliases)
|
|
691
|
+
...Object.values(state.relAliases),
|
|
692
|
+
...Object.values(state.patternAliases || {})
|
|
539
693
|
])].filter((name) => name.startsWith("@")).sort();
|
|
540
694
|
}
|
|
541
695
|
|
|
542
696
|
function aliasNames(state) {
|
|
543
697
|
return [...new Set([
|
|
544
698
|
...Object.keys(state.typeAliases),
|
|
545
|
-
...Object.keys(state.relAliases)
|
|
699
|
+
...Object.keys(state.relAliases),
|
|
700
|
+
...Object.keys(state.patternAliases || {})
|
|
546
701
|
])].sort();
|
|
547
702
|
}
|
|
548
703
|
|
|
@@ -550,7 +705,8 @@ function displayAliases(state) {
|
|
|
550
705
|
const aliases = {};
|
|
551
706
|
for (const [name, hash] of [
|
|
552
707
|
...Object.entries(state.typeAliases),
|
|
553
|
-
...Object.entries(state.relAliases)
|
|
708
|
+
...Object.entries(state.relAliases),
|
|
709
|
+
...Object.entries(state.patternAliases || {})
|
|
554
710
|
].sort(([a], [b]) => a.localeCompare(b))) {
|
|
555
711
|
if (!aliases[hash]) aliases[hash] = name;
|
|
556
712
|
}
|
|
@@ -1022,8 +1178,55 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
|
|
|
1022
1178
|
};
|
|
1023
1179
|
try {
|
|
1024
1180
|
const lib = compileWithOptionalIdentity(fullSource, compileOptions);
|
|
1025
|
-
const { relAlias, meta } = libraryOriginsFromSource(
|
|
1026
|
-
|
|
1181
|
+
const { relAlias, patternAlias, meta } = libraryOriginsFromSource(
|
|
1182
|
+
source,
|
|
1183
|
+
fullSource,
|
|
1184
|
+
lib,
|
|
1185
|
+
compileOptions
|
|
1186
|
+
);
|
|
1187
|
+
const { patternNames } = userDefinedNames(source);
|
|
1188
|
+
let parsedSource;
|
|
1189
|
+
try {
|
|
1190
|
+
parsedSource = parse(source);
|
|
1191
|
+
} catch {
|
|
1192
|
+
parsedSource = parse(`${source}\n()`);
|
|
1193
|
+
}
|
|
1194
|
+
const filePatterns = {};
|
|
1195
|
+
for (const name of patternNames) {
|
|
1196
|
+
const pat = parsedSource.defs?.patterns?.[name];
|
|
1197
|
+
if (pat) {
|
|
1198
|
+
filePatterns[name] = pat;
|
|
1199
|
+
if (patternAlias[name]) {
|
|
1200
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
1201
|
+
filePatterns[patternAlias[name]] = {
|
|
1202
|
+
params:
|
|
1203
|
+
info.canonicalParams ||
|
|
1204
|
+
info.params ||
|
|
1205
|
+
pat.params ||
|
|
1206
|
+
[],
|
|
1207
|
+
filter: info.canonicalFilterAst || pat.filter,
|
|
1208
|
+
_canonical: patternAlias[name],
|
|
1209
|
+
_canonicalDef: info.def,
|
|
1210
|
+
_canonicalParams: info.canonicalParams,
|
|
1211
|
+
_graphParams: info.canonicalParams,
|
|
1212
|
+
_aliasArgs: info.canonicalParams,
|
|
1213
|
+
_canonicalFilterAst: info.canonicalFilterAst
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
mergeLibrary(
|
|
1219
|
+
state,
|
|
1220
|
+
{
|
|
1221
|
+
...lib,
|
|
1222
|
+
relAlias,
|
|
1223
|
+
patternAlias,
|
|
1224
|
+
meta,
|
|
1225
|
+
patterns: filePatterns
|
|
1226
|
+
},
|
|
1227
|
+
targetPath,
|
|
1228
|
+
{ loadAliases }
|
|
1229
|
+
);
|
|
1027
1230
|
} catch (error) {
|
|
1028
1231
|
throw remapError(error, preambleLineCount(preamble));
|
|
1029
1232
|
}
|
|
@@ -1087,23 +1290,52 @@ function compileSnippetArtifacts(source, state, sourceName = "<repl>") {
|
|
|
1087
1290
|
};
|
|
1088
1291
|
try {
|
|
1089
1292
|
const lib = compileWithOptionalIdentity(fullSource, options);
|
|
1090
|
-
const { relAlias, meta } = libraryOriginsFromSource(
|
|
1293
|
+
const { relAlias, patternAlias, meta } = libraryOriginsFromSource(
|
|
1091
1294
|
source,
|
|
1092
1295
|
fullSource,
|
|
1093
1296
|
lib,
|
|
1094
1297
|
options
|
|
1095
1298
|
);
|
|
1096
1299
|
const annotated = annotateWithOptionalIdentity(fullSource, options);
|
|
1300
|
+
const { patternNames } = userDefinedNames(source);
|
|
1301
|
+
let parsedSnippet;
|
|
1302
|
+
try {
|
|
1303
|
+
parsedSnippet = parse(source);
|
|
1304
|
+
} catch {
|
|
1305
|
+
parsedSnippet = parse(`${source}\n()`);
|
|
1306
|
+
}
|
|
1307
|
+
const patterns = { ...(state.patterns || {}) };
|
|
1308
|
+
for (const name of patternNames) {
|
|
1309
|
+
const pat = parsedSnippet.defs?.patterns?.[name];
|
|
1310
|
+
if (pat) {
|
|
1311
|
+
patterns[name] = pat;
|
|
1312
|
+
if (patternAlias[name]) {
|
|
1313
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
1314
|
+
patterns[patternAlias[name]] = {
|
|
1315
|
+
params:
|
|
1316
|
+
info.canonicalParams ||
|
|
1317
|
+
info.params ||
|
|
1318
|
+
pat.params ||
|
|
1319
|
+
[],
|
|
1320
|
+
filter: info.canonicalFilterAst || pat.filter,
|
|
1321
|
+
_canonical: patternAlias[name],
|
|
1322
|
+
_canonicalDef: info.def,
|
|
1323
|
+
_canonicalParams: info.canonicalParams,
|
|
1324
|
+
_graphParams: info.canonicalParams,
|
|
1325
|
+
_aliasArgs: info.canonicalParams,
|
|
1326
|
+
_canonicalFilterAst: info.canonicalFilterAst
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1097
1331
|
return {
|
|
1098
1332
|
annotated,
|
|
1099
1333
|
lib: {
|
|
1100
1334
|
...lib,
|
|
1101
1335
|
relAlias,
|
|
1336
|
+
patternAlias,
|
|
1102
1337
|
meta,
|
|
1103
|
-
patterns
|
|
1104
|
-
...(state.patterns || {}),
|
|
1105
|
-
...(annotated.sourceDefs?.patterns || {})
|
|
1106
|
-
}
|
|
1338
|
+
patterns
|
|
1107
1339
|
},
|
|
1108
1340
|
lineOffset: preambleLineCount(preamble)
|
|
1109
1341
|
};
|
|
@@ -1380,17 +1612,37 @@ async function evaluateCommand(line, state) {
|
|
|
1380
1612
|
}
|
|
1381
1613
|
const rawName = arg.trim();
|
|
1382
1614
|
let patternName = rawName.replace(/\[.*\]$/, "").trim();
|
|
1383
|
-
let pat =
|
|
1384
|
-
if (
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1615
|
+
let pat = null;
|
|
1616
|
+
if (patternName.startsWith("@")) {
|
|
1617
|
+
pat = state.patterns?.[patternName];
|
|
1618
|
+
if (!pat) {
|
|
1619
|
+
for (const [name, hash] of Object.entries(
|
|
1620
|
+
state.patternAliases || {}
|
|
1621
|
+
)) {
|
|
1622
|
+
if (hash === patternName) {
|
|
1623
|
+
pat = state.patterns?.[hash] || state.patterns?.[name];
|
|
1624
|
+
break;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
if (!pat) {
|
|
1629
|
+
for (const [name, p] of Object.entries(
|
|
1630
|
+
state.patterns || {}
|
|
1631
|
+
)) {
|
|
1632
|
+
if (
|
|
1633
|
+
!name.startsWith("@") &&
|
|
1634
|
+
resolvePatternCanonicalName(state, name, p) === patternName
|
|
1635
|
+
) {
|
|
1636
|
+
pat = state.patterns?.[patternName] || p;
|
|
1637
|
+
break;
|
|
1638
|
+
}
|
|
1392
1639
|
}
|
|
1393
1640
|
}
|
|
1641
|
+
} else {
|
|
1642
|
+
pat = state.patterns?.[patternName];
|
|
1643
|
+
if (!pat && state.patternAliases?.[patternName]) {
|
|
1644
|
+
pat = state.patterns?.[state.patternAliases[patternName]];
|
|
1645
|
+
}
|
|
1394
1646
|
}
|
|
1395
1647
|
if (!pat) {
|
|
1396
1648
|
throw new Error(`Unknown pattern '${rawName}'`);
|
|
@@ -1399,7 +1651,7 @@ async function evaluateCommand(line, state) {
|
|
|
1399
1651
|
}
|
|
1400
1652
|
case "patterns":
|
|
1401
1653
|
case "pats":
|
|
1402
|
-
return [
|
|
1654
|
+
return [listPatternAliases(state)];
|
|
1403
1655
|
case "codec":
|
|
1404
1656
|
case "codecs":
|
|
1405
1657
|
return loadCodec(arg, state);
|
|
@@ -1493,7 +1745,7 @@ function helpText() {
|
|
|
1493
1745
|
":rel name show relation definition",
|
|
1494
1746
|
":rels list relation aliases",
|
|
1495
1747
|
":pattern name show named pattern definition (alias :pat)",
|
|
1496
|
-
":patterns list
|
|
1748
|
+
":patterns list pattern aliases (alias :pats)",
|
|
1497
1749
|
":limit [num|none] set maximum characters for value output",
|
|
1498
1750
|
":codec load file load a codec module from file",
|
|
1499
1751
|
":codec unload name unload a registered codec",
|
package/tests/test-k-object.mjs
CHANGED
|
@@ -92,7 +92,11 @@ const natDecompiledSource = decompileObjectBuffer(natLibraryBuffer);
|
|
|
92
92
|
const natRoundTripSource = decompileObjectBuffer(
|
|
93
93
|
compileLibraryBuffer(natDecompiledSource, { source: "Examples/nat.decompiled.k" })
|
|
94
94
|
);
|
|
95
|
-
const recursiveNatPattern =
|
|
95
|
+
const recursiveNatPattern = new RegExp(
|
|
96
|
+
"\\?\\{<\\{\\} 0, X0 \\+1>=X0 x, X0 y\\} " +
|
|
97
|
+
"<\\{\\.x [A-Za-z0-9_]+ x, \\.y [A-Za-z0-9_]+ y\\} " +
|
|
98
|
+
"[A-Za-z0-9_]+, \\.y> \\?X0;"
|
|
99
|
+
);
|
|
96
100
|
assert.equal(natRoundTripSource, natDecompiledSource);
|
|
97
101
|
assert.match(natDecompiledSource, recursiveNatPattern);
|
|
98
102
|
|