@calcit/procs 0.13.1 → 0.13.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/.yarn/install-state.gz +0 -0
- package/editing-history/202608061714-struct-enum-data-model-v2.md +39 -0
- package/editing-history/202608061731-release-0.13.2.md +18 -0
- package/history/202608061433-required-record-field-access.md +32 -0
- package/lib/calcit-data.mjs +24 -24
- package/lib/calcit.procs.d.mts +43 -43
- package/lib/calcit.procs.mjs +165 -168
- package/lib/custom-formatter.mjs +23 -38
- package/lib/js-cirru.mjs +25 -25
- package/lib/js-enum-def.d.mts +11 -0
- package/lib/{js-enum.mjs → js-enum-def.mjs} +3 -3
- package/lib/{js-tuple.d.mts → js-enum-value.d.mts} +6 -7
- package/lib/{js-tuple.mjs → js-enum-value.mjs} +8 -15
- package/lib/js-list.mjs +6 -6
- package/lib/js-primes.d.mts +5 -5
- package/lib/js-primes.mjs +16 -16
- package/lib/{js-struct.d.mts → js-struct-def.d.mts} +2 -2
- package/lib/{js-struct.mjs → js-struct-def.mjs} +6 -6
- package/lib/{js-record.d.mts → js-struct-value.d.mts} +16 -15
- package/lib/{js-record.mjs → js-struct-value.mjs} +67 -59
- package/lib/package.json +1 -1
- package/package.json +1 -1
- package/ts-src/calcit-data.mts +24 -24
- package/ts-src/calcit.procs.mts +165 -168
- package/ts-src/custom-formatter.mts +28 -56
- package/ts-src/js-cirru.mts +26 -26
- package/ts-src/{js-enum.mts → js-enum-def.mts} +7 -7
- package/ts-src/{js-tuple.mts → js-enum-value.mts} +12 -19
- package/ts-src/js-list.mts +6 -6
- package/ts-src/js-primes.mts +16 -16
- package/ts-src/{js-struct.mts → js-struct-def.mts} +7 -7
- package/ts-src/{js-record.mts → js-struct-value.mts} +72 -66
- package/lib/js-enum.d.mts +0 -11
package/ts-src/calcit.procs.mts
CHANGED
|
@@ -22,22 +22,22 @@ import {
|
|
|
22
22
|
} from "./calcit-data.mjs";
|
|
23
23
|
|
|
24
24
|
import { CalcitRef, atom } from "./js-ref.mjs";
|
|
25
|
-
import {
|
|
25
|
+
import { CalcitStructValue } from "./js-struct-value.mjs";
|
|
26
26
|
import { CalcitImpl } from "./js-impl.mjs";
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
27
|
+
import { CalcitStructDef } from "./js-struct-def.mjs";
|
|
28
|
+
import { CalcitEnumDef } from "./js-enum-def.mjs";
|
|
29
29
|
import { CalcitTrait } from "./js-trait.mjs";
|
|
30
30
|
|
|
31
31
|
export * from "./calcit-data.mjs";
|
|
32
|
-
export * from "./js-
|
|
32
|
+
export * from "./js-struct-value.mjs";
|
|
33
33
|
export * from "./js-impl.mjs";
|
|
34
|
-
export * from "./js-struct.mjs";
|
|
35
|
-
export * from "./js-enum.mjs";
|
|
34
|
+
export * from "./js-struct-def.mjs";
|
|
35
|
+
export * from "./js-enum-def.mjs";
|
|
36
36
|
export * from "./js-map.mjs";
|
|
37
37
|
export * from "./js-list.mjs";
|
|
38
38
|
export * from "./js-set.mjs";
|
|
39
39
|
export * from "./js-primes.mjs";
|
|
40
|
-
export * from "./js-
|
|
40
|
+
export * from "./js-enum-value.mjs";
|
|
41
41
|
export * from "./js-trait.mjs";
|
|
42
42
|
export * from "./custom-formatter.mjs";
|
|
43
43
|
export * from "./js-cirru.mjs";
|
|
@@ -49,7 +49,7 @@ export { _$n_compare } from "./js-primes.mjs";
|
|
|
49
49
|
import { CalcitList, CalcitSliceList, foldl } from "./js-list.mjs";
|
|
50
50
|
import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
|
|
51
51
|
import { CalcitSet } from "./js-set.mjs";
|
|
52
|
-
import {
|
|
52
|
+
import { CalcitEnumValue } from "./js-enum-value.mjs";
|
|
53
53
|
import { to_calcit_data, extract_cirru_edn, extract_cirru_edn_for_typed, CalcitCirruQuote } from "./js-cirru.mjs";
|
|
54
54
|
import { TypedEdnSetView } from "./typed-edn.mjs";
|
|
55
55
|
|
|
@@ -77,8 +77,8 @@ export let type_of = (x: any): CalcitTag => {
|
|
|
77
77
|
if (x instanceof CalcitRef) {
|
|
78
78
|
return newTag("ref");
|
|
79
79
|
}
|
|
80
|
-
if (x instanceof
|
|
81
|
-
return newTag("
|
|
80
|
+
if (x instanceof CalcitEnumValue) {
|
|
81
|
+
return newTag("enum");
|
|
82
82
|
}
|
|
83
83
|
if (x instanceof CalcitSymbol) {
|
|
84
84
|
return newTag("symbol");
|
|
@@ -86,17 +86,17 @@ export let type_of = (x: any): CalcitTag => {
|
|
|
86
86
|
if (x instanceof CalcitSet) {
|
|
87
87
|
return newTag("set");
|
|
88
88
|
}
|
|
89
|
-
if (x instanceof
|
|
90
|
-
return newTag("
|
|
89
|
+
if (x instanceof CalcitStructValue) {
|
|
90
|
+
return newTag("struct");
|
|
91
91
|
}
|
|
92
92
|
if (x instanceof CalcitImpl) {
|
|
93
93
|
return newTag("impl");
|
|
94
94
|
}
|
|
95
|
-
if (x instanceof
|
|
96
|
-
return newTag("struct");
|
|
95
|
+
if (x instanceof CalcitStructDef) {
|
|
96
|
+
return newTag("struct-def");
|
|
97
97
|
}
|
|
98
|
-
if (x instanceof
|
|
99
|
-
return newTag("enum");
|
|
98
|
+
if (x instanceof CalcitEnumDef) {
|
|
99
|
+
return newTag("enum-def");
|
|
100
100
|
}
|
|
101
101
|
if (x instanceof CalcitTrait) {
|
|
102
102
|
return newTag("trait");
|
|
@@ -212,7 +212,7 @@ export let _$n_assert_traits = function (value: CalcitValue, traitDef: CalcitVal
|
|
|
212
212
|
}
|
|
213
213
|
const impls = pair[0];
|
|
214
214
|
const reverse =
|
|
215
|
-
value instanceof
|
|
215
|
+
value instanceof CalcitStructValue || value instanceof CalcitEnumValue || value instanceof CalcitStructDef || value instanceof CalcitEnumDef;
|
|
216
216
|
const ordered = reverse ? [...impls].reverse() : impls;
|
|
217
217
|
const selected = ordered.find((impl) => impl != null && impl.origin === traitDef);
|
|
218
218
|
if (selected == null) {
|
|
@@ -237,7 +237,7 @@ export let _$n_assert_traits = function (value: CalcitValue, traitDef: CalcitVal
|
|
|
237
237
|
return value;
|
|
238
238
|
};
|
|
239
239
|
|
|
240
|
-
export let defstruct = (name: CalcitValue, ...entries: CalcitValue[]):
|
|
240
|
+
export let defstruct = (name: CalcitValue, ...entries: CalcitValue[]): CalcitStructDef => {
|
|
241
241
|
const structName = castTag(name);
|
|
242
242
|
const fields: Array<{ tag: CalcitTag; type: CalcitValue }> = [];
|
|
243
243
|
const fieldEntries = trimDataDefinitionEntries(entries);
|
|
@@ -261,10 +261,10 @@ export let defstruct = (name: CalcitValue, ...entries: CalcitValue[]): CalcitStr
|
|
|
261
261
|
|
|
262
262
|
const fieldTags = fields.map((entry) => entry.tag);
|
|
263
263
|
const fieldTypes = fields.map((entry) => entry.type);
|
|
264
|
-
return new
|
|
264
|
+
return new CalcitStructDef(structName, fieldTags, fieldTypes, null);
|
|
265
265
|
};
|
|
266
266
|
|
|
267
|
-
export let defenum = (name: CalcitValue, ...variants: CalcitValue[]):
|
|
267
|
+
export let defenum = (name: CalcitValue, ...variants: CalcitValue[]): CalcitEnumDef => {
|
|
268
268
|
const enumName = castTag(name);
|
|
269
269
|
const entries: Array<{ tag: CalcitTag; payload: CalcitSliceList }> = [];
|
|
270
270
|
const variantEntries = trimDataDefinitionEntries(variants);
|
|
@@ -288,8 +288,8 @@ export let defenum = (name: CalcitValue, ...variants: CalcitValue[]): CalcitEnum
|
|
|
288
288
|
|
|
289
289
|
const tags = entries.map((entry) => entry.tag);
|
|
290
290
|
const values = entries.map((entry) => entry.payload);
|
|
291
|
-
const prototype = new
|
|
292
|
-
return new
|
|
291
|
+
const prototype = new CalcitStructValue(enumName, tags, values, null);
|
|
292
|
+
return new CalcitEnumDef(prototype);
|
|
293
293
|
};
|
|
294
294
|
|
|
295
295
|
export let _$n_impl_$o__$o_new = (name: CalcitValue, ...pairs: CalcitValue[]): CalcitImpl => {
|
|
@@ -301,14 +301,14 @@ export let _$n_impl_$o__$o_new = (name: CalcitValue, ...pairs: CalcitValue[]): C
|
|
|
301
301
|
if (pairs.length === 1 && pairs[0] instanceof CalcitImpl) {
|
|
302
302
|
const sourceImpl = pairs[0];
|
|
303
303
|
sourcePairs = sourceImpl.fields.map(
|
|
304
|
-
(field, idx) => new
|
|
304
|
+
(field, idx) => new CalcitEnumValue(newTag(field.value), [sourceImpl.values[idx]], null)
|
|
305
305
|
);
|
|
306
306
|
}
|
|
307
307
|
for (let idx = 0; idx < sourcePairs.length; idx++) {
|
|
308
308
|
const pairValue = sourcePairs[idx];
|
|
309
309
|
let fieldTag: CalcitTag;
|
|
310
310
|
let value: CalcitValue;
|
|
311
|
-
if (pairValue instanceof
|
|
311
|
+
if (pairValue instanceof CalcitEnumValue) {
|
|
312
312
|
if (pairValue.extra.length !== 1) {
|
|
313
313
|
throw new Error(`&impl::new expects (field value) pairs, got: ${toString(pairValue, true)}`);
|
|
314
314
|
}
|
|
@@ -350,11 +350,11 @@ export let _$n_impl_$o__$o_new = (name: CalcitValue, ...pairs: CalcitValue[]): C
|
|
|
350
350
|
return new CalcitImpl(implName, fields, values, origin);
|
|
351
351
|
};
|
|
352
352
|
|
|
353
|
-
export let _$
|
|
353
|
+
export let _$n_struct_def_$o_new = (name: CalcitValue, ...entries: CalcitValue[]): CalcitStructDef => {
|
|
354
354
|
return defstruct(name, ...entries);
|
|
355
355
|
};
|
|
356
356
|
|
|
357
|
-
export let _$
|
|
357
|
+
export let _$n_enum_def_$o_new = (name: CalcitValue, ...variants: CalcitValue[]): CalcitEnumDef => {
|
|
358
358
|
return defenum(name, ...variants);
|
|
359
359
|
};
|
|
360
360
|
|
|
@@ -378,23 +378,23 @@ export function _$n_map_$o_count(x: CalcitValue): number {
|
|
|
378
378
|
|
|
379
379
|
throw new Error(`expected a map ${x}`);
|
|
380
380
|
}
|
|
381
|
-
export function _$
|
|
382
|
-
if (x instanceof
|
|
381
|
+
export function _$n_struct_$o_count(x: CalcitValue): number {
|
|
382
|
+
if (x instanceof CalcitStructValue) return x.fields.length;
|
|
383
383
|
|
|
384
|
-
throw new Error(`expected a
|
|
384
|
+
throw new Error(`expected a struct value ${x}`);
|
|
385
385
|
}
|
|
386
386
|
|
|
387
|
-
export function _$
|
|
388
|
-
if (!(x instanceof
|
|
387
|
+
export function _$n_struct_$o_field_tag(x: CalcitValue, idx: CalcitValue): CalcitTag {
|
|
388
|
+
if (!(x instanceof CalcitStructValue)) throw new Error(`&struct:field-tag expected a struct value, got ${x}`);
|
|
389
389
|
const i = idx as number;
|
|
390
|
-
if (i < 0 || i >= x.fields.length) throw new Error(`&
|
|
390
|
+
if (i < 0 || i >= x.fields.length) throw new Error(`&struct:field-tag index ${i} out of bounds (${x.fields.length})`);
|
|
391
391
|
return x.fields[i];
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
export function _$
|
|
395
|
-
if (!(x instanceof
|
|
394
|
+
export function _$n_struct_$o_nth(x: CalcitValue, idx: CalcitValue): CalcitValue {
|
|
395
|
+
if (!(x instanceof CalcitStructValue)) throw new Error(`&struct:nth expected a struct value, got ${x}`);
|
|
396
396
|
const i = idx as number;
|
|
397
|
-
if (i < 0 || i >= x.values.length) throw new Error(`&
|
|
397
|
+
if (i < 0 || i >= x.values.length) throw new Error(`&struct:nth index ${i} out of range for struct with ${x.values.length} fields`);
|
|
398
398
|
return x.values[i];
|
|
399
399
|
}
|
|
400
400
|
|
|
@@ -494,10 +494,10 @@ export let _$n_map_$o_contains_$q_ = (xs: CalcitValue, x: CalcitValue): boolean
|
|
|
494
494
|
throw new Error("map `contains?` expected a map");
|
|
495
495
|
};
|
|
496
496
|
|
|
497
|
-
export let _$
|
|
498
|
-
if (xs instanceof
|
|
497
|
+
export let _$n_struct_$o_contains_$q_ = (xs: CalcitValue, x: CalcitValue): boolean => {
|
|
498
|
+
if (xs instanceof CalcitStructValue) return xs.contains(x);
|
|
499
499
|
|
|
500
|
-
throw new Error("
|
|
500
|
+
throw new Error("struct `contains?` expected a struct value");
|
|
501
501
|
};
|
|
502
502
|
|
|
503
503
|
export let _$n_str_$o_includes_$q_ = (xs: CalcitValue, x: CalcitValue): boolean => {
|
|
@@ -566,18 +566,18 @@ export let _$n_list_$o_nth = function (xs: CalcitValue, k: CalcitValue) {
|
|
|
566
566
|
throw new Error("Does not support `nth` on this type");
|
|
567
567
|
};
|
|
568
568
|
|
|
569
|
-
export let _$
|
|
569
|
+
export let _$n_enum_$o_nth = function (xs: CalcitValue, k: CalcitValue) {
|
|
570
570
|
if (arguments.length !== 2) throw new Error("nth takes 2 arguments");
|
|
571
571
|
if (typeof k !== "number") throw new Error("Expected number index for a list");
|
|
572
572
|
|
|
573
|
-
if (xs instanceof
|
|
573
|
+
if (xs instanceof CalcitEnumValue) return xs.get(k);
|
|
574
574
|
|
|
575
575
|
throw new Error("Does not support `nth` on this type");
|
|
576
576
|
};
|
|
577
|
-
export let _$
|
|
578
|
-
if (arguments.length !== 1) throw new Error("&
|
|
577
|
+
export let _$n_enum_$o_count = function (xs: CalcitValue) {
|
|
578
|
+
if (arguments.length !== 1) throw new Error("&enum:count takes 1 arguments");
|
|
579
579
|
|
|
580
|
-
if (xs instanceof
|
|
580
|
+
if (xs instanceof CalcitEnumValue) return xs.count();
|
|
581
581
|
|
|
582
582
|
throw new Error("Does not support `count` on this type");
|
|
583
583
|
};
|
|
@@ -589,65 +589,62 @@ const coerce_impl = (value: CalcitValue, procName: string): CalcitImpl => {
|
|
|
589
589
|
throw new Error(`${procName} expects trait impls as impls`);
|
|
590
590
|
};
|
|
591
591
|
|
|
592
|
-
export let _$
|
|
593
|
-
if (arguments.length !== 1) throw new Error("&
|
|
592
|
+
export let _$n_enum_$o_impls = function (x: CalcitEnumValue) {
|
|
593
|
+
if (arguments.length !== 1) throw new Error("&enum:impls takes 1 argument");
|
|
594
594
|
return new CalcitSliceList(x.impls);
|
|
595
595
|
};
|
|
596
596
|
|
|
597
|
-
export let _$
|
|
598
|
-
if (arguments.length !== 1) throw new Error("&
|
|
597
|
+
export let _$n_enum_$o_params = function (x: CalcitEnumValue) {
|
|
598
|
+
if (arguments.length !== 1) throw new Error("&enum:params takes 1 argument");
|
|
599
599
|
return new CalcitSliceList(x.extra);
|
|
600
600
|
};
|
|
601
601
|
|
|
602
|
-
export let _$
|
|
603
|
-
if (arguments.length !== 2) throw new Error("&
|
|
604
|
-
if (!(x instanceof
|
|
605
|
-
const impl = coerce_impl(y, "&
|
|
602
|
+
export let _$n_enum_$o_with_impls = function (x: CalcitEnumValue, y: CalcitValue) {
|
|
603
|
+
if (arguments.length !== 2) throw new Error("&enum:with-impls takes 2 arguments");
|
|
604
|
+
if (!(x instanceof CalcitEnumValue)) throw new Error("&enum:with-impls expects an enum value");
|
|
605
|
+
const impl = coerce_impl(y, "&enum:with-impls");
|
|
606
606
|
let proto = x.enumPrototype;
|
|
607
607
|
if (proto == null) {
|
|
608
|
-
proto = new
|
|
608
|
+
proto = new CalcitEnumDef(new CalcitStructValue(newTag("_"), [], [], new CalcitStructDef(newTag("_"), [], [])));
|
|
609
609
|
}
|
|
610
|
-
return new
|
|
610
|
+
return new CalcitEnumValue(x.tag, x.extra, proto.withImpls(impl));
|
|
611
611
|
};
|
|
612
612
|
|
|
613
|
-
export let _$
|
|
614
|
-
if (traits.length < 1) throw new Error("&
|
|
615
|
-
if (!(x instanceof
|
|
616
|
-
const impls = traits.map((trait) => coerce_impl(trait, "&
|
|
613
|
+
export let _$n_enum_$o_impl_traits = function (x: CalcitValue, ...traits: CalcitValue[]) {
|
|
614
|
+
if (traits.length < 1) throw new Error("&enum:impl-traits takes 2+ arguments");
|
|
615
|
+
if (!(x instanceof CalcitEnumValue)) throw new Error("&enum:impl-traits expects an enum value");
|
|
616
|
+
const impls = traits.map((trait) => coerce_impl(trait, "&enum:impl-traits"));
|
|
617
617
|
let proto = x.enumPrototype;
|
|
618
618
|
if (proto == null) {
|
|
619
619
|
const tagName = x.tag instanceof CalcitTag ? x.tag : newTag("tag");
|
|
620
620
|
const anyTypes = new CalcitSliceList(new Array(x.extra.length).fill(newTag("any")));
|
|
621
|
-
proto = new
|
|
622
|
-
new
|
|
621
|
+
proto = new CalcitEnumDef(
|
|
622
|
+
new CalcitStructValue(newTag("_"), [tagName], [anyTypes], new CalcitStructDef(newTag("_"), [tagName], [anyTypes]))
|
|
623
623
|
);
|
|
624
624
|
}
|
|
625
|
-
return new
|
|
625
|
+
return new CalcitEnumValue(x.tag, x.extra, proto.withImpls(impls));
|
|
626
626
|
};
|
|
627
627
|
|
|
628
|
-
export let _$
|
|
629
|
-
if (arguments.length !== 1) throw new Error("&
|
|
630
|
-
if (!(x instanceof
|
|
628
|
+
export let _$n_enum_$o_definition = function (x: CalcitEnumValue) {
|
|
629
|
+
if (arguments.length !== 1) throw new Error("&enum:definition takes 1 argument");
|
|
630
|
+
if (!(x instanceof CalcitEnumValue)) throw new Error("&enum:definition expects an enum value");
|
|
631
631
|
if (x.enumPrototype == null) {
|
|
632
632
|
return null;
|
|
633
633
|
}
|
|
634
|
-
|
|
635
|
-
return x.enumPrototype;
|
|
636
|
-
}
|
|
637
|
-
return new CalcitEnum(x.enumPrototype as CalcitRecord);
|
|
634
|
+
return x.enumPrototype;
|
|
638
635
|
};
|
|
639
636
|
|
|
640
|
-
const unwrap_enum_prototype = (enumPrototype: CalcitValue, procName: string):
|
|
641
|
-
if (enumPrototype instanceof
|
|
637
|
+
const unwrap_enum_prototype = (enumPrototype: CalcitValue, procName: string): CalcitStructValue => {
|
|
638
|
+
if (enumPrototype instanceof CalcitEnumDef) {
|
|
642
639
|
return enumPrototype.prototype;
|
|
643
640
|
}
|
|
644
|
-
if (enumPrototype instanceof
|
|
645
|
-
|
|
641
|
+
if (enumPrototype instanceof CalcitStructValue) {
|
|
642
|
+
throw new Error(`${procName} no longer accepts a struct prototype; pass an EnumDef produced by defenum`);
|
|
646
643
|
}
|
|
647
|
-
throw new Error(`${procName} expects
|
|
644
|
+
throw new Error(`${procName} expects an EnumDef as first argument`);
|
|
648
645
|
};
|
|
649
646
|
|
|
650
|
-
const assert_enum_tag_args = (procName: string, enumPrototype: CalcitValue, variantTag: CalcitTag):
|
|
647
|
+
const assert_enum_tag_args = (procName: string, enumPrototype: CalcitValue, variantTag: CalcitTag): CalcitStructValue => {
|
|
651
648
|
const proto = unwrap_enum_prototype(enumPrototype, procName);
|
|
652
649
|
if (!(variantTag instanceof CalcitTag)) {
|
|
653
650
|
throw new Error(`${procName} expects tag as second argument`);
|
|
@@ -655,17 +652,17 @@ const assert_enum_tag_args = (procName: string, enumPrototype: CalcitValue, vari
|
|
|
655
652
|
return proto;
|
|
656
653
|
};
|
|
657
654
|
|
|
658
|
-
export let _$
|
|
659
|
-
if (arguments.length !== 2) throw new Error("&
|
|
660
|
-
const proto = assert_enum_tag_args("&
|
|
655
|
+
export let _$n_enum_def_$o_has_variant_$q_ = function (enumPrototype: CalcitValue, variantTag: CalcitTag) {
|
|
656
|
+
if (arguments.length !== 2) throw new Error("&enum-def:has-variant? takes 2 arguments");
|
|
657
|
+
const proto = assert_enum_tag_args("&enum-def:has-variant?", enumPrototype, variantTag);
|
|
661
658
|
return proto.contains(variantTag);
|
|
662
659
|
};
|
|
663
660
|
|
|
664
|
-
export let _$
|
|
661
|
+
export let _$n_enum_def_$o_has_variant = _$n_enum_def_$o_has_variant_$q_;
|
|
665
662
|
|
|
666
|
-
export let _$
|
|
667
|
-
if (arguments.length !== 2) throw new Error("&
|
|
668
|
-
const proto = assert_enum_tag_args("&
|
|
663
|
+
export let _$n_enum_def_$o_variant_arity = function (enumPrototype: CalcitValue, variantTag: CalcitTag) {
|
|
664
|
+
if (arguments.length !== 2) throw new Error("&enum-def:variant-arity takes 2 arguments");
|
|
665
|
+
const proto = assert_enum_tag_args("&enum-def:variant-arity", enumPrototype, variantTag);
|
|
669
666
|
|
|
670
667
|
const variant = proto.getOrNil(variantTag);
|
|
671
668
|
if (variant === undefined) {
|
|
@@ -678,26 +675,26 @@ export let _$n_tuple_$o_enum_variant_arity = function (enumPrototype: CalcitValu
|
|
|
678
675
|
throw new Error("Expected variant to be a list");
|
|
679
676
|
};
|
|
680
677
|
|
|
681
|
-
export let _$
|
|
682
|
-
if (arguments.length !== 2) throw new Error("&
|
|
683
|
-
if (!(
|
|
684
|
-
if (
|
|
678
|
+
export let _$n_enum_$o_validate = function (enumValue: CalcitValue, tag: CalcitValue): CalcitValue {
|
|
679
|
+
if (arguments.length !== 2) throw new Error("&enum:validate takes 2 arguments");
|
|
680
|
+
if (!(enumValue instanceof CalcitEnumValue)) throw new Error("&enum:validate expects an enum value as first argument");
|
|
681
|
+
if (enumValue.enumPrototype == null) {
|
|
685
682
|
return null;
|
|
686
683
|
}
|
|
687
684
|
|
|
688
|
-
const proto = assert_enum_tag_args("&
|
|
685
|
+
const proto = assert_enum_tag_args("&enum:validate", enumValue.enumPrototype as CalcitValue, tag as CalcitTag);
|
|
689
686
|
|
|
690
687
|
const tagValue = tag as CalcitTag;
|
|
691
688
|
const variant = proto.getOrNil(tagValue);
|
|
692
689
|
if (variant === undefined) {
|
|
693
|
-
throw new Error(`enum does not have variant ${tagValue.value} for ${
|
|
690
|
+
throw new Error(`enum does not have variant ${tagValue.value} for ${enumValue}`);
|
|
694
691
|
}
|
|
695
692
|
|
|
696
693
|
if (variant instanceof CalcitSliceList) {
|
|
697
694
|
const expected = variant.len();
|
|
698
|
-
const actual =
|
|
695
|
+
const actual = enumValue.extra.length;
|
|
699
696
|
if (expected !== actual) {
|
|
700
|
-
throw new Error(`enum variant expects ${expected} payload(s), got ${actual} for ${
|
|
697
|
+
throw new Error(`enum variant expects ${expected} payload(s), got ${actual} for ${enumValue}`);
|
|
701
698
|
}
|
|
702
699
|
return null;
|
|
703
700
|
}
|
|
@@ -705,12 +702,12 @@ export let _$n_tuple_$o_validate_enum = function (tuple: CalcitValue, tag: Calci
|
|
|
705
702
|
throw new Error("Expected variant to be a list");
|
|
706
703
|
};
|
|
707
704
|
|
|
708
|
-
export let _$
|
|
705
|
+
export let _$n_struct_$o_get = function (xs: CalcitValue, k: CalcitTag) {
|
|
709
706
|
if (arguments.length !== 2) {
|
|
710
|
-
throw new Error("
|
|
707
|
+
throw new Error("&struct:get takes 2 arguments");
|
|
711
708
|
}
|
|
712
709
|
|
|
713
|
-
if (xs instanceof
|
|
710
|
+
if (xs instanceof CalcitStructValue) return xs.getRequired(k);
|
|
714
711
|
|
|
715
712
|
throw new Error("Does not support `&get` on this type");
|
|
716
713
|
};
|
|
@@ -726,17 +723,17 @@ export let _$n_list_$o_assoc = function (xs: CalcitValue, k: CalcitValue, v: Cal
|
|
|
726
723
|
}
|
|
727
724
|
throw new Error("list `assoc` expected a list");
|
|
728
725
|
};
|
|
729
|
-
export let _$
|
|
726
|
+
export let _$n_enum_$o_assoc = function (xs: CalcitValue, k: CalcitValue, v: CalcitValue) {
|
|
730
727
|
if (arguments.length !== 3) throw new Error("assoc takes 3 arguments");
|
|
731
728
|
|
|
732
|
-
if (xs instanceof
|
|
729
|
+
if (xs instanceof CalcitEnumValue) {
|
|
733
730
|
if (typeof k !== "number") {
|
|
734
731
|
throw new Error("Expected number index for lists");
|
|
735
732
|
}
|
|
736
733
|
return xs.assoc(k, v);
|
|
737
734
|
}
|
|
738
735
|
|
|
739
|
-
throw new Error("
|
|
736
|
+
throw new Error("enum `assoc` expected an enum value");
|
|
740
737
|
};
|
|
741
738
|
export let _$n_map_$o_assoc = function (xs: CalcitValue, ...args: CalcitValue[]) {
|
|
742
739
|
if (arguments.length < 3) throw new Error("assoc takes at least 3 arguments");
|
|
@@ -746,47 +743,43 @@ export let _$n_map_$o_assoc = function (xs: CalcitValue, ...args: CalcitValue[])
|
|
|
746
743
|
|
|
747
744
|
throw new Error("map `assoc` expected a map");
|
|
748
745
|
};
|
|
749
|
-
export let _$
|
|
746
|
+
export let _$n_struct_$o_assoc = function (xs: CalcitValue, k: CalcitValue, v: CalcitValue) {
|
|
750
747
|
if (arguments.length !== 3) throw new Error("assoc takes 3 arguments");
|
|
751
748
|
|
|
752
|
-
if (xs instanceof
|
|
753
|
-
|
|
754
|
-
throw new Error("record `assoc` expected a record");
|
|
755
|
-
};
|
|
749
|
+
if (xs instanceof CalcitStructValue) return xs.assoc(k, v);
|
|
756
750
|
|
|
757
|
-
|
|
758
|
-
if (arguments.length !== 1) throw new Error("&record:impls takes 1 argument");
|
|
759
|
-
if (xs instanceof CalcitRecord) return new CalcitSliceList(xs.structRef.impls);
|
|
760
|
-
throw new Error("&record:impls expected a record");
|
|
751
|
+
throw new Error("struct `assoc` expected a struct value");
|
|
761
752
|
};
|
|
762
753
|
|
|
763
|
-
export let _$
|
|
764
|
-
if (
|
|
765
|
-
if (
|
|
766
|
-
|
|
767
|
-
const nextStruct = new CalcitStruct(xs.name, xs.fields, xs.structRef.fieldTypes, xs.structRef.impls.concat(impls));
|
|
768
|
-
return new CalcitRecord(xs.name, xs.fields, xs.values, nextStruct);
|
|
754
|
+
export let _$n_struct_$o_impls = function (xs: CalcitValue) {
|
|
755
|
+
if (arguments.length !== 1) throw new Error("&struct:impls takes 1 argument");
|
|
756
|
+
if (xs instanceof CalcitStructValue) return new CalcitSliceList(xs.structRef.impls);
|
|
757
|
+
throw new Error("&struct:impls expected a struct value");
|
|
769
758
|
};
|
|
770
759
|
|
|
771
760
|
export let _$n_struct_$o_impl_traits = function (xs: CalcitValue, ...traits: CalcitValue[]) {
|
|
772
761
|
if (traits.length < 1) throw new Error("&struct:impl-traits takes 2+ arguments");
|
|
773
|
-
if (!(xs instanceof
|
|
774
|
-
const
|
|
762
|
+
if (!(xs instanceof CalcitStructValue)) throw new Error("&struct:impl-traits expected a struct value");
|
|
763
|
+
const impls = traits.map((trait) => coerce_impl(trait, "&struct:impl-traits"));
|
|
764
|
+
const nextStruct = new CalcitStructDef(xs.name, xs.fields, xs.structRef.fieldTypes, xs.structRef.impls.concat(impls));
|
|
765
|
+
return new CalcitStructValue(xs.name, xs.fields, xs.values, nextStruct);
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
export let _$n_struct_def_$o_impl_traits = function (xs: CalcitValue, ...traits: CalcitValue[]) {
|
|
769
|
+
if (traits.length < 1) throw new Error("&struct-def:impl-traits takes 2+ arguments");
|
|
770
|
+
if (!(xs instanceof CalcitStructDef)) throw new Error("&struct-def:impl-traits expected a struct definition");
|
|
771
|
+
const addedImpls = traits.map((trait) => coerce_impl(trait, "&struct-def:impl-traits"));
|
|
775
772
|
const baseImpls = xs.impls ?? [];
|
|
776
|
-
return new
|
|
773
|
+
return new CalcitStructDef(xs.name, xs.fields, xs.fieldTypes, baseImpls.concat(addedImpls));
|
|
777
774
|
};
|
|
778
775
|
|
|
779
|
-
export let _$
|
|
780
|
-
if (traits.length < 1) throw new Error("&enum:impl-traits takes 2+ arguments");
|
|
781
|
-
const addedImpls = traits.map((trait) => coerce_impl(trait, "&enum:impl-traits"));
|
|
782
|
-
if (xs instanceof
|
|
776
|
+
export let _$n_enum_def_$o_impl_traits = function (xs: CalcitValue, ...traits: CalcitValue[]) {
|
|
777
|
+
if (traits.length < 1) throw new Error("&enum-def:impl-traits takes 2+ arguments");
|
|
778
|
+
const addedImpls = traits.map((trait) => coerce_impl(trait, "&enum-def:impl-traits"));
|
|
779
|
+
if (xs instanceof CalcitEnumDef) {
|
|
783
780
|
return xs.withImpls(addedImpls);
|
|
784
781
|
}
|
|
785
|
-
|
|
786
|
-
const nextStruct = new CalcitStruct(xs.name, xs.fields, xs.structRef.fieldTypes, xs.structRef.impls.concat(addedImpls));
|
|
787
|
-
return new CalcitRecord(xs.name, xs.fields, xs.values, nextStruct);
|
|
788
|
-
}
|
|
789
|
-
throw new Error("&enum:impl-traits expected an enum or enum record");
|
|
782
|
+
throw new Error("&enum-def:impl-traits expected an enum definition");
|
|
790
783
|
};
|
|
791
784
|
|
|
792
785
|
export let _$n_impl_$o_origin = function (impl: CalcitValue): CalcitValue {
|
|
@@ -1194,7 +1187,7 @@ export let _$n_merge = (a: CalcitValue, b: CalcitMap | CalcitSliceMap): CalcitVa
|
|
|
1194
1187
|
throw new Error("Expected an argument of map");
|
|
1195
1188
|
}
|
|
1196
1189
|
}
|
|
1197
|
-
if (a instanceof
|
|
1190
|
+
if (a instanceof CalcitStructValue) {
|
|
1198
1191
|
if (b instanceof CalcitMap || b instanceof CalcitSliceMap) {
|
|
1199
1192
|
let values = [];
|
|
1200
1193
|
for (let idx = 0; idx < a.values.length; idx++) {
|
|
@@ -1217,10 +1210,10 @@ export let _$n_merge = (a: CalcitValue, b: CalcitMap | CalcitSliceMap): CalcitVa
|
|
|
1217
1210
|
throw new Error(`Cannot find field ${field} among (${a.fields.join(", ")})`);
|
|
1218
1211
|
}
|
|
1219
1212
|
}
|
|
1220
|
-
return new
|
|
1213
|
+
return new CalcitStructValue(a.name, a.fields, values);
|
|
1221
1214
|
}
|
|
1222
1215
|
}
|
|
1223
|
-
throw new Error("Expected map or
|
|
1216
|
+
throw new Error("Expected map or struct value");
|
|
1224
1217
|
};
|
|
1225
1218
|
|
|
1226
1219
|
export let _$n_merge_non_nil = (a: CalcitMap | CalcitSliceMap, b: CalcitMap | CalcitSliceMap): CalcitMap | CalcitSliceMap => {
|
|
@@ -1248,7 +1241,7 @@ export let to_pairs = (xs: CalcitValue): CalcitValue | CalcitSliceList => {
|
|
|
1248
1241
|
result.push(new CalcitSliceList(pairs[idx]));
|
|
1249
1242
|
}
|
|
1250
1243
|
return new CalcitSet(result);
|
|
1251
|
-
} else if (xs instanceof
|
|
1244
|
+
} else if (xs instanceof CalcitStructValue) {
|
|
1252
1245
|
let arr_result: Array<CalcitSliceList> = [];
|
|
1253
1246
|
for (let idx = 0; idx < xs.fields.length; idx++) {
|
|
1254
1247
|
arr_result.push(new CalcitSliceList([xs.fields[idx], xs.values[idx]]));
|
|
@@ -1626,11 +1619,17 @@ export let fn_$q_ = (x: CalcitValue): boolean => {
|
|
|
1626
1619
|
export let ref_$q_ = (x: CalcitValue): boolean => {
|
|
1627
1620
|
return x instanceof CalcitRef;
|
|
1628
1621
|
};
|
|
1629
|
-
export let
|
|
1630
|
-
|
|
1622
|
+
export let struct_$q_ = (x: CalcitValue): boolean => {
|
|
1623
|
+
if (x instanceof CalcitStructDef) {
|
|
1624
|
+
throw new Error("`struct?` now checks struct values; use `struct-def?` for a value produced by `defstruct`");
|
|
1625
|
+
}
|
|
1626
|
+
return x instanceof CalcitStructValue;
|
|
1631
1627
|
};
|
|
1632
|
-
export let
|
|
1633
|
-
|
|
1628
|
+
export let enum_$q_ = (x: CalcitValue): boolean => {
|
|
1629
|
+
if (x instanceof CalcitEnumDef) {
|
|
1630
|
+
throw new Error("`enum?` now checks enum values; use `enum-def?` for a value produced by `defenum`");
|
|
1631
|
+
}
|
|
1632
|
+
return x instanceof CalcitEnumValue;
|
|
1634
1633
|
};
|
|
1635
1634
|
export let buffer_$q_ = (x: CalcitValue): boolean => {
|
|
1636
1635
|
console.warn("TODO, detecting buffer");
|
|
@@ -1680,8 +1679,8 @@ type DataShapeNode =
|
|
|
1680
1679
|
| { kind: "unit" | "bool" | "number" | "string" | "symbol" | "tag" | "buffer" | "cirru-quote" }
|
|
1681
1680
|
| { kind: "optional" | "list" | "set" | "ref"; inner: number }
|
|
1682
1681
|
| { kind: "map"; key: number; value: number }
|
|
1683
|
-
| { kind: "struct"; nominal:
|
|
1684
|
-
| { kind: "enum"; nominal:
|
|
1682
|
+
| { kind: "struct"; nominal: CalcitStructDef; fields: Array<[string, number]> }
|
|
1683
|
+
| { kind: "enum"; nominal: CalcitEnumDef; variants: Array<{ tag: string; payload: number[] }> };
|
|
1685
1684
|
|
|
1686
1685
|
type DataShapeGraph = {
|
|
1687
1686
|
version: number;
|
|
@@ -1700,8 +1699,8 @@ const typed_edn_kind = (value: any): string => {
|
|
|
1700
1699
|
if (value instanceof CalcitList || value instanceof CalcitSliceList) return "list";
|
|
1701
1700
|
if (value instanceof CalcitMap || value instanceof CalcitSliceMap) return "map";
|
|
1702
1701
|
if (value instanceof CalcitSet || value instanceof TypedEdnSetView) return "set";
|
|
1703
|
-
if (value instanceof
|
|
1704
|
-
if (value instanceof
|
|
1702
|
+
if (value instanceof CalcitStructValue) return "struct";
|
|
1703
|
+
if (value instanceof CalcitEnumValue) return "enum";
|
|
1705
1704
|
if (value instanceof CalcitRef) return "atom";
|
|
1706
1705
|
if (value instanceof CalcitCirruQuote) return "cirru-quote";
|
|
1707
1706
|
if (value instanceof Uint8Array) return "buffer";
|
|
@@ -1712,8 +1711,8 @@ const typed_edn_error = (path: string, message: string): never => {
|
|
|
1712
1711
|
throw new Error(`parse-cirru-edn-as failed at ${path}: ${message}`);
|
|
1713
1712
|
};
|
|
1714
1713
|
|
|
1715
|
-
const enum_prototype_name = (value:
|
|
1716
|
-
return value instanceof
|
|
1714
|
+
const enum_prototype_name = (value: CalcitEnumDef | CalcitStructValue): string => {
|
|
1715
|
+
return value instanceof CalcitEnumDef ? value.name() : value.name.value;
|
|
1717
1716
|
};
|
|
1718
1717
|
|
|
1719
1718
|
const decode_typed_edn_node = (graph: DataShapeGraph, nodeId: number, input: any, path: string, depth: number): any => {
|
|
@@ -1795,11 +1794,11 @@ const decode_typed_edn_node = (graph: DataShapeGraph, nodeId: number, input: any
|
|
|
1795
1794
|
if (!(input instanceof CalcitRef)) return typed_edn_error(path, `expected atom, got ${typed_edn_kind(input)}`);
|
|
1796
1795
|
return atom(decode_typed_edn_node(graph, node.inner, input.value, `${path}.value`, depth + 1));
|
|
1797
1796
|
case "struct": {
|
|
1798
|
-
if (!(input instanceof
|
|
1799
|
-
return typed_edn_error(path, `expected
|
|
1797
|
+
if (!(input instanceof CalcitStructValue)) {
|
|
1798
|
+
return typed_edn_error(path, `expected struct :${node.nominal.name.value}, got ${typed_edn_kind(input)}`);
|
|
1800
1799
|
}
|
|
1801
1800
|
if (input.name.value !== node.nominal.name.value) {
|
|
1802
|
-
return typed_edn_error(path, `expected
|
|
1801
|
+
return typed_edn_error(path, `expected struct :${node.nominal.name.value}, got struct :${input.name.value}`);
|
|
1803
1802
|
}
|
|
1804
1803
|
const expectedNames = node.fields.map(([name]) => name);
|
|
1805
1804
|
const actualNames = input.fields.map((field) => field.value);
|
|
@@ -1808,7 +1807,7 @@ const decode_typed_edn_node = (graph: DataShapeGraph, nodeId: number, input: any
|
|
|
1808
1807
|
if (missing.length > 0 || unknown.length > 0 || expectedNames.length !== actualNames.length) {
|
|
1809
1808
|
return typed_edn_error(
|
|
1810
1809
|
path,
|
|
1811
|
-
`
|
|
1810
|
+
`struct :${node.nominal.name.value} fields mismatch; missing [${missing.join(", ")}], unknown [${unknown.join(", ")}]`
|
|
1812
1811
|
);
|
|
1813
1812
|
}
|
|
1814
1813
|
const decodedFields = new Map<string, CalcitValue>();
|
|
@@ -1819,18 +1818,18 @@ const decode_typed_edn_node = (graph: DataShapeGraph, nodeId: number, input: any
|
|
|
1819
1818
|
// Native declarations use lexical field order while the JS runtime keeps
|
|
1820
1819
|
// its interned-tag order. Re-align values to the nominal JS declaration.
|
|
1821
1820
|
if (decodedFields.size !== node.nominal.fields.length) {
|
|
1822
|
-
return typed_edn_error(path, `
|
|
1821
|
+
return typed_edn_error(path, `struct :${node.nominal.name.value} decoder fields do not match its nominal declaration`);
|
|
1823
1822
|
}
|
|
1824
1823
|
const values = node.nominal.fields.map((field) => {
|
|
1825
1824
|
if (!decodedFields.has(field.value)) {
|
|
1826
|
-
return typed_edn_error(path, `
|
|
1825
|
+
return typed_edn_error(path, `struct :${node.nominal.name.value} is missing declared field :${field.value}`);
|
|
1827
1826
|
}
|
|
1828
1827
|
return decodedFields.get(field.value)!;
|
|
1829
1828
|
});
|
|
1830
|
-
return new
|
|
1829
|
+
return new CalcitStructValue(node.nominal.name, node.nominal.fields, values, node.nominal);
|
|
1831
1830
|
}
|
|
1832
1831
|
case "enum": {
|
|
1833
|
-
if (!(input instanceof
|
|
1832
|
+
if (!(input instanceof CalcitEnumValue) || input.enumPrototype == null) {
|
|
1834
1833
|
return typed_edn_error(path, `expected enum :${node.nominal.name()}, got ${typed_edn_kind(input)}`);
|
|
1835
1834
|
}
|
|
1836
1835
|
const actualEnumName = enum_prototype_name(input.enumPrototype);
|
|
@@ -1854,7 +1853,7 @@ const decode_typed_edn_node = (graph: DataShapeGraph, nodeId: number, input: any
|
|
|
1854
1853
|
const values = variant.payload.map((payloadNode, idx) =>
|
|
1855
1854
|
decode_typed_edn_node(graph, payloadNode, input.extra[idx], `${path}.payload[${idx}]`, depth + 1)
|
|
1856
1855
|
);
|
|
1857
|
-
return new
|
|
1856
|
+
return new CalcitEnumValue(newTag(variant.tag), values, node.nominal);
|
|
1858
1857
|
}
|
|
1859
1858
|
default:
|
|
1860
1859
|
return typed_edn_error(path, `invalid data shape node kind: ${(node as any).kind}`);
|
|
@@ -1937,13 +1936,13 @@ const calcit_to_json = (value: CalcitValue): any => {
|
|
|
1937
1936
|
}
|
|
1938
1937
|
return result;
|
|
1939
1938
|
}
|
|
1940
|
-
if (value instanceof
|
|
1939
|
+
if (value instanceof CalcitEnumValue) {
|
|
1941
1940
|
return [calcit_to_json(value.tag), ...value.extra.map(calcit_to_json)];
|
|
1942
1941
|
}
|
|
1943
1942
|
if (value instanceof CalcitCirruQuote) {
|
|
1944
1943
|
return cirru_quote_to_json(value.value as ICirruNode);
|
|
1945
1944
|
}
|
|
1946
|
-
if (value instanceof
|
|
1945
|
+
if (value instanceof CalcitStructValue) {
|
|
1947
1946
|
const result: Record<string, any> = {};
|
|
1948
1947
|
for (let idx = 0; idx < value.fields.length; idx++) {
|
|
1949
1948
|
result[value.fields[idx].value] = calcit_to_json(value.values[idx]);
|
|
@@ -2054,11 +2053,11 @@ export let _$n_js_object = (...xs: CalcitValue[]): Record<string, CalcitValue> =
|
|
|
2054
2053
|
return ret;
|
|
2055
2054
|
};
|
|
2056
2055
|
|
|
2057
|
-
export let _$o__$o_ = (tagName: CalcitValue, ...extra: CalcitValue[]):
|
|
2058
|
-
return new
|
|
2056
|
+
export let _$o__$o_ = (tagName: CalcitValue, ...extra: CalcitValue[]): CalcitEnumValue => {
|
|
2057
|
+
return new CalcitEnumValue(tagName, extra, null);
|
|
2059
2058
|
};
|
|
2060
2059
|
|
|
2061
|
-
export let _PCT__$o__$o_ = (enumPrototype: CalcitValue, tag: CalcitValue, ...extra: CalcitValue[]):
|
|
2060
|
+
export let _PCT__$o__$o_ = (enumPrototype: CalcitValue, tag: CalcitValue, ...extra: CalcitValue[]): CalcitEnumValue => {
|
|
2062
2061
|
const proto = assert_enum_tag_args("%::", enumPrototype, tag as CalcitTag);
|
|
2063
2062
|
const tagValue = tag as CalcitTag;
|
|
2064
2063
|
|
|
@@ -2077,11 +2076,10 @@ export let _PCT__$o__$o_ = (enumPrototype: CalcitValue, tag: CalcitValue, ...ext
|
|
|
2077
2076
|
throw new Error(`Expected variant definition to be a list, got ${variantDefinition}`);
|
|
2078
2077
|
}
|
|
2079
2078
|
|
|
2080
|
-
|
|
2081
|
-
return new CalcitTuple(tag, extra, tupleEnumPrototype);
|
|
2079
|
+
return new CalcitEnumValue(tag, extra, enumPrototype as CalcitEnumDef);
|
|
2082
2080
|
};
|
|
2083
2081
|
|
|
2084
|
-
export let _PCT__PCT__$o__$o_ = (impl: CalcitValue, enumPrototype: CalcitValue, tag: CalcitValue, ...extra: CalcitValue[]):
|
|
2082
|
+
export let _PCT__PCT__$o__$o_ = (impl: CalcitValue, enumPrototype: CalcitValue, tag: CalcitValue, ...extra: CalcitValue[]): CalcitEnumValue => {
|
|
2085
2083
|
// Runtime validation: check if tag exists in enum and arity matches
|
|
2086
2084
|
const proto = assert_enum_tag_args("%%::", enumPrototype, tag as CalcitTag);
|
|
2087
2085
|
const tagValue = tag as CalcitTag;
|
|
@@ -2101,9 +2099,8 @@ export let _PCT__PCT__$o__$o_ = (impl: CalcitValue, enumPrototype: CalcitValue,
|
|
|
2101
2099
|
throw new Error(`Expected variant definition to be a list, got ${variantDefinition}`);
|
|
2102
2100
|
}
|
|
2103
2101
|
|
|
2104
|
-
const tupleEnumPrototype = enumPrototype instanceof CalcitEnum ? enumPrototype : (proto as any);
|
|
2105
2102
|
const implValue = coerce_impl(impl, "%:: with impl");
|
|
2106
|
-
return new
|
|
2103
|
+
return new CalcitEnumValue(tag, extra, (enumPrototype as CalcitEnumDef).withImpls(implValue));
|
|
2107
2104
|
};
|
|
2108
2105
|
|
|
2109
2106
|
// mutable place for core to register
|
|
@@ -2116,8 +2113,8 @@ let calcit_builtin_impls = {
|
|
|
2116
2113
|
list: null as CalcitImplEntry,
|
|
2117
2114
|
map: null as CalcitImplEntry,
|
|
2118
2115
|
fn: null as CalcitImplEntry,
|
|
2119
|
-
|
|
2120
|
-
|
|
2116
|
+
enum: null as CalcitImplEntry,
|
|
2117
|
+
struct: null as CalcitImplEntry,
|
|
2121
2118
|
scalar: null as CalcitImplEntry,
|
|
2122
2119
|
};
|
|
2123
2120
|
|
|
@@ -2156,10 +2153,10 @@ function lookup_impls(obj: CalcitValue): [CalcitImpl[], string] {
|
|
|
2156
2153
|
} else if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
|
|
2157
2154
|
tag = "&core-map-methods";
|
|
2158
2155
|
impls = normalize_builtin_impls(calcit_builtin_impls.map);
|
|
2159
|
-
} else if (obj instanceof
|
|
2156
|
+
} else if (obj instanceof CalcitStructValue) {
|
|
2160
2157
|
tag = obj.name.toString();
|
|
2161
2158
|
let instanceImpls = obj.structRef.impls;
|
|
2162
|
-
let builtinRecordImpls = normalize_builtin_impls(calcit_builtin_impls.
|
|
2159
|
+
let builtinRecordImpls = normalize_builtin_impls(calcit_builtin_impls.struct);
|
|
2163
2160
|
if (builtinRecordImpls && instanceImpls && instanceImpls.length > 0) {
|
|
2164
2161
|
impls = [...builtinRecordImpls, ...instanceImpls];
|
|
2165
2162
|
} else if (builtinRecordImpls) {
|
|
@@ -2167,10 +2164,10 @@ function lookup_impls(obj: CalcitValue): [CalcitImpl[], string] {
|
|
|
2167
2164
|
} else {
|
|
2168
2165
|
impls = instanceImpls;
|
|
2169
2166
|
}
|
|
2170
|
-
} else if (obj instanceof
|
|
2167
|
+
} else if (obj instanceof CalcitEnumValue) {
|
|
2171
2168
|
tag = obj.tag.toString();
|
|
2172
2169
|
let instanceImpls = obj.impls;
|
|
2173
|
-
let builtinTupleImpls = normalize_builtin_impls(calcit_builtin_impls.
|
|
2170
|
+
let builtinTupleImpls = normalize_builtin_impls(calcit_builtin_impls.enum);
|
|
2174
2171
|
if (builtinTupleImpls && instanceImpls && instanceImpls.length > 0) {
|
|
2175
2172
|
impls = [...builtinTupleImpls, ...instanceImpls];
|
|
2176
2173
|
} else if (builtinTupleImpls) {
|
|
@@ -2181,16 +2178,16 @@ function lookup_impls(obj: CalcitValue): [CalcitImpl[], string] {
|
|
|
2181
2178
|
} else if (obj instanceof CalcitSet) {
|
|
2182
2179
|
tag = "&core-set-methods";
|
|
2183
2180
|
impls = normalize_builtin_impls(calcit_builtin_impls.set);
|
|
2184
|
-
} else if (obj instanceof
|
|
2181
|
+
} else if (obj instanceof CalcitStructDef) {
|
|
2185
2182
|
// Bare type definitions (not yet instantiated) carry their own attached
|
|
2186
2183
|
// impls, so introspection tools like `&methods-of` can answer "what
|
|
2187
2184
|
// methods will instances of this type have" without a concrete instance.
|
|
2188
2185
|
tag = obj.name.toString();
|
|
2189
|
-
const builtinRecordImpls = normalize_builtin_impls(calcit_builtin_impls.
|
|
2186
|
+
const builtinRecordImpls = normalize_builtin_impls(calcit_builtin_impls.struct) ?? [];
|
|
2190
2187
|
impls = [...builtinRecordImpls, ...(obj.impls ?? [])];
|
|
2191
|
-
} else if (obj instanceof
|
|
2188
|
+
} else if (obj instanceof CalcitEnumDef) {
|
|
2192
2189
|
tag = obj.name();
|
|
2193
|
-
const builtinTupleImpls = normalize_builtin_impls(calcit_builtin_impls.
|
|
2190
|
+
const builtinTupleImpls = normalize_builtin_impls(calcit_builtin_impls.enum) ?? [];
|
|
2194
2191
|
impls = [...builtinTupleImpls, ...(obj.impls ?? [])];
|
|
2195
2192
|
} else if (typeof obj === "number") {
|
|
2196
2193
|
tag = "&core-number-methods";
|
|
@@ -2226,7 +2223,7 @@ export function invoke_method(p: string, obj: CalcitValue, ...args: CalcitValue[
|
|
|
2226
2223
|
let tag = pair[1];
|
|
2227
2224
|
// builtin impl lists are ordered by priority in calcit-core.
|
|
2228
2225
|
// user-defined values use impl-traits append, so later impls override earlier ones.
|
|
2229
|
-
let reverse = obj instanceof
|
|
2226
|
+
let reverse = obj instanceof CalcitStructValue || obj instanceof CalcitEnumValue || obj instanceof CalcitStructDef || obj instanceof CalcitEnumDef;
|
|
2230
2227
|
let idx = reverse ? impls.length - 1 : 0;
|
|
2231
2228
|
while (reverse ? idx >= 0 : idx < impls.length) {
|
|
2232
2229
|
let klass = impls[idx];
|
|
@@ -2255,7 +2252,7 @@ export function _$n_methods_of(obj: CalcitValue): CalcitSliceList {
|
|
|
2255
2252
|
throw new Error(`&methods-of cannot resolve impls for: ${toString(obj, true)}`);
|
|
2256
2253
|
}
|
|
2257
2254
|
let impls = pair[0];
|
|
2258
|
-
let reverse = obj instanceof
|
|
2255
|
+
let reverse = obj instanceof CalcitStructValue || obj instanceof CalcitEnumValue || obj instanceof CalcitStructDef || obj instanceof CalcitEnumDef;
|
|
2259
2256
|
let seen = new Set<string>();
|
|
2260
2257
|
let ys: CalcitValue[] = [];
|
|
2261
2258
|
|
|
@@ -2298,7 +2295,7 @@ export function _$n_inspect_methods(obj: CalcitValue, note: CalcitValue): Calcit
|
|
|
2298
2295
|
throw new Error(`&inspect-methods cannot resolve impls for: ${toString(obj, true)}`);
|
|
2299
2296
|
}
|
|
2300
2297
|
let impls = pair[0];
|
|
2301
|
-
let reverse = obj instanceof
|
|
2298
|
+
let reverse = obj instanceof CalcitStructValue || obj instanceof CalcitEnumValue || obj instanceof CalcitStructDef || obj instanceof CalcitEnumDef;
|
|
2302
2299
|
|
|
2303
2300
|
console.log("\n&inspect-methods");
|
|
2304
2301
|
console.log(`Note: ${toString(note, true)}`);
|
|
@@ -2352,7 +2349,7 @@ export function _$n_trait_call(traitDef: CalcitValue, method: CalcitValue, obj:
|
|
|
2352
2349
|
throw new Error(`&trait-call cannot resolve impls for: ${toString(obj, true)}`);
|
|
2353
2350
|
}
|
|
2354
2351
|
const impls = pair[0];
|
|
2355
|
-
const reverse = obj instanceof
|
|
2352
|
+
const reverse = obj instanceof CalcitStructValue || obj instanceof CalcitEnumValue || obj instanceof CalcitStructDef || obj instanceof CalcitEnumDef;
|
|
2356
2353
|
let idx = reverse ? impls.length - 1 : 0;
|
|
2357
2354
|
while (reverse ? idx >= 0 : idx < impls.length) {
|
|
2358
2355
|
const impl = impls[idx];
|