@calcit/procs 0.5.29 → 0.5.32

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.
@@ -1,6 +1,6 @@
1
1
  var _a;
2
2
  // CALCIT VERSION
3
- export const calcit_version = "0.5.29";
3
+ export const calcit_version = "0.5.32";
4
4
  import "@calcit/ternary-tree";
5
5
  import { parse } from "@cirru/parser.ts";
6
6
  import { writeCirruCode } from "@cirru/writer.ts";
@@ -16,6 +16,7 @@ export * from "./js-primes.mjs";
16
16
  export * from "./js-tuple.mjs";
17
17
  export * from "./custom-formatter.mjs";
18
18
  export * from "./js-cirru.mjs";
19
+ export { _$n_compare } from "./js-primes.mjs";
19
20
  import { CalcitList, CalcitSliceList, foldl } from "./js-list.mjs";
20
21
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
21
22
  import { CalcitSet } from "./js-set.mjs";
@@ -1212,106 +1213,6 @@ export let _$n_map_$o_to_list = (m) => {
1212
1213
  throw new Error("&map:to-list expected a Map");
1213
1214
  }
1214
1215
  };
1215
- var PseudoTypeIndex;
1216
- (function (PseudoTypeIndex) {
1217
- PseudoTypeIndex[PseudoTypeIndex["nil"] = 0] = "nil";
1218
- PseudoTypeIndex[PseudoTypeIndex["bool"] = 1] = "bool";
1219
- PseudoTypeIndex[PseudoTypeIndex["number"] = 2] = "number";
1220
- PseudoTypeIndex[PseudoTypeIndex["symbol"] = 3] = "symbol";
1221
- PseudoTypeIndex[PseudoTypeIndex["keyword"] = 4] = "keyword";
1222
- PseudoTypeIndex[PseudoTypeIndex["string"] = 5] = "string";
1223
- PseudoTypeIndex[PseudoTypeIndex["ref"] = 6] = "ref";
1224
- PseudoTypeIndex[PseudoTypeIndex["tuple"] = 7] = "tuple";
1225
- PseudoTypeIndex[PseudoTypeIndex["recur"] = 8] = "recur";
1226
- PseudoTypeIndex[PseudoTypeIndex["list"] = 9] = "list";
1227
- PseudoTypeIndex[PseudoTypeIndex["set"] = 10] = "set";
1228
- PseudoTypeIndex[PseudoTypeIndex["map"] = 11] = "map";
1229
- PseudoTypeIndex[PseudoTypeIndex["record"] = 12] = "record";
1230
- PseudoTypeIndex[PseudoTypeIndex["fn"] = 13] = "fn";
1231
- })(PseudoTypeIndex || (PseudoTypeIndex = {}));
1232
- let typeAsInt = (x) => {
1233
- // based on order used in Ord traint
1234
- if (x == null)
1235
- return PseudoTypeIndex.nil;
1236
- let t = typeof x;
1237
- if (t === "boolean")
1238
- return PseudoTypeIndex.bool;
1239
- if (t === "number")
1240
- return PseudoTypeIndex.number;
1241
- if (x instanceof CalcitSymbol)
1242
- return PseudoTypeIndex.symbol;
1243
- if (x instanceof CalcitKeyword)
1244
- return PseudoTypeIndex.keyword;
1245
- if (t === "string")
1246
- return PseudoTypeIndex.string;
1247
- if (x instanceof CalcitRef)
1248
- return PseudoTypeIndex.ref;
1249
- if (x instanceof CalcitTuple)
1250
- return PseudoTypeIndex.tuple;
1251
- if (x instanceof CalcitRecur)
1252
- return PseudoTypeIndex.recur;
1253
- if (x instanceof CalcitList || x instanceof CalcitSliceList)
1254
- return PseudoTypeIndex.list;
1255
- if (x instanceof CalcitSet)
1256
- return PseudoTypeIndex.set;
1257
- if (x instanceof CalcitMap || x instanceof CalcitSliceMap)
1258
- return PseudoTypeIndex.map;
1259
- if (x instanceof CalcitRecord)
1260
- return PseudoTypeIndex.record;
1261
- // proc, fn, macro, syntax, not distinguished
1262
- if (t === "function")
1263
- return PseudoTypeIndex.fn;
1264
- throw new Error("unknown type to compare");
1265
- };
1266
- let rawCompare = (x, y) => {
1267
- if (x < y) {
1268
- return -1;
1269
- }
1270
- else if (x > y) {
1271
- return 1;
1272
- }
1273
- else {
1274
- return 0;
1275
- }
1276
- };
1277
- export let _$n_compare = (a, b) => {
1278
- if (a === b)
1279
- return 0;
1280
- let ta = typeAsInt(a);
1281
- let tb = typeAsInt(b);
1282
- if (ta === tb) {
1283
- switch (ta) {
1284
- case PseudoTypeIndex.nil:
1285
- return 0;
1286
- case PseudoTypeIndex.bool:
1287
- return rawCompare(a, b);
1288
- case PseudoTypeIndex.number:
1289
- return rawCompare(a, b);
1290
- case PseudoTypeIndex.keyword:
1291
- return a.cmp(b);
1292
- case PseudoTypeIndex.symbol:
1293
- return rawCompare(a, b);
1294
- case PseudoTypeIndex.string:
1295
- return rawCompare(a, b);
1296
- case PseudoTypeIndex.ref:
1297
- return rawCompare(a.path, b.path);
1298
- default:
1299
- // TODO, need more accurate solution
1300
- if (a < b) {
1301
- return -1;
1302
- }
1303
- else if (a > b) {
1304
- return 1;
1305
- }
1306
- else {
1307
- return 0;
1308
- }
1309
- }
1310
- }
1311
- else {
1312
- return rawCompare(ta, tb);
1313
- }
1314
- };
1315
1216
  export let _$n_map_$o_diff_new = (a, b) => {
1316
1217
  if ((a instanceof CalcitMap || a instanceof CalcitSliceMap) && (b instanceof CalcitMap || b instanceof CalcitSliceMap)) {
1317
1218
  return a.diffNew(b);
@@ -1372,6 +1273,22 @@ export let _$n_str_$o_pad_right = (s, size, pattern) => {
1372
1273
  export let _$n_get_os = () => {
1373
1274
  return kwd("js-engine");
1374
1275
  };
1276
+ export let _$n_buffer = (...xs) => {
1277
+ let buf = new Uint8Array(xs.length);
1278
+ for (let idx = 0; idx < xs.length; idx++) {
1279
+ let x = xs[idx];
1280
+ if (typeof x === "number") {
1281
+ buf[idx] = x;
1282
+ }
1283
+ else if (typeof x === "string") {
1284
+ buf[idx] = parseInt(x, 16);
1285
+ }
1286
+ else {
1287
+ throw new Error("invalid value for buffer");
1288
+ }
1289
+ }
1290
+ return buf;
1291
+ };
1375
1292
  // special procs have to be defined manually
1376
1293
  export let reduce = foldl;
1377
1294
  let unavailableProc = (...xs) => {
@@ -53,6 +53,13 @@ export let load_console_formatter_$x_ = () => {
53
53
  }
54
54
  return ret;
55
55
  }
56
+ if (obj instanceof CalcitTuple) {
57
+ let ret = ["div", {}];
58
+ ret.push(["div", { style: "display: inline-block; color: hsl(300, 100%, 40%); " }, "::"]);
59
+ ret.push(["div", { style: "margin-left: 6px; display: inline-block;" }, embedObject(obj.fst)]);
60
+ ret.push(["div", { style: "margin-left: 6px; display: inline-block;" }, embedObject(obj.snd)]);
61
+ return ret;
62
+ }
56
63
  if (obj instanceof CalcitRef) {
57
64
  return [
58
65
  "div",
@@ -95,12 +102,6 @@ export let load_console_formatter_$x_ = () => {
95
102
  }
96
103
  return ret;
97
104
  }
98
- if (obj instanceof CalcitTuple) {
99
- let ret = ["div", { style: "color: hsl(200, 90%, 60%)" }];
100
- ret.push(["div", { style: "margin-left: 8px; display: inline-block;" }, embedObject(obj.fst)]);
101
- ret.push(["div", { style: "margin-left: 8px; display: inline-block;" }, embedObject(obj.snd)]);
102
- return ret;
103
- }
104
105
  if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
105
106
  let ret = ["div", { style: "color: hsl(280, 80%, 60%)" }];
106
107
  let pairs = obj.pairs();
package/lib/js-cirru.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import "@calcit/ternary-tree";
2
2
  import { writeCirruCode } from "@cirru/writer.ts";
3
- import "./js-primes.mjs";
3
+ import { _$n_compare } from "./js-primes.mjs";
4
4
  import { CalcitList, CalcitSliceList } from "./js-list.mjs";
5
5
  import { CalcitRecord } from "./js-record.mjs";
6
6
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
@@ -45,7 +45,14 @@ export let to_cirru_edn = (x) => {
45
45
  }
46
46
  if (x instanceof CalcitMap || x instanceof CalcitSliceMap) {
47
47
  let buffer = ["{}"];
48
- for (let [k, v] of x.pairs()) {
48
+ let pairs = [];
49
+ for (let pair of x.pairs()) {
50
+ pairs.push(pair);
51
+ }
52
+ pairs.sort((a, b) => {
53
+ return _$n_compare(a[0], b[0]);
54
+ });
55
+ for (let [k, v] of pairs) {
49
56
  buffer.push([to_cirru_edn(k), to_cirru_edn(v)]);
50
57
  }
51
58
  return buffer;
@@ -60,6 +67,9 @@ export let to_cirru_edn = (x) => {
60
67
  if (x instanceof CalcitSet) {
61
68
  let buffer = ["#{}"];
62
69
  let values = x.values();
70
+ values.sort((a, b) => {
71
+ return _$n_compare(a, b);
72
+ });
63
73
  for (let idx = 0; idx < values.length; idx++) {
64
74
  let y = values[idx];
65
75
  buffer.push(to_cirru_edn(y));
package/lib/js-primes.mjs CHANGED
@@ -1,6 +1,106 @@
1
- import "./calcit-data.mjs";
2
- import "./js-list.mjs";
3
- import "./js-record.mjs";
4
- import "./js-map.mjs";
5
- import "./js-set.mjs";
6
- import "./js-tuple.mjs";
1
+ import { CalcitKeyword, CalcitSymbol, CalcitRef, CalcitRecur } from "./calcit-data.mjs";
2
+ import { CalcitList, CalcitSliceList } from "./js-list.mjs";
3
+ import { CalcitRecord } from "./js-record.mjs";
4
+ import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
5
+ import { CalcitSet as CalcitSet } from "./js-set.mjs";
6
+ import { CalcitTuple } from "./js-tuple.mjs";
7
+ var PseudoTypeIndex;
8
+ (function (PseudoTypeIndex) {
9
+ PseudoTypeIndex[PseudoTypeIndex["nil"] = 0] = "nil";
10
+ PseudoTypeIndex[PseudoTypeIndex["bool"] = 1] = "bool";
11
+ PseudoTypeIndex[PseudoTypeIndex["number"] = 2] = "number";
12
+ PseudoTypeIndex[PseudoTypeIndex["symbol"] = 3] = "symbol";
13
+ PseudoTypeIndex[PseudoTypeIndex["keyword"] = 4] = "keyword";
14
+ PseudoTypeIndex[PseudoTypeIndex["string"] = 5] = "string";
15
+ PseudoTypeIndex[PseudoTypeIndex["ref"] = 6] = "ref";
16
+ PseudoTypeIndex[PseudoTypeIndex["tuple"] = 7] = "tuple";
17
+ PseudoTypeIndex[PseudoTypeIndex["recur"] = 8] = "recur";
18
+ PseudoTypeIndex[PseudoTypeIndex["list"] = 9] = "list";
19
+ PseudoTypeIndex[PseudoTypeIndex["set"] = 10] = "set";
20
+ PseudoTypeIndex[PseudoTypeIndex["map"] = 11] = "map";
21
+ PseudoTypeIndex[PseudoTypeIndex["record"] = 12] = "record";
22
+ PseudoTypeIndex[PseudoTypeIndex["fn"] = 13] = "fn";
23
+ })(PseudoTypeIndex || (PseudoTypeIndex = {}));
24
+ let typeAsInt = (x) => {
25
+ // based on order used in Ord traint
26
+ if (x == null)
27
+ return PseudoTypeIndex.nil;
28
+ let t = typeof x;
29
+ if (t === "boolean")
30
+ return PseudoTypeIndex.bool;
31
+ if (t === "number")
32
+ return PseudoTypeIndex.number;
33
+ if (x instanceof CalcitSymbol)
34
+ return PseudoTypeIndex.symbol;
35
+ if (x instanceof CalcitKeyword)
36
+ return PseudoTypeIndex.keyword;
37
+ if (t === "string")
38
+ return PseudoTypeIndex.string;
39
+ if (x instanceof CalcitRef)
40
+ return PseudoTypeIndex.ref;
41
+ if (x instanceof CalcitTuple)
42
+ return PseudoTypeIndex.tuple;
43
+ if (x instanceof CalcitRecur)
44
+ return PseudoTypeIndex.recur;
45
+ if (x instanceof CalcitList || x instanceof CalcitSliceList)
46
+ return PseudoTypeIndex.list;
47
+ if (x instanceof CalcitSet)
48
+ return PseudoTypeIndex.set;
49
+ if (x instanceof CalcitMap || x instanceof CalcitSliceMap)
50
+ return PseudoTypeIndex.map;
51
+ if (x instanceof CalcitRecord)
52
+ return PseudoTypeIndex.record;
53
+ // proc, fn, macro, syntax, not distinguished
54
+ if (t === "function")
55
+ return PseudoTypeIndex.fn;
56
+ throw new Error("unknown type to compare");
57
+ };
58
+ let rawCompare = (x, y) => {
59
+ if (x < y) {
60
+ return -1;
61
+ }
62
+ else if (x > y) {
63
+ return 1;
64
+ }
65
+ else {
66
+ return 0;
67
+ }
68
+ };
69
+ export let _$n_compare = (a, b) => {
70
+ if (a === b)
71
+ return 0;
72
+ let ta = typeAsInt(a);
73
+ let tb = typeAsInt(b);
74
+ if (ta === tb) {
75
+ switch (ta) {
76
+ case PseudoTypeIndex.nil:
77
+ return 0;
78
+ case PseudoTypeIndex.bool:
79
+ return rawCompare(a, b);
80
+ case PseudoTypeIndex.number:
81
+ return rawCompare(a, b);
82
+ case PseudoTypeIndex.keyword:
83
+ return rawCompare(a.value, b.value);
84
+ case PseudoTypeIndex.symbol:
85
+ return rawCompare(a, b);
86
+ case PseudoTypeIndex.string:
87
+ return rawCompare(a, b);
88
+ case PseudoTypeIndex.ref:
89
+ return rawCompare(a.path, b.path);
90
+ default:
91
+ // TODO, need more accurate solution
92
+ if (a < b) {
93
+ return -1;
94
+ }
95
+ else if (a > b) {
96
+ return 1;
97
+ }
98
+ else {
99
+ return 0;
100
+ }
101
+ }
102
+ }
103
+ else {
104
+ return rawCompare(ta, tb);
105
+ }
106
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calcit/procs",
3
- "version": "0.5.29",
3
+ "version": "0.5.32",
4
4
  "main": "./lib/calcit.procs.mjs",
5
5
  "devDependencies": {
6
6
  "@types/node": "^17.0.23",
@@ -1,5 +1,5 @@
1
1
  // CALCIT VERSION
2
- export const calcit_version = "0.5.29";
2
+ export const calcit_version = "0.5.32";
3
3
 
4
4
  import { overwriteComparator, initTernaryTreeMap } from "@calcit/ternary-tree";
5
5
  import { parse, ICirruNode } from "@cirru/parser.ts";
@@ -31,6 +31,7 @@ export * from "./js-primes.mjs";
31
31
  export * from "./js-tuple.mjs";
32
32
  export * from "./custom-formatter.mjs";
33
33
  export * from "./js-cirru.mjs";
34
+ export { _$n_compare } from "./js-primes.mjs";
34
35
 
35
36
  import { CalcitList, CalcitSliceList, foldl } from "./js-list.mjs";
36
37
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
@@ -1298,89 +1299,6 @@ export let _$n_map_$o_to_list = (m: CalcitValue): CalcitSliceList => {
1298
1299
  }
1299
1300
  };
1300
1301
 
1301
- enum PseudoTypeIndex {
1302
- nil,
1303
- bool,
1304
- number,
1305
- symbol,
1306
- keyword,
1307
- string,
1308
- ref,
1309
- tuple,
1310
- recur,
1311
- list,
1312
- set,
1313
- map,
1314
- record,
1315
- fn,
1316
- }
1317
-
1318
- let typeAsInt = (x: CalcitValue): number => {
1319
- // based on order used in Ord traint
1320
- if (x == null) return PseudoTypeIndex.nil;
1321
- let t = typeof x;
1322
- if (t === "boolean") return PseudoTypeIndex.bool;
1323
- if (t === "number") return PseudoTypeIndex.number;
1324
- if (x instanceof CalcitSymbol) return PseudoTypeIndex.symbol;
1325
- if (x instanceof CalcitKeyword) return PseudoTypeIndex.keyword;
1326
- if (t === "string") return PseudoTypeIndex.string;
1327
- if (x instanceof CalcitRef) return PseudoTypeIndex.ref;
1328
- if (x instanceof CalcitTuple) return PseudoTypeIndex.tuple;
1329
- if (x instanceof CalcitRecur) return PseudoTypeIndex.recur;
1330
- if (x instanceof CalcitList || x instanceof CalcitSliceList) return PseudoTypeIndex.list;
1331
- if (x instanceof CalcitSet) return PseudoTypeIndex.set;
1332
- if (x instanceof CalcitMap || x instanceof CalcitSliceMap) return PseudoTypeIndex.map;
1333
- if (x instanceof CalcitRecord) return PseudoTypeIndex.record;
1334
- // proc, fn, macro, syntax, not distinguished
1335
- if (t === "function") return PseudoTypeIndex.fn;
1336
- throw new Error("unknown type to compare");
1337
- };
1338
-
1339
- let rawCompare = (x: any, y: any): number => {
1340
- if (x < y) {
1341
- return -1;
1342
- } else if (x > y) {
1343
- return 1;
1344
- } else {
1345
- return 0;
1346
- }
1347
- };
1348
-
1349
- export let _$n_compare = (a: CalcitValue, b: CalcitValue): number => {
1350
- if (a === b) return 0;
1351
- let ta = typeAsInt(a);
1352
- let tb = typeAsInt(b);
1353
- if (ta === tb) {
1354
- switch (ta) {
1355
- case PseudoTypeIndex.nil:
1356
- return 0;
1357
- case PseudoTypeIndex.bool:
1358
- return rawCompare(a, b);
1359
- case PseudoTypeIndex.number:
1360
- return rawCompare(a, b);
1361
- case PseudoTypeIndex.keyword:
1362
- return (a as CalcitKeyword).cmp(b as CalcitKeyword);
1363
- case PseudoTypeIndex.symbol:
1364
- return rawCompare(a, b);
1365
- case PseudoTypeIndex.string:
1366
- return rawCompare(a, b);
1367
- case PseudoTypeIndex.ref:
1368
- return rawCompare((a as CalcitRef).path, (b as CalcitRef).path);
1369
- default:
1370
- // TODO, need more accurate solution
1371
- if (a < b) {
1372
- return -1;
1373
- } else if (a > b) {
1374
- return 1;
1375
- } else {
1376
- return 0;
1377
- }
1378
- }
1379
- } else {
1380
- return rawCompare(ta, tb);
1381
- }
1382
- };
1383
-
1384
1302
  export let _$n_map_$o_diff_new = (a: CalcitValue, b: CalcitValue): CalcitMap => {
1385
1303
  if ((a instanceof CalcitMap || a instanceof CalcitSliceMap) && (b instanceof CalcitMap || b instanceof CalcitSliceMap)) {
1386
1304
  return a.diffNew(b);
@@ -1447,6 +1365,23 @@ export let _$n_get_os = (): CalcitKeyword => {
1447
1365
  return kwd("js-engine");
1448
1366
  };
1449
1367
 
1368
+ export let _$n_buffer = (...xs: CalcitValue[]): Uint8Array => {
1369
+ let buf = new Uint8Array(xs.length);
1370
+
1371
+ for (let idx = 0; idx < xs.length; idx++) {
1372
+ let x = xs[idx];
1373
+ if (typeof x === "number") {
1374
+ buf[idx] = x;
1375
+ } else if (typeof x === "string") {
1376
+ buf[idx] = parseInt(x, 16);
1377
+ } else {
1378
+ throw new Error("invalid value for buffer");
1379
+ }
1380
+ }
1381
+
1382
+ return buf;
1383
+ };
1384
+
1450
1385
  // special procs have to be defined manually
1451
1386
  export let reduce = foldl;
1452
1387
 
@@ -66,6 +66,13 @@ export let load_console_formatter_$x_ = () => {
66
66
  }
67
67
  return ret;
68
68
  }
69
+ if (obj instanceof CalcitTuple) {
70
+ let ret: any[] = ["div", {}];
71
+ ret.push(["div", { style: "display: inline-block; color: hsl(300, 100%, 40%); " }, "::"]);
72
+ ret.push(["div", { style: "margin-left: 6px; display: inline-block;" }, embedObject(obj.fst)]);
73
+ ret.push(["div", { style: "margin-left: 6px; display: inline-block;" }, embedObject(obj.snd)]);
74
+ return ret;
75
+ }
69
76
  if (obj instanceof CalcitRef) {
70
77
  return [
71
78
  "div",
@@ -110,12 +117,6 @@ export let load_console_formatter_$x_ = () => {
110
117
  }
111
118
  return ret;
112
119
  }
113
- if (obj instanceof CalcitTuple) {
114
- let ret: any[] = ["div", { style: "color: hsl(200, 90%, 60%)" }];
115
- ret.push(["div", { style: "margin-left: 8px; display: inline-block;" }, embedObject(obj.fst)]);
116
- ret.push(["div", { style: "margin-left: 8px; display: inline-block;" }, embedObject(obj.snd)]);
117
- return ret;
118
- }
119
120
  if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
120
121
  let ret: any[] = ["div", { style: "color: hsl(280, 80%, 60%)" }];
121
122
  let pairs = obj.pairs();
@@ -1,7 +1,7 @@
1
1
  import { overwriteComparator, initTernaryTreeMap } from "@calcit/ternary-tree";
2
2
  import { CirruWriterNode, writeCirruCode } from "@cirru/writer.ts";
3
3
 
4
- import { CalcitValue } from "./js-primes.mjs";
4
+ import { CalcitValue, _$n_compare } from "./js-primes.mjs";
5
5
  import { CalcitList, CalcitSliceList } from "./js-list.mjs";
6
6
  import { CalcitRecord } from "./js-record.mjs";
7
7
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
@@ -50,7 +50,14 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
50
50
  }
51
51
  if (x instanceof CalcitMap || x instanceof CalcitSliceMap) {
52
52
  let buffer: CirruEdnFormat = ["{}"];
53
- for (let [k, v] of x.pairs()) {
53
+ let pairs: [CalcitValue, CalcitValue][] = [];
54
+ for (let pair of x.pairs()) {
55
+ pairs.push(pair);
56
+ }
57
+ pairs.sort((a, b) => {
58
+ return _$n_compare(a[0], b[0]);
59
+ });
60
+ for (let [k, v] of pairs) {
54
61
  buffer.push([to_cirru_edn(k), to_cirru_edn(v)]);
55
62
  }
56
63
  return buffer;
@@ -65,6 +72,9 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
65
72
  if (x instanceof CalcitSet) {
66
73
  let buffer: CirruEdnFormat = ["#{}"];
67
74
  let values = x.values();
75
+ values.sort((a, b) => {
76
+ return _$n_compare(a, b);
77
+ });
68
78
  for (let idx = 0; idx < values.length; idx++) {
69
79
  let y = values[idx];
70
80
  buffer.push(to_cirru_edn(y));
@@ -22,3 +22,86 @@ export type CalcitValue =
22
22
  | CalcitRecur // should not be exposed to function
23
23
  | CalcitRecord
24
24
  | null;
25
+
26
+ enum PseudoTypeIndex {
27
+ nil,
28
+ bool,
29
+ number,
30
+ symbol,
31
+ keyword,
32
+ string,
33
+ ref,
34
+ tuple,
35
+ recur,
36
+ list,
37
+ set,
38
+ map,
39
+ record,
40
+ fn,
41
+ }
42
+
43
+ let typeAsInt = (x: CalcitValue): number => {
44
+ // based on order used in Ord traint
45
+ if (x == null) return PseudoTypeIndex.nil;
46
+ let t = typeof x;
47
+ if (t === "boolean") return PseudoTypeIndex.bool;
48
+ if (t === "number") return PseudoTypeIndex.number;
49
+ if (x instanceof CalcitSymbol) return PseudoTypeIndex.symbol;
50
+ if (x instanceof CalcitKeyword) return PseudoTypeIndex.keyword;
51
+ if (t === "string") return PseudoTypeIndex.string;
52
+ if (x instanceof CalcitRef) return PseudoTypeIndex.ref;
53
+ if (x instanceof CalcitTuple) return PseudoTypeIndex.tuple;
54
+ if (x instanceof CalcitRecur) return PseudoTypeIndex.recur;
55
+ if (x instanceof CalcitList || x instanceof CalcitSliceList) return PseudoTypeIndex.list;
56
+ if (x instanceof CalcitSet) return PseudoTypeIndex.set;
57
+ if (x instanceof CalcitMap || x instanceof CalcitSliceMap) return PseudoTypeIndex.map;
58
+ if (x instanceof CalcitRecord) return PseudoTypeIndex.record;
59
+ // proc, fn, macro, syntax, not distinguished
60
+ if (t === "function") return PseudoTypeIndex.fn;
61
+ throw new Error("unknown type to compare");
62
+ };
63
+
64
+ let rawCompare = (x: any, y: any): number => {
65
+ if (x < y) {
66
+ return -1;
67
+ } else if (x > y) {
68
+ return 1;
69
+ } else {
70
+ return 0;
71
+ }
72
+ };
73
+
74
+ export let _$n_compare = (a: CalcitValue, b: CalcitValue): number => {
75
+ if (a === b) return 0;
76
+ let ta = typeAsInt(a);
77
+ let tb = typeAsInt(b);
78
+ if (ta === tb) {
79
+ switch (ta) {
80
+ case PseudoTypeIndex.nil:
81
+ return 0;
82
+ case PseudoTypeIndex.bool:
83
+ return rawCompare(a, b);
84
+ case PseudoTypeIndex.number:
85
+ return rawCompare(a, b);
86
+ case PseudoTypeIndex.keyword:
87
+ return rawCompare((a as CalcitKeyword).value, (b as CalcitKeyword).value);
88
+ case PseudoTypeIndex.symbol:
89
+ return rawCompare(a, b);
90
+ case PseudoTypeIndex.string:
91
+ return rawCompare(a, b);
92
+ case PseudoTypeIndex.ref:
93
+ return rawCompare((a as CalcitRef).path, (b as CalcitRef).path);
94
+ default:
95
+ // TODO, need more accurate solution
96
+ if (a < b) {
97
+ return -1;
98
+ } else if (a > b) {
99
+ return 1;
100
+ } else {
101
+ return 0;
102
+ }
103
+ }
104
+ } else {
105
+ return rawCompare(ta, tb);
106
+ }
107
+ };