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.
Files changed (160) hide show
  1. package/README.md +18 -3
  2. package/dist/cli.js +13790 -11417
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/access.d.ts +10 -0
  12. package/dist/lib/core/access.js +210 -0
  13. package/dist/lib/core/base.d.ts +4 -0
  14. package/dist/lib/core/base.js +50 -0
  15. package/dist/lib/core/bitset.d.ts +9 -0
  16. package/dist/lib/core/bitset.js +75 -0
  17. package/dist/lib/core/boot.d.ts +18 -0
  18. package/dist/lib/core/boot.js +73 -0
  19. package/dist/lib/core/compare.d.ts +9 -0
  20. package/dist/lib/core/compare.js +156 -0
  21. package/dist/lib/core/ctx.d.ts +36 -0
  22. package/dist/lib/core/ctx.js +251 -0
  23. package/dist/lib/core/dec.d.ts +28 -0
  24. package/dist/lib/core/dec.js +158 -0
  25. package/dist/lib/core/errors.d.ts +15 -0
  26. package/dist/lib/core/errors.js +44 -0
  27. package/dist/lib/core/escapes.d.ts +14 -0
  28. package/dist/lib/core/escapes.js +80 -0
  29. package/dist/lib/core/eval.d.ts +220 -0
  30. package/dist/lib/core/eval.js +666 -0
  31. package/dist/lib/core/func.d.ts +68 -0
  32. package/dist/lib/core/func.js +129 -0
  33. package/dist/lib/core/hash.d.ts +5 -0
  34. package/dist/lib/core/hash.js +155 -0
  35. package/dist/lib/core/index.d.ts +18 -0
  36. package/dist/lib/core/index.js +20 -0
  37. package/dist/lib/core/int64.d.ts +22 -0
  38. package/dist/lib/core/int64.js +113 -0
  39. package/dist/lib/core/kinds.d.ts +67 -0
  40. package/dist/lib/core/kinds.js +134 -0
  41. package/dist/lib/core/lexer.d.ts +13 -0
  42. package/dist/lib/core/lexer.js +702 -0
  43. package/dist/lib/core/mezz.d.ts +1 -0
  44. package/dist/lib/core/mezz.js +360 -0
  45. package/dist/lib/core/mold.d.ts +20 -0
  46. package/dist/lib/core/mold.js +434 -0
  47. package/dist/lib/core/natives/control.d.ts +1 -0
  48. package/dist/lib/core/natives/control.js +374 -0
  49. package/dist/lib/core/natives/csv.d.ts +15 -0
  50. package/dist/lib/core/natives/csv.js +315 -0
  51. package/dist/lib/core/natives/data.d.ts +11 -0
  52. package/dist/lib/core/natives/data.js +367 -0
  53. package/dist/lib/core/natives/define.d.ts +17 -0
  54. package/dist/lib/core/natives/define.js +47 -0
  55. package/dist/lib/core/natives/encode.d.ts +3 -0
  56. package/dist/lib/core/natives/encode.js +187 -0
  57. package/dist/lib/core/natives/json.d.ts +5 -0
  58. package/dist/lib/core/natives/json.js +273 -0
  59. package/dist/lib/core/natives/math.d.ts +3 -0
  60. package/dist/lib/core/natives/math.js +500 -0
  61. package/dist/lib/core/natives/parse.d.ts +1 -0
  62. package/dist/lib/core/natives/parse.js +600 -0
  63. package/dist/lib/core/natives/series.d.ts +4 -0
  64. package/dist/lib/core/natives/series.js +519 -0
  65. package/dist/lib/core/natives/text.d.ts +1 -0
  66. package/dist/lib/core/natives/text.js +487 -0
  67. package/dist/lib/core/natives/xml.d.ts +9 -0
  68. package/dist/lib/core/natives/xml.js +242 -0
  69. package/dist/lib/core/series.d.ts +26 -0
  70. package/dist/lib/core/series.js +73 -0
  71. package/dist/lib/core/symbols.d.ts +9 -0
  72. package/dist/lib/core/symbols.js +44 -0
  73. package/dist/lib/core/values.d.ts +206 -0
  74. package/dist/lib/core/values.js +223 -0
  75. package/dist/lib/host/browser.d.ts +4 -0
  76. package/dist/lib/host/browser.js +4974 -0
  77. package/dist/lib/host/chart.d.ts +26 -0
  78. package/dist/lib/host/chart.js +158 -0
  79. package/dist/lib/host/compress.d.ts +1 -0
  80. package/dist/lib/host/compress.js +37 -0
  81. package/dist/lib/host/devlog.d.ts +11 -0
  82. package/dist/lib/host/devlog.js +13 -0
  83. package/dist/lib/host/devtools.d.ts +23 -0
  84. package/dist/lib/host/devtools.js +626 -0
  85. package/dist/lib/host/draw.d.ts +31 -0
  86. package/dist/lib/host/draw.js +348 -0
  87. package/dist/lib/host/fonts/OFL.txt +93 -0
  88. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  89. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  90. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  91. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  92. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  93. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  94. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  95. package/dist/lib/host/fonts.d.ts +11 -0
  96. package/dist/lib/host/fonts.js +29 -0
  97. package/dist/lib/host/icons.d.ts +3 -0
  98. package/dist/lib/host/icons.js +97 -0
  99. package/dist/lib/host/index.d.ts +40 -0
  100. package/dist/lib/host/index.js +192 -0
  101. package/dist/lib/host/js.d.ts +5 -0
  102. package/dist/lib/host/js.js +130 -0
  103. package/dist/lib/host/locale.d.ts +40 -0
  104. package/dist/lib/host/locale.js +241 -0
  105. package/dist/lib/host/markdown.d.ts +30 -0
  106. package/dist/lib/host/markdown.js +161 -0
  107. package/dist/lib/host/modules.d.ts +14 -0
  108. package/dist/lib/host/modules.js +300 -0
  109. package/dist/lib/host/natives.d.ts +6 -0
  110. package/dist/lib/host/natives.js +98 -0
  111. package/dist/lib/host/net.d.ts +12 -0
  112. package/dist/lib/host/net.js +549 -0
  113. package/dist/lib/host/orbit.d.ts +121 -0
  114. package/dist/lib/host/orbit.js +374 -0
  115. package/dist/lib/host/page.d.ts +9 -0
  116. package/dist/lib/host/page.js +55 -0
  117. package/dist/lib/host/rows.d.ts +29 -0
  118. package/dist/lib/host/rows.js +109 -0
  119. package/dist/lib/host/scheduler.d.ts +51 -0
  120. package/dist/lib/host/scheduler.js +197 -0
  121. package/dist/lib/host/theme.d.ts +58 -0
  122. package/dist/lib/host/theme.js +1667 -0
  123. package/dist/lib/ui/i18n.trb +117 -0
  124. package/dist/lib/ui/test.trb +565 -0
  125. package/dist/lib/ui/ui.trb +1678 -0
  126. package/dist/trebol.js +1427 -138
  127. package/editors/vscode/README.md +15 -0
  128. package/editors/vscode/language-configuration.json +28 -0
  129. package/editors/vscode/package.json +42 -0
  130. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  131. package/package.json +23 -7
  132. package/src/core/boot.ts +1 -1
  133. package/src/core/func.ts +4 -0
  134. package/src/core/natives/control.ts +1 -1
  135. package/src/core/natives/series.ts +9 -1
  136. package/src/core/natives/text.ts +2 -0
  137. package/src/host/browser.ts +1235 -83
  138. package/src/host/devlog.ts +16 -0
  139. package/src/host/devtools.ts +641 -0
  140. package/src/host/fonts/OFL.txt +93 -0
  141. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  142. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  143. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  144. package/src/host/fonts/inter-greek.woff2 +0 -0
  145. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  146. package/src/host/fonts/inter-latin.woff2 +0 -0
  147. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  148. package/src/host/fonts.ts +39 -0
  149. package/src/host/index.ts +5 -3
  150. package/src/host/js.ts +127 -0
  151. package/src/host/markdown.ts +9 -0
  152. package/src/host/modules.ts +41 -2
  153. package/src/host/net.ts +9 -1
  154. package/src/host/orbit.ts +432 -0
  155. package/src/host/rows.ts +30 -0
  156. package/src/host/scheduler.ts +62 -36
  157. package/src/host/theme.ts +270 -6
  158. package/src/ui/i18n.trb +10 -0
  159. package/src/ui/test.trb +565 -0
  160. package/src/ui/ui.trb +564 -90
