@calcit/procs 0.5.47 → 0.6.0-a2

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.
@@ -7,6 +7,8 @@ import { _$n_compare } from "./js-primes.mjs";
7
7
  import { CalcitList, CalcitSliceList } from "./js-list.mjs";
8
8
  import { CalcitSet, overwriteSetComparator } from "./js-set.mjs";
9
9
  import { CalcitTuple } from "./js-tuple.mjs";
10
+ import { CalcitCirruQuote, cirru_deep_equal } from "./js-cirru.mjs";
11
+ import "@cirru/writer.ts";
10
12
  // we have to inject cache in a dirty way in some cases
11
13
  const calcit_dirty_hash_key = "_calcit_cached_hash";
12
14
  let keywordIdx = 0;
@@ -165,6 +167,7 @@ let defaultHash_set = valueHash("set:");
165
167
  let defaultHash_list = valueHash("list:");
166
168
  let defaultHash_map = valueHash("map:");
167
169
  let defaultHash_record = valueHash("record:");
170
+ let defaultHash_cirru_quote = valueHash("cirru-quote:");
168
171
  let defaultHash_unknown = valueHash("unknown:");
169
172
  let fnHashCounter = 0;
170
173
  let jsObjectHashCounter = 0;
@@ -279,6 +282,11 @@ export let hashFunction = (x) => {
279
282
  x.cachedHash = base;
280
283
  return base;
281
284
  }
285
+ if (x instanceof CalcitCirruQuote) {
286
+ let base = defaultHash_cirru_quote;
287
+ base = hashCirru(base, x.value);
288
+ return base;
289
+ }
282
290
  console.warn(`[warn] calcit-js has no method for hashing this: ${x}`);
283
291
  // currently we use dirty solution here to generate a custom hash
284
292
  // probably happening in .to-pairs of maps, putting a js object into a set
@@ -289,6 +297,18 @@ export let hashFunction = (x) => {
289
297
  x[calcit_dirty_hash_key] = hashJsObject;
290
298
  return hashJsObject;
291
299
  };
300
+ /// traverse Cirru tree to make unique hash
301
+ let hashCirru = (base, x) => {
302
+ if (typeof x === "string") {
303
+ return mergeValueHash(base, hashFunction(x));
304
+ }
305
+ else {
306
+ for (let idx = 0; idx <= x.length; idx++) {
307
+ base = mergeValueHash(base, hashCirru(base, x[idx]));
308
+ }
309
+ return base;
310
+ }
311
+ };
292
312
  // Dirty code to change ternary-tree behavior
293
313
  overwriteHashGenerator(hashFunction);
