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.
- package/README.md +18 -3
- package/dist/cli.js +13790 -11417
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- 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 +73 -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 +68 -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 +519 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +487 -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 +4974 -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/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools.d.ts +23 -0
- package/dist/lib/host/devtools.js +626 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +29 -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 +40 -0
- package/dist/lib/host/index.js +192 -0
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -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 +30 -0
- package/dist/lib/host/markdown.js +161 -0
- package/dist/lib/host/modules.d.ts +14 -0
- package/dist/lib/host/modules.js +300 -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 +549 -0
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -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 +29 -0
- package/dist/lib/host/rows.js +109 -0
- package/dist/lib/host/scheduler.d.ts +51 -0
- package/dist/lib/host/scheduler.js +197 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1667 -0
- package/dist/lib/ui/i18n.trb +117 -0
- package/dist/lib/ui/test.trb +565 -0
- package/dist/lib/ui/ui.trb +1678 -0
- package/dist/trebol.js +1427 -138
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +23 -7
- package/src/core/boot.ts +1 -1
- package/src/core/func.ts +4 -0
- package/src/core/natives/control.ts +1 -1
- package/src/core/natives/series.ts +9 -1
- package/src/core/natives/text.ts +2 -0
- package/src/host/browser.ts +1235 -83
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools.ts +641 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +39 -0
- package/src/host/index.ts +5 -3
- package/src/host/js.ts +127 -0
- package/src/host/markdown.ts +9 -0
- package/src/host/modules.ts +41 -2
- package/src/host/net.ts +9 -1
- package/src/host/orbit.ts +432 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +62 -36
- package/src/host/theme.ts +270 -6
- package/src/ui/i18n.trb +10 -0
- package/src/ui/test.trb +565 -0
- package/src/ui/ui.trb +564 -90
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Sym } from './symbols.ts';
|
|
2
|
+
import { type BlockCell, type Cell, type Ctx, type Series } from './values.ts';
|
|
3
|
+
/** Parameter kinds. */
|
|
4
|
+
export declare const P: {
|
|
5
|
+
/** evaluated argument: `value` */
|
|
6
|
+
readonly Arg: 0;
|
|
7
|
+
/** quoted argument: `'value` (parens and get-words are still evaluated) */
|
|
8
|
+
readonly Lit: 1;
|
|
9
|
+
/** literal argument: `:value`, taken as is */
|
|
10
|
+
readonly Get: 2;
|
|
11
|
+
/** refinement: `/only`; true when used, none otherwise */
|
|
12
|
+
readonly Ref: 3;
|
|
13
|
+
/** local: after `/local`, starts as none */
|
|
14
|
+
readonly Local: 4;
|
|
15
|
+
};
|
|
16
|
+
export type PKind = (typeof P)[keyof typeof P];
|
|
17
|
+
export interface Param {
|
|
18
|
+
readonly sym: Sym;
|
|
19
|
+
readonly kind: PKind;
|
|
20
|
+
/** Allowed kinds as a typeset bitmask. */
|
|
21
|
+
readonly types: bigint;
|
|
22
|
+
/** The same, indexed by kind: `allows[k]`. */
|
|
23
|
+
readonly allows: readonly boolean[];
|
|
24
|
+
/** Index of the refinement this argument belongs to, or -1. */
|
|
25
|
+
readonly ref: number;
|
|
26
|
+
}
|
|
27
|
+
export interface Spec {
|
|
28
|
+
readonly params: readonly Param[];
|
|
29
|
+
/** Indexes of the arguments every call takes, in order. */
|
|
30
|
+
readonly base: readonly number[];
|
|
31
|
+
/** Allowed return kinds, or null when unchecked. */
|
|
32
|
+
readonly ret: bigint | null;
|
|
33
|
+
/** The same, indexed by kind. */
|
|
34
|
+
readonly retAllows: readonly boolean[] | null;
|
|
35
|
+
/** A frame's values before a call fills them: none for every parameter. */
|
|
36
|
+
readonly blank: readonly Cell[];
|
|
37
|
+
}
|
|
38
|
+
/** The payload of a function! cell. */
|
|
39
|
+
export interface FuncDef {
|
|
40
|
+
readonly spec: Spec;
|
|
41
|
+
/** The spec as written, for mold and help. */
|
|
42
|
+
readonly specBlock: BlockCell;
|
|
43
|
+
/** Body bound to `tmpl`; copied and rebound for every call. */
|
|
44
|
+
readonly body: Series<Cell>;
|
|
45
|
+
readonly tmpl: Ctx;
|
|
46
|
+
}
|
|
47
|
+
/** The payload of a native! cell: an index into the native table. */
|
|
48
|
+
export interface NativeDef {
|
|
49
|
+
readonly spec: Spec;
|
|
50
|
+
/** The spec as written, for help. */
|
|
51
|
+
readonly specBlock: BlockCell;
|
|
52
|
+
readonly id: number;
|
|
53
|
+
readonly name: string;
|
|
54
|
+
/** Explicit data for natives the host makes after boot, such as which JavaScript function a call runs. */
|
|
55
|
+
readonly data?: number;
|
|
56
|
+
/** Kept out of lib: only the host hands out natives built on it. */
|
|
57
|
+
readonly hidden?: boolean;
|
|
58
|
+
}
|
|
59
|
+
/** The payload of an op! cell: the two-argument function it calls. */
|
|
60
|
+
export interface OpDef {
|
|
61
|
+
readonly fn: Cell;
|
|
62
|
+
}
|
|
63
|
+
/** Parses a spec block. Returns an error message on a malformed spec. */
|
|
64
|
+
export declare function parseSpec(b: BlockCell): Spec | string;
|
|
65
|
+
/** The typeset of a block of datatype names, e.g. [integer! none!]. */
|
|
66
|
+
export declare function typesOf(b: BlockCell): bigint | string;
|
|
67
|
+
/** Makes a function from a spec and a body. Both are copied. */
|
|
68
|
+
export declare function makeFunc(specBlock: BlockCell, bodyBlock: BlockCell): FuncDef | string;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Function specs and function values.
|
|
2
|
+
//
|
|
3
|
+
// A spec block such as
|
|
4
|
+
//
|
|
5
|
+
// [series [series!] value "the value" /only /part length [integer!] return: [series!] /local x]
|
|
6
|
+
//
|
|
7
|
+
// is parsed once into a flat parameter list. That list is also the layout of
|
|
8
|
+
// the frame context of every call: arguments, refinements, refinement
|
|
9
|
+
// arguments and locals, in spec order. Natives and functions share it.
|
|
10
|
+
//
|
|
11
|
+
// A function keeps its body bound to a template context with the frame's
|
|
12
|
+
// keys. Each call makes a fresh context and a deep copy of the body rebound
|
|
13
|
+
// to it (see `copyDeep`), so frames are never shared between calls or tasks.
|
|
14
|
+
import { K, kindBit, kindTable, typeBitsByName, TYPESETS } from "./kinds.js";
|
|
15
|
+
import { canon, intern, spell } from "./symbols.js";
|
|
16
|
+
import { NONE, context } from "./values.js";
|
|
17
|
+
import { bindSeries, copyDeep } from "./ctx.js";
|
|
18
|
+
/** Parameter kinds. */
|
|
19
|
+
export const P = {
|
|
20
|
+
/** evaluated argument: `value` */
|
|
21
|
+
Arg: 0,
|
|
22
|
+
/** quoted argument: `'value` (parens and get-words are still evaluated) */
|
|
23
|
+
Lit: 1,
|
|
24
|
+
/** literal argument: `:value`, taken as is */
|
|
25
|
+
Get: 2,
|
|
26
|
+
/** refinement: `/only`; true when used, none otherwise */
|
|
27
|
+
Ref: 3,
|
|
28
|
+
/** local: after `/local`, starts as none */
|
|
29
|
+
Local: 4,
|
|
30
|
+
};
|
|
31
|
+
const DEFAULT_TYPES = TYPESETS['any-value!'];
|
|
32
|
+
const LOGIC_TYPES = kindBit(K.Logic) | kindBit(K.None);
|
|
33
|
+
const LOCAL = intern('local');
|
|
34
|
+
const RETURN = intern('return');
|
|
35
|
+
/** Parses a spec block. Returns an error message on a malformed spec. */
|
|
36
|
+
export function parseSpec(b) {
|
|
37
|
+
const params = [];
|
|
38
|
+
const base = [];
|
|
39
|
+
let ret = null;
|
|
40
|
+
let ref = -1;
|
|
41
|
+
let local = false;
|
|
42
|
+
const data = b.s.data;
|
|
43
|
+
for (let i = b.i; i < data.length; i++) {
|
|
44
|
+
const c = data[i];
|
|
45
|
+
switch (c.t) {
|
|
46
|
+
case K.String:
|
|
47
|
+
continue; // documentation
|
|
48
|
+
case K.Word:
|
|
49
|
+
case K.LitWord:
|
|
50
|
+
case K.GetWord: {
|
|
51
|
+
if (params.some(p => canon(p.sym) === canon(c.sym)))
|
|
52
|
+
return `duplicate parameter ${spell(c.sym)}`;
|
|
53
|
+
const kind = local ? P.Local : c.t === K.Word ? P.Arg : c.t === K.LitWord ? P.Lit : P.Get;
|
|
54
|
+
if (local && c.t !== K.Word)
|
|
55
|
+
return `bad local ${spell(c.sym)}`;
|
|
56
|
+
let types = DEFAULT_TYPES;
|
|
57
|
+
const next = data[i + 1];
|
|
58
|
+
if (!local && next !== undefined && next.t === K.Block) {
|
|
59
|
+
const bits = typesOf(next);
|
|
60
|
+
if (typeof bits === 'string')
|
|
61
|
+
return bits;
|
|
62
|
+
types = bits;
|
|
63
|
+
i++;
|
|
64
|
+
}
|
|
65
|
+
if (kind !== P.Local && ref < 0)
|
|
66
|
+
base.push(params.length);
|
|
67
|
+
params.push({ sym: c.sym, kind, types, allows: kindTable(types), ref: local ? -1 : ref });
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
case K.Refinement: {
|
|
71
|
+
if (canon(c.sym) === canon(LOCAL)) {
|
|
72
|
+
local = true;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (local)
|
|
76
|
+
return `refinement /${spell(c.sym)} after /local`;
|
|
77
|
+
if (params.some(p => canon(p.sym) === canon(c.sym)))
|
|
78
|
+
return `duplicate parameter ${spell(c.sym)}`;
|
|
79
|
+
ref = params.length;
|
|
80
|
+
params.push({ sym: c.sym, kind: P.Ref, types: LOGIC_TYPES, allows: kindTable(LOGIC_TYPES), ref: -1 });
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
case K.SetWord: {
|
|
84
|
+
const next = data[i + 1];
|
|
85
|
+
if (canon(c.sym) !== canon(RETURN) || next === undefined || next.t !== K.Block) {
|
|
86
|
+
return `bad spec item ${spell(c.sym)}:`;
|
|
87
|
+
}
|
|
88
|
+
const bits = typesOf(next);
|
|
89
|
+
if (typeof bits === 'string')
|
|
90
|
+
return bits;
|
|
91
|
+
ret = bits;
|
|
92
|
+
i++;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return 'bad spec item';
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { params, base, ret, retAllows: ret === null ? null : kindTable(ret), blank: params.map(() => NONE) };
|
|
100
|
+
}
|
|
101
|
+
/** The typeset of a block of datatype names, e.g. [integer! none!]. */
|
|
102
|
+
export function typesOf(b) {
|
|
103
|
+
let bits = 0n;
|
|
104
|
+
for (const w of b.s.data.slice(b.i)) {
|
|
105
|
+
const found = w.t === K.Word ? typeBitsByName(spell(w.sym))
|
|
106
|
+
: w.t === K.Datatype ? kindBit(w.k)
|
|
107
|
+
: w.t === K.Typeset ? w.bits
|
|
108
|
+
: undefined;
|
|
109
|
+
if (found === undefined)
|
|
110
|
+
return 'bad type in spec';
|
|
111
|
+
bits |= found;
|
|
112
|
+
}
|
|
113
|
+
return bits;
|
|
114
|
+
}
|
|
115
|
+
/** Makes a function from a spec and a body. Both are copied. */
|
|
116
|
+
export function makeFunc(specBlock, bodyBlock) {
|
|
117
|
+
const spec = parseSpec(specBlock);
|
|
118
|
+
if (typeof spec === 'string')
|
|
119
|
+
return spec;
|
|
120
|
+
const tmpl = context(spec.params.map(p => p.sym));
|
|
121
|
+
const body = copyDeep(bodyBlock.s, bodyBlock.i);
|
|
122
|
+
bindSeries(body, tmpl);
|
|
123
|
+
return {
|
|
124
|
+
spec,
|
|
125
|
+
specBlock: { t: K.Block, s: copyDeep(specBlock.s, specBlock.i), i: 0 },
|
|
126
|
+
body,
|
|
127
|
+
tmpl,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function crc32(bytes: readonly number[]): number;
|
|
2
|
+
export declare function adler32(bytes: readonly number[]): number;
|
|
3
|
+
export declare function md5(bytes: readonly number[]): number[];
|
|
4
|
+
export declare function sha1(bytes: readonly number[]): number[];
|
|
5
|
+
export declare function sha256(bytes: readonly number[]): number[];
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Checksums and hashes over bytes, written out so the core needs no host
|
|
2
|
+
// crypto: CRC-32, Adler-32, MD5, SHA-1 and SHA-256. Hashes give their
|
|
3
|
+
// digest as bytes; CRC-32 and Adler-32 give a number.
|
|
4
|
+
export function crc32(bytes) {
|
|
5
|
+
let c = 0xffffffff;
|
|
6
|
+
for (const b of bytes) {
|
|
7
|
+
c ^= b;
|
|
8
|
+
for (let k = 0; k < 8; k++)
|
|
9
|
+
c = c & 1 ? (c >>> 1) ^ 0xedb88320 : c >>> 1;
|
|
10
|
+
}
|
|
11
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
12
|
+
}
|
|
13
|
+
export function adler32(bytes) {
|
|
14
|
+
let a = 1;
|
|
15
|
+
let b = 0;
|
|
16
|
+
for (const x of bytes) {
|
|
17
|
+
a = (a + x) % 65521;
|
|
18
|
+
b = (b + a) % 65521;
|
|
19
|
+
}
|
|
20
|
+
return ((b << 16) | a) >>> 0;
|
|
21
|
+
}
|
|
22
|
+
/** The message padded to whole 64-byte blocks, with its bit length at the end. */
|
|
23
|
+
function pad(bytes, bigEndian) {
|
|
24
|
+
const m = bytes.slice();
|
|
25
|
+
const bits = bytes.length * 8;
|
|
26
|
+
m.push(0x80);
|
|
27
|
+
while (m.length % 64 !== 56)
|
|
28
|
+
m.push(0);
|
|
29
|
+
const len = [];
|
|
30
|
+
for (let k = 0; k < 8; k++)
|
|
31
|
+
len.push(Math.floor(bits / 2 ** (8 * k)) & 255);
|
|
32
|
+
m.push(...(bigEndian ? len.reverse() : len));
|
|
33
|
+
return m;
|
|
34
|
+
}
|
|
35
|
+
function wordsOf(m, at, bigEndian) {
|
|
36
|
+
const w = [];
|
|
37
|
+
for (let k = 0; k < 16; k++) {
|
|
38
|
+
const i = at + k * 4;
|
|
39
|
+
w.push(bigEndian
|
|
40
|
+
? ((m[i] << 24) | (m[i + 1] << 16) | (m[i + 2] << 8) | m[i + 3]) >>> 0
|
|
41
|
+
: (m[i] | (m[i + 1] << 8) | (m[i + 2] << 16) | (m[i + 3] << 24)) >>> 0);
|
|
42
|
+
}
|
|
43
|
+
return w;
|
|
44
|
+
}
|
|
45
|
+
function bytesOf(words, bigEndian) {
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const w of words) {
|
|
48
|
+
const b = [(w >>> 24) & 255, (w >>> 16) & 255, (w >>> 8) & 255, w & 255];
|
|
49
|
+
out.push(...(bigEndian ? b : b.reverse()));
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
const rotl = (x, n) => ((x << n) | (x >>> (32 - n))) >>> 0;
|
|
54
|
+
const rotr = (x, n) => ((x >>> n) | (x << (32 - n))) >>> 0;
|
|
55
|
+
const MD5_S = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
|
|
56
|
+
const MD5_K = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
|
|
57
|
+
export function md5(bytes) {
|
|
58
|
+
const m = pad(bytes, false);
|
|
59
|
+
const h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
|
|
60
|
+
for (let at = 0; at < m.length; at += 64) {
|
|
61
|
+
const w = wordsOf(m, at, false);
|
|
62
|
+
let [a, b, c, d] = h;
|
|
63
|
+
for (let i = 0; i < 64; i++) {
|
|
64
|
+
let f;
|
|
65
|
+
let g;
|
|
66
|
+
if (i < 16) {
|
|
67
|
+
f = (b & c) | (~b & d);
|
|
68
|
+
g = i;
|
|
69
|
+
}
|
|
70
|
+
else if (i < 32) {
|
|
71
|
+
f = (d & b) | (~d & c);
|
|
72
|
+
g = (5 * i + 1) % 16;
|
|
73
|
+
}
|
|
74
|
+
else if (i < 48) {
|
|
75
|
+
f = b ^ c ^ d;
|
|
76
|
+
g = (3 * i + 5) % 16;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
f = c ^ (b | ~d);
|
|
80
|
+
g = (7 * i) % 16;
|
|
81
|
+
}
|
|
82
|
+
const t = d;
|
|
83
|
+
d = c;
|
|
84
|
+
c = b;
|
|
85
|
+
b = (b + rotl((a + f + MD5_K[i] + w[g]) >>> 0, MD5_S[(i >> 4) * 4 + (i % 4)])) >>> 0;
|
|
86
|
+
a = t;
|
|
87
|
+
}
|
|
88
|
+
h[0] = (h[0] + a) >>> 0;
|
|
89
|
+
h[1] = (h[1] + b) >>> 0;
|
|
90
|
+
h[2] = (h[2] + c) >>> 0;
|
|
91
|
+
h[3] = (h[3] + d) >>> 0;
|
|
92
|
+
}
|
|
93
|
+
return bytesOf(h, false);
|
|
94
|
+
}
|
|
95
|
+
export function sha1(bytes) {
|
|
96
|
+
const m = pad(bytes, true);
|
|
97
|
+
const h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
|
98
|
+
for (let at = 0; at < m.length; at += 64) {
|
|
99
|
+
const w = wordsOf(m, at, true);
|
|
100
|
+
for (let i = 16; i < 80; i++)
|
|
101
|
+
w.push(rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1));
|
|
102
|
+
let [a, b, c, d, e] = h;
|
|
103
|
+
for (let i = 0; i < 80; i++) {
|
|
104
|
+
const [f, k] = i < 20 ? [(b & c) | (~b & d), 0x5a827999]
|
|
105
|
+
: i < 40 ? [b ^ c ^ d, 0x6ed9eba1]
|
|
106
|
+
: i < 60 ? [(b & c) | (b & d) | (c & d), 0x8f1bbcdc]
|
|
107
|
+
: [b ^ c ^ d, 0xca62c1d6];
|
|
108
|
+
const t = (rotl(a, 5) + f + e + k + w[i]) >>> 0;
|
|
109
|
+
e = d;
|
|
110
|
+
d = c;
|
|
111
|
+
c = rotl(b, 30);
|
|
112
|
+
b = a;
|
|
113
|
+
a = t;
|
|
114
|
+
}
|
|
115
|
+
[a, b, c, d, e].forEach((x, i) => { h[i] = (h[i] + x) >>> 0; });
|
|
116
|
+
}
|
|
117
|
+
return bytesOf(h, true);
|
|
118
|
+
}
|
|
119
|
+
const SHA256_K = [
|
|
120
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
121
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
122
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
123
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
124
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
125
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
126
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
127
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
128
|
+
];
|
|
129
|
+
export function sha256(bytes) {
|
|
130
|
+
const m = pad(bytes, true);
|
|
131
|
+
const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
132
|
+
for (let at = 0; at < m.length; at += 64) {
|
|
133
|
+
const w = wordsOf(m, at, true);
|
|
134
|
+
for (let i = 16; i < 64; i++) {
|
|
135
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
136
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
137
|
+
w.push((w[i - 16] + s0 + w[i - 7] + s1) >>> 0);
|
|
138
|
+
}
|
|
139
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
140
|
+
for (let i = 0; i < 64; i++) {
|
|
141
|
+
const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + SHA256_K[i] + w[i]) >>> 0;
|
|
142
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
143
|
+
hh = g;
|
|
144
|
+
g = f;
|
|
145
|
+
f = e;
|
|
146
|
+
e = (d + t1) >>> 0;
|
|
147
|
+
d = c;
|
|
148
|
+
c = b;
|
|
149
|
+
b = a;
|
|
150
|
+
a = (t1 + t2) >>> 0;
|
|
151
|
+
}
|
|
152
|
+
[a, b, c, d, e, f, g, hh].forEach((x, i) => { h[i] = (h[i] + x) >>> 0; });
|
|
153
|
+
}
|
|
154
|
+
return bytesOf(h, true);
|
|
155
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './kinds.ts';
|
|
2
|
+
export * from './symbols.ts';
|
|
3
|
+
export * as I64 from './int64.ts';
|
|
4
|
+
export * from './dec.ts';
|
|
5
|
+
export * from './values.ts';
|
|
6
|
+
export { load, LoadError, type LoadOptions } from './lexer.ts';
|
|
7
|
+
export { mold, form, formatDecimal, formatDate, formatTime, sourceOf, type MoldOptions } from './mold.ts';
|
|
8
|
+
export * from './ctx.ts';
|
|
9
|
+
export * from './errors.ts';
|
|
10
|
+
export * from './func.ts';
|
|
11
|
+
export * from './compare.ts';
|
|
12
|
+
export { pick, poke } from './access.ts';
|
|
13
|
+
export { splice } from './series.ts';
|
|
14
|
+
export { T, F, run, resume, resumeError, newTask, registerNative, throwError, currentFile, type Task, type Runtime, type RunResult, type Step, type Thrown, type Feed, type NativeFrame, type NativeFn, LIBRARY_FILES, } from './eval.ts';
|
|
15
|
+
export { native, op, value, truthy } from './natives/define.ts';
|
|
16
|
+
export { makeRuntime, runSync, doSource, scriptStart } from './boot.ts';
|
|
17
|
+
export { parseJson, toJson } from './natives/json.ts';
|
|
18
|
+
export { queryText } from './natives/encode.ts';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Public surface of the Trebol core. The core knows nothing about the DOM or
|
|
2
|
+
// the browser; hosts (browser, Node, desktop) build on top of it.
|
|
3
|
+
export * from "./kinds.js";
|
|
4
|
+
export * from "./symbols.js";
|
|
5
|
+
export * as I64 from "./int64.js";
|
|
6
|
+
export * from "./dec.js";
|
|
7
|
+
export * from "./values.js";
|
|
8
|
+
export { load, LoadError } from "./lexer.js";
|
|
9
|
+
export { mold, form, formatDecimal, formatDate, formatTime, sourceOf } from "./mold.js";
|
|
10
|
+
export * from "./ctx.js";
|
|
11
|
+
export * from "./errors.js";
|
|
12
|
+
export * from "./func.js";
|
|
13
|
+
export * from "./compare.js";
|
|
14
|
+
export { pick, poke } from "./access.js";
|
|
15
|
+
export { splice } from "./series.js";
|
|
16
|
+
export { T, F, run, resume, resumeError, newTask, registerNative, throwError, currentFile, LIBRARY_FILES, } from "./eval.js";
|
|
17
|
+
export { native, op, value, truthy } from "./natives/define.js";
|
|
18
|
+
export { makeRuntime, runSync, doSource, scriptStart } from "./boot.js";
|
|
19
|
+
export { parseJson, toJson } from "./natives/json.js";
|
|
20
|
+
export { queryText } from "./natives/encode.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type I64 = number | bigint;
|
|
2
|
+
/** Normalizes a bigint: number if it fits, null if it overflows 64 bits. */
|
|
3
|
+
export declare function fromBig(b: bigint): I64 | null;
|
|
4
|
+
export declare function add(a: I64, b: I64): I64 | null;
|
|
5
|
+
export declare function sub(a: I64, b: I64): I64 | null;
|
|
6
|
+
export declare function mul(a: I64, b: I64): I64 | null;
|
|
7
|
+
export declare function neg(a: I64): I64 | null;
|
|
8
|
+
/** Truncating integer division. Returns null on overflow or division by zero. */
|
|
9
|
+
export declare function quot(a: I64, b: I64): I64 | null;
|
|
10
|
+
/** Remainder with the sign of the dividend. Null on division by zero. */
|
|
11
|
+
export declare function rem(a: I64, b: I64): I64 | null;
|
|
12
|
+
export declare function cmp(a: I64, b: I64): -1 | 0 | 1;
|
|
13
|
+
export declare function isZero(a: I64): boolean;
|
|
14
|
+
export declare function toNumber(a: I64): number;
|
|
15
|
+
/** Converts a finite JS number to integer!, truncating. Null if out of range. */
|
|
16
|
+
export declare function fromNumber(n: number): I64 | null;
|
|
17
|
+
/**
|
|
18
|
+
* Parses decimal digits with an optional sign. Accepts Rebol's ' digit
|
|
19
|
+
* separators ("1'000'000"). Returns null if malformed or out of range.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parse(text: string): I64 | null;
|
|
22
|
+
export declare function toString(a: I64): string;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Hybrid 64-bit integers.
|
|
2
|
+
//
|
|
3
|
+
// Trebol's integer! is a signed 64-bit integer that raises an error on
|
|
4
|
+
// overflow, as in Rebol 3 and as an i64 would in Zig. Internally a value is
|
|
5
|
+
// a JS `number` while it fits in the safe range (|n| <= 2^53 - 1) and a
|
|
6
|
+
// `bigint` only beyond it. Every function here returns the normalized form,
|
|
7
|
+
// so `number` is always used when possible and equality stays simple.
|
|
8
|
+
//
|
|
9
|
+
// Arithmetic returns `null` on 64-bit overflow; the caller turns that into a
|
|
10
|
+
// Trebol error. No JS exceptions on the normal path.
|
|
11
|
+
const MIN64 = -(2n ** 63n);
|
|
12
|
+
const MAX64 = 2n ** 63n - 1n;
|
|
13
|
+
const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
|
|
14
|
+
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
15
|
+
/** Normalizes a bigint: number if it fits, null if it overflows 64 bits. */
|
|
16
|
+
export function fromBig(b) {
|
|
17
|
+
if (b >= MIN_SAFE && b <= MAX_SAFE)
|
|
18
|
+
return Number(b);
|
|
19
|
+
if (b < MIN64 || b > MAX64)
|
|
20
|
+
return null;
|
|
21
|
+
return b;
|
|
22
|
+
}
|
|
23
|
+
function big(a) {
|
|
24
|
+
return typeof a === 'bigint' ? a : BigInt(a);
|
|
25
|
+
}
|
|
26
|
+
export function add(a, b) {
|
|
27
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
28
|
+
const r = a + b;
|
|
29
|
+
if (Number.isSafeInteger(r))
|
|
30
|
+
return r;
|
|
31
|
+
}
|
|
32
|
+
return fromBig(big(a) + big(b));
|
|
33
|
+
}
|
|
34
|
+
export function sub(a, b) {
|
|
35
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
36
|
+
const r = a - b;
|
|
37
|
+
if (Number.isSafeInteger(r))
|
|
38
|
+
return r;
|
|
39
|
+
}
|
|
40
|
+
return fromBig(big(a) - big(b));
|
|
41
|
+
}
|
|
42
|
+
export function mul(a, b) {
|
|
43
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
44
|
+
const r = a * b;
|
|
45
|
+
if (Number.isSafeInteger(r))
|
|
46
|
+
return r === 0 ? 0 : r; // no -0
|
|
47
|
+
}
|
|
48
|
+
return fromBig(big(a) * big(b));
|
|
49
|
+
}
|
|
50
|
+
export function neg(a) {
|
|
51
|
+
if (typeof a === 'number')
|
|
52
|
+
return a === 0 ? 0 : -a;
|
|
53
|
+
return fromBig(-a);
|
|
54
|
+
}
|
|
55
|
+
/** Truncating integer division. Returns null on overflow or division by zero. */
|
|
56
|
+
export function quot(a, b) {
|
|
57
|
+
if (b === 0)
|
|
58
|
+
return null;
|
|
59
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
60
|
+
const r = Math.trunc(a / b);
|
|
61
|
+
return r === 0 ? 0 : r;
|
|
62
|
+
}
|
|
63
|
+
return fromBig(big(a) / big(b));
|
|
64
|
+
}
|
|
65
|
+
/** Remainder with the sign of the dividend. Null on division by zero. */
|
|
66
|
+
export function rem(a, b) {
|
|
67
|
+
if (b === 0)
|
|
68
|
+
return null;
|
|
69
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
70
|
+
const r = a % b;
|
|
71
|
+
return r === 0 ? 0 : r;
|
|
72
|
+
}
|
|
73
|
+
return fromBig(big(a) % big(b));
|
|
74
|
+
}
|
|
75
|
+
export function cmp(a, b) {
|
|
76
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
77
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
78
|
+
const x = big(a);
|
|
79
|
+
const y = big(b);
|
|
80
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
81
|
+
}
|
|
82
|
+
export function isZero(a) {
|
|
83
|
+
return a === 0 || a === 0n;
|
|
84
|
+
}
|
|
85
|
+
export function toNumber(a) {
|
|
86
|
+
return typeof a === 'number' ? a : Number(a);
|
|
87
|
+
}
|
|
88
|
+
/** Converts a finite JS number to integer!, truncating. Null if out of range. */
|
|
89
|
+
export function fromNumber(n) {
|
|
90
|
+
if (!Number.isFinite(n))
|
|
91
|
+
return null;
|
|
92
|
+
const t = Math.trunc(n);
|
|
93
|
+
if (Number.isSafeInteger(t))
|
|
94
|
+
return t === 0 ? 0 : t;
|
|
95
|
+
return fromBig(BigInt(t));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Parses decimal digits with an optional sign. Accepts Rebol's ' digit
|
|
99
|
+
* separators ("1'000'000"). Returns null if malformed or out of range.
|
|
100
|
+
*/
|
|
101
|
+
export function parse(text) {
|
|
102
|
+
const clean = text.replace(/'/g, '');
|
|
103
|
+
if (!/^[+-]?\d+$/.test(clean))
|
|
104
|
+
return null;
|
|
105
|
+
if (clean.replace(/^[+-]/, '').length <= 15) {
|
|
106
|
+
const n = Number(clean); // 15 digits always fit in a safe integer
|
|
107
|
+
return n === 0 ? 0 : n;
|
|
108
|
+
}
|
|
109
|
+
return fromBig(BigInt(clean));
|
|
110
|
+
}
|
|
111
|
+
export function toString(a) {
|
|
112
|
+
return typeof a === 'bigint' ? a.toString() : String(a);
|
|
113
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export declare const K: {
|
|
2
|
+
readonly Unset: 0;
|
|
3
|
+
readonly None: 1;
|
|
4
|
+
readonly Logic: 2;
|
|
5
|
+
readonly Integer: 3;
|
|
6
|
+
readonly Decimal: 4;
|
|
7
|
+
readonly Percent: 5;
|
|
8
|
+
readonly Money: 6;
|
|
9
|
+
readonly Char: 7;
|
|
10
|
+
readonly Pair: 8;
|
|
11
|
+
readonly Tuple: 9;
|
|
12
|
+
readonly Date: 10;
|
|
13
|
+
readonly Time: 11;
|
|
14
|
+
readonly String: 12;
|
|
15
|
+
readonly File: 13;
|
|
16
|
+
readonly Email: 14;
|
|
17
|
+
readonly Url: 15;
|
|
18
|
+
readonly Tag: 16;
|
|
19
|
+
readonly Binary: 17;
|
|
20
|
+
readonly Bitset: 18;
|
|
21
|
+
readonly Word: 19;
|
|
22
|
+
readonly SetWord: 20;
|
|
23
|
+
readonly GetWord: 21;
|
|
24
|
+
readonly LitWord: 22;
|
|
25
|
+
readonly Refinement: 23;
|
|
26
|
+
readonly Issue: 24;
|
|
27
|
+
readonly Block: 25;
|
|
28
|
+
readonly Paren: 26;
|
|
29
|
+
readonly Path: 27;
|
|
30
|
+
readonly SetPath: 28;
|
|
31
|
+
readonly GetPath: 29;
|
|
32
|
+
readonly LitPath: 30;
|
|
33
|
+
readonly Map: 31;
|
|
34
|
+
readonly Object: 32;
|
|
35
|
+
readonly Error: 33;
|
|
36
|
+
readonly Function: 34;
|
|
37
|
+
readonly Native: 35;
|
|
38
|
+
readonly Op: 36;
|
|
39
|
+
readonly Datatype: 37;
|
|
40
|
+
readonly Typeset: 38;
|
|
41
|
+
readonly Handle: 39;
|
|
42
|
+
};
|
|
43
|
+
export type Kind = (typeof K)[keyof typeof K];
|
|
44
|
+
/** Datatype names, indexed by Kind. */
|
|
45
|
+
export declare const KIND_NAMES: readonly string[];
|
|
46
|
+
export declare const KIND_COUNT: number;
|
|
47
|
+
/** Looks up a datatype by its name, e.g. "integer!". Case-insensitive. */
|
|
48
|
+
export declare function kindByName(name: string): Kind | undefined;
|
|
49
|
+
export declare function kindName(k: Kind): string;
|
|
50
|
+
/** The kind's name with its article: "a block!", "an email!". */
|
|
51
|
+
export declare function aKindName(k: Kind): string;
|
|
52
|
+
export declare function isAnyString(k: Kind): boolean;
|
|
53
|
+
export declare function isAnyWord(k: Kind): boolean;
|
|
54
|
+
export declare function isAnyBlock(k: Kind): boolean;
|
|
55
|
+
export declare function isAnyPath(k: Kind): boolean;
|
|
56
|
+
export declare function isSeries(k: Kind): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* A typeset as a table indexed by kind, for checks on the hot path: testing
|
|
59
|
+
* a bigint mask allocates, reading an array does not.
|
|
60
|
+
*/
|
|
61
|
+
export declare function kindTable(bits: bigint): readonly boolean[];
|
|
62
|
+
/** A set of kinds as a bitmask: bit `k` is set when kind `k` belongs to it. */
|
|
63
|
+
export declare function kindBit(k: Kind): bigint;
|
|
64
|
+
/** Built-in typesets, usable wherever a datatype is expected (function specs, `make typeset!`). */
|
|
65
|
+
export declare const TYPESETS: Readonly<Record<string, bigint>>;
|
|
66
|
+
/** Bits for a datatype or typeset name ("integer!", "series!"), or undefined. */
|
|
67
|
+
export declare function typeBitsByName(name: string): bigint | undefined;
|