aontu 0.3.0 → 0.6.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 (65) hide show
  1. package/aontu.ts +3 -2
  2. package/dist/aontu.d.ts +2 -2
  3. package/dist/aontu.js +3 -4
  4. package/dist/aontu.js.map +1 -1
  5. package/dist/aontu.min.js +1 -63
  6. package/dist/lib/lang.d.ts +1 -3
  7. package/dist/lib/lang.js +47 -30
  8. package/dist/lib/lang.js.map +1 -1
  9. package/dist/lib/op/disjunct.js +10 -10
  10. package/dist/lib/op/disjunct.js.map +1 -1
  11. package/dist/lib/op/op.d.ts +1 -1
  12. package/dist/lib/op/op.js.map +1 -1
  13. package/dist/lib/op/unite.js +19 -14
  14. package/dist/lib/op/unite.js.map +1 -1
  15. package/dist/lib/type.d.ts +33 -22
  16. package/dist/lib/type.js +22 -76
  17. package/dist/lib/type.js.map +1 -1
  18. package/dist/lib/unify.d.ts +4 -1
  19. package/dist/lib/unify.js +7 -5
  20. package/dist/lib/unify.js.map +1 -1
  21. package/dist/lib/val/ConjunctVal.d.ts +12 -0
  22. package/dist/lib/val/ConjunctVal.js +191 -0
  23. package/dist/lib/val/ConjunctVal.js.map +1 -0
  24. package/dist/lib/val/DisjunctVal.d.ts +12 -0
  25. package/dist/lib/val/DisjunctVal.js +85 -0
  26. package/dist/lib/val/DisjunctVal.js.map +1 -0
  27. package/dist/lib/val/ListVal.d.ts +14 -0
  28. package/dist/lib/val/ListVal.js +91 -0
  29. package/dist/lib/val/ListVal.js.map +1 -0
  30. package/dist/lib/val/MapVal.d.ts +14 -0
  31. package/dist/lib/val/MapVal.js +141 -0
  32. package/dist/lib/val/MapVal.js.map +1 -0
  33. package/dist/lib/val/Nil.d.ts +15 -0
  34. package/dist/lib/val/Nil.js +54 -0
  35. package/dist/lib/val/Nil.js.map +1 -0
  36. package/dist/lib/val/PrefVal.d.ts +12 -0
  37. package/dist/lib/val/PrefVal.js +60 -0
  38. package/dist/lib/val/PrefVal.js.map +1 -0
  39. package/dist/lib/val/RefVal.d.ts +15 -0
  40. package/dist/lib/val/RefVal.js +73 -0
  41. package/dist/lib/val/RefVal.js.map +1 -0
  42. package/dist/lib/val/ValBase.d.ts +22 -0
  43. package/dist/lib/val/ValBase.js +25 -0
  44. package/dist/lib/val/ValBase.js.map +1 -0
  45. package/dist/lib/val.d.ts +5 -90
  46. package/dist/lib/val.js +13 -593
  47. package/dist/lib/val.js.map +1 -1
  48. package/lib/lang.ts +77 -59
  49. package/lib/op/disjunct.ts +14 -8
  50. package/lib/op/op.ts +2 -1
  51. package/lib/op/unite.ts +14 -12
  52. package/lib/type.ts +104 -0
  53. package/lib/unify.ts +10 -8
  54. package/lib/val/ConjunctVal.ts +284 -0
  55. package/lib/val/DisjunctVal.ts +145 -0
  56. package/lib/val/ListVal.ts +154 -0
  57. package/lib/val/MapVal.ts +226 -0
  58. package/lib/val/Nil.ts +94 -0
  59. package/lib/val/PrefVal.ts +113 -0
  60. package/lib/val/RefVal.ts +126 -0
  61. package/lib/val/RefVal.ts~ +319 -0
  62. package/lib/val/ValBase.ts +76 -0
  63. package/lib/val.ts +15 -802
  64. package/package.json +27 -23
  65. package/lib/common.ts +0 -19
