aontu 0.30.1 → 0.30.2

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 (184) hide show
  1. package/dist/ctx.d.ts +51 -0
  2. package/dist/ctx.js +105 -0
  3. package/dist/ctx.js.map +1 -0
  4. package/dist/err.d.ts +12 -0
  5. package/dist/err.js +123 -0
  6. package/dist/err.js.map +1 -0
  7. package/dist/hints.d.ts +8 -0
  8. package/dist/hints.js +84 -0
  9. package/dist/hints.js.map +1 -0
  10. package/dist/lang.d.ts +11 -0
  11. package/dist/lang.js +569 -0
  12. package/dist/lang.js.map +1 -0
  13. package/dist/site.d.ts +12 -0
  14. package/dist/site.js +27 -0
  15. package/dist/site.js.map +1 -0
  16. package/dist/tsconfig.tsbuildinfo +1 -0
  17. package/dist/type.d.ts +32 -0
  18. package/dist/type.js +16 -0
  19. package/dist/type.js.map +1 -0
  20. package/dist/unify.d.ts +14 -0
  21. package/dist/unify.js +156 -0
  22. package/dist/unify.js.map +1 -0
  23. package/dist/utility.d.ts +14 -0
  24. package/dist/utility.js +122 -0
  25. package/dist/utility.js.map +1 -0
  26. package/dist/val/BagVal.d.ts +10 -0
  27. package/dist/val/BagVal.js +15 -0
  28. package/dist/val/BagVal.js.map +1 -0
  29. package/dist/val/BooleanVal.d.ts +9 -0
  30. package/dist/val/BooleanVal.js +16 -0
  31. package/dist/val/BooleanVal.js.map +1 -0
  32. package/dist/val/CloseFuncVal.d.ts +11 -0
  33. package/dist/val/CloseFuncVal.js +32 -0
  34. package/dist/val/CloseFuncVal.js.map +1 -0
  35. package/dist/val/ConjunctVal.d.ts +14 -0
  36. package/dist/val/ConjunctVal.js +187 -0
  37. package/dist/val/ConjunctVal.js.map +1 -0
  38. package/dist/val/CopyFuncVal.d.ts +13 -0
  39. package/dist/val/CopyFuncVal.js +35 -0
  40. package/dist/val/CopyFuncVal.js.map +1 -0
  41. package/dist/val/DisjunctVal.d.ts +19 -0
  42. package/dist/val/DisjunctVal.js +194 -0
  43. package/dist/val/DisjunctVal.js.map +1 -0
  44. package/dist/val/FeatureVal.d.ts +10 -0
  45. package/dist/val/FeatureVal.js +29 -0
  46. package/dist/val/FeatureVal.js.map +1 -0
  47. package/dist/val/FuncBaseVal.d.ts +15 -0
  48. package/dist/val/FuncBaseVal.js +127 -0
  49. package/dist/val/FuncBaseVal.js.map +1 -0
  50. package/dist/val/HideFuncVal.d.ts +12 -0
  51. package/dist/val/HideFuncVal.js +48 -0
  52. package/dist/val/HideFuncVal.js.map +1 -0
  53. package/dist/val/IntegerVal.d.ts +9 -0
  54. package/dist/val/IntegerVal.js +46 -0
  55. package/dist/val/IntegerVal.js.map +1 -0
  56. package/dist/val/JunctionVal.d.ts +12 -0
  57. package/dist/val/JunctionVal.js +31 -0
  58. package/dist/val/JunctionVal.js.map +1 -0
  59. package/dist/val/KeyFuncVal.d.ts +12 -0
  60. package/dist/val/KeyFuncVal.js +34 -0
  61. package/dist/val/KeyFuncVal.js.map +1 -0
  62. package/dist/val/ListVal.d.ts +17 -0
  63. package/dist/val/ListVal.js +186 -0
  64. package/dist/val/ListVal.js.map +1 -0
  65. package/dist/val/LowerFuncVal.d.ts +12 -0
  66. package/dist/val/LowerFuncVal.js +40 -0
  67. package/dist/val/LowerFuncVal.js.map +1 -0
  68. package/dist/val/MapVal.d.ts +16 -0
  69. package/dist/val/MapVal.js +211 -0
  70. package/dist/val/MapVal.js.map +1 -0
  71. package/dist/val/MoveFuncVal.d.ts +11 -0
  72. package/dist/val/MoveFuncVal.js +41 -0
  73. package/dist/val/MoveFuncVal.js.map +1 -0
  74. package/dist/val/NilVal.d.ts +25 -0
  75. package/dist/val/NilVal.js +92 -0
  76. package/dist/val/NilVal.js.map +1 -0
  77. package/dist/val/NullVal.d.ts +9 -0
  78. package/dist/val/NullVal.js +18 -0
  79. package/dist/val/NullVal.js.map +1 -0
  80. package/dist/val/NumberVal.d.ts +9 -0
  81. package/dist/val/NumberVal.js +44 -0
  82. package/dist/val/NumberVal.js.map +1 -0
  83. package/dist/val/OpBaseVal.d.ts +18 -0
  84. package/dist/val/OpBaseVal.js +152 -0
  85. package/dist/val/OpBaseVal.js.map +1 -0
  86. package/dist/val/OpenFuncVal.d.ts +11 -0
  87. package/dist/val/OpenFuncVal.js +30 -0
  88. package/dist/val/OpenFuncVal.js.map +1 -0
  89. package/dist/val/PathFuncVal.d.ts +13 -0
  90. package/dist/val/PathFuncVal.js +42 -0
  91. package/dist/val/PathFuncVal.js.map +1 -0
  92. package/dist/val/PlusOpVal.d.ts +16 -0
  93. package/dist/val/PlusOpVal.js +59 -0
  94. package/dist/val/PlusOpVal.js.map +1 -0
  95. package/dist/val/PrefFuncVal.d.ts +11 -0
  96. package/dist/val/PrefFuncVal.js +39 -0
  97. package/dist/val/PrefFuncVal.js.map +1 -0
  98. package/dist/val/PrefVal.d.ts +15 -0
  99. package/dist/val/PrefVal.js +104 -0
  100. package/dist/val/PrefVal.js.map +1 -0
  101. package/dist/val/RefVal.d.ts +22 -0
  102. package/dist/val/RefVal.js +276 -0
  103. package/dist/val/RefVal.js.map +1 -0
  104. package/dist/val/ScalarKindVal.d.ts +16 -0
  105. package/dist/val/ScalarKindVal.js +76 -0
  106. package/dist/val/ScalarKindVal.js.map +1 -0
  107. package/dist/val/ScalarVal.d.ts +16 -0
  108. package/dist/val/ScalarVal.js +61 -0
  109. package/dist/val/ScalarVal.js.map +1 -0
  110. package/dist/val/StringVal.d.ts +10 -0
  111. package/dist/val/StringVal.js +19 -0
  112. package/dist/val/StringVal.js.map +1 -0
  113. package/dist/val/SuperFuncVal.d.ts +11 -0
  114. package/dist/val/SuperFuncVal.js +22 -0
  115. package/dist/val/SuperFuncVal.js.map +1 -0
  116. package/dist/val/TopVal.d.ts +16 -0
  117. package/dist/val/TopVal.js +38 -0
  118. package/dist/val/TopVal.js.map +1 -0
  119. package/dist/val/TypeFuncVal.d.ts +12 -0
  120. package/dist/val/TypeFuncVal.js +35 -0
  121. package/dist/val/TypeFuncVal.js.map +1 -0
  122. package/dist/val/UpperFuncVal.d.ts +12 -0
  123. package/dist/val/UpperFuncVal.js +40 -0
  124. package/dist/val/UpperFuncVal.js.map +1 -0
  125. package/dist/val/Val.d.ts +89 -0
  126. package/dist/val/Val.js +186 -0
  127. package/dist/val/Val.js.map +1 -0
  128. package/dist/val/VarVal.d.ts +13 -0
  129. package/dist/val/VarVal.js +109 -0
  130. package/dist/val/VarVal.js.map +1 -0
  131. package/dist/val/top.d.ts +2 -0
  132. package/dist/val/top.js +10 -0
  133. package/dist/val/top.js.map +1 -0
  134. package/dist/val/valutil.d.ts +2 -0
  135. package/dist/val/valutil.js +30 -0
  136. package/dist/val/valutil.js.map +1 -0
  137. package/package.json +3 -5
  138. package/src/aontu.ts +218 -0
  139. package/src/ctx.ts +201 -0
  140. package/src/err.ts +172 -0
  141. package/src/hints.ts +119 -0
  142. package/src/lang.ts +782 -0
  143. package/src/site.ts +34 -0
  144. package/src/tsconfig.json +16 -0
  145. package/src/type.ts +72 -0
  146. package/src/unify.ts +225 -0
  147. package/src/utility.ts +179 -0
  148. package/src/val/BagVal.ts +33 -0
  149. package/src/val/BooleanVal.ts +32 -0
  150. package/src/val/CloseFuncVal.ts +61 -0
  151. package/src/val/ConjunctVal.ts +285 -0
  152. package/src/val/CopyFuncVal.ts +69 -0
  153. package/src/val/DisjunctVal.ts +288 -0
  154. package/src/val/FeatureVal.ts +56 -0
  155. package/src/val/FuncBaseVal.ts +198 -0
  156. package/src/val/HideFuncVal.ts +85 -0
  157. package/src/val/IntegerVal.ts +75 -0
  158. package/src/val/JunctionVal.ts +54 -0
  159. package/src/val/KeyFuncVal.ts +67 -0
  160. package/src/val/ListVal.ts +286 -0
  161. package/src/val/LowerFuncVal.ts +73 -0
  162. package/src/val/MapVal.ts +316 -0
  163. package/src/val/MoveFuncVal.ts +79 -0
  164. package/src/val/NilVal.ts +156 -0
  165. package/src/val/NullVal.ts +38 -0
  166. package/src/val/NumberVal.ts +75 -0
  167. package/src/val/OpBaseVal.ts +231 -0
  168. package/src/val/OpenFuncVal.ts +60 -0
  169. package/src/val/PathFuncVal.ts +75 -0
  170. package/src/val/PlusOpVal.ts +92 -0
  171. package/src/val/PrefFuncVal.ts +73 -0
  172. package/src/val/PrefVal.ts +159 -0
  173. package/src/val/RefVal.ts +384 -0
  174. package/src/val/ScalarKindVal.ts +125 -0
  175. package/src/val/ScalarVal.ts +105 -0
  176. package/src/val/StringVal.ts +37 -0
  177. package/src/val/SuperFuncVal.ts +46 -0
  178. package/src/val/TopVal.ts +69 -0
  179. package/src/val/TypeFuncVal.ts +69 -0
  180. package/src/val/UpperFuncVal.ts +72 -0
  181. package/src/val/Val.ts +294 -0
  182. package/src/val/VarVal.ts +171 -0
  183. package/src/val/top.ts +12 -0
  184. package/src/val/valutil.ts +31 -0
