@fraczak/k 8.4.1 → 8.5.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.
Files changed (109) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/ARCHITECTURE.md +83 -0
  3. package/DOCS/CODECS.md +93 -204
  4. package/DOCS/DICTIONARY.md +42 -1241
  5. package/DOCS/FILE_FORMATS.md +170 -625
  6. package/DOCS/GETTING_STARTED.md +164 -0
  7. package/DOCS/KIR_V1.md +22 -33
  8. package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
  9. package/DOCS/PATTERNS.md +155 -138
  10. package/DOCS/POLYMORPHIC_IR.md +83 -101
  11. package/DOCS/PROFILING_AND_TRACING.md +52 -166
  12. package/DOCS/README.md +35 -0
  13. package/DOCS/REPL.md +100 -377
  14. package/DOCS/TEXTUAL_VALUES.md +53 -81
  15. package/DOCS/TYPE_DERIVATION.md +95 -234
  16. package/DOCS/book-combined.md +3786 -0
  17. package/Examples/arithmetics.k +45 -59
  18. package/Examples/djson.k +102 -112
  19. package/Examples/karatsuba-mult.k +2 -3
  20. package/Examples/list.k +6 -7
  21. package/Examples/nat.k +6 -6
  22. package/Examples/poly.k +45 -45
  23. package/LICENSE +15 -0
  24. package/README.md +72 -410
  25. package/backends/llvm/README.md +75 -97
  26. package/backends/wasm/README.md +88 -66
  27. package/canonical-pattern.mjs +7 -3
  28. package/codecs/README.md +23 -261
  29. package/codecs/WIRE_FORMAT.md +132 -0
  30. package/core.k +5 -15
  31. package/named-patterns.mjs +19 -7
  32. package/objects/README.md +24 -109
  33. package/package.json +8 -3
  34. package/paper/Makefile +17 -0
  35. package/paper/k-paper.pdf +0 -0
  36. package/paper/k-paper.tex +435 -0
  37. package/parser.jison +0 -10
  38. package/parser.mjs +32 -43
  39. package/repl.mjs +74 -29
  40. package/scripts/benchmark-idempotency.mjs +3 -3
  41. package/scripts/run-tests.mjs +1 -1
  42. package/scripts/test-package.mjs +64 -0
  43. package/tests/README.md +66 -46
  44. package/tests/code-derivation/core.mjs +61 -0
  45. package/tests/helpers/cli.mjs +45 -0
  46. package/tests/integration.sh +2 -3
  47. package/tests/test-arithmetics.mjs +47 -237
  48. package/tests/test-djson-methods.mjs +71 -275
  49. package/tests/test-hash-normalization.mjs +22 -1
  50. package/tests/test-karatsuba.mjs +18 -126
  51. package/tests/test-named-patterns.mjs +174 -621
  52. package/tests/test-paper-examples.mjs +90 -0
  53. package/tests/test-poly.mjs +2 -2
  54. package/tests/test-repl-html.mjs +2 -2
  55. package/tests/test-repl.mjs +49 -41
  56. package/tests/test.mjs +27 -68
  57. package/AGENTS.md +0 -35
  58. package/CLAUDE.md +0 -64
  59. package/DOCS/BIG_PICTURE.md +0 -439
  60. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  61. package/DOCS/CANONICAL_PATTERNS.md +0 -362
  62. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  63. package/DOCS/CONVERGENCE.md +0 -123
  64. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  65. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  66. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  67. package/DOCS/Makefile +0 -99
  68. package/DOCS/NAMED_PATTERNS.md +0 -361
  69. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  70. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  71. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
  72. package/DOCS/book/01-introduction.md +0 -48
  73. package/DOCS/book/02-syntax-and-values.md +0 -110
  74. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  75. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  76. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  77. package/DOCS/book/06-values-in-memory.md +0 -1085
  78. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  79. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  80. package/DOCS/book/09-compiler-architecture.md +0 -160
  81. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  82. package/DOCS/book/14-canonical-serialization.md +0 -176
  83. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  84. package/DOCS/book/overview.txt +0 -68
  85. package/DOCS/book.md +0 -50
  86. package/Examples/bnat.k +0 -308
  87. package/Examples/buda.k +0 -66
  88. package/Examples/byte.k +0 -35
  89. package/Examples/code.k +0 -43
  90. package/Examples/vhdl_to_k.k +0 -67
  91. package/JOURNAL.md +0 -288
  92. package/TODO.md +0 -107
  93. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  94. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  95. package/backends/wasm/demo_ieee.mjs +0 -318
  96. package/browser/vfs-data.mjs +0 -21
  97. package/codecs/BINARY_FORMAT.md +0 -53
  98. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  99. package/codecs/pandoc.css +0 -71
  100. package/tests/code-derivation/blowup.mjs +0 -25
  101. package/tests/code-derivation/bnat.mjs +0 -11
  102. package/tests/code-derivation/dot.mjs +0 -37
  103. package/tests/code-derivation/index.mjs +0 -20
  104. package/tests/code-derivation/loop.mjs +0 -17
  105. package/tests/code-derivation/nat.mjs +0 -10
  106. package/tests/code-derivation/not-not.mjs +0 -14
  107. package/tests/code-derivation/product.mjs +0 -22
  108. package/tests/code-derivation/rlz.mjs +0 -13
  109. package/tests/test-fingerprint.mjs +0 -38