@@ -0,0 +1,319 @@
1
+ /* Copyright (c) 2021 Richard Rodger, MIT License */
2
+
3
+ // TODO: infinite recursion protection
4
+
5
+
6
+ // NOTES
7
+ // - Vals are immutable
8
+ // - each Val must handle all parent and child unifications explicitly
9
+ // - performance is not considered yet
10
+
11
+
12
+
13
+ /*
14
+
15
+ TOP -> Scalar/Boolean -> BooleanVal
16
+ -> Scalar/String -> StringVal
17
+ -> Scalar/Number -> NumberVal -> IntegerVal
18
+ -> Scalar/Integer
19
+ -> Scalar/Integer -> IntegerVal
20
+
21
+ */
22
+
23
+
24
+ import type {
25
+ Val,
26
+ ValMap,
27
+ ValList,
28
+ } from './type'
29
+
30
+ import {
31
+ DONE,
32
+ TOP,
33
+ } from './type'
34
+
35
+ import {
36
+ Context,
37
+ } from './unify'
38
+
39
+
40
+ import {
41
+ Site
42
+ } from './lang'
43
+
44
+
45
+ import {
46
+ unite
47
+ } from './op/op'
48
+
49
+ import { Nil } from './val/Nil'
50
+
51
+ import {
52
+ ValBase,
53
+ } from './val/ValBase'
54
+
55
+ import { ConjunctVal } from './val/ConjunctVal'
56
+
57
+
58
+
59
+
60
+
61
+
62
+ // A ScalarType for integers. Number includes floats.
63
+ class Integer { }
64
+
65
+
66
+
67
+ type ScalarConstructor =
68
+ StringConstructor |
69
+ NumberConstructor |
70
+ BooleanConstructor |
71
+ (typeof Integer.constructor)
72
+
73
+
74
+ class ScalarTypeVal extends ValBase {
75
+ constructor(peg: ScalarConstructor, ctx?: Context) {
76
+ super(peg, ctx)
77
+ this.done = DONE
78
+ }
79
+
80
+ unify(peer: Val, ctx: Context): Val {
81
+ if (peer instanceof ScalarVal) {
82
+ if (peer.type === this.peg) {
83
+ return peer
84
+ }
85
+ else if (Number === this.peg && Integer === peer.type) {
86
+ return peer
87
+ }
88
+ return Nil.make(ctx, 'no-scalar-unify', this, peer)
89
+ }
90
+ else {
91
+ if (peer instanceof ScalarTypeVal) {
92
+ if (Number === this.peg && Integer === peer.peg) {
93
+ return peer
94
+ }
95
+ else if (Number === peer.peg && Integer === this.peg) {
96
+ return this
97
+ }
98
+ }
99
+ return Nil.make(ctx, 'scalar-type', this, peer)
100
+ }
101
+ }
102
+
103
+ get canon() {
104
+ let ctor = (this.peg as any)
105
+ return ctor.name.toLowerCase()
106
+ }
107
+
108
+ same(peer: Val): boolean {
109
+ return peer instanceof ScalarTypeVal ? this.peg === peer.peg : super.same(peer)
110
+ }
111
+
112
+ gen(_ctx?: Context) {
113
+ return undefined
114
+ }
115
+
116
+ }
117
+
118
+
119
+ class ScalarVal<T> extends ValBase {
120
+ type: any
121
+ constructor(peg: T, type: ScalarConstructor, ctx?: Context) {
122
+ super(peg, ctx)
123
+ this.type = type
124
+ this.done = DONE
125
+ }
126
+ unify(peer: Val, ctx: Context): Val {
127
+ // Exactly equal scalars are handled in op/unite
128
+ if (peer instanceof ScalarTypeVal) {
129
+ return peer.unify(this, ctx)
130
+ }
131
+ return Nil.make(ctx, 'scalar', this, peer)
132
+ }
133
+ get canon() {
134
+ return (this.peg as any).toString()
135
+ }
136
+ same(peer: Val): boolean {
137
+ return peer instanceof ScalarVal ? peer.peg === this.peg : super.same(peer)
138
+ }
139
+
140
+ gen(_ctx?: Context) {
141
+ return this.peg
142
+ }
143
+ }
144
+
145
+
146
+ class NumberVal extends ScalarVal<number> {
147
+ constructor(peg: number, ctx?: Context) {
148
+ super(peg, Number, ctx)
149
+ }
150
+ unify(peer: Val, ctx: Context): Val {
151
+ if (peer instanceof ScalarVal && peer.type === Integer) {
152
+ return peer
153
+ }
154
+ else {
155
+ return super.unify(peer, ctx)
156
+ }
157
+ }
158
+ }
159
+
160
+
161
+ class IntegerVal extends ScalarVal<number> {
162
+ constructor(peg: number, ctx?: Context) {
163
+ if (!Number.isInteger(peg)) {
164
+ // TODO: use Nil?
165
+ throw new Error('not-integer')
166
+ }
167
+ super(peg, Integer, ctx)
168
+ }
169
+ unify(peer: Val, ctx: Context): Val {
170
+ if (peer instanceof ScalarTypeVal && peer.peg === Number) {
171
+ return this
172
+ }
173
+ else if (peer instanceof ScalarVal &&
174
+ peer.type === Number &&
175
+ this.peg === peer.peg) {
176
+ return this
177
+ }
178
+ else {
179
+ return super.unify(peer, ctx)
180
+ }
181
+ }
182
+ }
183
+
184
+
185
+ class StringVal extends ScalarVal<string> {
186
+ constructor(peg: string, ctx?: Context) {
187
+ super(peg, String, ctx)
188
+ }
189
+ unify(peer: Val, ctx: Context): Val {
190
+ return super.unify(peer, ctx)
191
+ }
192
+ get canon() {
193
+ return JSON.stringify(this.peg)
194
+ }
195
+
196
+ }
197
+
198
+
199
+ class BooleanVal extends ScalarVal<boolean> {
200
+ constructor(peg: boolean, ctx?: Context) {
201
+ super(peg, Boolean, ctx)
202
+ }
203
+ unify(peer: Val, ctx: Context): Val {
204
+ return super.unify(peer, ctx)
205
+ }
206
+
207
+ static TRUE = new BooleanVal(true, new Context({ vc: 1, root: TOP }))
208
+ static FALSE = new BooleanVal(false, new Context({ vc: 2, root: TOP }))
209
+ }
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+ class RefVal extends ValBase {
226
+ parts: string[]
227
+ absolute: boolean
228
+ sep = '.'
229
+
230
+ constructor(peg: any[], abs?: boolean) {
231
+ super('')
232
+ this.absolute = true === abs
233
+ this.parts = []
234
+
235
+ for (let part of peg) {
236
+ this.append(part)
237
+ }
238
+ }
239
+
240
+
241
+ append(part: any) {
242
+ //console.log('APPEND 0', part)
243
+
244
+ if ('string' === typeof part) {
245
+ this.parts.push(part)
246
+ }
247
+
248
+ else if (part instanceof StringVal) {
249
+ this.parts.push(part.peg)
250
+ }
251
+
252
+ else if (part instanceof RefVal) {
253
+ this.parts.push(...part.parts)
254
+
255
+ if (part.absolute) {
256
+ this.absolute = true
257
+ }
258
+ }
259
+
260
+ this.peg = (this.absolute ? this.sep : '') + this.parts.join(this.sep)
261
+ }
262
+
263
+ unify(peer: Val, ctx: Context): Val {
264
+ let resolved: Val | undefined = null == ctx ? this : ctx.find(this)
265
+
266
+ // TODO: large amount of reruns needed? why?
267
+ resolved = null == resolved && 999 < this.done ?
268
+ Nil.make(ctx, 'no-path', this, peer) : (resolved || this)
269
+ let out: Val
270
+
271
+ if (resolved instanceof RefVal) {
272
+ if (TOP === peer) {
273
+ out = this
274
+ }
275
+ else if (peer instanceof Nil) {
276
+ out = Nil.make(ctx, 'ref[' + this.peg + ']', this, peer)
277
+ }
278
+ else {
279
+ // Ensure RefVal done is incremented
280
+ this.done = DONE === this.done ? DONE : this.done + 1
281
+ out = new ConjunctVal([this, peer], ctx)
282
+ }
283
+ }
284
+ else {
285
+ out = unite(ctx, resolved, peer)
286
+ }
287
+
288
+ out.done = DONE === out.done ? DONE : this.done + 1
289
+
290
+ return out
291
+ }
292
+
293
+
294
+ same(peer: Val): boolean {
295
+ return null == peer ? false : this.peg === peer.peg
296
+ }
297
+
298
+
299
+ get canon() {
300
+ return this.peg
301
+ }
302
+
303
+
304
+ gen(_ctx?: Context) {
305
+ return undefined
306
+ }
307
+ }
308
+
309
+
310
+ export {
311
+ Integer,
312
+ TOP,
313
+ ScalarTypeVal,
314
+ NumberVal,
315
+ StringVal,
316
+ BooleanVal,
317
+ IntegerVal,
318
+ RefVal,
319
+ }
@@ -0,0 +1,76 @@
1
+ /* Copyright (c) 2021-2022 Richard Rodger, MIT License */
2
+
3
+
4
+
5
+ import type {
6
+ Val,
7
+ } from '../type'
8
+
9
+ import {
10
+ DONE,
11
+ TOP,
12
+ } from '../type'
13
+
14
+ import {
15
+ Context,
16
+ } from '../unify'
17
+
18
+
19
+ import {
20
+ Site
21
+ } from '../lang'
22
+
23
+
24
+ import {
25
+ unite
26
+ } from '../op/op'
27
+
28
+
29
+
30
+
31
+
32
+ abstract class ValBase implements Val {
33
+ id: number
34
+ done: number = 0
35
+ path: string[]
36
+ row: number = -1
37
+ col: number = -1
38
+ url: string = ''
39
+
40
+ top?: boolean
41
+
42
+ // Actual native value.
43
+ peg?: any
44
+
45
+ // TODO: used for top level result - not great
46
+ err?: any[]
47
+ deps?: any
48
+
49
+ constructor(peg?: any, ctx?: Context) {
50
+ this.peg = peg
51
+ this.path = (ctx && ctx.path) || []
52
+ this.id = (ctx && ctx.vc++) || (9e9 + Math.floor(Math.random() * (1e9)))
53
+ }
54
+
55
+ same(peer: Val): boolean {
56
+ // return this === peer
57
+ return null == peer ? false : this.id === peer.id
58
+ }
59
+
60
+ get site(): Site {
61
+ return new Site(this)
62
+ }
63
+
64
+
65
+ // TODO: can ctx be optional?
66
+ abstract unify(peer: Val, ctx?: Context): Val
67
+ abstract get canon(): string
68
+ abstract gen(ctx?: Context): any
69
+ }
70
+
71
+
72
+
73
+
74
+ export {
75
+ ValBase,
76
+ }