trebol 0.1.0 → 0.1.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.
- package/README.md +8 -2
- package/dist/cli.js +55 -20
- package/dist/lib/core/access.d.ts +10 -0
- package/dist/lib/core/access.js +210 -0
- package/dist/lib/core/base.d.ts +4 -0
- package/dist/lib/core/base.js +50 -0
- package/dist/lib/core/bitset.d.ts +9 -0
- package/dist/lib/core/bitset.js +75 -0
- package/dist/lib/core/boot.d.ts +18 -0
- package/dist/lib/core/boot.js +72 -0
- package/dist/lib/core/compare.d.ts +9 -0
- package/dist/lib/core/compare.js +156 -0
- package/dist/lib/core/ctx.d.ts +36 -0
- package/dist/lib/core/ctx.js +251 -0
- package/dist/lib/core/dec.d.ts +28 -0
- package/dist/lib/core/dec.js +158 -0
- package/dist/lib/core/errors.d.ts +15 -0
- package/dist/lib/core/errors.js +44 -0
- package/dist/lib/core/escapes.d.ts +14 -0
- package/dist/lib/core/escapes.js +80 -0
- package/dist/lib/core/eval.d.ts +220 -0
- package/dist/lib/core/eval.js +666 -0
- package/dist/lib/core/func.d.ts +64 -0
- package/dist/lib/core/func.js +129 -0
- package/dist/lib/core/hash.d.ts +5 -0
- package/dist/lib/core/hash.js +155 -0
- package/dist/lib/core/index.d.ts +18 -0
- package/dist/lib/core/index.js +20 -0
- package/dist/lib/core/int64.d.ts +22 -0
- package/dist/lib/core/int64.js +113 -0
- package/dist/lib/core/kinds.d.ts +67 -0
- package/dist/lib/core/kinds.js +134 -0
- package/dist/lib/core/lexer.d.ts +13 -0
- package/dist/lib/core/lexer.js +702 -0
- package/dist/lib/core/mezz.d.ts +1 -0
- package/dist/lib/core/mezz.js +360 -0
- package/dist/lib/core/mold.d.ts +20 -0
- package/dist/lib/core/mold.js +434 -0
- package/dist/lib/core/natives/control.d.ts +1 -0
- package/dist/lib/core/natives/control.js +374 -0
- package/dist/lib/core/natives/csv.d.ts +15 -0
- package/dist/lib/core/natives/csv.js +315 -0
- package/dist/lib/core/natives/data.d.ts +11 -0
- package/dist/lib/core/natives/data.js +367 -0
- package/dist/lib/core/natives/define.d.ts +17 -0
- package/dist/lib/core/natives/define.js +47 -0
- package/dist/lib/core/natives/encode.d.ts +3 -0
- package/dist/lib/core/natives/encode.js +187 -0
- package/dist/lib/core/natives/json.d.ts +5 -0
- package/dist/lib/core/natives/json.js +273 -0
- package/dist/lib/core/natives/math.d.ts +3 -0
- package/dist/lib/core/natives/math.js +500 -0
- package/dist/lib/core/natives/parse.d.ts +1 -0
- package/dist/lib/core/natives/parse.js +600 -0
- package/dist/lib/core/natives/series.d.ts +4 -0
- package/dist/lib/core/natives/series.js +509 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +484 -0
- package/dist/lib/core/natives/xml.d.ts +9 -0
- package/dist/lib/core/natives/xml.js +242 -0
- package/dist/lib/core/series.d.ts +26 -0
- package/dist/lib/core/series.js +73 -0
- package/dist/lib/core/symbols.d.ts +9 -0
- package/dist/lib/core/symbols.js +44 -0
- package/dist/lib/core/values.d.ts +206 -0
- package/dist/lib/core/values.js +223 -0
- package/dist/lib/host/browser.d.ts +4 -0
- package/dist/lib/host/browser.js +4081 -0
- package/dist/lib/host/chart.d.ts +26 -0
- package/dist/lib/host/chart.js +158 -0
- package/dist/lib/host/compress.d.ts +1 -0
- package/dist/lib/host/compress.js +37 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/icons.d.ts +3 -0
- package/dist/lib/host/icons.js +97 -0
- package/dist/lib/host/index.d.ts +38 -0
- package/dist/lib/host/index.js +190 -0
- package/dist/lib/host/locale.d.ts +40 -0
- package/dist/lib/host/locale.js +241 -0
- package/dist/lib/host/markdown.d.ts +28 -0
- package/dist/lib/host/markdown.js +151 -0
- package/dist/lib/host/modules.d.ts +10 -0
- package/dist/lib/host/modules.js +262 -0
- package/dist/lib/host/natives.d.ts +6 -0
- package/dist/lib/host/natives.js +98 -0
- package/dist/lib/host/net.d.ts +12 -0
- package/dist/lib/host/net.js +541 -0
- package/dist/lib/host/page.d.ts +9 -0
- package/dist/lib/host/page.js +55 -0
- package/dist/lib/host/rows.d.ts +15 -0
- package/dist/lib/host/rows.js +80 -0
- package/dist/lib/host/scheduler.d.ts +45 -0
- package/dist/lib/host/scheduler.js +173 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1462 -0
- package/dist/lib/ui/i18n.trb +111 -0
- package/dist/lib/ui/ui.trb +1237 -0
- package/dist/trebol.js +123 -56
- package/package.json +17 -5
- package/src/host/browser.ts +274 -33
- package/src/host/index.ts +2 -2
- package/src/host/modules.ts +8 -1
- package/src/host/theme.ts +63 -4
- package/src/ui/i18n.trb +4 -0
- package/src/ui/ui.trb +48 -15
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
// Series natives: navigation, editing, searching, ordering and set operations.
|
|
2
|
+
//
|
|
3
|
+
// Blocks hold cells, strings code points and binaries bytes; the helpers in
|
|
4
|
+
// series.ts hide the difference. Searching is case-insensitive unless /case,
|
|
5
|
+
// as `=` is.
|
|
6
|
+
import { K, isAnyBlock, kindBit } from "../kinds.js";
|
|
7
|
+
import * as I64 from "../int64.js";
|
|
8
|
+
import { ownMeta, NONE, TRUE, integer, char, logic, strictEqual, } from "../values.js";
|
|
9
|
+
import { bindSeries, copyDeep } from "../ctx.js";
|
|
10
|
+
import { isEqual, compare, foldCase } from "../compare.js";
|
|
11
|
+
import { pick } from "../access.js";
|
|
12
|
+
import { context, time, sameValue } from "../values.js";
|
|
13
|
+
import { dateDiff } from "./math.js";
|
|
14
|
+
import { splice, itemsFor, lengthOf, at, clampIndex, } from "../series.js";
|
|
15
|
+
import { T } from "../eval.js";
|
|
16
|
+
import { native, value, fail, truthy, doBlock } from "./define.js";
|
|
17
|
+
const num = (c) => I64.toNumber(c.v);
|
|
18
|
+
const used = (c) => c === TRUE;
|
|
19
|
+
/** The cell for element `i` of a series. */
|
|
20
|
+
export function cellAt(s, i) {
|
|
21
|
+
const x = s.s.data[i];
|
|
22
|
+
if (x === undefined)
|
|
23
|
+
return NONE;
|
|
24
|
+
if (isAnyBlock(s.t))
|
|
25
|
+
return x;
|
|
26
|
+
return s.t === K.Binary ? integer(x) : char(x);
|
|
27
|
+
}
|
|
28
|
+
/** End index for a /part argument: a count or a position in the same series. */
|
|
29
|
+
function partEnd(s, part) {
|
|
30
|
+
if (part === null || part.t === K.None)
|
|
31
|
+
return s.s.data.length;
|
|
32
|
+
if (part.t === K.Integer)
|
|
33
|
+
return clampIndex(s, s.i + num(part));
|
|
34
|
+
return clampIndex(s, part.i);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The span a /part argument covers, as R3 reads it: a negative count, or a
|
|
38
|
+
* position before the series' own, goes back from where the series is.
|
|
39
|
+
*/
|
|
40
|
+
function partSpan(s, part) {
|
|
41
|
+
const e = part.t === K.Integer ? clampIndex(s, s.i + num(part)) : clampIndex(s, part.i);
|
|
42
|
+
return e < s.i ? [e, s.i] : [s.i, e];
|
|
43
|
+
}
|
|
44
|
+
/** What insert, append and change put in: /part limits the value, /dup repeats it. */
|
|
45
|
+
function toPut(s, v, only, part, dup) {
|
|
46
|
+
let { add, nl } = itemsFor(s.t, v, only);
|
|
47
|
+
if (part !== null) {
|
|
48
|
+
const n = part.t === K.Integer ? Math.max(0, num(part)) : Math.max(0, part.i - v.i);
|
|
49
|
+
add = add.slice(0, n);
|
|
50
|
+
nl = nl?.slice(0, n);
|
|
51
|
+
}
|
|
52
|
+
if (dup !== null) {
|
|
53
|
+
const times = Math.max(0, num(dup));
|
|
54
|
+
const one = add;
|
|
55
|
+
const oneNl = nl;
|
|
56
|
+
add = [];
|
|
57
|
+
nl = oneNl === undefined ? undefined : [];
|
|
58
|
+
for (let k = 0; k < times; k++) {
|
|
59
|
+
add.push(...one);
|
|
60
|
+
if (nl !== undefined)
|
|
61
|
+
nl.push(...oneNl);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return nl === undefined ? { add } : { add, nl };
|
|
65
|
+
}
|
|
66
|
+
// --- navigation ------------------------------------------------------------------
|
|
67
|
+
native('next', '["The series one position on." series [series!]]', (_t, f) => value(at(f.args[0], f.args[0].i + 1)));
|
|
68
|
+
native('back', '["The series one position back." series [series!]]', (_t, f) => value(at(f.args[0], f.args[0].i - 1)));
|
|
69
|
+
native('head', '["The series at its first position." series [series!]]', (_t, f) => value(at(f.args[0], 0)));
|
|
70
|
+
native('tail', '["The series just past its last value." series [series!]]', (_t, f) => value(at(f.args[0], Infinity)));
|
|
71
|
+
native('skip', '["The series moved on, or back, by an offset." series [series!] offset [integer!]]', (_t, f) => {
|
|
72
|
+
const s = f.args[0];
|
|
73
|
+
return value(at(s, s.i + num(f.args[1])));
|
|
74
|
+
});
|
|
75
|
+
native('at', '["The series at a position, counted from where it is." series [series!] index [integer!]]', (_t, f) => {
|
|
76
|
+
const s = f.args[0];
|
|
77
|
+
const n = num(f.args[1]);
|
|
78
|
+
return value(at(s, s.i + (n > 0 ? n - 1 : n)));
|
|
79
|
+
});
|
|
80
|
+
native('index?', '["The position of the series, from 1." series [series!]]', (_t, f) => value(integer(f.args[0].i + 1)));
|
|
81
|
+
native('head?', '["True if the series is at its head." series [series!]]', (_t, f) => value(logic(f.args[0].i === 0)));
|
|
82
|
+
native('tail?', '["True if the series is at its tail." series [series!]]', (_t, f) => value(logic(lengthOf(f.args[0]) === 0)));
|
|
83
|
+
// --- editing ---------------------------------------------------------------------
|
|
84
|
+
const PUT_SPEC = 'series [series!] value [any-value!] /part "Put in only part of the value" range [integer! series!] /only "A block goes in as one value" /dup "Put it in several times" count [integer!]';
|
|
85
|
+
const putArgs = (f) => toPut(f.args[0], f.args[1], used(f.args[4]), used(f.args[2]) ? f.args[3] : null, used(f.args[5]) ? f.args[6] : null);
|
|
86
|
+
native('append', `["Puts a value at the tail of a series; gives the series at its head." ${PUT_SPEC}]`, (_t, f) => {
|
|
87
|
+
const s = f.args[0];
|
|
88
|
+
const { add, nl } = putArgs(f);
|
|
89
|
+
splice(s.s, s.s.data.length, 0, add, nl);
|
|
90
|
+
return value(at(s, 0));
|
|
91
|
+
});
|
|
92
|
+
native('insert', `["Puts a value where the series is; gives the series just after it." ${PUT_SPEC}]`, (_t, f) => {
|
|
93
|
+
const s = f.args[0];
|
|
94
|
+
const { add, nl } = putArgs(f);
|
|
95
|
+
splice(s.s, s.i, 0, add, nl);
|
|
96
|
+
return value(at(s, s.i + add.length));
|
|
97
|
+
});
|
|
98
|
+
native('change', '["Puts a value over what the series holds where it is; gives the series just after it." series [series!] value [any-value!] /part "Change this much of the series" range [integer! series!] /only "A block goes in as one value" /dup "Put the value in several times" count [integer!]]', (_t, f) => {
|
|
99
|
+
const s = f.args[0];
|
|
100
|
+
const { add, nl } = toPut(s, f.args[1], used(f.args[4]), null, used(f.args[5]) ? f.args[6] : null);
|
|
101
|
+
const end = used(f.args[2]) ? partEnd(s, f.args[3]) : Math.min(s.s.data.length, s.i + add.length);
|
|
102
|
+
splice(s.s, s.i, Math.max(0, end - s.i), add, nl);
|
|
103
|
+
return value(at(s, s.i + add.length));
|
|
104
|
+
});
|
|
105
|
+
native('remove', '["Removes what the series holds where it is; gives the series." series [series!] /part "Remove this much" range [integer! series!]]', (_t, f) => {
|
|
106
|
+
const s = f.args[0];
|
|
107
|
+
const [from, end] = used(f.args[1]) ? partSpan(s, f.args[2]) : [s.i, Math.min(s.s.data.length, s.i + 1)];
|
|
108
|
+
splice(s.s, from, Math.max(0, end - from), []);
|
|
109
|
+
return value(at(s, from));
|
|
110
|
+
});
|
|
111
|
+
native('clear', '["Removes everything from the series\' position to its tail." series [series! map!]]', (_t, f) => {
|
|
112
|
+
const x = f.args[0];
|
|
113
|
+
if (x.t === K.Map) {
|
|
114
|
+
x.s.data.length = 0;
|
|
115
|
+
return value(x);
|
|
116
|
+
}
|
|
117
|
+
const s = x;
|
|
118
|
+
splice(s.s, s.i, s.s.data.length - s.i, []);
|
|
119
|
+
return value(s);
|
|
120
|
+
});
|
|
121
|
+
native('copy', '["A copy of the series, map, object or bitset." value [series! map! object! bitset!] /part length [integer! series!] /deep]', (_t, f) => {
|
|
122
|
+
const x = f.args[0];
|
|
123
|
+
const deep = used(f.args[3]);
|
|
124
|
+
if (x.t === K.Map)
|
|
125
|
+
return value({ t: K.Map, s: deep ? copyDeep(x.s) : { data: x.s.data.slice() } });
|
|
126
|
+
if (x.t === K.Object || x.t === K.Error) {
|
|
127
|
+
const ctx = context(x.ctx.keys.slice(), x.ctx.vals.slice());
|
|
128
|
+
if (x.ctx.self)
|
|
129
|
+
ctx.self = true;
|
|
130
|
+
return value({ t: x.t, ctx });
|
|
131
|
+
}
|
|
132
|
+
if (x.t === K.Bitset)
|
|
133
|
+
return value({ t: K.Bitset, s: { data: x.s.data.slice() }, neg: x.neg });
|
|
134
|
+
const s = x;
|
|
135
|
+
const [from, end] = used(f.args[1]) ? partSpan(s, f.args[2]) : [s.i, s.s.data.length];
|
|
136
|
+
if (isAnyBlock(s.t)) {
|
|
137
|
+
const b = s;
|
|
138
|
+
const part = { data: b.s.data.slice(from, end) };
|
|
139
|
+
if (b.s.nl)
|
|
140
|
+
part.nl = b.s.nl.slice(from, end);
|
|
141
|
+
return value({ t: b.t, s: deep ? copyDeep(part) : part, i: 0 });
|
|
142
|
+
}
|
|
143
|
+
return value({ t: s.t, s: { data: s.s.data.slice(from, end) }, i: 0 });
|
|
144
|
+
});
|
|
145
|
+
native('reverse', '["Reverses the order of the values from the series\' position." series [series!] /part length [integer! series!]]', (_t, f) => {
|
|
146
|
+
const s = f.args[0];
|
|
147
|
+
const end = used(f.args[1]) ? partEnd(s, f.args[2]) : s.s.data.length;
|
|
148
|
+
const d = s.s.data;
|
|
149
|
+
ownMeta(s.s);
|
|
150
|
+
const nl = s.s.nl;
|
|
151
|
+
for (let a = s.i, b = end - 1; a < b; a++, b--) {
|
|
152
|
+
[d[a], d[b]] = [d[b], d[a]];
|
|
153
|
+
if (nl)
|
|
154
|
+
[nl[a], nl[b]] = [nl[b], nl[a]];
|
|
155
|
+
}
|
|
156
|
+
return value(s);
|
|
157
|
+
});
|
|
158
|
+
// --- searching -------------------------------------------------------------------
|
|
159
|
+
/** Does block element `el` match `v`? Datatypes and typesets match by kind. */
|
|
160
|
+
function matchCell(el, v, cased) {
|
|
161
|
+
if (v.t === K.Datatype && el.t !== K.Datatype)
|
|
162
|
+
return el.t === v.k;
|
|
163
|
+
if (v.t === K.Typeset && el.t !== K.Typeset)
|
|
164
|
+
return (v.bits & kindBit(el.t)) !== 0n;
|
|
165
|
+
return cased ? strictEqual(el, v) : isEqual(el, v);
|
|
166
|
+
}
|
|
167
|
+
/** What to look for: a run of elements, matched with `eq`. */
|
|
168
|
+
function needle(s, v, only, cased, same = false) {
|
|
169
|
+
if (isAnyBlock(s.t)) {
|
|
170
|
+
const run = !only && isAnyBlock(v.t) ? v.s.data.slice(v.i) : [v];
|
|
171
|
+
return { run, eq: same ? (a, b) => sameValue(a, b) : (a, b) => matchCell(a, b, cased) };
|
|
172
|
+
}
|
|
173
|
+
const run = itemsFor(s.t, v, false).add;
|
|
174
|
+
const eq = cased || s.t === K.Binary
|
|
175
|
+
? (a, b) => a === b
|
|
176
|
+
: (a, b) => foldCase(a) === foldCase(b);
|
|
177
|
+
return { run, eq };
|
|
178
|
+
}
|
|
179
|
+
const STAR = 0x2a;
|
|
180
|
+
const QUESTION = 0x3f;
|
|
181
|
+
/**
|
|
182
|
+
* Length of the shortest match of a wildcard pattern at `i`, or -1. Each *
|
|
183
|
+
* tries the shortest run first; the backtracking keeps one entry per *.
|
|
184
|
+
*/
|
|
185
|
+
function wildAt(d, i, end, run, eq) {
|
|
186
|
+
const stars = [];
|
|
187
|
+
let p = 0;
|
|
188
|
+
let k = i;
|
|
189
|
+
for (;;) {
|
|
190
|
+
if (p === run.length)
|
|
191
|
+
return k - i;
|
|
192
|
+
const c = run[p];
|
|
193
|
+
if (c === STAR) {
|
|
194
|
+
stars.push([p + 1, k]);
|
|
195
|
+
p++;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (k < end && (c === QUESTION || eq(d[k], c))) {
|
|
199
|
+
p++;
|
|
200
|
+
k++;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
// let the last * take one more character, or fail
|
|
204
|
+
const last = stars.pop();
|
|
205
|
+
if (last === undefined || last[1] >= end)
|
|
206
|
+
return -1;
|
|
207
|
+
p = last[0];
|
|
208
|
+
k = last[1] + 1;
|
|
209
|
+
stars.push([p, k]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/** Index where the run starts, or -1. */
|
|
213
|
+
function findIn(s, v, o) {
|
|
214
|
+
const d = s.s.data;
|
|
215
|
+
const { run, eq } = needle(s, v, o.only, o.cased, o.same);
|
|
216
|
+
const wild = o.wild === true && !isAnyBlock(s.t);
|
|
217
|
+
let len = run.length;
|
|
218
|
+
const fits = (i) => {
|
|
219
|
+
if (wild) {
|
|
220
|
+
len = wildAt(d, i, o.end, run, eq);
|
|
221
|
+
return len >= 0;
|
|
222
|
+
}
|
|
223
|
+
if (i + run.length > o.end)
|
|
224
|
+
return false;
|
|
225
|
+
for (let k = 0; k < run.length; k++)
|
|
226
|
+
if (!eq(d[i + k], run[k]))
|
|
227
|
+
return false;
|
|
228
|
+
return true;
|
|
229
|
+
};
|
|
230
|
+
if (o.match)
|
|
231
|
+
return { at: fits(s.i) ? s.i : -1, len };
|
|
232
|
+
if (o.reverse) {
|
|
233
|
+
for (let i = s.i - o.skip; i >= 0; i -= o.skip)
|
|
234
|
+
if (fits(i))
|
|
235
|
+
return { at: i, len };
|
|
236
|
+
}
|
|
237
|
+
else if (o.last) {
|
|
238
|
+
for (let i = o.end - (wild ? 0 : run.length); i >= s.i; i--)
|
|
239
|
+
if ((i - s.i) % o.skip === 0 && fits(i))
|
|
240
|
+
return { at: i, len };
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
for (let i = s.i; i + (wild ? 0 : run.length) <= o.end; i += o.skip)
|
|
244
|
+
if (fits(i))
|
|
245
|
+
return { at: i, len };
|
|
246
|
+
}
|
|
247
|
+
return { at: -1, len };
|
|
248
|
+
}
|
|
249
|
+
native('find', '["Finds a value in a series; gives the series where it is, or none." series [series! none! typeset!] value [any-type!] /part "Look only this far" length [integer! series!] /only "A block is one value" /case "Capitals matter" /skip "Look only at every so many" size [integer!] /last "Find the last one" /tail "Give the series just after it" /match "Only where the series is" /reverse "Look back from where the series is" /any "In a string, * is any run of characters and ? any one" /same "In a block, the very same value (same?), not an equal one"]', (_t, f) => {
|
|
250
|
+
const x = f.args[0];
|
|
251
|
+
if (x.t === K.None)
|
|
252
|
+
return value(NONE);
|
|
253
|
+
if (x.t === K.Typeset) {
|
|
254
|
+
const v = f.args[1];
|
|
255
|
+
return value(logic(v.t === K.Datatype && (x.bits & kindBit(v.k)) !== 0n));
|
|
256
|
+
}
|
|
257
|
+
const s = x;
|
|
258
|
+
const a = f.args;
|
|
259
|
+
const o = {
|
|
260
|
+
only: used(a[4]), cased: used(a[5]), skip: used(a[6]) ? Math.max(1, num(a[7])) : 1,
|
|
261
|
+
last: used(a[8]), match: used(a[10]), end: used(a[2]) ? partEnd(s, a[3]) : s.s.data.length,
|
|
262
|
+
reverse: used(a[11]), wild: used(a[12]), same: used(a[13]),
|
|
263
|
+
};
|
|
264
|
+
const r = findIn(s, a[1], o);
|
|
265
|
+
if (r.at < 0)
|
|
266
|
+
return value(NONE);
|
|
267
|
+
return value(at(s, used(a[9]) || o.match ? r.at + r.len : r.at));
|
|
268
|
+
});
|
|
269
|
+
native('select', '["The value after the one found, or none." series [series! map! object! none!] value [any-type!] /only /case /skip size [integer!] /last]', (_t, f) => {
|
|
270
|
+
const x = f.args[0];
|
|
271
|
+
const a = f.args;
|
|
272
|
+
if (x.t === K.None)
|
|
273
|
+
return value(NONE);
|
|
274
|
+
if (x.t === K.Map || x.t === K.Object || x.t === K.Error) {
|
|
275
|
+
const r = pick(x, a[1]);
|
|
276
|
+
return value(typeof r === 'string' ? NONE : r);
|
|
277
|
+
}
|
|
278
|
+
const s = x;
|
|
279
|
+
const skip = used(a[4]) ? Math.max(1, num(a[5])) : 1;
|
|
280
|
+
const r = findIn(s, a[1], { only: used(a[2]), cased: used(a[3]), skip, last: used(a[6]), match: false, end: s.s.data.length });
|
|
281
|
+
if (r.at < 0)
|
|
282
|
+
return value(NONE);
|
|
283
|
+
return value(cellAt(s, r.at + r.len));
|
|
284
|
+
});
|
|
285
|
+
/**
|
|
286
|
+
* Bottom-up merge sort as a resumable state machine: it stops whenever it
|
|
287
|
+
* needs a comparison from a Trebol function, and continues with the answer.
|
|
288
|
+
* `before(a, b)` returns true/false, or null when it must ask the comparator.
|
|
289
|
+
*/
|
|
290
|
+
function sortRun(st, before, answer) {
|
|
291
|
+
const n = st.src.length;
|
|
292
|
+
for (;;) {
|
|
293
|
+
if (st.width >= n)
|
|
294
|
+
return null;
|
|
295
|
+
if (!st.merging) {
|
|
296
|
+
if (st.lo >= n) {
|
|
297
|
+
[st.src, st.dst] = [st.dst, st.src];
|
|
298
|
+
st.width *= 2;
|
|
299
|
+
st.lo = 0;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
st.mid = Math.min(st.lo + st.width, n);
|
|
303
|
+
st.hi = Math.min(st.lo + 2 * st.width, n);
|
|
304
|
+
st.i = st.lo;
|
|
305
|
+
st.j = st.mid;
|
|
306
|
+
st.k = st.lo;
|
|
307
|
+
st.merging = true;
|
|
308
|
+
}
|
|
309
|
+
while (st.k < st.hi) {
|
|
310
|
+
if (st.i < st.mid && st.j < st.hi) {
|
|
311
|
+
// take from the right only when it must come strictly first (stable)
|
|
312
|
+
let rightFirst = answer;
|
|
313
|
+
answer = null;
|
|
314
|
+
if (rightFirst === null)
|
|
315
|
+
rightFirst = before(st.src[st.j], st.src[st.i]);
|
|
316
|
+
if (rightFirst === null)
|
|
317
|
+
return [st.src[st.j], st.src[st.i]];
|
|
318
|
+
st.dst[st.k++] = rightFirst ? st.src[st.j++] : st.src[st.i++];
|
|
319
|
+
}
|
|
320
|
+
else if (st.i < st.mid) {
|
|
321
|
+
st.dst[st.k++] = st.src[st.i++];
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
st.dst[st.k++] = st.src[st.j++];
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
st.merging = false;
|
|
328
|
+
st.lo += 2 * st.width;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// As in R3 and Red, /compare takes a function or, with /skip, the position of the field to sort by.
|
|
332
|
+
native('sort', '["Sorts the series in place." series [series!] /case /skip size [integer!] /compare comparator [any-function! integer!] /reverse]', (_t, f, v) => {
|
|
333
|
+
const s = f.args[0];
|
|
334
|
+
const a = f.args;
|
|
335
|
+
const cased = used(a[1]);
|
|
336
|
+
const size = used(a[2]) ? Math.max(1, num(a[3])) : 1;
|
|
337
|
+
const by = used(a[4]) && a[5].t === K.Integer ? num(a[5]) : null;
|
|
338
|
+
if (by !== null && (by < 1 || by > size))
|
|
339
|
+
return fail('script', 'out-of-range', `sort cannot compare field ${by} of records of ${size}`);
|
|
340
|
+
const custom = used(a[4]) && by === null ? a[5] : null;
|
|
341
|
+
const rev = used(a[6]);
|
|
342
|
+
if (f.phase === 0) {
|
|
343
|
+
const d = s.s.data;
|
|
344
|
+
const recs = [];
|
|
345
|
+
for (let i = s.i; i < d.length; i += size)
|
|
346
|
+
recs.push(d.slice(i, i + size));
|
|
347
|
+
f.data = { src: recs, dst: recs.slice(), width: 1, lo: 0, mid: 0, hi: 0, i: 0, j: 0, k: 0, merging: false };
|
|
348
|
+
f.phase = 1;
|
|
349
|
+
}
|
|
350
|
+
const st = f.data;
|
|
351
|
+
let answer = null;
|
|
352
|
+
if (v !== null) {
|
|
353
|
+
answer = v.t === K.Integer ? num(v) < 0 : truthy(v);
|
|
354
|
+
if (rev)
|
|
355
|
+
answer = !answer;
|
|
356
|
+
}
|
|
357
|
+
const cellOf = (r, at = 0) => cellAt({ ...s, s: { data: r }, i: 0 }, at);
|
|
358
|
+
let bad = false;
|
|
359
|
+
const before = (x, y) => {
|
|
360
|
+
if (custom !== null)
|
|
361
|
+
return null;
|
|
362
|
+
const at = by === null ? 0 : by - 1;
|
|
363
|
+
const c = sortCompare(cellOf(x, at), cellOf(y, at), cased);
|
|
364
|
+
if (c === null)
|
|
365
|
+
bad = true;
|
|
366
|
+
return rev ? (c ?? 0) > 0 : (c ?? 0) < 0;
|
|
367
|
+
};
|
|
368
|
+
const ask = sortRun(st, before, answer);
|
|
369
|
+
if (bad)
|
|
370
|
+
return fail('script', 'bad-compare', 'sort cannot compare those values');
|
|
371
|
+
if (ask !== null)
|
|
372
|
+
return { k: 'call', fn: custom, args: [cellOf(ask[0]), cellOf(ask[1])] };
|
|
373
|
+
const flat = st.src.flat();
|
|
374
|
+
const d = s.s.data;
|
|
375
|
+
for (let k = 0; k < flat.length; k++)
|
|
376
|
+
d[s.i + k] = flat[k];
|
|
377
|
+
ownMeta(s.s);
|
|
378
|
+
if (s.s.nl)
|
|
379
|
+
s.s.nl.fill(false, s.i);
|
|
380
|
+
return value(s);
|
|
381
|
+
});
|
|
382
|
+
/** Order for sort: like `<`, case-sensitive with /case, kinds ordered by id when mixed. */
|
|
383
|
+
function sortCompare(a, b, cased) {
|
|
384
|
+
if (cased && a.t === K.String && b.t === K.String) {
|
|
385
|
+
const x = a.s.data.slice(a.i);
|
|
386
|
+
const y = b.s.data.slice(b.i);
|
|
387
|
+
for (let k = 0; k < Math.min(x.length, y.length); k++)
|
|
388
|
+
if (x[k] !== y[k])
|
|
389
|
+
return x[k] < y[k] ? -1 : 1;
|
|
390
|
+
return x.length < y.length ? -1 : x.length > y.length ? 1 : 0;
|
|
391
|
+
}
|
|
392
|
+
if (cased && a.t === K.Char && b.t === K.Char)
|
|
393
|
+
return a.v < b.v ? -1 : a.v > b.v ? 1 : 0;
|
|
394
|
+
const c = compare(a, b);
|
|
395
|
+
if (c !== null)
|
|
396
|
+
return c;
|
|
397
|
+
if (a.t !== b.t)
|
|
398
|
+
return a.t < b.t ? -1 : 1;
|
|
399
|
+
if (a.t === K.None)
|
|
400
|
+
return 0;
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
// --- iteration that edits or builds --------------------------------------------
|
|
404
|
+
/** Shared setup for remove-each and map-each: a context for the words and a bound body copy. */
|
|
405
|
+
function eachSetup(f) {
|
|
406
|
+
const words = f.args[0];
|
|
407
|
+
const list = words.t === K.Block ? words.s.data.slice(words.i) : [words];
|
|
408
|
+
const ctx = context(list.map(w => w.sym), list.map(() => NONE));
|
|
409
|
+
const body = f.args[2];
|
|
410
|
+
const s = copyDeep(body.s, body.i);
|
|
411
|
+
bindSeries(s, ctx);
|
|
412
|
+
return { ctx, body: { t: K.Block, s, i: 0 } };
|
|
413
|
+
}
|
|
414
|
+
function eachNext(f, st, data) {
|
|
415
|
+
const d = data.s.data;
|
|
416
|
+
if (st.pos >= d.length)
|
|
417
|
+
return null;
|
|
418
|
+
for (let k = 0; k < st.ctx.vals.length; k++)
|
|
419
|
+
st.ctx.vals[k] = cellAt(data, st.pos + k);
|
|
420
|
+
st.pos += st.ctx.vals.length;
|
|
421
|
+
f.trap = (1 << T.Break) | (1 << T.Continue);
|
|
422
|
+
return doBlock(st.body);
|
|
423
|
+
}
|
|
424
|
+
native('remove-each', "[\"Removes the values of a series for which the block gives true.\" 'word [word! block!] data [series!] body [block!]]", (_t, f, v) => {
|
|
425
|
+
const data = f.args[1];
|
|
426
|
+
if (f.phase === 0) {
|
|
427
|
+
f.phase = 1;
|
|
428
|
+
f.data = { ...eachSetup(f), pos: data.i, out: [], keep: data.s.data.slice(0, data.i) };
|
|
429
|
+
}
|
|
430
|
+
const st = f.data;
|
|
431
|
+
const n = st.ctx.vals.length;
|
|
432
|
+
const th = f.thrown;
|
|
433
|
+
f.thrown = null;
|
|
434
|
+
const broke = th !== null && th.kind === T.Break;
|
|
435
|
+
if (v !== null || th !== null) {
|
|
436
|
+
const from = st.pos - n;
|
|
437
|
+
const chunk = data.s.data.slice(from, Math.min(from + n, data.s.data.length));
|
|
438
|
+
if (broke || v === null || !truthy(v))
|
|
439
|
+
st.keep.push(...chunk);
|
|
440
|
+
}
|
|
441
|
+
const step = broke ? null : eachNext(f, st, data);
|
|
442
|
+
if (step !== null)
|
|
443
|
+
return step;
|
|
444
|
+
if (broke)
|
|
445
|
+
st.keep.push(...data.s.data.slice(st.pos));
|
|
446
|
+
splice(data.s, 0, data.s.data.length, st.keep);
|
|
447
|
+
return value(data);
|
|
448
|
+
});
|
|
449
|
+
native('map-each', "[\"A block of what the block gives for each value of a series.\" 'word [word! block!] data [series!] body [block!]]", (_t, f, v) => {
|
|
450
|
+
const data = f.args[1];
|
|
451
|
+
if (f.phase === 0) {
|
|
452
|
+
f.phase = 1;
|
|
453
|
+
f.data = { ...eachSetup(f), pos: data.i, out: [], keep: [] };
|
|
454
|
+
}
|
|
455
|
+
const st = f.data;
|
|
456
|
+
const th = f.thrown;
|
|
457
|
+
f.thrown = null;
|
|
458
|
+
if (th !== null && th.kind === T.Break)
|
|
459
|
+
return value({ t: K.Block, s: { data: st.out }, i: 0 });
|
|
460
|
+
if (v !== null && v.t !== K.Unset) {
|
|
461
|
+
if (v.t === K.Block)
|
|
462
|
+
st.out.push(...v.s.data.slice(v.i));
|
|
463
|
+
else
|
|
464
|
+
st.out.push(v);
|
|
465
|
+
}
|
|
466
|
+
return eachNext(f, st, data) ?? value({ t: K.Block, s: { data: st.out }, i: 0 });
|
|
467
|
+
});
|
|
468
|
+
// --- set operations ----------------------------------------------------------------
|
|
469
|
+
function itemEq(t, cased) {
|
|
470
|
+
if (isAnyBlock(t))
|
|
471
|
+
return (a, b) => (cased ? strictEqual(a, b) : isEqual(a, b));
|
|
472
|
+
if (cased || t === K.Binary)
|
|
473
|
+
return (a, b) => a === b;
|
|
474
|
+
return (a, b) => foldCase(a) === foldCase(b);
|
|
475
|
+
}
|
|
476
|
+
function distinct(list, eq) {
|
|
477
|
+
const out = [];
|
|
478
|
+
for (const x of list)
|
|
479
|
+
if (!out.some(y => eq(x, y)))
|
|
480
|
+
out.push(x);
|
|
481
|
+
return out;
|
|
482
|
+
}
|
|
483
|
+
function setResult(like, list) {
|
|
484
|
+
return { t: like.t, s: { data: list }, i: 0 };
|
|
485
|
+
}
|
|
486
|
+
function setOp(f, two, pick) {
|
|
487
|
+
const a = f.args[0];
|
|
488
|
+
const b = two ? f.args[1] : null;
|
|
489
|
+
if (b !== null && isAnyBlock(a.t) !== isAnyBlock(b.t))
|
|
490
|
+
return fail('script', 'bad-set', 'both series must be of the same kind');
|
|
491
|
+
const eq = itemEq(a.t, used(f.args[two ? 2 : 1]));
|
|
492
|
+
const xs = a.s.data.slice(a.i);
|
|
493
|
+
const ys = b === null ? [] : b.s.data.slice(b.i);
|
|
494
|
+
return value(setResult(a, pick(xs, ys, eq)));
|
|
495
|
+
}
|
|
496
|
+
native('unique', '["The values of the series, each once." set [series!] /case]', (_t, f) => setOp(f, false, (a, _b, eq) => distinct(a, eq)));
|
|
497
|
+
native('union', '["The values that are in either series, each once." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct([...a, ...b], eq)));
|
|
498
|
+
native('intersect', '["The values that are in both series." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => b.some(y => eq(x, y))), eq)));
|
|
499
|
+
// Of two dates, the time between them (R3).
|
|
500
|
+
native('difference', '["The values that are in one series but not the other; of two dates, the time between them." a [series! date!] b [series! date!] /case]', (_t, f) => {
|
|
501
|
+
const [a, b] = f.args;
|
|
502
|
+
if (a.t === K.Date || b.t === K.Date) {
|
|
503
|
+
if (a.t !== K.Date || b.t !== K.Date)
|
|
504
|
+
return fail('script', 'bad-set', 'difference takes two dates or two series');
|
|
505
|
+
return value(time(dateDiff(a.v, b.v)));
|
|
506
|
+
}
|
|
507
|
+
return setOp(f, true, (xs, ys, eq) => distinct([...xs.filter(x => !ys.some(y => eq(x, y))), ...ys.filter(y => !xs.some(x => eq(x, y)))], eq));
|
|
508
|
+
});
|
|
509
|
+
native('exclude', '["The values of the first series that are not in the second." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => !b.some(y => eq(x, y))), eq)));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|