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,434 @@
|
|
|
1
|
+
// MOLD and FORM: cells to text.
|
|
2
|
+
//
|
|
3
|
+
// mold produces source text that load reads back as an equivalent value; it
|
|
4
|
+
// is the inverse of load and the main test of the lexer. With `all`, values
|
|
5
|
+
// that have no literal form (none, logic, datatypes, series at an index...)
|
|
6
|
+
// use construction syntax #[...] so the round trip is exact.
|
|
7
|
+
//
|
|
8
|
+
// form produces human-readable text: strings without quotes, blocks without
|
|
9
|
+
// brackets, an error as its report. It is what `print` shows.
|
|
10
|
+
import { K, kindName } from "./kinds.js";
|
|
11
|
+
import { spell } from "./symbols.js";
|
|
12
|
+
import * as I64 from "./int64.js";
|
|
13
|
+
import { formatDec } from "./dec.js";
|
|
14
|
+
import { encodeChar } from "./escapes.js";
|
|
15
|
+
import { errorField, errorMessage } from "./errors.js";
|
|
16
|
+
import { fromCodePoints, unpackPos, } from "./values.js";
|
|
17
|
+
export function mold(c, opts = {}) {
|
|
18
|
+
const m = new Molder(opts.all ?? false);
|
|
19
|
+
if (opts.only && (c.t === K.Block || c.t === K.Paren)) {
|
|
20
|
+
m.contents(c.s, c.i, -1);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
m.value(c, 0);
|
|
24
|
+
}
|
|
25
|
+
return m.out;
|
|
26
|
+
}
|
|
27
|
+
export function form(c) {
|
|
28
|
+
switch (c.t) {
|
|
29
|
+
case K.Unset:
|
|
30
|
+
return '';
|
|
31
|
+
case K.String:
|
|
32
|
+
case K.File:
|
|
33
|
+
case K.Email:
|
|
34
|
+
case K.Url:
|
|
35
|
+
return fromCodePoints(c.s.data, c.i);
|
|
36
|
+
case K.Char:
|
|
37
|
+
return String.fromCodePoint(c.v);
|
|
38
|
+
case K.Word:
|
|
39
|
+
case K.SetWord:
|
|
40
|
+
case K.GetWord:
|
|
41
|
+
case K.LitWord:
|
|
42
|
+
case K.Refinement:
|
|
43
|
+
case K.Issue:
|
|
44
|
+
return spell(c.sym);
|
|
45
|
+
case K.Block:
|
|
46
|
+
case K.Paren:
|
|
47
|
+
return c.s.data.slice(c.i).map(form).join(' ');
|
|
48
|
+
case K.Object:
|
|
49
|
+
return c.ctx.keys.map((k, i) => `${spell(k)}: ${form(c.ctx.vals[i])}`).join('\n');
|
|
50
|
+
case K.Error:
|
|
51
|
+
return report(c);
|
|
52
|
+
default:
|
|
53
|
+
return mold(c);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// --- scalar formatting ---------------------------------------------------------
|
|
57
|
+
/** An error as the console shows it, as in R3 and Red: "** Math error: ...", then where, near and at. */
|
|
58
|
+
function report(e) {
|
|
59
|
+
const lines = [`** ${errorMessage(e)}`];
|
|
60
|
+
const where = errorField(e, 'where');
|
|
61
|
+
const near = errorField(e, 'near');
|
|
62
|
+
const at = errorField(e, 'at');
|
|
63
|
+
if (where.t !== K.None)
|
|
64
|
+
lines.push(`** Where: ${form(where)}`);
|
|
65
|
+
if (near.t !== K.None)
|
|
66
|
+
lines.push(`** Near: ${mold(near)}`);
|
|
67
|
+
if (at.t !== K.None)
|
|
68
|
+
lines.push(`** At: ${form(at)}`);
|
|
69
|
+
return lines.join('\n');
|
|
70
|
+
}
|
|
71
|
+
const MONTH_ABBR = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
72
|
+
/**
|
|
73
|
+
* A decimal as Trebol writes it. As in R3, mold shows 15 significant digits,
|
|
74
|
+
* so 0.1 + 0.2 reads 0.3; `exact` (mold/all) gives the shortest text that
|
|
75
|
+
* loads back to the very same number.
|
|
76
|
+
*/
|
|
77
|
+
export function formatDecimal(n, exact = true) {
|
|
78
|
+
if (Number.isNaN(n))
|
|
79
|
+
return '1.#NaN';
|
|
80
|
+
if (!Number.isFinite(n))
|
|
81
|
+
return n > 0 ? '1.#INF' : '-1.#INF';
|
|
82
|
+
let s = String(exact ? n : Number(n.toPrecision(15)));
|
|
83
|
+
if (s.includes('e')) {
|
|
84
|
+
s = s.replace('e+', 'e');
|
|
85
|
+
const [mant, exp] = s.split('e');
|
|
86
|
+
s = (mant.includes('.') ? mant : mant + '.0') + 'e' + exp;
|
|
87
|
+
}
|
|
88
|
+
else if (!s.includes('.')) {
|
|
89
|
+
s += '.0';
|
|
90
|
+
}
|
|
91
|
+
return s;
|
|
92
|
+
}
|
|
93
|
+
function formatPercent(v, exact) {
|
|
94
|
+
// The shortest digits that load back to exactly v (0.15 * 100 is
|
|
95
|
+
// 15.000000000000002, but 0.15 should mold as 15%); without `exact`, at
|
|
96
|
+
// most 15 digits, as decimals.
|
|
97
|
+
if (!exact)
|
|
98
|
+
v = Number(v.toPrecision(15));
|
|
99
|
+
for (let p = 1; p <= 17; p++) {
|
|
100
|
+
const digits = String(Number((v * 100).toPrecision(p))).replace('e+', 'e');
|
|
101
|
+
const [mant, exp] = digits.split('e');
|
|
102
|
+
if (Number(`${mant}e${Number(exp ?? 0) - 2}`) === v)
|
|
103
|
+
return digits + '%';
|
|
104
|
+
}
|
|
105
|
+
return String(v * 100).replace('e+', 'e') + '%';
|
|
106
|
+
}
|
|
107
|
+
function formatPairPart(n) {
|
|
108
|
+
return Number.isInteger(n) ? String(n) : String(n).replace('e+', 'e');
|
|
109
|
+
}
|
|
110
|
+
/** h:mm[:ss[.fraction]], seconds only when not zero. Sign handled by caller. */
|
|
111
|
+
export function formatTime(ns) {
|
|
112
|
+
const totalSec = Math.floor(ns / 1e9);
|
|
113
|
+
const frac = ns - totalSec * 1e9;
|
|
114
|
+
const h = Math.floor(totalSec / 3600);
|
|
115
|
+
const m = Math.floor(totalSec / 60) % 60;
|
|
116
|
+
const s = totalSec % 60;
|
|
117
|
+
let out = `${h}:${String(m).padStart(2, '0')}`;
|
|
118
|
+
if (s !== 0 || frac !== 0) {
|
|
119
|
+
out += ':' + String(s).padStart(2, '0');
|
|
120
|
+
if (frac !== 0)
|
|
121
|
+
out += '.' + String(frac).padStart(9, '0').replace(/0+$/, '');
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
function formatZone(minutes) {
|
|
126
|
+
const sign = minutes < 0 ? '-' : '+';
|
|
127
|
+
const a = Math.abs(minutes);
|
|
128
|
+
return `${sign}${Math.floor(a / 60)}:${String(a % 60).padStart(2, '0')}`;
|
|
129
|
+
}
|
|
130
|
+
export function formatDate(v) {
|
|
131
|
+
let out = `${v.d}-${MONTH_ABBR[v.m - 1]}-${v.y}`;
|
|
132
|
+
if (v.time !== null) {
|
|
133
|
+
out += '/' + formatTime(v.time);
|
|
134
|
+
if (v.zone !== null)
|
|
135
|
+
out += formatZone(v.zone);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
// --- strings ---------------------------------------------------------------------
|
|
140
|
+
/** Quoted "..." for short single-line text, braces {...} otherwise. */
|
|
141
|
+
function moldString(cps, start) {
|
|
142
|
+
let useBraces = false;
|
|
143
|
+
let depth = 0;
|
|
144
|
+
let balanced = true;
|
|
145
|
+
for (let i = start; i < cps.length; i++) {
|
|
146
|
+
const cp = cps[i];
|
|
147
|
+
if (cp === 10 || cp === 0x22)
|
|
148
|
+
useBraces = true;
|
|
149
|
+
if (cp === 0x7b)
|
|
150
|
+
depth++;
|
|
151
|
+
if (cp === 0x7d && --depth < 0)
|
|
152
|
+
balanced = false;
|
|
153
|
+
}
|
|
154
|
+
if (depth !== 0)
|
|
155
|
+
balanced = false;
|
|
156
|
+
let out = useBraces ? '{' : '"';
|
|
157
|
+
for (let i = start; i < cps.length; i++) {
|
|
158
|
+
const cp = cps[i];
|
|
159
|
+
if (useBraces && !balanced && (cp === 0x7b || cp === 0x7d)) {
|
|
160
|
+
out += '^' + String.fromCodePoint(cp);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
out += encodeChar(cp, useBraces ? '}' : '"', useBraces);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return out + (useBraces ? '}' : '"');
|
|
167
|
+
}
|
|
168
|
+
const FILE_SAFE = /[A-Za-z0-9._~/:+\-!*',=@&$?#\u0080-\u{10FFFF}]/u;
|
|
169
|
+
function moldFile(cps, start) {
|
|
170
|
+
if (cps.length === start)
|
|
171
|
+
return '%""';
|
|
172
|
+
let out = '%';
|
|
173
|
+
for (let i = start; i < cps.length; i++) {
|
|
174
|
+
const ch = String.fromCodePoint(cps[i]);
|
|
175
|
+
out += FILE_SAFE.test(ch)
|
|
176
|
+
? ch
|
|
177
|
+
: [...new TextEncoder().encode(ch)].map(b => '%' + b.toString(16).toUpperCase().padStart(2, '0')).join('');
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
function hex(bytes, start = 0) {
|
|
182
|
+
let out = '';
|
|
183
|
+
for (let i = start; i < bytes.length; i++)
|
|
184
|
+
out += bytes[i].toString(16).toUpperCase().padStart(2, '0');
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
// --- the molder -------------------------------------------------------------------
|
|
188
|
+
const INDENT = ' ';
|
|
189
|
+
class Molder {
|
|
190
|
+
out = '';
|
|
191
|
+
all;
|
|
192
|
+
active = new Set();
|
|
193
|
+
constructor(all) {
|
|
194
|
+
this.all = all;
|
|
195
|
+
}
|
|
196
|
+
value(c, depth) {
|
|
197
|
+
const all = this.all;
|
|
198
|
+
switch (c.t) {
|
|
199
|
+
case K.Unset:
|
|
200
|
+
this.out += all ? '#[unset]' : '';
|
|
201
|
+
return;
|
|
202
|
+
case K.None:
|
|
203
|
+
this.out += all ? '#[none]' : 'none';
|
|
204
|
+
return;
|
|
205
|
+
case K.Logic:
|
|
206
|
+
this.out += all ? `#[${c.v}]` : String(c.v);
|
|
207
|
+
return;
|
|
208
|
+
case K.Integer:
|
|
209
|
+
this.out += I64.toString(c.v);
|
|
210
|
+
return;
|
|
211
|
+
case K.Decimal:
|
|
212
|
+
this.out += formatDecimal(c.v, all);
|
|
213
|
+
return;
|
|
214
|
+
case K.Percent:
|
|
215
|
+
this.out += formatPercent(c.v, all);
|
|
216
|
+
return;
|
|
217
|
+
case K.Money: {
|
|
218
|
+
const neg = c.v.d < 0n;
|
|
219
|
+
const abs = neg ? { d: -c.v.d, e: c.v.e } : c.v;
|
|
220
|
+
this.out += (neg ? '-' : '') + c.cur + '$' + formatDec(abs, 2);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
case K.Char:
|
|
224
|
+
this.out += '#"' + encodeChar(c.v, '"') + '"';
|
|
225
|
+
return;
|
|
226
|
+
case K.Pair:
|
|
227
|
+
this.out += formatPairPart(c.x) + 'x' + formatPairPart(c.y);
|
|
228
|
+
return;
|
|
229
|
+
case K.Tuple:
|
|
230
|
+
this.out += c.v.join('.');
|
|
231
|
+
return;
|
|
232
|
+
case K.Date:
|
|
233
|
+
this.out += formatDate(c.v);
|
|
234
|
+
return;
|
|
235
|
+
case K.Time:
|
|
236
|
+
this.out += (c.v < 0 ? '-' : '') + formatTime(Math.abs(c.v));
|
|
237
|
+
return;
|
|
238
|
+
case K.String:
|
|
239
|
+
this.indexed(c, () => moldString(c.s.data, all ? 0 : c.i));
|
|
240
|
+
return;
|
|
241
|
+
case K.File:
|
|
242
|
+
this.indexed(c, () => moldFile(c.s.data, all ? 0 : c.i));
|
|
243
|
+
return;
|
|
244
|
+
case K.Email:
|
|
245
|
+
case K.Url:
|
|
246
|
+
this.indexed(c, () => fromCodePoints(c.s.data, all ? 0 : c.i));
|
|
247
|
+
return;
|
|
248
|
+
case K.Tag:
|
|
249
|
+
this.indexed(c, () => '<' + fromCodePoints(c.s.data, all ? 0 : c.i) + '>');
|
|
250
|
+
return;
|
|
251
|
+
case K.Binary:
|
|
252
|
+
this.indexed(c, () => '#{' + hex(c.s.data, all ? 0 : c.i) + '}');
|
|
253
|
+
return;
|
|
254
|
+
case K.Bitset: {
|
|
255
|
+
const body = (c.neg ? 'not ' : '') + '#{' + hex(c.s.data) + '}';
|
|
256
|
+
this.out += all ? `#[bitset! ${body}]` : `make bitset! ${c.neg ? '[not bits ' : ''}#{${hex(c.s.data)}}${c.neg ? ']' : ''}`;
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
case K.Word:
|
|
260
|
+
this.out += spell(c.sym);
|
|
261
|
+
return;
|
|
262
|
+
case K.SetWord:
|
|
263
|
+
this.out += spell(c.sym) + ':';
|
|
264
|
+
return;
|
|
265
|
+
case K.GetWord:
|
|
266
|
+
this.out += ':' + spell(c.sym);
|
|
267
|
+
return;
|
|
268
|
+
case K.LitWord:
|
|
269
|
+
this.out += "'" + spell(c.sym);
|
|
270
|
+
return;
|
|
271
|
+
case K.Refinement:
|
|
272
|
+
this.out += '/' + spell(c.sym);
|
|
273
|
+
return;
|
|
274
|
+
case K.Issue:
|
|
275
|
+
this.out += '#' + spell(c.sym);
|
|
276
|
+
return;
|
|
277
|
+
case K.Block:
|
|
278
|
+
this.blockLike(c, '[', ']', depth);
|
|
279
|
+
return;
|
|
280
|
+
case K.Paren:
|
|
281
|
+
this.blockLike(c, '(', ')', depth);
|
|
282
|
+
return;
|
|
283
|
+
case K.Path:
|
|
284
|
+
case K.SetPath:
|
|
285
|
+
case K.GetPath:
|
|
286
|
+
case K.LitPath:
|
|
287
|
+
this.path(c, depth);
|
|
288
|
+
return;
|
|
289
|
+
case K.Map:
|
|
290
|
+
this.map(c.s, depth);
|
|
291
|
+
return;
|
|
292
|
+
case K.Object:
|
|
293
|
+
this.object(c.ctx, depth, all ? null : 'make object!');
|
|
294
|
+
return;
|
|
295
|
+
case K.Error:
|
|
296
|
+
this.object(c.ctx, depth, 'make error!');
|
|
297
|
+
return;
|
|
298
|
+
case K.Datatype:
|
|
299
|
+
this.out += all ? `#[datatype! ${kindName(c.k)}]` : kindName(c.k);
|
|
300
|
+
return;
|
|
301
|
+
case K.Typeset: {
|
|
302
|
+
const names = [];
|
|
303
|
+
for (let k = 0; k < 64; k++)
|
|
304
|
+
if ((c.bits >> BigInt(k)) & 1n)
|
|
305
|
+
names.push(kindName(k));
|
|
306
|
+
this.out += all ? `#[typeset! [${names.join(' ')}]]` : `make typeset! [${names.join(' ')}]`;
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
case K.Function: {
|
|
310
|
+
// as Red: the source that makes it. No construction syntax loads a
|
|
311
|
+
// function back, so mold/all keeps the opaque form.
|
|
312
|
+
if (all)
|
|
313
|
+
break;
|
|
314
|
+
const def = c.p;
|
|
315
|
+
this.out += 'func ';
|
|
316
|
+
this.blockLike(def.specBlock, '[', ']', depth);
|
|
317
|
+
this.out += ' ';
|
|
318
|
+
this.blockLike({ t: K.Block, s: def.body, i: 0 }, '[', ']', depth);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
this.out += `#[${kindName(c.t)}]`;
|
|
323
|
+
}
|
|
324
|
+
/** A series not at its head molds as #[kind! value index] with `all`. */
|
|
325
|
+
indexed(c, body) {
|
|
326
|
+
if (this.all && c.i > 0)
|
|
327
|
+
this.out += `#[${kindName(c.t)} ${body()} ${c.i + 1}]`;
|
|
328
|
+
else
|
|
329
|
+
this.out += body();
|
|
330
|
+
}
|
|
331
|
+
blockLike(c, open, close, depth) {
|
|
332
|
+
if (this.all && c.i > 0) {
|
|
333
|
+
this.out += `#[${kindName(c.t)} `;
|
|
334
|
+
this.blockLike({ ...c, i: 0 }, open, close, depth);
|
|
335
|
+
this.out += ` ${c.i + 1}]`;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (this.active.has(c.s)) {
|
|
339
|
+
this.out += open + '...' + close;
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
this.out += open;
|
|
343
|
+
const multiline = this.contents(c.s, c.i, depth);
|
|
344
|
+
if (multiline)
|
|
345
|
+
this.out += '\n' + INDENT.repeat(depth);
|
|
346
|
+
this.out += close;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Molds series elements from `start`, honoring newline markers.
|
|
350
|
+
* `depth` is the depth of the enclosing block (-1 for top-level contents).
|
|
351
|
+
* Returns true if any element was placed on a new line.
|
|
352
|
+
*/
|
|
353
|
+
contents(s, start, depth) {
|
|
354
|
+
this.active.add(s);
|
|
355
|
+
let multiline = false;
|
|
356
|
+
const indent = INDENT.repeat(depth + 1);
|
|
357
|
+
for (let j = start; j < s.data.length; j++) {
|
|
358
|
+
if (s.nl?.[j] && (j > start || depth >= 0)) {
|
|
359
|
+
this.out += '\n' + indent;
|
|
360
|
+
multiline = true;
|
|
361
|
+
}
|
|
362
|
+
else if (j > start) {
|
|
363
|
+
this.out += ' ';
|
|
364
|
+
}
|
|
365
|
+
this.value(s.data[j], depth + 1);
|
|
366
|
+
}
|
|
367
|
+
this.active.delete(s);
|
|
368
|
+
return multiline;
|
|
369
|
+
}
|
|
370
|
+
path(c, depth) {
|
|
371
|
+
if (c.t === K.GetPath)
|
|
372
|
+
this.out += ':';
|
|
373
|
+
if (c.t === K.LitPath)
|
|
374
|
+
this.out += "'";
|
|
375
|
+
c.s.data.slice(c.i).forEach((seg, j) => {
|
|
376
|
+
if (j > 0)
|
|
377
|
+
this.out += '/';
|
|
378
|
+
this.value(seg, depth);
|
|
379
|
+
});
|
|
380
|
+
if (c.t === K.SetPath)
|
|
381
|
+
this.out += ':';
|
|
382
|
+
}
|
|
383
|
+
map(s, depth) {
|
|
384
|
+
if (this.active.has(s)) {
|
|
385
|
+
this.out += '#(...)';
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
this.active.add(s);
|
|
389
|
+
this.out += '#(';
|
|
390
|
+
for (let j = 0; j < s.data.length; j += 2) {
|
|
391
|
+
if (j > 0)
|
|
392
|
+
this.out += ' ';
|
|
393
|
+
const key = s.data[j];
|
|
394
|
+
if (key.t === K.Word)
|
|
395
|
+
this.out += spell(key.sym) + ':';
|
|
396
|
+
else
|
|
397
|
+
this.value(key, depth + 1);
|
|
398
|
+
this.out += ' ';
|
|
399
|
+
this.value(s.data[j + 1], depth + 1);
|
|
400
|
+
}
|
|
401
|
+
this.out += ')';
|
|
402
|
+
this.active.delete(s);
|
|
403
|
+
}
|
|
404
|
+
/** With `prefix` null, molds as construction syntax #[object! [...]]. */
|
|
405
|
+
object(ctx, depth, prefix) {
|
|
406
|
+
if (this.active.has(ctx)) {
|
|
407
|
+
this.out += prefix === null ? '#[object! [...]]' : prefix + ' [...]';
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
this.active.add(ctx);
|
|
411
|
+
this.out += prefix === null ? '#[object! [' : prefix + ' [';
|
|
412
|
+
const indent = INDENT.repeat(depth + 1);
|
|
413
|
+
ctx.keys.forEach((k, i) => {
|
|
414
|
+
const v = ctx.vals[i];
|
|
415
|
+
this.out += '\n' + indent + spell(k) + ': ';
|
|
416
|
+
// `make object! [...]` evaluates its block, so a word or path must be quoted to stay itself
|
|
417
|
+
if (prefix !== null && (v.t === K.Word || v.t === K.Path))
|
|
418
|
+
this.out += "'";
|
|
419
|
+
this.value(v, depth + 1);
|
|
420
|
+
});
|
|
421
|
+
if (ctx.keys.length > 0)
|
|
422
|
+
this.out += '\n' + INDENT.repeat(depth);
|
|
423
|
+
this.out += prefix === null ? ']]' : ']';
|
|
424
|
+
this.active.delete(ctx);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/** "file:line:col" for an element of a loaded block, for error messages. */
|
|
428
|
+
export function sourceOf(s, index) {
|
|
429
|
+
const p = s.pos?.[index];
|
|
430
|
+
if (p === undefined || p < 0)
|
|
431
|
+
return null; // -1: inserted at run time
|
|
432
|
+
const { line, col } = unpackPos(p);
|
|
433
|
+
return `${s.file ? s.file + ':' : ''}${line}:${col}`;
|
|
434
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|