aontu 0.5.0 → 0.7.0

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.
Files changed (65) hide show
  1. package/aontu.ts +3 -2
  2. package/dist/aontu.d.ts +2 -2
  3. package/dist/aontu.js +3 -4
  4. package/dist/aontu.js.map +1 -1
  5. package/dist/aontu.min.js +1 -63
  6. package/dist/lib/lang.d.ts +1 -3
  7. package/dist/lib/lang.js +53 -25
  8. package/dist/lib/lang.js.map +1 -1
  9. package/dist/lib/op/disjunct.js +10 -10
  10. package/dist/lib/op/disjunct.js.map +1 -1
  11. package/dist/lib/op/op.d.ts +2 -2
  12. package/dist/lib/op/op.js.map +1 -1
  13. package/dist/lib/op/unite.js +19 -14
  14. package/dist/lib/op/unite.js.map +1 -1
  15. package/dist/lib/type.d.ts +34 -22
  16. package/dist/lib/type.js +23 -76
  17. package/dist/lib/type.js.map +1 -1
  18. package/dist/lib/unify.d.ts +5 -2
  19. package/dist/lib/unify.js +7 -5
  20. package/dist/lib/unify.js.map +1 -1
  21. package/dist/lib/val/ConjunctVal.d.ts +12 -0
  22. package/dist/lib/val/ConjunctVal.js +191 -0
  23. package/dist/lib/val/ConjunctVal.js.map +1 -0
  24. package/dist/lib/val/DisjunctVal.d.ts +12 -0
  25. package/dist/lib/val/DisjunctVal.js +85 -0
  26. package/dist/lib/val/DisjunctVal.js.map +1 -0
  27. package/dist/lib/val/ListVal.d.ts +14 -0
  28. package/dist/lib/val/ListVal.js +91 -0
  29. package/dist/lib/val/ListVal.js.map +1 -0
  30. package/dist/lib/val/MapVal.d.ts +14 -0
  31. package/dist/lib/val/MapVal.js +141 -0
  32. package/dist/lib/val/MapVal.js.map +1 -0
  33. package/dist/lib/val/Nil.d.ts +15 -0
  34. package/dist/lib/val/Nil.js +54 -0
  35. package/dist/lib/val/Nil.js.map +1 -0
  36. package/dist/lib/val/PrefVal.d.ts +12 -0
  37. package/dist/lib/val/PrefVal.js +60 -0
  38. package/dist/lib/val/PrefVal.js.map +1 -0
  39. package/dist/lib/val/RefVal.d.ts +15 -0
  40. package/dist/lib/val/RefVal.js +73 -0
  41. package/dist/lib/val/RefVal.js.map +1 -0
  42. package/dist/lib/val/ValBase.d.ts +22 -0
  43. package/dist/lib/val/ValBase.js +27 -0
  44. package/dist/lib/val/ValBase.js.map +1 -0
  45. package/dist/lib/val.d.ts +6 -93
  46. package/dist/lib/val.js +13 -609
  47. package/dist/lib/val.js.map +1 -1
  48. package/lib/lang.ts +79 -49
  49. package/lib/op/disjunct.ts +14 -8
  50. package/lib/op/op.ts +2 -1
  51. package/lib/op/unite.ts +14 -12
  52. package/lib/type.ts +108 -0
  53. package/lib/unify.ts +10 -8
  54. package/lib/val/ConjunctVal.ts +284 -0
  55. package/lib/val/DisjunctVal.ts +145 -0
  56. package/lib/val/ListVal.ts +154 -0
  57. package/lib/val/MapVal.ts +226 -0
  58. package/lib/val/Nil.ts +94 -0
  59. package/lib/val/PrefVal.ts +113 -0
  60. package/lib/val/RefVal.ts +126 -0
  61. package/lib/val/RefVal.ts~ +319 -0
  62. package/lib/val/ValBase.ts +78 -0
  63. package/lib/val.ts +15 -825
  64. package/package.json +27 -24
  65. package/lib/common.ts +0 -19
