@fraczak/k 8.4.0 → 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 (113) hide show
  1. package/CHANGELOG.md +59 -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 +156 -123
  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 -371
  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/llvm/src/cli.mjs +0 -31
  27. package/backends/wasm/README.md +88 -66
  28. package/backends/wasm/src/cli.mjs +0 -31
  29. package/canonical-pattern.mjs +69 -35
  30. package/codecs/README.md +23 -261
  31. package/codecs/WIRE_FORMAT.md +132 -0
  32. package/core.k +5 -15
  33. package/index.mjs +5 -1
  34. package/named-patterns.mjs +55 -7
  35. package/object.mjs +28 -6
  36. package/objects/README.md +24 -109
  37. package/package.json +8 -3
  38. package/paper/Makefile +17 -0
  39. package/paper/k-paper.pdf +0 -0
  40. package/paper/k-paper.tex +435 -0
  41. package/parser.jison +0 -10
  42. package/parser.mjs +32 -43
  43. package/repl.mjs +79 -101
  44. package/scripts/benchmark-idempotency.mjs +3 -3
  45. package/scripts/run-tests.mjs +1 -1
  46. package/scripts/test-package.mjs +64 -0
  47. package/tests/README.md +66 -46
  48. package/tests/code-derivation/core.mjs +61 -0
  49. package/tests/helpers/cli.mjs +45 -0
  50. package/tests/integration.sh +2 -3
  51. package/tests/test-arithmetics.mjs +47 -237
  52. package/tests/test-djson-methods.mjs +71 -275
  53. package/tests/test-hash-normalization.mjs +22 -1
  54. package/tests/test-karatsuba.mjs +18 -126
  55. package/tests/test-named-patterns.mjs +174 -606
  56. package/tests/test-paper-examples.mjs +90 -0
  57. package/tests/test-poly.mjs +2 -2
  58. package/tests/test-repl-html.mjs +2 -2
  59. package/tests/test-repl.mjs +89 -38
  60. package/tests/test.mjs +27 -68
  61. package/AGENTS.md +0 -35
  62. package/CLAUDE.md +0 -64
  63. package/DOCS/BIG_PICTURE.md +0 -439
  64. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  65. package/DOCS/CANONICAL_PATTERNS.md +0 -349
  66. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  67. package/DOCS/CONVERGENCE.md +0 -123
  68. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  69. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  70. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  71. package/DOCS/Makefile +0 -99
  72. package/DOCS/NAMED_PATTERNS.md +0 -340
  73. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  74. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  75. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
  76. package/DOCS/book/01-introduction.md +0 -48
  77. package/DOCS/book/02-syntax-and-values.md +0 -110
  78. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  79. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  80. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  81. package/DOCS/book/06-values-in-memory.md +0 -1085
  82. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  83. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  84. package/DOCS/book/09-compiler-architecture.md +0 -160
  85. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  86. package/DOCS/book/14-canonical-serialization.md +0 -176
  87. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  88. package/DOCS/book/overview.txt +0 -68
  89. package/DOCS/book.md +0 -50
  90. package/Examples/bnat.k +0 -308
  91. package/Examples/buda.k +0 -66
  92. package/Examples/byte.k +0 -35
  93. package/Examples/code.k +0 -43
  94. package/Examples/vhdl_to_k.k +0 -67
  95. package/JOURNAL.md +0 -288
  96. package/TODO.md +0 -107
  97. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  98. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  99. package/backends/wasm/demo_ieee.mjs +0 -318
  100. package/browser/vfs-data.mjs +0 -21
  101. package/codecs/BINARY_FORMAT.md +0 -53
  102. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  103. package/codecs/pandoc.css +0 -71
  104. package/tests/code-derivation/blowup.mjs +0 -25
  105. package/tests/code-derivation/bnat.mjs +0 -11
  106. package/tests/code-derivation/dot.mjs +0 -37
  107. package/tests/code-derivation/index.mjs +0 -20
  108. package/tests/code-derivation/loop.mjs +0 -17
  109. package/tests/code-derivation/nat.mjs +0 -10
  110. package/tests/code-derivation/not-not.mjs +0 -14
  111. package/tests/code-derivation/product.mjs +0 -22
  112. package/tests/code-derivation/rlz.mjs +0 -13
  113. 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,
@@ -1153,15 +1198,6 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
1153
1198
  const normKey = normalizeFilePath(targetPath);
1154
1199
  if (!state.loadedFiles) state.loadedFiles = new Set();
1155
1200
 
1156
- // If loading poly.k, auto-load arithmetics.k first if not already loaded
1157
- if (normKey === "Examples/poly.k" || normKey.endsWith("/poly.k") || normKey === "poly.k") {
1158
- const arithKey = "arithmetics.k";
1159
- const alreadyHasArith = Array.from(state.loadedFiles).some(f => path.basename(f) === "arithmetics.k");
1160
- if (!alreadyHasArith) {
1161
- loadSourceOrKlib(state, arithKey, { loadAliases });
1162
- }
1163
- }
1164
-
1165
1201
  const loadPath = resolveKFilePath(targetPath);
1166
1202
  if (loadPath.endsWith(".klib")) {
1167
1203
  const lib = loadLibrary(decodeLibrary(fs.readFileSync(loadPath)));
@@ -1169,7 +1205,7 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
1169
1205
  } else {
1170
1206
  restoreCodes(state);
1171
1207
  const source = fs.readFileSync(loadPath, "utf8");
1172
- const preamble = aliasPreamble(state);
1208
+ const preamble = aliasPreamble(state, definedNamesInSource(source));
1173
1209
  const fullSource = [preamble, source].filter(Boolean).join("\n");
1174
1210
  const compileOptions = {
1175
1211
  source: targetPath,
@@ -1184,45 +1220,13 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
1184
1220
  lib,
1185
1221
  compileOptions
1186
1222
  );
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
1223
  mergeLibrary(
1219
1224
  state,
1220
1225
  {
1221
1226
  ...lib,
1222
1227
  relAlias,
1223
1228
  patternAlias,
1224
- meta,
1225
- patterns: filePatterns
1229
+ meta
1226
1230
  },
1227
1231
  targetPath,
1228
1232
  { loadAliases }
@@ -1281,7 +1285,7 @@ function analyzeAcceptedSnippet(source, explicitTerminated = explicitSnippetTerm
1281
1285
 
1282
1286
  function compileSnippetArtifacts(source, state, sourceName = "<repl>") {
1283
1287
  restoreCodes(state);
1284
- const preamble = aliasPreamble(state);
1288
+ const preamble = aliasPreamble(state, definedNamesInSource(source));
1285
1289
  const fullSource = [preamble, source].filter(Boolean).join("\n");
1286
1290
  const options = {
1287
1291
  source: sourceName,
@@ -1298,36 +1302,10 @@ function compileSnippetArtifacts(source, state, sourceName = "<repl>") {
1298
1302
  );
1299
1303
  const annotated = annotateWithOptionalIdentity(fullSource, options);
1300
1304
  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
- }
1305
+ const patterns = {
1306
+ ...(state.patterns || {}),
1307
+ ...(lib.patterns || {})
1308
+ };
1331
1309
  return {
1332
1310
  annotated,
1333
1311
  lib: {
@@ -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
+ }