trebol 0.1.0 → 0.1.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/README.md +18 -3
- package/dist/cli.js +13790 -11417
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- 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 +73 -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 +68 -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 +519 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +487 -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 +4974 -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/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools.d.ts +23 -0
- package/dist/lib/host/devtools.js +626 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +29 -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 +40 -0
- package/dist/lib/host/index.js +192 -0
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -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 +30 -0
- package/dist/lib/host/markdown.js +161 -0
- package/dist/lib/host/modules.d.ts +14 -0
- package/dist/lib/host/modules.js +300 -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 +549 -0
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -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 +29 -0
- package/dist/lib/host/rows.js +109 -0
- package/dist/lib/host/scheduler.d.ts +51 -0
- package/dist/lib/host/scheduler.js +197 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1667 -0
- package/dist/lib/ui/i18n.trb +117 -0
- package/dist/lib/ui/test.trb +565 -0
- package/dist/lib/ui/ui.trb +1678 -0
- package/dist/trebol.js +1427 -138
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +23 -7
- package/src/core/boot.ts +1 -1
- package/src/core/func.ts +4 -0
- package/src/core/natives/control.ts +1 -1
- package/src/core/natives/series.ts +9 -1
- package/src/core/natives/text.ts +2 -0
- package/src/host/browser.ts +1235 -83
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools.ts +641 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +39 -0
- package/src/host/index.ts +5 -3
- package/src/host/js.ts +127 -0
- package/src/host/markdown.ts +9 -0
- package/src/host/modules.ts +41 -2
- package/src/host/net.ts +9 -1
- package/src/host/orbit.ts +432 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +62 -36
- package/src/host/theme.ts +270 -6
- package/src/ui/i18n.trb +10 -0
- package/src/ui/test.trb +565 -0
- package/src/ui/ui.trb +564 -90
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// Text and conversion natives: case, trim, split, `to`, `construct`, and the
|
|
2
|
+
// block builders `compose` and `collect-words`, plus the iteration natives
|
|
3
|
+
// that walk a series held in a word (`forall`, `forskip`) and `apply`.
|
|
4
|
+
import { K, isAnyBlock, isAnyString, isAnyWord, kindName, kindBit, kindByName } from "../kinds.js";
|
|
5
|
+
import { intern, canon, spell } from "../symbols.js";
|
|
6
|
+
import * as I64 from "../int64.js";
|
|
7
|
+
import { decToNumber } from "../dec.js";
|
|
8
|
+
import { ownMeta, NONE, TRUE, FALSE, integer, decimal, percent, money, char, logic, pair, tuple, codePoints, text, datatype, typeset, } from "../values.js";
|
|
9
|
+
import { getWord, setWord } from "../ctx.js";
|
|
10
|
+
import { foldCase, toDec } from "../compare.js";
|
|
11
|
+
import { load, LoadError } from "../lexer.js";
|
|
12
|
+
import { form } from "../mold.js";
|
|
13
|
+
import { typesOf } from "../func.js";
|
|
14
|
+
import { textOf, utf8, at } from "../series.js";
|
|
15
|
+
import { T } from "../eval.js";
|
|
16
|
+
import { native, value, fail, truthy, doBlock } from "./define.js";
|
|
17
|
+
const used = (c) => c === TRUE;
|
|
18
|
+
const num = (c) => I64.toNumber(c.v);
|
|
19
|
+
const isSpace = (cp) => cp === 32 || cp === 9 || cp === 10 || cp === 13 || cp === 0xa0;
|
|
20
|
+
// --- case ------------------------------------------------------------------------
|
|
21
|
+
function recase(upper) {
|
|
22
|
+
return (_t, f) => {
|
|
23
|
+
const x = f.args[0];
|
|
24
|
+
const conv = (cp) => {
|
|
25
|
+
const s = String.fromCodePoint(cp);
|
|
26
|
+
const r = upper ? s.toUpperCase() : s.toLowerCase();
|
|
27
|
+
return r.length === s.length ? r.codePointAt(0) : cp;
|
|
28
|
+
};
|
|
29
|
+
if (x.t === K.Char)
|
|
30
|
+
return value(char(conv(x.v)));
|
|
31
|
+
const s = x;
|
|
32
|
+
const end = used(f.args[1]) ? Math.min(s.s.data.length, s.i + num(f.args[2])) : s.s.data.length;
|
|
33
|
+
for (let i = s.i; i < end; i++)
|
|
34
|
+
s.s.data[i] = conv(s.s.data[i]);
|
|
35
|
+
return value(s);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
native('uppercase', '["Changes the text to capitals." string [any-string! char!] /part length [integer!]]', recase(true));
|
|
39
|
+
native('lowercase', '["Changes the text to small letters." string [any-string! char!] /part length [integer!]]', recase(false));
|
|
40
|
+
// --- trim and split --------------------------------------------------------------
|
|
41
|
+
native('trim', '["Removes spaces at both ends, or with refinements other spaces too." series [any-string! block!] /head /tail /lines /all]', (_t, f) => {
|
|
42
|
+
const x = f.args[0];
|
|
43
|
+
if (x.t === K.Block) {
|
|
44
|
+
const kept = x.s.data.filter((c, i) => i < x.i || c.t !== K.None);
|
|
45
|
+
x.s.data.splice(0, x.s.data.length, ...kept);
|
|
46
|
+
ownMeta(x.s);
|
|
47
|
+
if (x.s.nl)
|
|
48
|
+
x.s.nl.length = 0;
|
|
49
|
+
if (x.s.pos)
|
|
50
|
+
x.s.pos.length = 0;
|
|
51
|
+
return value(x);
|
|
52
|
+
}
|
|
53
|
+
const s = x;
|
|
54
|
+
let cps = s.s.data.slice(s.i);
|
|
55
|
+
const [head, tail, lines, all] = [used(f.args[1]), used(f.args[2]), used(f.args[3]), used(f.args[4])];
|
|
56
|
+
if (all) {
|
|
57
|
+
cps = cps.filter(cp => !isSpace(cp));
|
|
58
|
+
}
|
|
59
|
+
else if (lines) {
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const cp of cps) {
|
|
62
|
+
if (isSpace(cp)) {
|
|
63
|
+
if (out.length > 0 && out[out.length - 1] !== 32)
|
|
64
|
+
out.push(32);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
out.push(cp);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (out[out.length - 1] === 32)
|
|
71
|
+
out.pop();
|
|
72
|
+
cps = out;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
let a = 0;
|
|
76
|
+
let b = cps.length;
|
|
77
|
+
if (head || !tail)
|
|
78
|
+
while (a < b && isSpace(cps[a]))
|
|
79
|
+
a++;
|
|
80
|
+
if (tail || !head)
|
|
81
|
+
while (b > a && isSpace(cps[b - 1]))
|
|
82
|
+
b--;
|
|
83
|
+
cps = cps.slice(a, b);
|
|
84
|
+
}
|
|
85
|
+
s.s.data.splice(s.i, s.s.data.length - s.i, ...cps);
|
|
86
|
+
return value(s);
|
|
87
|
+
});
|
|
88
|
+
native('split', '["Splits a series at a delimiter, or into pieces of a size." series [any-string! block!] dlm [any-string! char! integer!]]', (_t, f) => {
|
|
89
|
+
const s = f.args[0];
|
|
90
|
+
const dlm = f.args[1];
|
|
91
|
+
const d = s.s.data.slice(s.i);
|
|
92
|
+
const piece = (from, to) => ({ t: s.t, s: { data: d.slice(from, to) }, i: 0 });
|
|
93
|
+
const out = [];
|
|
94
|
+
if (dlm.t === K.Integer) {
|
|
95
|
+
const n = Math.max(1, num(dlm));
|
|
96
|
+
for (let i = 0; i < d.length; i += n)
|
|
97
|
+
out.push(piece(i, i + n));
|
|
98
|
+
return value({ t: K.Block, s: { data: out }, i: 0 });
|
|
99
|
+
}
|
|
100
|
+
if (isAnyBlock(s.t))
|
|
101
|
+
return fail('script', 'bad-split', 'a block splits only into pieces of a size');
|
|
102
|
+
const sep = dlm.t === K.Char ? [dlm.v] : dlm.s.data.slice(dlm.i);
|
|
103
|
+
if (sep.length === 0)
|
|
104
|
+
return fail('script', 'bad-split', 'the delimiter is empty');
|
|
105
|
+
let start = 0;
|
|
106
|
+
for (let i = 0; i + sep.length <= d.length;) {
|
|
107
|
+
if (sep.every((cp, k) => foldCase(d[i + k]) === foldCase(cp))) {
|
|
108
|
+
out.push(piece(start, i));
|
|
109
|
+
i += sep.length;
|
|
110
|
+
start = i;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
i++;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
out.push(piece(start, d.length));
|
|
117
|
+
return value({ t: K.Block, s: { data: out }, i: 0 });
|
|
118
|
+
});
|
|
119
|
+
// --- to and construct -------------------------------------------------------------
|
|
120
|
+
/** Loads text expecting exactly one value of one of the given kinds. */
|
|
121
|
+
function loadOne(src, kinds) {
|
|
122
|
+
try {
|
|
123
|
+
const b = load(src);
|
|
124
|
+
const c = b.s.data[0];
|
|
125
|
+
return b.s.data.length === 1 && c !== undefined && kinds.includes(c.t) ? c : null;
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
if (e instanceof LoadError)
|
|
129
|
+
return null;
|
|
130
|
+
throw e;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const NUMBERS = [K.Integer, K.Decimal, K.Percent, K.Money];
|
|
134
|
+
function toNumberCell(v) {
|
|
135
|
+
if (v.t === K.Integer || v.t === K.Decimal || v.t === K.Percent || v.t === K.Money)
|
|
136
|
+
return v;
|
|
137
|
+
if (v.t === K.Char)
|
|
138
|
+
return integer(v.v);
|
|
139
|
+
if (v.t === K.Logic)
|
|
140
|
+
return integer(v.v ? 1 : 0);
|
|
141
|
+
if (isAnyString(v.t))
|
|
142
|
+
return loadOne(text(v).trim(), NUMBERS);
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
function toKind(k, v) {
|
|
146
|
+
const bad = () => `cannot convert ${kindName(v.t)} to ${kindName(k)}`;
|
|
147
|
+
switch (k) {
|
|
148
|
+
case K.String:
|
|
149
|
+
case K.File:
|
|
150
|
+
case K.Email:
|
|
151
|
+
case K.Url:
|
|
152
|
+
case K.Tag: {
|
|
153
|
+
if (v.t === K.Binary)
|
|
154
|
+
return { t: k, s: { data: codePoints(new TextDecoder().decode(new Uint8Array(v.s.data.slice(v.i)))) }, i: 0 };
|
|
155
|
+
if (isAnyWord(v.t))
|
|
156
|
+
return { t: k, s: { data: codePoints(spell(v.sym)) }, i: 0 };
|
|
157
|
+
if (v.t === K.Char)
|
|
158
|
+
return { t: k, s: { data: [v.v] }, i: 0 };
|
|
159
|
+
// the text itself, so to string! <a> is "a", as in R3; form gives "<a>"
|
|
160
|
+
if (isAnyString(v.t))
|
|
161
|
+
return { t: k, s: { data: v.s.data.slice(v.i) }, i: 0 };
|
|
162
|
+
return { t: k, s: { data: codePoints(textOf(v)) }, i: 0 };
|
|
163
|
+
}
|
|
164
|
+
case K.Binary:
|
|
165
|
+
if (v.t === K.Binary)
|
|
166
|
+
return { t: K.Binary, s: { data: v.s.data.slice(v.i) }, i: 0 };
|
|
167
|
+
if (v.t === K.Block)
|
|
168
|
+
return { t: K.Binary, s: { data: v.s.data.slice(v.i).map(c => (c.t === K.Integer ? num(c) & 255 : 0)) }, i: 0 };
|
|
169
|
+
return { t: K.Binary, s: { data: utf8(textOf(v)) }, i: 0 };
|
|
170
|
+
case K.Word:
|
|
171
|
+
case K.SetWord:
|
|
172
|
+
case K.GetWord:
|
|
173
|
+
case K.LitWord:
|
|
174
|
+
case K.Refinement:
|
|
175
|
+
case K.Issue: {
|
|
176
|
+
if (isAnyWord(v.t))
|
|
177
|
+
return { t: k, sym: v.sym, bind: v.bind };
|
|
178
|
+
if (isAnyString(v.t) || v.t === K.Char) {
|
|
179
|
+
const name = v.t === K.Char ? String.fromCodePoint(v.v) : text(v);
|
|
180
|
+
const w = loadOne(name, [K.Word]);
|
|
181
|
+
if (w === null && k !== K.Issue && k !== K.Refinement)
|
|
182
|
+
return `${JSON.stringify(name)} is not a valid word`;
|
|
183
|
+
return { t: k, sym: intern(name), bind: null };
|
|
184
|
+
}
|
|
185
|
+
if (v.t === K.Datatype)
|
|
186
|
+
return { t: k, sym: intern(kindName(v.k)), bind: null };
|
|
187
|
+
return bad();
|
|
188
|
+
}
|
|
189
|
+
case K.Integer: {
|
|
190
|
+
// a time converts to its seconds, as in R3
|
|
191
|
+
const n = v.t === K.Time ? decimal(v.v / 1e9) : toNumberCell(v);
|
|
192
|
+
if (n === null)
|
|
193
|
+
return bad();
|
|
194
|
+
if (n.t === K.Integer)
|
|
195
|
+
return n;
|
|
196
|
+
const x = n.t === K.Money ? decToNumber(n.v) : n.v;
|
|
197
|
+
const r = I64.fromNumber(Math.trunc(x));
|
|
198
|
+
return r === null ? 'number is too big for integer!' : integer(r);
|
|
199
|
+
}
|
|
200
|
+
case K.Decimal:
|
|
201
|
+
case K.Percent: {
|
|
202
|
+
const n = v.t === K.Time && k === K.Decimal ? decimal(v.v / 1e9) : toNumberCell(v);
|
|
203
|
+
if (n === null)
|
|
204
|
+
return bad();
|
|
205
|
+
const x = n.t === K.Money ? decToNumber(n.v) : n.t === K.Integer ? I64.toNumber(n.v) : n.v;
|
|
206
|
+
return k === K.Decimal ? decimal(x) : percent(x);
|
|
207
|
+
}
|
|
208
|
+
case K.Money: {
|
|
209
|
+
const n = toNumberCell(v);
|
|
210
|
+
const d = n === null ? null : toDec(n);
|
|
211
|
+
if (d === null)
|
|
212
|
+
return bad();
|
|
213
|
+
return money(d, n.t === K.Money ? n.cur : '');
|
|
214
|
+
}
|
|
215
|
+
case K.Char:
|
|
216
|
+
if (v.t === K.Char)
|
|
217
|
+
return v;
|
|
218
|
+
if (v.t === K.Integer)
|
|
219
|
+
return char(num(v));
|
|
220
|
+
if (isAnyString(v.t) && v.s.data.length > v.i)
|
|
221
|
+
return char(v.s.data[v.i]);
|
|
222
|
+
return bad();
|
|
223
|
+
case K.Logic: return logic(truthy(v));
|
|
224
|
+
case K.None: return NONE;
|
|
225
|
+
case K.Block:
|
|
226
|
+
case K.Paren:
|
|
227
|
+
case K.Path:
|
|
228
|
+
case K.SetPath:
|
|
229
|
+
case K.GetPath:
|
|
230
|
+
case K.LitPath: {
|
|
231
|
+
if (isAnyBlock(v.t))
|
|
232
|
+
return { t: k, s: { data: v.s.data.slice(v.i) }, i: 0 };
|
|
233
|
+
if (isAnyString(v.t)) {
|
|
234
|
+
try {
|
|
235
|
+
const b = load(text(v));
|
|
236
|
+
return { t: k, s: b.s, i: 0 };
|
|
237
|
+
}
|
|
238
|
+
catch (e) {
|
|
239
|
+
if (e instanceof LoadError)
|
|
240
|
+
return e.message;
|
|
241
|
+
throw e;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (v.t === K.Map)
|
|
245
|
+
return { t: k, s: { data: v.s.data.slice() }, i: 0 };
|
|
246
|
+
if (v.t === K.Object || v.t === K.Error) {
|
|
247
|
+
const data = [];
|
|
248
|
+
v.ctx.keys.forEach((key, i) => data.push({ t: K.SetWord, sym: key, bind: null }, v.ctx.vals[i]));
|
|
249
|
+
return { t: k, s: { data }, i: 0 };
|
|
250
|
+
}
|
|
251
|
+
return { t: k, s: { data: [v] }, i: 0 };
|
|
252
|
+
}
|
|
253
|
+
case K.Map: {
|
|
254
|
+
if (v.t === K.Map)
|
|
255
|
+
return { t: K.Map, s: { data: v.s.data.slice() } };
|
|
256
|
+
if (v.t === K.Block) {
|
|
257
|
+
const items = v.s.data.slice(v.i);
|
|
258
|
+
if (items.length % 2 !== 0)
|
|
259
|
+
return 'a map needs key value pairs';
|
|
260
|
+
return { t: K.Map, s: { data: items.map((c, i) => (i % 2 === 0 && isAnyWord(c.t) ? { t: K.Word, sym: c.sym, bind: null } : c)) } };
|
|
261
|
+
}
|
|
262
|
+
if (v.t === K.Object) {
|
|
263
|
+
const data = [];
|
|
264
|
+
v.ctx.keys.forEach((key, i) => data.push({ t: K.Word, sym: key, bind: null }, v.ctx.vals[i]));
|
|
265
|
+
return { t: K.Map, s: { data } };
|
|
266
|
+
}
|
|
267
|
+
return bad();
|
|
268
|
+
}
|
|
269
|
+
case K.Pair: {
|
|
270
|
+
if (v.t === K.Pair)
|
|
271
|
+
return v;
|
|
272
|
+
if (v.t === K.Integer || v.t === K.Decimal) {
|
|
273
|
+
const x = v.t === K.Integer ? num(v) : v.v;
|
|
274
|
+
return pair(x, x);
|
|
275
|
+
}
|
|
276
|
+
if (v.t === K.Block) {
|
|
277
|
+
const [a, b] = v.s.data.slice(v.i);
|
|
278
|
+
const n = (c) => (c?.t === K.Integer ? num(c) : c?.t === K.Decimal ? c.v : null);
|
|
279
|
+
const x = n(a);
|
|
280
|
+
const y = n(b);
|
|
281
|
+
return x === null || y === null ? bad() : pair(x, y);
|
|
282
|
+
}
|
|
283
|
+
return isAnyString(v.t) ? loadOne(text(v), [K.Pair]) ?? bad() : bad();
|
|
284
|
+
}
|
|
285
|
+
case K.Tuple:
|
|
286
|
+
if (v.t === K.Tuple)
|
|
287
|
+
return v;
|
|
288
|
+
if (v.t === K.Block && v.s.data.slice(v.i).every(c => c.t === K.Integer))
|
|
289
|
+
return tuple(v.s.data.slice(v.i).map(num));
|
|
290
|
+
return isAnyString(v.t) ? loadOne(text(v), [K.Tuple]) ?? bad() : bad();
|
|
291
|
+
case K.Date:
|
|
292
|
+
case K.Time:
|
|
293
|
+
if (v.t === k)
|
|
294
|
+
return v;
|
|
295
|
+
if (k === K.Time && (v.t === K.Integer || v.t === K.Decimal))
|
|
296
|
+
return { t: K.Time, v: Math.round((v.t === K.Integer ? num(v) : v.v) * 1e9) };
|
|
297
|
+
return isAnyString(v.t) ? loadOne(text(v), [k]) ?? bad() : bad();
|
|
298
|
+
case K.Datatype:
|
|
299
|
+
if (v.t === K.Datatype)
|
|
300
|
+
return v;
|
|
301
|
+
if (isAnyWord(v.t)) {
|
|
302
|
+
const name = spell(canon(v.sym));
|
|
303
|
+
const found = kindByName(name);
|
|
304
|
+
return found === undefined ? bad() : datatype(found);
|
|
305
|
+
}
|
|
306
|
+
return bad();
|
|
307
|
+
case K.Typeset: {
|
|
308
|
+
if (v.t === K.Typeset)
|
|
309
|
+
return v;
|
|
310
|
+
if (v.t === K.Datatype)
|
|
311
|
+
return typeset(kindBit(v.k));
|
|
312
|
+
if (v.t !== K.Block)
|
|
313
|
+
return bad();
|
|
314
|
+
const bits = typesOf(v);
|
|
315
|
+
return typeof bits === 'string' ? bits : typeset(bits);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return bad();
|
|
319
|
+
}
|
|
320
|
+
native('to', '["Converts a value to another type." type [datatype! any-value!] value [any-type!]]', (_t, f) => {
|
|
321
|
+
const type = f.args[0];
|
|
322
|
+
const k = type.t === K.Datatype ? type.k : type.t;
|
|
323
|
+
const r = toKind(k, f.args[1]);
|
|
324
|
+
return typeof r === 'string' ? fail('script', 'bad-to', r) : value(r);
|
|
325
|
+
});
|
|
326
|
+
/** Object from a block without evaluating it; logic and none words become values. */
|
|
327
|
+
native('construct', '["Makes an object from a block without evaluating its values." block [block!]]', (_t, f) => {
|
|
328
|
+
const b = f.args[0];
|
|
329
|
+
const keys = [];
|
|
330
|
+
const vals = [];
|
|
331
|
+
const words = { none: NONE, true: TRUE, false: FALSE, on: TRUE, off: FALSE, yes: TRUE, no: FALSE };
|
|
332
|
+
const d = b.s.data;
|
|
333
|
+
for (let i = b.i; i < d.length; i++) {
|
|
334
|
+
const c = d[i];
|
|
335
|
+
if (c.t !== K.SetWord)
|
|
336
|
+
continue;
|
|
337
|
+
let j = i + 1;
|
|
338
|
+
while (j < d.length && d[j].t === K.SetWord)
|
|
339
|
+
j++;
|
|
340
|
+
let v = d[j] ?? NONE;
|
|
341
|
+
if (v.t === K.Word)
|
|
342
|
+
v = words[spell(canon(v.sym))] ?? v;
|
|
343
|
+
else if (v.t === K.LitWord)
|
|
344
|
+
v = { t: K.Word, sym: v.sym, bind: null };
|
|
345
|
+
for (let k = i; k < j; k++) {
|
|
346
|
+
keys.push(d[k].sym);
|
|
347
|
+
vals.push(v);
|
|
348
|
+
}
|
|
349
|
+
i = j;
|
|
350
|
+
}
|
|
351
|
+
return value({ t: K.Object, ctx: { keys, vals, parent: null } });
|
|
352
|
+
});
|
|
353
|
+
native('compose', '["Evaluates the parens in a block and puts their values in a copy of it." value [any-value!] /deep /only]', (_t, f, v) => {
|
|
354
|
+
const x = f.args[0];
|
|
355
|
+
if (x.t !== K.Block)
|
|
356
|
+
return value(x);
|
|
357
|
+
const deep = used(f.args[1]);
|
|
358
|
+
const only = used(f.args[2]);
|
|
359
|
+
if (f.phase === 0) {
|
|
360
|
+
f.phase = 1;
|
|
361
|
+
f.data = [{ src: x, i: x.i, out: [], nl: [] }];
|
|
362
|
+
}
|
|
363
|
+
const levels = f.data;
|
|
364
|
+
if (v !== null) {
|
|
365
|
+
const lvl = levels[levels.length - 1];
|
|
366
|
+
const wasNl = lvl.src.s.nl?.[lvl.i - 1] ?? false;
|
|
367
|
+
if (v.t === K.Block && !only) {
|
|
368
|
+
v.s.data.slice(v.i).forEach((c, k) => {
|
|
369
|
+
lvl.out.push(c);
|
|
370
|
+
lvl.nl.push(k === 0 ? wasNl : v.s.nl?.[v.i + k] ?? false);
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
else if (v.t !== K.Unset) {
|
|
374
|
+
lvl.out.push(v);
|
|
375
|
+
lvl.nl.push(wasNl);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
for (;;) {
|
|
379
|
+
const lvl = levels[levels.length - 1];
|
|
380
|
+
const d = lvl.src.s.data;
|
|
381
|
+
if (lvl.i >= d.length) {
|
|
382
|
+
levels.pop();
|
|
383
|
+
const built = { t: lvl.src.t, s: { data: lvl.out, nl: lvl.nl }, i: 0 };
|
|
384
|
+
if (levels.length === 0)
|
|
385
|
+
return value(built);
|
|
386
|
+
const parent = levels[levels.length - 1];
|
|
387
|
+
parent.out.push(built);
|
|
388
|
+
parent.nl.push(parent.src.s.nl?.[parent.i - 1] ?? false);
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const c = d[lvl.i++];
|
|
392
|
+
if (c.t === K.Paren)
|
|
393
|
+
return { k: 'do', s: c.s, i: c.i };
|
|
394
|
+
if (deep && isAnyBlock(c.t)) {
|
|
395
|
+
levels.push({ src: c, i: c.i, out: [], nl: [] });
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
lvl.out.push(c);
|
|
399
|
+
lvl.nl.push(lvl.src.s.nl?.[lvl.i - 1] ?? false);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
// --- collect-words ---------------------------------------------------------------
|
|
403
|
+
native('collect-words', '["The words used in a block, each once." block [block!] /deep /set /ignore words [block!]]', (_t, f) => {
|
|
404
|
+
const deep = used(f.args[1]);
|
|
405
|
+
const setOnly = used(f.args[2]);
|
|
406
|
+
const ignore = used(f.args[3]) ? f.args[4].s.data.filter(c => isAnyWord(c.t)).map(c => canon(c.sym)) : [];
|
|
407
|
+
const seen = new Set(ignore);
|
|
408
|
+
const out = [];
|
|
409
|
+
const stack = [];
|
|
410
|
+
const walk = (s, from) => {
|
|
411
|
+
if (stack.includes(s))
|
|
412
|
+
return;
|
|
413
|
+
stack.push(s);
|
|
414
|
+
for (let i = from; i < s.data.length; i++) {
|
|
415
|
+
const c = s.data[i];
|
|
416
|
+
if ((setOnly ? c.t === K.SetWord : c.t >= K.Word && c.t <= K.LitWord) && !seen.has(canon(c.sym))) {
|
|
417
|
+
seen.add(canon(c.sym));
|
|
418
|
+
out.push({ t: K.Word, sym: c.sym, bind: null });
|
|
419
|
+
}
|
|
420
|
+
else if (deep && isAnyBlock(c.t)) {
|
|
421
|
+
walk(c.s, c.i);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
stack.pop();
|
|
425
|
+
};
|
|
426
|
+
const b = f.args[0];
|
|
427
|
+
walk(b.s, b.i);
|
|
428
|
+
return value({ t: K.Block, s: { data: out }, i: 0 });
|
|
429
|
+
});
|
|
430
|
+
// --- iteration over a series word ----------------------------------------------------
|
|
431
|
+
/** forall and forskip: the word walks its series; it is restored at the end. */
|
|
432
|
+
function walkWord(stepSize, bodyAt) {
|
|
433
|
+
return (_t, f, v) => {
|
|
434
|
+
const w = f.args[0];
|
|
435
|
+
const n = stepSize(f.args);
|
|
436
|
+
if (f.phase === 0) {
|
|
437
|
+
const s = getWord(w);
|
|
438
|
+
if (s === null || !(isAnyBlock(s.t) || isAnyString(s.t)))
|
|
439
|
+
return fail('script', 'bad-walk', `${spell(w.sym)} must refer to a series`);
|
|
440
|
+
f.phase = 1;
|
|
441
|
+
f.trap = (1 << T.Break) | (1 << T.Continue);
|
|
442
|
+
f.data = { start: s, cur: s, out: NONE, first: true };
|
|
443
|
+
}
|
|
444
|
+
const st = f.data;
|
|
445
|
+
const th = f.thrown;
|
|
446
|
+
f.thrown = null;
|
|
447
|
+
const finish = (r) => {
|
|
448
|
+
setWord(w, st.start);
|
|
449
|
+
return value(r);
|
|
450
|
+
};
|
|
451
|
+
if (th !== null && th.kind === T.Break)
|
|
452
|
+
return finish(th.v);
|
|
453
|
+
if (v !== null)
|
|
454
|
+
st.out = v;
|
|
455
|
+
if (!st.first)
|
|
456
|
+
st.cur = at(st.cur, st.cur.i + n);
|
|
457
|
+
st.first = false;
|
|
458
|
+
if (st.cur.i >= st.cur.s.data.length)
|
|
459
|
+
return finish(st.out);
|
|
460
|
+
setWord(w, st.cur);
|
|
461
|
+
return doBlock(f.args[bodyAt]);
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
native('forall', "[\"Evaluates a block for each position of the series a word refers to.\" 'word [word!] body [block!]]", walkWord(() => 1, 1));
|
|
465
|
+
native('forskip', "[\"Evaluates a block at every so many positions of the series a word refers to.\" 'word [word!] size [integer!] body [block!]]", walkWord(a => Math.max(1, num(a[1])), 2));
|
|
466
|
+
// --- apply -------------------------------------------------------------------------
|
|
467
|
+
native('apply', '["Calls a function with the arguments in a block." func [any-function!] block [block!] /only]', (_t, f, v) => {
|
|
468
|
+
const fn = f.args[0];
|
|
469
|
+
const b = f.args[1];
|
|
470
|
+
if (f.phase === 2)
|
|
471
|
+
return value(v);
|
|
472
|
+
if (f.phase === 0) {
|
|
473
|
+
if (used(f.args[2])) {
|
|
474
|
+
f.phase = 2;
|
|
475
|
+
return { k: 'call', fn, args: b.s.data.slice(b.i), full: true };
|
|
476
|
+
}
|
|
477
|
+
f.phase = 1;
|
|
478
|
+
f.data = { feed: { s: b.s, i: b.i }, out: [] };
|
|
479
|
+
}
|
|
480
|
+
const st = f.data;
|
|
481
|
+
if (v !== null)
|
|
482
|
+
st.out.push(v);
|
|
483
|
+
if (st.feed.i < st.feed.s.data.length)
|
|
484
|
+
return { k: 'expr', feed: st.feed };
|
|
485
|
+
f.phase = 2;
|
|
486
|
+
return { k: 'call', fn, args: st.out, full: true };
|
|
487
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Cell } from '../values.ts';
|
|
2
|
+
/** Parses XML text into a block of nodes; returns an error message when it is not well formed. */
|
|
3
|
+
export declare function parseXml(src: string): Cell[] | string;
|
|
4
|
+
/** The XML text of a block of nodes, or an error message when a node is not name, content and attributes. */
|
|
5
|
+
export declare function writeXml(nodes: Cell[]): {
|
|
6
|
+
xml: string;
|
|
7
|
+
} | {
|
|
8
|
+
error: string;
|
|
9
|
+
};
|