@@ -0,0 +1,285 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+
5
+ import type {
6
+ Val,
7
+ ValSpec,
8
+ } from '../type'
9
+
10
+ import {
11
+ DONE,
12
+ } from '../type'
13
+
14
+ import { AontuContext } from '../ctx'
15
+
16
+ import { makeNilErr } from '../err'
17
+ import { unite } from '../unify'
18
+
19
+ import {
20
+ AontuError
21
+ } from '../err'
22
+
23
+
24
+ import { JunctionVal } from './JunctionVal'
25
+
26
+ import {
27
+ explainOpen,
28
+ ec,
29
+ explainClose,
30
+ propagateMarks,
31
+ } from '../utility'
32
+
33
+
34
+
35
+ import {
36
+ top
37
+ } from './top'
38
+
39
+
40
+ const CONJUNCT_ORDERING: Record<string, number> = {
41
+ PrefVal: 30000,
42
+ RefVal: 32500,
43
+ DisjunctVal: 35000,
44
+ ConjunctVal: 40000,
45
+ Any: 99999
46
+ }
47
+
48
+
49
+
50
+ // TODO: move main logic to op/conjunct
51
+ class ConjunctVal extends JunctionVal {
52
+ isConjunct = true
53
+
54
+ constructor(
55
+ spec: ValSpec,
56
+ ctx?: AontuContext
57
+ ) {
58
+ super(spec, ctx)
59
+ this.mark.type = !!spec.mark?.type
60
+ this.mark.hide = !!spec.mark?.hide
61
+ this.peg = (Array.isArray(this.peg) ? this.peg : [])
62
+ .filter((p: Val) => null != p && p.isVal)
63
+ this.peg?.map((v: Val) => {
64
+ propagateMarks(this, v)
65
+ return v
66
+ })
67
+ // console.log('CONJUNCT-ctor', this.peg.map((v: Val) => v.canon))
68
+ }
69
+
70
+ // NOTE: mutation!
71
+ append(peer: Val): ConjunctVal {
72
+ super.append(peer)
73
+ propagateMarks(this, peer)
74
+ return this
75
+ }
76
+
77
+
78
+ unify(peer: Val, ctx: AontuContext): Val {
79
+ peer = peer ?? top()
80
+
81
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Conjunct', this, peer)
82
+
83
+ let done = true
84
+
85
+ this.peg = norm(this.peg)
86
+
87
+
88
+ // Unify each term of conjunct against peer
89
+ let upeer: Val[] = []
90
+
91
+ let newtype = this.mark.type || peer.mark.type
92
+ let newhide = this.mark.hide || peer.mark.hide
93
+
94
+ for (let vI = 0; vI < this.peg.length; vI++) {
95
+ newtype = this.peg[vI].mark.type || newtype
96
+ newhide = this.peg[vI].mark.hide || newhide
97
+ }
98
+
99
+ for (let vI = 0; vI < this.peg.length; vI++) {
100
+ this.peg[vI].mark.type = newtype
101
+ this.peg[vI].mark.hide = newhide
102
+ // console.log('CONJUNCT-TERM', this.id, vI, this.peg[vI].canon)
103
+
104
+ upeer[vI] = (this.peg[vI].done && peer.isTop) ? this.peg[vI] :
105
+ unite(ctx.clone({ explain: ec(te, 'OWN') }), this.peg[vI], peer, 'cj-own')
106
+ upeer[vI].mark.type = newtype = newtype || upeer[vI].mark.type
107
+ upeer[vI].mark.hide = newhide = newhide || upeer[vI].mark.hide
108
+
109
+ // let prevdone = done
110
+ done = done && (DONE === upeer[vI].dc)
111
+
112
+ if (upeer[vI].isNil) {
113
+ return upeer[vI]
114
+ // return Nil.make(
115
+ // ctx,
116
+ // '&peer[' + upeer[vI].canon + ',' + peer.canon + ']',
117
+ // this.peg[vI],
118
+ // peer
119
+ // )
120
+ }
121
+ }
122
+
123
+ upeer = norm(upeer)
124
+ // console.log('CONJUNCT-UPEER', this.id, upeer.map((v: Val) => v.canon))
125
+
126
+ // Unify terms against each other
127
+
128
+ let outvals: Val[] = []
129
+ let val: Val
130
+
131
+
132
+ let t0 = upeer[0]
133
+
134
+ // next_term:
135
+ for (let pI = 0; pI < upeer.length; pI++) {
136
+ let t1 = upeer[pI + 1]
137
+
138
+ // console.log('CONJUNCT-TERMS-C', this.id, pI, t0, t1, 'OV=', outvals.map((v: Val) => v))
139
+
140
+ if (null == t1) {
141
+ outvals.push(t0)
142
+ newtype = this.mark.type || t0.mark.type
143
+ newhide = this.mark.hide || t0.mark.hide
144
+ }
145
+
146
+ // Can't unite with a RefVal, unless also a RefVal with same path.
147
+ // else if (t0 instanceof RefVal && !(t1 instanceof RefVal)) {
148
+ else if (t0.isRef && !(t1.isRef)) {
149
+ outvals.push(t0)
150
+ t0 = t1
151
+ }
152
+
153
+ else if (t1.isRef && !(t0.isRef)) {
154
+ outvals.push(t0)
155
+ t0 = t1
156
+ }
157
+
158
+
159
+ else {
160
+ val = unite(ctx.clone({ explain: ec(te, 'DEF') }), t0, t1, 'cj-peer-t0t1')
161
+ done = done && DONE === val.dc
162
+ newtype = this.mark.type || val.mark.type
163
+ newhide = this.mark.hide || val.mark.hide
164
+
165
+ // Unite was just a conjunt anyway, so discard.
166
+ if (val.isConjunct) {
167
+ outvals.push(t0)
168
+ t0 = t1
169
+ }
170
+ else if (val.isNil) {
171
+ return val
172
+ }
173
+ else {
174
+ t0 = val
175
+ }
176
+ // TODO: t0 should become this to avoid unnecessary repasses
177
+ // outvals.push(val)
178
+
179
+ // pI++
180
+ }
181
+ }
182
+
183
+ let out: Val
184
+
185
+ // console.log('CONJUCT-prepout', this.id, outvals.map((v: Val) => v.canon))
186
+
187
+ if (0 === outvals.length) {
188
+
189
+ // Empty conjuncts evaporate.
190
+ out = top()
191
+ }
192
+
193
+ // TODO: corrects CV[CV[1&/x]] issue above, but swaps term order!
194
+ else if (1 === outvals.length) {
195
+ out = outvals[0]
196
+ out.mark.type = newtype
197
+ out.mark.hide = newhide
198
+ }
199
+ else {
200
+ out = new ConjunctVal({ peg: outvals, mark: { type: newtype, hide: newhide } }, ctx)
201
+ }
202
+
203
+ out.dc = done ? DONE : this.dc + 1
204
+
205
+ // console.log('CONJUNCT-unify', this.id, sc, pc, '->', out.canon, 'D=' + out.dc, 'E=', this.err)
206
+
207
+ explainClose(te, out)
208
+
209
+ return out
210
+ }
211
+
212
+
213
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
214
+ let out = (super.clone(ctx, spec) as ConjunctVal)
215
+ return out
216
+ }
217
+
218
+
219
+ getJunctionSymbol(): string {
220
+ return '&'
221
+ }
222
+
223
+
224
+ gen(ctx?: AontuContext) {
225
+ // Unresolved conjunct cannot be generated, so always an error.
226
+ let nil = makeNilErr(
227
+ ctx,
228
+ 'conjunct',
229
+ this, // (formatPath(this.peg, this.absolute) as any),
230
+ undefined
231
+ )
232
+
233
+ // TODO: refactor to use Site
234
+ nil.path = this.path
235
+ nil.site.url = this.site.url
236
+ nil.site.row = this.site.row
237
+ nil.site.col = this.site.col
238
+
239
+ // descErr(nil, ctx)
240
+
241
+ if (null == ctx) {
242
+ throw new AontuError(nil.msg)
243
+ }
244
+
245
+ return undefined
246
+ }
247
+ }
248
+
249
+
250
+
251
+ // Normalize Conjunct:
252
+ // - flatten child conjuncts
253
+ // - consistent sorting of terms
254
+ function norm(terms: Val[]): Val[] {
255
+
256
+ let expand: Val[] = []
257
+ for (let tI = 0, pI = 0; tI < terms.length; tI++, pI++) {
258
+ if (terms[tI].isConjunct) {
259
+ expand.push(...terms[tI].peg)
260
+ pI += terms[tI].peg.length - 1
261
+ }
262
+ else {
263
+ expand[pI] = terms[tI]
264
+ }
265
+ }
266
+
267
+
268
+ // Consistent ordering ensures order independent unification.
269
+ expand = expand.sort((a: Val, b: Val) => {
270
+ const an = CONJUNCT_ORDERING[a.constructor.name] ?? CONJUNCT_ORDERING.Any
271
+ const bn = CONJUNCT_ORDERING[b.constructor.name] ?? CONJUNCT_ORDERING.Any
272
+ return an - bn
273
+ })
274
+
275
+ // console.log('NORM', expand.map(t => t.canon).join(', '))
276
+
277
+
278
+ return expand
279
+ }
280
+
281
+
282
+ export {
283
+ norm,
284
+ ConjunctVal,
285
+ }
@@ -0,0 +1,69 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+
5
+ import type {
6
+ Val,
7
+ ValSpec,
8
+ } from '../type'
9
+
10
+ import {
11
+ AontuContext,
12
+ } from '../ctx'
13
+
14
+ import { makeNilErr } from '../err'
15
+
16
+ import { NilVal } from '../val/NilVal'
17
+
18
+
19
+ import {
20
+ walk
21
+ } from '../utility'
22
+
23
+
24
+ import { FuncBaseVal } from './FuncBaseVal'
25
+
26
+
27
+ class CopyFuncVal extends FuncBaseVal {
28
+ isCopyFunc = true
29
+
30
+ resolved?: Val
31
+
32
+ constructor(
33
+ spec: ValSpec,
34
+ ctx?: AontuContext
35
+ ) {
36
+ super(spec, ctx)
37
+ this.mark.type = false
38
+ this.mark.hide = false
39
+ }
40
+
41
+
42
+ make(_ctx: AontuContext, spec: ValSpec): Val {
43
+ return new CopyFuncVal(spec)
44
+ }
45
+
46
+ funcname() {
47
+ return 'copy'
48
+ }
49
+
50
+ resolve(ctx: AontuContext | undefined, args: Val[]) {
51
+ const val = args?.[0]
52
+ const out = null == val || null == ctx ?
53
+ makeNilErr(ctx, 'invalid-arg', this) :
54
+ val.clone(ctx, { mark: { type: false, hide: false } })
55
+
56
+ walk(out, (_key: string | number | undefined, val: Val) => {
57
+ val.mark.type = false
58
+ val.mark.hide = false
59
+ return val
60
+ })
61
+
62
+ return out
63
+ }
64
+ }
65
+
66
+
67
+ export {
68
+ CopyFuncVal,
69
+ }
@@ -0,0 +1,288 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+
5
+ import type {
6
+ Val,
7
+ ValSpec,
8
+ } from '../type'
9
+
10
+ import {
11
+ DONE,
12
+ } from '../type'
13
+
14
+ import { AontuContext } from '../ctx'
15
+
16
+ import { makeNilErr } from '../err'
17
+ import { unite } from '../unify'
18
+
19
+ import {
20
+ explainOpen,
21
+ ec,
22
+ explainClose,
23
+ } from '../utility'
24
+
25
+ import {
26
+ Site
27
+ } from '../site'
28
+
29
+ import {
30
+ top
31
+ } from './top'
32
+
33
+ import { NilVal } from '../val/NilVal'
34
+ import { PrefVal } from '../val/PrefVal'
35
+ import { JunctionVal } from '../val/JunctionVal'
36
+
37
+
38
+
39
+ // TODO: move main logic to op/disjunct
40
+ class DisjunctVal extends JunctionVal {
41
+ isDisjunct = true
42
+
43
+ prefsRanked = false
44
+
45
+ // TODO: sites from normalization of orginal Disjuncts, as well as child pegs
46
+ constructor(
47
+ spec: {
48
+ peg: Val[]
49
+ },
50
+ ctx?: AontuContext,
51
+ _sites?: Site[]
52
+ ) {
53
+ super(spec, ctx)
54
+ }
55
+
56
+
57
+ // NOTE: mutation!
58
+ append(peer: Val): DisjunctVal {
59
+ super.append(peer)
60
+ this.prefsRanked = false
61
+ return this
62
+ }
63
+
64
+
65
+ unify(peer: Val, ctx: AontuContext): Val {
66
+ peer = peer ?? top()
67
+
68
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Disjunct', this, peer)
69
+
70
+ if (!this.prefsRanked) {
71
+ this.rankPrefs(ctx)
72
+ }
73
+
74
+ // // // console.log('DISJUNCT-unify-A', this.id, this.canon)
75
+
76
+ let done = true
77
+
78
+ let oval: Val[] = []
79
+
80
+ // Conjunction (&) distributes over disjunction (|)
81
+ for (let vI = 0; vI < this.peg.length; vI++) {
82
+ const v = this.peg[vI]
83
+ const cloneCtx = ctx?.clone({ err: [] })
84
+
85
+ // // // console.log('DJ-DIST-A', this.peg[vI].canon, peer.canon)
86
+ oval[vI] = unite(cloneCtx.clone({ explain: ec(te, 'DIST:' + vI) }), v, peer, 'dj-peer')
87
+ // // // console.log('DJ-DIST-B', oval[vI].canon, cloneCtx?.err)
88
+
89
+ if (0 < cloneCtx?.err.length) {
90
+ oval[vI] = makeNilErr(cloneCtx, '|:empty-dist', this)
91
+ }
92
+
93
+ done = done && DONE === oval[vI].dc
94
+ }
95
+
96
+ // // // console.log('DISJUNCT-unify-B', this.id, oval.map(v => v.canon))
97
+
98
+ // Remove duplicates, and normalize
99
+ if (1 < oval.length) {
100
+ for (let vI = 0; vI < oval.length; vI++) {
101
+ if (oval[vI].isDisjunct) {
102
+ oval.splice(vI, 1, ...oval[vI].peg)
103
+ }
104
+ }
105
+
106
+ // // // console.log('DISJUNCT-unify-C', this.id, oval.map(v => v.id + '=' + v.canon))
107
+
108
+ // TODO: not an error Nil!
109
+ let remove = new NilVal()
110
+ for (let vI = 0; vI < oval.length; vI++) {
111
+ for (let kI = vI + 1; kI < oval.length; kI++) {
112
+ if (oval[kI].same(oval[vI])) {
113
+ oval[kI] = remove
114
+ }
115
+ }
116
+ }
117
+
118
+ // // // console.log('DISJUNCT-unify-D', this.id, oval.map(v => v.canon))
119
+
120
+ oval = oval.filter(v => !v.isNil)
121
+ }
122
+
123
+ let out: Val
124
+
125
+ if (1 == oval.length) {
126
+ out = oval[0]
127
+ }
128
+ else if (0 == oval.length) {
129
+ return makeNilErr(ctx, '|:empty', this, peer)
130
+ }
131
+ else {
132
+ out = new DisjunctVal({ peg: oval }, ctx)
133
+ }
134
+
135
+ out.dc = done ? DONE : this.dc + 1
136
+
137
+ // // // console.log('DISJUNCT-unify',
138
+ // this.id, sc, pc, '->', out.canon, 'D=' + out.dc, 'E=', this.err)
139
+
140
+ explainClose(te, out)
141
+
142
+ return out
143
+ }
144
+
145
+
146
+ rankPrefs(ctx: AontuContext) {
147
+ let lastpref: PrefVal | undefined = undefined
148
+ let lastprefI = -1
149
+
150
+ // // // console.log('RP-A', this.peg.map((p: Val) => p.canon))
151
+
152
+ for (let vI = 0; vI < this.peg.length; vI++) {
153
+ const v = this.peg[vI]
154
+ if (v instanceof PrefVal) {
155
+ if (null != lastpref) {
156
+ if (v.rank === lastpref.rank) {
157
+ const pref = v.unify(lastpref, ctx) as PrefVal
158
+ if (pref.isNil) {
159
+ return pref
160
+ }
161
+ else {
162
+ this.peg[lastprefI] = pref
163
+ lastpref = pref
164
+ this.peg[vI] = null
165
+ }
166
+ // return Nil.make(ctx, '|:prefs', lastpref, v, 'associate')
167
+ }
168
+ else if (v.rank < lastpref.rank) {
169
+ this.peg[lastprefI] = null
170
+ lastpref = v
171
+ lastprefI = vI
172
+ }
173
+ else {
174
+ this.peg[vI] = null
175
+ }
176
+ }
177
+ else {
178
+ lastpref = v
179
+ lastprefI = vI
180
+ }
181
+ }
182
+ else if (v.isDisjunct) {
183
+ let subrank: any = v.rankPrefs(ctx)
184
+ if (subrank instanceof PrefVal) {
185
+ this.peg[vI] = subrank
186
+ lastpref = subrank
187
+ lastprefI = vI
188
+ }
189
+ }
190
+ }
191
+
192
+ this.peg = this.peg.filter((p: any) => null != p)
193
+ this.prefsRanked = true
194
+
195
+ // // // console.log('RP-Z', this.peg.map((p: Val) => p.canon))
196
+
197
+ if (1 === this.peg.length && this.peg[0] instanceof PrefVal) {
198
+ return this.peg[0]
199
+ }
200
+ }
201
+
202
+
203
+
204
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
205
+ let out = (super.clone(ctx, spec) as DisjunctVal)
206
+ return out
207
+ }
208
+
209
+
210
+ getJunctionSymbol(): string {
211
+ return '|'
212
+ }
213
+
214
+
215
+ gen(ctx: AontuContext) {
216
+ // TODO: move this to main unify
217
+
218
+ // console.log('DJ-GEN', this.peg.map((p: any) => p.canon), ctx.err)
219
+
220
+ if (0 < this.peg.length) {
221
+
222
+ let vals = this.peg.filter((v: Val) => v instanceof PrefVal)
223
+ // // // console.log('DJ-GEN-VALS-A', vals.map((p: any) => p.canon))
224
+
225
+ vals = 0 === vals.length ? this.peg : vals
226
+
227
+ let val = vals[0]
228
+
229
+ // TODO: over unifies complex types like maps
230
+ // ({x:1}|{y:2})&{z:3} should be {"x":1,"z":3}|{"y":2,"z":3} not { x:1, z:3, y:2 }
231
+ for (let vI = 1; vI < vals.length; vI++) {
232
+ let valnext = val.unify(this.peg[vI], ctx)
233
+ // // // console.log('DJ-GEN-VALS-NEXT', valnext.canon)
234
+ val = valnext
235
+ }
236
+
237
+ // console.log('DJ-GEN-VALS-B', val.canon)
238
+ const out = val.gen(ctx)
239
+ // console.log('DJ-GEN-VALS-C', out)
240
+ return out
241
+ }
242
+
243
+ return super.gen(ctx)
244
+
245
+ // // // console.log('DJ-GEN', this.peg)
246
+
247
+ // if (1 === this.peg.length) {
248
+ // return this.peg[0].gen(ctx)
249
+ // }
250
+ // else if (1 < this.peg.length) {
251
+ // let peg = this.peg.filter((v: Val) => v instanceof PrefVal)
252
+
253
+ // if (1 === peg.length) {
254
+ // return peg[0].gen(ctx)
255
+ // }
256
+ // else {
257
+
258
+ // let nil = Nil.make(
259
+ // ctx,
260
+ // 'disjunct',
261
+ // this,
262
+ // undefined
263
+ // )
264
+
265
+ // // TODO: refactor to use Site
266
+ // nil.path = this.path
267
+ // nil.site.url = this.url
268
+ // nil.site.row = this.row
269
+ // nil.site.col = this.col
270
+
271
+ // // descErr(nil, ctx)
272
+
273
+ // if (null == ctx) {
274
+ // throw new Error(nil.msg)
275
+ // }
276
+ // }
277
+
278
+ // return undefined
279
+ // }
280
+ }
281
+ }
282
+
283
+
284
+
285
+
286
+ export {
287
+ DisjunctVal,
288
+ }
@@ -0,0 +1,56 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ ValSpec,
6
+ } from '../type'
7
+
8
+ import {
9
+ AontuContext,
10
+ } from '../ctx'
11
+
12
+
13
+ import { Val } from './Val'
14
+
15
+ import { top } from './top'
16
+
17
+ import { AontuError, descErr, makeNilErr } from '../err'
18
+
19
+
20
+ abstract class FeatureVal extends Val {
21
+ isFeature = true
22
+
23
+ constructor(
24
+ spec: ValSpec,
25
+ ctx?: AontuContext
26
+ ) {
27
+ super(spec, ctx)
28
+ }
29
+
30
+
31
+ superior(): Val {
32
+ return top()
33
+ }
34
+
35
+
36
+ gen(ctx: AontuContext) {
37
+ // Unresolved nil cannot be generated, so always an error.
38
+
39
+ let nerr = makeNilErr(ctx, 'no_gen', this)
40
+ descErr(nerr, ctx)
41
+ ctx?.adderr(nerr)
42
+
43
+ if (null == ctx || !ctx?.collect) {
44
+ const aerr = new AontuError(nerr.msg, [nerr])
45
+ throw aerr
46
+ }
47
+
48
+ return undefined
49
+ }
50
+
51
+ }
52
+
53
+
54
+ export {
55
+ FeatureVal,
56
+ }