package/dist/lib/val.js CHANGED
@@ -1,124 +1,19 @@
1
1
  "use strict";
2
2
  /* Copyright (c) 2021 Richard Rodger, MIT License */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.PrefVal = exports.RefVal = exports.DisjunctVal = exports.ConjunctVal = exports.ListVal = exports.MapVal = exports.IntegerVal = exports.BooleanVal = exports.StringVal = exports.NumberVal = exports.ScalarTypeVal = exports.Nil = exports.TOP = exports.Val = exports.Integer = exports.DONE = void 0;
5
- // TODO: infinite recursion protection
6
- // NOTES
7
- // - Vals are immutable
8
- // - each Val must handle all parent and child unifications explicitly
9
- // - performance is not considered yet
10
- /*
11
-
12
- TOP -> Scalar/Boolean -> BooleanVal
13
- -> Scalar/String -> StringVal
14
- -> Scalar/Number -> NumberVal -> IntegerVal
15
- -> Scalar/Integer
16
- -> Scalar/Integer -> IntegerVal
17
-
18
- */
4
+ exports.IntegerVal = exports.BooleanVal = exports.StringVal = exports.NumberVal = exports.ScalarTypeVal = exports.Integer = void 0;
5
+ const type_1 = require("./type");
19
6
  const unify_1 = require("./unify");
20
- const lang_1 = require("./lang");
21
- const op_1 = require("./op/op");
22
- const DONE = -1;
23
- exports.DONE = DONE;
24
- // There can be only one.
25
- const TOP = {
26
- id: 0,
27
- top: true,
28
- peg: undefined,
29
- done: DONE,
30
- path: [],
31
- row: -1,
32
- col: -1,
33
- url: '',
34
- unify(peer, _ctx) {
35
- return peer;
36
- },
37
- get canon() { return 'top'; },
38
- get site() { return new lang_1.Site(this); },
39
- same(peer) {
40
- return TOP === peer;
41
- },
42
- gen: (_ctx) => {
43
- return undefined;
44
- },
45
- };
46
- exports.TOP = TOP;
47
- // TODO: extends Val ???
48
- class Val {
49
- constructor(peg, ctx) {
50
- this.done = 0;
51
- this.row = -1;
52
- this.col = -1;
53
- this.url = '';
54
- this.peg = peg;
55
- this.path = (ctx && ctx.path) || [];
56
- this.id = (ctx && ctx.vc++) || (9e9 + Math.floor(Math.random() * (1e9)));
57
- }
58
- same(peer) {
59
- // return this === peer
60
- return null == peer ? false : this.id === peer.id;
61
- }
62
- get site() {
63
- return new lang_1.Site(this);
64
- }
65
- }
66
- exports.Val = Val;
67
- class Nil extends Val {
68
- constructor(why, ctx) {
69
- super(null, ctx);
70
- this.nil = true;
71
- this.why = why;
72
- // Nil is always DONE, by definition.
73
- this.done = DONE;
74
- }
75
- unify(_peer, _ctx) {
76
- return this;
77
- }
78
- get canon() {
79
- return 'nil';
80
- }
81
- gen(_ctx) {
82
- return undefined;
83
- }
84
- }
85
- exports.Nil = Nil;
86
- // TODO: include Val generating nil, thus capture type
87
- Nil.make = (ctx, why, av, bv) => {
88
- let nil = new Nil(why, ctx);
89
- // TODO: this should be done lazily, for multiple terms
90
- // Terms later in same file are considered the primary error location.
91
- if (null != av) {
92
- nil.row = av.row;
93
- nil.col = av.col;
94
- nil.url = av.url;
95
- nil.primary = av;
96
- if (null != bv) {
97
- nil.secondary = bv;
98
- let bv_loc_wins = (nil.url === bv.url) && ((nil.row < bv.row) ||
99
- (nil.row === bv.row && nil.col < bv.col));
100
- if (bv_loc_wins) {
101
- nil.row = bv.row;
102
- nil.col = bv.col;
103
- nil.url = bv.url;
104
- nil.primary = bv;
105
- nil.secondary = av;
106
- }
107
- }
108
- }
109
- if (ctx) {
110
- ctx.err.push(nil);
111
- }
112
- return nil;
113
- };
7
+ const Nil_1 = require("./val/Nil");
8
+ const ValBase_1 = require("./val/ValBase");
114
9
  // A ScalarType for integers. Number includes floats.
