@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.
- package/Value.mjs +50 -40
- package/a.klib +1 -1
- package/a.kvm +142676 -0
- package/a.wasm +0 -0
- package/backend-api.mjs +1 -1
- package/benchmark-results.html +532 -0
- package/benchmark-results.json +480 -0
- package/codecs/example-pipeline.mjs +7 -7
- package/codecs/ieee.mjs +8 -8
- package/codecs/int.mjs +6 -6
- package/codecs/json-codec.mjs +15 -15
- package/codecs/runtime/codec.mjs +13 -13
- package/codecs/runtime/prefix-codec.mjs +18 -18
- package/codecs/runtime/show-value.mjs +3 -3
- package/codecs/runtime/test-codec.mjs +7 -7
- package/codecs/runtime/typeFromValue.mjs +4 -4
- package/codecs/runtime/unicode-string.mjs +30 -30
- package/codecs/unit.mjs +4 -4
- package/index.mjs +2 -2
- package/kvm.mjs +86 -19
- package/package.json +4 -1
- package/repl.mjs +2 -2
- package/run.mjs +9 -9
- package/scripts/benchmark.mjs +393 -0
- package/scripts/git-hooks/pre-commit +10 -0
- package/scripts/render-benchmark.mjs +343 -0
- package/scripts/run-tests.mjs +2 -0
- package/tests/code-derivation/codec-witness-pattern.mjs +4 -4
- package/tests/code-derivation/pattern-envelope.mjs +12 -12
- package/tests/integration.sh +7 -0
- package/tests/test-arithmetics.mjs +95 -0
- package/tests/test-deep-wire-value.mjs +4 -4
- package/tests/test-ieee-arithmetic.mjs +3 -3
- package/tests/test-k-object.mjs +10 -10
- package/tests/test-kvm.mjs +1 -1
- package/tests/test-repl.mjs +4 -4
- package/tests/test-structural-values.mjs +27 -0
- package/valueIO.mjs +1 -1
- package/valueParser.jison +9 -9
- package/valueParser.mjs +7 -7
package/Value.mjs
CHANGED
|
@@ -16,9 +16,9 @@ function fromObject(obj) {
|
|
|
16
16
|
const fields = getFields(obj);
|
|
17
17
|
if (fields.length === 1) {
|
|
18
18
|
const field = fields[0];
|
|
19
|
-
return
|
|
19
|
+
return Value.variant(field, fromObject(obj[field]));
|
|
20
20
|
}
|
|
21
|
-
return
|
|
21
|
+
return Value.product(
|
|
22
22
|
fields.reduce( (result, field) => {
|
|
23
23
|
result[field] = fromObject(obj[field]);
|
|
24
24
|
return result;
|
|
@@ -26,17 +26,26 @@ function fromObject(obj) {
|
|
|
26
26
|
);
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
function isObject(value) {
|
|
30
|
+
return value !== null && typeof value === "object";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isProduct(value) {
|
|
34
|
+
return isObject(value) &&
|
|
35
|
+
value.type === "{}" &&
|
|
36
|
+
isObject(value.product) &&
|
|
37
|
+
!Array.isArray(value.product);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isVariant(value) {
|
|
41
|
+
return isObject(value) &&
|
|
42
|
+
value.type === "<>" &&
|
|
43
|
+
typeof value.tag === "string" &&
|
|
44
|
+
Object.hasOwn(value, "value");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isValue(value) {
|
|
48
|
+
return isProduct(value) || isVariant(value);
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
function toVector(m) {
|
|
@@ -147,46 +156,47 @@ function mergePatterns(declared, observed) {
|
|
|
147
156
|
function withPattern(value, pattern) {
|
|
148
157
|
if (!pattern) return value;
|
|
149
158
|
if (value.pattern === pattern) return value;
|
|
150
|
-
if (value
|
|
151
|
-
if (value
|
|
159
|
+
if (isProduct(value)) return Value.product(value.product, pattern);
|
|
160
|
+
if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);
|
|
152
161
|
return value;
|
|
153
162
|
}
|
|
154
163
|
|
|
155
|
-
class
|
|
156
|
-
constructor(
|
|
157
|
-
|
|
158
|
-
this.product = product;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
toString() {
|
|
162
|
-
return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(',')}}`;
|
|
164
|
+
class Value {
|
|
165
|
+
constructor(fields) {
|
|
166
|
+
Object.assign(this, fields);
|
|
163
167
|
}
|
|
164
168
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (vector.length > 0) return vector;
|
|
168
|
-
return this.product;
|
|
169
|
+
static product(product, pattern = null) {
|
|
170
|
+
return new Value({ type: "{}", pattern, product });
|
|
169
171
|
}
|
|
170
|
-
}
|
|
171
172
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
super("<>", pattern);
|
|
175
|
-
this.tag = tag;
|
|
176
|
-
this.value = value;
|
|
173
|
+
static variant(tag, value, pattern = null) {
|
|
174
|
+
return new Value({ type: "<>", pattern, tag, value });
|
|
177
175
|
}
|
|
178
176
|
|
|
179
177
|
toString() {
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
if (isVariant(this)) {
|
|
179
|
+
// return `{${JSON.stringify(this.tag)}:${this.value.toString()}}`;
|
|
180
|
+
return `${this.value.toString()}|${pLabel(this.tag)}`;
|
|
181
|
+
}
|
|
182
|
+
if (isProduct(this)) {
|
|
183
|
+
return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(',')}}`;
|
|
184
|
+
}
|
|
185
|
+
return String(this.type);
|
|
182
186
|
}
|
|
183
187
|
|
|
184
188
|
toJSON() {
|
|
185
|
-
if (
|
|
186
|
-
|
|
189
|
+
if (isVariant(this)) {
|
|
190
|
+
if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {
|
|
191
|
+
return this.tag;
|
|
192
|
+
}
|
|
193
|
+
return {[this.tag]: this.value.toJSON()};
|
|
187
194
|
}
|
|
188
|
-
return {
|
|
195
|
+
if (!isProduct(this)) return { type: this.type };
|
|
196
|
+
let vector = toVector(this.product);
|
|
197
|
+
if (vector.length > 0) return vector;
|
|
198
|
+
return this.product;
|
|
189
199
|
}
|
|
190
200
|
}
|
|
191
|
-
export { Value,
|
|
192
|
-
export default { Value,
|
|
201
|
+
export { Value, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern, isProduct, isVariant, isValue };
|
|
202
|
+
export default { Value, fromObject, cloneSubpattern, edgeSubpattern, composePattern, mergePatterns, withPattern, isProduct, isVariant, isValue };
|