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.
- package/dist/ctx.d.ts +51 -0
- package/dist/ctx.js +105 -0
- package/dist/ctx.js.map +1 -0
- package/dist/err.d.ts +12 -0
- package/dist/err.js +123 -0
- package/dist/err.js.map +1 -0
- package/dist/hints.d.ts +8 -0
- package/dist/hints.js +84 -0
- package/dist/hints.js.map +1 -0
- package/dist/lang.d.ts +11 -0
- package/dist/lang.js +569 -0
- package/dist/lang.js.map +1 -0
- package/dist/site.d.ts +12 -0
- package/dist/site.js +27 -0
- package/dist/site.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/type.d.ts +32 -0
- package/dist/type.js +16 -0
- package/dist/type.js.map +1 -0
- package/dist/unify.d.ts +14 -0
- package/dist/unify.js +156 -0
- package/dist/unify.js.map +1 -0
- package/dist/utility.d.ts +14 -0
- package/dist/utility.js +122 -0
- package/dist/utility.js.map +1 -0
- package/dist/val/BagVal.d.ts +10 -0
- package/dist/val/BagVal.js +15 -0
- package/dist/val/BagVal.js.map +1 -0
- package/dist/val/BooleanVal.d.ts +9 -0
- package/dist/val/BooleanVal.js +16 -0
- package/dist/val/BooleanVal.js.map +1 -0
- package/dist/val/CloseFuncVal.d.ts +11 -0
- package/dist/val/CloseFuncVal.js +32 -0
- package/dist/val/CloseFuncVal.js.map +1 -0
- package/dist/val/ConjunctVal.d.ts +14 -0
- package/dist/val/ConjunctVal.js +187 -0
- package/dist/val/ConjunctVal.js.map +1 -0
- package/dist/val/CopyFuncVal.d.ts +13 -0
- package/dist/val/CopyFuncVal.js +35 -0
- package/dist/val/CopyFuncVal.js.map +1 -0
- package/dist/val/DisjunctVal.d.ts +19 -0
- package/dist/val/DisjunctVal.js +194 -0
- package/dist/val/DisjunctVal.js.map +1 -0
- package/dist/val/FeatureVal.d.ts +10 -0
- package/dist/val/FeatureVal.js +29 -0
- package/dist/val/FeatureVal.js.map +1 -0
- package/dist/val/FuncBaseVal.d.ts +15 -0
- package/dist/val/FuncBaseVal.js +127 -0
- package/dist/val/FuncBaseVal.js.map +1 -0
- package/dist/val/HideFuncVal.d.ts +12 -0
- package/dist/val/HideFuncVal.js +48 -0
- package/dist/val/HideFuncVal.js.map +1 -0
- package/dist/val/IntegerVal.d.ts +9 -0
- package/dist/val/IntegerVal.js +46 -0
- package/dist/val/IntegerVal.js.map +1 -0
- package/dist/val/JunctionVal.d.ts +12 -0
- package/dist/val/JunctionVal.js +31 -0
- package/dist/val/JunctionVal.js.map +1 -0
- package/dist/val/KeyFuncVal.d.ts +12 -0
- package/dist/val/KeyFuncVal.js +34 -0
- package/dist/val/KeyFuncVal.js.map +1 -0
- package/dist/val/ListVal.d.ts +17 -0
- package/dist/val/ListVal.js +186 -0
- package/dist/val/ListVal.js.map +1 -0
- package/dist/val/LowerFuncVal.d.ts +12 -0
- package/dist/val/LowerFuncVal.js +40 -0
- package/dist/val/LowerFuncVal.js.map +1 -0
- package/dist/val/MapVal.d.ts +16 -0
- package/dist/val/MapVal.js +211 -0
- package/dist/val/MapVal.js.map +1 -0
- package/dist/val/MoveFuncVal.d.ts +11 -0
- package/dist/val/MoveFuncVal.js +41 -0
- package/dist/val/MoveFuncVal.js.map +1 -0
- package/dist/val/NilVal.d.ts +25 -0
- package/dist/val/NilVal.js +92 -0
- package/dist/val/NilVal.js.map +1 -0
- package/dist/val/NullVal.d.ts +9 -0
- package/dist/val/NullVal.js +18 -0
- package/dist/val/NullVal.js.map +1 -0
- package/dist/val/NumberVal.d.ts +9 -0
- package/dist/val/NumberVal.js +44 -0
- package/dist/val/NumberVal.js.map +1 -0
- package/dist/val/OpBaseVal.d.ts +18 -0
- package/dist/val/OpBaseVal.js +152 -0
- package/dist/val/OpBaseVal.js.map +1 -0
- package/dist/val/OpenFuncVal.d.ts +11 -0
- package/dist/val/OpenFuncVal.js +30 -0
- package/dist/val/OpenFuncVal.js.map +1 -0
- package/dist/val/PathFuncVal.d.ts +13 -0
- package/dist/val/PathFuncVal.js +42 -0
- package/dist/val/PathFuncVal.js.map +1 -0
- package/dist/val/PlusOpVal.d.ts +16 -0
- package/dist/val/PlusOpVal.js +59 -0
- package/dist/val/PlusOpVal.js.map +1 -0
- package/dist/val/PrefFuncVal.d.ts +11 -0
- package/dist/val/PrefFuncVal.js +39 -0
- package/dist/val/PrefFuncVal.js.map +1 -0
- package/dist/val/PrefVal.d.ts +15 -0
- package/dist/val/PrefVal.js +104 -0
- package/dist/val/PrefVal.js.map +1 -0
- package/dist/val/RefVal.d.ts +22 -0
- package/dist/val/RefVal.js +276 -0
- package/dist/val/RefVal.js.map +1 -0
- package/dist/val/ScalarKindVal.d.ts +16 -0
- package/dist/val/ScalarKindVal.js +76 -0
- package/dist/val/ScalarKindVal.js.map +1 -0
- package/dist/val/ScalarVal.d.ts +16 -0
- package/dist/val/ScalarVal.js +61 -0
- package/dist/val/ScalarVal.js.map +1 -0
- package/dist/val/StringVal.d.ts +10 -0
- package/dist/val/StringVal.js +19 -0
- package/dist/val/StringVal.js.map +1 -0
- package/dist/val/SuperFuncVal.d.ts +11 -0
- package/dist/val/SuperFuncVal.js +22 -0
- package/dist/val/SuperFuncVal.js.map +1 -0
- package/dist/val/TopVal.d.ts +16 -0
- package/dist/val/TopVal.js +38 -0
- package/dist/val/TopVal.js.map +1 -0
- package/dist/val/TypeFuncVal.d.ts +12 -0
- package/dist/val/TypeFuncVal.js +35 -0
- package/dist/val/TypeFuncVal.js.map +1 -0
- package/dist/val/UpperFuncVal.d.ts +12 -0
- package/dist/val/UpperFuncVal.js +40 -0
- package/dist/val/UpperFuncVal.js.map +1 -0
- package/dist/val/Val.d.ts +89 -0
- package/dist/val/Val.js +186 -0
- package/dist/val/Val.js.map +1 -0
- package/dist/val/VarVal.d.ts +13 -0
- package/dist/val/VarVal.js +109 -0
- package/dist/val/VarVal.js.map +1 -0
- package/dist/val/top.d.ts +2 -0
- package/dist/val/top.js +10 -0
- package/dist/val/top.js.map +1 -0
- package/dist/val/valutil.d.ts +2 -0
- package/dist/val/valutil.js +30 -0
- package/dist/val/valutil.js.map +1 -0
- package/package.json +3 -5
- package/src/aontu.ts +218 -0
- package/src/ctx.ts +201 -0
- package/src/err.ts +172 -0
- package/src/hints.ts +119 -0
- package/src/lang.ts +782 -0
- package/src/site.ts +34 -0
- package/src/tsconfig.json +16 -0
- package/src/type.ts +72 -0
- package/src/unify.ts +225 -0
- package/src/utility.ts +179 -0
- package/src/val/BagVal.ts +33 -0
- package/src/val/BooleanVal.ts +32 -0
- package/src/val/CloseFuncVal.ts +61 -0
- package/src/val/ConjunctVal.ts +285 -0
- package/src/val/CopyFuncVal.ts +69 -0
- package/src/val/DisjunctVal.ts +288 -0
- package/src/val/FeatureVal.ts +56 -0
- package/src/val/FuncBaseVal.ts +198 -0
- package/src/val/HideFuncVal.ts +85 -0
- package/src/val/IntegerVal.ts +75 -0
- package/src/val/JunctionVal.ts +54 -0
- package/src/val/KeyFuncVal.ts +67 -0
- package/src/val/ListVal.ts +286 -0
- package/src/val/LowerFuncVal.ts +73 -0
- package/src/val/MapVal.ts +316 -0
- package/src/val/MoveFuncVal.ts +79 -0
- package/src/val/NilVal.ts +156 -0
- package/src/val/NullVal.ts +38 -0
- package/src/val/NumberVal.ts +75 -0
- package/src/val/OpBaseVal.ts +231 -0
- package/src/val/OpenFuncVal.ts +60 -0
- package/src/val/PathFuncVal.ts +75 -0
- package/src/val/PlusOpVal.ts +92 -0
- package/src/val/PrefFuncVal.ts +73 -0
- package/src/val/PrefVal.ts +159 -0
- package/src/val/RefVal.ts +384 -0
- package/src/val/ScalarKindVal.ts +125 -0
- package/src/val/ScalarVal.ts +105 -0
- package/src/val/StringVal.ts +37 -0
- package/src/val/SuperFuncVal.ts +46 -0
- package/src/val/TopVal.ts +69 -0
- package/src/val/TypeFuncVal.ts +69 -0
- package/src/val/UpperFuncVal.ts +72 -0
- package/src/val/Val.ts +294 -0
- package/src/val/VarVal.ts +171 -0
- package/src/val/top.ts +12 -0
- package/src/val/valutil.ts +31 -0
package/src/site.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* Copyright (c) 2021-2025 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
Val,
|
|
6
|
+
} from './type'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Site {
|
|
10
|
+
row: number = -1
|
|
11
|
+
col: number = -1
|
|
12
|
+
url: string = ''
|
|
13
|
+
|
|
14
|
+
constructor(val?: Val | { row?: number, col?: number, url?: string }) {
|
|
15
|
+
if (val) {
|
|
16
|
+
if ('site' in val) {
|
|
17
|
+
// Val with site property
|
|
18
|
+
this.row = val.site?.row ?? -1
|
|
19
|
+
this.col = val.site?.col ?? -1
|
|
20
|
+
this.url = val.site?.url ?? ''
|
|
21
|
+
} else {
|
|
22
|
+
// Plain object with row, col, url
|
|
23
|
+
this.row = val.row ?? -1
|
|
24
|
+
this.col = val.col ?? -1
|
|
25
|
+
this.url = val.url ?? ''
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
Site,
|
|
34
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"noEmitOnError": true,
|
|
6
|
+
"outDir":"../dist",
|
|
7
|
+
"rootDir":".",
|
|
8
|
+
"resolveJsonModule": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"target": "es2021",
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"declarationDir": "../dist"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
package/src/type.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/* Copyright (c) 2022-2025 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
import * as Fs from 'node:fs'
|
|
4
|
+
|
|
5
|
+
// TODO: refactor these out
|
|
6
|
+
|
|
7
|
+
import { Resolver } from '@jsonic/multisource'
|
|
8
|
+
|
|
9
|
+
import { AontuContext } from './ctx'
|
|
10
|
+
import { Val, DONE, SPREAD } from './val/Val'
|
|
11
|
+
import type { ValMark, ValSpec } from './val/Val'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
type FST = typeof Fs
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
type AontuOptions = {
|
|
19
|
+
src?: string // Source text.
|
|
20
|
+
print?: number // Print debug verbosity
|
|
21
|
+
resolver?: Resolver // Source resolver
|
|
22
|
+
base?: string // Base path for resolver
|
|
23
|
+
path?: string // Path of entry file
|
|
24
|
+
debug?: boolean
|
|
25
|
+
trace?: boolean
|
|
26
|
+
fs?: FST
|
|
27
|
+
deps?: any
|
|
28
|
+
log?: any
|
|
29
|
+
idcount?: number
|
|
30
|
+
collect?: boolean // Collect errors into an errs property, rather than throw them.
|
|
31
|
+
err?: any[]
|
|
32
|
+
explain?: any[]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const DEFAULT_OPTS: () => AontuOptions = () => {
|
|
37
|
+
return {
|
|
38
|
+
print: -1,
|
|
39
|
+
debug: false,
|
|
40
|
+
trace: false,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
type ValMap = { [key: string]: Val }
|
|
46
|
+
type ValList = Val[]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
type ErrContext = {
|
|
51
|
+
src?: string,
|
|
52
|
+
fs?: FST
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type {
|
|
56
|
+
Val,
|
|
57
|
+
ValMark,
|
|
58
|
+
ValSpec,
|
|
59
|
+
ValMap,
|
|
60
|
+
ValList,
|
|
61
|
+
AontuOptions,
|
|
62
|
+
ErrContext,
|
|
63
|
+
FST,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export {
|
|
67
|
+
DONE,
|
|
68
|
+
SPREAD,
|
|
69
|
+
DEFAULT_OPTS,
|
|
70
|
+
Resolver,
|
|
71
|
+
}
|
|
72
|
+
|
package/src/unify.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/* Copyright (c) 2021-2023 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import type { Val } from './type'
|
|
5
|
+
|
|
6
|
+
import { AontuContext } from './ctx'
|
|
7
|
+
|
|
8
|
+
import { DONE } from './type'
|
|
9
|
+
|
|
10
|
+
import { makeNilErr } from './err'
|
|
11
|
+
|
|
12
|
+
import { NilVal } from './val/NilVal'
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
Lang
|
|
16
|
+
} from './lang'
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
explainOpen, ec, explainClose,
|
|
21
|
+
} from './utility'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
top
|
|
26
|
+
} from './val/top'
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// TODO: relation to unify loops?
|
|
30
|
+
const MAXCYCLE = 9
|
|
31
|
+
|
|
32
|
+
let uc = 0
|
|
33
|
+
|
|
34
|
+
// Vals should only have to unify downwards (in .unify) over Vals they understand.
|
|
35
|
+
// and for complex Vals, TOP, which means self unify if not yet done
|
|
36
|
+
const unite = (ctx: AontuContext, a: any, b: any, whence: string) => {
|
|
37
|
+
const te = ctx.explain && explainOpen(ctx, ctx.explain, 'unite', a, b)
|
|
38
|
+
|
|
39
|
+
let out = a
|
|
40
|
+
let why = 'u'
|
|
41
|
+
|
|
42
|
+
// const saw = a?.isTop && b?.isTop ? '' :
|
|
43
|
+
// (a ? a.id + (a.done ? '' : '*') : '') + '~' + (b ? b.id + (b.done ? '' : '*') : '')
|
|
44
|
+
|
|
45
|
+
const saw =
|
|
46
|
+
(a ? a.id + (a.done ? '' : '*') : '') + '~' + (b ? b.id + (b.done ? '' : '*') : '') +
|
|
47
|
+
'@' + ctx.pathstr
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
if (10 < ctx.seen[saw]) {
|
|
51
|
+
console.log('UNITE-SAW', ctx.cc, saw, ctx.seen[saw], 1 < ctx.seen[saw] ? (a?.canon + ' ~ ' + b?.canon) : '')
|
|
52
|
+
console.trace()
|
|
53
|
+
}
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
if (MAXCYCLE < ctx.seen[saw]) {
|
|
57
|
+
out = makeNilErr(ctx, 'cycle', a, b)
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
ctx.seen[saw] = 1 + (ctx.seen[saw] ?? 0)
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
|
|
64
|
+
let unified = false
|
|
65
|
+
|
|
66
|
+
if (b && (!a || a.isTop)) {
|
|
67
|
+
out = b
|
|
68
|
+
why = 'b'
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
else if (a && (!b || b.isTop)) {
|
|
72
|
+
out = a
|
|
73
|
+
why = 'a'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
else if (a && b && !b.isTop) {
|
|
77
|
+
if (a.isNil) {
|
|
78
|
+
out = update(a, b)
|
|
79
|
+
why = 'an'
|
|
80
|
+
}
|
|
81
|
+
else if (b.isNil) {
|
|
82
|
+
out = update(b, a)
|
|
83
|
+
why = 'bn'
|
|
84
|
+
}
|
|
85
|
+
else if (a.isConjunct) {
|
|
86
|
+
out = a.unify(b, ctx.clone({ explain: ec(te, 'CJ') }))
|
|
87
|
+
unified = true
|
|
88
|
+
why = 'acj'
|
|
89
|
+
}
|
|
90
|
+
else if (
|
|
91
|
+
b.isConjunct
|
|
92
|
+
|| b.isDisjunct
|
|
93
|
+
|| b.isRef
|
|
94
|
+
|| b.isPref
|
|
95
|
+
|| b.isFunc
|
|
96
|
+
) {
|
|
97
|
+
|
|
98
|
+
out = b.unify(a, ctx.clone({ explain: ec(te, 'BW') }))
|
|
99
|
+
unified = true
|
|
100
|
+
why = 'bv'
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Exactly equal scalars.
|
|
104
|
+
else if (a.constructor === b.constructor && a.peg === b.peg) {
|
|
105
|
+
out = update(a, b)
|
|
106
|
+
why = 'up'
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
else {
|
|
110
|
+
out = a.unify(b, ctx.clone({ explain: ec(te, 'GN') }))
|
|
111
|
+
unified = true
|
|
112
|
+
why = 'ab'
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!out || !out.unify) {
|
|
117
|
+
out = makeNilErr(ctx, 'unite', a, b, whence + '/nil')
|
|
118
|
+
why += 'N'
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (DONE !== out.dc && !unified) {
|
|
122
|
+
let nout = out.unify(top(), ctx.clone({ explain: ec(te, 'ND') }))
|
|
123
|
+
out = nout
|
|
124
|
+
why += 'T'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
uc++
|
|
128
|
+
}
|
|
129
|
+
catch (err: any) {
|
|
130
|
+
// TODO: handle unexpected
|
|
131
|
+
out = makeNilErr(ctx, 'internal', a, b)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
ctx.explain && explainClose(te, out)
|
|
136
|
+
|
|
137
|
+
return out
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
function update(x: Val, _y: Val) {
|
|
142
|
+
// TODO: update x with y.site
|
|
143
|
+
return x
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class Unify {
|
|
148
|
+
root: Val
|
|
149
|
+
res: Val
|
|
150
|
+
// err: Omit<NilVal[], "push">
|
|
151
|
+
err: any[]
|
|
152
|
+
explain: any[] | null
|
|
153
|
+
cc: number
|
|
154
|
+
lang: Lang
|
|
155
|
+
|
|
156
|
+
constructor(root: Val | string, lang?: Lang, ctx?: AontuContext | any, src?: any) {
|
|
157
|
+
this.lang = lang || new Lang()
|
|
158
|
+
if ('string' === typeof root) {
|
|
159
|
+
root = this.lang.parse(root)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if ('string' !== typeof src) {
|
|
163
|
+
src = ''
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this.cc = 0
|
|
167
|
+
this.root = root
|
|
168
|
+
this.res = root
|
|
169
|
+
this.err = ctx?.err ?? root.err ?? []
|
|
170
|
+
this.explain = ctx?.explain ?? root.explain ?? null
|
|
171
|
+
|
|
172
|
+
let res = root
|
|
173
|
+
let uctx: AontuContext
|
|
174
|
+
|
|
175
|
+
// Only unify if no syntax errors
|
|
176
|
+
if (!(root as NilVal).isNil) {
|
|
177
|
+
if (ctx instanceof AontuContext) {
|
|
178
|
+
uctx = ctx
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
uctx = new AontuContext({
|
|
182
|
+
...(ctx || {}),
|
|
183
|
+
root: res,
|
|
184
|
+
err: this.err,
|
|
185
|
+
explain: this.explain,
|
|
186
|
+
src,
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// TODO: messy
|
|
191
|
+
// uctx.seterr(this.err)
|
|
192
|
+
uctx.err = this.err
|
|
193
|
+
uctx.explain = this.explain
|
|
194
|
+
|
|
195
|
+
const explain = null == ctx?.explain ? undefined : ctx?.explain
|
|
196
|
+
const te = explain && explainOpen(uctx, explain, 'root', res)
|
|
197
|
+
|
|
198
|
+
// NOTE: if true === res.done already, then this loop never needs to run.
|
|
199
|
+
let maxcc = 9 // 99
|
|
200
|
+
for (; this.cc < maxcc && DONE !== res.dc; this.cc++) {
|
|
201
|
+
// console.log('CC', this.cc, res.canon)
|
|
202
|
+
uctx.cc = this.cc
|
|
203
|
+
res = unite(uctx.clone({ explain: ec(te, 'run') }), res, top(), 'unify')
|
|
204
|
+
|
|
205
|
+
if (0 < uctx.err.length) {
|
|
206
|
+
break
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
uctx = uctx.clone({ root: res })
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
uctx.explain && explainClose(te, res)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.res = res
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
export {
|
|
223
|
+
Unify,
|
|
224
|
+
unite,
|
|
225
|
+
}
|
package/src/utility.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/* Copyright (c) 2023-2025 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import type { Val } from './type'
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
// Mark value in source is propagated to target (true ratchets).
|
|
9
|
+
function propagateMarks(source: Val, target: Val): void {
|
|
10
|
+
for (let name in source.mark) {
|
|
11
|
+
(target.mark as any)[name] = (target.mark as any)[name] || (source.mark as any)[name]
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function formatPath(path: Val | string[], absolute?: boolean) {
|
|
17
|
+
let parts: string[]
|
|
18
|
+
if (Array.isArray(path)) {
|
|
19
|
+
parts = path
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
parts = path.path
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let pathstr = (0 < parts.length && false !== absolute ? '$.' : '') + parts.join('.')
|
|
26
|
+
|
|
27
|
+
return pathstr
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
type WalkApply = (
|
|
32
|
+
key: string | number | undefined,
|
|
33
|
+
val: Val,
|
|
34
|
+
parent: Val | undefined,
|
|
35
|
+
path: (string | number)[]
|
|
36
|
+
) => Val
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Walk a Val structure depth first, applying functions before and after descending.
|
|
40
|
+
* Only traverses Val instances - stops at non-Val children.
|
|
41
|
+
*/
|
|
42
|
+
function walk(
|
|
43
|
+
// These arguments are the public interface.
|
|
44
|
+
val: Val,
|
|
45
|
+
|
|
46
|
+
// Before descending into a node.
|
|
47
|
+
before?: WalkApply,
|
|
48
|
+
|
|
49
|
+
// After descending into a node.
|
|
50
|
+
after?: WalkApply,
|
|
51
|
+
|
|
52
|
+
// Maximum recursive depth, default: 32. Use null for infinite depth.
|
|
53
|
+
maxdepth?: number | null,
|
|
54
|
+
|
|
55
|
+
// These arguments are used for recursive state.
|
|
56
|
+
key?: string | number,
|
|
57
|
+
parent?: Val,
|
|
58
|
+
path?: (string | number)[]
|
|
59
|
+
): Val {
|
|
60
|
+
let out = null == before ? val : before(key, val, parent, path || [])
|
|
61
|
+
|
|
62
|
+
maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : 32
|
|
63
|
+
if (null != maxdepth && 0 === maxdepth) {
|
|
64
|
+
return out
|
|
65
|
+
}
|
|
66
|
+
if (null != path && null != maxdepth && 0 < maxdepth && maxdepth <= path.length) {
|
|
67
|
+
return out
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const child: any = out.peg
|
|
71
|
+
|
|
72
|
+
// Container Vals (Map etc) have peg = plain {} or []
|
|
73
|
+
if (null != child && !child.isVal) {
|
|
74
|
+
if ('object' === typeof child) {
|
|
75
|
+
for (let ckey in child) {
|
|
76
|
+
child[ckey] = walk(
|
|
77
|
+
child[ckey], before, after, maxdepth, ckey, out, [...(path || []), ckey])
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (Array.isArray(child)) {
|
|
81
|
+
for (let i = 0; i < child.length; i++) {
|
|
82
|
+
child[i] = walk(
|
|
83
|
+
child[i], before, after, maxdepth, i, out, [...(path || []), '' + i])
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
out = null == after ? out : after(key, out, parent, path || [])
|
|
89
|
+
|
|
90
|
+
return out
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
const T_NOTE = 0
|
|
95
|
+
const T_WHY = 1
|
|
96
|
+
const T_AVAL = 2
|
|
97
|
+
const T_BVAL = 3
|
|
98
|
+
const T_OVAL = 4
|
|
99
|
+
const T_CHILDREN = 5
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
function explainOpen(
|
|
103
|
+
ctx: any,
|
|
104
|
+
t: any[] | undefined | null | false,
|
|
105
|
+
note: string,
|
|
106
|
+
ac?: Val,
|
|
107
|
+
bc?: Val
|
|
108
|
+
): any[] | null {
|
|
109
|
+
if (false === t) return null;
|
|
110
|
+
|
|
111
|
+
t = t ?? [null, 'root', null, null, null, null]
|
|
112
|
+
t[T_WHY] = t[T_WHY] ?? ''
|
|
113
|
+
t[T_NOTE] = (0 <= ctx.cc ? ctx.cc + '~' : '') + note
|
|
114
|
+
if (ac) {
|
|
115
|
+
t[T_AVAL] = ac.id + (ac.done ? '' : '!') + '=' + ac.canon
|
|
116
|
+
}
|
|
117
|
+
if (bc) {
|
|
118
|
+
t[T_BVAL] = bc.id + (bc.done ? '' : '!') + '=' + bc.canon
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return t
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
function ec(t: any[] | undefined | null, why: string) {
|
|
126
|
+
if (null == t) return;
|
|
127
|
+
|
|
128
|
+
const child = [null, why, null, null, null, null]
|
|
129
|
+
t[T_CHILDREN] = t[T_CHILDREN] ?? []
|
|
130
|
+
t[T_CHILDREN].push(child)
|
|
131
|
+
return child
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
function explainClose(t: any[] | undefined | null, out?: Val) {
|
|
136
|
+
if (null == t) return;
|
|
137
|
+
|
|
138
|
+
if (out) {
|
|
139
|
+
t[T_OVAL] = out.id + (out.done ? '' : '!') + '=' + out.canon
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
function formatExplain(t: any[], d?: number) {
|
|
145
|
+
d = null == d ? 0 : d
|
|
146
|
+
const indent = (' '.repeat(d))
|
|
147
|
+
|
|
148
|
+
if (Array.isArray(t)) {
|
|
149
|
+
const b = [
|
|
150
|
+
indent + t.slice(0, t.length - 1).join(' ')
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
const children = t[t.length - 1]
|
|
154
|
+
if (Array.isArray(children)) {
|
|
155
|
+
for (let ce of children) {
|
|
156
|
+
b.push(formatExplain(ce, d + 1))
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return b.join('\n')
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return indent + t
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
export {
|
|
170
|
+
propagateMarks,
|
|
171
|
+
formatPath,
|
|
172
|
+
walk,
|
|
173
|
+
WalkApply,
|
|
174
|
+
explainOpen,
|
|
175
|
+
ec,
|
|
176
|
+
explainClose,
|
|
177
|
+
formatExplain,
|
|
178
|
+
}
|
|
179
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { FeatureVal } from './FeatureVal'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
abstract class BagVal extends FeatureVal {
|
|
17
|
+
isBag = true
|
|
18
|
+
|
|
19
|
+
closed: boolean = false
|
|
20
|
+
optionalKeys: string[] = []
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
spec: ValSpec,
|
|
24
|
+
ctx?: AontuContext
|
|
25
|
+
) {
|
|
26
|
+
super(spec, ctx)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
BagVal,
|
|
33
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* Copyright (c) 2021-2023 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
|
+
class BooleanVal extends ScalarVal {
|
|
15
|
+
isBoolean = true
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
spec: ValSpec,
|
|
19
|
+
ctx?: AontuContext
|
|
20
|
+
) {
|
|
21
|
+
super({ peg: spec.peg, kind: Boolean }, ctx)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
unify(peer: Val, ctx: AontuContext): Val {
|
|
25
|
+
return super.unify(peer, ctx)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
BooleanVal,
|
|
32
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
|
|
16
|
+
import { FuncBaseVal } from './FuncBaseVal'
|
|
17
|
+
import { BagVal } from '../val/BagVal'
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class CloseFuncVal extends FuncBaseVal {
|
|
21
|
+
isCloseFunc = true
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
spec: ValSpec,
|
|
25
|
+
ctx?: AontuContext
|
|
26
|
+
) {
|
|
27
|
+
super(spec, ctx)
|
|
28
|
+
this.validateArgs(spec.peg, 1)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
make(_ctx: AontuContext, spec: ValSpec): Val {
|
|
33
|
+
return new CloseFuncVal(spec)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
funcname() {
|
|
37
|
+
return 'close'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
resolve(ctx: AontuContext, args: Val[]) {
|
|
42
|
+
const argval: any = args[0]
|
|
43
|
+
|
|
44
|
+
if (null == argval) {
|
|
45
|
+
return makeNilErr(ctx, 'no_first_arg', this, undefined, 'close')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (argval.isMap || argval.isList) {
|
|
49
|
+
(argval as BagVal).closed = true
|
|
50
|
+
// console.log('CLOSED', argval.canon)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return argval
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
export {
|
|
60
|
+
CloseFuncVal,
|
|
61
|
+
}
|