@calcit/procs 0.7.12 → 0.7.14

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.7.12";
3
+ export const calcit_version = "0.7.14";
4
4
  import { parse } from "@cirru/parser.ts";
5
5
  import { writeCirruCode } from "@cirru/writer.ts";
6
6
  import { CalcitSymbol, CalcitTag, CalcitRef, CalcitRecur, newTag, refsRegistry, toString, getStringName, _$n__$e_, hashFunction, } from "./calcit-data.mjs";
@@ -316,6 +316,20 @@ export let _$n_record_$o_assoc = function (xs, k, v) {
316
316
  return xs.assoc(k, v);
317
317
  throw new Error("record `assoc` expected a record");
318
318
  };
319
+ export let _$n_record_$o_class = function (xs) {
320
+ if (arguments.length !== 1)
321
+ throw new Error("&record:class takes 1 argument");
322
+ if (xs instanceof CalcitRecord)
323
+ return xs.klass;
324
+ throw new Error("&record:class expected a record");
325
+ };
326
+ export let _$n_record_$o_with_class = function (xs, k) {
327
+ if (arguments.length !== 2)
328
+ throw new Error("&record:with-class takes 2 arguments");
329
+ if (xs instanceof CalcitRecord)
330
+ return xs.withClass(k);
331
+ throw new Error("&record:with-class expected a record");
332
+ };
319
333
  export let _$n_list_$o_assoc_before = function (xs, k, v) {
320
334
  if (arguments.length !== 3) {
321
335
  throw new Error("assoc takes 3 arguments");
@@ -1082,8 +1096,8 @@ export let parse_cirru = (code) => {
1082
1096
  export let parse_cirru_list = (code) => {
1083
1097
  return to_calcit_data(parse(code), true);
1084
1098
  };
1085
- export let parse_cirru_edn = (code) => {
1086
- return extract_cirru_edn(parse(code)[0]);
1099
+ export let parse_cirru_edn = (code, options) => {
1100
+ return extract_cirru_edn(parse(code)[0], options);
1087
1101
  };
1088
1102
  export let format_to_lisp = (x) => {
1089
1103
  if (x == null) {
@@ -1175,7 +1189,6 @@ let calcit_builtin_classes = {
1175
1189
  set: null,
1176
1190
  list: null,
1177
1191
  map: null,
1178
- record: null,
1179
1192
  nil: null,
1180
1193
  fn: null,
1181
1194
  };
@@ -1206,6 +1219,15 @@ export function invoke_method(p, obj, ...args) {
1206
1219
  throw new Error("Method invoking expected a record as class");
1207
1220
  }
1208
1221
  }
1222
+ else if (obj instanceof CalcitRecord) {
1223
+ if (obj.klass instanceof CalcitRecord) {
1224
+ tag = obj.name.toString();
1225
+ klass = obj.klass;
1226
+ }
1227
+ else {
1228
+ throw new Error("Method invoking expected a record as class");
1229
+ }
1230
+ }
1209
1231
  else if (typeof obj === "number") {
1210
1232
  tag = "&core-number-class";
1211
1233
  klass = calcit_builtin_classes.number;
@@ -1226,10 +1248,6 @@ export function invoke_method(p, obj, ...args) {
1226
1248
  tag = "&core-list-class";
1227
1249
  klass = calcit_builtin_classes.list;
1228
1250
  }
1229
- else if (obj instanceof CalcitRecord) {
1230
- tag = "&core-record-class";
1231
- klass = calcit_builtin_classes.record;
1232
- }
1233
1251
  else if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
1234
1252
  tag = "&core-map-class";
1235
1253
  klass = calcit_builtin_classes.map;
package/lib/js-cirru.mjs CHANGED
@@ -6,6 +6,7 @@ import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
6
6
  import { CalcitSet } from "./js-set.mjs";
7
7
  import { CalcitTag, CalcitSymbol, CalcitRecur, CalcitRef, newTag } from "./calcit-data.mjs";
8
8
  import { CalcitTuple } from "./js-tuple.mjs";
9
+ import { deepEqual } from "@calcit/ternary-tree/lib/utils.mjs";
9
10
  export class CalcitCirruQuote {
10
11
  constructor(value) {
11
12
  this.value = value;
@@ -149,7 +150,7 @@ let extractFieldTag = (x) => {
149
150
  return newTag(x);
150
151
  }
151
152
  };
152
- export let extract_cirru_edn = (x) => {
153
+ export let extract_cirru_edn = (x, options) => {
153
154
  if (typeof x === "string") {
154
155
  if (x === "nil") {
155
156
  return null;
@@ -190,7 +191,7 @@ export let extract_cirru_edn = (x) => {
190
191
  return; // skip first `{}` symbol
191
192
  }
192
193
  if (pair instanceof Array && pair.length === 2) {
193
- result.push(extract_cirru_edn(pair[0]), extract_cirru_edn(pair[1]));
194
+ result.push(extract_cirru_edn(pair[0], options), extract_cirru_edn(pair[1], options));
194
195
  }
195
196
  else {
196
197
  throw new Error("Expected pairs for map");
@@ -211,7 +212,7 @@ export let extract_cirru_edn = (x) => {
211
212
  }
212
213
  if (pair instanceof Array && pair.length === 2) {
213
214
  if (typeof pair[0] === "string") {
214
- entries.push([extractFieldTag(pair[0]), extract_cirru_edn(pair[1])]);
215
+ entries.push([extractFieldTag(pair[0]), extract_cirru_edn(pair[1], options)]);
215
216
  }
216
217
  else {
217
218
  throw new Error("Expected string as field");
@@ -230,16 +231,25 @@ export let extract_cirru_edn = (x) => {
230
231
  fields.push(entries[idx][0]);
231
232
  values.push(entries[idx][1]);
232
233
  }
234
+ if (options instanceof CalcitMap || options instanceof CalcitSliceMap) {
235
+ let v = options.get(extractFieldTag(name));
236
+ if (v != null && v instanceof CalcitRecord) {
237
+ if (!deepEqual(v.fields, fields)) {
238
+ throw new Error(`Fields mismatch for ${name}, expected ${fields}, got ${v.fields}`);
239
+ }
240
+ return new CalcitRecord(extractFieldTag(name), fields, values, v.klass);
241
+ }
242
+ }
233
243
  return new CalcitRecord(extractFieldTag(name), fields, values);
234
244
  }
235
245
  if (x[0] === "[]") {
236
- return new CalcitSliceList(x.slice(1).map(extract_cirru_edn));
246
+ return new CalcitSliceList(x.slice(1).map((x) => extract_cirru_edn(x, options)));
237
247
  }
238
248
  if (x[0] === "#{}") {
239
- return new CalcitSet(x.slice(1).map(extract_cirru_edn));
249
+ return new CalcitSet(x.slice(1).map((x) => extract_cirru_edn(x, options)));
240
250
  }
241
251
  if (x[0] === "do" && x.length === 2) {
242
- return extract_cirru_edn(x[1]);
252
+ return extract_cirru_edn(x[1], options);
243
253
  }
244
254
  if (x[0] === "quote") {
245
255
  if (x.length !== 2) {
@@ -252,7 +262,7 @@ export let extract_cirru_edn = (x) => {
252
262
  throw new Error("tuple expects at least 1 value1");
253
263
  }
254
264
  let baseClass = new CalcitRecord(newTag("base-class"), [], []);
255
- return new CalcitTuple(extract_cirru_edn(x[1]), x.slice(2).map(extract_cirru_edn), baseClass);
265
+ return new CalcitTuple(extract_cirru_edn(x[1], options), x.slice(2).map((x) => extract_cirru_edn(x, options)), baseClass);
256
266
  }
257
267
  }
258
268
  console.error(x);
package/lib/js-record.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { castTag, toString, findInFields } from "./calcit-data.mjs";
2
2
  import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
3
3
  export class CalcitRecord {
4
- constructor(name, fields, values) {
4
+ constructor(name, fields, values, klass) {
5
5
  this.name = name;
6
6
  let fieldNames = fields.map(castTag);
7
7
  this.fields = fields;
@@ -15,6 +15,7 @@ export class CalcitRecord {
15
15
  this.values = new Array(fieldNames.length);
16
16
  }
17
17
  this.cachedHash = null;
18
+ this.klass = klass;
18
19
  }
19
20
  get(k) {
20
21
  let field = castTag(k);
@@ -37,7 +38,7 @@ export class CalcitRecord {
37
38
  values[idx] = this.values[idx];
38
39
  }
39
40
  }
40
- return new CalcitRecord(this.name, this.fields, values);
41
+ return new CalcitRecord(this.name, this.fields, values, this.klass);
41
42
  }
42
43
  /** return -1 for missing */
43
44
  findIndex(k) {
@@ -56,6 +57,14 @@ export class CalcitRecord {
56
57
  }
57
58
  return ret + ")";
58
59
  }
60
+ withClass(klass) {
61
+ if (klass instanceof CalcitRecord) {
62
+ return new CalcitRecord(this.name, this.fields, this.values, klass);
63
+ }
64
+ else {
65
+ throw new Error("Expected a record");
66
+ }
67
+ }
59
68
  }
60
69
  export let new_record = (name, ...fields) => {
61
70
  let fieldNames = fields.map(castTag).sort((x, y) => {
@@ -71,6 +80,20 @@ export let new_record = (name, ...fields) => {
71
80
  });
72
81
  return new CalcitRecord(castTag(name), fieldNames);
73
82
  };
83
+ export let new_class_record = (klass, name, ...fields) => {
84
+ let fieldNames = fields.map(castTag).sort((x, y) => {
85
+ if (x.idx < y.idx) {
86
+ return -1;
87
+ }
88
+ else if (x.idx > y.idx) {
89
+ return 1;
90
+ }
91
+ else {
92
+ throw new Error(`Unexpected duplication in record fields: ${x.toString()}`);
93
+ }
94
+ });
95
+ return new CalcitRecord(castTag(name), fieldNames, undefined, klass);
96
+ };
74
97
  export let fieldsEqual = (xs, ys) => {
75
98
  if (xs === ys) {
76
99
  return true; // special case, referential equal
@@ -111,7 +134,7 @@ export let _$n__PCT__$M_ = (proto, ...xs) => {
111
134
  }
112
135
  values[i] = xs[idx * 2 + 1];
113
136
  }
114
- return new CalcitRecord(proto.name, proto.fields, values);
137
+ return new CalcitRecord(proto.name, proto.fields, values, proto.klass);
115
138
  }
116
139
  else {
117
140
  throw new Error("Expected prototype to be a record");
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@calcit/procs",
3
- "version": "0.7.12",
3
+ "version": "0.7.14",
4
4
  "main": "./lib/calcit.procs.mjs",
5
5
  "devDependencies": {
6
- "@types/node": "^20.4.6",
6
+ "@types/node": "^20.4.8",
7
7
  "typescript": "^5.1.6"
8
8
  },
9
9
  "scripts": {
@@ -11,6 +11,7 @@
11
11
  "procs-link": "ln -s ../../ node_modules/@calcit/procs",
12
12
  "cp-mac": "cargo build --release && rm -rfv builds/* && node scripts/cp-version.js && scp builds/* rsync-user@calcit-lang.org:/web-assets/repo/calcit-lang/binaries/macos/",
13
13
  "try-rs": "cargo run --bin cr -- calcit/test.cirru -1",
14
+ "try-js-brk": "cargo run --bin cr -- calcit/test.cirru --emit-js -1 && node js-out/main.mjs",
14
15
  "try-js": "cargo run --bin cr -- calcit/test.cirru --emit-js -1 && node js-out/main.mjs"
15
16
  },
16
17
  "repository": {
@@ -18,7 +19,7 @@
18
19
  "url": "https://github.com/calcit-lang/calcit"
19
20
  },
20
21
  "dependencies": {
21
- "@calcit/ternary-tree": "0.0.22",
22
+ "@calcit/ternary-tree": "0.0.23",
22
23
  "@cirru/parser.ts": "^0.0.6",
23
24
  "@cirru/writer.ts": "^0.1.4"
24
25
  }
@@ -1,5 +1,5 @@
1
1
  // CALCIT VERSION
2
- export const calcit_version = "0.7.12";
2
+ export const calcit_version = "0.7.14";
3
3
 
4
4
  import { parse, ICirruNode } from "@cirru/parser.ts";
5
5
  import { writeCirruCode } from "@cirru/writer.ts";
@@ -364,6 +364,18 @@ export let _$n_record_$o_assoc = function (xs: CalcitValue, k: CalcitValue, v: C
364
364
  throw new Error("record `assoc` expected a record");
365
365
  };
366
366
 
367
+ export let _$n_record_$o_class = function (xs: CalcitValue) {
368
+ if (arguments.length !== 1) throw new Error("&record:class takes 1 argument");
369
+ if (xs instanceof CalcitRecord) return xs.klass;
370
+ throw new Error("&record:class expected a record");
371
+ };
372
+
373
+ export let _$n_record_$o_with_class = function (xs: CalcitValue, k: CalcitValue) {
374
+ if (arguments.length !== 2) throw new Error("&record:with-class takes 2 arguments");
375
+ if (xs instanceof CalcitRecord) return xs.withClass(k);
376
+ throw new Error("&record:with-class expected a record");
377
+ };
378
+
367
379
  export let _$n_list_$o_assoc_before = function (xs: CalcitList | CalcitSliceList, k: number, v: CalcitValue): CalcitList {
368
380
  if (arguments.length !== 3) {
369
381
  throw new Error("assoc takes 3 arguments");
@@ -1185,8 +1197,8 @@ export let parse_cirru_list = (code: string): CalcitList => {
1185
1197
  return to_calcit_data(parse(code), true) as CalcitList;
1186
1198
  };
1187
1199
 
1188
- export let parse_cirru_edn = (code: string) => {
1189
- return extract_cirru_edn(parse(code)[0]);
1200
+ export let parse_cirru_edn = (code: string, options: CalcitValue) => {
1201
+ return extract_cirru_edn(parse(code)[0], options);
1190
1202
  };
1191
1203
 
1192
1204
  export let format_to_lisp = (x: CalcitValue): string => {
@@ -1276,7 +1288,6 @@ let calcit_builtin_classes = {
1276
1288
  set: null as CalcitRecord,
1277
1289
  list: null as CalcitRecord,
1278
1290
  map: null as CalcitRecord,
1279
- record: null as CalcitRecord,
1280
1291
  nil: null as CalcitRecord,
1281
1292
  fn: null as CalcitRecord,
1282
1293
  };
@@ -1307,6 +1318,13 @@ export function invoke_method(p: string, obj: CalcitValue, ...args: CalcitValue[
1307
1318
  } else {
1308
1319
  throw new Error("Method invoking expected a record as class");
1309
1320
  }
1321
+ } else if (obj instanceof CalcitRecord) {
1322
+ if (obj.klass instanceof CalcitRecord) {
1323
+ tag = obj.name.toString();
1324
+ klass = obj.klass;
1325
+ } else {
1326
+ throw new Error("Method invoking expected a record as class");
1327
+ }
1310
1328
  } else if (typeof obj === "number") {
1311
1329
  tag = "&core-number-class";
1312
1330
  klass = calcit_builtin_classes.number;
@@ -1322,9 +1340,6 @@ export function invoke_method(p: string, obj: CalcitValue, ...args: CalcitValue[
1322
1340
  } else if (obj instanceof CalcitList || obj instanceof CalcitSliceList) {
1323
1341
  tag = "&core-list-class";
1324
1342
  klass = calcit_builtin_classes.list;
1325
- } else if (obj instanceof CalcitRecord) {
1326
- tag = "&core-record-class";
1327
- klass = calcit_builtin_classes.record;
1328
1343
  } else if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
1329
1344
  tag = "&core-map-class";
1330
1345
  klass = calcit_builtin_classes.map;
@@ -8,6 +8,7 @@ import { CalcitMap, CalcitSliceMap } from "./js-map.mjs";
8
8
  import { CalcitSet } from "./js-set.mjs";
9
9
  import { CalcitTag, CalcitSymbol, CalcitRecur, CalcitRef, newTag } from "./calcit-data.mjs";
10
10
  import { CalcitTuple } from "./js-tuple.mjs";
11
+ import { deepEqual } from "@calcit/ternary-tree/lib/utils.mjs";
11
12
 
12
13
  type CirruEdnFormat = string | CirruEdnFormat[];
13
14
 
@@ -150,7 +151,7 @@ let extractFieldTag = (x: string) => {
150
151
  }
151
152
  };
152
153
 
153
- export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
154
+ export let extract_cirru_edn = (x: CirruEdnFormat, options: CalcitValue): CalcitValue => {
154
155
  if (typeof x === "string") {
155
156
  if (x === "nil") {
156
157
  return null;
@@ -191,7 +192,7 @@ export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
191
192
  return; // skip first `{}` symbol
192
193
  }
193
194
  if (pair instanceof Array && pair.length === 2) {
194
- result.push(extract_cirru_edn(pair[0]), extract_cirru_edn(pair[1]));
195
+ result.push(extract_cirru_edn(pair[0], options), extract_cirru_edn(pair[1], options));
195
196
  } else {
196
197
  throw new Error("Expected pairs for map");
197
198
  }
@@ -212,7 +213,7 @@ export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
212
213
 
213
214
  if (pair instanceof Array && pair.length === 2) {
214
215
  if (typeof pair[0] === "string") {
215
- entries.push([extractFieldTag(pair[0]), extract_cirru_edn(pair[1])]);
216
+ entries.push([extractFieldTag(pair[0]), extract_cirru_edn(pair[1], options)]);
216
217
  } else {
217
218
  throw new Error("Expected string as field");
218
219
  }
@@ -230,16 +231,27 @@ export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
230
231
  fields.push(entries[idx][0]);
231
232
  values.push(entries[idx][1]);
232
233
  }
234
+
235
+ if (options instanceof CalcitMap || options instanceof CalcitSliceMap) {
236
+ let v = options.get(extractFieldTag(name));
237
+ if (v != null && v instanceof CalcitRecord) {
238
+ if (!deepEqual(v.fields, fields)) {
239
+ throw new Error(`Fields mismatch for ${name}, expected ${fields}, got ${v.fields}`);
240
+ }
241
+ return new CalcitRecord(extractFieldTag(name), fields, values, v.klass);
242
+ }
243
+ }
244
+
233
245
  return new CalcitRecord(extractFieldTag(name), fields, values);
234
246
  }
235
247
  if (x[0] === "[]") {
236
- return new CalcitSliceList(x.slice(1).map(extract_cirru_edn));
248
+ return new CalcitSliceList(x.slice(1).map((x) => extract_cirru_edn(x, options)));
237
249
  }
238
250
  if (x[0] === "#{}") {
239
- return new CalcitSet(x.slice(1).map(extract_cirru_edn));
251
+ return new CalcitSet(x.slice(1).map((x) => extract_cirru_edn(x, options)));
240
252
  }
241
253
  if (x[0] === "do" && x.length === 2) {
242
- return extract_cirru_edn(x[1]);
254
+ return extract_cirru_edn(x[1], options);
243
255
  }
244
256
  if (x[0] === "quote") {
245
257
  if (x.length !== 2) {
@@ -252,7 +264,11 @@ export let extract_cirru_edn = (x: CirruEdnFormat): CalcitValue => {
252
264
  throw new Error("tuple expects at least 1 value1");
253
265
  }
254
266
  let baseClass = new CalcitRecord(newTag("base-class"), [], []);
255
- return new CalcitTuple(extract_cirru_edn(x[1]), x.slice(2).map(extract_cirru_edn), baseClass);
267
+ return new CalcitTuple(
268
+ extract_cirru_edn(x[1], options),
269
+ x.slice(2).map((x) => extract_cirru_edn(x, options)),
270
+ baseClass
271
+ );
256
272
  }
257
273
  }
258
274
  console.error(x);
@@ -8,8 +8,9 @@ export class CalcitRecord {
8
8
  name: CalcitTag;
9
9
  fields: Array<CalcitTag>;
10
10
  values: Array<CalcitValue>;
11
+ klass: CalcitValue;
11
12
  cachedHash: Hash;
12
- constructor(name: CalcitTag, fields: Array<CalcitTag>, values?: Array<CalcitValue>) {
13
+ constructor(name: CalcitTag, fields: Array<CalcitTag>, values?: Array<CalcitValue>, klass?: CalcitValue) {
13
14
  this.name = name;
14
15
  let fieldNames = fields.map(castTag);
15
16
  this.fields = fields;
@@ -22,6 +23,7 @@ export class CalcitRecord {
22
23
  this.values = new Array(fieldNames.length);
23
24
  }
24
25
  this.cachedHash = null;
26
+ this.klass = klass;
25
27
  }
26
28
  get(k: CalcitValue) {
27
29
  let field = castTag(k);
@@ -42,7 +44,7 @@ export class CalcitRecord {
42
44
  values[idx] = this.values[idx];
43
45
  }
44
46
  }
45
- return new CalcitRecord(this.name, this.fields, values);
47
+ return new CalcitRecord(this.name, this.fields, values, this.klass);
46
48
  }
47
49
  /** return -1 for missing */
48
50
  findIndex(k: CalcitValue) {
@@ -61,6 +63,13 @@ export class CalcitRecord {
61
63
  }
62
64
  return ret + ")";
63
65
  }
66
+ withClass(klass: CalcitValue): CalcitRecord {
67
+ if (klass instanceof CalcitRecord) {
68
+ return new CalcitRecord(this.name, this.fields, this.values, klass);
69
+ } else {
70
+ throw new Error("Expected a record");
71
+ }
72
+ }
64
73
  }
65
74
 
66
75
  export let new_record = (name: CalcitValue, ...fields: Array<CalcitValue>): CalcitValue => {
@@ -76,6 +85,19 @@ export let new_record = (name: CalcitValue, ...fields: Array<CalcitValue>): Calc
76
85
  return new CalcitRecord(castTag(name), fieldNames);
77
86
  };
78
87
 
88
+ export let new_class_record = (klass: CalcitRecord, name: CalcitValue, ...fields: Array<CalcitValue>): CalcitValue => {
89
+ let fieldNames = fields.map(castTag).sort((x, y) => {
90
+ if (x.idx < y.idx) {
91
+ return -1;
92
+ } else if (x.idx > y.idx) {
93
+ return 1;
94
+ } else {
95
+ throw new Error(`Unexpected duplication in record fields: ${x.toString()}`);
96
+ }
97
+ });
98
+ return new CalcitRecord(castTag(name), fieldNames, undefined, klass);
99
+ };
100
+
79
101
  export let fieldsEqual = (xs: Array<CalcitTag>, ys: Array<CalcitTag>): boolean => {
80
102
  if (xs === ys) {
81
103
  return true; // special case, referential equal
@@ -121,7 +143,7 @@ export let _$n__PCT__$M_ = (proto: CalcitValue, ...xs: Array<CalcitValue>): Calc
121
143
  values[i] = xs[idx * 2 + 1];
122
144
  }
123
145
 
124
- return new CalcitRecord(proto.name, proto.fields, values);
146
+ return new CalcitRecord(proto.name, proto.fields, values, proto.klass);
125
147
  } else {
126
148
  throw new Error("Expected prototype to be a record");
127
149
  }