aontu 0.12.0 → 0.13.1

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 (77) hide show
  1. package/aontu.ts +12 -2
  2. package/dist/aontu.d.ts +3 -0
  3. package/dist/aontu.js +9 -2
  4. package/dist/aontu.js.map +1 -1
  5. package/dist/aontu.min.js +1 -1
  6. package/dist/lib/err.js +22 -15
  7. package/dist/lib/err.js.map +1 -1
  8. package/dist/lib/lang.js +99 -33
  9. package/dist/lib/lang.js.map +1 -1
  10. package/dist/lib/op/disjunct.js +2 -2
  11. package/dist/lib/op/disjunct.js.map +1 -1
  12. package/dist/lib/op/op.js +1 -1
  13. package/dist/lib/op/op.js.map +1 -1
  14. package/dist/lib/op/unite.js +36 -6
  15. package/dist/lib/op/unite.js.map +1 -1
  16. package/dist/lib/type.d.ts +10 -8
  17. package/dist/lib/type.js +2 -27
  18. package/dist/lib/type.js.map +1 -1
  19. package/dist/lib/unify.d.ts +7 -4
  20. package/dist/lib/unify.js +27 -37
  21. package/dist/lib/unify.js.map +1 -1
  22. package/dist/lib/utility.d.ts +3 -0
  23. package/dist/lib/utility.js +17 -0
  24. package/dist/lib/utility.js.map +1 -0
  25. package/dist/lib/val/ConjunctVal.d.ts +7 -4
  26. package/dist/lib/val/ConjunctVal.js +62 -29
  27. package/dist/lib/val/ConjunctVal.js.map +1 -1
  28. package/dist/lib/val/DisjunctVal.d.ts +5 -2
  29. package/dist/lib/val/DisjunctVal.js +15 -7
  30. package/dist/lib/val/DisjunctVal.js.map +1 -1
  31. package/dist/lib/val/ListVal.d.ts +5 -2
  32. package/dist/lib/val/ListVal.js +39 -19
  33. package/dist/lib/val/ListVal.js.map +1 -1
  34. package/dist/lib/val/MapVal.d.ts +6 -3
  35. package/dist/lib/val/MapVal.js +56 -30
  36. package/dist/lib/val/MapVal.js.map +1 -1
  37. package/dist/lib/val/Nil.d.ts +8 -3
  38. package/dist/lib/val/Nil.js +31 -6
  39. package/dist/lib/val/Nil.js.map +1 -1
  40. package/dist/lib/val/PrefVal.d.ts +6 -2
  41. package/dist/lib/val/PrefVal.js +31 -11
  42. package/dist/lib/val/PrefVal.js.map +1 -1
  43. package/dist/lib/val/RefVal.d.ts +11 -5
  44. package/dist/lib/val/RefVal.js +198 -40
  45. package/dist/lib/val/RefVal.js.map +1 -1
  46. package/dist/lib/val/ValBase.d.ts +10 -9
  47. package/dist/lib/val/ValBase.js +25 -6
  48. package/dist/lib/val/ValBase.js.map +1 -1
  49. package/dist/lib/val/VarVal.d.ts +14 -0
  50. package/dist/lib/val/VarVal.js +79 -0
  51. package/dist/lib/val/VarVal.js.map +1 -0
  52. package/dist/lib/val.d.ts +42 -8
  53. package/dist/lib/val.js +63 -18
  54. package/dist/lib/val.js.map +1 -1
  55. package/lib/err.ts +28 -18
  56. package/lib/lang.ts +115 -41
  57. package/lib/op/disjunct.ts +10 -3
  58. package/lib/op/op.ts +1 -1
  59. package/lib/op/unite.ts +44 -4
  60. package/lib/type.ts +12 -40
  61. package/lib/unify.ts +43 -45
  62. package/lib/utility.ts +23 -0
  63. package/lib/utility.ts~ +80 -0
  64. package/lib/val/ConjunctVal.ts +90 -46
  65. package/lib/val/DisjunctVal.ts +35 -12
  66. package/lib/val/ListVal.ts +57 -22
  67. package/lib/val/MapVal.ts +78 -52
  68. package/lib/val/Nil.ts +55 -12
  69. package/lib/val/PrefVal.ts +56 -17
  70. package/lib/val/RefVal.ts +275 -63
  71. package/lib/val/ValBase.ts +33 -28
  72. package/lib/val/VarVal.ts +155 -0
  73. package/lib/val.ts +115 -25
  74. package/package.json +8 -8
  75. package/dist/lib/common.d.ts +0 -8
  76. package/dist/lib/common.js +0 -3
  77. package/dist/lib/common.js.map +0 -1
