@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
package/repl.mjs
CHANGED
|
@@ -27,6 +27,9 @@ import {
|
|
|
27
27
|
prettyCode,
|
|
28
28
|
prettyFilter
|
|
29
29
|
} from "./pretty.mjs";
|
|
30
|
+
import {
|
|
31
|
+
resolvePatternCanonicalInfo as canonicalPatternInfo
|
|
32
|
+
} from "./canonical-pattern.mjs";
|
|
30
33
|
import {
|
|
31
34
|
compileObject,
|
|
32
35
|
compileLibrary,
|
|
@@ -95,6 +98,7 @@ function createState(options = {}) {
|
|
|
95
98
|
codes: codes.dump(),
|
|
96
99
|
rels: {},
|
|
97
100
|
relAliases: {},
|
|
101
|
+
patternAliases: {},
|
|
98
102
|
typeAliases: {},
|
|
99
103
|
patterns: {},
|
|
100
104
|
codecs: {},
|
|
@@ -116,6 +120,7 @@ function stateLibrary(state) {
|
|
|
116
120
|
codes: state.codes,
|
|
117
121
|
rels: state.rels,
|
|
118
122
|
relAlias: state.relAliases,
|
|
123
|
+
patternAlias: state.patternAliases || {},
|
|
119
124
|
patterns: state.patterns,
|
|
120
125
|
meta: state.meta,
|
|
121
126
|
main: null
|
|
@@ -137,6 +142,27 @@ function aliasPreamble(state, omitName = null) {
|
|
|
137
142
|
for (const [name, hash] of Object.entries(state.relAliases).sort()) {
|
|
138
143
|
if (name !== omitName) lines.push(aliasLine("rel", name, hash));
|
|
139
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
|
+
}
|
|
140
166
|
return lines.filter(Boolean).join("\n");
|
|
141
167
|
}
|
|
142
168
|
|
|
@@ -208,10 +234,10 @@ function mergeMeta(state, meta = {}) {
|
|
|
208
234
|
function rememberOrigin(state, hash, name, type, source = "<repl>") {
|
|
209
235
|
if (!state.meta[hash]) state.meta[hash] = { type, origins: [] };
|
|
210
236
|
if (state.meta[hash].type == null) state.meta[hash].type = type;
|
|
211
|
-
const
|
|
212
|
-
origin.name === name
|
|
237
|
+
const existing = state.meta[hash].origins.find((origin) =>
|
|
238
|
+
origin.name === name
|
|
213
239
|
);
|
|
214
|
-
if (!
|
|
240
|
+
if (!existing) {
|
|
215
241
|
state.meta[hash].origins.push({
|
|
216
242
|
source,
|
|
217
243
|
name,
|
|
@@ -225,8 +251,13 @@ function recoverAliasesFromMeta(state, lib) {
|
|
|
225
251
|
for (const origin of entry?.origins || []) {
|
|
226
252
|
if (!origin?.name || !NAME_RE.test(origin.name)) continue;
|
|
227
253
|
const type = entry?.type;
|
|
228
|
-
if (
|
|
254
|
+
if (
|
|
255
|
+
type === "code" ||
|
|
256
|
+
(!type && hash in state.codes && !(hash in state.rels))
|
|
257
|
+
) {
|
|
229
258
|
state.typeAliases[origin.name] = hash;
|
|
259
|
+
} else if (type === "pattern") {
|
|
260
|
+
state.patternAliases[origin.name] = hash;
|
|
230
261
|
} else if (type === "rel" || hash in state.rels) {
|
|
231
262
|
state.relAliases[origin.name] = hash;
|
|
232
263
|
}
|
|
@@ -243,16 +274,30 @@ function mergeLibrary(state, lib, source = "<load>", options = {}) {
|
|
|
243
274
|
...(lib.patterns || lib.sourceDefs?.patterns ||
|
|
244
275
|
lib.defs?.patterns || {})
|
|
245
276
|
};
|
|
277
|
+
state.patternAliases = {
|
|
278
|
+
...state.patternAliases,
|
|
279
|
+
...(lib.patternAlias || {})
|
|
280
|
+
};
|
|
246
281
|
mergeMeta(state, lib.meta);
|
|
247
282
|
|
|
248
283
|
if (loadAliases) {
|
|
249
284
|
state.relAliases = { ...state.relAliases, ...(lib.relAlias || {}) };
|
|
250
285
|
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
251
|
-
if (
|
|
286
|
+
if (
|
|
287
|
+
name !== "__main__" &&
|
|
288
|
+
NAME_RE.test(name) &&
|
|
289
|
+
hash in state.rels
|
|
290
|
+
) {
|
|
252
291
|
state.relAliases[name] = hash;
|
|
253
292
|
rememberOrigin(state, hash, name, "rel", source);
|
|
254
293
|
}
|
|
255
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
|
+
}
|
|
256
301
|
recoverAliasesFromMeta(state, lib);
|
|
257
302
|
if (state.typeAliases.string && !state.typeAliases.utf8) {
|
|
258
303
|
state.typeAliases.utf8 = state.typeAliases.string;
|
|
@@ -308,23 +353,71 @@ function savedLibrary(state) {
|
|
|
308
353
|
for (const [name, hash] of Object.entries(state.typeAliases)) {
|
|
309
354
|
if (!meta[hash]) meta[hash] = { type: "code", origins: [] };
|
|
310
355
|
if (meta[hash].type == null) meta[hash].type = "code";
|
|
311
|
-
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
|
+
}
|
|
312
363
|
}
|
|
313
364
|
for (const [name, hash] of Object.entries(state.relAliases)) {
|
|
314
365
|
if (!meta[hash]) meta[hash] = { type: "rel", origins: [] };
|
|
315
366
|
if (meta[hash].type == null) meta[hash].type = "rel";
|
|
316
|
-
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
|
+
}
|
|
317
408
|
}
|
|
318
409
|
const rels = reachableRelations(state.rels, Object.values(state.relAliases));
|
|
319
410
|
const storedHashes = new Set([
|
|
320
411
|
...Object.keys(state.codes),
|
|
321
|
-
...Object.keys(rels)
|
|
412
|
+
...Object.keys(rels),
|
|
413
|
+
...Object.values(state.patternAliases || {})
|
|
322
414
|
]);
|
|
323
415
|
return {
|
|
324
416
|
format: "k-object",
|
|
325
417
|
codes: state.codes,
|
|
326
418
|
rels,
|
|
327
419
|
relAlias: state.relAliases,
|
|
420
|
+
patternAlias: state.patternAliases || {},
|
|
328
421
|
compileStats: { sccs: [], sccCount: 0 },
|
|
329
422
|
patterns: state.patterns || {},
|
|
330
423
|
meta: Object.fromEntries(
|
|
@@ -373,67 +466,12 @@ function listAliases(aliases) {
|
|
|
373
466
|
|
|
374
467
|
function resolvePatternCanonicalInfo(state, name, pat) {
|
|
375
468
|
if (!state || !pat) return { hash: null, def: null, params: null };
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
const params = pat.params || [];
|
|
384
|
-
const source = `? ${name}[${params.join(", ")}]`;
|
|
385
|
-
try {
|
|
386
|
-
restoreCodes(state);
|
|
387
|
-
const annotated = annotate(source, {
|
|
388
|
-
libraries: [stateLibrary(state)],
|
|
389
|
-
patterns: state.patterns
|
|
390
|
-
});
|
|
391
|
-
const mainRel = annotated.rels.__main__;
|
|
392
|
-
if (
|
|
393
|
-
mainRel?.typePatternGraph &&
|
|
394
|
-
mainRel?.def?.patterns?.[0] != null
|
|
395
|
-
) {
|
|
396
|
-
const rootId = mainRel.typePatternGraph.find(
|
|
397
|
-
mainRel.def.patterns[0]
|
|
398
|
-
);
|
|
399
|
-
const { filter: canonicalFilter, canonicalParams } =
|
|
400
|
-
patternGraphToCanonical(mainRel.typePatternGraph, rootId);
|
|
401
|
-
if (canonicalFilter) {
|
|
402
|
-
pat._canonicalDef = prettyFilter(canonicalFilter);
|
|
403
|
-
pat._canonicalParams = canonicalParams;
|
|
404
|
-
}
|
|
405
|
-
const propList = patternToPropertyList(
|
|
406
|
-
exportPatternGraph(mainRel.typePatternGraph, rootId)
|
|
407
|
-
);
|
|
408
|
-
const codeHash = closedPatternToCodeHash(propList);
|
|
409
|
-
if (codeHash) {
|
|
410
|
-
state.codes = codes.dump();
|
|
411
|
-
pat._canonical = codeHash;
|
|
412
|
-
return {
|
|
413
|
-
hash: codeHash,
|
|
414
|
-
def: pat._canonicalDef || null,
|
|
415
|
-
params: pat._canonicalParams || null
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
const relHash = annotated.relAlias?.__main__ || null;
|
|
420
|
-
if (relHash) {
|
|
421
|
-
pat._canonical = relHash;
|
|
422
|
-
return {
|
|
423
|
-
hash: relHash,
|
|
424
|
-
def: pat._canonicalDef || null,
|
|
425
|
-
params: pat._canonicalParams || null
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
} catch {
|
|
429
|
-
restoreCodes(state);
|
|
430
|
-
return { hash: null, def: null, params: null };
|
|
431
|
-
}
|
|
432
|
-
return {
|
|
433
|
-
hash: null,
|
|
434
|
-
def: pat._canonicalDef || null,
|
|
435
|
-
params: pat._canonicalParams || null
|
|
436
|
-
};
|
|
469
|
+
const res = canonicalPatternInfo(name, pat, {
|
|
470
|
+
libraries: [stateLibrary(state)],
|
|
471
|
+
patterns: state.patterns
|
|
472
|
+
});
|
|
473
|
+
state.codes = codes.dump();
|
|
474
|
+
return res;
|
|
437
475
|
}
|
|
438
476
|
|
|
439
477
|
function resolvePatternCanonicalName(state, name, pat) {
|
|
@@ -442,24 +480,64 @@ function resolvePatternCanonicalName(state, name, pat) {
|
|
|
442
480
|
|
|
443
481
|
function prettyPattern(name, pat, state = null) {
|
|
444
482
|
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
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 || []);
|
|
448
490
|
const params = paramList.join(", ");
|
|
449
491
|
const filterStr = info.def ||
|
|
450
492
|
(pat?.filter ? prettyFilter(pat.filter) : "()");
|
|
451
|
-
const suffix =
|
|
493
|
+
const suffix =
|
|
494
|
+
info.hash && !isHash ? ` -- ${info.hash}` : "";
|
|
452
495
|
return `${name}[${params}] = ${filterStr};${suffix}`;
|
|
453
496
|
}
|
|
454
497
|
|
|
455
|
-
function
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
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 || {} });
|
|
463
541
|
}
|
|
464
542
|
|
|
465
543
|
function formatCodecOutput(output) {
|
|
@@ -516,6 +594,11 @@ function userDefinedNames(source) {
|
|
|
516
594
|
),
|
|
517
595
|
relNames: Object.keys(parsed.defs.rels).filter((name) =>
|
|
518
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)))
|
|
519
602
|
)
|
|
520
603
|
};
|
|
521
604
|
}
|
|
@@ -531,7 +614,7 @@ function originFromSourceNode(source, name, compiledAt, node) {
|
|
|
531
614
|
}
|
|
532
615
|
|
|
533
616
|
function libraryOriginsFromSource(source, fullSource, lib, options) {
|
|
534
|
-
const { typeNames, relNames } = userDefinedNames(source);
|
|
617
|
+
const { typeNames, relNames, patternNames } = userDefinedNames(source);
|
|
535
618
|
const annotated = annotateWithOptionalIdentity(fullSource, options);
|
|
536
619
|
const meta = {};
|
|
537
620
|
const now = new Date().toISOString();
|
|
@@ -564,7 +647,40 @@ function libraryOriginsFromSource(source, fullSource, lib, options) {
|
|
|
564
647
|
));
|
|
565
648
|
}
|
|
566
649
|
|
|
567
|
-
|
|
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 };
|
|
568
684
|
}
|
|
569
685
|
|
|
570
686
|
function canonicalNames(state) {
|
|
@@ -572,14 +688,16 @@ function canonicalNames(state) {
|
|
|
572
688
|
...Object.keys(state.codes),
|
|
573
689
|
...Object.keys(state.rels),
|
|
574
690
|
...Object.values(state.typeAliases),
|
|
575
|
-
...Object.values(state.relAliases)
|
|
691
|
+
...Object.values(state.relAliases),
|
|
692
|
+
...Object.values(state.patternAliases || {})
|
|
576
693
|
])].filter((name) => name.startsWith("@")).sort();
|
|
577
694
|
}
|
|
578
695
|
|
|
579
696
|
function aliasNames(state) {
|
|
580
697
|
return [...new Set([
|
|
581
698
|
...Object.keys(state.typeAliases),
|
|
582
|
-
...Object.keys(state.relAliases)
|
|
699
|
+
...Object.keys(state.relAliases),
|
|
700
|
+
...Object.keys(state.patternAliases || {})
|
|
583
701
|
])].sort();
|
|
584
702
|
}
|
|
585
703
|
|
|
@@ -587,7 +705,8 @@ function displayAliases(state) {
|
|
|
587
705
|
const aliases = {};
|
|
588
706
|
for (const [name, hash] of [
|
|
589
707
|
...Object.entries(state.typeAliases),
|
|
590
|
-
...Object.entries(state.relAliases)
|
|
708
|
+
...Object.entries(state.relAliases),
|
|
709
|
+
...Object.entries(state.patternAliases || {})
|
|
591
710
|
].sort(([a], [b]) => a.localeCompare(b))) {
|
|
592
711
|
if (!aliases[hash]) aliases[hash] = name;
|
|
593
712
|
}
|
|
@@ -1059,8 +1178,55 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
|
|
|
1059
1178
|
};
|
|
1060
1179
|
try {
|
|
1061
1180
|
const lib = compileWithOptionalIdentity(fullSource, compileOptions);
|
|
1062
|
-
const { relAlias, meta } = libraryOriginsFromSource(
|
|
1063
|
-
|
|
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
|
+
);
|
|
1064
1230
|
} catch (error) {
|
|
1065
1231
|
throw remapError(error, preambleLineCount(preamble));
|
|
1066
1232
|
}
|
|
@@ -1124,23 +1290,52 @@ function compileSnippetArtifacts(source, state, sourceName = "<repl>") {
|
|
|
1124
1290
|
};
|
|
1125
1291
|
try {
|
|
1126
1292
|
const lib = compileWithOptionalIdentity(fullSource, options);
|
|
1127
|
-
const { relAlias, meta } = libraryOriginsFromSource(
|
|
1293
|
+
const { relAlias, patternAlias, meta } = libraryOriginsFromSource(
|
|
1128
1294
|
source,
|
|
1129
1295
|
fullSource,
|
|
1130
1296
|
lib,
|
|
1131
1297
|
options
|
|
1132
1298
|
);
|
|
1133
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
|
+
}
|
|
1134
1331
|
return {
|
|
1135
1332
|
annotated,
|
|
1136
1333
|
lib: {
|
|
1137
1334
|
...lib,
|
|
1138
1335
|
relAlias,
|
|
1336
|
+
patternAlias,
|
|
1139
1337
|
meta,
|
|
1140
|
-
patterns
|
|
1141
|
-
...(state.patterns || {}),
|
|
1142
|
-
...(annotated.sourceDefs?.patterns || {})
|
|
1143
|
-
}
|
|
1338
|
+
patterns
|
|
1144
1339
|
},
|
|
1145
1340
|
lineOffset: preambleLineCount(preamble)
|
|
1146
1341
|
};
|
|
@@ -1417,17 +1612,37 @@ async function evaluateCommand(line, state) {
|
|
|
1417
1612
|
}
|
|
1418
1613
|
const rawName = arg.trim();
|
|
1419
1614
|
let patternName = rawName.replace(/\[.*\]$/, "").trim();
|
|
1420
|
-
let pat =
|
|
1421
|
-
if (
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
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
|
+
}
|
|
1429
1626
|
}
|
|
1430
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
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
} else {
|
|
1642
|
+
pat = state.patterns?.[patternName];
|
|
1643
|
+
if (!pat && state.patternAliases?.[patternName]) {
|
|
1644
|
+
pat = state.patterns?.[state.patternAliases[patternName]];
|
|
1645
|
+
}
|
|
1431
1646
|
}
|
|
1432
1647
|
if (!pat) {
|
|
1433
1648
|
throw new Error(`Unknown pattern '${rawName}'`);
|
|
@@ -1436,7 +1651,7 @@ async function evaluateCommand(line, state) {
|
|
|
1436
1651
|
}
|
|
1437
1652
|
case "patterns":
|
|
1438
1653
|
case "pats":
|
|
1439
|
-
return [
|
|
1654
|
+
return [listPatternAliases(state)];
|
|
1440
1655
|
case "codec":
|
|
1441
1656
|
case "codecs":
|
|
1442
1657
|
return loadCodec(arg, state);
|
|
@@ -1530,7 +1745,7 @@ function helpText() {
|
|
|
1530
1745
|
":rel name show relation definition",
|
|
1531
1746
|
":rels list relation aliases",
|
|
1532
1747
|
":pattern name show named pattern definition (alias :pat)",
|
|
1533
|
-
":patterns list
|
|
1748
|
+
":patterns list pattern aliases (alias :pats)",
|
|
1534
1749
|
":limit [num|none] set maximum characters for value output",
|
|
1535
1750
|
":codec load file load a codec module from file",
|
|
1536
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
|
|