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,666 @@
1
+ // The evaluator.
2
+ //
3
+ // A loop over an explicit stack of frames: evaluation never nests on the JS
4
+ // stack, so a task can stop at any point (a native returning `await`, or the
5
+ // step budget running out) and continue later from exactly the same place.
6
+ //
7
+ // Frames communicate through one register, `task.v`: a frame that needs a
8
+ // value pushes a child frame and leaves `task.v` null; when the child pops it
9
+ // puts its result in `task.v` and the parent, back on top, consumes it.
10
+ //
11
+ // Natives get their arguments already evaluated and answer with a Step. To
12
+ // evaluate code (`if`, `loop`, `try`...) they return `do` or `expr` and are
13
+ // called again with the result; they never call the evaluator themselves.
14
+ // Errors, `break`, `return` and `throw` travel as a `Thrown` that unwinds
15
+ // frames until a native that traps it, or a function call for `return`.
16
+ import { K, kindName, kindBit } from "./kinds.js";
17
+ import { spell, canon } from "./symbols.js";
18
+ import { NONE, UNSET, TRUE, string, blockOf, } from "./values.js";
19
+ import { getWord, setWord, instantiate } from "./ctx.js";
20
+ import { makeError, fillErrorField } from "./errors.js";
21
+ import { P } from "./func.js";
22
+ import { pick, poke } from "./access.js";
23
+ import { sourceOf } from "./mold.js";
24
+ // --- steps and throws ------------------------------------------------------------
25
+ /** What a thrown value is: an error, or one of the non-local exits. */
26
+ export const T = { Error: 0, Break: 1, Continue: 2, Return: 3, Throw: 4 };
27
+ // --- frames ----------------------------------------------------------------------
28
+ /**
29
+ * Files whose code is Trebol's own library. An error raised inside them
30
+ * reports where the program called into the library, not the library's
31
+ * insides: `fetch`, not the native it calls.
32
+ */
33
+ export const LIBRARY_FILES = new Set(['mezzanines']);
34
+ const inLibrary = (s) => s.file !== undefined && LIBRARY_FILES.has(s.file);
35
+ /** True when the expression that made the call on top of the stack is library code. */
36
+ function calledFromLibrary(t) {
37
+ for (let i = t.stack.length - 2; i >= 0; i--) {
38
+ const f = t.stack[i];
39
+ if (f.k === F.Expr || f.k === F.Do || f.k === F.Set)
40
+ return inLibrary(f.feed.s);
41
+ }
42
+ return false;
43
+ }
44
+ export const F = { Do: 0, Expr: 1, Set: 2, Args: 3, Native: 4, Call: 5, Path: 6 };
45
+ const PM = { Call: 0, Get: 1, Set: 2 };
46
+ const PS = { Start: 0, Walk: 1, WaitKey: 2, WaitValue: 3 };
47
+ const NATIVES = [];
48
+ /** Adds a native to the global table. `boot` installs every registered native in lib. */
49
+ export function registerNative(def, fn) {
50
+ const full = { ...def, id: NATIVES.length };
51
+ NATIVES.push({ def: full, fn });
52
+ return { t: K.Native, p: full };
53
+ }
54
+ export function registeredNatives() {
55
+ return NATIVES.map(n => n.def);
56
+ }
57
+ /** A task that evaluates `block`. */
58
+ export function newTask(rt, s, i = 0) {
59
+ const t = { rt, stack: [], v: null, thrown: null, waiting: null };
60
+ pushDo(t, s, i);
61
+ return t;
62
+ }
63
+ /** Continues a parked task with the awaited value. */
64
+ export function resume(t, v) {
65
+ t.waiting = null;
66
+ t.v = v;
67
+ }
68
+ /** Continues a parked task with an error (a rejected promise). */
69
+ export function resumeError(t, e) {
70
+ t.waiting = null;
71
+ t.thrown = { kind: T.Error, v: e, name: null };
72
+ }
73
+ /**
74
+ * Runs a task for at most `budget` steps. Returns `yield` when the budget
75
+ * runs out (call again to continue) and `await` when a native parked it.
76
+ */
77
+ export function run(t, budget = Infinity) {
78
+ if (t.waiting !== null)
79
+ return { k: 'await', p: t.waiting };
80
+ for (let n = 0; n < budget; n++) {
81
+ if (t.thrown !== null) {
82
+ const done = unwind(t);
83
+ if (done !== null)
84
+ return done;
85
+ continue;
86
+ }
87
+ const f = t.stack[t.stack.length - 1];
88
+ if (f === undefined)
89
+ return { k: 'done', v: t.v ?? UNSET };
90
+ switch (f.k) {
91
+ case F.Do:
92
+ if (f.state === 0)
93
+ doFrame(t, f);
94
+ else
95
+ exprInfix(t, f);
96
+ break;
97
+ case F.Expr:
98
+ if (f.state === 0)
99
+ exprStart(t, f);
100
+ else
101
+ exprInfix(t, f);
102
+ break;
103
+ case F.Set:
104
+ if (f.state === 0)
105
+ exprStart(t, f);
106
+ else
107
+ exprInfix(t, f);
108
+ break;
109
+ case F.Args:
110
+ argsFrame(t, f);
111
+ break;
112
+ case F.Native: {
113
+ const v = t.v;
114
+ t.v = null;
115
+ applyStep(t, f, NATIVES[f.def.id].fn(t, f, v));
116
+ break;
117
+ }
118
+ case F.Call: {
119
+ const v = t.v ?? UNSET;
120
+ t.v = null;
121
+ t.stack.pop();
122
+ finishCall(t, f, v);
123
+ break;
124
+ }
125
+ case F.Path:
126
+ pathFrame(t, f);
127
+ break;
128
+ }
129
+ if (t.waiting !== null)
130
+ return { k: 'await', p: t.waiting };
131
+ }
132
+ return { k: 'yield' };
133
+ }
134
+ /** The file of the innermost code being evaluated, if it was loaded from one. */
135
+ export function currentFile(t) {
136
+ for (let i = t.stack.length - 1; i >= 0; i--) {
137
+ const f = t.stack[i];
138
+ if ((f.k === F.Do || f.k === F.Expr) && f.feed.s.file !== undefined)
139
+ return f.feed.s.file;
140
+ }
141
+ return null;
142
+ }
143
+ // --- errors ----------------------------------------------------------------------
144
+ export function throwError(type, id, message) {
145
+ return { k: 'throw', e: { kind: T.Error, v: makeError(type, id, message), name: null } };
146
+ }
147
+ function fail(t, type, id, message) {
148
+ t.v = null;
149
+ t.thrown = { kind: T.Error, v: makeError(type, id, message), name: null };
150
+ }
151
+ const nameOf = (s) => (s === null ? 'function' : spell(s));
152
+ function unwind(t) {
153
+ const th = t.thrown;
154
+ const f = t.stack[t.stack.length - 1];
155
+ if (f === undefined) {
156
+ t.thrown = null;
157
+ return { k: 'error', e: uncaught(th) };
158
+ }
159
+ const e = th.kind === T.Error ? th.v : null;
160
+ switch (f.k) {
161
+ case F.Native:
162
+ if (f.trap & (1 << th.kind)) {
163
+ f.thrown = th;
164
+ t.thrown = null;
165
+ t.v = null;
166
+ applyStep(t, f, NATIVES[f.def.id].fn(t, f, null));
167
+ return null;
168
+ }
169
+ if (e !== null && f.name !== null && !calledFromLibrary(t))
170
+ fillErrorField(e, 'where', { t: K.Word, sym: f.name, bind: null });
171
+ break;
172
+ case F.Call:
173
+ if (th.kind === T.Return) {
174
+ t.stack.pop();
175
+ t.thrown = null;
176
+ finishCall(t, f, th.v);
177
+ return null;
178
+ }
179
+ if (th.kind === T.Break || th.kind === T.Continue) {
180
+ t.thrown = { kind: T.Error, v: uncaught(th), name: null };
181
+ return null;
182
+ }
183
+ if (e !== null && f.name !== null && !calledFromLibrary(t))
184
+ fillErrorField(e, 'where', { t: K.Word, sym: f.name, bind: null });
185
+ break;
186
+ case F.Args:
187
+ if (e !== null && f.name !== null && !calledFromLibrary(t))
188
+ fillErrorField(e, 'where', { t: K.Word, sym: f.name, bind: null });
189
+ break;
190
+ case F.Do:
191
+ case F.Expr:
192
+ case F.Set:
193
+ if (e !== null && (f.k !== F.Do || f.state === 1) && !inLibrary(f.feed.s)) {
194
+ const s = f.feed.s;
195
+ const end = Math.max(f.feed.i, f.start + 1);
196
+ fillErrorField(e, 'near', blockOf(K.Block, s.data.slice(f.start, Math.min(end, f.start + 4))));
197
+ const at = sourceOf(s, f.start);
198
+ if (at !== null)
199
+ fillErrorField(e, 'at', string(at));
200
+ }
201
+ break;
202
+ }
203
+ t.stack.pop();
204
+ return null;
205
+ }
206
+ /** A throw that reached the top: errors as they are, other exits become errors. */
207
+ function uncaught(th) {
208
+ switch (th.kind) {
209
+ case T.Error: return th.v;
210
+ case T.Break: return makeError('script', 'no-loop', 'break used outside of a loop');
211
+ case T.Continue: return makeError('script', 'no-loop', 'continue used outside of a loop');
212
+ case T.Return: return makeError('script', 'no-function', 'return used outside of a function');
213
+ default: return makeError('script', 'no-catch', 'throw has no catch');
214
+ }
215
+ }
216
+ // --- frame handlers --------------------------------------------------------------
217
+ function pushDo(t, s, i) {
218
+ t.stack.push({ k: F.Do, feed: { s, i }, out: UNSET, infix: true, start: i, state: 0 });
219
+ }
220
+ function pushExpr(t, feed, infix) {
221
+ t.stack.push({ k: F.Expr, feed, infix, start: feed.i, state: 0 });
222
+ }
223
+ function doFrame(t, f) {
224
+ if (t.v !== null) {
225
+ f.out = t.v;
226
+ t.v = null;
227
+ }
228
+ if (f.feed.i >= f.feed.s.data.length) {
229
+ t.stack.pop();
230
+ t.v = f.out;
231
+ return;
232
+ }
233
+ exprStart(t, f);
234
+ }
235
+ function exprStart(t, f) {
236
+ const feed = f.feed;
237
+ const data = feed.s.data;
238
+ f.start = feed.i;
239
+ f.state = 1;
240
+ const c = data[feed.i++];
241
+ switch (c.t) {
242
+ case K.Word: {
243
+ const v = getWord(c);
244
+ if (v === null)
245
+ return unbound(t, c);
246
+ switch (v.t) {
247
+ case K.Function:
248
+ case K.Native:
249
+ return beginCall(t, feed, v, c.sym, null, null);
250
+ case K.Op:
251
+ return fail(t, 'script', 'no-op-arg', `${spell(c.sym)} operator is missing its left argument`);
252
+ case K.Unset:
253
+ return fail(t, 'script', 'no-value', `${spell(c.sym)} has no value`);
254
+ }
255
+ t.v = v;
256
+ return;
257
+ }
258
+ case K.SetWord: {
259
+ if (feed.i >= data.length)
260
+ return fail(t, 'script', 'need-value', `${spell(c.sym)}: needs a value`);
261
+ // a plain value with no operator after it is assigned at once
262
+ const q = quickValue(data[feed.i]);
263
+ if (q !== null && q.t !== K.Unset && !isOpWord(data[feed.i + 1])) {
264
+ feed.i++;
265
+ if (!setWord(c, q))
266
+ return unbound(t, c);
267
+ t.v = q;
268
+ return;
269
+ }
270
+ t.stack.push({ k: F.Set, w: c, feed, infix: true, start: feed.i, state: 0 });
271
+ return;
272
+ }
273
+ case K.GetWord: {
274
+ const v = getWord(c);
275
+ if (v === null)
276
+ return unbound(t, c);
277
+ t.v = v;
278
+ return;
279
+ }
280
+ case K.LitWord:
281
+ t.v = { t: K.Word, sym: c.sym, bind: c.bind };
282
+ return;
283
+ case K.Paren:
284
+ pushDo(t, c.s, c.i);
285
+ return;
286
+ case K.Path:
287
+ case K.GetPath:
288
+ t.stack.push({
289
+ k: F.Path, feed, path: c, mode: c.t === K.Path ? PM.Call : PM.Get,
290
+ state: PS.Start, j: 0, cur: NONE, setv: NONE,
291
+ });
292
+ return;
293
+ case K.SetPath:
294
+ if (feed.i >= data.length)
295
+ return fail(t, 'script', 'need-value', 'set-path needs a value');
296
+ t.stack.push({ k: F.Path, feed, path: c, mode: PM.Set, state: PS.WaitValue, j: 0, cur: NONE, setv: NONE });
297
+ pushExpr(t, feed, true);
298
+ return;
299
+ case K.LitPath:
300
+ t.v = { t: K.Path, s: c.s, i: c.i };
301
+ return;
302
+ // a function itself in a block, as reduce [:f x] leaves it, is called, as in R3
303
+ case K.Function:
304
+ case K.Native:
305
+ return beginCall(t, feed, c, null, null, null);
306
+ }
307
+ t.v = c;
308
+ }
309
+ function exprInfix(t, f) {
310
+ if (f.infix) {
311
+ const next = f.feed.s.data[f.feed.i];
312
+ if (next !== undefined && next.t === K.Word) {
313
+ const op = getWord(next);
314
+ if (op !== null && op.t === K.Op) {
315
+ f.feed.i++;
316
+ const left = t.v;
317
+ t.v = null;
318
+ const fn = op.p.fn;
319
+ if (!quickOp(t, f.feed, fn, next.sym, left))
320
+ beginCall(t, f.feed, fn, next.sym, null, left);
321
+ return;
322
+ }
323
+ }
324
+ }
325
+ // the value stays in t.v: for the parent, or for the Do frame to keep
326
+ if (f.k === F.Do)
327
+ f.state = 0;
328
+ else if (f.k === F.Set)
329
+ setFrame(t, f);
330
+ else
331
+ t.stack.pop();
332
+ }
333
+ /**
334
+ * An operator whose native takes just two arguments, with a plain value on its
335
+ * right that both types allow, runs at once; false means take the general way.
336
+ */
337
+ function quickOp(t, feed, fn, name, left) {
338
+ if (fn.t !== K.Native)
339
+ return false;
340
+ const def = fn.p;
341
+ const params = def.spec.params;
342
+ if (params.length !== 2 || params[0].kind !== P.Arg || params[1].kind !== P.Arg)
343
+ return false;
344
+ const rc = feed.s.data[feed.i];
345
+ const right = rc === undefined ? null : quickValue(rc);
346
+ if (right === null || !params[0].allows[left.t] || !params[1].allows[right.t])
347
+ return false;
348
+ feed.i++;
349
+ const nf = { k: F.Native, def, args: [left, right], phase: 0, data: null, trap: 0, thrown: null, name };
350
+ t.stack.push(nf);
351
+ applyStep(t, nf, NATIVES[def.id].fn(t, nf, null));
352
+ return true;
353
+ }
354
+ function setFrame(t, f) {
355
+ const v = t.v;
356
+ t.stack.pop();
357
+ if (v.t === K.Unset)
358
+ return fail(t, 'script', 'need-value', `${spell(f.w.sym)}: needs a value`);
359
+ if (!setWord(f.w, v))
360
+ return unbound(t, f.w);
361
+ }
362
+ function unbound(t, w) {
363
+ if (w.bind === null)
364
+ fail(t, 'script', 'not-bound', `${spell(w.sym)} is not bound to a context`);
365
+ else
366
+ fail(t, 'script', 'no-value', `${spell(w.sym)} has no value`);
367
+ }
368
+ function specOf(fn) {
369
+ return fn.t === K.Native ? fn.p.spec : fn.p.spec;
370
+ }
371
+ /**
372
+ * Starts a call: pushes an Args frame that gathers arguments from `feed`.
373
+ * `refs` are the refinement segments of a path call; `left` is the left
374
+ * operand of an op.
375
+ */
376
+ function beginCall(t, feed, fn, name, refs, left) {
377
+ if (fn.t !== K.Function && fn.t !== K.Native) {
378
+ return fail(t, 'script', 'not-function', `${nameOf(name)} is not a function`);
379
+ }
380
+ const spec = specOf(fn);
381
+ const vals = spec.blank.slice();
382
+ // the plan is the spec's own list unless refinements add to it
383
+ let plan = spec.base;
384
+ if (refs !== null && refs.length > 0) {
385
+ plan = plan.slice();
386
+ for (const seg of refs) {
387
+ const ri = seg.t === K.Word
388
+ ? spec.params.findIndex(p => p.kind === P.Ref && canon(p.sym) === canon(seg.sym))
389
+ : -1;
390
+ if (ri < 0) {
391
+ const what = seg.t === K.Word ? spell(seg.sym) : 'that';
392
+ return fail(t, 'script', 'bad-refine', `${nameOf(name)} has no refinement called ${what}`);
393
+ }
394
+ vals[ri] = TRUE;
395
+ spec.params.forEach((p, j) => { if (p.ref === ri)
396
+ plan.push(j); });
397
+ }
398
+ }
399
+ let n = 0;
400
+ if (left !== null) {
401
+ if (plan.length < 2)
402
+ return fail(t, 'script', 'bad-op', `${nameOf(name)} needs two arguments to be an operator`);
403
+ vals[plan[0]] = left;
404
+ n = 1;
405
+ }
406
+ const a = { k: F.Args, feed, fn: fn, spec, vals, plan, n, name, op: left !== null };
407
+ t.stack.push(a);
408
+ // take the plain values among the arguments at once; if they are all plain, the call starts now
409
+ if (feed !== null && left === null) {
410
+ const data = feed.s.data;
411
+ while (a.n < plan.length) {
412
+ const p = spec.params[plan[a.n]];
413
+ const c = data[feed.i];
414
+ if (c === undefined || p.kind !== P.Arg)
415
+ break;
416
+ const q = quickValue(c);
417
+ if (q === null || isOpWord(data[feed.i + 1]))
418
+ break;
419
+ feed.i++;
420
+ vals[plan[a.n++]] = q;
421
+ }
422
+ if (a.n === plan.length)
423
+ argsFrame(t, a);
424
+ }
425
+ }
426
+ /**
427
+ * The value of an item that evaluates to itself, or to a word's value that is
428
+ * not a function; null when it needs the full evaluator (a call, a path, a paren...).
429
+ */
430
+ function quickValue(c) {
431
+ switch (c.t) {
432
+ case K.Word:
433
+ case K.GetWord: {
434
+ const v = getWord(c);
435
+ if (v === null)
436
+ return null;
437
+ if (c.t === K.Word && (v.t === K.Function || v.t === K.Native || v.t === K.Op || v.t === K.Unset))
438
+ return null;
439
+ return v;
440
+ }
441
+ case K.SetWord:
442
+ case K.LitWord:
443
+ case K.Paren:
444
+ case K.Path:
445
+ case K.GetPath:
446
+ case K.SetPath:
447
+ case K.LitPath:
448
+ case K.Function:
449
+ case K.Native:
450
+ return null;
451
+ }
452
+ return c;
453
+ }
454
+ function isOpWord(c) {
455
+ if (c === undefined || c.t !== K.Word)
456
+ return false;
457
+ const v = getWord(c);
458
+ return v !== null && v.t === K.Op;
459
+ }
460
+ function argsFrame(t, f) {
461
+ if (t.v !== null) {
462
+ f.vals[f.plan[f.n++]] = t.v;
463
+ t.v = null;
464
+ }
465
+ const params = f.spec.params;
466
+ if (f.n < f.plan.length) {
467
+ const p = params[f.plan[f.n]];
468
+ const feed = f.feed;
469
+ if (feed === null || feed.i >= feed.s.data.length) {
470
+ return fail(t, 'script', 'no-arg', `${nameOf(f.name)} is missing its ${spell(p.sym)} argument`);
471
+ }
472
+ if (p.kind === P.Arg) {
473
+ // a plain value with no operator after it needs no expression frame
474
+ const q = quickValue(feed.s.data[feed.i]);
475
+ if (q !== null && (f.op || !isOpWord(feed.s.data[feed.i + 1]))) {
476
+ feed.i++;
477
+ t.v = q;
478
+ return;
479
+ }
480
+ return pushExpr(t, feed, !f.op);
481
+ }
482
+ const c = feed.s.data[feed.i++];
483
+ if (p.kind === P.Lit && c.t === K.Paren)
484
+ return pushDo(t, c.s, c.i);
485
+ if (p.kind === P.Lit && c.t === K.GetWord) {
486
+ const v = getWord(c);
487
+ if (v === null)
488
+ return unbound(t, c);
489
+ t.v = v;
490
+ return;
491
+ }
492
+ t.v = c;
493
+ return;
494
+ }
495
+ for (const i of f.plan) {
496
+ const p = params[i];
497
+ const v = f.vals[i];
498
+ if (!p.allows[v.t]) {
499
+ return fail(t, 'script', 'expect-arg', `${nameOf(f.name)} does not allow ${kindName(v.t)} for its ${spell(p.sym)} argument`);
500
+ }
501
+ }
502
+ t.stack.pop();
503
+ if (f.fn.t === K.Native) {
504
+ // run it at once: most natives give their value in this first call
505
+ const def = f.fn.p;
506
+ const nf = { k: F.Native, def, args: f.vals, phase: 0, data: null, trap: 0, thrown: null, name: f.name };
507
+ t.stack.push(nf);
508
+ applyStep(t, nf, NATIVES[def.id].fn(t, nf, null));
509
+ return;
510
+ }
511
+ const def = f.fn.p;
512
+ const ctx = { keys: def.tmpl.keys, vals: f.vals, parent: null };
513
+ const body = instantiate(def.body, def.tmpl, ctx);
514
+ t.stack.push({ k: F.Call, def, name: f.name });
515
+ pushDo(t, body, 0);
516
+ }
517
+ function finishCall(t, f, v) {
518
+ const ret = f.def.spec.ret;
519
+ if (ret !== null && !f.def.spec.retAllows[v.t]) {
520
+ return fail(t, 'script', 'bad-return', `${nameOf(f.name)} does not allow ${kindName(v.t)} as its result`);
521
+ }
522
+ t.v = v;
523
+ }
524
+ function applyStep(t, f, step) {
525
+ switch (step.k) {
526
+ case 'value':
527
+ t.stack.pop();
528
+ t.v = step.v;
529
+ return;
530
+ case 'do':
531
+ pushDo(t, step.s, step.i);
532
+ return;
533
+ case 'expr':
534
+ pushExpr(t, step.feed, true);
535
+ return;
536
+ case 'call': {
537
+ const depth = t.stack.length;
538
+ beginCall(t, null, step.fn, f.name, null, null);
539
+ const a = t.stack[t.stack.length - 1];
540
+ if (t.stack.length > depth && a !== undefined && a.k === F.Args) {
541
+ if (step.full) {
542
+ applyFull(a, step.args);
543
+ return;
544
+ }
545
+ const n = Math.min(step.args.length, a.plan.length);
546
+ for (let i = 0; i < n; i++)
547
+ a.vals[a.plan[i]] = step.args[i];
548
+ a.n = n;
549
+ }
550
+ return;
551
+ }
552
+ case 'await':
553
+ t.waiting = step.p;
554
+ return;
555
+ case 'throw': {
556
+ // A native that throws is finished: pop it so it cannot trap its own throw.
557
+ const e = step.e;
558
+ if (e.kind === T.Error && f.name !== null && !calledFromLibrary(t)) {
559
+ fillErrorField(e.v, 'where', { t: K.Word, sym: f.name, bind: null });
560
+ }
561
+ t.stack.pop();
562
+ t.thrown = e;
563
+ return;
564
+ }
565
+ }
566
+ }
567
+ /** Fills an Args frame from arguments laid out as the whole spec (see `call` with `full`). */
568
+ function applyFull(a, args) {
569
+ const params = a.spec.params;
570
+ a.plan = [];
571
+ let k = 0;
572
+ let on = true;
573
+ for (let i = 0; i < params.length; i++) {
574
+ const p = params[i];
575
+ if (p.kind === P.Local)
576
+ continue;
577
+ const v = args[k++] ?? NONE;
578
+ if (p.kind === P.Ref) {
579
+ on = v.t !== K.None && !(v.t === K.Logic && !v.v);
580
+ a.vals[i] = on ? TRUE : NONE;
581
+ }
582
+ else if (p.ref < 0 || on) {
583
+ a.vals[i] = v;
584
+ a.plan.push(i);
585
+ }
586
+ }
587
+ a.n = a.plan.length;
588
+ }
589
+ // --- paths -----------------------------------------------------------------------
590
+ function pathFrame(t, f) {
591
+ const segs = f.path.s.data;
592
+ switch (f.state) {
593
+ case PS.WaitValue:
594
+ f.setv = t.v;
595
+ t.v = null;
596
+ if (f.setv.t === K.Unset)
597
+ return fail(t, 'script', 'need-value', 'set-path needs a value');
598
+ f.state = PS.Start;
599
+ return;
600
+ case PS.Start: {
601
+ const head = segs[f.path.i];
602
+ if (head === undefined || head.t !== K.Word)
603
+ return fail(t, 'script', 'bad-path', 'a path must start with a word');
604
+ const v = getWord(head);
605
+ if (v === null)
606
+ return unbound(t, head);
607
+ if (v.t === K.Unset)
608
+ return fail(t, 'script', 'no-value', `${spell(head.sym)} has no value`);
609
+ f.cur = v;
610
+ f.j = f.path.i + 1;
611
+ f.state = PS.Walk;
612
+ if (f.mode === PM.Call && (v.t === K.Function || v.t === K.Native)) {
613
+ t.stack.pop();
614
+ return beginCall(t, f.feed, v, head.sym, segs.slice(f.j), null);
615
+ }
616
+ return;
617
+ }
618
+ case PS.WaitKey: {
619
+ const key = t.v;
620
+ t.v = null;
621
+ f.state = PS.Walk;
622
+ return walk(t, f, key);
623
+ }
624
+ case PS.Walk: {
625
+ if (f.j >= segs.length) {
626
+ t.stack.pop();
627
+ t.v = f.cur;
628
+ return;
629
+ }
630
+ const seg = segs[f.j];
631
+ if (seg.t === K.Paren) {
632
+ f.state = PS.WaitKey;
633
+ pushDo(t, seg.s, seg.i);
634
+ return;
635
+ }
636
+ if (seg.t === K.GetWord) {
637
+ const v = getWord(seg);
638
+ if (v === null)
639
+ return unbound(t, seg);
640
+ return walk(t, f, v);
641
+ }
642
+ return walk(t, f, seg);
643
+ }
644
+ }
645
+ }
646
+ function walk(t, f, key) {
647
+ const segs = f.path.s.data;
648
+ const seg = segs[f.j];
649
+ f.j++;
650
+ if (f.mode === PM.Set && f.j >= segs.length) {
651
+ const msg = poke(f.cur, key, f.setv);
652
+ if (msg !== null)
653
+ return fail(t, 'script', 'bad-path-set', msg);
654
+ t.stack.pop();
655
+ t.v = f.setv;
656
+ return;
657
+ }
658
+ const v = pick(f.cur, key);
659
+ if (typeof v === 'string')
660
+ return fail(t, 'script', 'bad-path', v);
661
+ f.cur = v;
662
+ if (f.mode === PM.Call && (v.t === K.Function || v.t === K.Native)) {
663
+ t.stack.pop();
664
+ beginCall(t, f.feed, v, seg.t === K.Word ? seg.sym : null, segs.slice(f.j), null);
665
+ }
666
+ }