aontu 0.30.1 → 0.31.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 (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 +90 -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 +162 -0
  22. package/dist/unify.js.map +1 -0
  23. package/dist/utility.d.ts +14 -0
  24. package/dist/utility.js +128 -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 +14 -0
  39. package/dist/val/CopyFuncVal.js +43 -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 +131 -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 +13 -0
  60. package/dist/val/KeyFuncVal.js +64 -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 +233 -0
  70. package/dist/val/MapVal.js.map +1 -0
  71. package/dist/val/MoveFuncVal.d.ts +12 -0
  72. package/dist/val/MoveFuncVal.js +44 -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 +112 -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 +310 -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 +62 -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 +90 -0
  126. package/dist/val/Val.js +205 -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 +129 -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 +233 -0
  147. package/src/utility.ts +185 -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 +82 -0
  153. package/src/val/DisjunctVal.ts +288 -0
  154. package/src/val/FeatureVal.ts +56 -0
  155. package/src/val/FuncBaseVal.ts +204 -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 +103 -0
  160. package/src/val/ListVal.ts +286 -0
  161. package/src/val/LowerFuncVal.ts +73 -0
  162. package/src/val/MapVal.ts +349 -0
  163. package/src/val/MoveFuncVal.ts +83 -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 +170 -0
  173. package/src/val/RefVal.ts +436 -0
  174. package/src/val/ScalarKindVal.ts +125 -0
  175. package/src/val/ScalarVal.ts +107 -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 +318 -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,436 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+
4
+ import Util from 'node:util'
5
+
6
+ import {
7
+ walk,
8
+ explainOpen,
9
+ ec,
10
+ explainClose,
11
+ } from '../utility'
12
+
13
+
14
+ import type {
15
+ Val,
16
+ ValSpec,
17
+ } from '../type'
18
+
19
+ import {
20
+ DONE,
21
+ } from '../type'
22
+
23
+ import { AontuContext } from '../ctx'
24
+
25
+ import { makeNilErr } from '../err'
26
+ import { unite } from '../unify'
27
+
28
+
29
+ import {
30
+ top
31
+ } from './top'
32
+
33
+
34
+ import { StringVal } from './StringVal'
35
+ import { IntegerVal } from './IntegerVal'
36
+ import { NumberVal } from './NumberVal'
37
+ import { ConjunctVal } from './ConjunctVal'
38
+ import { VarVal } from './VarVal'
39
+ import { FeatureVal } from './FeatureVal'
40
+
41
+
42
+
43
+ class RefVal extends FeatureVal {
44
+ isRef = true
45
+
46
+ absolute: boolean = false
47
+ prefix: boolean = false
48
+
49
+ constructor(
50
+ spec: {
51
+ peg: any[],
52
+ absolute?: boolean,
53
+ prefix?: boolean
54
+ },
55
+ ctx?: AontuContext
56
+ ) {
57
+ super(spec, ctx)
58
+ this.peg = []
59
+
60
+ this.absolute = true === this.absolute ? true : // absolute sticks
61
+ true === spec.absolute ? true : false
62
+
63
+ this.prefix = true === spec.prefix
64
+
65
+ for (let pI = 0; pI < spec.peg.length; pI++) {
66
+ this.append(spec.peg[pI])
67
+ }
68
+
69
+ //console.log('RefVal', this.id, this.peg)
70
+ }
71
+
72
+
73
+ append(part: any) {
74
+ let partval
75
+
76
+ // console.log('APPEND', part)
77
+
78
+ if ('string' === typeof part) {
79
+ partval = part
80
+ this.peg.push(partval)
81
+ }
82
+
83
+ else if (part instanceof StringVal) {
84
+ partval = part.peg
85
+ this.peg.push(partval)
86
+ }
87
+
88
+ else if (part instanceof IntegerVal) {
89
+ // partval = '' + part.peg
90
+ partval = part.src
91
+ this.peg.push(partval)
92
+ }
93
+
94
+ // TODO: this is a bit of a hack, review
95
+ // Seems like a fundamental ambiguity?
96
+ // Resolved by path function
97
+ else if (part instanceof NumberVal) {
98
+ // let partvals: string[] = part.peg.toFixed(11).replace(/(\.0)?0+$/, '$1').split('.')
99
+ let partvals: string[] = part.src.split('.')
100
+ this.peg.push(...partvals)
101
+ }
102
+
103
+ else if (part instanceof VarVal) {
104
+ partval = part
105
+ this.peg.push(partval)
106
+ }
107
+
108
+ else if (part instanceof RefVal) {
109
+ if (part.absolute) {
110
+ this.absolute = true
111
+ }
112
+
113
+ if (this.prefix) {
114
+ if (part.prefix) {
115
+ this.peg.push('.')
116
+ }
117
+ }
118
+ else {
119
+ if (part.prefix) {
120
+ if (0 === this.peg.length) {
121
+ this.prefix = true
122
+ }
123
+
124
+ else if (0 < this.peg.length) {
125
+ this.peg.push('.')
126
+ }
127
+ }
128
+ }
129
+
130
+ this.peg.push(...part.peg)
131
+ }
132
+ }
133
+
134
+
135
+ unify(peer: Val, ctx: AontuContext): Val {
136
+ peer = peer ?? top()
137
+
138
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Ref', this, peer)
139
+ let out: Val = this
140
+ let why = 'id'
141
+
142
+ if (this.id !== peer.id) {
143
+
144
+ // TODO: not resolved when all Vals in path are done is an error
145
+ // as path cannot be found
146
+ // let resolved: Val | undefined = null == ctx ? this : ctx.find(this)
147
+ let found: Val | undefined = null == ctx ? this : this.find(ctx)
148
+
149
+ const resolved = found ?? this
150
+
151
+ if (null == resolved && this.canon === peer.canon) {
152
+ out = this
153
+ }
154
+ else if (resolved instanceof RefVal) {
155
+ if (peer.isTop) {
156
+ out = this
157
+ why = 'pt'
158
+ }
159
+ else if (peer.isNil) {
160
+ out = makeNilErr(ctx, 'ref[' + this.peg + ']', this, peer)
161
+ why = 'pn'
162
+ }
163
+
164
+ // same path
165
+ else if (this.canon === peer.canon) {
166
+ out = this
167
+ why = 'pp'
168
+ }
169
+
170
+ else {
171
+ // Ensure RefVal done is incremented
172
+ this.dc = DONE === this.dc ? DONE : this.dc + 1
173
+ out = new ConjunctVal({ peg: [this, peer] }, ctx)
174
+ why = 'cj'
175
+ }
176
+ }
177
+ else {
178
+ out = unite(ctx.clone({ explain: ec(te, 'RES') }), resolved, peer, 'ref')
179
+ why = 'u'
180
+ }
181
+
182
+ out.dc = DONE === out.dc ? DONE : this.dc + 1
183
+ }
184
+
185
+ // console.log('REFVAL-UNIFY-OUT', ctx.cc, this.id, this.canon, this.done, 'P=', peer.id, peer.canon, peer.done, '->', out.id, out.canon, out.done)
186
+
187
+ explainClose(te, out)
188
+ return out
189
+ }
190
+
191
+
192
+ find(ctx: AontuContext) {
193
+ let out: Val | undefined = undefined
194
+
195
+ const selfpath = this.path.join('.')
196
+ const pegpath = this.peg.join('.')
197
+ const isprefixpath = selfpath.startsWith(pegpath)
198
+
199
+ let refpath: string[] = []
200
+ let pI = 0
201
+ // let descent = ''
202
+
203
+ if (isprefixpath) {
204
+ out = makeNilErr(ctx, 'path_cycle', this)
205
+ }
206
+ else {
207
+
208
+ let parts: string[] = []
209
+
210
+ let modes: string[] = []
211
+
212
+ for (let pI = 0; pI < this.peg.length; pI++) {
213
+ let part = this.peg[pI]
214
+ if (part instanceof VarVal) {
215
+ let strval = (part as VarVal).peg
216
+ let name = strval ? '' + strval.peg : ''
217
+
218
+ if ('KEY' === name) {
219
+ if (pI === this.peg.length - 1) {
220
+ modes.push(name)
221
+ }
222
+ else {
223
+ // TODO: return a Nil explaining error
224
+ return
225
+ }
226
+ }
227
+
228
+ if ('SELF' === name) {
229
+ if (pI === 0) {
230
+ modes.push(name)
231
+ }
232
+ else {
233
+ // TODO: return a Nil explaining error
234
+ return
235
+ }
236
+ }
237
+ else if ('PARENT' === name) {
238
+ if (pI === 0) {
239
+ modes.push(name)
240
+ }
241
+ else {
242
+ // TODO: return a Nil explaining error
243
+ return
244
+ }
245
+ }
246
+ else if (0 === modes.length) {
247
+ part = (part as VarVal).unify(top(), ctx)
248
+ if (part.isNil) {
249
+ // TODO: var not found, so can't find path
250
+ return
251
+ }
252
+ else {
253
+ part = '' + part.peg
254
+ }
255
+ }
256
+ }
257
+ else {
258
+ parts.push(part)
259
+ }
260
+ }
261
+
262
+ if (this.absolute) {
263
+ refpath = parts
264
+ }
265
+ else {
266
+ // TODO: deprecate $KEY, etc
267
+ refpath = this.path.slice(
268
+ 0,
269
+ (
270
+ modes.includes('SELF') ? 0 :
271
+ modes.includes('PARENT') ? -1 :
272
+ -1 // siblings
273
+ )
274
+ ).concat(parts)
275
+ }
276
+
277
+ let sep = '.'
278
+ refpath = refpath
279
+ .reduce(((a: string[], p: string) =>
280
+ (p === sep ? a.length = a.length - 1 : a.push(p), a)), [])
281
+
282
+ if (modes.includes('KEY')) {
283
+ let key = this.path[this.path.length - 2]
284
+ let sv = new StringVal({ peg: null == key ? '' : key }, ctx)
285
+
286
+ // TODO: other props?
287
+ sv.dc = DONE
288
+ sv.path = this.path
289
+
290
+ return sv
291
+ }
292
+
293
+ let node = ctx.root as Val
294
+
295
+ let nopath = false
296
+
297
+ if (null != node) {
298
+ for (; pI < refpath.length; pI++) {
299
+ let part = refpath[pI]
300
+ // console.log('PART', pI, part, node)
301
+
302
+ // descent += (' | ' + pI + '=' + node.canon) // Util.inspect(node))
303
+
304
+ if (node.isMap) {
305
+ node = node.peg[part]
306
+ }
307
+ else if (node.isList) {
308
+ node = node.peg[part]
309
+ }
310
+ else if (node.done) {
311
+ nopath = true
312
+ break;
313
+ }
314
+ else {
315
+ break;
316
+ }
317
+
318
+ if (null == node) {
319
+ nopath = true
320
+ break
321
+ }
322
+
323
+ }
324
+ }
325
+
326
+ // console.log('REFPATH', ctx.cc, pI, refpath, nopath, ctx.root, node)
327
+
328
+
329
+ if (nopath) {
330
+ out = makeNilErr(ctx, 'no_path', this)
331
+ }
332
+ else if (pI === refpath.length) {
333
+ out = node
334
+
335
+ // Types and hidden values are cloned and made concrete
336
+ if (null != out) { // && (out.mark.type || out.mark.hide)) {
337
+
338
+ // console.log('FOUND-A', out)
339
+
340
+ if (this.mark.type || this.mark.hide) {
341
+ out.mark.type = this.mark.type
342
+ out.mark.hide = this.mark.hide
343
+
344
+ // walk(out, (_key: string | number | undefined, val: Val) => {
345
+ // val.mark.type = this.mark.type
346
+ // val.mark.hide = this.mark.hide
347
+ // return val
348
+ // })
349
+ }
350
+
351
+ if (this.mark._hide_found) {
352
+ out.mark.hide = true
353
+ }
354
+
355
+ // console.log('FOUND-B', out)
356
+
357
+ out = out.clone(ctx)
358
+
359
+ // if (this.mark.type || this.mark.hide) {
360
+ walk(out, (_key: string | number | undefined, val: Val) => {
361
+ val.mark.type = false
362
+ val.mark.hide = false
363
+ return val
364
+ })
365
+ //}
366
+
367
+ // onsole.log('FOUND-C', out)
368
+ }
369
+ }
370
+ }
371
+
372
+ // console.log('REF-FIND', ctx.cc, this.id, selfpath, 'PEG=', pegpath, 'RP', pI, refpath.join('.'), descent, 'O=', out?.id, out?.canon, out?.done)
373
+
374
+ return out
375
+ }
376
+
377
+
378
+ same(peer: Val): boolean {
379
+ return null == peer ? false : this.peg === peer.peg
380
+ }
381
+
382
+
383
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
384
+ let out = (super.clone(ctx, {
385
+ peg: this.peg,
386
+ absolute: this.absolute,
387
+ ...(spec || {})
388
+ }) as RefVal)
389
+ return out
390
+ }
391
+
392
+
393
+ get canon() {
394
+ let str =
395
+ (this.absolute ? '$' : '') +
396
+ (0 < this.peg.length ? '.' : '') +
397
+ // this.peg.join(this.sep)
398
+ this.peg.map((p: any) => '.' === p ? '' :
399
+ (p.isVal ? p.canon : '' + p))
400
+ .join('.')
401
+ return str
402
+ }
403
+
404
+
405
+ gen(ctx: AontuContext) {
406
+ // Unresolved ref cannot be generated, so always an error.
407
+ let nil = makeNilErr(
408
+ ctx,
409
+ 'ref',
410
+ this, // (formatPath(this.peg, this.absolute) as any),
411
+ undefined,
412
+ )
413
+
414
+ // TODO: refactor to use Site pointer
415
+ nil.path = this.path
416
+ nil.site.url = this.site.url
417
+ nil.site.row = this.site.row
418
+ nil.site.col = this.site.col
419
+
420
+ return undefined
421
+ }
422
+
423
+
424
+ inspection() {
425
+ return [
426
+ this.absolute ? 'absolute' : '',
427
+ this.prefix ? 'prefix' : '',
428
+ ].filter(p => '' != p).join(',')
429
+ }
430
+
431
+ }
432
+
433
+
434
+ export {
435
+ RefVal,
436
+ }
@@ -0,0 +1,125 @@
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 {
13
+ AontuContext,
14
+ } from '../ctx'
15
+
16
+ import {
17
+ explainOpen,
18
+ explainClose,
19
+ } from '../utility'
20
+
21
+ import { makeNilErr, AontuError } from '../err'
22
+
23
+ // import { BaseVal } from './BaseVal'
24
+ import { FeatureVal } from './FeatureVal'
25
+
26
+
27
+ // A ScalarKind for integers. Number includes floats.
28
+ class Integer { }
29
+
30
+ // A ScalarKind for null.
31
+ class Null { }
32
+
33
+
34
+ type ScalarConstructor =
35
+ StringConstructor |
36
+ NumberConstructor |
37
+ BooleanConstructor |
38
+ (typeof Integer.constructor)
39
+
40
+
41
+ // class ScalarKindVal extends BaseVal {
42
+ class ScalarKindVal extends FeatureVal {
43
+ isScalarKind = true
44
+
45
+ constructor(
46
+ spec: ValSpec,
47
+ ctx?: AontuContext
48
+ ) {
49
+ super(spec, ctx)
50
+
51
+ if (null == this.peg) {
52
+ throw new AontuError('ScalarKindVal spec.peg undefined')
53
+ }
54
+
55
+ this.dc = DONE
56
+ }
57
+
58
+
59
+ unify(peer: Val, ctx: AontuContext): Val {
60
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'ScalarKind', this, peer)
61
+
62
+ const peerIsScalarVal = peer.isScalar
63
+ const peerIsScalarKind = (peer as ScalarKindVal).isScalarKind
64
+
65
+ let out: Val = this
66
+
67
+ if (peerIsScalarVal) {
68
+ let peerKind = (peer as any).kind
69
+
70
+ if (peerKind === this.peg) {
71
+ out = peer
72
+ }
73
+ else if (Number === this.peg && Integer === peerKind) {
74
+ out = peer
75
+ }
76
+ else {
77
+ out = makeNilErr(ctx, 'no-scalar-unify', this, peer)
78
+ }
79
+ }
80
+ else if (peerIsScalarKind) {
81
+ if (Number === this.peg && Integer === peer.peg) {
82
+ out = peer
83
+ }
84
+ else if (Number === peer.peg && Integer === this.peg) {
85
+ out = this
86
+ }
87
+ else if (this.peg === peer.peg) {
88
+ out = this
89
+ }
90
+ else {
91
+ out = makeNilErr(ctx, 'scalar-type', this, peer)
92
+ }
93
+ }
94
+ else {
95
+ out = makeNilErr(ctx, 'not-scalar-type', this, peer)
96
+ }
97
+
98
+ ctx.explain && explainClose(te, out)
99
+
100
+ // console.log('SCALARKINDVAL', this.canon.peer.canon, '->', out.canon)
101
+ return out
102
+ }
103
+
104
+
105
+ get canon() {
106
+ let ctor = (this.peg as any)
107
+ return ctor.name.toLowerCase()
108
+ }
109
+
110
+
111
+ same(peer: any): boolean {
112
+ let out = peer?.isScalarKind ? this.peg === peer?.peg : super.same(peer)
113
+ return out
114
+ }
115
+
116
+
117
+ }
118
+
119
+
120
+ export {
121
+ Integer,
122
+ Null,
123
+ ScalarConstructor,
124
+ ScalarKindVal,
125
+ }
@@ -0,0 +1,107 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+ import type {
4
+ ValSpec,
5
+ } from '../type'
6
+
7
+ import {
8
+ DONE,
9
+ } from '../type'
10
+
11
+ import {
12
+ AontuContext,
13
+ } from '../ctx'
14
+
15
+ import {
16
+ explainOpen,
17
+ explainClose,
18
+ } from '../utility'
19
+
20
+ import { makeNilErr } from '../err'
21
+
22
+ import { Val } from './Val'
23
+ import { ScalarKindVal } from './ScalarKindVal'
24
+
25
+
26
+
27
+ class ScalarVal extends Val {
28
+ kind: any
29
+ isScalar = true
30
+ src: string
31
+
32
+ constructor(
33
+ spec: ValSpec,
34
+ ctx?: AontuContext
35
+ ) {
36
+ super(spec, ctx)
37
+ this.kind = spec.kind
38
+ this.src = spec.src ?? ''
39
+ this.dc = DONE
40
+ }
41
+
42
+
43
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
44
+ let out = super.clone(ctx, {
45
+ peg: this.peg,
46
+ kind: this.kind,
47
+ ...(spec || {})
48
+ })
49
+
50
+ return out
51
+ }
52
+
53
+
54
+ unify(peer: Val, ctx: AontuContext): Val {
55
+ const te = ctx.explain && explainOpen(ctx, ctx.explain, 'Scalar', this, peer)
56
+
57
+ let out: Val
58
+
59
+ // Exactly equal scalars are handled in unify.unite
60
+ if (peer.isScalarKind) {
61
+ out = peer.unify(this, ctx)
62
+ }
63
+ else if (peer.isTop) {
64
+ out = this
65
+ }
66
+ else {
67
+ out = makeNilErr(ctx, 'scalar_' +
68
+ ((peer as any).kind === this.kind ? 'value' : 'kind'), this, peer)
69
+ }
70
+
71
+ // console.log('SCALAR', this.canon, peer.canon, '->', out.canon)
72
+
73
+ explainClose(te, out)
74
+
75
+ return out
76
+ }
77
+
78
+
79
+ get canon() {
80
+ return null === this.peg ? 'null' :
81
+ undefined === this.peg ? 'undefined' :
82
+ (this.peg as any).toString()
83
+ }
84
+
85
+
86
+ same(peer: any): boolean {
87
+ return peer?.isScalar ? peer.peg === this.peg : super.same(peer)
88
+ }
89
+
90
+
91
+ gen(_ctx?: AontuContext) {
92
+ return this.peg
93
+ }
94
+
95
+
96
+ superior() {
97
+ return this.place(new ScalarKindVal({
98
+ peg: this.kind
99
+ }))
100
+ }
101
+
102
+ }
103
+
104
+
105
+ export {
106
+ ScalarVal,
107
+ }
@@ -0,0 +1,37 @@
1
+ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */
2
+
3
+ import type {
4
+ Val,
5
+ ValSpec,
6
+ } from '../type'
7
+
8
+ import {
9
+ AontuContext,
10
+ } from '../ctx'
11
+
12
+ import { ScalarVal } from './ScalarVal'
13
+
14
+
15
+ class StringVal extends ScalarVal {
16
+ isString = true
17
+
18
+ constructor(
19
+ spec: ValSpec,
20
+ ctx?: AontuContext
21
+ ) {
22
+ super({ peg: spec.peg, kind: String }, ctx)
23
+ }
24
+
25
+ unify(peer: Val, ctx: AontuContext): Val {
26
+ return super.unify(peer, ctx)
27
+ }
28
+
29
+ get canon() {
30
+ return JSON.stringify(this.peg)
31
+ }
32
+
33
+ }
34
+
35
+ export {
36
+ StringVal,
37
+ }