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,92 @@
1
+ /* Copyright (c) 2024-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ Val,
6
+ ValSpec,
7
+ } from '../type'
8
+
9
+
10
+ import {
11
+ AontuContext,
12
+ } from '../ctx'
13
+
14
+
15
+
16
+ import { IntegerVal } from '../val/IntegerVal'
17
+ import { NumberVal } from '../val/NumberVal'
18
+ import { StringVal } from '../val/StringVal'
19
+ import { BooleanVal } from '../val/BooleanVal'
20
+ import { OpBaseVal } from './OpBaseVal'
21
+
22
+
23
+ class PlusOpVal extends OpBaseVal {
24
+ isPlusOp = true
25
+
26
+ constructor(
27
+ spec: ValSpec,
28
+ ctx?: AontuContext
29
+ ) {
30
+ super(spec, ctx)
31
+ }
32
+
33
+
34
+ make(_ctx: AontuContext, spec: ValSpec): Val {
35
+ return new PlusOpVal(spec)
36
+ }
37
+
38
+ opname() {
39
+ return 'plus'
40
+ }
41
+
42
+
43
+ operate(_ctx: AontuContext, args: Val[]) {
44
+ let a: any = this.primatize(args[0]?.peg)
45
+ let b: any = this.primatize(args[1]?.peg)
46
+
47
+ let peg = undefined
48
+ if (undefined === a && undefined !== b) {
49
+ peg = b
50
+ }
51
+ else if (undefined === b && undefined !== a) {
52
+ peg = a
53
+ }
54
+ else {
55
+ const at = typeof a
56
+ const bt = typeof b
57
+ if ('boolean' === at && 'boolean' === bt) {
58
+ peg = a || b
59
+ }
60
+ else {
61
+ peg = a + b
62
+ }
63
+ }
64
+
65
+ let pegtype = typeof peg
66
+
67
+ let out = undefined
68
+
69
+ if ('string' === pegtype) {
70
+ out = new StringVal({ peg })
71
+ }
72
+ else if ('boolean' === pegtype) {
73
+ out = new BooleanVal({ peg })
74
+ }
75
+ else if ('number' === pegtype) {
76
+ out = Number.isInteger(peg) ? new IntegerVal({ peg }) : new NumberVal({ peg })
77
+ }
78
+
79
+ return out
80
+ }
81
+
82
+
83
+ get canon() {
84
+ return this.peg[0]?.canon + '+' + this.peg[1]?.canon
85
+ }
86
+
87
+ }
88
+
89
+
90
+ export {
91
+ PlusOpVal,
92
+ }
@@ -0,0 +1,73 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import type {
5
+ Val,
6
+ ValSpec,
7
+ } from '../type'
8
+
9
+ import {
10
+ AontuContext,
11
+ } from '../ctx'
12
+
13
+ import { makeNilErr } from '../err'
14
+
15
+ import {
16
+ walk
17
+ } from '../utility'
18
+
19
+ import { NilVal } from '../val/NilVal'
20
+ import { PrefVal } from '../val/PrefVal'
21
+ import { FuncBaseVal } from './FuncBaseVal'
22
+
23
+
24
+ class PrefFuncVal extends FuncBaseVal {
25
+ isPrefFunc = true
26
+
27
+ constructor(
28
+ spec: ValSpec,
29
+ ctx?: AontuContext
30
+ ) {
31
+ super(spec, ctx)
32
+ }
33
+
34
+
35
+ make(_ctx: AontuContext, spec: ValSpec): Val {
36
+ return new PrefFuncVal(spec)
37
+ }
38
+
39
+ funcname() {
40
+ return 'pref'
41
+ }
42
+
43
+
44
+ resolve(ctx: AontuContext, args: Val[]) {
45
+ let out = args[0] ?? makeNilErr(ctx, 'arg', this)
46
+
47
+ if (!out.isNil) {
48
+ out = out.clone(ctx)
49
+
50
+ // Wrap every child val in a PrefVal
51
+ out = walk(out, (_key: string | number | undefined, val: Val) => {
52
+ let oval = val
53
+ // console.log('PREFVAL', _key, oval.canon, oval.constructor.name)
54
+ if (
55
+ val.isScalar
56
+ || val.isPref
57
+ ) {
58
+ oval = new PrefVal({ peg: val }, ctx)
59
+ }
60
+
61
+ return oval
62
+ })
63
+ }
64
+
65
+ return out
66
+ }
67
+
68
+ }
69
+
70
+
71
+ export {
72
+ PrefFuncVal,
73
+ }
@@ -0,0 +1,159 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+ import type {
4
+ Val,
5
+ ValSpec,
6
+ } from '../type'
7
+
8
+ import {
9
+ DONE,
10
+ } from '../type'
11
+
12
+ import { AontuContext } from '../ctx'
13
+ import { unite } from '../unify'
14
+ import { AontuError } from '../err'
15
+
16
+ import {
17
+ explainOpen,
18
+ ec,
19
+ explainClose,
20
+ } from '../utility'
21
+
22
+
23
+ import { top } from './top'
24
+
25
+ import { FeatureVal } from './FeatureVal'
26
+
27
+
28
+ class PrefVal extends FeatureVal {
29
+ isPref = true
30
+
31
+ superpeg: Val
32
+ rank: number = 0
33
+
34
+ constructor(
35
+ spec: ValSpec,
36
+ ctx?: AontuContext
37
+ ) {
38
+ super(spec, ctx)
39
+ // this.pref = spec.pref || spec.peg
40
+
41
+ // this.superpeg = makeSuper(spec.peg)
42
+
43
+ if (spec.peg instanceof PrefVal) {
44
+ this.rank = 1 + spec.peg.rank
45
+ }
46
+
47
+ this.superpeg = this.peg.superior()
48
+ // console.log('PVC', this.peg.canon, this.superpeg.canon)
49
+ }
50
+
51
+
52
+ // PrefVal unify always returns a PrefVal
53
+ // PrefVals can only be removed by becoming Nil in a Disjunct
54
+ unify(peer: Val, ctx: AontuContext): Val {
55
+ peer = peer ?? top()
56
+
57
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Pref', this, peer)
58
+ let done = true
59
+ let out: Val = this
60
+ let why = ''
61
+
62
+
63
+ if (!this.peg.done) {
64
+ const resolved = unite(ctx.clone({ explain: ec(te, 'RES') }), this.peg, top(), 'pref/resolve')
65
+ // console.log('PREF-RESOLVED', this.peg.canon, '->', resolved)
66
+ this.peg = resolved
67
+ }
68
+
69
+
70
+ if (peer instanceof PrefVal) {
71
+ why += 'pref-'
72
+ if (this.rank < peer.rank) {
73
+ out = this
74
+ why += 'rank-win'
75
+ }
76
+ else if (peer.rank < this.rank) {
77
+ out = peer
78
+ why += 'rank-lose'
79
+ }
80
+ else {
81
+ let peg = unite(ctx.clone({ explain: ec(te, 'PEER') }), this.peg, peer.peg, 'pref-peer/' + this.id)
82
+ out = new PrefVal({ peg }, ctx)
83
+ why += 'rank-same'
84
+ }
85
+ }
86
+ else if (!peer.isTop) {
87
+ why += 'super-'
88
+
89
+ // if (this.superpeg instanceof Nil) {
90
+ // out = peer
91
+ // why += 'nil'
92
+ // }
93
+ // else {
94
+ // why += 'unify'
95
+
96
+ out = unite(ctx.clone({ explain: ec(te, 'SUPER') }), this.superpeg, peer, 'pref-super/' + this.id)
97
+ if (out.same(this.superpeg)) {
98
+ out = this.peg
99
+ why += 'same'
100
+ }
101
+
102
+ // }
103
+ }
104
+ else {
105
+ why += 'none'
106
+ }
107
+
108
+ out.dc = done ? DONE : this.dc + 1
109
+
110
+ // console.log('PREFVAL-OUT', why, this.canon, peer.canon, '->', out.canon, out.done)
111
+
112
+ explainClose(te, out)
113
+
114
+ return out
115
+ }
116
+
117
+
118
+ same(peer: Val): boolean {
119
+ if (null == peer) {
120
+ return false
121
+ }
122
+
123
+ let pegsame = (this.peg === peer.peg) ||
124
+ (this.peg.isVal && this.peg.same(peer.peg))
125
+
126
+ return pegsame
127
+ }
128
+
129
+
130
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
131
+ let out = (super.clone(ctx, spec) as PrefVal)
132
+ // out.pref = this.pref.clone(null, ctx)
133
+ return out
134
+ }
135
+
136
+
137
+ get canon() {
138
+ // return this.pref instanceof Nil ? this.peg.canon : '*' + this.pref.canon
139
+ return '*' + this.peg.canon
140
+ }
141
+
142
+
143
+ gen(ctx?: AontuContext) {
144
+ let val = this.peg
145
+
146
+ if (val.isNil) {
147
+ if (null == ctx) {
148
+ throw new AontuError(val.msg)
149
+ }
150
+ }
151
+
152
+ return val.gen(ctx)
153
+ }
154
+ }
155
+
156
+
157
+ export {
158
+ PrefVal,
159
+ }
@@ -0,0 +1,384 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import {
5
+ walk,
6
+ explainOpen,
7
+ ec,
8
+ explainClose,
9
+ } from '../utility'
10
+
11
+
12
+ import type {
13
+ Val,
14
+ ValSpec,
15
+ } from '../type'
16
+
17
+ import {
18
+ DONE,
19
+ } from '../type'
20
+
21
+ import { AontuContext } from '../ctx'
22
+
23
+ import { makeNilErr } from '../err'
24
+ import { unite } from '../unify'
25
+
26
+
27
+ import {
28
+ top
29
+ } from './top'
30
+
31
+
32
+ import { StringVal } from './StringVal'
33
+ import { IntegerVal } from './IntegerVal'
34
+ import { NumberVal } from './NumberVal'
35
+ import { ConjunctVal } from './ConjunctVal'
36
+ import { VarVal } from './VarVal'
37
+ import { FeatureVal } from './FeatureVal'
38
+
39
+
40
+
41
+ class RefVal extends FeatureVal {
42
+ isRef = true
43
+
44
+ absolute: boolean = false
45
+ prefix: boolean = false
46
+
47
+ constructor(
48
+ spec: {
49
+ peg: any[],
50
+ absolute?: boolean,
51
+ prefix?: boolean
52
+ },
53
+ ctx?: AontuContext
54
+ ) {
55
+ super(spec, ctx)
56
+ this.peg = []
57
+
58
+ this.absolute = true === this.absolute ? true : // absolute sticks
59
+ true === spec.absolute ? true : false
60
+
61
+ this.prefix = true === spec.prefix
62
+
63
+ for (let pI = 0; pI < spec.peg.length; pI++) {
64
+ this.append(spec.peg[pI])
65
+ }
66
+
67
+ //console.log('RefVal', this.id, this.peg)
68
+ }
69
+
70
+
71
+ append(part: any) {
72
+ let partval
73
+
74
+ // console.log('APPEND', part)
75
+
76
+ if ('string' === typeof part) {
77
+ partval = part
78
+ this.peg.push(partval)
79
+ }
80
+
81
+ else if (part instanceof StringVal) {
82
+ partval = part.peg
83
+ this.peg.push(partval)
84
+ }
85
+
86
+ else if (part instanceof IntegerVal) {
87
+ // partval = '' + part.peg
88
+ partval = part.src
89
+ this.peg.push(partval)
90
+ }
91
+
92
+ // TODO: this is a bit of a hack, review
93
+ // Seems like a fundamental ambiguity?
94
+ // Resolved by path function
95
+ else if (part instanceof NumberVal) {
96
+ // let partvals: string[] = part.peg.toFixed(11).replace(/(\.0)?0+$/, '$1').split('.')
97
+ let partvals: string[] = part.src.split('.')
98
+ this.peg.push(...partvals)
99
+ }
100
+
101
+ else if (part instanceof VarVal) {
102
+ partval = part
103
+ this.peg.push(partval)
104
+ }
105
+
106
+ else if (part instanceof RefVal) {
107
+ if (part.absolute) {
108
+ this.absolute = true
109
+ }
110
+
111
+ if (this.prefix) {
112
+ if (part.prefix) {
113
+ this.peg.push('.')
114
+ }
115
+ }
116
+ else {
117
+ if (part.prefix) {
118
+ if (0 === this.peg.length) {
119
+ this.prefix = true
120
+ }
121
+
122
+ else if (0 < this.peg.length) {
123
+ this.peg.push('.')
124
+ }
125
+ }
126
+ }
127
+
128
+ this.peg.push(...part.peg)
129
+ }
130
+ }
131
+
132
+
133
+ unify(peer: Val, ctx: AontuContext): Val {
134
+ peer = peer ?? top()
135
+
136
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Ref', this, peer)
137
+ let out: Val = this
138
+ // let why = 'id'
139
+
140
+ if (this.id !== peer.id) {
141
+
142
+ // TODO: not resolved when all Vals in path are done is an error
143
+ // as path cannot be found
144
+ // let resolved: Val | undefined = null == ctx ? this : ctx.find(this)
145
+ let found: Val | undefined = null == ctx ? this : this.find(ctx)
146
+
147
+ const resolved = found ?? this
148
+
149
+ if (null == resolved && this.canon === peer.canon) {
150
+ out = this
151
+ }
152
+ else if (resolved instanceof RefVal) {
153
+ if (peer.isTop) {
154
+ out = this
155
+ // why = 'pt'
156
+ }
157
+ else if (peer.isNil) {
158
+ out = makeNilErr(ctx, 'ref[' + this.peg + ']', this, peer)
159
+ // why = 'pn'
160
+ }
161
+
162
+ // same path
163
+ // else if (this.peg === peer.peg) {
164
+ else if (this.canon === peer.canon) {
165
+ out = this
166
+ // why = 'pp'
167
+ }
168
+
169
+ else {
170
+ // Ensure RefVal done is incremented
171
+ this.dc = DONE === this.dc ? DONE : this.dc + 1
172
+ out = new ConjunctVal({ peg: [this, peer] }, ctx)
173
+ // why = 'cj'
174
+ }
175
+ }
176
+ else {
177
+ out = unite(ctx.clone({ explain: ec(te, 'RES') }), resolved, peer, 'ref')
178
+ // why = 'u'
179
+ }
180
+
181
+ out.dc = DONE === out.dc ? DONE : this.dc + 1
182
+ }
183
+
184
+ explainClose(te, out)
185
+ return out
186
+ }
187
+
188
+
189
+ find(ctx: AontuContext) {
190
+ let out: Val | undefined = undefined
191
+
192
+ if (this.path.join('.').startsWith(this.peg.join('.'))) {
193
+ out = makeNilErr(ctx, 'path_cycle', this)
194
+ }
195
+ else {
196
+
197
+ let parts: string[] = []
198
+
199
+ let modes: string[] = []
200
+
201
+ for (let pI = 0; pI < this.peg.length; pI++) {
202
+ let part = this.peg[pI]
203
+ if (part instanceof VarVal) {
204
+ let strval = (part as VarVal).peg
205
+ let name = strval ? '' + strval.peg : ''
206
+
207
+ if ('KEY' === name) {
208
+ if (pI === this.peg.length - 1) {
209
+ modes.push(name)
210
+ }
211
+ else {
212
+ // TODO: return a Nil explaining error
213
+ return
214
+ }
215
+ }
216
+
217
+ if ('SELF' === name) {
218
+ if (pI === 0) {
219
+ modes.push(name)
220
+ }
221
+ else {
222
+ // TODO: return a Nil explaining error
223
+ return
224
+ }
225
+ }
226
+ else if ('PARENT' === name) {
227
+ if (pI === 0) {
228
+ modes.push(name)
229
+ }
230
+ else {
231
+ // TODO: return a Nil explaining error
232
+ return
233
+ }
234
+ }
235
+ else if (0 === modes.length) {
236
+ part = (part as VarVal).unify(top(), ctx)
237
+ if (part.isNil) {
238
+ // TODO: var not found, so can't find path
239
+ return
240
+ }
241
+ else {
242
+ part = '' + part.peg
243
+ }
244
+ }
245
+ }
246
+ else {
247
+ parts.push(part)
248
+ }
249
+ }
250
+
251
+ let refpath: string[] = []
252
+
253
+ if (this.absolute) {
254
+ refpath = parts
255
+ }
256
+ else {
257
+ // TODO: deprecate $KEY, etc
258
+ refpath = this.path.slice(
259
+ 0,
260
+ (
261
+ modes.includes('SELF') ? 0 :
262
+ modes.includes('PARENT') ? -1 :
263
+ -1 // siblings
264
+ )
265
+ ).concat(parts)
266
+ }
267
+
268
+ let sep = '.'
269
+ refpath = refpath
270
+ .reduce(((a: string[], p: string) =>
271
+ (p === sep ? a.length = a.length - 1 : a.push(p), a)), [])
272
+
273
+ if (modes.includes('KEY')) {
274
+ let key = this.path[this.path.length - 2]
275
+ let sv = new StringVal({ peg: null == key ? '' : key }, ctx)
276
+
277
+ // TODO: other props?
278
+ sv.dc = DONE
279
+ sv.path = this.path
280
+
281
+ return sv
282
+ }
283
+
284
+ let node = ctx.root as Val
285
+ let pI = 0
286
+
287
+ if (null != node) {
288
+ for (; pI < refpath.length; pI++) {
289
+ let part = refpath[pI]
290
+
291
+ if (null == node) {
292
+ break
293
+ }
294
+ else if (node.isMap) {
295
+ node = node.peg[part]
296
+ }
297
+ else if (node.isList) {
298
+ node = node.peg[part]
299
+ }
300
+ else {
301
+ break;
302
+ }
303
+ }
304
+ }
305
+
306
+ if (pI === refpath.length) {
307
+ out = node
308
+
309
+ // Types and hidden values are cloned and made concrete
310
+ if (null != out && (out.mark.type || out.mark.hide)) {
311
+ out = out.clone(ctx)
312
+
313
+ walk(out, (_key: string | number | undefined, val: Val) => {
314
+ val.mark.type = false
315
+ val.mark.hide = false
316
+ return val
317
+ })
318
+ }
319
+ }
320
+ }
321
+
322
+ return out
323
+ }
324
+
325
+
326
+ same(peer: Val): boolean {
327
+ return null == peer ? false : this.peg === peer.peg
328
+ }
329
+
330
+
331
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
332
+ let out = (super.clone(ctx, {
333
+ peg: this.peg,
334
+ absolute: this.absolute,
335
+ ...(spec || {})
336
+ }) as RefVal)
337
+ return out
338
+ }
339
+
340
+
341
+ get canon() {
342
+ let str =
343
+ (this.absolute ? '$' : '') +
344
+ (0 < this.peg.length ? '.' : '') +
345
+ // this.peg.join(this.sep)
346
+ this.peg.map((p: any) => '.' === p ? '' :
347
+ (p.isVal ? p.canon : '' + p))
348
+ .join('.')
349
+ return str
350
+ }
351
+
352
+
353
+ gen(ctx: AontuContext) {
354
+ // Unresolved ref cannot be generated, so always an error.
355
+ let nil = makeNilErr(
356
+ ctx,
357
+ 'ref',
358
+ this, // (formatPath(this.peg, this.absolute) as any),
359
+ undefined,
360
+ )
361
+
362
+ // TODO: refactor to use Site pointer
363
+ nil.path = this.path
364
+ nil.site.url = this.site.url
365
+ nil.site.row = this.site.row
366
+ nil.site.col = this.site.col
367
+
368
+ return undefined
369
+ }
370
+
371
+
372
+ inspection() {
373
+ return [
374
+ this.absolute ? 'absolute' : '',
375
+ this.prefix ? 'prefix' : '',
376
+ ].filter(p => '' != p).join(',')
377
+ }
378
+
379
+ }
380
+
381
+
382
+ export {
383
+ RefVal,
384
+ }