@fraczak/k 6.2.4 → 6.3.2

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.
@@ -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;