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,14 @@
1
+ export declare const NAMED_CHARS: Readonly<Record<string, number>>;
2
+ /**
3
+ * Decodes the escape that starts right after a caret at `src[i]`.
4
+ * Returns the code point and the index after the escape, or null if malformed.
5
+ */
6
+ export declare function decodeEscape(src: string, i: number): {
7
+ cp: number;
8
+ next: number;
9
+ } | null;
10
+ /**
11
+ * Encodes a code point for use inside a quoted string or char literal.
12
+ * `inBraces` keeps line feeds raw, as braced strings allow them.
13
+ */
14
+ export declare function encodeChar(cp: number, quote: '"' | '}', inBraces?: boolean): string;
@@ -0,0 +1,80 @@
1
+ // Caret escapes, shared by the lexer (decoding) and mold (encoding).
2
+ //
3
+ // ^/ line feed ^- tab ^@ null ^~ delete ^^ caret
4
+ // ^" quote ^{ ^} braces ^A..^Z control chars 1..26
5
+ // ^(line) ^(tab) ^(null) ^(back) ^(page) ^(esc) ^(del) named chars
6
+ // ^(1F600) any code point in hex
7
+ export const NAMED_CHARS = {
8
+ null: 0,
9
+ back: 8,
10
+ tab: 9,
11
+ line: 10,
12
+ page: 12,
13
+ esc: 27,
14
+ del: 127,
15
+ };
16
+ const SINGLE = {
17
+ '/': 10,
18
+ '-': 9,
19
+ '@': 0,
20
+ '~': 127,
21
+ '[': 27,
22
+ '\\': 28,
23
+ ']': 29,
24
+ '_': 31,
25
+ };
26
+ /**
27
+ * Decodes the escape that starts right after a caret at `src[i]`.
28
+ * Returns the code point and the index after the escape, or null if malformed.
29
+ */
30
+ export function decodeEscape(src, i) {
31
+ const c = src[i];
32
+ if (c === undefined)
33
+ return null;
34
+ if (c === '(') {
35
+ const close = src.indexOf(')', i + 1);
36
+ if (close < 0)
37
+ return null;
38
+ const body = src.slice(i + 1, close);
39
+ const named = NAMED_CHARS[body.toLowerCase()];
40
+ if (named !== undefined)
41
+ return { cp: named, next: close + 1 };
42
+ if (!/^[0-9A-Fa-f]{1,6}$/.test(body))
43
+ return null;
44
+ const cp = parseInt(body, 16);
45
+ if (cp > 0x10ffff)
46
+ return null;
47
+ return { cp, next: close + 1 };
48
+ }
49
+ const single = SINGLE[c];
50
+ if (single !== undefined)
51
+ return { cp: single, next: i + 1 };
52
+ if (/[A-Za-z]/.test(c)) {
53
+ return { cp: c.toUpperCase().charCodeAt(0) - 64, next: i + 1 };
54
+ }
55
+ const cp = src.codePointAt(i);
56
+ return { cp, next: i + (cp > 0xffff ? 2 : 1) };
57
+ }
58
+ /**
59
+ * Encodes a code point for use inside a quoted string or char literal.
60
+ * `inBraces` keeps line feeds raw, as braced strings allow them.
61
+ */
62
+ export function encodeChar(cp, quote, inBraces = false) {
63
+ switch (cp) {
64
+ case 0x5e: return '^^'; // ^
65
+ case 10: return inBraces ? '\n' : '^/';
66
+ case 9: return '^-';
67
+ case 0: return '^@';
68
+ case 127: return '^~';
69
+ }
70
+ if (quote === '"' && cp === 0x22)
71
+ return '^"';
72
+ // ^A..^Z, ^[ ^\ ^] ^_ as R3 and Red write them; 30 has no short form (^^ is the caret)
73
+ if (cp >= 1 && cp <= 26)
74
+ return '^' + String.fromCharCode(64 + cp);
75
+ if (cp === 27 || cp === 28 || cp === 29 || cp === 31)
76
+ return '^' + String.fromCharCode(64 + cp);
77
+ if (cp < 32)
78
+ return '^(' + cp.toString(16).toUpperCase().padStart(2, '0') + ')';
79
+ return String.fromCodePoint(cp);
80
+ }
@@ -0,0 +1,220 @@
1
+ import { type Sym } from './symbols.ts';
2
+ import { type Cell, type Ctx, type Series, type BlockCell, type WordCell, type ObjectCell, type OpaqueCell } from './values.ts';
3
+ import { type Spec, type FuncDef, type NativeDef } from './func.ts';
4
+ /** What a thrown value is: an error, or one of the non-local exits. */
5
+ export declare const T: {
6
+ readonly Error: 0;
7
+ readonly Break: 1;
8
+ readonly Continue: 2;
9
+ readonly Return: 3;
10
+ readonly Throw: 4;
11
+ };
12
+ export type TKind = (typeof T)[keyof typeof T];
13
+ export interface Thrown {
14
+ readonly kind: TKind;
15
+ /** The error!, the `break/return` or `return` value, or the thrown value. */
16
+ readonly v: Cell;
17
+ /** `throw/name` only. */
18
+ readonly name: Cell | null;
19
+ }
20
+ /** A read position in a block, shared by the frames evaluating from it. */
21
+ export interface Feed {
22
+ s: Series<Cell>;
23
+ i: number;
24
+ }
25
+ export type Step =
26
+ /** Finished, with this result. */
27
+ {
28
+ readonly k: 'value';
29
+ readonly v: Cell;
30
+ }
31
+ /** Evaluate this block, then call me again with its result. */
32
+ | {
33
+ readonly k: 'do';
34
+ readonly s: Series<Cell>;
35
+ readonly i: number;
36
+ }
37
+ /** Evaluate one expression from this feed, then call me again with it. */
38
+ | {
39
+ readonly k: 'expr';
40
+ readonly feed: Feed;
41
+ }
42
+ /**
43
+ * Call a function with these arguments, then call me again with the result.
44
+ * With `full`, `args` follows the whole spec as `apply` does: refinements
45
+ * take a truthy value to be used, followed by their own arguments.
46
+ */
47
+ | {
48
+ readonly k: 'call';
49
+ readonly fn: Cell;
50
+ readonly args: readonly Cell[];
51
+ readonly full?: boolean;
52
+ }
53
+ /** Park the task until the host resumes it with the promise's value. */
54
+ | {
55
+ readonly k: 'await';
56
+ readonly p: Promise<Cell>;
57
+ }
58
+ /** Unwind with an error or a non-local exit. */
59
+ | {
60
+ readonly k: 'throw';
61
+ readonly e: Thrown;
62
+ };
63
+ /**
64
+ * Files whose code is Trebol's own library. An error raised inside them
65
+ * reports where the program called into the library, not the library's
66
+ * insides: `fetch`, not the native it calls.
67
+ */
68
+ export declare const LIBRARY_FILES: Set<string>;
69
+ export declare const F: {
70
+ readonly Do: 0;
71
+ readonly Expr: 1;
72
+ readonly Set: 2;
73
+ readonly Args: 3;
74
+ readonly Native: 4;
75
+ readonly Call: 5;
76
+ readonly Path: 6;
77
+ };
78
+ /**
79
+ * Evaluates expressions until the end of the feed; the result is the last one.
80
+ * It evaluates each expression itself, as an Expr frame would (state 1 while
81
+ * one is under way), so a block needs no frame per expression.
82
+ */
83
+ interface DoFrame {
84
+ readonly k: typeof F.Do;
85
+ readonly feed: Feed;
86
+ out: Cell;
87
+ readonly infix: true;
88
+ start: number;
89
+ state: 0 | 1;
90
+ }
91
+ /** One expression: a primary value, then any infix operators after it. */
92
+ interface ExprFrame {
93
+ readonly k: typeof F.Expr;
94
+ readonly feed: Feed;
95
+ /** False for the right operand of an op, which takes no infix of its own. */
96
+ readonly infix: boolean;
97
+ start: number;
98
+ state: 0 | 1;
99
+ }
100
+ /** Waits for a value to assign to a set-word. */
101
+ /**
102
+ * Assigns a set-word: evaluates the expression after it itself, as an Expr
103
+ * frame would (state 1 once its first value is in), then sets the word.
104
+ */
105
+ interface SetFrame {
106
+ readonly k: typeof F.Set;
107
+ readonly w: WordCell;
108
+ readonly feed: Feed;
109
+ readonly infix: true;
110
+ start: number;
111
+ state: 0 | 1;
112
+ }
113
+ /** Gathers the arguments of a call. */
114
+ interface ArgsFrame {
115
+ readonly k: typeof F.Args;
116
+ readonly feed: Feed | null;
117
+ readonly fn: OpaqueCell;
118
+ readonly spec: Spec;
119
+ /** The frame values, laid out as `spec.params`. */
120
+ readonly vals: Cell[];
121
+ /** Indexes into `vals` still to gather, in order; may be the spec's own `base`, never changed. */
122
+ plan: number[];
123
+ n: number;
124
+ readonly name: Sym | null;
125
+ readonly op: boolean;
126
+ }
127
+ /** A native running; it may evaluate code and be called again. */
128
+ export interface NativeFrame {
129
+ readonly k: typeof F.Native;
130
+ readonly def: NativeDef;
131
+ /** Arguments laid out as the native's spec. */
132
+ readonly args: Cell[];
133
+ /** Free for the native: where it is in its work. Starts at 0. */
134
+ phase: number;
135
+ /** Free for the native: state kept between calls. */
136
+ data: unknown;
137
+ /** Bitmask of thrown kinds (`1 << T.x`) the native wants to catch. */
138
+ trap: number;
139
+ /** Set when a trapped throw arrives; the native is then called with v = null. */
140
+ thrown: Thrown | null;
141
+ readonly name: Sym | null;
142
+ }
143
+ /** A function body running; catches `return`. */
144
+ interface CallFrame {
145
+ readonly k: typeof F.Call;
146
+ readonly def: FuncDef;
147
+ readonly name: Sym | null;
148
+ }
149
+ declare const PM: {
150
+ readonly Call: 0;
151
+ readonly Get: 1;
152
+ readonly Set: 2;
153
+ };
154
+ type PathMode = (typeof PM)[keyof typeof PM];
155
+ declare const PS: {
156
+ readonly Start: 0;
157
+ readonly Walk: 1;
158
+ readonly WaitKey: 2;
159
+ readonly WaitValue: 3;
160
+ };
161
+ /** Evaluates a path: picks segment by segment, and calls a function at the end. */
162
+ interface PathFrame {
163
+ readonly k: typeof F.Path;
164
+ readonly feed: Feed;
165
+ readonly path: BlockCell;
166
+ readonly mode: PathMode;
167
+ state: (typeof PS)[keyof typeof PS];
168
+ j: number;
169
+ cur: Cell;
170
+ setv: Cell;
171
+ }
172
+ type Frame = DoFrame | ExprFrame | SetFrame | ArgsFrame | NativeFrame | CallFrame | PathFrame;
173
+ /**
174
+ * A native: called first with `v` null and `f.phase` 0, then again with the
175
+ * result of each `do`, `expr`, `call` or `await` it returns.
176
+ */
177
+ export type NativeFn = (t: Task, f: NativeFrame, v: Cell | null) => Step;
178
+ /** Adds a native to the global table. `boot` installs every registered native in lib. */
179
+ export declare function registerNative(def: Omit<NativeDef, 'id'>, fn: NativeFn): OpaqueCell;
180
+ export declare function registeredNatives(): readonly NativeDef[];
181
+ export interface Runtime {
182
+ readonly lib: Ctx;
183
+ readonly user: Ctx;
184
+ }
185
+ export interface Task {
186
+ readonly rt: Runtime;
187
+ readonly stack: Frame[];
188
+ /** The value register. */
189
+ v: Cell | null;
190
+ thrown: Thrown | null;
191
+ /** Set while parked on an `await`. */
192
+ waiting: Promise<Cell> | null;
193
+ }
194
+ export type RunResult = {
195
+ readonly k: 'done';
196
+ readonly v: Cell;
197
+ } | {
198
+ readonly k: 'error';
199
+ readonly e: ObjectCell;
200
+ } | {
201
+ readonly k: 'await';
202
+ readonly p: Promise<Cell>;
203
+ } | {
204
+ readonly k: 'yield';
205
+ };
206
+ /** A task that evaluates `block`. */
207
+ export declare function newTask(rt: Runtime, s: Series<Cell>, i?: number): Task;
208
+ /** Continues a parked task with the awaited value. */
209
+ export declare function resume(t: Task, v: Cell): void;
210
+ /** Continues a parked task with an error (a rejected promise). */
211
+ export declare function resumeError(t: Task, e: ObjectCell): void;
212
+ /**
213
+ * Runs a task for at most `budget` steps. Returns `yield` when the budget
214
+ * runs out (call again to continue) and `await` when a native parked it.
215
+ */
216
+ export declare function run(t: Task, budget?: number): RunResult;
217
+ /** The file of the innermost code being evaluated, if it was loaded from one. */
218
+ export declare function currentFile(t: Task): string | null;
219
+ export declare function throwError(type: string, id: string, message: string): Step;
220
+ export {};