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,93 @@
1
+ Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter) Inter-Italic[opsz,wght].ttf: Copyright 2016 The Inter Project Authors (https://github.com/rsms/inter)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,10 @@
1
+ import { type Cell, type Series } from './values.ts';
2
+ /** Keys of maps and selects: words by symbol (any word type), the rest strictly. */
3
+ export declare function sameKey(a: Cell, b: Cell): boolean;
4
+ /** Index of the value slot for `key` in a map series, or -1. */
5
+ export declare function mapFind(s: Series<Cell>, key: Cell): number;
6
+ /** Position of the first element equal to `key` (as a key) from `from`, or -1. */
7
+ export declare function blockFind(s: Series<Cell>, from: number, key: Cell): number;
8
+ export declare function pick(cur: Cell, key: Cell): Cell | string;
9
+ /** Sets `key` in `cur` to `v`. Returns null on success, or an error message. */
10
+ export declare function poke(cur: Cell, key: Cell, v: Cell): string | null;
@@ -0,0 +1,210 @@
1
+ // Picking and poking by key: what paths do at each segment, and what the
2
+ // `pick`, `select` and `poke` natives build on.
3
+ //
4
+ // Both return an error message string instead of throwing; the caller turns
5
+ // it into a Trebol error.
6
+ import { K, isAnyBlock, isAnyString, isAnyWord, aKindName } from "./kinds.js";
7
+ import { canon, spell } from "./symbols.js";
8
+ import * as I64 from "./int64.js";
9
+ import { NONE, integer, char, decimal, time, date, strictEqual, codePoints, fromCodePoints, } from "./values.js";
10
+ import { ctxFind, ctxSet } from "./ctx.js";
11
+ import { mold } from "./mold.js";
12
+ /** Keys of maps and selects: words by symbol (any word type), the rest strictly. */
13
+ export function sameKey(a, b) {
14
+ if (isAnyWord(a.t) && isAnyWord(b.t))
15
+ return canon(a.sym) === canon(b.sym);
16
+ return strictEqual(a, b);
17
+ }
18
+ /** Index of the value slot for `key` in a map series, or -1. */
19
+ export function mapFind(s, key) {
20
+ for (let j = 0; j < s.data.length; j += 2)
21
+ if (sameKey(s.data[j], key))
22
+ return j + 1;
23
+ return -1;
24
+ }
25
+ /** Position of the first element equal to `key` (as a key) from `from`, or -1. */
26
+ export function blockFind(s, from, key) {
27
+ for (let j = from; j < s.data.length; j++)
28
+ if (sameKey(s.data[j], key))
29
+ return j;
30
+ return -1;
31
+ }
32
+ /** 0-based offset for a 1-based integer index, or null if it is not one. */
33
+ function offset(key) {
34
+ if (key.t !== K.Integer)
35
+ return null;
36
+ return I64.toNumber(key.v) - 1;
37
+ }
38
+ const DATE_PARTS = ['year', 'month', 'day', 'time', 'zone', 'date', 'weekday', 'yearday', 'hour', 'minute', 'second'];
39
+ const TIME_PARTS = ['hour', 'minute', 'second'];
40
+ const NS_HOUR = 3600e9;
41
+ const NS_MIN = 60e9;
42
+ const DAY_MS = 86400000;
43
+ function partName(key) {
44
+ return isAnyWord(key.t) ? spell(canon(key.sym)) : null;
45
+ }
46
+ export function pick(cur, key) {
47
+ switch (cur.t) {
48
+ case K.Url:
49
+ case K.File:
50
+ // url/segment and file/segment build a longer path
51
+ if (isAnyWord(key.t) || isAnyString(key.t) || key.t === K.Integer) {
52
+ const base = cur.s.data.slice(cur.i);
53
+ const seg = codePoints(isAnyWord(key.t) ? spell(key.sym) : isAnyString(key.t) ? fromCodePoints(key.s.data, key.i) : mold(key));
54
+ const slash = base[base.length - 1] === 47 ? [] : [47];
55
+ const tail = seg[0] === 47 ? seg.slice(1) : seg;
56
+ return { t: cur.t, s: { data: [...base, ...slash, ...tail] }, i: 0 };
57
+ }
58
+ break;
59
+ case K.Email: {
60
+ // email/user and email/host, as in R3
61
+ const p = partName(key);
62
+ if (p === 'user' || p === 'host') {
63
+ const cps = cur.s.data.slice(cur.i);
64
+ const at = cps.indexOf(64);
65
+ const part = p === 'user' ? (at < 0 ? cps : cps.slice(0, at)) : at < 0 ? [] : cps.slice(at + 1);
66
+ return { t: K.String, s: { data: part }, i: 0 };
67
+ }
68
+ break;
69
+ }
70
+ case K.Object:
71
+ case K.Error: {
72
+ if (!isAnyWord(key.t))
73
+ return `cannot pick ${mold(key)} in an object`;
74
+ const i = ctxFind(cur.ctx, key.sym);
75
+ if (i < 0)
76
+ return `${mold(key)} is not a field of the object`;
77
+ return cur.ctx.vals[i];
78
+ }
79
+ case K.Map: {
80
+ const i = mapFind(cur.s, key);
81
+ return i < 0 ? NONE : cur.s.data[i];
82
+ }
83
+ case K.Pair: {
84
+ const p = partName(key);
85
+ const n = offset(key);
86
+ if (p === 'x' || n === 0)
87
+ return decimalOrInt(cur.x);
88
+ if (p === 'y' || n === 1)
89
+ return decimalOrInt(cur.y);
90
+ return `cannot pick ${mold(key)} in a pair`;
91
+ }
92
+ case K.Tuple: {
93
+ const n = offset(key);
94
+ if (n === null)
95
+ return `cannot pick ${mold(key)} in a tuple`;
96
+ const x = cur.v[n];
97
+ return x === undefined ? NONE : integer(x);
98
+ }
99
+ case K.Date: {
100
+ const p = partName(key) ?? DATE_PARTS[offset(key) ?? -1] ?? null;
101
+ const d = cur.v;
102
+ switch (p) {
103
+ case 'year': return integer(d.y);
104
+ case 'month': return integer(d.m);
105
+ case 'day': return integer(d.d);
106
+ case 'time': return d.time === null ? NONE : time(d.time);
107
+ case 'zone': return d.zone === null ? NONE : time(d.zone * NS_MIN);
108
+ case 'date': return date({ y: d.y, m: d.m, d: d.d, time: null, zone: null });
109
+ // ISO weekday: Monday is 1, Sunday 7; 1970-01-01 was a Thursday
110
+ case 'weekday': return integer(((Date.UTC(d.y, d.m - 1, d.d) / DAY_MS) % 7 + 10) % 7 + 1);
111
+ case 'yearday': return integer((Date.UTC(d.y, d.m - 1, d.d) - Date.UTC(d.y, 0, 1)) / DAY_MS + 1);
112
+ case 'hour':
113
+ case 'minute':
114
+ case 'second':
115
+ return d.time === null ? NONE : pick(time(d.time), key.t === K.Integer ? integer(TIME_PARTS.indexOf(p) + 1) : key);
116
+ }
117
+ return `cannot pick ${mold(key)} in a date`;
118
+ }
119
+ case K.Time: {
120
+ const p = partName(key) ?? TIME_PARTS[offset(key) ?? -1] ?? null;
121
+ const ns = Math.abs(cur.v);
122
+ const sign = cur.v < 0 ? -1 : 1;
123
+ switch (p) {
124
+ case 'hour': return integer(sign * Math.floor(ns / NS_HOUR));
125
+ case 'minute': return integer(sign * Math.floor((ns % NS_HOUR) / NS_MIN));
126
+ case 'second': {
127
+ const s = (ns % NS_MIN) / 1e9;
128
+ return Number.isInteger(s) ? integer(sign * s) : decimal(sign * s);
129
+ }
130
+ }
131
+ return `cannot pick ${mold(key)} in a time`;
132
+ }
133
+ }
134
+ if (isAnyBlock(cur.t)) {
135
+ const b = cur;
136
+ const n = offset(key);
137
+ if (n !== null) {
138
+ const v = n >= 0 ? b.s.data[b.i + n] : undefined;
139
+ return v ?? NONE;
140
+ }
141
+ const at = blockFind(b.s, b.i, key);
142
+ return at < 0 ? NONE : b.s.data[at + 1] ?? NONE;
143
+ }
144
+ if (isAnyString(cur.t) || cur.t === K.Binary) {
145
+ const s = cur;
146
+ const n = offset(key);
147
+ if (n === null)
148
+ return `cannot pick ${mold(key)} in ${aKindName(cur.t)}`;
149
+ const cp = n >= 0 ? s.s.data[s.i + n] : undefined;
150
+ if (cp === undefined)
151
+ return NONE;
152
+ return cur.t === K.Binary ? integer(cp) : char(cp);
153
+ }
154
+ return `cannot pick ${mold(key)} in ${aKindName(cur.t)}`;
155
+ }
156
+ function decimalOrInt(n) {
157
+ return Number.isInteger(n) ? integer(n) : decimal(n);
158
+ }
159
+ /** Sets `key` in `cur` to `v`. Returns null on success, or an error message. */
160
+ export function poke(cur, key, v) {
161
+ switch (cur.t) {
162
+ case K.Object:
163
+ case K.Error: {
164
+ if (!isAnyWord(key.t))
165
+ return `cannot set ${mold(key)} in an object`;
166
+ const i = ctxFind(cur.ctx, key.sym);
167
+ if (i < 0)
168
+ return `${mold(key)} is not a field of the object`;
169
+ ctxSet(cur.ctx, i, v);
170
+ return null;
171
+ }
172
+ case K.Map: {
173
+ const i = mapFind(cur.s, key);
174
+ if (i >= 0) {
175
+ cur.s.data[i] = v;
176
+ }
177
+ else {
178
+ const k = isAnyWord(key.t) ? { t: K.Word, sym: key.sym, bind: null } : key;
179
+ cur.s.data.push(k, v);
180
+ }
181
+ return null;
182
+ }
183
+ }
184
+ if (isAnyBlock(cur.t)) {
185
+ const b = cur;
186
+ const n = offset(key);
187
+ if (n !== null) {
188
+ if (n < 0 || b.i + n >= b.s.data.length)
189
+ return `index ${n + 1} is out of range`;
190
+ b.s.data[b.i + n] = v;
191
+ return null;
192
+ }
193
+ const at = blockFind(b.s, b.i, key);
194
+ if (at < 0 || at + 1 >= b.s.data.length)
195
+ return `${mold(key)} not found in the block`;
196
+ b.s.data[at + 1] = v;
197
+ return null;
198
+ }
199
+ if (cur.t === K.String) {
200
+ const s = cur;
201
+ const n = offset(key);
202
+ if (n === null || v.t !== K.Char)
203
+ return `cannot set ${mold(key)} in a string`;
204
+ if (n < 0 || s.i + n >= s.s.data.length)
205
+ return `index ${n + 1} is out of range`;
206
+ s.s.data[s.i + n] = v.v;
207
+ return null;
208
+ }
209
+ return `cannot set ${mold(key)} in ${aKindName(cur.t)}`;
210
+ }
@@ -0,0 +1,4 @@
1
+ /** The bytes the text holds in that base (white space ignored), or null if it is not valid. */
2
+ export declare function decodeBase(text: string, base: number): number[] | null;
3
+ /** The bytes written in base 2, 16 (capitals) or 64 (padded with =). */
4
+ export declare function encodeBase(bytes: readonly number[], base: number): string;
@@ -0,0 +1,50 @@
1
+ // Bytes as text in base 2, 16 and 64, shared by the lexer (2#{...} 16#{...}
2
+ // 64#{...}) and by enbase and debase.
3
+ const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
4
+ /** The bytes the text holds in that base (white space ignored), or null if it is not valid. */
5
+ export function decodeBase(text, base) {
6
+ const clean = text.replace(/\s+/g, '');
7
+ const bytes = [];
8
+ if (base === 16) {
9
+ if (!/^(?:[0-9A-Fa-f]{2})*$/.test(clean))
10
+ return null;
11
+ for (let j = 0; j < clean.length; j += 2)
12
+ bytes.push(parseInt(clean.slice(j, j + 2), 16));
13
+ return bytes;
14
+ }
15
+ if (base === 2) {
16
+ if (!/^(?:[01]{8})*$/.test(clean))
17
+ return null;
18
+ for (let j = 0; j < clean.length; j += 8)
19
+ bytes.push(parseInt(clean.slice(j, j + 8), 2));
20
+ return bytes;
21
+ }
22
+ if (base !== 64 || !/^[A-Za-z0-9+/]*={0,2}$/.test(clean) || clean.length % 4 === 1)
23
+ return null;
24
+ let buf = 0;
25
+ let bits = 0;
26
+ for (const ch of clean.replace(/=+$/, '')) {
27
+ buf = (buf << 6) | B64.indexOf(ch);
28
+ bits += 6;
29
+ if (bits >= 8) {
30
+ bits -= 8;
31
+ bytes.push((buf >> bits) & 0xff);
32
+ }
33
+ }
34
+ return bytes;
35
+ }
36
+ /** The bytes written in base 2, 16 (capitals) or 64 (padded with =). */
37
+ export function encodeBase(bytes, base) {
38
+ if (base === 16)
39
+ return bytes.map(b => b.toString(16).toUpperCase().padStart(2, '0')).join('');
40
+ if (base === 2)
41
+ return bytes.map(b => b.toString(2).padStart(8, '0')).join('');
42
+ let out = '';
43
+ for (let i = 0; i < bytes.length; i += 3) {
44
+ const n = (bytes[i] << 16) | ((bytes[i + 1] ?? 0) << 8) | (bytes[i + 2] ?? 0);
45
+ out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63];
46
+ out += i + 1 < bytes.length ? B64[(n >> 6) & 63] : '=';
47
+ out += i + 2 < bytes.length ? B64[n & 63] : '=';
48
+ }
49
+ return out;
50
+ }
@@ -0,0 +1,9 @@
1
+ import type { Cell, BitsetCell } from './values.ts';
2
+ export declare function bitHas(b: BitsetCell, n: number): boolean;
3
+ export declare function bitSet(bytes: number[], n: number): void;
4
+ /**
5
+ * Adds the members described by a spec: a char, an integer, a string (its
6
+ * chars), a binary (raw bytes, as the bitset data itself), or a block of
7
+ * those where `a - b` is a range. Returns an error message on a bad spec.
8
+ */
9
+ export declare function bitsFrom(spec: Cell, bytes: number[]): string | null;
@@ -0,0 +1,75 @@
1
+ // Bitsets, as in Rebol: bit n is bit (0x80 >> (n & 7)) of byte (n >> 3),
2
+ // so `#{80}` holds code point 0. A negated bitset holds everything its bytes
3
+ // do not, which keeps `complement charset "abc"` small.
4
+ import { K } from "./kinds.js";
5
+ import * as I64 from "./int64.js";
6
+ import { spell } from "./symbols.js";
7
+ export function bitHas(b, n) {
8
+ const byte = b.s.data[n >> 3];
9
+ const on = byte !== undefined && (byte & (0x80 >> (n & 7))) !== 0;
10
+ return on !== b.neg;
11
+ }
12
+ export function bitSet(bytes, n) {
13
+ const i = n >> 3;
14
+ while (bytes.length <= i)
15
+ bytes.push(0);
16
+ bytes[i] |= 0x80 >> (n & 7);
17
+ }
18
+ /**
19
+ * Adds the members described by a spec: a char, an integer, a string (its
20
+ * chars), a binary (raw bytes, as the bitset data itself), or a block of
21
+ * those where `a - b` is a range. Returns an error message on a bad spec.
22
+ */
23
+ export function bitsFrom(spec, bytes) {
24
+ switch (spec.t) {
25
+ case K.Char:
26
+ bitSet(bytes, spec.v);
27
+ return null;
28
+ case K.Integer:
29
+ bitSet(bytes, I64.toNumber(spec.v));
30
+ return null;
31
+ case K.String:
32
+ case K.File:
33
+ case K.Email:
34
+ case K.Url:
35
+ case K.Tag:
36
+ for (const cp of spec.s.data.slice(spec.i))
37
+ bitSet(bytes, cp);
38
+ return null;
39
+ case K.Binary:
40
+ spec.s.data.slice(spec.i).forEach((x, i) => { bytes[i] = (bytes[i] ?? 0) | x; });
41
+ return null;
42
+ case K.Block: {
43
+ const d = spec.s.data.slice(spec.i);
44
+ for (let i = 0; i < d.length; i++) {
45
+ const c = d[i];
46
+ const dash = d[i + 1];
47
+ if (dash !== undefined && dash.t === K.Word && d[i + 2] !== undefined && isDash(dash)) {
48
+ const lo = codeOf(c);
49
+ const hi = codeOf(d[i + 2]);
50
+ if (lo === null || hi === null || hi < lo)
51
+ return 'bad range in bitset spec';
52
+ for (let n = lo; n <= hi; n++)
53
+ bitSet(bytes, n);
54
+ i += 2;
55
+ continue;
56
+ }
57
+ const err = bitsFrom(c, bytes);
58
+ if (err !== null)
59
+ return err;
60
+ }
61
+ return null;
62
+ }
63
+ }
64
+ return 'bad bitset spec';
65
+ }
66
+ function isDash(w) {
67
+ return w.t === K.Word && spell(w.sym) === '-';
68
+ }
69
+ function codeOf(c) {
70
+ if (c.t === K.Char)
71
+ return c.v;
72
+ if (c.t === K.Integer)
73
+ return I64.toNumber(c.v);
74
+ return null;
75
+ }
@@ -0,0 +1,18 @@
1
+ import { type BlockCell } from './values.ts';
2
+ import { type Runtime, type RunResult } from './eval.ts';
3
+ import './natives/control.ts';
4
+ import { scriptStart } from './natives/data.ts';
5
+ export { scriptStart };
6
+ import './natives/math.ts';
7
+ import './natives/series.ts';
8
+ import './natives/text.ts';
9
+ import './natives/encode.ts';
10
+ import './natives/json.ts';
11
+ import './natives/csv.ts';
12
+ import './natives/xml.ts';
13
+ import './natives/parse.ts';
14
+ export declare function makeRuntime(): Runtime;
15
+ /** Runs a block to completion; an `await` becomes an error. */
16
+ export declare function runSync(rt: Runtime, code: BlockCell): RunResult;
17
+ /** Loads source into the user context and runs it to completion. */
18
+ export declare function doSource(rt: Runtime, src: string, file?: string): RunResult;
@@ -0,0 +1,73 @@
1
+ // Startup: builds lib (natives, operators, datatype words, mezzanines) and an
2
+ // empty user context that inherits from it.
3
+ //
4
+ // Hosts register their own natives (print, fetch...) before calling
5
+ // `makeRuntime`; every registered native ends up in lib.
6
+ import { K, KIND_NAMES, TYPESETS } from "./kinds.js";
7
+ import { intern, canon } from "./symbols.js";
8
+ import { NONE, TRUE, FALSE, datatype, typeset, context } from "./values.js";
9
+ import { ctxAdd, ctxFind, bindSeries } from "./ctx.js";
10
+ import { load } from "./lexer.js";
11
+ import { registeredNatives, newTask, run } from "./eval.js";
12
+ import { makeError, errorMessage } from "./errors.js";
13
+ import { OPS } from "./natives/define.js";
14
+ import "./natives/control.js";
15
+ import { scriptStart } from "./natives/data.js";
16
+ export { scriptStart };
17
+ import "./natives/math.js";
18
+ import "./natives/series.js";
19
+ import "./natives/text.js";
20
+ import "./natives/encode.js";
21
+ import "./natives/json.js";
22
+ import "./natives/csv.js";
23
+ import "./natives/xml.js";
24
+ import "./natives/parse.js";
25
+ import { MEZZANINES } from "./mezz.js";
26
+ export function makeRuntime() {
27
+ const lib = context();
28
+ const set = (name, v) => {
29
+ const sym = intern(name);
30
+ const i = ctxFind(lib, sym);
31
+ if (i >= 0)
32
+ lib.vals[i] = v;
33
+ else
34
+ ctxAdd(lib, sym, v);
35
+ };
36
+ for (const [name, v] of [['none', NONE], ['true', TRUE], ['false', FALSE], ['on', TRUE],
37
+ ['off', FALSE], ['yes', TRUE], ['no', FALSE]])
38
+ set(name, v);
39
+ KIND_NAMES.forEach((name, k) => set(name, datatype(k)));
40
+ for (const [name, bits] of Object.entries(TYPESETS))
41
+ set(name, typeset(bits));
42
+ for (const def of registeredNatives())
43
+ if (def.hidden !== true)
44
+ set(def.name, { t: K.Native, p: def });
45
+ for (const [name, fn] of OPS) {
46
+ const i = ctxFind(lib, intern(fn));
47
+ if (i < 0)
48
+ throw new Error(`operator ${name} refers to unknown native ${fn}`);
49
+ set(name, { t: K.Op, p: { fn: lib.vals[i] } });
50
+ }
51
+ const rt = { lib, user: context([], [], lib) };
52
+ const code = load(MEZZANINES, { file: 'mezzanines' });
53
+ bindSeries(code.s, lib, true);
54
+ const r = runSync(rt, code);
55
+ // Mezzanines failing to load is a bug in Trebol itself.
56
+ if (r.k === 'error')
57
+ throw new Error(`mezzanines failed: ${errorMessage(r.e)}`);
58
+ return rt;
59
+ }
60
+ /** Runs a block to completion; an `await` becomes an error. */
61
+ export function runSync(rt, code) {
62
+ const r = run(newTask(rt, code.s, code.i));
63
+ if (r.k === 'await' || r.k === 'yield') {
64
+ return { k: 'error', e: makeError('script', 'no-await', 'cannot wait in synchronous evaluation') };
65
+ }
66
+ return r;
67
+ }
68
+ /** Loads source into the user context and runs it to completion. */
69
+ export function doSource(rt, src, file) {
70
+ const code = load(src, file !== undefined ? { file } : {});
71
+ bindSeries(code.s, rt.user, true);
72
+ return runSync(rt, { ...code, i: scriptStart(code).start });
73
+ }
@@ -0,0 +1,9 @@
1
+ import { type Dec } from './dec.ts';
2
+ import { type Cell } from './values.ts';
3
+ /** An exact decimal for any numeric cell (null for NaN/infinity). */
4
+ export declare function toDec(c: Cell): Dec | null;
5
+ /** Lowercase code point for case-insensitive comparison. */
6
+ export declare function foldCase(cp: number): number;
7
+ export declare function isEqual(a: Cell, b: Cell): boolean;
8
+ /** Ordering for `<`, `>` and sort; null when the two cannot be ordered. */
9
+ export declare function compare(a: Cell, b: Cell): -1 | 0 | 1 | null;