trebol 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/cli.js +55 -20
- package/dist/lib/core/access.d.ts +10 -0
- package/dist/lib/core/access.js +210 -0
- package/dist/lib/core/base.d.ts +4 -0
- package/dist/lib/core/base.js +50 -0
- package/dist/lib/core/bitset.d.ts +9 -0
- package/dist/lib/core/bitset.js +75 -0
- package/dist/lib/core/boot.d.ts +18 -0
- package/dist/lib/core/boot.js +72 -0
- package/dist/lib/core/compare.d.ts +9 -0
- package/dist/lib/core/compare.js +156 -0
- package/dist/lib/core/ctx.d.ts +36 -0
- package/dist/lib/core/ctx.js +251 -0
- package/dist/lib/core/dec.d.ts +28 -0
- package/dist/lib/core/dec.js +158 -0
- package/dist/lib/core/errors.d.ts +15 -0
- package/dist/lib/core/errors.js +44 -0
- package/dist/lib/core/escapes.d.ts +14 -0
- package/dist/lib/core/escapes.js +80 -0
- package/dist/lib/core/eval.d.ts +220 -0
- package/dist/lib/core/eval.js +666 -0
- package/dist/lib/core/func.d.ts +64 -0
- package/dist/lib/core/func.js +129 -0
- package/dist/lib/core/hash.d.ts +5 -0
- package/dist/lib/core/hash.js +155 -0
- package/dist/lib/core/index.d.ts +18 -0
- package/dist/lib/core/index.js +20 -0
- package/dist/lib/core/int64.d.ts +22 -0
- package/dist/lib/core/int64.js +113 -0
- package/dist/lib/core/kinds.d.ts +67 -0
- package/dist/lib/core/kinds.js +134 -0
- package/dist/lib/core/lexer.d.ts +13 -0
- package/dist/lib/core/lexer.js +702 -0
- package/dist/lib/core/mezz.d.ts +1 -0
- package/dist/lib/core/mezz.js +360 -0
- package/dist/lib/core/mold.d.ts +20 -0
- package/dist/lib/core/mold.js +434 -0
- package/dist/lib/core/natives/control.d.ts +1 -0
- package/dist/lib/core/natives/control.js +374 -0
- package/dist/lib/core/natives/csv.d.ts +15 -0
- package/dist/lib/core/natives/csv.js +315 -0
- package/dist/lib/core/natives/data.d.ts +11 -0
- package/dist/lib/core/natives/data.js +367 -0
- package/dist/lib/core/natives/define.d.ts +17 -0
- package/dist/lib/core/natives/define.js +47 -0
- package/dist/lib/core/natives/encode.d.ts +3 -0
- package/dist/lib/core/natives/encode.js +187 -0
- package/dist/lib/core/natives/json.d.ts +5 -0
- package/dist/lib/core/natives/json.js +273 -0
- package/dist/lib/core/natives/math.d.ts +3 -0
- package/dist/lib/core/natives/math.js +500 -0
- package/dist/lib/core/natives/parse.d.ts +1 -0
- package/dist/lib/core/natives/parse.js +600 -0
- package/dist/lib/core/natives/series.d.ts +4 -0
- package/dist/lib/core/natives/series.js +509 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +484 -0
- package/dist/lib/core/natives/xml.d.ts +9 -0
- package/dist/lib/core/natives/xml.js +242 -0
- package/dist/lib/core/series.d.ts +26 -0
- package/dist/lib/core/series.js +73 -0
- package/dist/lib/core/symbols.d.ts +9 -0
- package/dist/lib/core/symbols.js +44 -0
- package/dist/lib/core/values.d.ts +206 -0
- package/dist/lib/core/values.js +223 -0
- package/dist/lib/host/browser.d.ts +4 -0
- package/dist/lib/host/browser.js +4081 -0
- package/dist/lib/host/chart.d.ts +26 -0
- package/dist/lib/host/chart.js +158 -0
- package/dist/lib/host/compress.d.ts +1 -0
- package/dist/lib/host/compress.js +37 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/icons.d.ts +3 -0
- package/dist/lib/host/icons.js +97 -0
- package/dist/lib/host/index.d.ts +38 -0
- package/dist/lib/host/index.js +190 -0
- package/dist/lib/host/locale.d.ts +40 -0
- package/dist/lib/host/locale.js +241 -0
- package/dist/lib/host/markdown.d.ts +28 -0
- package/dist/lib/host/markdown.js +151 -0
- package/dist/lib/host/modules.d.ts +10 -0
- package/dist/lib/host/modules.js +262 -0
- package/dist/lib/host/natives.d.ts +6 -0
- package/dist/lib/host/natives.js +98 -0
- package/dist/lib/host/net.d.ts +12 -0
- package/dist/lib/host/net.js +541 -0
- package/dist/lib/host/page.d.ts +9 -0
- package/dist/lib/host/page.js +55 -0
- package/dist/lib/host/rows.d.ts +15 -0
- package/dist/lib/host/rows.js +80 -0
- package/dist/lib/host/scheduler.d.ts +45 -0
- package/dist/lib/host/scheduler.js +173 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1462 -0
- package/dist/lib/ui/i18n.trb +111 -0
- package/dist/lib/ui/ui.trb +1237 -0
- package/dist/trebol.js +123 -56
- package/package.json +17 -5
- package/src/host/browser.ts +274 -33
- package/src/host/index.ts +2 -2
- package/src/host/modules.ts +8 -1
- package/src/host/theme.ts +63 -4
- package/src/ui/i18n.trb +4 -0
- package/src/ui/ui.trb +48 -15
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// The value model.
|
|
2
|
+
//
|
|
3
|
+
// Every Trebol value is a Cell: a small, fixed-shape, immutable record tagged
|
|
4
|
+
// by `t`. Mutable state lives in Series and Ctx objects, shared by reference,
|
|
5
|
+
// exactly as in Rebol 3. Cells are copied freely and never mutated in place,
|
|
6
|
+
// which keeps the pausable evaluator simple and maps onto a 16-byte tagged
|
|
7
|
+
// union in a future Zig core.
|
|
8
|
+
import { K } from "./kinds.js";
|
|
9
|
+
import { intern, sameSym } from "./symbols.js";
|
|
10
|
+
import { decEqual } from "./dec.js";
|
|
11
|
+
/** Gives a series its own `nl` and `pos` before they are changed. Call it before every edit of them. */
|
|
12
|
+
export function ownMeta(s) {
|
|
13
|
+
if (s.sharedMeta !== true)
|
|
14
|
+
return;
|
|
15
|
+
if (s.nl)
|
|
16
|
+
s.nl = s.nl.slice();
|
|
17
|
+
if (s.pos)
|
|
18
|
+
s.pos = s.pos.slice();
|
|
19
|
+
s.sharedMeta = undefined;
|
|
20
|
+
}
|
|
21
|
+
// --- constructors ----------------------------------------------------------
|
|
22
|
+
export const UNSET = { t: K.Unset };
|
|
23
|
+
export const NONE = { t: K.None };
|
|
24
|
+
export const TRUE = { t: K.Logic, v: true };
|
|
25
|
+
export const FALSE = { t: K.Logic, v: false };
|
|
26
|
+
export const logic = (v) => (v ? TRUE : FALSE);
|
|
27
|
+
export const integer = (v) => ({ t: K.Integer, v });
|
|
28
|
+
export const decimal = (v) => ({ t: K.Decimal, v });
|
|
29
|
+
export const percent = (v) => ({ t: K.Percent, v });
|
|
30
|
+
export const money = (v, cur = '') => ({ t: K.Money, v, cur });
|
|
31
|
+
export const char = (v) => ({ t: K.Char, v });
|
|
32
|
+
export const pair = (x, y) => ({ t: K.Pair, x, y });
|
|
33
|
+
export const tuple = (v) => ({ t: K.Tuple, v: [...v] });
|
|
34
|
+
export const date = (v) => ({ t: K.Date, v: { ...v } });
|
|
35
|
+
export const time = (ns) => ({ t: K.Time, v: ns });
|
|
36
|
+
export const datatype = (k) => ({ t: K.Datatype, k });
|
|
37
|
+
export const typeset = (bits) => ({ t: K.Typeset, bits });
|
|
38
|
+
/** Converts a JS string to an array of code points. */
|
|
39
|
+
export function codePoints(s) {
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const ch of s)
|
|
42
|
+
out.push(ch.codePointAt(0));
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
export function series(data) {
|
|
46
|
+
return { data };
|
|
47
|
+
}
|
|
48
|
+
export function stringOf(t, text) {
|
|
49
|
+
return { t, s: series(codePoints(text)), i: 0 };
|
|
50
|
+
}
|
|
51
|
+
export const string = (text) => stringOf(K.String, text);
|
|
52
|
+
export function binary(bytes) {
|
|
53
|
+
return { t: K.Binary, s: series(bytes), i: 0 };
|
|
54
|
+
}
|
|
55
|
+
export function bitset(bytes, neg = false) {
|
|
56
|
+
return { t: K.Bitset, s: series(bytes), neg };
|
|
57
|
+
}
|
|
58
|
+
export function wordOf(t, name, bind = null) {
|
|
59
|
+
return { t, sym: typeof name === 'string' ? intern(name) : name, bind };
|
|
60
|
+
}
|
|
61
|
+
export const word = (name) => wordOf(K.Word, name);
|
|
62
|
+
export function blockOf(t, cells, i = 0) {
|
|
63
|
+
return { t, s: series(cells), i };
|
|
64
|
+
}
|
|
65
|
+
export const block = (cells) => blockOf(K.Block, cells);
|
|
66
|
+
export function map(pairs) {
|
|
67
|
+
return { t: K.Map, s: series(pairs) };
|
|
68
|
+
}
|
|
69
|
+
export function object(ctx) {
|
|
70
|
+
return { t: K.Object, ctx };
|
|
71
|
+
}
|
|
72
|
+
export function context(keys = [], vals = [], parent = null) {
|
|
73
|
+
return { keys, vals, parent };
|
|
74
|
+
}
|
|
75
|
+
// --- accessors -------------------------------------------------------------
|
|
76
|
+
/** The JS string for a string-like cell, from its index to the tail. */
|
|
77
|
+
export function text(c) {
|
|
78
|
+
return fromCodePoints(c.s.data, c.i);
|
|
79
|
+
}
|
|
80
|
+
/** Builds a JS string from code points, in chunks to stay off the JS stack limit. */
|
|
81
|
+
export function fromCodePoints(cps, start = 0, end = cps.length) {
|
|
82
|
+
let out = '';
|
|
83
|
+
for (let i = start; i < end; i += 8192) {
|
|
84
|
+
out += String.fromCodePoint(...cps.slice(i, Math.min(i + 8192, end)));
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
/** Packs a source position into one number (line < 2^32, column < 2^20). */
|
|
89
|
+
export function packPos(line, col) {
|
|
90
|
+
return line * 1048576 + col;
|
|
91
|
+
}
|
|
92
|
+
export function unpackPos(p) {
|
|
93
|
+
return { line: Math.floor(p / 1048576), col: p % 1048576 };
|
|
94
|
+
}
|
|
95
|
+
// --- strict structural equality ------------------------------------------
|
|
96
|
+
//
|
|
97
|
+
// Same kind and same contents; strings and word spellings compare by case.
|
|
98
|
+
// Used by tests and as the base for `==` (strict-equal?). The looser `=`
|
|
99
|
+
// (equal?) arrives with the natives.
|
|
100
|
+
/** True if two cells are the very same value: the same series at the same position, the same context. */
|
|
101
|
+
export function sameValue(a, b) {
|
|
102
|
+
if (a.t !== b.t)
|
|
103
|
+
return false;
|
|
104
|
+
const x = a;
|
|
105
|
+
const y = b;
|
|
106
|
+
if (x.ctx !== undefined)
|
|
107
|
+
return x.ctx === y.ctx;
|
|
108
|
+
if (x.s !== undefined)
|
|
109
|
+
return x.s === y.s && x.i === y.i;
|
|
110
|
+
return strictEqual(a, b);
|
|
111
|
+
}
|
|
112
|
+
export function strictEqual(a, b) {
|
|
113
|
+
return eq(a, b, new Set());
|
|
114
|
+
}
|
|
115
|
+
function eq(a, b, seen) {
|
|
116
|
+
if (a === b)
|
|
117
|
+
return true;
|
|
118
|
+
if (a.t !== b.t)
|
|
119
|
+
return false;
|
|
120
|
+
switch (a.t) {
|
|
121
|
+
case K.Unset:
|
|
122
|
+
case K.None:
|
|
123
|
+
return true;
|
|
124
|
+
case K.Logic:
|
|
125
|
+
case K.Integer:
|
|
126
|
+
case K.Char:
|
|
127
|
+
case K.Time:
|
|
128
|
+
return a.v === b.v;
|
|
129
|
+
case K.Decimal:
|
|
130
|
+
case K.Percent:
|
|
131
|
+
return Object.is(a.v, b.v) || a.v === b.v;
|
|
132
|
+
case K.Money: {
|
|
133
|
+
const o = b;
|
|
134
|
+
return a.cur.toUpperCase() === o.cur.toUpperCase() && decEqual(a.v, o.v);
|
|
135
|
+
}
|
|
136
|
+
case K.Pair: {
|
|
137
|
+
const o = b;
|
|
138
|
+
return a.x === o.x && a.y === o.y;
|
|
139
|
+
}
|
|
140
|
+
case K.Tuple: {
|
|
141
|
+
const o = b;
|
|
142
|
+
return a.v.length === o.v.length && a.v.every((x, i) => x === o.v[i]);
|
|
143
|
+
}
|
|
144
|
+
case K.Date: {
|
|
145
|
+
const x = a.v;
|
|
146
|
+
const y = b.v;
|
|
147
|
+
return x.y === y.y && x.m === y.m && x.d === y.d && x.time === y.time && x.zone === y.zone;
|
|
148
|
+
}
|
|
149
|
+
case K.String:
|
|
150
|
+
case K.File:
|
|
151
|
+
case K.Email:
|
|
152
|
+
case K.Url:
|
|
153
|
+
case K.Tag:
|
|
154
|
+
case K.Binary: {
|
|
155
|
+
const o = b;
|
|
156
|
+
return arraysEqual(a.s.data, a.i, o.s.data, o.i);
|
|
157
|
+
}
|
|
158
|
+
case K.Bitset: {
|
|
159
|
+
const o = b;
|
|
160
|
+
return a.neg === o.neg && arraysEqual(a.s.data, 0, o.s.data, 0);
|
|
161
|
+
}
|
|
162
|
+
case K.Word:
|
|
163
|
+
case K.SetWord:
|
|
164
|
+
case K.GetWord:
|
|
165
|
+
case K.LitWord:
|
|
166
|
+
case K.Refinement:
|
|
167
|
+
case K.Issue:
|
|
168
|
+
return a.sym === b.sym;
|
|
169
|
+
case K.Block:
|
|
170
|
+
case K.Paren:
|
|
171
|
+
case K.Path:
|
|
172
|
+
case K.SetPath:
|
|
173
|
+
case K.GetPath:
|
|
174
|
+
case K.LitPath: {
|
|
175
|
+
const o = b;
|
|
176
|
+
return cellsEqual(a.s, a.i, o.s, o.i, seen);
|
|
177
|
+
}
|
|
178
|
+
case K.Map:
|
|
179
|
+
return cellsEqual(a.s, 0, b.s, 0, seen);
|
|
180
|
+
case K.Object:
|
|
181
|
+
case K.Error: {
|
|
182
|
+
const x = a.ctx;
|
|
183
|
+
const y = b.ctx;
|
|
184
|
+
if (x === y)
|
|
185
|
+
return true;
|
|
186
|
+
if (x.keys.length !== y.keys.length)
|
|
187
|
+
return false;
|
|
188
|
+
if (seen.has(x))
|
|
189
|
+
return true;
|
|
190
|
+
seen.add(x);
|
|
191
|
+
return x.keys.every((k, i) => sameSym(k, y.keys[i]) && eq(x.vals[i], y.vals[i], seen));
|
|
192
|
+
}
|
|
193
|
+
case K.Datatype:
|
|
194
|
+
return a.k === b.k;
|
|
195
|
+
case K.Typeset:
|
|
196
|
+
return a.bits === b.bits;
|
|
197
|
+
default:
|
|
198
|
+
return a === b || a.p === b.p;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function arraysEqual(x, i, y, j) {
|
|
202
|
+
if (x.length - i !== y.length - j)
|
|
203
|
+
return false;
|
|
204
|
+
for (let n = 0; n < x.length - i; n++)
|
|
205
|
+
if (x[i + n] !== y[j + n])
|
|
206
|
+
return false;
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
function cellsEqual(x, i, y, j, seen) {
|
|
210
|
+
if (x === y && i === j)
|
|
211
|
+
return true;
|
|
212
|
+
if (x.data.length - i !== y.data.length - j)
|
|
213
|
+
return false;
|
|
214
|
+
if (seen.has(x))
|
|
215
|
+
return true; // cyclic: assume equal at the cycle
|
|
216
|
+
seen.add(x);
|
|
217
|
+
for (let n = 0; n < x.data.length - i; n++) {
|
|
218
|
+
if (!eq(x.data[i + n], y.data[j + n], seen))
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
seen.delete(x);
|
|
222
|
+
return true;
|
|
223
|
+
}
|