294
314
  export let toString = (x, escaped, disableJsDataWarning = false) => {
@@ -342,6 +362,9 @@ export let toString = (x, escaped, disableJsDataWarning = false) => {
342
362
  if (x instanceof CalcitTuple) {
343
363
  return x.toString(disableJsDataWarning);
344
364
  }
365
+ if (x instanceof CalcitCirruQuote) {
366
+ return x.toString();
367
+ }
345
368
  if (!disableJsDataWarning) {
346
369
  console.warn("Unknown structure to string, better use `console.log`", x);
347
370
  }
@@ -460,6 +483,12 @@ export let _$n__$e_ = (x, y) => {
460
483
  }
461
484
  return false;
462
485
  }
486
+ if (x instanceof CalcitCirruQuote) {
487
+ if (y instanceof CalcitCirruQuote) {
488
+ return cirru_deep_equal(x.value, y.value);
489
+ }
490
+ return false;
491
+ }
463
492
  if (x instanceof CalcitList || x instanceof CalcitSliceList) {
464
493
  if (y instanceof CalcitList || y instanceof CalcitSliceList) {
465
494
  if (x.len() !== y.len()) {
@@ -1,6 +1,6 @@
1
1
  var _a;
2
2
  // CALCIT VERSION
3
- export const calcit_version = "0.5.47";
3
+ export const calcit_version = "0.6.0-a2";
4
4
  import { parse } from "@cirru/parser.ts";
5
5
  import { writeCirruCode } from "@cirru/writer.ts";
6
6
  import "./js-primes.mjs";
@@ -1063,6 +1063,10 @@ export let write_file = (path, content) => {
1063
1063
  export let parse_cirru = (code) => {
1064
1064
  return to_calcit_data(parse(code), true);
1065
1065
  };
1066
+ // for JavaScript, it's same as parse_cirru
1067
+ export let parse_cirru_list = (code) => {
1068
+ return to_calcit_data(parse(code), true);
1069
+ };
1066
1070
  export let parse_cirru_edn = (code) => {
1067
1071
  return extract_cirru_edn(parse(code)[0]);
1068
1072
  };
@@ -1318,6 +1322,9 @@ export let _$n_buffer = (...xs) => {
1318
1322
  export let _$n_hash = (x) => {
1319
1323
  return hashFunction(x);
1320
1324
  };
1325
+ export let _$n_cirru_quote_$o_to_list = (x) => {
1326
+ return x.toList();
1327
+ };
1321
1328
  // special procs have to be defined manually
1322
1329
  export let reduce = foldl;
1323
1330
  let unavailableProc = (...xs) => {
package/lib/js-cirru.mjs CHANGED
@@ -7,7 +7,21 @@ import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
7
7
  import { CalcitSet } from "./js-set.mjs";
8
8
  import { CalcitKeyword, CalcitSymbol, CalcitRecur, CalcitRef, kwd } from "./calcit-data.mjs";
9
9
  import { CalcitTuple } from "./js-tuple.mjs";
10
+ export class CalcitCirruQuote {
11
+ constructor(value) {
12
+ this.value = value;
13
+ }
14
+ toString() {
15
+ return `(cirru-quote ${JSON.stringify(this.value)})`;
16
+ }
17
+ toList() {
18
+ return to_calcit_data(this.value, true);
19
+ }
20
+ }
10
21
  export let format_cirru = (data, useInline) => {
22
+ if (data instanceof CalcitCirruQuote) {
23
+ return writeCirruCode(data.value, { useInline });
24
+ }
11
25
  let chunk = toWriterNode(data);
12
26
  if (!Array.isArray(chunk)) {
13
27
  throw new Error("Expected data of list");
@@ -43,6 +57,9 @@ export let to_cirru_edn = (x) => {
43
57
  // TODO can be faster
44
58
  return ["[]"].concat(x.toArray().map(to_cirru_edn));
45
59
  }
60
+ if (x instanceof CalcitCirruQuote) {
61
+ return ["quote", x.value];
62
+ }
46
63
  if (x instanceof CalcitMap || x instanceof CalcitSliceMap) {
47
64
  let buffer = ["{}"];
48
65
  let pairs = [];
@@ -219,7 +236,7 @@ export let extract_cirru_edn = (x) => {
219
236
  if (x.length !== 2) {
220
237
  throw new Error("quote expects 1 argument");
221
238
  }
222
- return new CalcitTuple(new CalcitSymbol("quote"), to_calcit_data(x[1], true));
239
+ return new CalcitCirruQuote(x[1]);
223
240
  }
224
241
  if (x[0] === "::") {
225
242
  if (x.length !== 3) {
@@ -314,6 +331,9 @@ let toWriterNode = (xs) => {
314
331
  if (typeof xs === "string") {
315
332
  return xs;
316
333
  }
334
+ else if (Array.isArray(xs)) {
335
+ return xs.map(toWriterNode);
336
+ }
317
337
  if (xs instanceof CalcitList || xs instanceof CalcitSliceList) {
318
338
  return xs.toArray().map(toWriterNode);
319
339
  }
@@ -321,3 +341,23 @@ let toWriterNode = (xs) => {
321
341
  throw new Error("Unexpected type for CirruWriteNode");
322
342
  }
323
343
  };
344
+ /** deep compare cirru array */
345
+ export let cirru_deep_equal = (x, y) => {
346
+ if (x === y) {
347
+ return true;
348
+ }
349
+ else if (Array.isArray(x) && Array.isArray(y)) {
350
+ if (x.length !== y.length) {
351
+ return false;
352
+ }
353
+ for (let idx = 0; idx < x.length; idx++) {
354
+ if (!cirru_deep_equal(x[idx], y[idx])) {
355
+ return false;
356
+ }
357
+ }
358
+ return true;
359
+ }
360
+ else {
361
+ return false;
362
+ }
363
+ };
package/lib/js-primes.mjs CHANGED
@@ -4,6 +4,7 @@ import { CalcitRecord } from "./js-record.mjs";
4
4
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
5
5
  import { CalcitSet as CalcitSet } from "./js-set.mjs";
6
6
  import { CalcitTuple } from "./js-tuple.mjs";
7
+ import { CalcitCirruQuote } from "./js-cirru.mjs";
7
8
  export let is_literal = (x) => {
8
9
  if (x == null)
9
10
  return true;
@@ -35,6 +36,7 @@ var PseudoTypeIndex;
35
36
  PseudoTypeIndex[PseudoTypeIndex["map"] = 11] = "map";
36
37
  PseudoTypeIndex[PseudoTypeIndex["record"] = 12] = "record";
37
38
  PseudoTypeIndex[PseudoTypeIndex["fn"] = 13] = "fn";
39
+ PseudoTypeIndex[PseudoTypeIndex["cirru_quote"] = 14] = "cirru_quote";
38
40
  })(PseudoTypeIndex || (PseudoTypeIndex = {}));
39
41
  let typeAsInt = (x) => {
40
42
  // based on order used in Ord traint
@@ -65,6 +67,8 @@ let typeAsInt = (x) => {
65
67
  return PseudoTypeIndex.map;
66
68
  if (x instanceof CalcitRecord)
67
69
  return PseudoTypeIndex.record;
70
+ if (x instanceof CalcitCirruQuote)
71
+ return PseudoTypeIndex.cirru_quote;
68
72
  // proc, fn, macro, syntax, not distinguished
69
73
  if (t === "function")
70
74
  return PseudoTypeIndex.fn;
@@ -102,6 +106,8 @@ export let _$n_compare = (a, b) => {
102
106
  return rawCompare(a, b);
103
107
  case PseudoTypeIndex.ref:
104
108
  return rawCompare(a.path, b.path);
109
+ case PseudoTypeIndex.cirru_quote:
110
+ return rawCompare(a, b); // TODO not stable
105
111
  default:
106
112
  // TODO, need more accurate solution
107
113
  if (a < b) {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@calcit/procs",
3
- "version": "0.5.47",
3
+ "version": "0.6.0-a2",
4
4
  "main": "./lib/calcit.procs.mjs",
5
5
  "devDependencies": {
6
- "@types/node": "^18.0.0",
6
+ "@types/node": "^18.0.6",
7
7
  "typescript": "^4.7.4"
8
8
  },
9
9
  "scripts": {
@@ -9,6 +9,8 @@ import { CalcitValue, _$n_compare } from "./js-primes.mjs";
9
9
  import { CalcitList, CalcitSliceList } from "./js-list.mjs";
10
10
  import { CalcitSet, overwriteSetComparator } from "./js-set.mjs";
11
11
  import { CalcitTuple } from "./js-tuple.mjs";
12
+ import { CalcitCirruQuote, cirru_deep_equal } from "./js-cirru.mjs";
13
+ import { CirruWriterNode } from "@cirru/writer.ts";
12
14
 
13
15
  // we have to inject cache in a dirty way in some cases
14
16
  const calcit_dirty_hash_key = "_calcit_cached_hash";
@@ -192,6 +194,7 @@ let defaultHash_set = valueHash("set:");
192
194
  let defaultHash_list = valueHash("list:");
193
195
  let defaultHash_map = valueHash("map:");
194
196
  let defaultHash_record = valueHash("record:");
197
+ let defaultHash_cirru_quote = valueHash("cirru-quote:");
195
198
 
196
199
  let defaultHash_unknown = valueHash("unknown:");
197
200
 
@@ -311,6 +314,11 @@ export let hashFunction = (x: CalcitValue): Hash => {
311
314
  x.cachedHash = base;
312
315
  return base;
313
316
  }
317
+ if (x instanceof CalcitCirruQuote) {
318
+ let base = defaultHash_cirru_quote;
319
+ base = hashCirru(base, x.value);
320
+ return base;
321
+ }
314
322
  console.warn(`[warn] calcit-js has no method for hashing this: ${x}`);
315
323
  // currently we use dirty solution here to generate a custom hash
316
324
  // probably happening in .to-pairs of maps, putting a js object into a set
@@ -322,6 +330,18 @@ export let hashFunction = (x: CalcitValue): Hash => {
322
330
  return hashJsObject;
323
331
  };
324
332
 
333
+ /// traverse Cirru tree to make unique hash
334
+ let hashCirru = (base: number, x: CirruWriterNode) => {
335
+ if (typeof x === "string") {
336
+ return mergeValueHash(base, hashFunction(x));
337
+ } else {
338
+ for (let idx = 0; idx <= x.length; idx++) {
339
+ base = mergeValueHash(base, hashCirru(base, x[idx]));
340
+ }
341
+ return base;
342
+ }
343
+ };
344
+
325
345
  // Dirty code to change ternary-tree behavior
326
346
  overwriteHashGenerator(hashFunction);
327
347
 
@@ -374,6 +394,9 @@ export let toString = (x: CalcitValue, escaped: boolean, disableJsDataWarning: b
374
394
  if (x instanceof CalcitTuple) {
375
395
  return x.toString(disableJsDataWarning);
376
396
  }
397
+ if (x instanceof CalcitCirruQuote) {
398
+ return x.toString();
399
+ }
377
400
 
378
401
  if (!disableJsDataWarning) {
379
402
  console.warn("Unknown structure to string, better use `console.log`", x);
@@ -501,6 +524,12 @@ export let _$n__$e_ = (x: CalcitValue, y: CalcitValue): boolean => {
501
524
  }
502
525
  return false;
503
526
  }
527
+ if (x instanceof CalcitCirruQuote) {
528
+ if (y instanceof CalcitCirruQuote) {
529
+ return cirru_deep_equal(x.value, y.value);
530
+ }
531
+ return false;
532
+ }
504
533
  if (x instanceof CalcitList || x instanceof CalcitSliceList) {
505
534
  if (y instanceof CalcitList || y instanceof CalcitSliceList) {
506
535
  if (x.len() !== y.len()) {
@@ -1,5 +1,5 @@
1
1
  // CALCIT VERSION
2
- export const calcit_version = "0.5.47";
2
+ export const calcit_version = "0.6.0-a2";
3
3
 
4
4
  import { parse, ICirruNode } from "@cirru/parser.ts";
5
5
  import { writeCirruCode } from "@cirru/writer.ts";
@@ -37,7 +37,7 @@ import { CalcitList, CalcitSliceList, foldl } from "./js-list.mjs";
37
37
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
38
38
  import { CalcitSet } from "./js-set.mjs";
39
39
  import { CalcitTuple } from "./js-tuple.mjs";
40
- import { to_calcit_data, extract_cirru_edn } from "./js-cirru.mjs";
40
+ import { to_calcit_data, extract_cirru_edn, CalcitCirruQuote } from "./js-cirru.mjs";
41
41
 
42
42
  let inNodeJs = typeof process !== "undefined" && process?.release?.name === "node";
43
43
 
@@ -1162,6 +1162,11 @@ export let parse_cirru = (code: string): CalcitList => {
1162
1162
  return to_calcit_data(parse(code), true) as CalcitList;
1163
1163
  };
1164
1164
 
1165
+ // for JavaScript, it's same as parse_cirru
1166
+ export let parse_cirru_list = (code: string): CalcitList => {
1167
+ return to_calcit_data(parse(code), true) as CalcitList;
1168
+ };
1169
+
1165
1170
  export let parse_cirru_edn = (code: string) => {
1166
1171
  return extract_cirru_edn(parse(code)[0]);
1167
1172
  };
@@ -1414,6 +1419,10 @@ export let _$n_hash = (x: CalcitValue): number => {
1414
1419
  return hashFunction(x);
1415
1420
  };
1416
1421
 
1422
+ export let _$n_cirru_quote_$o_to_list = (x: CalcitCirruQuote): CalcitValue => {
1423
+ return x.toList();
1424
+ };
1425
+
1417
1426
  // special procs have to be defined manually
1418
1427
  export let reduce = foldl;
1419
1428
 
@@ -11,7 +11,23 @@ import { CalcitTuple } from "./js-tuple.mjs";
11
11
 
12
12
  type CirruEdnFormat = string | CirruEdnFormat[];
13
13
 
14
- export let format_cirru = (data: CalcitList, useInline: boolean): string => {
14
+ export class CalcitCirruQuote {
15
+ value: CirruWriterNode;
16
+ constructor(value: CirruWriterNode) {
17
+ this.value = value;
18
+ }
19
+ toString(): string {
20
+ return `(cirru-quote ${JSON.stringify(this.value)})`;
21
+ }
22
+ toList(): CalcitValue {
23
+ return to_calcit_data(this.value, true);
24
+ }
25
+ }
26
+
27
+ export let format_cirru = (data: CalcitCirruQuote | CalcitList, useInline: boolean): string => {
28
+ if (data instanceof CalcitCirruQuote) {
29
+ return writeCirruCode(data.value, { useInline });
30
+ }
15
31
  let chunk = toWriterNode(data);
16
32
  if (!Array.isArray(chunk)) {
17
33
  throw new Error("Expected data of list");
@@ -48,6 +64,9 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
48
64
  // TODO can be faster
49
65
  return (["[]"] as CirruEdnFormat[]).concat(x.toArray().map(to_cirru_edn));
50
66
  }
67
+ if (x instanceof CalcitCirruQuote) {
68
+ return ["quote", x.value];
69
+ }
51
70
  if (x instanceof CalcitMap || x instanceof CalcitSliceMap) {
52
71
  let buffer: CirruEdnFormat = ["{}"];
53
72
  let pairs: [CalcitValue, CalcitValue][] = [];
@@ -217,7 +236,7 @@ export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
217
236
  if (x.length !== 2) {
218
237
  throw new Error("quote expects 1 argument");
219
238
  }
220
- return new CalcitTuple(new CalcitSymbol("quote"), to_calcit_data(x[1], true));
239
+ return new CalcitCirruQuote(x[1]);
221
240
  }
222
241
  if (x[0] === "::") {
223
242
  if (x.length !== 3) {
@@ -305,13 +324,34 @@ export let to_calcit_data = (x: any, noKeyword: boolean = false): CalcitValue =>
305
324
  throw new Error("Unexpected data for converting");
306
325
  };
307
326
 
308
- let toWriterNode = (xs: CalcitList | CalcitSliceList): CirruWriterNode => {
327
+ let toWriterNode = (xs: CalcitList | CalcitSliceList | Array<any> | String): CirruWriterNode => {
309
328
  if (typeof xs === "string") {
310
329
  return xs;
330
+ } else if (Array.isArray(xs)) {
331
+ return xs.map(toWriterNode);
311
332
  }
312
333
  if (xs instanceof CalcitList || xs instanceof CalcitSliceList) {
313
- return xs.toArray().map(toWriterNode);
334
+ return (xs.toArray() as Array<any>).map(toWriterNode);
314
335
  } else {
315
336
  throw new Error("Unexpected type for CirruWriteNode");
316
337
  }
317
338
  };
339
+
340
+ /** deep compare cirru array */
341
+ export let cirru_deep_equal = (x: CirruWriterNode, y: CirruWriterNode): boolean => {
342
+ if (x === y) {
343
+ return true;
344
+ } else if (Array.isArray(x) && Array.isArray(y)) {
345
+ if (x.length !== y.length) {
346
+ return false;
347
+ }
348
+ for (let idx = 0; idx < x.length; idx++) {
349
+ if (!cirru_deep_equal(x[idx], y[idx])) {
350
+ return false;
351
+ }
352
+ }
353
+ return true;
354
+ } else {
355
+ return false;
356
+ }
357
+ };
@@ -4,6 +4,7 @@ import { CalcitRecord } from "./js-record.mjs";
4
4
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
5
5
  import { CalcitSet as CalcitSet } from "./js-set.mjs";
6
6
  import { CalcitTuple } from "./js-tuple.mjs";
7
+ import { CalcitCirruQuote, cirru_deep_equal } from "./js-cirru.mjs";
7
8
 
8
9
  export type CalcitValue =
9
10
  | string
@@ -21,6 +22,7 @@ export type CalcitValue =
21
22
  | CalcitFn
22
23
  | CalcitRecur // should not be exposed to function
23
24
  | CalcitRecord
25
+ | CalcitCirruQuote
24
26
  | null;
25
27
 
26
28
  export let is_literal = (x: CalcitValue): boolean => {
@@ -48,6 +50,7 @@ enum PseudoTypeIndex {
48
50
  map,
49
51
  record,
50
52
  fn,
53
+ cirru_quote,
51
54
  }
52
55
 
53
56
  let typeAsInt = (x: CalcitValue): number => {
@@ -66,6 +69,7 @@ let typeAsInt = (x: CalcitValue): number => {
66
69
  if (x instanceof CalcitSet) return PseudoTypeIndex.set;
67
70
  if (x instanceof CalcitMap || x instanceof CalcitSliceMap) return PseudoTypeIndex.map;
68
71
  if (x instanceof CalcitRecord) return PseudoTypeIndex.record;
72
+ if (x instanceof CalcitCirruQuote) return PseudoTypeIndex.cirru_quote;
69
73
  // proc, fn, macro, syntax, not distinguished
70
74
  if (t === "function") return PseudoTypeIndex.fn;
71
75
  throw new Error("unknown type to compare");
@@ -101,6 +105,8 @@ export let _$n_compare = (a: CalcitValue, b: CalcitValue): number => {
101
105
  return rawCompare(a, b);
102
106
  case PseudoTypeIndex.ref:
103
107
  return rawCompare((a as CalcitRef).path, (b as CalcitRef).path);
108
+ case PseudoTypeIndex.cirru_quote:
109
+ return rawCompare(a, b); // TODO not stable
104
110
  default:
105
111
  // TODO, need more accurate solution
106
112
  if (a < b) {