package/lib/val/MapVal.ts CHANGED
@@ -1,15 +1,15 @@
1
- /* Copyright (c) 2021 Richard Rodger, MIT License */
1
+ /* Copyright (c) 2021-2023 Richard Rodger, MIT License */
2
2
 
3
3
 
4
4
 
5
5
  import type {
6
6
  Val,
7
7
  ValMap,
8
+ ValSpec,
8
9
  } from '../type'
9
10
 
10
11
  import {
11
12
  DONE,
12
- TOP,
13
13
  } from '../type'
14
14
 
15
15
  import {
@@ -17,30 +17,21 @@ import {
17
17
  } from '../unify'
18
18
 
19
19
 
20
- import {
21
- Site
22
- } from '../lang'
23
-
24
20
 
25
21
  import {
26
22
  unite
27
23
  } from '../op/op'
28
24
 
29
- import {
30
- ValBase,
31
- } from '../val/ValBase'
32
-
33
- import {
34
- Nil,
35
- } from './Nil'
36
-
37
- import {
38
- ConjunctVal,
39
- } from './ConjunctVal'
40
-
41
-
42
25
 
43
26
 
27
+ import { TOP } from '../val'
28
+ import { ConjunctVal } from '../val/ConjunctVal'
29
+ import { DisjunctVal } from '../val/DisjunctVal'
30
+ import { ListVal } from '../val/ListVal'
31
+ import { Nil } from '../val/Nil'
32
+ import { PrefVal } from '../val/PrefVal'
33
+ import { RefVal } from '../val/RefVal'
34
+ import { ValBase } from '../val/ValBase'
44
35
 
45
36
 
46
37
  class MapVal extends ValBase {
@@ -50,37 +41,48 @@ class MapVal extends ValBase {
50
41
  cj: (undefined as Val | undefined),
51
42
  }
52
43
 
53
- constructor(peg: ValMap, ctx?: Context) {
54
- super(peg, ctx)
44
+ constructor(
45
+ spec: {
46
+ peg: ValMap
47
+ },
48
+ ctx?: Context
49
+ ) {
50
+ super(spec, ctx)
51
+
52
+ if (null == this.peg) {
53
+ throw new Error('MapVal spec.peg undefined')
54
+ }
55
+
55
56
 
56
57
  let spread = (this.peg as any)[MapVal.SPREAD]
57
58
  delete (this.peg as any)[MapVal.SPREAD]
58
59
 
60
+ // console.log('MC', this.id, peg, spread)
61
+
59
62
  if (spread) {
60
63
  if ('&' === spread.o) {
61
- // TODO: handle existing spread!
62
- this.spread.cj =
63
- new ConjunctVal(Array.isArray(spread.v) ? spread.v : [spread.v], ctx)
64
+ let tmv = Array.isArray(spread.v) ? spread.v : [spread.v]
65
+ this.spread.cj = new ConjunctVal({ peg: tmv }, ctx)
64
66
  }
65
67
  }
66
68
  }
67
69
 
70
+
68
71
  // NOTE: order of keys is not preserved!
69
72
  // not possible in any case - consider {a,b} unify {b,a}
70
73
  unify(peer: Val, ctx: Context): Val {
71
- // if (null == ctx) {
72
- // console.trace()
73
- // }
74
+ // let mark = Math.random()
74
75
 
75
76
  let done: boolean = true
76
- let out: MapVal = TOP === peer ? this : new MapVal({}, ctx)
77
+ let out: MapVal = TOP === peer ? this : new MapVal({ peg: {} }, ctx)
77
78
 
78
79
  out.spread.cj = this.spread.cj
79
80
 
80
81
  if (peer instanceof MapVal) {
81
82
  out.spread.cj = null == out.spread.cj ? peer.spread.cj : (
82
83
  null == peer.spread.cj ? out.spread.cj : (
83
- out.spread.cj = new ConjunctVal([out.spread.cj, peer.spread.cj], ctx)
84
+ out.spread.cj =
85
+ new ConjunctVal({ peg: [out.spread.cj, peer.spread.cj] }, ctx)
84
86
  )
85
87
  )
86
88
  }
@@ -88,32 +90,37 @@ class MapVal extends ValBase {
88
90
 
89
91
  out.done = this.done + 1
90
92
 
91
- if (this.spread.cj) {
92
- out.spread.cj =
93
- DONE !== this.spread.cj.done ? unite(ctx, this.spread.cj) :
94
- this.spread.cj
95
- done = (done && DONE === out.spread.cj.done)
96
- }
97
-
98
-
99
93
  let spread_cj = out.spread.cj || TOP
100
94
 
101
- // Always unify children first
95
+ // Always unify own children first
102
96
  for (let key in this.peg) {
103
- // console.log('MAP ukA', key, this.peg[key].canon)
104
-
105
- out.peg[key] =
106
- unite(ctx.descend(key), this.peg[key], spread_cj)
107
-
108
- // console.log('MAP ukB', key, out.peg[key].canon)
109
-
97
+ let keyctx = ctx.descend(key)
98
+ let key_spread_cj = spread_cj.clone(null, keyctx)
99
+
100
+ // console.log('M0', this.id, mark, Object.keys(this.peg).join('~'),
101
+ // 'p=', this.path.join('.'),
102
+ // 'k=', key, peer.top || peer.constructor.name,
103
+ // 'pp=', this.peg[key].path.join('.'),
104
+ // this.peg[key].canon,
105
+ // 'sp=', key_spread_cj.path.join('.'),
106
+ // key_spread_cj.canon)
107
+
108
+ // if (1000000000 === this.id) {
109
+ // console.dir(key_spread_cj, { depth: null })
110
+ // }
111
+
112
+ out.peg[key] = unite(keyctx, this.peg[key], key_spread_cj, 'map-own')
110
113
  done = (done && DONE === out.peg[key].done)
111
114
  }
112
115
 
116
+
113
117
  if (peer instanceof MapVal) {
114
- let upeer: MapVal = (unite(ctx, peer) as MapVal)
118
+ let upeer: MapVal = (unite(ctx, peer, undefined, 'map-peer-map') as MapVal)
115
119
 
116
120
  for (let peerkey in upeer.peg) {
121
+ // console.log('M1', this.id, mark, Object.keys(this.peg).join('~'),
122
+ // 'pk=', peerkey)
123
+
117
124
  let peerchild = upeer.peg[peerkey]
118
125
  let child = out.peg[peerkey]
119
126
 
@@ -121,14 +128,19 @@ class MapVal extends ValBase {
121
128
  undefined === child ? peerchild :
122
129
  child instanceof Nil ? child :
123
130
  peerchild instanceof Nil ? peerchild :
124
- unite(ctx.descend(peerkey), child, peerchild)
131
+ unite(ctx.descend(peerkey), child, peerchild, 'map-peer')
125
132
 
126
133
  if (this.spread.cj) {
127
- out.peg[peerkey] = unite(ctx, out.peg[peerkey], spread_cj)
128
- }
129
-
130
- done = (done && DONE === oval.done)
134
+ let key_ctx = ctx.descend(peerkey)
135
+ let key_spread_cj = spread_cj.clone(null, key_ctx)
131
136
 
137
+ out.peg[peerkey] =
138
+ new ConjunctVal({ peg: [out.peg[peerkey], key_spread_cj] }, key_ctx)
139
+ done = false
140
+ }
141
+ else {
142
+ done = (done && DONE === oval.done)
143
+ }
132
144
  }
133
145
  }
134
146
  else if (TOP !== peer) {
@@ -140,6 +152,20 @@ class MapVal extends ValBase {
140
152
  }
141
153
 
142
154
 
155
+ clone(spec?: ValSpec, ctx?: Context): Val {
156
+ let out = (super.clone(spec, ctx) as MapVal)
157
+ out.peg = {}
158
+ for (let entry of Object.entries(this.peg)) {
159
+ out.peg[entry[0]] =
160
+ entry[1] instanceof ValBase ? entry[1].clone(null, ctx) : entry[1]
161
+ }
162
+ if (this.spread.cj) {
163
+ out.spread.cj = this.spread.cj.clone(null, ctx)
164
+ }
165
+ return out
166
+ }
167
+
168
+
143
169
  get canon() {
144
170
  let keys = Object.keys(this.peg)
145
171
  return '{' +
@@ -151,7 +177,7 @@ class MapVal extends ValBase {
151
177
  }
152
178
 
153
179
 
154
- gen(ctx: Context) {
180
+ gen(ctx?: Context) {
155
181
  let out: any = {}
156
182
  for (let p in this.peg) {
157
183
  out[p] = this.peg[p].gen(ctx)
package/lib/val/Nil.ts CHANGED
@@ -1,8 +1,9 @@
1
- /* Copyright (c) 2021-2022 Richard Rodger, MIT License */
1
+ /* Copyright (c) 2021-2023 Richard Rodger, MIT License */
2
2
 
3
3
 
4
4
  import type {
5
5
  Val,
6
+ ValSpec,
6
7
  } from '../type'
7
8
 
8
9
  import {
@@ -10,16 +11,15 @@ import {
10
11
  } from '../type'
11
12
 
12
13
  import {
13
- Context,
14
- } from '../unify'
15
-
14
+ descErr
15
+ } from '../err'
16
16
 
17
17
  import {
18
- ValBase,
19
- } from '../val/ValBase'
20
-
18
+ Context,
19
+ } from '../unify'
21
20
 
22
21
 
22
+ import { ValBase } from '../val/ValBase'
23
23
 
24
24
 
25
25
  class Nil extends ValBase {
@@ -32,8 +32,9 @@ class Nil extends ValBase {
32
32
  // TODO: include Val generating nil, thus capture type
33
33
 
34
34
  // A Nil is an error - should not happen - unify failed
35
+ // refactor ,make(spec,ctx)
35
36
  static make = (ctx?: Context, why?: any, av?: Val, bv?: Val) => {
36
- let nil = new Nil(why, ctx)
37
+ let nil = new Nil({ why }, ctx)
37
38
 
38
39
  // TODO: this should be done lazily, for multiple terms
39
40
 
@@ -71,27 +72,69 @@ class Nil extends ValBase {
71
72
  return nil
72
73
  }
73
74
 
74
- constructor(why?: any, ctx?: Context) {
75
- super(null, ctx)
76
- this.why = why
75
+
76
+ constructor(
77
+ spec?: {
78
+ why?: string
79
+ msg?: string
80
+ err?: Nil | Nil[] | Error | Error[]
81
+ } | string,
82
+ ctx?: Context
83
+ ) {
84
+ super(spec && 'string' !== typeof spec ? spec : {}, ctx)
85
+
86
+ if (spec && 'object' === typeof spec) {
87
+ this.why = spec?.why
88
+ this.msg = 'string' === typeof spec?.msg ? spec.msg : this.msg
89
+ this.err = spec ? (Array.isArray(spec.err) ? [...spec.err] : [spec.err]) : []
90
+ }
77
91
 
78
92
  // Nil is always DONE, by definition.
79
93
  this.done = DONE
80
94
  }
81
95
 
96
+
82
97
  unify(_peer: Val, _ctx: Context) {
83
98
  return this
84
99
  }
85
100
 
101
+
102
+ clone(spec?: ValSpec, ctx?: Context): Val {
103
+ let out = (super.clone(spec, ctx) as Nil)
104
+ out.why = this.why
105
+
106
+ // Should these clone?
107
+ // out.primary = this.primary?.clone()
108
+ // out.secondary = this.secondary?.clone()
109
+ out.primary = this.primary
110
+ out.secondary = this.secondary
111
+
112
+ out.msg = this.msg
113
+ return out
114
+ }
115
+
116
+
86
117
  get canon() {
87
118
  return 'nil'
88
119
  }
89
120
 
90
- gen(_ctx?: Context) {
121
+ gen(ctx?: Context) {
122
+ // Unresolved nil cannot be generated, so always an error.
123
+
124
+ descErr(this)
125
+
126
+ if (ctx) {
127
+ ctx.err.push(this)
128
+ }
129
+ else {
130
+ throw new Error(this.msg)
131
+ }
132
+
91
133
  return undefined
92
134
  }
93
135
  }
94
136
 
137
+
95
138
  export {
96
139
  Nil,
97
140
  }
@@ -1,14 +1,20 @@
1
- /* Copyright (c) 2021-2022 Richard Rodger, MIT License */
1
+ /* Copyright (c) 2021-2023 Richard Rodger, MIT License */
2
+
2
3
 
3
4
 
4
5
  import type {
5
6
  Val,
7
+ ValSpec,
6
8
  } from '../type'
7
9
 
8
10
  import {
9
11
  DONE,
10
12
  } from '../type'
11
13
 
14
+ import {
15
+ descErr
16
+ } from '../err'
17
+
12
18
  import {
13
19
  Context,
14
20
  } from '../unify'
@@ -23,19 +29,30 @@ import {
23
29
  unite
24
30
  } from '../op/op'
25
31
 
26
- import { Nil } from '../val/Nil'
27
32
 
28
- import {
29
- ValBase,
30
- } from '../val/ValBase'
31
33
 
34
+ import { TOP } from '../val'
35
+ import { ConjunctVal } from '../val/ConjunctVal'
36
+ import { DisjunctVal } from '../val/DisjunctVal'
37
+ import { ListVal } from '../val/ListVal'
38
+ import { MapVal } from '../val/MapVal'
39
+ import { Nil } from '../val/Nil'
40
+ import { RefVal } from '../val/RefVal'
41
+ import { ValBase } from '../val/ValBase'
32
42
 
33
43
 
34
44
  class PrefVal extends ValBase {
35
45
  pref: Val
36
- constructor(peg: any, pref?: any, ctx?: Context) {
37
- super(peg, ctx)
38
- this.pref = pref || peg
46
+
47
+ constructor(
48
+ spec: {
49
+ peg: any,
50
+ pref?: any
51
+ },
52
+ ctx?: Context
53
+ ) {
54
+ super(spec, ctx)
55
+ this.pref = spec.pref || spec.peg
39
56
  }
40
57
 
41
58
  // PrefVal unify always returns a PrefVal
@@ -46,17 +63,21 @@ class PrefVal extends ValBase {
46
63
 
47
64
  if (peer instanceof PrefVal) {
48
65
  out = new PrefVal(
49
- unite(ctx, this.peg, peer.peg, 'Pref000'),
50
- unite(ctx, this.pref, peer.pref, 'Pref010'),
66
+ {
67
+ peg: unite(ctx, this.peg, peer.peg, 'Pref000'),
68
+ pref: unite(ctx, this.pref, peer.pref, 'Pref010'),
69
+ },
51
70
  ctx
52
71
  )
53
-
54
72
  }
73
+
55
74
  else {
56
75
  out = new PrefVal(
57
- // TODO: find a better way to drop Nil non-errors
58
- unite(ctx?.clone({ err: [] }), this.peg, peer, 'Pref020'),
59
- unite(ctx?.clone({ err: [] }), this.pref, peer, 'Pref030'),
76
+ {
77
+ // TODO: find a better way to drop Nil non-errors
78
+ peg: unite(ctx?.clone({ err: [] }), this.peg, peer, 'Pref020'),
79
+ pref: unite(ctx?.clone({ err: [] }), this.pref, peer, 'Pref030'),
80
+ },
60
81
  ctx
61
82
  )
62
83
  }
@@ -93,16 +114,34 @@ class PrefVal extends ValBase {
93
114
  }
94
115
 
95
116
 
117
+ clone(spec?: ValSpec, ctx?: Context): Val {
118
+ let out = (super.clone(spec, ctx) as PrefVal)
119
+ out.pref = this.pref.clone(null, ctx)
120
+ return out
121
+ }
122
+
123
+
96
124
  get canon() {
97
125
  return this.pref instanceof Nil ? this.peg.canon : '*' + this.pref.canon
98
126
  }
99
127
 
100
128
  gen(ctx?: Context) {
101
129
  let val = !(this.pref instanceof Nil) ? this.pref :
102
- !(this.peg instanceof Nil) ? this.peg :
103
- undefined
130
+ (!(this.peg instanceof Nil) ? this.peg :
131
+ this.pref)
132
+
133
+ if (val instanceof Nil) {
134
+ descErr(val)
135
+
136
+ if (ctx) {
137
+ ctx.err.push(val)
138
+ }
139
+ else {
140
+ throw new Error(val.msg)
141
+ }
142
+ }
104
143
 
105
- return undefined === val ? undefined : val.gen(ctx)
144
+ return val.gen(ctx)
106
145
  }
107
146
  }
108
147