package/parser.mjs CHANGED
@@ -162,12 +162,12 @@ function fromEscString(escString) {
162
162
 
163
163
 
164
164
  var parser = (function(){
165
- var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[6,8,10,12,14,30,32,34,42],$V1=[1,10],$V2=[1,11],$V3=[1,20],$V4=[1,19],$V5=[1,21],$V6=[1,22],$V7=[1,23],$V8=[1,24],$V9=[1,25],$Va=[6,8,10,12,14,30,32,34,42,49],$Vb=[2,62],$Vc=[1,33],$Vd=[1,32],$Ve=[4,6,8,10,12,14,18,20,22,30,32,34,36,38,42,49],$Vf=[1,41],$Vg=[1,47],$Vh=[4,6],$Vi=[2,29],$Vj=[2,59],$Vk=[1,68],$Vl=[1,70],$Vm=[20,36],$Vn=[4,6,8,10,12,14,30,32,34,42],$Vo=[1,74],$Vp=[1,77],$Vq=[18,36],$Vr=[4,6,8,10,12,14,18,20,22,24,26,30,32,34,36,38,42,49],$Vs=[4,6,8,10,12,14,18,20,22,24,30,32,34,36,38,42,49],$Vt=[2,45],$Vu=[1,83],$Vv=[2,46],$Vw=[1,94],$Vx=[1,96],$Vy=[24,36],$Vz=[2,74],$VA=[2,39],$VB=[18,20,36];
165
+ var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[6,8,10,12,14,30,32,34,42],$V1=[1,10],$V2=[1,11],$V3=[1,20],$V4=[1,19],$V5=[1,21],$V6=[1,22],$V7=[1,23],$V8=[1,24],$V9=[1,25],$Va=[6,8,10,12,14,30,32,34,42,49],$Vb=[2,61],$Vc=[1,33],$Vd=[1,32],$Ve=[4,6,8,10,12,14,18,20,22,30,32,34,36,38,42,49],$Vf=[1,41],$Vg=[1,47],$Vh=[4,6],$Vi=[2,29],$Vj=[2,58],$Vk=[1,68],$Vl=[1,70],$Vm=[20,36],$Vn=[4,6,8,10,12,14,30,32,34,42],$Vo=[1,74],$Vp=[1,77],$Vq=[18,36],$Vr=[4,6,8,10,12,14,18,20,22,24,26,30,32,34,36,38,42,49],$Vs=[4,6,8,10,12,14,18,20,22,24,30,32,34,36,38,42,49],$Vt=[2,44],$Vu=[1,83],$Vv=[2,45],$Vw=[1,94],$Vx=[1,96],$Vy=[24,36],$Vz=[2,73],$VA=[2,38],$VB=[18,20,36];
166
166
  var parser = {trace: function trace () { },
167
167
  yy: {},
168
168
  symbols_: {"error":2,"str":3,"STRING":4,"name":5,"NAME":6,"at":7,"AT":8,"la":9,"LA":10,"lc":11,"LC":12,"lp":13,"LP":14,"lb":15,"LB":16,"ra":17,"RA":18,"rc":19,"RC":20,"rp":21,"RP":22,"rb":23,"RB":24,"eq":25,"EQ":26,"dots":27,"DOTS":28,"dot":29,"DOT":30,"div":31,"DIV":32,"vid":33,"VID":34,"comma":35,"COMMA":36,"sc":37,"SC":38,"col":39,"COL":40,"qmark":41,"QMARK":42,"incremental":43,"INCREMENTAL":44,"input_with_eof":45,"initialize_symbol_table":46,"defs":47,"comp":48,"EOF":49,"pattern_params":50,"filter":51,"non_empty_pattern_params":52,"filter_":53,"labelled_filters":54,"pattern_args":55,"non_empty_pattern_args":56,"non_empty_labelled_filters":57,"filter_label":58,"exp":59,"labelled":60,"list":61,"non_empty_labelled":62,"comp_label":63,"non_empty_list":64,"$accept":0,"$end":1},
169
169
  terminals_: {2:"error",4:"STRING",6:"NAME",8:"AT",10:"LA",12:"LC",14:"LP",16:"LB",18:"RA",20:"RC",22:"RP",24:"RB",26:"EQ",28:"DOTS",30:"DOT",32:"DIV",34:"VID",36:"COMMA",38:"SC",40:"COL",42:"QMARK",44:"INCREMENTAL",49:"EOF"},
170
- productions_: [0,[3,1],[5,1],[7,1],[9,1],[11,1],[13,1],[15,1],[17,1],[19,1],[21,1],[23,1],[25,1],[27,1],[29,1],[31,1],[33,1],[35,1],[37,1],[39,1],[41,1],[43,1],[45,4],[46,0],[46,1],[47,0],[47,5],[47,8],[47,9],[50,0],[50,1],[52,1],[52,3],[53,3],[53,3],[53,3],[53,4],[53,5],[53,2],[55,0],[55,1],[56,1],[56,3],[51,1],[51,3],[51,1],[54,0],[54,1],[57,1],[57,3],[58,1],[58,2],[58,2],[58,3],[58,3],[48,1],[48,2],[59,3],[59,3],[59,2],[59,2],[59,3],[59,1],[59,2],[59,2],[59,2],[59,2],[59,2],[59,2],[59,2],[60,0],[60,1],[62,1],[62,3],[63,2],[63,2],[63,3],[63,3],[61,0],[61,1],[64,1],[64,3]],
170
+ productions_: [0,[3,1],[5,1],[7,1],[9,1],[11,1],[13,1],[15,1],[17,1],[19,1],[21,1],[23,1],[25,1],[27,1],[29,1],[31,1],[33,1],[35,1],[37,1],[39,1],[41,1],[43,1],[45,4],[46,0],[46,1],[47,0],[47,5],[47,8],[47,9],[50,0],[50,1],[52,1],[52,3],[53,3],[53,3],[53,3],[53,4],[53,5],[55,0],[55,1],[56,1],[56,3],[51,1],[51,3],[51,1],[54,0],[54,1],[57,1],[57,3],[58,1],[58,2],[58,2],[58,3],[58,3],[48,1],[48,2],[59,3],[59,3],[59,2],[59,2],[59,3],[59,1],[59,2],[59,2],[59,2],[59,2],[59,2],[59,2],[59,2],[60,0],[60,1],[62,1],[62,3],[63,2],[63,2],[63,3],[63,3],[61,0],[61,1],[64,1],[64,3]],
171
171
  performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) {
172
172
  /* this == yyval */
173
173
 
@@ -213,10 +213,10 @@ case 28:
213
213
  s.add_pattern("@" + $$[$0-6].value, $$[$0-4], $$[$0-1]);
214
214
 
215
215
  break;
216
- case 29: case 39: case 78:
216
+ case 29: case 38: case 77:
217
217
  this.$ = [];
218
218
  break;
219
- case 30: case 40: case 43: case 55: case 79:
219
+ case 30: case 39: case 42: case 54: case 78:
220
220
  this.$ = $$[$0];
221
221
  break;
222
222
  case 31:
@@ -256,24 +256,13 @@ case 37:
256
256
  };
257
257
 
258
258
  break;
259
- case 38:
260
-
261
- this.$ = {
262
- type: "pattern_apply",
263
- pattern: "@" + $$[$0].value,
264
- args: [],
265
- start: $$[$0-1].start,
266
- end: $$[$0].end
267
- };
268
-
269
- break;
270
- case 41: case 48: case 80:
259
+ case 40: case 47: case 79:
271
260
  this.$ = [$$[$0]];
272
261
  break;
273
- case 42:
262
+ case 41:
274
263
  this.$ = [].concat($$[$0-2], $$[$0]);
275
264
  break;
276
- case 44:
265
+ case 43:
277
266
 
278
267
  this.$ = {
279
268
  ...$$[$0-2],
@@ -283,13 +272,13 @@ case 44:
283
272
  };
284
273
 
285
274
  break;
286
- case 45:
275
+ case 44:
287
276
  this.$ = { type: 'name', name: $$[$0].value, open: true, start: $$[$0].start, end: $$[$0].end};
288
277
  break;
289
- case 46:
278
+ case 45:
290
279
  this.$ = { fields: {}};
291
280
  break;
292
- case 47:
281
+ case 46:
293
282
  this.$ = $$[$0].reduce((r, lc) => {
294
283
  if (lc.dots) {
295
284
  r.open = true;
@@ -301,55 +290,55 @@ case 47:
301
290
  return r }
302
291
  , { fields: {} });
303
292
  break;
304
- case 49: case 81:
293
+ case 48: case 80:
305
294
  this.$ = [].concat($$[$0-2],$$[$0]);
306
295
  break;
307
- case 50:
296
+ case 49:
308
297
  this.$ = {dots: true };
309
298
  break;
310
- case 51: case 52:
299
+ case 50: case 51:
311
300
  this.$ = {label: $$[$0].value, filter: $$[$0-1]};
312
301
  break;
313
- case 53: case 54:
302
+ case 52: case 53:
314
303
  this.$ = {label: $$[$0-2].value, filter: $$[$0]};
315
304
  break;
316
- case 56:
305
+ case 55:
317
306
  this.$ = {...comp($$[$0-1], $$[$0]), start: $$[$0-1].start, end:$$[$0].end};
318
307
  break;
319
- case 57:
308
+ case 56:
320
309
  this.$ = {...$$[$0-1], start: $$[$0-2].start, end: $$[$0].end};
321
310
  break;
322
- case 58:
311
+ case 57:
323
312
  this.$ = {...union($$[$0-1]), start: $$[$0-2].start, end: $$[$0].end};
324
313
  break;
325
- case 59:
314
+ case 58:
326
315
  this.$ = {op: "ref", ref: "@" + $$[$0].value, start: $$[$0-1].start, end: $$[$0].end};
327
316
  break;
328
- case 60:
317
+ case 59:
329
318
  this.$ = {...identity, start: $$[$0-1].start, end: $$[$0].end};
330
319
  break;
331
- case 61:
320
+ case 60:
332
321
  this.$ = {...$$[$0-1], start: $$[$0-2].start, end: $$[$0].end };
333
322
  break;
334
- case 62:
323
+ case 61:
335
324
  this.$ = {op: "ref", ref: $$[$0].value, start: $$[$0].start, end: $$[$0].end};
336
325
  break;
337
- case 63: case 64:
326
+ case 62: case 63:
338
327
  this.$ = {op: "dot", dot: $$[$0].value, start: $$[$0-1].start, end: $$[$0].end };
339
328
  break;
340
- case 65: case 66:
329
+ case 64: case 65:
341
330
  this.$ = {op: "div", div: $$[$0].value, start: $$[$0-1].start, end: $$[$0].end };
342
331
  break;
343
- case 67: case 68:
332
+ case 66: case 67:
344
333
  this.$ = {op: "vid", vid: $$[$0].value, start: $$[$0-1].start, end: $$[$0].end };
345
334
  break;
346
- case 69:
335
+ case 68:
347
336
  this.$ = {op: "filter", filter: $$[$0], start: $$[$0-1].start, end:$$[$0].end};
348
337
  break;
349
- case 70:
338
+ case 69:
350
339
  this.$ = {op: "product", product: []};
351
340
  break;
352
- case 71:
341
+ case 70:
353
342
  $$[$0].product.reduce( (labels, {label}) =>
354
343
  { if (labels[label])
355
344
  anError(this._$,`Duplicate label '${label}'.`)
@@ -357,21 +346,21 @@ case 71:
357
346
  return labels; }, {});
358
347
  this.$ = $$[$0];
359
348
  break;
360
- case 72:
349
+ case 71:
361
350
  this.$ = {op: "product", product: [$$[$0]]};
362
351
  break;
363
- case 73:
352
+ case 72:
364
353
  $$[$0-2].product = [].concat($$[$0-2].product,$$[$0]); this.$ = $$[$0-2];
365
354
  break;
366
- case 74: case 75:
355
+ case 73: case 74:
367
356
  this.$ = {label: $$[$0].value, exp: $$[$0-1]};
368
357
  break;
369
- case 76: case 77:
358
+ case 75: case 76:
370
359
  this.$ = {label: $$[$0-2].value, exp: $$[$0]};
371
360
  break;
372
361
  }
373
362
  },
374
- table: [o($V0,[2,23],{45:1,46:2,43:3,44:[1,4]}),{1:[3]},o($V0,[2,25],{47:5}),o($V0,[2,24]),o($V0,[2,21]),{5:7,6:$V1,7:8,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:6,59:9},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,49:[1,26],59:27},o($Va,$Vb,{25:30,15:31,16:$Vc,26:$Vd}),{5:34,6:$V1},o($Ve,[2,55]),o([4,6,8,10,12,14,16,18,20,22,24,26,30,32,34,36,38,40,42,49],[2,2]),{6:[2,3]},{3:40,4:$Vf,5:39,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,20:[2,70],29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:38,59:9,60:35,62:36,63:37},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,18:[2,78],29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:44,59:9,61:42,64:43},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,21:45,22:$Vg,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:46,59:9},{3:49,4:$Vf,5:48,6:$V1},{3:51,4:$Vf,5:50,6:$V1},{3:53,4:$Vf,5:52,6:$V1},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:54,53:55},o([4,6,8,10,12,14,20,28,30,32,34,42],[2,5]),o([4,6,8,10,12,14,18,28,30,32,34,42],[2,4]),o([6,8,10,12,14,22,28,30,32,34,42],[2,6]),o($Vh,[2,14]),o($Vh,[2,15]),o($Vh,[2,16]),o([6,8,10,12,14],[2,20]),{1:[2,22]},o($Ve,[2,56]),{5:61,6:$V1},o([6,8,10,12,14,18,20,22,30,32,34,36,38,42,49],$Vb),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:62,59:9},{5:65,6:$V1,24:$Vi,50:63,52:64},o($V0,[2,12]),o([6,8,10,12,14,24],[2,7]),o($Va,$Vj,{15:66,16:$Vc}),{19:67,20:$Vk},{20:[2,71],35:69,36:$Vl},o($Vm,[2,72]),{3:72,4:$Vf,5:71,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,59:27},o($Vn,$Vb,{39:73,40:$Vo}),{39:75,40:$Vo},o([4,6,8,10,12,14,18,20,22,30,32,34,36,38,40,42,49],[2,1]),{17:76,18:$Vp},{18:[2,79],35:78,36:$Vl},o($Vq,[2,80],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Ve,[2,60]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,21:79,22:$Vg,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,59:27},o($Vr,[2,10]),o($Ve,[2,63]),o($Ve,[2,64]),o($Ve,[2,65]),o($Ve,[2,66]),o($Ve,[2,67]),o($Ve,[2,68]),o($Ve,[2,69]),o($Vs,[2,43],{26:[1,80]}),o($Vs,$Vt,{15:81,16:$Vc}),{27:82,28:$Vu},{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,18:$Vv,27:87,28:$Vu,51:88,53:55,54:84,57:85,58:86},{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,20:$Vv,27:87,28:$Vu,51:88,53:55,54:91,57:85,58:86},{5:92,6:$V1},o($Ve,$Vj),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,37:93,38:$Vw,41:18,42:$V9,59:27},{23:95,24:$Vx},{24:[2,30],35:97,36:$Vl},o($Vy,[2,31]),{5:65,6:$V1,24:$Vi,50:98,52:64},o($Ve,[2,57]),o($Vr,[2,9]),{3:40,4:$Vf,5:39,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:38,59:9,63:99},o([4,6,8,10,12,14,28,30,32,34,42],[2,17]),o($Vn,$Vb,{20:$Vz,36:$Vz}),o($Vm,[2,75]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:100,59:9},o($V0,[2,19]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:101,59:9},o($Ve,[2,58]),o($Vr,[2,8]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:102,59:9},o($Ve,[2,61]),{5:103,6:$V1},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,24:$VA,51:106,53:55,55:104,56:105},{21:107,22:$Vg},o([18,20,22,36],[2,13]),{17:108,18:$Vp},o([18,20],[2,47],{35:109,36:$Vl}),o($VB,[2,48]),o($VB,[2,50]),{3:111,4:$Vf,5:110,6:$V1},o($Vh,$Vt,{15:81,39:112,16:$Vc,40:$Vo}),{39:113,40:$Vo},{19:114,20:$Vk},o($Vr,[2,38],{15:115,16:$Vc}),o($V0,[2,26]),o($V0,[2,18]),{25:116,26:$Vd},o($Vr,[2,11]),{5:117,6:$V1},{23:118,24:$Vx},o($Vm,[2,73]),o($Vm,[2,76],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vm,[2,77],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vq,[2,81],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vs,[2,44]),{23:119,24:$Vx},{24:[2,40],35:120,36:$Vl},o($Vy,[2,41]),o($Vr,[2,33]),o($Vr,[2,34]),{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,27:87,28:$Vu,51:88,53:55,58:121},o($VB,[2,51]),o($VB,[2,52]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:122,53:55},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:123,53:55},o($Vr,[2,35]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,24:$VA,51:106,53:55,55:124,56:105},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:125,53:55},o($Vy,[2,32]),{25:126,26:$Vd},o($Vr,[2,36]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:127,53:55},o($VB,[2,49]),o($VB,[2,53]),o($VB,[2,54]),{23:128,24:$Vx},{37:129,38:$Vw},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:130,53:55},o($Vy,[2,42]),o($Vr,[2,37]),o($V0,[2,27]),{37:131,38:$Vw},o($V0,[2,28])],
363
+ table: [o($V0,[2,23],{45:1,46:2,43:3,44:[1,4]}),{1:[3]},o($V0,[2,25],{47:5}),o($V0,[2,24]),o($V0,[2,21]),{5:7,6:$V1,7:8,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:6,59:9},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,49:[1,26],59:27},o($Va,$Vb,{25:30,15:31,16:$Vc,26:$Vd}),{5:34,6:$V1},o($Ve,[2,54]),o([4,6,8,10,12,14,16,18,20,22,24,26,30,32,34,36,38,40,42,49],[2,2]),{6:[2,3]},{3:40,4:$Vf,5:39,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,20:[2,69],29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:38,59:9,60:35,62:36,63:37},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,18:[2,77],29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:44,59:9,61:42,64:43},{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,21:45,22:$Vg,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:46,59:9},{3:49,4:$Vf,5:48,6:$V1},{3:51,4:$Vf,5:50,6:$V1},{3:53,4:$Vf,5:52,6:$V1},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:54,53:55},o([4,6,8,10,12,14,20,28,30,32,34,42],[2,5]),o([4,6,8,10,12,14,18,28,30,32,34,42],[2,4]),o([6,8,10,12,14,22,28,30,32,34,42],[2,6]),o($Vh,[2,14]),o($Vh,[2,15]),o($Vh,[2,16]),o([6,8,10,12,14],[2,20]),{1:[2,22]},o($Ve,[2,55]),{5:61,6:$V1},o([6,8,10,12,14,18,20,22,30,32,34,36,38,42,49],$Vb),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:62,59:9},{5:65,6:$V1,24:$Vi,50:63,52:64},o($V0,[2,12]),o([6,8,10,12,14,24],[2,7]),o($Va,$Vj,{15:66,16:$Vc}),{19:67,20:$Vk},{20:[2,70],35:69,36:$Vl},o($Vm,[2,71]),{3:72,4:$Vf,5:71,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,59:27},o($Vn,$Vb,{39:73,40:$Vo}),{39:75,40:$Vo},o([4,6,8,10,12,14,18,20,22,30,32,34,36,38,40,42,49],[2,1]),{17:76,18:$Vp},{18:[2,78],35:78,36:$Vl},o($Vq,[2,79],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Ve,[2,59]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,21:79,22:$Vg,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,59:27},o($Vr,[2,10]),o($Ve,[2,62]),o($Ve,[2,63]),o($Ve,[2,64]),o($Ve,[2,65]),o($Ve,[2,66]),o($Ve,[2,67]),o($Ve,[2,68]),o($Vs,[2,42],{26:[1,80]}),o($Vs,$Vt,{15:81,16:$Vc}),{27:82,28:$Vu},{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,18:$Vv,27:87,28:$Vu,51:88,53:55,54:84,57:85,58:86},{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,20:$Vv,27:87,28:$Vu,51:88,53:55,54:91,57:85,58:86},{5:92,6:$V1},o($Ve,$Vj),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,37:93,38:$Vw,41:18,42:$V9,59:27},{23:95,24:$Vx},{24:[2,30],35:97,36:$Vl},o($Vy,[2,31]),{5:65,6:$V1,24:$Vi,50:98,52:64},o($Ve,[2,56]),o($Vr,[2,9]),{3:40,4:$Vf,5:39,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:38,59:9,63:99},o([4,6,8,10,12,14,28,30,32,34,42],[2,17]),o($Vn,$Vb,{20:$Vz,36:$Vz}),o($Vm,[2,74]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:100,59:9},o($V0,[2,19]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:101,59:9},o($Ve,[2,57]),o($Vr,[2,8]),{5:29,6:$V1,7:28,8:$V2,9:13,10:$V3,11:12,12:$V4,13:14,14:$V5,29:15,30:$V6,31:16,32:$V7,33:17,34:$V8,41:18,42:$V9,48:102,59:9},o($Ve,[2,60]),{5:103,6:$V1},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,24:$VA,51:106,53:55,55:104,56:105},{21:107,22:$Vg},o([18,20,22,36],[2,13]),{17:108,18:$Vp},o([18,20],[2,46],{35:109,36:$Vl}),o($VB,[2,47]),o($VB,[2,49]),{3:111,4:$Vf,5:110,6:$V1},o($Vh,$Vt,{15:81,39:112,16:$Vc,40:$Vo}),{39:113,40:$Vo},{19:114,20:$Vk},{15:115,16:$Vc},o($V0,[2,26]),o($V0,[2,18]),{25:116,26:$Vd},o($Vr,[2,11]),{5:117,6:$V1},{23:118,24:$Vx},o($Vm,[2,72]),o($Vm,[2,75],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vm,[2,76],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vq,[2,80],{11:12,9:13,13:14,29:15,31:16,33:17,41:18,59:27,7:28,5:29,6:$V1,8:$V2,10:$V3,12:$V4,14:$V5,30:$V6,32:$V7,34:$V8,42:$V9}),o($Vs,[2,43]),{23:119,24:$Vx},{24:[2,39],35:120,36:$Vl},o($Vy,[2,40]),o($Vr,[2,33]),o($Vr,[2,34]),{3:90,4:$Vf,5:89,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,27:87,28:$Vu,51:88,53:55,58:121},o($VB,[2,50]),o($VB,[2,51]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:122,53:55},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:123,53:55},o($Vr,[2,35]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,24:$VA,51:106,53:55,55:124,56:105},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:125,53:55},o($Vy,[2,32]),{25:126,26:$Vd},o($Vr,[2,36]),{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:127,53:55},o($VB,[2,48]),o($VB,[2,52]),o($VB,[2,53]),{23:128,24:$Vx},{37:129,38:$Vw},{5:56,6:$V1,7:60,8:$V2,9:58,10:$V3,11:59,12:$V4,13:57,14:$V5,51:130,53:55},o($Vy,[2,41]),o($Vr,[2,37]),o($V0,[2,27]),{37:131,38:$Vw},o($V0,[2,28])],
375
364
  defaultActions: {11:[2,3],26:[2,22]},
