@fraczak/k 6.2.3 → 6.3.1

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.
@@ -1,6 +1,6 @@
1
1
  import assert from "node:assert/strict";
2
2
  import k from "../../index.mjs";
3
- import { Product, Variant, mergePatterns, withPattern } from "../../Value.mjs";
3
+ import { Value, mergePatterns, withPattern } from "../../Value.mjs";
4
4
  import { decodeWire, encodeToWire } from "../../codecs/runtime/prefix-codec.mjs";
5
5
  import run from "../../run.mjs";
6
6
  import codes from "../../codes.mjs";
@@ -17,16 +17,16 @@ const script = `
17
17
  {}|_|0|1|+ $int
18
18
  `;
19
19
 
20
- const result = k.compile(script)(new Product({}));
20
+ const result = k.compile(script)(Value.product({}));
21
21
  assert.deepEqual(result.pattern, INT_PATTERN);
22
22
 
23
23
  const decoded = decodeWire(encodeToWire(result, result.pattern));
24
24
  assert.deepEqual(decoded.pattern, INT_PATTERN);
25
25
 
26
- const two = new Variant("+",
27
- new Variant("1",
28
- new Variant("0",
29
- new Variant("_", new Product({}))
26
+ const two = Value.variant("+",
27
+ Value.variant("1",
28
+ Value.variant("0",
29
+ Value.variant("_", Value.product({}))
30
30
  )
31
31
  )
32
32
  );
@@ -53,13 +53,13 @@ const wrappedFloatPattern = [
53
53
  ["closed-product", []],
54
54
  ["closed-union", [["inexact", 5]]]
55
55
  ];
56
- const wrappedFloat = new Product({
57
- result: new Product({
58
- sign: new Variant("+", new Product({})),
59
- exponent: new Product({ 0: new Variant("1", new Product({})) }),
60
- fraction: new Product({ 0: new Variant("0", new Product({})) })
56
+ const wrappedFloat = Value.product({
57
+ result: Value.product({
58
+ sign: Value.variant("+", Value.product({})),
59
+ exponent: Value.product({ 0: Value.variant("1", Value.product({})) }),
60
+ fraction: Value.product({ 0: Value.variant("0", Value.product({})) })
61
61
  }),
62
- flags: new Variant("inexact", new Product({}))
62
+ flags: Value.variant("inexact", Value.product({}))
63
63
  });
64
64
  const resultProjection = k.compile(".result")(withPattern(wrappedFloat, wrappedFloatPattern));
65
65
  assert.deepEqual(resultProjection.pattern, [
@@ -19,6 +19,22 @@ grep -q '"__main__"' "$TMP_DIR/stdin.kvm"
19
19
  ./objects/compile.mjs --lib "$TMP_DIR/export.klib" --export id:alias 'alias' "$TMP_DIR/export.ko"
20
20
  ./objects/decompile.mjs "$TMP_DIR/export.ko" | grep -q '^----- main -----$'
21
21
  node ./codecs/unit.mjs --parse | ./k.mjs --lib "$TMP_DIR/export.klib" --export id:alias 'alias' | node ./codecs/unit.mjs --print | grep -qx '{}'
22
+ node ./codecs/unit.mjs --parse | ./k.mjs "$TMP_DIR/export.ko" | node ./codecs/unit.mjs --print | grep -qx '{}'
23
+ if ./k.mjs --lib "$TMP_DIR/export.klib" --lib "$TMP_DIR/export.klib" '()' > "$TMP_DIR/two-lib.out" 2> "$TMP_DIR/two-lib.err"; then
24
+ echo "expected repeated --lib to fail" >&2
25
+ exit 1
26
+ fi
27
+ grep -q -- '--lib may be specified at most once' "$TMP_DIR/two-lib.err"
28
+ if ./objects/compile.mjs --lib "$TMP_DIR/export.klib" --lib "$TMP_DIR/export.klib" '()' "$TMP_DIR/two-lib.ko" > "$TMP_DIR/two-lib-compile.out" 2> "$TMP_DIR/two-lib-compile.err"; then
29
+ echo "expected repeated k-compile --lib to fail" >&2
30
+ exit 1
31
+ fi
32
+ grep -q -- '--lib may be specified at most once' "$TMP_DIR/two-lib-compile.err"
33
+ if ./kvm.mjs --lib "$TMP_DIR/export.klib" --lib "$TMP_DIR/export.klib" '()' > "$TMP_DIR/two-lib-kvm.out" 2> "$TMP_DIR/two-lib-kvm.err"; then
34
+ echo "expected repeated k-vm --lib to fail" >&2
35
+ exit 1
36
+ fi
37
+ grep -q -- '--lib may be specified at most once' "$TMP_DIR/two-lib-kvm.err"
22
38
  if ./objects/compile.mjs "$TMP_DIR/missing.k" > "$TMP_DIR/missing.out" 2> "$TMP_DIR/missing.err"; then
23
39
  echo "expected missing .k input to fail" >&2
24
40
  exit 1
@@ -27,11 +43,20 @@ grep -q 'Input file not found:' "$TMP_DIR/missing.err"
27
43
 
28
44
  echo '["zebara", "ela", "kupa", ala, owca]' | node ./codecs/k-parse.mjs | ./k.mjs '{.1 0,.3 1}' | node ./codecs/k-print.mjs
29
45
  echo
46
+ echo "a" | ./codecs/k-parse.mjs --input-type '?< {} a, X b, ...>' | ./k.mjs '{() i, /a o}' | ./codecs/show.mjs > "$TMP_DIR/polymorphic-relation.wire" 2> "$TMP_DIR/polymorphic-relation.show"
47
+ grep -Fqx '{{}|a i, {} o} ?{<{}=X0 a, (...) b, ...> i, X0 o}' "$TMP_DIR/polymorphic-relation.show"
48
+ ./codecs/k-print.mjs "$TMP_DIR/polymorphic-relation.wire" | grep -qx '{"i":"a","o":{}}'
49
+ echo
30
50
  echo '-21' | node ./codecs/int.mjs --parse | node ./codecs/int.mjs --print | grep -qx -- '-21'
31
51
  echo
32
52
  node ./codecs/unit.mjs --parse | node ./codecs/unit.mjs --print | grep -qx '{}'
33
53
  echo
34
- printf 'A🙂\nBé~\t' | ./codecs/utf8.mjs --parse | ./k.mjs -k Examples/byte.k |./codecs/utf8.mjs --print
54
+ if ./k.mjs -k Examples/byte.k > "$TMP_DIR/dash-k.out" 2> "$TMP_DIR/dash-k.err"; then
55
+ echo "expected -k to fail" >&2
56
+ exit 1
57
+ fi
58
+ grep -q -- '-k is no longer supported' "$TMP_DIR/dash-k.err"
59
+ printf 'A🙂\nBé~\t' | ./codecs/utf8.mjs --parse | ./k.mjs Examples/byte.k |./codecs/utf8.mjs --print
35
60
  echo
36
61
  ./objects/compile.mjs Examples/ieee.k "$TMP_DIR/ieee.klib"
37
62
  MUL=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^mul = @' | sed 's/^mul = \(@[^;]*\);.*$/\1/'`
@@ -1,5 +1,5 @@
1
1
  import assert from "node:assert/strict";
2
- import { Product, Variant } from "../Value.mjs";
2
+ import { Value } from "../Value.mjs";
3
3
  import { decodeWire, encodeToWire } from "../codecs/runtime/prefix-codec.mjs";
4
4
 
5
5
  const depth = 12000;
@@ -9,12 +9,12 @@ const pattern = [
9
9
  ["closed-product", []]
10
10
  ];
11
11
 
12
- let bits = new Variant("_", new Product({}));
12
+ let bits = Value.variant("_", Value.product({}));
13
13
  for (let i = 0; i < depth; i++) {
14
- bits = new Variant("1", bits);
14
+ bits = Value.variant("1", bits);
15
15
  }
16
16
 
17
- const wire = encodeToWire(new Variant("+", bits), pattern);
17
+ const wire = encodeToWire(Value.variant("+", bits), pattern);
18
18
  const decoded = decodeWire(wire).value;
19
19
  assert.equal(decoded.tag, "+");
20
20
 
@@ -1,7 +1,7 @@
1
1
  import assert from "node:assert/strict";
2
2
 
3
3
  import { createState, evaluateInput } from "../repl.mjs";
4
- import { Product } from "../Value.mjs";
4
+ import { Value, isProduct } from "../Value.mjs";
5
5
  import codes from "../codes.mjs";
6
6
  import { parse as parseFloat64, print as printFloat64 } from "../codecs/ieee.mjs";
7
7
  import { singletonPatternToCodeHash, valueForCode } from "../repl-codecs.mjs";
@@ -21,7 +21,7 @@ function float64(text) {
21
21
  }
22
22
 
23
23
  function floatPair(x, y) {
24
- return valueForCode(new Product({
24
+ return valueForCode(Value.product({
25
25
  x: float64(x),
26
26
  y: float64(y)
27
27
  }), floatPairHash, codes.find);
@@ -46,7 +46,7 @@ function assertFloatResult(value, expected, label) {
46
46
 
47
47
  async function assertOperation({ op, x, y, result, flags = null, projection = false }) {
48
48
  const bundle = await runOperation(op, x, y);
49
- assert.ok(bundle instanceof Product, `${op} should return a result bundle`);
49
+ assert.ok(isProduct(bundle), `${op} should return a result bundle`);
50
50
  assert.deepEqual(Object.keys(bundle.product).sort(), ["flags", "result"]);
51
51
  assertFloatResult(bundle.product.result, result, `${op}(${x}, ${y}).result`);
52
52
  if (flags != null) {
@@ -3,7 +3,7 @@ import fs from "node:fs";
3
3
  import k from "../index.mjs";
4
4
  import { parseValue } from "../valueIO.mjs";
5
5
  import { decodeWire, encodeToWire, CORE_PATTERN_PROPERTY_LIST } from "../codecs/runtime/prefix-codec.mjs";
6
- import { Product, Variant } from "../Value.mjs";
6
+ import { Value } from "../Value.mjs";
7
7
  import { exportPatternGraph } from "../codecs/runtime/codec.mjs";
8
8
  import { patternToPropertyList } from "../codecs/runtime/pattern-json.mjs";
9
9
  import {
@@ -27,9 +27,9 @@ assert.deepEqual(decodedWire.pattern, [
27
27
  ]);
28
28
  assert.deepEqual(decodedWire.value.toJSON(), value.toJSON());
29
29
 
30
- const unicodeLabelValue = new Product({
31
- "é": new Variant("🙂", new Product({})),
32
- "𐐷": new Product({})
30
+ const unicodeLabelValue = Value.product({
31
+ "é": Value.variant("🙂", Value.product({})),
32
+ "𐐷": Value.product({})
33
33
  });
34
34
  const unicodeWire = encodeToWire(unicodeLabelValue, null);
35
35
  const decodedUnicode = decodeWire(unicodeWire);
@@ -73,15 +73,15 @@ assert(object.meta[object.relAlias.__main__]?.origins?.some((origin) =>
73
73
  ));
74
74
 
75
75
  const objectFn = objectToFunction(object);
76
- const objectResult = objectFn(new Product({}));
76
+ const objectResult = objectFn(Value.product({}));
77
77
  assert.deepEqual(objectResult.pattern, INT_PATTERN);
78
78
  assert.deepEqual(objectResult.toJSON(), { "+": { "1": { "0": "_" } } });
79
- const convergedObjectResult = runConvergedObject(object, new Product({}));
79
+ const convergedObjectResult = runConvergedObject(object, Value.product({}));
80
80
  assert.deepEqual(convergedObjectResult.pattern, INT_PATTERN);
81
81
  assert.deepEqual(convergedObjectResult.toJSON(), objectResult.toJSON());
82
82
 
83
83
  const decompiledSource = decompileObjectBuffer(objectBuffer);
84
- const decompiledResult = k.compile(decompiledSource)(new Product({}));
84
+ const decompiledResult = k.compile(decompiledSource)(Value.product({}));
85
85
  assert.deepEqual(decompiledResult.pattern, INT_PATTERN);
86
86
  assert.deepEqual(decompiledResult.toJSON(), objectResult.toJSON());
87
87
  assert.match(decompiledSource, /^----- codes -----$/m);
@@ -112,11 +112,11 @@ assert.equal(arithmeticsRoundTripSource, arithmeticsDecompiledSource);
112
112
 
113
113
  assert.throws(() => k.compile("@A = (); @A"), /Parse error/);
114
114
  assert.throws(() => k.compile("$ @A = {}; $@A"), /Parse error/);
115
- assert.deepEqual(k.compile("7jfi = ?X0; 7jfi")(new Product({})).toJSON(), {});
115
+ assert.deepEqual(k.compile("7jfi = ?X0; 7jfi")(Value.product({})).toJSON(), {});
116
116
 
117
117
  const sccSource = decompileObjectBuffer(compileObjectBuffer("c = {}; b = c |x; a = b |y; a"));
118
118
  assert.match(sccSource, /----- rels -----\nPQgV = .+\n\naQAD = .+\n\nN9UH = /s);
119
- assert.deepEqual(k.compile(sccSource)(new Product({})).toJSON(), { y: "x" });
119
+ assert.deepEqual(k.compile(sccSource)(Value.product({})).toJSON(), { y: "x" });
120
120
 
121
121
  const libraryBuffer = compileLibraryBuffer("$ nat = <{} zero, nat succ>;\nsucc = |succ;\n", { source: "defs-only.k" });
122
122
  assert.equal(libraryBuffer.subarray(0, 1).toString("utf8"), "{");
@@ -173,7 +173,7 @@ assert(derivedAliases.indexOf("other = @") < derivedAliases.indexOf("succ = @"))
173
173
  }));
174
174
  assert.equal(nonConverged.rels.__main__.typeDerivation.status, "not-converged");
175
175
  assert.throws(
176
- () => runConvergedObject(nonConverged, new Product({ x: new Product({}) })),
176
+ () => runConvergedObject(nonConverged, Value.product({ x: Value.product({}) })),
177
177
  /type derivation is not-converged/
178
178
  );
179
179
  } finally {
@@ -2,7 +2,7 @@ import assert from "node:assert";
2
2
  import k from "../index.mjs";
3
3
  import { lowerToKVM, executeKVM } from "../kvm.mjs";
4
4
  import codes from "../codes.mjs";
5
- import { fromObject, Product, Variant } from "../Value.mjs";
5
+ import { fromObject } from "../Value.mjs";
6
6
  import run, { run_converged } from "../run.mjs";
7
7
 
8
8
  function runKVM(script, data, options = {}) {
@@ -18,7 +18,7 @@ import {
18
18
  lineTerminatesSnippet
19
19
  } from "../repl.mjs";
20
20
  import { decodeObject, objectToFunction } from "../object.mjs";
21
- import { Product } from "../Value.mjs";
21
+ import { Value } from "../Value.mjs";
22
22
 
23
23
  const state = createState();
24
24
 
@@ -123,7 +123,7 @@ assert(completions.includes(`:load --no-alias ${libPath}`));
123
123
  output = await evaluateInput(`:ko ${koPath} succ`, state);
124
124
  assert.equal(output[0], `saved ${koPath} (succ)`);
125
125
  const objectFn = objectToFunction(decodeObject(fs.readFileSync(koPath)));
126
- const objectResult = objectFn(new Product({}, [["closed-product", []]]));
126
+ const objectResult = objectFn(Value.product({}, [["closed-product", []]]));
127
127
  assert.equal(objectResult.toJSON(), "succ");
128
128
 
129
129
  fs.writeFileSync(sourcePath, "$ nat = <{} zero, nat succ>;\nsucc = $nat |succ $nat;\ntwice = succ succ;\n");
@@ -145,7 +145,7 @@ assert.match(output[0], /^\$ bool = @/);
145
145
  const boolHash = codecState.typeAliases.bool;
146
146
  const valueModuleUrl = pathToFileURL(path.resolve("Value.mjs")).href;
147
147
  fs.writeFileSync(codecPath, `
148
- import { Product, Variant } from ${JSON.stringify(valueModuleUrl)};
148
+ import { Value } from ${JSON.stringify(valueModuleUrl)};
149
149
 
150
150
  export const name = "yn";
151
151
  export const codes = [${JSON.stringify(boolHash)}];
@@ -153,7 +153,7 @@ export const codes = [${JSON.stringify(boolHash)}];
153
153
  export function parse(text) {
154
154
  const tag = text.trim();
155
155
  if (tag !== "true" && tag !== "false") throw new Error("expected true or false");
156
- return new Variant(tag, new Product({}));
156
+ return Value.variant(tag, Value.product({}));
157
157
  }
158
158
 
159
159
  export function print(value) {
@@ -0,0 +1,27 @@
1
+ import assert from "node:assert/strict";
2
+
3
+ import { Value, isProduct, isVariant, isValue } from "../Value.mjs";
4
+ import { INT_PATTERN, print as printInt } from "../codecs/int.mjs";
5
+ import { print as printUnit } from "../codecs/unit.mjs";
6
+ import { valueToK } from "../codecs/runtime/show-value.mjs";
7
+ import { decodeWire, encodeToWire } from "../codecs/runtime/prefix-codec.mjs";
8
+
9
+ const foreign = await import("../Value.mjs?structural-values-regression");
10
+
11
+ assert.notEqual(foreign.Value, Value);
12
+
13
+ const foreignUnit = foreign.Value.product({});
14
+ const foreignTwo = foreign.Value.variant("+",
15
+ foreign.Value.variant("1",
16
+ foreign.Value.variant("0",
17
+ foreign.Value.variant("_", foreignUnit))));
18
+
19
+ assert.equal(isProduct(foreignUnit), true);
20
+ assert.equal(isVariant(foreignTwo), true);
21
+ assert.equal(isValue(foreignTwo), true);
22
+ assert.equal(printUnit(foreignUnit), "{}");
23
+ assert.equal(printInt(foreignTwo), "2");
24
+ assert.equal(valueToK(foreignTwo), "{}|_|0|1|+");
25
+
26
+ const decoded = decodeWire(encodeToWire(foreignTwo, INT_PATTERN)).value;
27
+ assert.equal(printInt(decoded), "2");
package/valueIO.mjs CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
  import { parse as parseValueText } from "./valueParser.mjs";
12
- import { Value, fromObject } from "./Value.mjs";
12
+ import { fromObject } from "./Value.mjs";
13
13
 
14
14
  /**
15
15
  * Parse a value from text with optional type information
package/valueParser.jison CHANGED
@@ -1,6 +1,6 @@
1
1
  %{
2
2
 
3
- import { Product, Variant } from "./Value.mjs";
3
+ import { Value } from "./Value.mjs";
4
4
 
5
5
  //--------------------------
6
6
 
@@ -87,19 +87,19 @@ input_with_eof
87
87
  exp
88
88
  : lc labelled rc { $$ = {value: $2.value, start: $1.start, end: $3.end}; }
89
89
  | lb exps rb { $$ = {value: $2.value, start: $1.start, end: $3.end}; }
90
- | str { $$ = {value: new Variant($1.value, new Product({})), start: $1.start, end: $1.end}; }
91
- | name { $$ = {value: new Variant($1.value, new Product({})), start: $1.start, end: $1.end}; }
90
+ | str { $$ = {value: Value.variant($1.value, Value.product({})), start: $1.start, end: $1.end}; }
91
+ | name { $$ = {value: Value.variant($1.value, Value.product({})), start: $1.start, end: $1.end}; }
92
92
  ;
93
93
 
94
94
  exps
95
- : /* empty */ { $$ = {value: new Product({})}; }
95
+ : /* empty */ { $$ = {value: Value.product({})}; }
96
96
  | non_empty_exps {
97
97
  if ($1.list.length === 1) {
98
- $$ = {value: new Variant('0', $1.list[0].value)};
98
+ $$ = {value: Value.variant('0', $1.list[0].value)};
99
99
  } else {
100
100
  const product = {};
101
101
  $1.list.forEach( (e, i) => product[i] = e.value );
102
- $$ = {value: new Product(product)};
102
+ $$ = {value: Value.product(product)};
103
103
  };
104
104
  }
105
105
  ;
@@ -111,10 +111,10 @@ non_empty_exps
111
111
  ;
112
112
 
113
113
  labelled
114
- : /* empty */ { $$ = {value: new Product({})}; }
114
+ : /* empty */ { $$ = {value: Value.product({})}; }
115
115
  | non_empty_labelled {
116
116
  if ($1.list.length === 1) {
117
- $$ = {value: new Variant($1.list[0].label, $1.list[0].value)};
117
+ $$ = {value: Value.variant($1.list[0].label, $1.list[0].value)};
118
118
  } else {
119
119
  const product = {};
120
120
  for (const {label, value} of $1.list) {
@@ -122,7 +122,7 @@ labelled
122
122
  anError(@0,`Duplicate label '${label}'.`);
123
123
  product[label] = value;
124
124
  }
125
- $$ = {value: new Product(product)};
125
+ $$ = {value: Value.product(product)};
126
126
  };
127
127
  }
128
128
  ;
package/valueParser.mjs CHANGED
@@ -75,7 +75,7 @@
75
75
 
76
76
 
77
77
 
78
- import { Product, Variant } from "./Value.mjs";
78
+ import { Value } from "./Value.mjs";
79
79
 
80
80
  //--------------------------
81
81
 
@@ -144,19 +144,19 @@ case 10: case 11:
144
144
  this.$ = {value: $$[$0-1].value, start: $$[$0-2].start, end: $$[$0].end};
145
145
  break;
146
146
  case 12: case 13:
147
- this.$ = {value: new Variant($$[$0].value, new Product({})), start: $$[$0].start, end: $$[$0].end};
147
+ this.$ = {value: Value.variant($$[$0].value, Value.product({})), start: $$[$0].start, end: $$[$0].end};
148
148
  break;
149
149
  case 14: case 18:
150
- this.$ = {value: new Product({})};
150
+ this.$ = {value: Value.product({})};
151
151
  break;
152
152
  case 15:
153
153
 
154
154
  if ($$[$0].list.length === 1) {
155
- this.$ = {value: new Variant('0', $$[$0].list[0].value)};
155
+ this.$ = {value: Value.variant('0', $$[$0].list[0].value)};
156
156
  } else {
157
157
  const product = {};
158
158
  $$[$0].list.forEach( (e, i) => product[i] = e.value );
159
- this.$ = {value: new Product(product)};
159
+ this.$ = {value: Value.product(product)};
160
160
  };
161
161
 
162
162
  break;
@@ -169,7 +169,7 @@ break;
169
169
  case 19:
170
170
 
171
171
  if ($$[$0].list.length === 1) {
172
- this.$ = {value: new Variant($$[$0].list[0].label, $$[$0].list[0].value)};
172
+ this.$ = {value: Value.variant($$[$0].list[0].label, $$[$0].list[0].value)};
173
173
  } else {
174
174
  const product = {};
175
175
  for (const {label, value} of $$[$0].list) {
@@ -177,7 +177,7 @@ case 19:
177
177
  anError(this._$,`Duplicate label '${label}'.`);
178
178
  product[label] = value;
179
179
  }
180
- this.$ = {value: new Product(product)};
180
+ this.$ = {value: Value.product(product)};
181
181
  };
182
182
 
183
183
  break;