aontu 0.2.0 → 0.5.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.
package/lib/val.ts CHANGED
@@ -37,6 +37,8 @@ import {
37
37
 
38
38
 
39
39
  type ValMap = { [key: string]: Val }
40
+ type ValList = Val[]
41
+
40
42
 
41
43
  const DONE = -1
42
44
 
@@ -54,21 +56,6 @@ const TOP: Val = {
54
56
 
55
57
  unify(peer: Val, _ctx: Context): Val {
56
58
  return peer
57
-
58
- /*
59
- if (peer instanceof DisjunctVal) {
60
- return peer.unify(this, ctx)
61
- }
62
- else if (peer instanceof ConjunctVal) {
63
- return peer.unify(this, ctx)
64
- }
65
- else if (peer instanceof RefVal) {
66
- return peer.unify(this, ctx)
67
- }
68
- else {
69
- return peer
70
- }
71
- */
72
59
  },
73
60
 
74
61
  get canon() { return 'top' },
@@ -86,7 +73,7 @@ const TOP: Val = {
86
73
  }
87
74
 
88
75
 
89
-
76
+ // TODO: extends Val ???
90
77
  abstract class Val {
91
78
  id: number
92
79
  done: number = 0
@@ -111,7 +98,8 @@ abstract class Val {
111
98
  }
112
99
 
113
100
  same(peer: Val): boolean {
114
- return this === peer
101
+ // return this === peer
102
+ return null == peer ? false : this.id === peer.id
115
103
  }
116
104
 
117
105
  get site(): Site {
@@ -518,6 +506,118 @@ class MapVal extends Val {
518
506
  }
519
507
 
520
508
 
509
+
510
+ class ListVal extends Val {
511
+ static SPREAD = Symbol('spread')
512
+
513
+ spread = {
514
+ cj: (undefined as Val | undefined),
515
+ }
516
+
517
+ constructor(peg: ValList, ctx?: Context) {
518
+ super(peg, ctx)
519
+
520
+ let spread = (this.peg as any)[ListVal.SPREAD]
521
+ delete (this.peg as any)[ListVal.SPREAD]
522
+
523
+ if (spread) {
524
+ if ('&' === spread.o) {
525
+ // TODO: handle existing spread!
526
+ this.spread.cj =
527
+ new ConjunctVal(Array.isArray(spread.v) ? spread.v : [spread.v], ctx)
528
+ }
529
+ }
530
+ }
531
+
532
+ // NOTE: order of keys is not preserved!
533
+ // not possible in any case - consider {a,b} unify {b,a}
534
+ unify(peer: Val, ctx: Context): Val {
535
+ let done: boolean = true
536
+ let out: ListVal = TOP === peer ? this : new ListVal([], ctx)
537
+
538
+ out.spread.cj = this.spread.cj
539
+
540
+ if (peer instanceof ListVal) {
541
+ out.spread.cj = null == out.spread.cj ? peer.spread.cj : (
542
+ null == peer.spread.cj ? out.spread.cj : (
543
+ out.spread.cj = new ConjunctVal([out.spread.cj, peer.spread.cj], ctx)
544
+ )
545
+ )
546
+ }
547
+
548
+
549
+ out.done = this.done + 1
550
+
551
+ if (this.spread.cj) {
552
+ out.spread.cj =
553
+ DONE !== this.spread.cj.done ? unite(ctx, this.spread.cj) :
554
+ this.spread.cj
555
+ }
556
+
557
+ let spread_cj = out.spread.cj || TOP
558
+
559
+ // Always unify children first
560
+ for (let key in this.peg) {
561
+ out.peg[key] =
562
+ unite(ctx.descend(key), this.peg[key], spread_cj)
563
+
564
+ done = (done && DONE === out.peg[key].done)
565
+ }
566
+
567
+ if (peer instanceof ListVal) {
568
+ let upeer: ListVal = (unite(ctx, peer) as ListVal)
569
+
570
+ for (let peerkey in upeer.peg) {
571
+ let peerchild = upeer.peg[peerkey]
572
+ let child = out.peg[peerkey]
573
+
574
+ let oval = out.peg[peerkey] =
575
+ undefined === child ? peerchild :
576
+ child instanceof Nil ? child :
577
+ peerchild instanceof Nil ? peerchild :
578
+ unite(ctx.descend(peerkey), child, peerchild)
579
+
580
+ if (this.spread.cj) {
581
+ out.peg[peerkey] = unite(ctx, out.peg[peerkey], spread_cj)
582
+ }
583
+
584
+ done = (done && DONE === oval.done)
585
+
586
+ }
587
+ }
588
+ else if (TOP !== peer) {
589
+ return Nil.make(ctx, 'map', this, peer)
590
+ }
591
+
592
+ out.done = done ? DONE : out.done
593
+ return out
594
+ }
595
+
596
+ get canon() {
597
+ let keys = Object.keys(this.peg)
598
+ return '[' +
599
+ (this.spread.cj ? '&:' + this.spread.cj.canon +
600
+ (0 < keys.length ? ',' : '') : '') +
601
+ keys
602
+ // NOTE: handle array non-index key vals
603
+ // .map(k => [JSON.stringify(k) + ':' + this.peg[k].canon]).join(',') +
604
+ .map(k => [this.peg[k].canon]).join(',') +
605
+ ']'
606
+ }
607
+
608
+ gen(ctx?: Context) {
609
+ let out: any = this.peg.map((v: Val) => v.gen(ctx))
610
+ // for (let p in this.peg) {
611
+ // out[p] = this.peg[p].gen(ctx)
612
+ // }
613
+
614
+ return out
615
+ }
616
+ }
617
+
618
+
619
+
620
+
521
621
  // TODO: move main logic to op/conjunct
522
622
  class ConjunctVal extends Val {
523
623
  constructor(peg: Val[], ctx?: Context) {
@@ -668,18 +768,18 @@ class DisjunctVal extends Val {
668
768
 
669
769
  let oval: Val[] = []
670
770
 
671
- //console.log('oval', this.canon, peer.canon)
771
+ // console.log('oval', this.canon, peer.canon)
672
772
 
673
773
  // Conjunction (&) distributes over disjunction (|)
674
774
  for (let vI = 0; vI < this.peg.length; vI++) {
675
775
  //oval[vI] = this.peg[vI].unify(peer, ctx)
676
776
  oval[vI] = unite(ctx, this.peg[vI], peer)
677
- //console.log('ovalA', vI, this.peg[vI].canon, peer.canon, oval[vI].canon)
777
+ // console.log('ovalA', vI, this.peg[vI].canon, peer.canon, oval[vI].canon)
678
778
 
679
779
  done = done && DONE === oval[vI].done
680
780
  }
681
781
 
682
- //console.log('ovalB', oval.map(v => v.canon))
782
+ // console.log('ovalB', oval.map(v => v.canon))
683
783
 
684
784
  // Remove duplicates, and normalize
685
785
  if (1 < oval.length) {
@@ -750,23 +850,13 @@ class DisjunctVal extends Val {
750
850
  class RefVal extends Val {
751
851
  parts: string[]
752
852
  absolute: boolean
753
- sep = '.' // was '/'
754
-
755
- // constructor(peg: string | string[], ctx?: Context) {
756
- // super(peg, ctx)
757
- // this.parts = 'string' === typeof peg ? peg.split(this.sep) : peg
758
- // this.parts = this.parts.filter(p => '' != p)
759
- // // this.absolute = peg.startsWith(this.sep)
760
- // }
761
-
853
+ sep = '.'
762
854
 
763
855
  constructor(peg: any[], abs?: boolean) {
764
856
  super('')
765
857
  this.absolute = true === abs
766
858
  this.parts = []
767
859
 
768
- // console.log('RV', peg)
769
-
770
860
  for (let part of peg) {
771
861
  this.append(part)
772
862
  }
@@ -793,8 +883,6 @@ class RefVal extends Val {
793
883
  }
794
884
 
795
885
  this.peg = (this.absolute ? this.sep : '') + this.parts.join(this.sep)
796
-
797
- // console.log('APPEND 1', this.parts)
798
886
  }
799
887
 
800
888
  unify(peer: Val, ctx: Context): Val {
@@ -805,12 +893,9 @@ class RefVal extends Val {
805
893
  Nil.make(ctx, 'no-path', this, peer) : (resolved || this)
806
894
  let out: Val
807
895
 
808
- // console.log('RV', this.id, this.done, this.canon, peer.canon, !!resolved, resolved instanceof RefVal, resolved && resolved.canon)
809
-
810
896
  if (resolved instanceof RefVal) {
811
897
  if (TOP === peer) {
812
898
  out = this
813
- //out = new RefVal(this.peg, ctx)
814
899
  }
815
900
  else if (peer instanceof Nil) {
816
901
  out = Nil.make(ctx, 'ref[' + this.peg + ']', this, peer)
@@ -822,9 +907,6 @@ class RefVal extends Val {
822
907
  }
823
908
  }
824
909
  else {
825
- //console.log('RVr', resolved.canon, peer.canon)
826
-
827
- //out = resolved.unify(peer, ctx)
828
910
  out = unite(ctx, resolved, peer)
829
911
  }
830
912
 
@@ -833,10 +915,17 @@ class RefVal extends Val {
833
915
  return out
834
916
  }
835
917
 
918
+
919
+ same(peer: Val): boolean {
920
+ return null == peer ? false : this.peg === peer.peg
921
+ }
922
+
923
+
836
924
  get canon() {
837
925
  return this.peg
838
926
  }
839
927
 
928
+
840
929
  gen(_ctx?: Context) {
841
930
  return undefined
842
931
  }
@@ -888,6 +977,23 @@ class PrefVal extends Val {
888
977
  return out
889
978
  }
890
979
 
980
+
981
+ same(peer: Val): boolean {
982
+ if (null == peer) {
983
+ return false
984
+ }
985
+
986
+ let pegsame = (this.peg === peer.peg) ||
987
+ (this.peg instanceof Val && this.peg.same(peer.peg))
988
+
989
+ let prefsame = peer instanceof PrefVal &&
990
+ ((this.pref === peer.pref) ||
991
+ (this.pref instanceof Val && this.pref.same(peer.pref)))
992
+
993
+ return pegsame && prefsame
994
+ }
995
+
996
+
891
997
  get canon() {
892
998
  return this.pref instanceof Nil ? this.peg.canon : '*' + this.pref.canon
893
999
  }
@@ -915,6 +1021,7 @@ export {
915
1021
  BooleanVal,
916
1022
  IntegerVal,
917
1023
  MapVal,
1024
+ ListVal,
918
1025
  ConjunctVal,
919
1026
  DisjunctVal,
920
1027
  RefVal,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aontu",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "main": "dist/aontu.js",
5
5
  "type": "commonjs",
6
6
  "browser": "dist/aontu.min.js",
@@ -39,22 +39,23 @@
39
39
  "LICENSE"
40
40
  ],
41
41
  "dependencies": {
42
- "@jsonic/directive": "^0.6.0",
43
- "@jsonic/expr": "^0.2.0",
44
- "@jsonic/multisource": "0.5.0",
45
- "@types/node": "^17.0.35",
46
- "@jsonic/jsonic-next": "2.0.1"
42
+ "@jsonic/directive": "^0.8.0",
43
+ "@jsonic/expr": "^0.5.0",
44
+ "@jsonic/jsonic-next": "^2.3.0",
45
+ "@jsonic/multisource": "^0.7.0",
46
+ "@jsonic/path": "^0.2.0",
47
+ "@types/node": "^18.6.5"
47
48
  },
48
49
  "devDependencies": {
49
- "@hapi/code": "^9.0.0",
50
+ "@hapi/code": "^9.0.1",
50
51
  "@hapi/lab": "^25.0.1",
51
52
  "benchmark": "^2.1.4",
52
53
  "coveralls": "^3.1.1",
53
54
  "hapi-lab-shim": "0.0.2",
54
55
  "lab-transform-typescript": "^3.0.1",
55
- "prettier": "^2.6.2",
56
- "serve": "^13.0.2",
57
- "tinyify": "^3.0.0",
58
- "typescript": "^4.7.2"
56
+ "prettier": "^2.7.1",
57
+ "serve": "^14.0.1",
58
+ "tinyify": "^3.1.0",
59
+ "typescript": "^4.7.4"
59
60
  }
60
61
  }