@@ -0,0 +1,374 @@
1
+ // Control natives: evaluation, conditionals, loops, exits and errors.
2
+ //
3
+ // Each one is a small state machine over `f.phase`: phase 0 is the first
4
+ // call with the arguments; later phases receive the value of the block or
5
+ // expression the native asked the evaluator for.
6
+ import { K, kindName } from "../kinds.js";
7
+ import * as I64 from "../int64.js";
8
+ import { NONE, UNSET, TRUE, block, context } from "../values.js";
9
+ import { bindSeries, copyDeep, setWord } from "../ctx.js";
10
+ import { at } from "../series.js";
11
+ import { T } from "../eval.js";
12
+ import { isEqual } from "../compare.js";
13
+ import { native, value, fail, truthy, doBlock, hostCall } from "./define.js";
14
+ import { loadUser } from "./data.js";
15
+ import { makeError } from "../errors.js";
16
+ import { spell } from "../symbols.js";
17
+ import { form } from "../mold.js";
18
+ const LOOP_TRAP = (1 << T.Break) | (1 << T.Continue);
19
+ /** Takes the pending throw of a trapping native. */
20
+ function caught(f) {
21
+ const th = f.thrown;
22
+ f.thrown = null;
23
+ return th;
24
+ }
25
+ // --- evaluation ------------------------------------------------------------------
26
+ native('do', '["Evaluates a block, source text or a script file; gives the last value." value [any-type!] /next "Evaluates only the first expression of a block" position [word!] "Set to the block after it"]', (t, f, v) => {
27
+ if (f.phase === 1)
28
+ return value(v);
29
+ let x = f.args[0];
30
+ if (f.args[1] === TRUE) {
31
+ // as R3: source text is loaded first; any other value is itself, with nothing after it
32
+ if (x.t === K.String && f.phase === 0) {
33
+ const loaded = loadUser(t.rt, x);
34
+ if (loaded.k !== 'value')
35
+ return loaded;
36
+ x = f.args[0] = loaded.v;
37
+ }
38
+ if (x.t !== K.Block && x.t !== K.Paren) {
39
+ setWord(f.args[2], NONE);
40
+ return x.t === K.Error ? { k: 'throw', e: { kind: T.Error, v: x, name: null } } : value(x);
41
+ }
42
+ const b = x;
43
+ if (f.phase === 2) {
44
+ const feed = f.data;
45
+ setWord(f.args[2], at(b, feed.i));
46
+ return value(v);
47
+ }
48
+ if (b.i >= b.s.data.length) {
49
+ setWord(f.args[2], b);
50
+ return value(UNSET);
51
+ }
52
+ f.phase = 2;
53
+ f.data = { s: b.s, i: b.i };
54
+ return { k: 'expr', feed: f.data };
55
+ }
56
+ switch (x.t) {
57
+ case K.Block:
58
+ case K.Paren:
59
+ f.phase = 1;
60
+ return { k: 'do', s: x.s, i: x.i };
61
+ case K.String: {
62
+ const b = loadUser(t.rt, x);
63
+ if (b.k !== 'value')
64
+ return b;
65
+ f.phase = 1;
66
+ return doBlock(b.v);
67
+ }
68
+ case K.File:
69
+ case K.Url:
70
+ // as R3: runs the script in that file; the host reads it
71
+ f.phase = 1;
72
+ return hostCall(t, 'do-file', [x], 'read files');
73
+ case K.Error:
74
+ return { k: 'throw', e: { kind: T.Error, v: x, name: null } };
75
+ }
76
+ return value(x);
77
+ });
78
+ native('reduce', '["Evaluates each expression of a block; gives a block of the values." value [any-value!]]', (_t, f, v) => {
79
+ const x = f.args[0];
80
+ if (x.t !== K.Block)
81
+ return value(x);
82
+ if (f.phase === 0) {
83
+ f.phase = 1;
84
+ f.data = { feed: { s: x.s, i: x.i }, out: [] };
85
+ }
86
+ const st = f.data;
87
+ if (v !== null)
88
+ st.out.push(v);
89
+ if (st.feed.i >= st.feed.s.data.length)
90
+ return value(block(st.out));
91
+ return { k: 'expr', feed: st.feed };
92
+ });
93
+ native('quote', '["The value as it is, not evaluated." :value [any-type!]]', (_t, f) => value(f.args[0]));
94
+ // --- conditionals ----------------------------------------------------------------
95
+ native('if', '["Evaluates the block if the condition is true; gives its value, or none." condition [any-value!] body [block!]]', (_t, f, v) => {
96
+ if (f.phase === 1)
97
+ return value(v);
98
+ if (!truthy(f.args[0]))
99
+ return value(NONE);
100
+ f.phase = 1;
101
+ return doBlock(f.args[1]);
102
+ });
103
+ native('unless', '["Evaluates the block if the condition is not true; gives its value, or none." condition [any-value!] body [block!]]', (_t, f, v) => {
104
+ if (f.phase === 1)
105
+ return value(v);
106
+ if (truthy(f.args[0]))
107
+ return value(NONE);
108
+ f.phase = 1;
109
+ return doBlock(f.args[1]);
110
+ });
111
+ native('either', '["Evaluates the first block if the condition is true, the second if not." condition [any-value!] true-body [block!] false-body [block!]]', (_t, f, v) => {
112
+ if (f.phase === 1)
113
+ return value(v);
114
+ f.phase = 1;
115
+ return doBlock(f.args[truthy(f.args[0]) ? 1 : 2]);
116
+ });
117
+ native('not', '["True if the value is false or none, false otherwise." value [any-value!]]', (_t, f) => value({ t: K.Logic, v: !truthy(f.args[0]) }));
118
+ /** `all` and `any`: evaluate expressions until one is false (all) or true (any). */
119
+ function shortCircuit(f, v, stopOn) {
120
+ const b = f.args[0];
121
+ if (f.phase === 0) {
122
+ f.phase = 1;
123
+ f.data = { s: b.s, i: b.i };
124
+ }
125
+ const feed = f.data;
126
+ if (v !== null) {
127
+ if (truthy(v) === stopOn)
128
+ return value(stopOn ? v : NONE);
129
+ if (feed.i >= feed.s.data.length)
130
+ return value(stopOn ? NONE : v);
131
+ }
132
+ else if (feed.i >= feed.s.data.length) {
133
+ return value(stopOn ? NONE : { t: K.Logic, v: true });
134
+ }
135
+ return { k: 'expr', feed };
136
+ }
137
+ native('all', '["Evaluates expressions while they are true; gives the last value, or none at the first false one." block [block!]]', (_t, f, v) => shortCircuit(f, v, false));
138
+ native('any', '["Evaluates expressions until one is true; gives it, or none." block [block!]]', (_t, f, v) => shortCircuit(f, v, true));
139
+ native('case', '["Evaluates the block after the first true condition." cases [block!] /all]', (_t, f, v) => {
140
+ const b = f.args[0];
141
+ const every = f.args[1].t === K.Logic;
142
+ if (f.phase === 0)
143
+ f.data = { feed: { s: b.s, i: b.i }, out: NONE };
144
+ const st = f.data;
145
+ const data = st.feed.s.data;
146
+ if (f.phase === 1) {
147
+ // v is a condition
148
+ const body = data[st.feed.i];
149
+ if (body === undefined)
150
+ return fail('script', 'case-body', 'case condition has no block after it');
151
+ st.feed.i++;
152
+ if (truthy(v)) {
153
+ if (body.t !== K.Block)
154
+ return value(body);
155
+ f.phase = 2;
156
+ return doBlock(body);
157
+ }
158
+ }
159
+ else if (f.phase === 2) {
160
+ // v is the result of a body
161
+ if (!every)
162
+ return value(v);
163
+ st.out = v;
164
+ }
165
+ if (st.feed.i >= data.length)
166
+ return value(st.out);
167
+ f.phase = 1;
168
+ return { k: 'expr', feed: st.feed };
169
+ });
170
+ native('switch', '["Evaluates the block after the value that is equal to the one given." value [any-value!] cases [block!] /default case [block!]]', (_t, f, v) => {
171
+ if (f.phase === 1)
172
+ return value(v);
173
+ const x = f.args[0];
174
+ const cases = f.args[1];
175
+ const data = cases.s.data;
176
+ for (let i = cases.i; i < data.length; i++) {
177
+ const c = data[i];
178
+ if (c.t !== K.Block && isEqual(c, x)) {
179
+ let j = i + 1;
180
+ while (j < data.length && data[j].t !== K.Block)
181
+ j++;
182
+ const body = data[j];
183
+ if (body === undefined)
184
+ return value(NONE);
185
+ f.phase = 1;
186
+ return doBlock(body);
187
+ }
188
+ }
189
+ if (f.args[2].t === K.Logic) {
190
+ f.phase = 1;
191
+ return doBlock(f.args[3]);
192
+ }
193
+ return value(NONE);
194
+ });
195
+ /** Shared break/continue handling. Returns a final Step on break, else null. */
196
+ function loopThrow(f, st, v) {
197
+ const th = caught(f);
198
+ if (th !== null) {
199
+ if (th.kind === T.Break)
200
+ return value(th.v);
201
+ }
202
+ else if (v !== null) {
203
+ st.out = v;
204
+ }
205
+ return null;
206
+ }
207
+ native('loop', '["Evaluates a block a number of times." count [integer!] body [block!]]', (_t, f, v) => {
208
+ if (f.phase === 0) {
209
+ f.phase = 1;
210
+ f.trap = LOOP_TRAP;
211
+ f.data = { left: I64.toNumber(f.args[0].v), out: NONE };
212
+ }
213
+ const st = f.data;
214
+ const stop = loopThrow(f, st, v);
215
+ if (stop !== null)
216
+ return stop;
217
+ if (st.left <= 0)
218
+ return value(st.out);
219
+ st.left--;
220
+ return doBlock(f.args[1]);
221
+ });
222
+ /** A fresh context for loop words, and the body copied and bound to it. */
223
+ function loopBody(words, body) {
224
+ const list = words.t === K.Block ? words.s.data.slice(words.i) : [words];
225
+ const ctx = context(list.map(w => w.sym), list.map(() => NONE));
226
+ const s = copyDeep(body.s, body.i);
227
+ bindSeries(s, ctx);
228
+ return { ctx, body: { t: K.Block, s, i: 0 } };
229
+ }
230
+ native('repeat', "[\"Evaluates a block a number of times, with a word counting from 1.\" 'word [word!] count [integer!] body [block!]]", (_t, f, v) => {
231
+ if (f.phase === 0) {
232
+ f.phase = 1;
233
+ f.trap = LOOP_TRAP;
234
+ const n = I64.toNumber(f.args[1].v);
235
+ f.data = { ...loopBody(f.args[0], f.args[2]), n, i: 0, out: NONE };
236
+ }
237
+ const st = f.data;
238
+ const stop = loopThrow(f, st, v);
239
+ if (stop !== null)
240
+ return stop;
241
+ if (st.i >= st.n)
242
+ return value(st.out);
243
+ st.i++;
244
+ st.ctx.vals[0] = { t: K.Integer, v: st.i };
245
+ return doBlock(st.body);
246
+ });
247
+ native('foreach', "[\"Evaluates a block for each value of a series, with a word set to it; for a map, each key, or each key and value.\" 'word [word! block!] data [series! map!] body [block!]]", (_t, f, v) => {
248
+ const data = f.args[1];
249
+ if (f.phase === 0) {
250
+ f.phase = 1;
251
+ f.trap = LOOP_TRAP;
252
+ const words = f.args[0];
253
+ if (words.t === K.Block && (words.s.data.length === words.i || words.s.data.slice(words.i).some(w => w.t !== K.Word))) {
254
+ return fail('script', 'bad-words', 'foreach needs one or more words');
255
+ }
256
+ const body = loopBody(words, f.args[2]);
257
+ if (data.t === K.Map && body.ctx.keys.length > 2)
258
+ return fail('script', 'bad-words', 'foreach over a map takes a key, or a key and a value');
259
+ // a map is walked as it was when the loop began (R3): the body may change it
260
+ f.data = { ...body, at: data.t === K.Map ? 0 : data.i, pairs: data.t === K.Map ? data.s.data.slice() : null, out: NONE };
261
+ }
262
+ const st = f.data;
263
+ const stop = loopThrow(f, st, v);
264
+ if (stop !== null)
265
+ return stop;
266
+ if (st.pairs !== null) {
267
+ if (st.at >= st.pairs.length)
268
+ return value(st.out);
269
+ st.ctx.vals[0] = st.pairs[st.at];
270
+ if (st.ctx.vals.length === 2)
271
+ st.ctx.vals[1] = st.pairs[st.at + 1];
272
+ st.at += 2;
273
+ return doBlock(st.body);
274
+ }
275
+ const items = data.s.data;
276
+ if (st.at >= items.length)
277
+ return value(st.out);
278
+ for (let k = 0; k < st.ctx.vals.length; k++)
279
+ st.ctx.vals[k] = itemAt(data, st.at + k);
280
+ st.at += st.ctx.vals.length;
281
+ return doBlock(st.body);
282
+ });
283
+ /** Element `i` of any series as a cell, or none past the end. */
284
+ function itemAt(series, i) {
285
+ if (series.t === K.Binary) {
286
+ const b = series.s.data[i];
287
+ return b === undefined ? NONE : { t: K.Integer, v: b };
288
+ }
289
+ if (series.t >= K.String && series.t <= K.Tag) {
290
+ const cp = series.s.data[i];
291
+ return cp === undefined ? NONE : { t: K.Char, v: cp };
292
+ }
293
+ return series.s.data[i] ?? NONE;
294
+ }
295
+ native('while', '["Evaluates the body as long as the condition block is true." condition [block!] body [block!]]', (_t, f, v) => {
296
+ if (f.phase === 0) {
297
+ f.trap = LOOP_TRAP;
298
+ f.data = { out: NONE };
299
+ }
300
+ const st = f.data;
301
+ const th = caught(f);
302
+ if (th !== null && th.kind === T.Break)
303
+ return value(th.v);
304
+ if (f.phase === 1 && th === null) {
305
+ // v is the condition
306
+ if (!truthy(v))
307
+ return value(st.out);
308
+ f.phase = 2;
309
+ return doBlock(f.args[1]);
310
+ }
311
+ if (f.phase === 2 && th === null)
312
+ st.out = v;
313
+ f.phase = 1;
314
+ return doBlock(f.args[0]);
315
+ });
316
+ native('until', '["Evaluates the block until it gives true." body [block!]]', (_t, f, v) => {
317
+ const th = caught(f);
318
+ if (th !== null && th.kind === T.Break)
319
+ return value(th.v);
320
+ if (f.phase === 1 && th === null && truthy(v))
321
+ return value(v);
322
+ f.phase = 1;
323
+ f.trap = LOOP_TRAP;
324
+ return doBlock(f.args[0]);
325
+ });
326
+ native('break', '["Leaves the loop it is in." /return value [any-value!]]', (_t, f) => ({ k: 'throw', e: { kind: T.Break, v: f.args[0].t === K.Logic ? f.args[1] : UNSET, name: null } }));
327
+ native('continue', '["Goes on with the next turn of the loop it is in." ]', () => ({ k: 'throw', e: { kind: T.Continue, v: UNSET, name: null } }));
328
+ // --- functions -------------------------------------------------------------------
329
+ native('return', '["Leaves the function it is in, giving a value." value [any-type!]]', (_t, f) => ({ k: 'throw', e: { kind: T.Return, v: f.args[0], name: null } }));
330
+ native('exit', '["Leaves the function it is in, giving no value." ]', () => ({ k: 'throw', e: { kind: T.Return, v: UNSET, name: null } }));
331
+ // --- errors and throws -----------------------------------------------------------
332
+ native('try', '["Evaluates a block; gives an error as a value instead of raising it." body [block!]]', (_t, f, v) => {
333
+ const th = caught(f);
334
+ if (th !== null)
335
+ return value(th.v);
336
+ if (f.phase === 1)
337
+ return value(v);
338
+ f.phase = 1;
339
+ f.trap = 1 << T.Error;
340
+ return doBlock(f.args[0]);
341
+ });
342
+ native('attempt', '["Evaluates a block; gives none if it raises an error." body [block!]]', (_t, f, v) => {
343
+ if (caught(f) !== null)
344
+ return value(NONE);
345
+ if (f.phase === 1)
346
+ return value(v);
347
+ f.phase = 1;
348
+ f.trap = 1 << T.Error;
349
+ return doBlock(f.args[0]);
350
+ });
351
+ native('catch', '["Evaluates a block; gives the value a throw inside it threw." body [block!] /name word [word! block!]]', (_t, f, v) => {
352
+ const th = caught(f);
353
+ if (th !== null) {
354
+ const named = f.args[1].t === K.Logic;
355
+ const names = f.args[2];
356
+ const list = names.t === K.Block ? names.s.data.slice(names.i) : [names];
357
+ const match = named
358
+ ? th.name !== null && list.some(n => isEqual(n, th.name))
359
+ : th.name === null;
360
+ return match ? value(th.v) : { k: 'throw', e: th };
361
+ }
362
+ if (f.phase === 1)
363
+ return value(v);
364
+ f.phase = 1;
365
+ f.trap = 1 << T.Throw;
366
+ return doBlock(f.args[0]);
367
+ });
368
+ native('throw', '["Throws a value to the catch around it." value [any-value!] /name word [word!]]', (_t, f) => ({ k: 'throw', e: { kind: T.Throw, v: f.args[0], name: f.args[1].t === K.Logic ? f.args[2] : null } }));
369
+ // As in R3, with no catalog of messages: the args, formed, are the message.
370
+ native('cause-error', '["Raises an error of a type and an id." type [word!] id [word!] args [any-value!]]', (_t, f) => {
371
+ const [type, id, args] = f.args;
372
+ const e = makeError(spell(type.sym), spell(id.sym), form(args));
373
+ return { k: 'throw', e: { kind: T.Error, v: e, name: null } };
374
+ });
@@ -0,0 +1,15 @@
1
+ import { type Cell } from '../values.ts';
2
+ interface CsvOpts {
3
+ delim: number[];
4
+ quote: number;
5
+ trim: boolean;
6
+ }
7
+ /** Parses CSV text into rows of fields; returns an error message when a quote never closes. */
8
+ export declare function parseCsv(src: readonly number[], o: CsvOpts): string[][] | string;
9
+ /**
10
+ * A value as the text of a field: none is empty, strings and words as they are, dates in ISO 8601
11
+ * (a space before the time, as spreadsheets read it) and amounts as plain numbers, as to-json
12
+ * writes them; the rest formed.
13
+ */
14
+ export declare function fieldText(c: Cell): string;
15
+ export {};