115
10
  class Integer {
116
11
  }
117
12
  exports.Integer = Integer;
118
- class ScalarTypeVal extends Val {
13
+ class ScalarTypeVal extends ValBase_1.ValBase {
119
14
  constructor(peg, ctx) {
120
15
  super(peg, ctx);
121
- this.done = DONE;
16
+ this.done = type_1.DONE;
122
17
  }
123
18
  unify(peer, ctx) {
124
19
  if (peer instanceof ScalarVal) {
@@ -128,7 +23,7 @@ class ScalarTypeVal extends Val {
128
23
  else if (Number === this.peg && Integer === peer.type) {
129
24
  return peer;
130
25
  }
131
- return Nil.make(ctx, 'no-scalar-unify', this, peer);
26
+ return Nil_1.Nil.make(ctx, 'no-scalar-unify', this, peer);
132
27
  }
133
28
  else {
134
29
  if (peer instanceof ScalarTypeVal) {
@@ -139,7 +34,7 @@ class ScalarTypeVal extends Val {
139
34
  return this;
140
35
  }
141
36
  }
142
- return Nil.make(ctx, 'scalar-type', this, peer);
37
+ return Nil_1.Nil.make(ctx, 'scalar-type', this, peer);
143
38
  }
144
39
  }
145
40
  get canon() {
@@ -154,18 +49,18 @@ class ScalarTypeVal extends Val {
154
49
  }
155
50
  }
156
51
  exports.ScalarTypeVal = ScalarTypeVal;
157
- class ScalarVal extends Val {
52
+ class ScalarVal extends ValBase_1.ValBase {
158
53
  constructor(peg, type, ctx) {
159
54
  super(peg, ctx);
160
55
  this.type = type;
161
- this.done = DONE;
56
+ this.done = type_1.DONE;
162
57
  }
163
58
  unify(peer, ctx) {
164
59
  // Exactly equal scalars are handled in op/unite
165
60
  if (peer instanceof ScalarTypeVal) {
166
61
  return peer.unify(this, ctx);
167
62
  }
168
- return Nil.make(ctx, 'scalar', this, peer);
63
+ return Nil_1.Nil.make(ctx, 'scalar', this, peer);
169
64
  }
170
65
  get canon() {
171
66
  return this.peg.toString();
@@ -235,497 +130,6 @@ class BooleanVal extends ScalarVal {
235
130
  }
236
131
  }
237
132
  exports.BooleanVal = BooleanVal;
238
- BooleanVal.TRUE = new BooleanVal(true, new unify_1.Context({ vc: 1, root: TOP }));
239
- BooleanVal.FALSE = new BooleanVal(false, new unify_1.Context({ vc: 2, root: TOP }));
240
- class MapVal extends Val {
241
- constructor(peg, ctx) {
242
- super(peg, ctx);
243
- this.spread = {
244
- cj: undefined,
245
- };
246
- let spread = this.peg[MapVal.SPREAD];
247
- delete this.peg[MapVal.SPREAD];
248
- if (spread) {
249
- if ('&' === spread.o) {
250
- // TODO: handle existing spread!
251
- this.spread.cj =
252
- new ConjunctVal(Array.isArray(spread.v) ? spread.v : [spread.v], ctx);
253
- }
254
- }
255
- }
256
- // NOTE: order of keys is not preserved!
257
- // not possible in any case - consider {a,b} unify {b,a}
258
- unify(peer, ctx) {
259
- let done = true;
260
- let out = TOP === peer ? this : new MapVal({}, ctx);
261
- out.spread.cj = this.spread.cj;
262
- if (peer instanceof MapVal) {
263
- out.spread.cj = null == out.spread.cj ? peer.spread.cj : (null == peer.spread.cj ? out.spread.cj : (out.spread.cj = new ConjunctVal([out.spread.cj, peer.spread.cj], ctx)));
264
- }
265
- out.done = this.done + 1;
266
- if (this.spread.cj) {
267
- //out.spread.cj =
268
- // DONE !== this.spread.cj.done ? this.spread.cj.unify(TOP, ctx) :
269
- // this.spread.cj
270
- out.spread.cj =
271
- DONE !== this.spread.cj.done ? (0, op_1.unite)(ctx, this.spread.cj) :
272
- this.spread.cj;
273
- }
274
- // console.log(
275
- // (' '.repeat(ctx.path.length)),
276
- // 'MV spread', this.id, peer.id, out.id, '|',
277
- // this.canon, peer.canon, out.canon, '|',
278
- // (this.spread.cj || {}).done,
279
- // (this.spread.cj || {}).canon, (out.spread.cj || {}).canon)
280
- let spread_cj = out.spread.cj || TOP;
281
- // Always unify children first
282
- for (let key in this.peg) {
283
- //let oval = out.peg[key] = this.peg[key].unify(spread_cj, ctx.descend(key))
284
- //let oval =
285
- out.peg[key] =
286
- (0, op_1.unite)(ctx.descend(key), this.peg[key], spread_cj);
287
- done = (done && DONE === out.peg[key].done);
288
- //if (oval instanceof Nil) {
289
- // ctx.err.push(oval)
290
- //}
291
- }
292
- // console.log(
293
- // (' '.repeat(ctx.path.length)),
294
- // 'MV child ', this.id, peer.id, out.id, '|',
295
- // this.canon, peer.canon, out.canon, '|',
296
- // this.constructor.name,
297
- // peer.constructor.name,
298
- // out.constructor.name,
299
- // )
300
- if (peer instanceof MapVal) {
301
- //let upeer: MapVal = (peer.unify(TOP, ctx) as MapVal)
302
- let upeer = (0, op_1.unite)(ctx, peer);
303
- // console.log(
304
- // (' '.repeat(ctx.path.length)),
305
- // 'MV peer A', this.id, peer.id, out.id, '|',
306
- // Object.keys(this.peg), Object.keys(upeer.peg), Object.keys(out.peg))
307
- for (let peerkey in upeer.peg) {
308
- let peerchild = upeer.peg[peerkey];
309
- let child = out.peg[peerkey];
310
- let oval = out.peg[peerkey] =
311
- undefined === child ? peerchild :
312
- child instanceof Nil ? child :
313
- peerchild instanceof Nil ? peerchild :
314
- //child.unify(peerchild, ctx.descend(peerkey))
315
- (0, op_1.unite)(ctx.descend(peerkey), child, peerchild);
316
- if (this.spread.cj) {
317
- //out.peg[peerkey] = out.peg[peerkey].unify(spread_cj, ctx)
318
- out.peg[peerkey] = (0, op_1.unite)(ctx, out.peg[peerkey], spread_cj);
319
- }
320
- done = (done && DONE === oval.done);
321
- if (oval instanceof Nil) {
322
- // ctx.err.push(oval)
323
- }
324
- }
325
- // console.log(
326
- // (' '.repeat(ctx.path.length)),
327
- // 'MV peer B', this.id, peer.id, out.id, '|',
328
- // Object.keys(this.peg), Object.keys(upeer.peg), Object.keys(out.peg))
329
- //out.done = done ? DONE : out.done
330
- // console.log(' '.repeat(W) + 'MV OUT A', this.id, out.done, out.id, out.canon)//this.spread.cj, out.spread.cj)
331
- // console.log(
332
- // (' '.repeat(ctx.path.length)),
333
- // 'MV out ', this.id, peer.id, out.id, '|',
334
- // this.canon, peer.canon, out.canon, '|',
335
- // this.constructor.name,
336
- // peer.constructor.name,
337
- // out.constructor.name,
338
- // )
339
- }
340
- else if (TOP !== peer) {
341
- //out.done = done ? DONE : out.done
342
- //return (UNIFIER(out, peer, ctx) as MapVal)
343
- return Nil.make(ctx, 'map', this, peer);
344
- }
345
- out.done = done ? DONE : out.done;
346
- return out;
347
- }
348
- get canon() {
349
- let keys = Object.keys(this.peg);
350
- return '{' +
351
- (this.spread.cj ? '&:' + this.spread.cj.canon +
352
- (0 < keys.length ? ',' : '') : '') +
353
- keys
354
- .map(k => [JSON.stringify(k) + ':' + this.peg[k].canon]).join(',') +
355
- '}';
356
- }
357
- gen(ctx) {
358
- let out = {};
359
- for (let p in this.peg) {
360
- out[p] = this.peg[p].gen(ctx);
361
- }
362
- return out;
363
- }
364
- }
365
- exports.MapVal = MapVal;
366
- MapVal.SPREAD = Symbol('spread');
367
- class ListVal extends Val {
368
- constructor(peg, ctx) {
369
- super(peg, ctx);
370
- this.spread = {
371
- cj: undefined,
372
- };
373
- let spread = this.peg[ListVal.SPREAD];
374
- delete this.peg[ListVal.SPREAD];
375
- if (spread) {
376
- if ('&' === spread.o) {
377
- // TODO: handle existing spread!
378
- this.spread.cj =
379
- new ConjunctVal(Array.isArray(spread.v) ? spread.v : [spread.v], ctx);
380
- }
381
- }
382
- }
383
- // NOTE: order of keys is not preserved!
384
- // not possible in any case - consider {a,b} unify {b,a}
385
- unify(peer, ctx) {
386
- let done = true;
387
- let out = TOP === peer ? this : new ListVal([], ctx);
388
- out.spread.cj = this.spread.cj;
389
- if (peer instanceof ListVal) {
390
- out.spread.cj = null == out.spread.cj ? peer.spread.cj : (null == peer.spread.cj ? out.spread.cj : (out.spread.cj = new ConjunctVal([out.spread.cj, peer.spread.cj], ctx)));
391
- }
392
- out.done = this.done + 1;
393
- if (this.spread.cj) {
394
- out.spread.cj =
395
- DONE !== this.spread.cj.done ? (0, op_1.unite)(ctx, this.spread.cj) :
396
- this.spread.cj;
397
- }
398
- let spread_cj = out.spread.cj || TOP;
399
- // Always unify children first
400
- for (let key in this.peg) {
401
- out.peg[key] =
402
- (0, op_1.unite)(ctx.descend(key), this.peg[key], spread_cj);
403
- done = (done && DONE === out.peg[key].done);
404
- }
405
- if (peer instanceof ListVal) {
406
- let upeer = (0, op_1.unite)(ctx, peer);
407
- for (let peerkey in upeer.peg) {
408
- let peerchild = upeer.peg[peerkey];
409
- let child = out.peg[peerkey];
410
- let oval = out.peg[peerkey] =
411
- undefined === child ? peerchild :
412
- child instanceof Nil ? child :
413
- peerchild instanceof Nil ? peerchild :
414
- (0, op_1.unite)(ctx.descend(peerkey), child, peerchild);
415
- if (this.spread.cj) {
416
- out.peg[peerkey] = (0, op_1.unite)(ctx, out.peg[peerkey], spread_cj);
417
- }
418
- done = (done && DONE === oval.done);
419
- }
420
- }
421
- else if (TOP !== peer) {
422
- return Nil.make(ctx, 'map', this, peer);
423
- }
424
- out.done = done ? DONE : out.done;
425
- return out;
426
- }
427
- get canon() {
428
- let keys = Object.keys(this.peg);
429
- return '[' +
430
- (this.spread.cj ? '&:' + this.spread.cj.canon +
431
- (0 < keys.length ? ',' : '') : '') +
432
- keys
433
- // NOTE: handle array non-index key vals
434
- // .map(k => [JSON.stringify(k) + ':' + this.peg[k].canon]).join(',') +
435
- .map(k => [this.peg[k].canon]).join(',') +
436
- ']';
437
- }
438
- gen(ctx) {
439
- let out = this.peg.map((v) => v.gen(ctx));
440
- // for (let p in this.peg) {
441
- // out[p] = this.peg[p].gen(ctx)
442
- // }
443
- return out;
444
- }
445
- }
446
- exports.ListVal = ListVal;
447
- ListVal.SPREAD = Symbol('spread');
448
- // TODO: move main logic to op/conjunct
449
- class ConjunctVal extends Val {
450
- constructor(peg, ctx) {
451
- super(peg, ctx);
452
- }
453
- // NOTE: mutation!
454
- append(peer) {
455
- this.peg.push(peer);
456
- return this;
457
- }
458
- unify(peer, ctx) {
459
- let done = true;
460
- // Unify each term of conjunct against peer
461
- let upeer = [];
462
- for (let vI = 0; vI < this.peg.length; vI++) {
463
- // upeer[vI] = this.peg[vI].unify(peer, ctx)
464
- upeer[vI] = (0, op_1.unite)(ctx, this.peg[vI], peer);
465
- done = done && DONE === upeer[vI].done;
466
- // // console.log('Ca', vI, this.peg[vI].canon, peer.canon, upeer[vI].canon)
467
- if (upeer[vI] instanceof Nil) {
468
- return Nil.make(ctx, '&peer[' + upeer[vI].canon + ',' + peer.canon + ']', this.peg[vI], peer);
469
- }
470
- }
471
- // // console.log('Cb', upeer.map(x => x.canon))
472
- // TODO: FIX: conjuncts get replicated inside each other
473
- // 1&/x => CV[CV[1&/x]]
474
- // Unify each term of conjunct against following sibling,
475
- // reducing to smallest conjunct or single val
476
- let outvals = 0 < upeer.length ? [upeer[0]] : [];
477
- let oI = 0;
478
- for (let uI = 1; uI < upeer.length; uI++) {
479
- // // console.log('Cu', oI, uI, outvals.map(x => x.canon))
480
- if (outvals[oI] instanceof ConjunctVal) {
481
- outvals.splice(oI, 0, ...outvals[oI].peg);
482
- oI += outvals[oI].peg.length;
483
- done = false;
484
- }
485
- else {
486
- outvals[oI] = null == outvals[oI] ? upeer[uI] :
487
- //outvals[oI].unify(upeer[uI], ctx)
488
- (0, op_1.unite)(ctx, outvals[oI], upeer[uI]);
489
- done = done && DONE === outvals[oI].done;
490
- // Conjuct fails
491
- if (outvals[oI] instanceof Nil) {
492
- return outvals[oI];
493
- /*
494
- return Nil.make(
495
- ctx,
496
- '&reduce[' + outvals[oI].canon + ',' + upeer[uI].canon + ']',
497
- outvals[oI],
498
- upeer[uI]
499
- )
500
- */
501
- }
502
- }
503
- }
504
- // // console.log('Cc', outvals.map(x => x.canon), outvals)
505
- let out;
506
- //let why = ''
507
- if (0 === outvals.length) {
508
- //out = Nil.make(ctx, '&empty', this)
509
- // Empty conjuncts evaporate.
510
- out = TOP;
511
- //why += 'A'
512
- }
513
- // TODO: corrects CV[CV[1&/x]] issue above, but swaps term order!
514
- else if (1 === outvals.length) {
515
- out = outvals[0];
516
- //why += 'B'
517
- }
518
- else {
519
- out = new ConjunctVal(outvals, ctx);
520
- //why += 'C'
521
- }
522
- // // console.log('Cd', why, out.peg)
523
- out.done = done ? DONE : this.done + 1;
524
- return out;
525
- }
526
- // TODO: need a well-defined val order so conjunt canon is always the same
527
- get canon() {
528
- return this.peg.map((v) => v.canon).join('&');
529
- }
530
- gen(ctx) {
531
- if (0 < this.peg.length) {
532
- // Default is just the first term - does this work?
533
- // TODO: maybe use a PrefVal() ?
534
- let v = this.peg[0];
535
- let out = undefined;
536
- if (undefined !== v && !(v instanceof Nil)) {
537
- out = v.gen(ctx);
538
- }
539
- return out;
540
- }
541
- return undefined;
542
- }
543
- }
544
- exports.ConjunctVal = ConjunctVal;
545
- // TODO: move main logic to op/disjunct
546
- class DisjunctVal extends Val {
547
- // TODO: sites from normalization of orginal Disjuncts, as well as child pegs
548
- constructor(peg, ctx, _sites) {
549
- super(peg, ctx);
550
- }
551
- // NOTE: mutation!
552
- append(peer) {
553
- this.peg.push(peer);
554
- return this;
555
- }
556
- unify(peer, ctx) {
557
- let done = true;
558
- let oval = [];
559
- // console.log('oval', this.canon, peer.canon)
560
- // Conjunction (&) distributes over disjunction (|)
561
- for (let vI = 0; vI < this.peg.length; vI++) {
562
- //oval[vI] = this.peg[vI].unify(peer, ctx)
563
- oval[vI] = (0, op_1.unite)(ctx, this.peg[vI], peer);
564
- // console.log('ovalA', vI, this.peg[vI].canon, peer.canon, oval[vI].canon)
565
- done = done && DONE === oval[vI].done;
566
- }
567
- // console.log('ovalB', oval.map(v => v.canon))
568
- // Remove duplicates, and normalize
569
- if (1 < oval.length) {
570
- for (let vI = 0; vI < oval.length; vI++) {
571
- if (oval[vI] instanceof DisjunctVal) {
572
- oval.splice(vI, 1, ...oval[vI].peg);
573
- }
574
- }
575
- //console.log('ovalC', oval.map(v => v.canon))
576
- // TODO: not an error Nil!
577
- let remove = new Nil();
578
- for (let vI = 0; vI < oval.length; vI++) {
579
- for (let kI = vI + 1; kI < oval.length; kI++) {
580
- if (oval[kI].same(oval[vI])) {
581
- oval[kI] = remove;
582
- }
583
- }
584
- }
585
- //console.log('ovalD', oval.map(v => v.canon))
586
- oval = oval.filter(v => !(v instanceof Nil));
587
- }
588
- let out;
589
- if (1 == oval.length) {
590
- out = oval[0];
591
- }
592
- else if (0 == oval.length) {
593
- return Nil.make(ctx, '|:empty', this);
594
- }
595
- else {
596
- out = new DisjunctVal(oval, ctx);
597
- }
598
- out.done = done ? DONE : this.done + 1;
599
- return out;
600
- }
601
- get canon() {
602
- return this.peg.map((v) => v.canon).join('|');
603
- }
604
- gen(ctx) {
605
- if (0 < this.peg.length) {
606
- let vals = this.peg.filter((v) => v instanceof PrefVal);
607
- vals = 0 === vals.length ? this.peg : vals;
608
- let val = vals[0];
609
- for (let vI = 1; vI < this.peg.length; vI++) {
610
- val = val.unify(this.peg[vI]);
611
- }
612
- return val.gen(ctx);
613
- }
614
- return undefined;
615
- }
616
- }
617
- exports.DisjunctVal = DisjunctVal;
618
- class RefVal extends Val {
619
- constructor(peg, abs) {
620
- super('');
621
- this.sep = '.';
622
- this.absolute = true === abs;
623
- this.parts = [];
624
- for (let part of peg) {
625
- this.append(part);
626
- }
627
- }
628
- append(part) {
629
- //console.log('APPEND 0', part)
630
- if ('string' === typeof part) {
631
- this.parts.push(part);
632
- }
633
- else if (part instanceof StringVal) {
634
- this.parts.push(part.peg);
635
- }
636
- else if (part instanceof RefVal) {
637
- this.parts.push(...part.parts);
638
- if (part.absolute) {
639
- this.absolute = true;
640
- }
641
- }
642
- this.peg = (this.absolute ? this.sep : '') + this.parts.join(this.sep);
643
- }
644
- unify(peer, ctx) {
645
- let resolved = null == ctx ? this : ctx.find(this);
646
- // TODO: large amount of reruns needed? why?
647
- resolved = null == resolved && 999 < this.done ?
648
- Nil.make(ctx, 'no-path', this, peer) : (resolved || this);
649
- let out;
650
- if (resolved instanceof RefVal) {
651
- if (TOP === peer) {
652
- out = this;
653
- }
654
- else if (peer instanceof Nil) {
655
- out = Nil.make(ctx, 'ref[' + this.peg + ']', this, peer);
656
- }
657
- else {
658
- // Ensure RefVal done is incremented
659
- this.done = DONE === this.done ? DONE : this.done + 1;
660
- out = new ConjunctVal([this, peer], ctx);
661
- }
662
- }
663
- else {
664
- out = (0, op_1.unite)(ctx, resolved, peer);
665
- }
666
- out.done = DONE === out.done ? DONE : this.done + 1;
667
- return out;
668
- }
669
- same(peer) {
670
- return null == peer ? false : this.peg === peer.peg;
671
- }
672
- get canon() {
673
- return this.peg;
674
- }
675
- gen(_ctx) {
676
- return undefined;
677
- }
678
- }
679
- exports.RefVal = RefVal;
680
- class PrefVal extends Val {
681
- constructor(peg, pref, ctx) {
682
- super(peg, ctx);
683
- this.pref = pref || peg;
684
- }
685
- // PrefVal unify always returns a PrefVal
686
- // PrefVals can only be removed by becoming Nil in a Disjunct
687
- unify(peer, ctx) {
688
- let done = true;
689
- let out;
690
- if (peer instanceof PrefVal) {
691
- out = new PrefVal((0, op_1.unite)(ctx, this.peg, peer.peg, 'Pref000'), (0, op_1.unite)(ctx, this.pref, peer.pref, 'Pref010'), ctx);
692
- }
693
- else {
694
- out = new PrefVal(
695
- // TODO: find a better way to drop Nil non-errors
696
- (0, op_1.unite)(ctx === null || ctx === void 0 ? void 0 : ctx.clone({ err: [] }), this.peg, peer, 'Pref020'), (0, op_1.unite)(ctx === null || ctx === void 0 ? void 0 : ctx.clone({ err: [] }), this.pref, peer, 'Pref030'), ctx);
697
- }
698
- done = done && DONE === out.peg.done &&
699
- (null != out.pref ? DONE === out.pref.done : true);
700
- if (out.peg instanceof Nil) {
701
- out = out.pref;
702
- }
703
- else if (out.pref instanceof Nil) {
704
- out = out.peg;
705
- }
706
- out.done = done ? DONE : this.done + 1;
707
- return out;
708
- }
709
- same(peer) {
710
- if (null == peer) {
711
- return false;
712
- }
713
- let pegsame = (this.peg === peer.peg) ||
714
- (this.peg instanceof Val && this.peg.same(peer.peg));
715
- let prefsame = peer instanceof PrefVal &&
716
- ((this.pref === peer.pref) ||
717
- (this.pref instanceof Val && this.pref.same(peer.pref)));
718
- return pegsame && prefsame;
719
- }
720
- get canon() {
721
- return this.pref instanceof Nil ? this.peg.canon : '*' + this.pref.canon;
722
- }
723
- gen(ctx) {
724
- let val = !(this.pref instanceof Nil) ? this.pref :
725
- !(this.peg instanceof Nil) ? this.peg :
726
- undefined;
727
- return undefined === val ? undefined : val.gen(ctx);
728
- }
729
- }
730
- exports.PrefVal = PrefVal;
133
+ BooleanVal.TRUE = new BooleanVal(true, new unify_1.Context({ vc: 1, root: type_1.TOP }));
134
+ BooleanVal.FALSE = new BooleanVal(false, new unify_1.Context({ vc: 2, root: type_1.TOP }));
731
135
  //# sourceMappingURL=val.js.map