@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
|
@@ -5,15 +5,15 @@ import { newTag, castTag, toString, CalcitTag, getStringName, findInFields } fro
|
|
|
5
5
|
|
|
6
6
|
import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { CalcitStructDef } from "./js-struct-def.mjs";
|
|
9
9
|
|
|
10
|
-
export class
|
|
10
|
+
export class CalcitStructValue {
|
|
11
11
|
name: CalcitTag;
|
|
12
12
|
fields: Array<CalcitTag>;
|
|
13
13
|
values: Array<CalcitValue>;
|
|
14
|
-
structRef:
|
|
14
|
+
structRef: CalcitStructDef;
|
|
15
15
|
cachedHash: Hash;
|
|
16
|
-
constructor(name: CalcitTag, fields: Array<CalcitTag>, values?: Array<CalcitValue>, structRef?:
|
|
16
|
+
constructor(name: CalcitTag, fields: Array<CalcitTag>, values?: Array<CalcitValue>, structRef?: CalcitStructDef) {
|
|
17
17
|
this.name = name;
|
|
18
18
|
let fieldNames = fields.map(castTag);
|
|
19
19
|
this.fields = fields;
|
|
@@ -26,7 +26,7 @@ export class CalcitRecord {
|
|
|
26
26
|
this.values = new Array(fieldNames.length);
|
|
27
27
|
}
|
|
28
28
|
this.cachedHash = null;
|
|
29
|
-
this.structRef = structRef || new
|
|
29
|
+
this.structRef = structRef || new CalcitStructDef(name, fields, new Array(fields.length).fill(null));
|
|
30
30
|
}
|
|
31
31
|
get(k: CalcitValue) {
|
|
32
32
|
let field = castTag(k);
|
|
@@ -37,6 +37,11 @@ export class CalcitRecord {
|
|
|
37
37
|
return undefined;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
getRequired(k: CalcitValue): CalcitValue {
|
|
41
|
+
const value = this.get(k);
|
|
42
|
+
if (value !== undefined) return value;
|
|
43
|
+
throw new Error(`struct '${this.name.value}' does not define field ${castTag(k).toString()}`);
|
|
44
|
+
}
|
|
40
45
|
getOrNil(k: CalcitValue) {
|
|
41
46
|
let field = castTag(k);
|
|
42
47
|
let idx = findInFields(this.fields, field);
|
|
@@ -46,7 +51,7 @@ export class CalcitRecord {
|
|
|
46
51
|
return undefined;
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
|
-
assoc(k: CalcitValue, v: CalcitValue):
|
|
54
|
+
assoc(k: CalcitValue, v: CalcitValue): CalcitStructValue {
|
|
50
55
|
let values: Array<CalcitValue> = new Array(this.fields.length);
|
|
51
56
|
let k_id = castTag(k);
|
|
52
57
|
for (let idx = 0; idx < this.fields.length; idx++) {
|
|
@@ -56,7 +61,7 @@ export class CalcitRecord {
|
|
|
56
61
|
values[idx] = this.values[idx];
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
|
-
return new
|
|
64
|
+
return new CalcitStructValue(this.name, this.fields, values, this.structRef);
|
|
60
65
|
}
|
|
61
66
|
/** return -1 for missing */
|
|
62
67
|
findIndex(k: CalcitValue) {
|
|
@@ -70,14 +75,14 @@ export class CalcitRecord {
|
|
|
70
75
|
}
|
|
71
76
|
toString(disableJsDataWarning: boolean = false): string {
|
|
72
77
|
// Optimize string building using array join instead of concatenation
|
|
73
|
-
const parts = ["(%{} '", this.name.value];
|
|
78
|
+
const parts = this.name.value === "_" ? ["(%{} _"] : ["(%{} '", this.name.value];
|
|
74
79
|
for (let idx = 0; idx < this.fields.length; idx++) {
|
|
75
80
|
parts.push(" (", this.fields[idx].toString(), " ", toString(this.values[idx], true, disableJsDataWarning), ")");
|
|
76
81
|
}
|
|
77
82
|
parts.push(")");
|
|
78
83
|
return parts.join("");
|
|
79
84
|
}
|
|
80
|
-
withImpls(impl: CalcitValue | CalcitImpl[]):
|
|
85
|
+
withImpls(impl: CalcitValue | CalcitImpl[]): CalcitStructValue {
|
|
81
86
|
let nextImpls: CalcitImpl[];
|
|
82
87
|
if (impl instanceof CalcitImpl) {
|
|
83
88
|
nextImpls = [impl];
|
|
@@ -86,37 +91,37 @@ export class CalcitRecord {
|
|
|
86
91
|
} else {
|
|
87
92
|
throw new Error("Expected an impl or array of impls");
|
|
88
93
|
}
|
|
89
|
-
let nextStruct = new
|
|
90
|
-
return new
|
|
94
|
+
let nextStruct = new CalcitStructDef(this.name, this.fields, this.structRef.fieldTypes, this.structRef.impls.concat(nextImpls));
|
|
95
|
+
return new CalcitStructValue(this.name, this.fields, this.values, nextStruct);
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
export let
|
|
99
|
+
export let new_struct_value = (name: CalcitValue, ...fields: Array<CalcitValue>): CalcitValue => {
|
|
95
100
|
let fieldNames = fields.map(castTag).sort((x, y) => {
|
|
96
101
|
if (x.idx < y.idx) {
|
|
97
102
|
return -1;
|
|
98
103
|
} else if (x.idx > y.idx) {
|
|
99
104
|
return 1;
|
|
100
105
|
} else {
|
|
101
|
-
throw new Error(`Unexpected duplication in
|
|
106
|
+
throw new Error(`Unexpected duplication in struct fields: ${x.toString()}`);
|
|
102
107
|
}
|
|
103
108
|
});
|
|
104
|
-
return new
|
|
109
|
+
return new CalcitStructValue(castTag(name), fieldNames);
|
|
105
110
|
};
|
|
106
111
|
|
|
107
|
-
export let
|
|
112
|
+
export let new_impl_struct_value = (impl: CalcitImpl, name: CalcitValue, ...fields: Array<CalcitValue>): CalcitValue => {
|
|
108
113
|
let fieldNames = fields.map(castTag).sort((x, y) => {
|
|
109
114
|
if (x.idx < y.idx) {
|
|
110
115
|
return -1;
|
|
111
116
|
} else if (x.idx > y.idx) {
|
|
112
117
|
return 1;
|
|
113
118
|
} else {
|
|
114
|
-
throw new Error(`Unexpected duplication in
|
|
119
|
+
throw new Error(`Unexpected duplication in struct fields: ${x.toString()}`);
|
|
115
120
|
}
|
|
116
121
|
});
|
|
117
122
|
let nameTag = castTag(name);
|
|
118
|
-
let structRef = new
|
|
119
|
-
return new
|
|
123
|
+
let structRef = new CalcitStructDef(nameTag, fieldNames, new Array(fieldNames.length).fill(null), [impl]);
|
|
124
|
+
return new CalcitStructValue(nameTag, fieldNames, undefined, structRef);
|
|
120
125
|
};
|
|
121
126
|
|
|
122
127
|
/** Loose record: `?{} :field1 val1 :field2 val2` – record without a declared struct.
|
|
@@ -138,8 +143,8 @@ export let _$q__$M_ = (...xs: Array<CalcitValue>): CalcitValue => {
|
|
|
138
143
|
}
|
|
139
144
|
let fieldNames = pairs.map((p) => p[0]);
|
|
140
145
|
let values = pairs.map((p) => p[1]);
|
|
141
|
-
let looseTag = newTag("
|
|
142
|
-
return new
|
|
146
|
+
let looseTag = newTag("_");
|
|
147
|
+
return new CalcitStructValue(looseTag, fieldNames, values);
|
|
143
148
|
};
|
|
144
149
|
|
|
145
150
|
export let fieldsEqual = (xs: Array<CalcitTag>, ys: Array<CalcitTag>): boolean => {
|
|
@@ -158,13 +163,13 @@ export let fieldsEqual = (xs: Array<CalcitTag>, ys: Array<CalcitTag>): boolean =
|
|
|
158
163
|
};
|
|
159
164
|
|
|
160
165
|
export let _$n__PCT__$M_ = (proto: CalcitValue, ...xs: Array<CalcitValue>): CalcitValue => {
|
|
161
|
-
let recordProto:
|
|
162
|
-
if (proto instanceof
|
|
166
|
+
let recordProto: CalcitStructValue;
|
|
167
|
+
if (proto instanceof CalcitStructValue) {
|
|
163
168
|
recordProto = proto;
|
|
164
|
-
} else if (proto instanceof
|
|
165
|
-
recordProto = new
|
|
169
|
+
} else if (proto instanceof CalcitStructDef) {
|
|
170
|
+
recordProto = new CalcitStructValue(proto.name, proto.fields, new Array(proto.fields.length).fill(null), proto);
|
|
166
171
|
} else {
|
|
167
|
-
throw new Error("Expected prototype to be a
|
|
172
|
+
throw new Error("Expected prototype to be a StructDef");
|
|
168
173
|
}
|
|
169
174
|
{
|
|
170
175
|
if (xs.length % 2 !== 0) {
|
|
@@ -187,23 +192,23 @@ export let _$n__PCT__$M_ = (proto: CalcitValue, ...xs: Array<CalcitValue>): Calc
|
|
|
187
192
|
}
|
|
188
193
|
|
|
189
194
|
if (idx < 0) {
|
|
190
|
-
throw new Error("invalid field name for this
|
|
195
|
+
throw new Error("invalid field name for this struct");
|
|
191
196
|
}
|
|
192
197
|
if (values[i] != null) {
|
|
193
|
-
throw new Error("
|
|
198
|
+
throw new Error("struct field already has value, probably duplicated key");
|
|
194
199
|
}
|
|
195
200
|
values[i] = xs[idx * 2 + 1];
|
|
196
201
|
}
|
|
197
202
|
|
|
198
|
-
return new
|
|
203
|
+
return new CalcitStructValue(recordProto.name, recordProto.fields, values, recordProto.structRef);
|
|
199
204
|
}
|
|
200
205
|
};
|
|
201
206
|
|
|
202
207
|
export let _$n__PCT__$M__$q_ = (proto: CalcitValue, ...xs: Array<CalcitValue>): CalcitValue => {
|
|
203
|
-
let recordProto:
|
|
208
|
+
let recordProto: CalcitStructValue;
|
|
204
209
|
let values: Array<CalcitValue>;
|
|
205
|
-
if (proto instanceof
|
|
206
|
-
recordProto = new
|
|
210
|
+
if (proto instanceof CalcitStructDef) {
|
|
211
|
+
recordProto = new CalcitStructValue(proto.name, proto.fields, new Array(proto.fields.length).fill(null), proto);
|
|
207
212
|
values = recordProto.values.slice();
|
|
208
213
|
} else {
|
|
209
214
|
throw new Error("Expected prototype to be a struct");
|
|
@@ -221,18 +226,18 @@ export let _$n__PCT__$M__$q_ = (proto: CalcitValue, ...xs: Array<CalcitValue>):
|
|
|
221
226
|
throw new Error(`Cannot find field ${k} among ${recordProto.fields}`);
|
|
222
227
|
}
|
|
223
228
|
if (touched.has(idx)) {
|
|
224
|
-
throw new Error(`
|
|
229
|
+
throw new Error(`struct field already has value, probably duplicated key: ${k}`);
|
|
225
230
|
}
|
|
226
231
|
touched.add(idx);
|
|
227
232
|
values[idx] = xs[i + 1];
|
|
228
233
|
}
|
|
229
234
|
|
|
230
|
-
return new
|
|
235
|
+
return new CalcitStructValue(recordProto.name, recordProto.fields, values, recordProto.structRef);
|
|
231
236
|
};
|
|
232
237
|
|
|
233
238
|
/// update record with new values
|
|
234
|
-
export let _$
|
|
235
|
-
if (proto instanceof
|
|
239
|
+
export let _$n_struct_$o_with = (proto: CalcitValue, ...xs: Array<CalcitValue>): CalcitValue => {
|
|
240
|
+
if (proto instanceof CalcitStructValue) {
|
|
236
241
|
if (xs.length % 2 !== 0) {
|
|
237
242
|
throw new Error("Expected even number of key/value");
|
|
238
243
|
}
|
|
@@ -246,39 +251,40 @@ export let _$n_record_$o_with = (proto: CalcitValue, ...xs: Array<CalcitValue>):
|
|
|
246
251
|
}
|
|
247
252
|
values[idx] = v;
|
|
248
253
|
}
|
|
249
|
-
return new
|
|
254
|
+
return new CalcitStructValue(proto.name, proto.fields, values, proto.structRef);
|
|
250
255
|
} else {
|
|
251
|
-
throw new Error("Expected prototype to be a
|
|
256
|
+
throw new Error("Expected prototype to be a StructDef");
|
|
252
257
|
}
|
|
253
258
|
};
|
|
254
259
|
|
|
255
|
-
export let _$
|
|
256
|
-
if (x instanceof
|
|
260
|
+
export let _$n_struct_$o_get_name = (x: CalcitValue): CalcitTag => {
|
|
261
|
+
if (x instanceof CalcitStructValue) {
|
|
257
262
|
return x.name;
|
|
258
263
|
} else {
|
|
259
|
-
throw new Error("Expected a
|
|
264
|
+
throw new Error("Expected a struct value");
|
|
260
265
|
}
|
|
261
266
|
};
|
|
262
267
|
|
|
263
|
-
export let _$
|
|
264
|
-
if (x instanceof
|
|
268
|
+
export let _$n_struct_$o_definition = (x: CalcitValue): CalcitValue => {
|
|
269
|
+
if (x instanceof CalcitStructValue) {
|
|
270
|
+
if (x.name.value === "_") return null;
|
|
265
271
|
return x.structRef ?? null;
|
|
266
272
|
} else {
|
|
267
|
-
throw new Error("Expected a
|
|
273
|
+
throw new Error("Expected a struct value");
|
|
268
274
|
}
|
|
269
275
|
};
|
|
270
276
|
|
|
271
|
-
export let _$
|
|
272
|
-
let recordProto:
|
|
273
|
-
if (proto instanceof
|
|
274
|
-
recordProto = new
|
|
277
|
+
export let _$n_struct_$o_from_map = (proto: CalcitValue, data: CalcitValue): CalcitValue => {
|
|
278
|
+
let recordProto: CalcitStructValue;
|
|
279
|
+
if (proto instanceof CalcitStructDef) {
|
|
280
|
+
recordProto = new CalcitStructValue(proto.name, proto.fields, new Array(proto.fields.length).fill(null), proto);
|
|
275
281
|
} else {
|
|
276
282
|
throw new Error("Expected prototype to be struct");
|
|
277
283
|
}
|
|
278
284
|
|
|
279
|
-
if (data instanceof
|
|
285
|
+
if (data instanceof CalcitStructValue) {
|
|
280
286
|
if (fieldsEqual(recordProto.fields, data.fields)) {
|
|
281
|
-
return new
|
|
287
|
+
return new CalcitStructValue(recordProto.name, recordProto.fields, data.values, recordProto.structRef);
|
|
282
288
|
} else {
|
|
283
289
|
let values: Array<CalcitValue> = [];
|
|
284
290
|
for (let i = 0; i < recordProto.fields.length; i++) {
|
|
@@ -289,7 +295,7 @@ export let _$n_record_$o_from_map = (proto: CalcitValue, data: CalcitValue): Cal
|
|
|
289
295
|
}
|
|
290
296
|
values.push(data.values[idx]);
|
|
291
297
|
}
|
|
292
|
-
return new
|
|
298
|
+
return new CalcitStructValue(recordProto.name, recordProto.fields, values, recordProto.structRef);
|
|
293
299
|
}
|
|
294
300
|
} else if (data instanceof CalcitMap || data instanceof CalcitSliceMap) {
|
|
295
301
|
let pairs_buffer: Array<[CalcitTag, CalcitValue]> = [];
|
|
@@ -314,47 +320,47 @@ export let _$n_record_$o_from_map = (proto: CalcitValue, data: CalcitValue): Cal
|
|
|
314
320
|
}
|
|
315
321
|
throw new Error(`Cannot find field ${field} among ${pairs_buffer}`);
|
|
316
322
|
}
|
|
317
|
-
return new
|
|
323
|
+
return new CalcitStructValue(recordProto.name, recordProto.fields, values, recordProto.structRef);
|
|
318
324
|
} else {
|
|
319
|
-
throw new Error("Expected
|
|
325
|
+
throw new Error("Expected a struct value or data for making a struct");
|
|
320
326
|
}
|
|
321
327
|
};
|
|
322
328
|
|
|
323
|
-
export let _$
|
|
324
|
-
if (x instanceof
|
|
329
|
+
export let _$n_struct_$o_to_map = (x: CalcitValue): CalcitValue => {
|
|
330
|
+
if (x instanceof CalcitStructValue) {
|
|
325
331
|
var dict: Array<CalcitValue> = [];
|
|
326
332
|
for (let idx = 0; idx < x.fields.length; idx++) {
|
|
327
333
|
dict.push(x.fields[idx], x.values[idx]);
|
|
328
334
|
}
|
|
329
335
|
return new CalcitSliceMap(dict);
|
|
330
336
|
} else {
|
|
331
|
-
throw new Error("Expected
|
|
337
|
+
throw new Error("Expected a struct value");
|
|
332
338
|
}
|
|
333
339
|
};
|
|
334
340
|
|
|
335
|
-
export let _$
|
|
336
|
-
let targetStruct:
|
|
337
|
-
if (y instanceof
|
|
341
|
+
export let _$n_struct_$o_matches_$q_ = (x: CalcitValue, y: CalcitValue): boolean => {
|
|
342
|
+
let targetStruct: CalcitStructDef;
|
|
343
|
+
if (y instanceof CalcitStructValue) {
|
|
338
344
|
targetStruct = y.structRef;
|
|
339
|
-
} else if (y instanceof
|
|
345
|
+
} else if (y instanceof CalcitStructDef) {
|
|
340
346
|
targetStruct = y;
|
|
341
347
|
} else {
|
|
342
|
-
throw new Error("Expected second argument to be
|
|
348
|
+
throw new Error("Expected second argument to be a struct value or StructDef");
|
|
343
349
|
}
|
|
344
350
|
|
|
345
|
-
if (x instanceof
|
|
351
|
+
if (x instanceof CalcitStructValue) {
|
|
346
352
|
if (x.name !== targetStruct.name) {
|
|
347
353
|
return false;
|
|
348
354
|
}
|
|
349
355
|
return fieldsEqual(x.fields, targetStruct.fields);
|
|
350
356
|
} else {
|
|
351
|
-
throw new Error("Expected first argument to be
|
|
357
|
+
throw new Error("Expected first argument to be a struct value");
|
|
352
358
|
}
|
|
353
359
|
};
|
|
354
360
|
|
|
355
|
-
export function _$
|
|
361
|
+
export function _$n_struct_$o_extend_as(obj: CalcitValue, new_name: CalcitValue, new_key: CalcitValue, new_value: CalcitValue) {
|
|
356
362
|
if (arguments.length !== 4) throw new Error(`Expected 4 arguments, got ${arguments.length}`);
|
|
357
|
-
if (!(obj instanceof
|
|
363
|
+
if (!(obj instanceof CalcitStructValue)) throw new Error("Expected a struct value");
|
|
358
364
|
let field = castTag(new_key);
|
|
359
365
|
let new_name_tag = castTag(new_name);
|
|
360
366
|
let new_fields: CalcitTag[] = [];
|
|
@@ -378,7 +384,7 @@ export function _$n_record_$o_extend_as(obj: CalcitValue, new_name: CalcitValue,
|
|
|
378
384
|
new_fields.push(k);
|
|
379
385
|
new_values.push(obj.values[i]);
|
|
380
386
|
} else {
|
|
381
|
-
throw new Error("
|
|
387
|
+
throw new Error("Cannot extend an existing struct field");
|
|
382
388
|
}
|
|
383
389
|
}
|
|
384
390
|
}
|
|
@@ -387,5 +393,5 @@ export function _$n_record_$o_extend_as(obj: CalcitValue, new_name: CalcitValue,
|
|
|
387
393
|
new_values.push(new_value);
|
|
388
394
|
}
|
|
389
395
|
|
|
390
|
-
return new
|
|
396
|
+
return new CalcitStructValue(new_name_tag, new_fields, new_values);
|
|
391
397
|
}
|
package/lib/js-enum.d.mts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { CalcitRecord } from "./js-record.mjs";
|
|
2
|
-
import { CalcitImpl } from "./js-impl.mjs";
|
|
3
|
-
export declare class CalcitEnum {
|
|
4
|
-
prototype: CalcitRecord;
|
|
5
|
-
cachedHash: number;
|
|
6
|
-
constructor(prototype: CalcitRecord);
|
|
7
|
-
name(): string;
|
|
8
|
-
get impls(): CalcitImpl[];
|
|
9
|
-
withImpls(impls: CalcitImpl | CalcitImpl[]): CalcitEnum;
|
|
10
|
-
toString(): string;
|
|
11
|
-
}
|