376
365
  parseError: function parseError (str, hash) {
377
366
  if (hash.recoverable) {
package/repl.mjs CHANGED
@@ -137,10 +137,19 @@ function aliasLine(kind, name, hash) {
137
137
  return `${name} = @${body};`;
138
138
  }
139
139
 
140
- function aliasPreamble(state, omitName = null) {
140
+ function shouldOmitAlias(name, omit) {
141
+ if (!omit) return false;
142
+ if (omit instanceof Set) return omit.has(name);
143
+ if (typeof omit === "string") return name === omit;
144
+ return false;
145
+ }
146
+
147
+ function aliasPreamble(state, omit = null) {
141
148
  const lines = [];
142
149
  for (const [name, hash] of Object.entries(state.relAliases).sort()) {
143
- if (name !== omitName) lines.push(aliasLine("rel", name, hash));
150
+ if (!shouldOmitAlias(name, omit)) {
151
+ lines.push(aliasLine("rel", name, hash));
152
+ }
144
153
  }
145
154
  for (const [name, hash] of Object.entries(
146
155
  state.patternAliases || {}
@@ -148,20 +157,20 @@ function aliasPreamble(state, omitName = null) {
148
157
  if (
149
158
  name.startsWith("@") ||
150
159
  name === hash ||
151
- name === omitName
160
+ shouldOmitAlias(name, omit)
152
161
  ) {
153
162
  continue;
154
163
  }
155
164
  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
+ const params = pat?.params || [];
166
+ const paramStr =
167
+ params.length > 0 ? `[${params.join(", ")}]` : "[]";
168
+ const body = hash.startsWith("@") ? hash.slice(1) : hash;
169
+ const info = resolvePatternCanonicalInfo(state, name, pat);
170
+ const args = info?.aliasArgs || pat?._aliasArgs || params;
171
+ const argStr =
172
+ args && args.length > 0 ? `[${args.join(", ")}]` : paramStr;
173
+ lines.push(`${name}${paramStr} = @${body}${argStr};`);
165
174
  }
166
175
  return lines.filter(Boolean).join("\n");
167
176
  }
@@ -179,14 +188,30 @@ function remapDiagnosticMessage(message, lineOffset) {
179
188
  if (!lineOffset) return message;
180
189
 
181
190
  let rewritten = message.replace(
182
- /\(lines (\d+):(\d+)\.\.\.(\d+):(\d+)\)/g,
183
- (_, startLine, startCol, endLine, endCol) =>
184
- `(lines ${remapLineNumber(Number(startLine), lineOffset)}:${startCol}...${remapLineNumber(Number(endLine), lineOffset)}:${endCol})`
191
+ /\(lines (\d+):(\d+)(?:\.\.\.(\d+):(\d+))?\)/g,
192
+ (_, startLine, startCol, endLine, endCol) => {
193
+ const remStart = remapLineNumber(Number(startLine), lineOffset);
194
+ if (endLine != null && endCol != null) {
195
+ const remEnd = remapLineNumber(Number(endLine), lineOffset);
196
+ return `(lines ${remStart}:${startCol}...${remEnd}:${endCol})`;
197
+ }
198
+ return `(lines ${remStart}:${startCol})`;
199
+ }
200
+ );
201
+
202
+ rewritten = rewritten.replace(
203
+ /(?:Parse|Lexical) error on line (\d+):/g,
204
+ (_, line) =>
205
+ `Parse error on line ${remapLineNumber(Number(line), lineOffset)}:`
185
206
  );
186
207
 
187
208
  rewritten = rewritten.replace(
188
- /Parse error on line (\d+):/g,
189
- (_, line) => `Parse error on line ${remapLineNumber(Number(line), lineOffset)}:`
209
+ /(?<!\()lines (\d+):(\d+)\.\.\.(\d+):(\d+)/g,
210
+ (_, startLine, startCol, endLine, endCol) => {
211
+ const remStart = remapLineNumber(Number(startLine), lineOffset);
212
+ const remEnd = remapLineNumber(Number(endLine), lineOffset);
213
+ return `lines ${remStart}:${startCol}...${remEnd}:${endCol}`;
214
+ }
190
215
  );
191
216
 
192
217
  return rewritten;
@@ -274,10 +299,6 @@ function mergeLibrary(state, lib, source = "<load>", options = {}) {
274
299
  ...(lib.patterns || lib.sourceDefs?.patterns ||
275
300
  lib.defs?.patterns || {})
276
301
  };
277
- state.patternAliases = {
278
- ...state.patternAliases,
279
- ...(lib.patternAlias || {})
280
- };
281
302
  mergeMeta(state, lib.meta);
282
303
 
283
304
  if (loadAliases) {
@@ -481,17 +502,32 @@ function resolvePatternCanonicalName(state, name, pat) {
481
502
  function prettyPattern(name, pat, state = null) {
482
503
  const info = resolvePatternCanonicalInfo(state, name, pat);
483
504
  const isHash = typeof name === "string" && name.startsWith("@");
505
+ const isDirectAlias =
506
+ pat?.filter?.type === "pattern_apply" &&
507
+ typeof pat.filter.pattern === "string" &&
508
+ pat.filter.pattern.startsWith("@");
509
+
484
510
  const paramList =
485
511
  isHash && (info.canonicalParams || pat?.params)
486
512
  ? (info.canonicalParams || pat.params)
487
- : info.params != null
488
- ? info.params
489
- : (pat?.params || []);
513
+ : (pat?.params || info.params || []);
490
514
  const params = paramList.join(", ");
491
- const filterStr = info.def ||
492
- (pat?.filter ? prettyFilter(pat.filter) : "()");
515
+
516
+ let filterStr;
517
+ if (isHash) {
518
+ filterStr =
519
+ info.def || (pat?.filter ? prettyFilter(pat.filter) : "()");
520
+ } else if (pat?.filter && !isDirectAlias) {
521
+ filterStr = prettyFilter(pat.filter);
522
+ } else {
523
+ filterStr =
524
+ info.def || (pat?.filter ? prettyFilter(pat.filter) : "()");
525
+ }
526
+
527
+ const aliasArgs = info.aliasArgs || pat?.params || [];
528
+ const argStr = aliasArgs.join(", ");
493
529
  const suffix =
494
- info.hash && !isHash ? ` -- ${info.hash}` : "";
530
+ info.hash && !isHash ? ` -- ${info.hash}[${argStr}]` : "";
495
531
  return `${name}[${params}] = ${filterStr};${suffix}`;
496
532
  }
497
533
 
@@ -603,6 +639,15 @@ function userDefinedNames(source) {
603
639
  };
604
640
  }
605
641
 
642
+ function definedNamesInSource(source) {
643
+ try {
644
+ const { relNames, patternNames } = userDefinedNames(source);
645
+ return new Set([...(relNames || []), ...(patternNames || [])]);
646
+ } catch {
647
+ return new Set();
648
+ }
649
+ }
650
+
606
651
  function originFromSourceNode(source, name, compiledAt, node) {
607
652
  return {
608
653
  source,
@@ -1160,7 +1205,7 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
1160
1205
  } else {
1161
1206
  restoreCodes(state);
1162
1207
  const source = fs.readFileSync(loadPath, "utf8");
1163
- const preamble = aliasPreamble(state);
1208
+ const preamble = aliasPreamble(state, definedNamesInSource(source));
1164
1209
  const fullSource = [preamble, source].filter(Boolean).join("\n");
1165
1210
  const compileOptions = {
1166
1211
  source: targetPath,
@@ -1240,7 +1285,7 @@ function analyzeAcceptedSnippet(source, explicitTerminated = explicitSnippetTerm
1240
1285
 
1241
1286
  function compileSnippetArtifacts(source, state, sourceName = "<repl>") {
1242
1287
  restoreCodes(state);
1243
- const preamble = aliasPreamble(state);
1288
+ const preamble = aliasPreamble(state, definedNamesInSource(source));
1244
1289
  const fullSource = [preamble, source].filter(Boolean).join("\n");
1245
1290
  const options = {
1246
1291
  source: sourceName,
@@ -8,8 +8,7 @@ import { performance } from "node:perf_hooks";
8
8
 
9
9
  const DEFAULT_EXAMPLES = [
10
10
  "Examples/nat.k",
11
- "Examples/byte.k",
12
- "Examples/bnat.k",
11
+ "Examples/list.k",
13
12
  "Examples/arithmetics.k",
14
13
  "Examples/ieee.k"
15
14
  ];
@@ -24,7 +23,7 @@ for K source files through the .klib object format.
24
23
  Arguments:
25
24
  example.k ...
26
25
  One or more .k source files to benchmark.
27
- (default: Examples/nat.k Examples/byte.k Examples/bnat.k
26
+ (default: Examples/nat.k Examples/list.k
28
27
  Examples/arithmetics.k Examples/ieee.k)
29
28
 
30
29
  Options:
@@ -51,6 +50,7 @@ function run(args) {
51
50
  encoding: "utf8",
52
51
  stdio: ["ignore", "pipe", "pipe"]
53
52
  });
53
+ if (result.error) throw result.error;
54
54
  if (result.status !== 0) {
55
55
  throw new Error(`${args.join(" ")} failed\n${result.stderr || result.stdout}`);
56
56
  }
@@ -38,7 +38,7 @@ const tests = [
38
38
  ["tests/test-named-patterns.mjs"]
39
39
  ],
40
40
  ...derivationTests,
41
- ["tests/test-fingerprint.mjs", node, ["tests/test-fingerprint.mjs"]],
41
+ ["tests/test-paper-examples.mjs", node, ["tests/test-paper-examples.mjs"]],
42
42
  ["tests/test-hash-fuzz.mjs", node, ["tests/test-hash-fuzz.mjs"]],
43
43
  ["tests/test-hash-normalization.mjs", node, ["tests/test-hash-normalization.mjs"]],
44
44
  ["tests/test-structural-values.mjs", node, ["tests/test-structural-values.mjs"]],
@@ -0,0 +1,64 @@
1
+ import assert from "node:assert/strict";
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { spawnSync } from "node:child_process";
6
+ import { pathToFileURL } from "node:url";
7
+
8
+ const packageDir = path.resolve(process.argv[2] || ".");
9
+ const expected = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url)));
10
+ const manifest = JSON.parse(fs.readFileSync(path.join(packageDir, "package.json")));
11
+ assert.equal(manifest.name, expected.name);
12
+ assert.equal(manifest.version, expected.version);
13
+ for (const file of ["parser.mjs", "valueParser.mjs", "core.k", "LICENSE",
14
+ "backends/wasm/runtime.wat", "backends/llvm/runtime/krt.c"]) {
15
+ assert.ok(fs.existsSync(path.join(packageDir, file)), file);
16
+ }
17
+ assert.ok(!fs.existsSync(path.join(packageDir, "browser/vfs-data.mjs")),
18
+ "Generated VFS data must not be packed");
19
+ for (const file of ["AGENTS.md", "CLAUDE.md"]) {
20
+ assert.ok(!fs.existsSync(path.join(packageDir, file)), file);
21
+ }
22
+
23
+ const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "k-package-smoke-"));
24
+ function run(name, args = [], input) {
25
+ const bin = manifest.bin[name];
26
+ assert.ok(bin, name);
27
+ const result = spawnSync(process.execPath, [path.join(packageDir, bin), ...args], {
28
+ cwd: scratch, input, maxBuffer: 8 * 1024 * 1024
29
+ });
30
+ assert.ifError(result.error);
31
+ assert.equal(result.status, 0, `${name}: ${result.stderr}`);
32
+ return result.stdout;
33
+ }
34
+
35
+ try {
36
+ for (const name of Object.keys(manifest.bin)) {
37
+ assert.ok(run(name, ["--help"]).length > 0, `${name} help`);
38
+ }
39
+
40
+ const { default: k } = await import(pathToFileURL(path.join(packageDir, "index.mjs")));
41
+ assert.equal(k.compile("{} |ok")(k.fromObject({})).tag, "ok");
42
+
43
+ const unit = run("k-unit", ["--parse"]);
44
+ const hello = run("k", ["{} |ok"], unit);
45
+ assert.equal(run("k-print", [], hello).toString().trim(), '"ok"');
46
+
47
+ const integer = run("k-int", ["--parse"], "42\n");
48
+ const incremented = run("k", ["--lib", path.join(packageDir, "Examples/arithmetics.k"),
49
+ "--export", "inc", "inc"], integer);
50
+ assert.equal(run("k-int", ["--print"], incremented).toString().trim(), "43");
51
+
52
+ const object = path.join(scratch, "ok.ko");
53
+ run("k-compile", ["?{} {} |ok", object]);
54
+ assert.match(run("k-validate-object", [object]).toString(), /OK object/);
55
+ assert.equal(run("k-print", [], run("k", [object], unit)).toString().trim(), '"ok"');
56
+
57
+ const wasm = path.join(scratch, "ok.wasm");
58
+ run("k-wasm-compile", ["?{} {} |ok", wasm]);
59
+ assert.equal(run("k-print", [], run("k-wasm-run", [wasm], unit)).toString().trim(), '"ok"');
60
+
61
+ console.log(`Package ${manifest.name}@${manifest.version}: API, ${Object.keys(manifest.bin).length} CLIs, codecs, objects and Wasm passed on ${process.version}`);
62
+ } finally {
63
+ fs.rmSync(scratch, { recursive: true, force: true });
64
+ }
package/tests/README.md CHANGED
@@ -1,46 +1,66 @@
1
- # k Test Suite
2
-
3
- This directory contains automated tests, type-derivation validation
4
- cases, and integration test scripts for the `k` compiler and runtime.
5
-
6
- ## Running the Tests
7
-
8
- ### 1. Run Unit and Integration Test Suite
9
- To run all core unit tests, type-derivation checks, and integration tests:
10
- ```bash
11
- npm test
12
- ```
13
- This executes `node scripts/run-tests.mjs`, which discovers and runs the
14
- test scripts sequentially.
15
-
16
- ## Test Directory Structure
17
-
18
- ### Core Test Files
19
- - **[test.mjs](test.mjs)**: Verifies basic parser and runtime
20
- execution behavior.
21
- - **[test-kvm.mjs](test-kvm.mjs)**: Conformance testing for the kVM
22
- register compiler and interpreter in envelope-aware and envelope-free
23
- modes.
24
- - **[test-kvm-polymorphic.mjs](test-kvm-polymorphic.mjs)**: Tests for
25
- polymorphic `.kvm` templates (`layer: "KVM-P"`) and runtime input
26
- envelope specialization (`specializeKVM`).
27
- - **[test-ieee-arithmetic.mjs](test-ieee-arithmetic.mjs)**: Verifies
28
- IEEE-754 double precision float arithmetic (`Examples/ieee.k`).
29
- - **[test-repl.mjs](test-repl.mjs)**: Tests interactive REPL commands,
30
- state transitions, imports, `.klib` and `.ko` exports.
31
- - **[test-k-object.mjs](test-k-object.mjs)**: Tests serialization,
32
- compilation, and loading of `.ko` binary objects and `.klib` libraries.
33
- - **[test-fingerprint.mjs](test-fingerprint.mjs)**: Tests semantic
34
- expression and relation hashing/fingerprinting (`@hash`).
35
- - **[test-hash-normalization.mjs](test-hash-normalization.mjs)**:
36
- Verifies consistent normalization of type pattern graphs and hashes.
37
- - **[test-hash-fuzz.mjs](test-hash-fuzz.mjs)**: Fuzzes expression and
38
- relation hashing to guard against collisions.
39
-
40
- ### Type Derivation Suites
41
- - **[code-derivation/](code-derivation/)**: Individual files validating
42
- type-derivation convergence and pattern-envelope generation.
43
-
44
- ### Integration Scripts
45
- - **[integration.sh](integration.sh)**: Shell script checking boundary
46
- data pipelines (`k-parse`, `k-show`, `k-print`) against binary outputs.
1
+ # Tests
2
+
3
+ Run commands below from the repository root after `npm install`.
4
+ Use Node.js 22+ for development: browser verification uses the global WebSocket
5
+ client. The source parser and runtime can run without a browser.
6
+
7
+ ## Suite commands
8
+
9
+ | Command | Coverage |
10
+ | --- | --- |
11
+ | `npm test` | Registered runtime, patterns, paper examples, codecs, hashes, objects, REPL, browser, kVM, PKIR, library, and shell suites |
12
+ | `npm run conformance` | Shared source/object/KIR-P fixtures |
13
+ | `npm run test:wasm` | Wasm runtime/lowering and backend shell integration |
14
+ | `npm run test:llvm` | LLVM runtime/lowering, field-layout regression, shell integration, and shared fixtures |
15
+ | `npm run test:backends` | Both backend suites |
16
+ | `npm --prefix k-vscode test` | Editor grammar and expression-background tests |
17
+ | `node tests/test-paper-examples.mjs` | Publication examples, including canonical hashes and the half adder |
18
+ | `npm run test:package -- /path/to/installed/@fraczak/k` | Packed installation, API, command help, codec pipeline, objects, and Wasm |
19
+
20
+ The backend and editor suites are separate commands; `npm test` does not run
21
+ every suite in this table. The paper command is also available as a focused check.
22
+
23
+ The main runner executes suites sequentially, prints durations, and stops at
24
+ the first failure. Browser tests build `repl.html` and perform headless
25
+ verification if Chromium is found. Without Chromium, that browser execution
26
+ portion is skipped. LLVM execution checks require Clang; individual optional
27
+ checks report skips when their toolchain is unavailable.
28
+
29
+ ## Registration and organization
30
+
31
+ Root suites are explicitly registered in
32
+ [scripts/run-tests.mjs](../scripts/run-tests.mjs).
33
+ Files ending in `.mjs` under [code-derivation/](code-derivation/) are discovered
34
+ automatically. The compact `core.mjs` table groups the elementary derivation
35
+ cases; shared helpers live outside the discovered directory.
36
+
37
+ | Area | Representative suites |
38
+ | --- | --- |
39
+ | Templates and routing | `test-named-patterns.mjs` |
40
+ | Derivation and envelope propagation | `code-derivation/` |
41
+ | Canonical identities | `test-hash-normalization.mjs` (including code fingerprints), `test-hash-fuzz.mjs` |
42
+ | Wire values and codecs | `test-codecs.mjs`, `test-deep-wire-value.mjs`, `test-structural-values.mjs` |
43
+ | Objects and portable export | `test-k-object.mjs`, `test-kir.mjs`, `test-validate-object.mjs` |
44
+ | Interactive state and browser UI | `test-repl.mjs`, `test-repl-html.mjs`, `test-tree-view.mjs` |
45
+ | Execution layers | `test-kvm.mjs`, `test-kvm-polymorphic.mjs`, `test-access-table.mjs`, `test-pkir.mjs` |
46
+ | Libraries | Arithmetic, IEEE, list, Karatsuba, and dynamic JSON suites |
47
+
48
+ `test-hash-fuzz.mjs` uses a deterministic generator to check renaming and
49
+ definition-order invariance; it is not a general cryptographic collision test.
50
+
51
+ The [conformance fixtures](../conformance/) provide programs, input values,
52
+ expected values, and case descriptions reused by supported execution paths.
53
+ A rendered payload alone can hide envelope differences, so keep pattern
54
+ comparisons when checking structural correctness.
55
+
56
+ Arithmetic and Karatsuba tests share CLI setup in `helpers/cli.mjs`. They still
57
+ run the library through `k.mjs` and compare numerical results with BigInt;
58
+ codec modules prepare and decode the wire values in process. The dedicated
59
+ codec and shell suites cover the codec command-line boundary.
60
+
61
+ ## Adding coverage
62
+
63
+ Use assertions for expected values, schemas, and failures. Register a new
64
+ root suite explicitly, or add a derivation case in the discovered directory.
65
+ Run the main suite and the relevant backend/editor checks. Grammar edits also
66
+ require `npm run prepare`; generated parser files should not be hand-edited.
@@ -0,0 +1,61 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { annotate } from "../../index.mjs";
4
+ import hash from "../../hash.mjs";
5
+
6
+ const bool = "@GWnxJCupZr96BZfdpDsfQQKxKjCbjyDh7Qaur7y9enCY";
7
+ const bit = "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg";
8
+ const unit = hash("$C0={};");
9
+ const cases = [
10
+ ["unit", "{}", { pattern: "(...)" }, { type: unit }],
11
+ ["product", `
12
+ ?<{} true, {} false> {() one, () two}
13
+ ?{<{} true, {} false> one, <{} true, {} false> two}
14
+ `, { type: bool }, { type: "@ZR2yff4pdXTVqZdMu9x3t9GG5rwmhEfQuenbAwFnD2Nk" }],
15
+ ["product projection", `
16
+ 1 = {} |1 ?<{} 0, {} 1>;
17
+ ?<{} true, {} false> {1 one, () two, () c} .one
18
+ `, { type: bool }, { type: bit }],
19
+ ["closed variant projection", "?< <{} 0, {} 1> true, <{} 0, {} 1> false> /true",
20
+ { pattern: "type" }, { type: bit }],
21
+ ["composed variant projection", `
22
+ x = /x; xy = x/y;
23
+ f = {{} |y |x xy i, {} |y |x xy b}; f .b
24
+ `, { pattern: "(...)" }, { type: unit }],
25
+ ["recursive open union", `
26
+ B = {}|u A;
27
+ A = ?X < /a, /b ?X A, /c |z ?X A, B >; A
28
+ `, { pattern: "<...>" }, { pattern: "(...)" }],
29
+ ["double negation", `
30
+ true = {} |true; false = {} |false;
31
+ not = ?<{} true, {} false> </true false, /false true> ?<{} true, {} false>;
32
+ not not
33
+ `, { type: bool }, { type: bool }],
34
+ ["leading zero normalization", `
35
+ zero = {}|_|0;
36
+ rlz = ?<X 0, X 1, {} _>=X </0/_ zero, /0 rlz, ()> ?X; rlz
37
+ `, { type: "@VtPHxGf5GNMzzyVFxtv7gegFfJRYapBGtCeyV56bs5Zb" },
38
+ { type: "@VtPHxGf5GNMzzyVFxtv7gegFfJRYapBGtCeyV56bs5Zb" }],
39
+ ["Peano arithmetic", readFileSync(new URL("../../Examples/nat.k", import.meta.url), "utf8"),
40
+ { type: "@e9WP6QX6URfgnd9hHYrrYUBhiL7UcvbWErVmyQJfFcr8" },
41
+ { type: "@e9WP6QX6URfgnd9hHYrrYUBhiL7UcvbWErVmyQJfFcr8" }]
42
+ ];
43
+
44
+ function boundaries(source) {
45
+ const { def, typePatternGraph: graph } = annotate(source).rels.__main__;
46
+ return def.patterns.map((id) => graph.get_pattern(id));
47
+ }
48
+ for (const [name, source, ...expected] of cases) {
49
+ const actual = boundaries(source);
50
+ expected.forEach((fields, side) => {
51
+ for (const [key, value] of Object.entries(fields)) {
52
+ assert.equal(actual[side][key], value, `${name}: boundary ${side} ${key}`);
53
+ }
54
+ });
55
+ }
56
+ for (const [source, pattern] of [["</a, ()>", "<...>"], ["<.a, ()>", "{...}"]]) {
57
+ const [input, output] = boundaries(source);
58
+ assert.equal(input.pattern, pattern);
59
+ assert.equal(output, input, source);
60
+ }
61
+ console.log("core derivation cases: ok");