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,500 @@
|
|
|
1
|
+
// Arithmetic, comparison and logic natives, and their infix operators.
|
|
2
|
+
//
|
|
3
|
+
// integer! stays exact and raises an error on 64-bit overflow; integer
|
|
4
|
+
// division gives an integer when exact and a decimal otherwise, as in R3.
|
|
5
|
+
// money! is exact (see dec.ts). A percent scaled by a number stays a percent.
|
|
6
|
+
import { K, kindName } from "../kinds.js";
|
|
7
|
+
import * as I64 from "../int64.js";
|
|
8
|
+
import { decAdd, decSub, decMul, decDiv, decRem, decRound, decFromNumber, decToNumber, decEqual } from "../dec.js";
|
|
9
|
+
import { ownMeta, NONE, UNSET, TRUE, integer, decimal, percent, money, time, pair, logic, date, strictEqual, sameValue, } from "../values.js";
|
|
10
|
+
import { isSeries } from "../kinds.js";
|
|
11
|
+
import { mold } from "../mold.js";
|
|
12
|
+
import { pick } from "../access.js";
|
|
13
|
+
import { isEqual, compare, toDec } from "../compare.js";
|
|
14
|
+
import { native, op, value, fail, truthy } from "./define.js";
|
|
15
|
+
const A = { Add: 0, Sub: 1, Mul: 2, Div: 3, Rem: 4 };
|
|
16
|
+
const VERBS = ['add', 'subtract', 'multiply', 'divide', 'take the remainder of'];
|
|
17
|
+
const zeroDivide = () => fail('math', 'zero-divide', 'attempt to divide by zero');
|
|
18
|
+
const overflow = () => fail('math', 'overflow', 'math or number overflow');
|
|
19
|
+
function num(c) {
|
|
20
|
+
switch (c.t) {
|
|
21
|
+
case K.Integer: return I64.toNumber(c.v);
|
|
22
|
+
case K.Decimal:
|
|
23
|
+
case K.Percent: return c.v;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
function floatOp(op, x, y) {
|
|
28
|
+
switch (op) {
|
|
29
|
+
case A.Add: return x + y;
|
|
30
|
+
case A.Sub: return x - y;
|
|
31
|
+
case A.Mul: return x * y;
|
|
32
|
+
case A.Div: return y === 0 ? null : x / y;
|
|
33
|
+
case A.Rem: return y === 0 ? null : x % y;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function intArith(op, a, b) {
|
|
37
|
+
let r;
|
|
38
|
+
switch (op) {
|
|
39
|
+
case A.Add:
|
|
40
|
+
r = I64.add(a, b);
|
|
41
|
+
break;
|
|
42
|
+
case A.Sub:
|
|
43
|
+
r = I64.sub(a, b);
|
|
44
|
+
break;
|
|
45
|
+
case A.Mul:
|
|
46
|
+
r = I64.mul(a, b);
|
|
47
|
+
break;
|
|
48
|
+
case A.Rem:
|
|
49
|
+
if (I64.isZero(b))
|
|
50
|
+
return zeroDivide();
|
|
51
|
+
r = I64.rem(a, b);
|
|
52
|
+
break;
|
|
53
|
+
case A.Div: {
|
|
54
|
+
if (I64.isZero(b))
|
|
55
|
+
return zeroDivide();
|
|
56
|
+
const m = I64.rem(a, b);
|
|
57
|
+
if (m !== null && I64.isZero(m)) {
|
|
58
|
+
r = I64.quot(a, b);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
return value(decimal(I64.toNumber(a) / I64.toNumber(b)));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return r === null ? overflow() : value(integer(r));
|
|
65
|
+
}
|
|
66
|
+
function moneyArith(op, a, b) {
|
|
67
|
+
const x = toDec(a);
|
|
68
|
+
const y = toDec(b);
|
|
69
|
+
if (x === null || y === null)
|
|
70
|
+
return null;
|
|
71
|
+
const ca = a.t === K.Money ? a.cur : '';
|
|
72
|
+
const cb = b.t === K.Money ? b.cur : '';
|
|
73
|
+
if (ca !== '' && cb !== '' && ca.toUpperCase() !== cb.toUpperCase()) {
|
|
74
|
+
return fail('math', 'currency', `cannot mix ${ca} and ${cb}`);
|
|
75
|
+
}
|
|
76
|
+
const cur = ca || cb;
|
|
77
|
+
const both = a.t === K.Money && b.t === K.Money;
|
|
78
|
+
let r;
|
|
79
|
+
switch (op) {
|
|
80
|
+
case A.Add:
|
|
81
|
+
r = decAdd(x, y);
|
|
82
|
+
break;
|
|
83
|
+
case A.Sub:
|
|
84
|
+
r = decSub(x, y);
|
|
85
|
+
break;
|
|
86
|
+
case A.Mul:
|
|
87
|
+
if (both)
|
|
88
|
+
return null;
|
|
89
|
+
r = decMul(x, y);
|
|
90
|
+
break;
|
|
91
|
+
case A.Div:
|
|
92
|
+
r = decDiv(x, y);
|
|
93
|
+
if (r === null)
|
|
94
|
+
return zeroDivide();
|
|
95
|
+
return value(both ? decimal(decToNumber(r)) : money(r, cur));
|
|
96
|
+
case A.Rem:
|
|
97
|
+
r = decRem(x, y);
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return r === null ? zeroDivide() : value(money(r, cur));
|
|
101
|
+
}
|
|
102
|
+
function arith(op, a, b) {
|
|
103
|
+
if (a.t === K.Integer && b.t === K.Integer)
|
|
104
|
+
return intArith(op, a.v, b.v);
|
|
105
|
+
const x = num(a);
|
|
106
|
+
const y = num(b);
|
|
107
|
+
if (a.t === K.Money || b.t === K.Money) {
|
|
108
|
+
const r = moneyArith(op, a, b);
|
|
109
|
+
if (r !== null)
|
|
110
|
+
return r;
|
|
111
|
+
}
|
|
112
|
+
else if (a.t === K.Pair || b.t === K.Pair) {
|
|
113
|
+
const ax = a.t === K.Pair ? a.x : x;
|
|
114
|
+
const ay = a.t === K.Pair ? a.y : x;
|
|
115
|
+
const bx = b.t === K.Pair ? b.x : y;
|
|
116
|
+
const by = b.t === K.Pair ? b.y : y;
|
|
117
|
+
if (ax !== null && ay !== null && bx !== null && by !== null) {
|
|
118
|
+
const rx = floatOp(op, ax, bx);
|
|
119
|
+
const ry = floatOp(op, ay, by);
|
|
120
|
+
return rx === null || ry === null ? zeroDivide() : value(pair(rx, ry));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (a.t === K.Date) {
|
|
124
|
+
const r = dateArith(op, a, b);
|
|
125
|
+
if (r !== null)
|
|
126
|
+
return r;
|
|
127
|
+
}
|
|
128
|
+
else if (a.t === K.Time || b.t === K.Time) {
|
|
129
|
+
const r = timeArith(op, a, b, x, y);
|
|
130
|
+
if (r !== null)
|
|
131
|
+
return r;
|
|
132
|
+
}
|
|
133
|
+
else if (x !== null && y !== null) {
|
|
134
|
+
const r = floatOp(op, x, y);
|
|
135
|
+
if (r === null)
|
|
136
|
+
return zeroDivide();
|
|
137
|
+
if (!Number.isFinite(r))
|
|
138
|
+
return overflow();
|
|
139
|
+
const keepsPercent = a.t === K.Percent &&
|
|
140
|
+
(b.t === K.Percent ? op !== A.Div : op === A.Mul || op === A.Div);
|
|
141
|
+
return value(keepsPercent ? percent(r) : decimal(r));
|
|
142
|
+
}
|
|
143
|
+
return fail('script', 'bad-math', `cannot ${VERBS[op]} ${kindName(a.t)} and ${kindName(b.t)}`);
|
|
144
|
+
}
|
|
145
|
+
function timeArith(op, a, b, x, y) {
|
|
146
|
+
if (a.t === K.Time && b.t === K.Time) {
|
|
147
|
+
switch (op) {
|
|
148
|
+
case A.Add: return value(time(a.v + b.v));
|
|
149
|
+
case A.Sub: return value(time(a.v - b.v));
|
|
150
|
+
case A.Div: return b.v === 0 ? zeroDivide() : value(decimal(a.v / b.v));
|
|
151
|
+
case A.Rem: return b.v === 0 ? zeroDivide() : value(time(a.v % b.v));
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (a.t === K.Time && y !== null) {
|
|
156
|
+
if (op === A.Mul)
|
|
157
|
+
return value(time(Math.round(a.v * y)));
|
|
158
|
+
if (op === A.Div)
|
|
159
|
+
return y === 0 ? zeroDivide() : value(time(Math.round(a.v / y)));
|
|
160
|
+
}
|
|
161
|
+
if (b.t === K.Time && x !== null && op === A.Mul)
|
|
162
|
+
return value(time(Math.round(b.v * x)));
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
const DAY_NS = 86400e9;
|
|
166
|
+
/** Days since 1970-01-01 for a calendar date. */
|
|
167
|
+
function dayNumber(d) {
|
|
168
|
+
return Date.UTC(d.y, d.m - 1, d.d) / 86400000;
|
|
169
|
+
}
|
|
170
|
+
function fromDayNumber(n, time, zone) {
|
|
171
|
+
const js = new Date(n * 86400000);
|
|
172
|
+
return date({ y: js.getUTCFullYear(), m: js.getUTCMonth() + 1, d: js.getUTCDate(), time, zone });
|
|
173
|
+
}
|
|
174
|
+
/** date ± integer days, date ± time, date - date in days. */
|
|
175
|
+
function dateArith(op, a, b) {
|
|
176
|
+
const d = a.v;
|
|
177
|
+
if (b.t === K.Integer && (op === A.Add || op === A.Sub)) {
|
|
178
|
+
const n = I64.toNumber(b.v);
|
|
179
|
+
return value(fromDayNumber(dayNumber(d) + (op === A.Add ? n : -n), d.time, d.zone));
|
|
180
|
+
}
|
|
181
|
+
if (b.t === K.Time && (op === A.Add || op === A.Sub)) {
|
|
182
|
+
const total = (d.time ?? 0) + (op === A.Add ? b.v : -b.v);
|
|
183
|
+
const days = Math.floor(total / DAY_NS);
|
|
184
|
+
return value(fromDayNumber(dayNumber(d) + days, total - days * DAY_NS, d.zone));
|
|
185
|
+
}
|
|
186
|
+
if (b.t === K.Date && op === A.Sub) {
|
|
187
|
+
const ns = dateDiff(d, b.v);
|
|
188
|
+
return value(d.time === null && b.v.time === null ? integer(Math.round(ns / DAY_NS)) : time(ns));
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
/** Nanoseconds from date `e` to date `d`, zones taken into account. */
|
|
193
|
+
export function dateDiff(d, e) {
|
|
194
|
+
return (dayNumber(d) - dayNumber(e)) * DAY_NS + ((d.time ?? 0) - (d.zone ?? 0) * 60e9) - ((e.time ?? 0) - (e.zone ?? 0) * 60e9);
|
|
195
|
+
}
|
|
196
|
+
const NUMERIC = '[number! money! time! pair! date!]';
|
|
197
|
+
native('add', `["The sum of two values." a ${NUMERIC} b ${NUMERIC}]`, (_t, f) => arith(A.Add, f.args[0], f.args[1]));
|
|
198
|
+
native('subtract', `["The difference of two values." a ${NUMERIC} b ${NUMERIC}]`, (_t, f) => arith(A.Sub, f.args[0], f.args[1]));
|
|
199
|
+
native('multiply', `["The product of two values." a ${NUMERIC} b ${NUMERIC}]`, (_t, f) => arith(A.Mul, f.args[0], f.args[1]));
|
|
200
|
+
native('divide', `["The first value divided by the second." a ${NUMERIC} b ${NUMERIC}]`, (_t, f) => arith(A.Div, f.args[0], f.args[1]));
|
|
201
|
+
native('remainder', `["What is left after dividing the first value by the second." a ${NUMERIC} b ${NUMERIC}]`, (_t, f) => arith(A.Rem, f.args[0], f.args[1]));
|
|
202
|
+
native('negate', `["The value with its sign changed." value ${NUMERIC}]`, (_t, f) => {
|
|
203
|
+
const a = f.args[0];
|
|
204
|
+
switch (a.t) {
|
|
205
|
+
case K.Integer: {
|
|
206
|
+
const r = I64.neg(a.v);
|
|
207
|
+
return r === null ? overflow() : value(integer(r));
|
|
208
|
+
}
|
|
209
|
+
case K.Decimal: return value(decimal(-a.v));
|
|
210
|
+
case K.Percent: return value(percent(-a.v));
|
|
211
|
+
case K.Money: return value(money({ d: -a.v.d, e: a.v.e }, a.cur));
|
|
212
|
+
case K.Time: return value(time(-a.v));
|
|
213
|
+
case K.Pair: return value(pair(-a.x, -a.y));
|
|
214
|
+
}
|
|
215
|
+
return fail('script', 'bad-math', `cannot negate ${kindName(a.t)}`);
|
|
216
|
+
});
|
|
217
|
+
// round: the nearest multiple of a scale (1 by default; 1 second for time!),
|
|
218
|
+
// exact for every kind since it works on decimals. As in R3, half goes away
|
|
219
|
+
// from zero; an integer scale gives an integer, a money! on either side money.
|
|
220
|
+
const ROUNDINGS = ['even', 'down', 'half-down', 'floor', 'ceiling', 'half-ceiling'];
|
|
221
|
+
native('round', '["The value rounded to the nearest whole number, or to a multiple of a scale." value [number! money! time!] /to scale [number! money! time!] /even /down /half-down /floor /ceiling /half-ceiling]', (_t, f) => {
|
|
222
|
+
const a = f.args[0];
|
|
223
|
+
const to = f.args[1] === TRUE;
|
|
224
|
+
const mode = ROUNDINGS.find((_m, i) => f.args[3 + i] === TRUE) ?? 'half-away';
|
|
225
|
+
if (a.t === K.Time) {
|
|
226
|
+
const s = to ? f.args[2] : integer(1);
|
|
227
|
+
const step = s.t === K.Time ? s.v : (num(s) ?? decToNumber(toDec(s))) * 1e9;
|
|
228
|
+
const r = decRound(decFromNumber(a.v), decFromNumber(step), mode);
|
|
229
|
+
return r === null ? zeroDivide() : value(time(decToNumber(r)));
|
|
230
|
+
}
|
|
231
|
+
const s = to ? f.args[2] : a.t === K.Percent ? percent(0.01) : integer(1);
|
|
232
|
+
if (s.t === K.Time)
|
|
233
|
+
return fail('script', 'bad-math', 'cannot round a number to a time!');
|
|
234
|
+
const r = decRound(toDec(a), toDec(s), mode);
|
|
235
|
+
if (r === null)
|
|
236
|
+
return zeroDivide();
|
|
237
|
+
if (a.t === K.Money || s.t === K.Money)
|
|
238
|
+
return value(money(r, a.t === K.Money ? a.cur : s.cur));
|
|
239
|
+
if (s.t === K.Integer && (to || a.t === K.Integer)) {
|
|
240
|
+
const n = r.e === 0 ? I64.fromBig(r.d) : null;
|
|
241
|
+
return n === null ? overflow() : value(integer(n));
|
|
242
|
+
}
|
|
243
|
+
return value(a.t === K.Percent ? percent(decToNumber(r)) : decimal(decToNumber(r)));
|
|
244
|
+
});
|
|
245
|
+
native('abs', `["The value without its sign." value ${NUMERIC}]`, (_t, f) => {
|
|
246
|
+
const a = f.args[0];
|
|
247
|
+
switch (a.t) {
|
|
248
|
+
case K.Integer: {
|
|
249
|
+
if (I64.cmp(a.v, 0) >= 0)
|
|
250
|
+
return value(a);
|
|
251
|
+
const r = I64.neg(a.v);
|
|
252
|
+
return r === null ? overflow() : value(integer(r));
|
|
253
|
+
}
|
|
254
|
+
case K.Decimal: return value(decimal(Math.abs(a.v)));
|
|
255
|
+
case K.Percent: return value(percent(Math.abs(a.v)));
|
|
256
|
+
case K.Money: return value(money({ d: a.v.d < 0n ? -a.v.d : a.v.d, e: a.v.e }, a.cur));
|
|
257
|
+
case K.Time: return value(time(Math.abs(a.v)));
|
|
258
|
+
case K.Pair: return value(pair(Math.abs(a.x), Math.abs(a.y)));
|
|
259
|
+
}
|
|
260
|
+
return fail('script', 'bad-math', `cannot take abs of ${kindName(a.t)}`);
|
|
261
|
+
});
|
|
262
|
+
native('zero?', `["True if the value is zero." value ${NUMERIC}]`, (_t, f) => {
|
|
263
|
+
const a = f.args[0];
|
|
264
|
+
switch (a.t) {
|
|
265
|
+
case K.Integer: return value(logic(I64.isZero(a.v)));
|
|
266
|
+
case K.Money: return value(logic(decEqual(a.v, { d: 0n, e: 0 })));
|
|
267
|
+
case K.Pair: return value(logic(a.x === 0 && a.y === 0));
|
|
268
|
+
case K.Decimal:
|
|
269
|
+
case K.Percent:
|
|
270
|
+
case K.Time: return value(logic(a.v === 0));
|
|
271
|
+
}
|
|
272
|
+
return value(logic(false));
|
|
273
|
+
});
|
|
274
|
+
native('even?', '["True if the integer is even." value [integer!]]', (_t, f) => value(logic(I64.isZero(I64.rem(f.args[0].v, 2)))));
|
|
275
|
+
native('odd?', '["True if the integer is odd." value [integer!]]', (_t, f) => value(logic(!I64.isZero(I64.rem(f.args[0].v, 2)))));
|
|
276
|
+
native('sign?', '["Gives -1, 0 or 1 by the sign of the value." value [number! money! time!]]', (_t, f) => {
|
|
277
|
+
return value(integer(signOf(f.args[0])));
|
|
278
|
+
});
|
|
279
|
+
native('negative?', '["True if the value is less than zero." value [number! money! time!]]', (_t, f) => value(logic(signOf(f.args[0]) < 0)));
|
|
280
|
+
native('positive?', '["True if the value is more than zero." value [number! money! time!]]', (_t, f) => value(logic(signOf(f.args[0]) > 0)));
|
|
281
|
+
function signOf(a) {
|
|
282
|
+
return a.t === K.Integer ? I64.cmp(a.v, 0) : a.t === K.Money ? Math.sign(Number(a.v.d)) : Math.sign(a.v);
|
|
283
|
+
}
|
|
284
|
+
// --- powers, roots, logarithms and trigonometry -------------------------------------
|
|
285
|
+
//
|
|
286
|
+
// As in Red, an integer to a whole power stays an exact integer (an error on
|
|
287
|
+
// 64-bit overflow); anything else is a decimal. Angles are in degrees unless
|
|
288
|
+
// /radians, as in R3 and Red.
|
|
289
|
+
const NUMBER = '[number!]';
|
|
290
|
+
function real(c) {
|
|
291
|
+
return c.t === K.Integer ? I64.toNumber(c.v) : c.v;
|
|
292
|
+
}
|
|
293
|
+
/** As in R3 and Red, a result with no number is 1.#NaN, and one too big 1.#INF: not an error. */
|
|
294
|
+
function finite(x) {
|
|
295
|
+
return value(decimal(x));
|
|
296
|
+
}
|
|
297
|
+
native('power', `["The number raised to the exponent." number ${NUMBER} exponent ${NUMBER}]`, (_t, f) => {
|
|
298
|
+
const [a, b] = f.args;
|
|
299
|
+
if (a.t === K.Integer && b.t === K.Integer && I64.cmp(b.v, 0) >= 0) {
|
|
300
|
+
const base = BigInt(a.v);
|
|
301
|
+
const e = BigInt(b.v);
|
|
302
|
+
// past 63 doublings even a base of 2 overflows; 0, 1 and -1 never do
|
|
303
|
+
if (e > 64n && base !== 0n && base !== 1n && base !== -1n)
|
|
304
|
+
return overflow();
|
|
305
|
+
const n = I64.fromBig(base ** (e > 64n ? e % 2n + 2n : e));
|
|
306
|
+
return n === null ? overflow() : value(integer(n));
|
|
307
|
+
}
|
|
308
|
+
return finite(Math.pow(real(a), real(b)));
|
|
309
|
+
});
|
|
310
|
+
native('square-root', `["The square root of the number." value ${NUMBER}]`, (_t, f) => {
|
|
311
|
+
return finite(Math.sqrt(real(f.args[0])));
|
|
312
|
+
});
|
|
313
|
+
native('exp', `["e raised to the power of the number." power ${NUMBER}]`, (_t, f) => finite(Math.exp(real(f.args[0]))));
|
|
314
|
+
function logOf(f, log) {
|
|
315
|
+
return finite(log(real(f.args[0])));
|
|
316
|
+
}
|
|
317
|
+
native('log-e', `["The natural logarithm of the number." value ${NUMBER}]`, (_t, f) => logOf(f, Math.log));
|
|
318
|
+
native('log-10', `["The base-10 logarithm of the number." value ${NUMBER}]`, (_t, f) => logOf(f, Math.log10));
|
|
319
|
+
native('log-2', `["The base-2 logarithm of the number." value ${NUMBER}]`, (_t, f) => logOf(f, Math.log2));
|
|
320
|
+
const DEG = Math.PI / 180;
|
|
321
|
+
/** In degrees, what is left of an exact zero is zero: sine 180 is 0.0, as in R3. */
|
|
322
|
+
function clean(x) {
|
|
323
|
+
return Math.abs(x) < 1e-15 ? 0 : x;
|
|
324
|
+
}
|
|
325
|
+
function trig(name, doc, fn) {
|
|
326
|
+
native(name, `["${doc}" angle ${NUMBER} /radians "The angle is in radians"]`, (_t, f) => {
|
|
327
|
+
const x = real(f.args[0]) * (f.args[1] === TRUE ? 1 : DEG);
|
|
328
|
+
if (name === 'tangent' && Math.abs(Math.cos(x)) < 1e-15)
|
|
329
|
+
return finite(Math.sin(x) > 0 ? Infinity : -Infinity);
|
|
330
|
+
const r = fn(x);
|
|
331
|
+
return finite(f.args[1] === TRUE ? r : clean(r));
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
trig('sine', 'The sine of an angle, in degrees.', Math.sin);
|
|
335
|
+
trig('cosine', 'The cosine of an angle, in degrees.', Math.cos);
|
|
336
|
+
trig('tangent', 'The tangent of an angle, in degrees.', Math.tan);
|
|
337
|
+
function arc(name, doc, fn, bounded) {
|
|
338
|
+
native(name, `["${doc}" value ${NUMBER} /radians "Give the angle in radians"]`, (_t, f) => {
|
|
339
|
+
const x = real(f.args[0]);
|
|
340
|
+
return finite(f.args[1] === TRUE ? fn(x) : clean(fn(x) / DEG));
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
arc('arcsine', 'The angle whose sine is the value, in degrees.', Math.asin, true);
|
|
344
|
+
arc('arccosine', 'The angle whose cosine is the value, in degrees.', Math.acos, true);
|
|
345
|
+
arc('arctangent', 'The angle whose tangent is the value, in degrees.', Math.atan, false);
|
|
346
|
+
native('arctangent2', `["The angle of the point x, y, in degrees." y ${NUMBER} x ${NUMBER} /radians "Give the angle in radians"]`, (_t, f) => {
|
|
347
|
+
const r = Math.atan2(real(f.args[0]), real(f.args[1]));
|
|
348
|
+
return finite(f.args[2] === TRUE ? r : clean(r / DEG));
|
|
349
|
+
});
|
|
350
|
+
// --- random ------------------------------------------------------------------------
|
|
351
|
+
//
|
|
352
|
+
// A small seeded generator (xorshift128+), so /seed gives the same sequence
|
|
353
|
+
// again. Seeded from the clock until a program seeds it.
|
|
354
|
+
const rng = { a: BigInt(Date.now()) | 1n, b: 0x9e3779b97f4a7c15n };
|
|
355
|
+
function nextRandom() {
|
|
356
|
+
let x = rng.a;
|
|
357
|
+
const y = rng.b;
|
|
358
|
+
rng.a = y;
|
|
359
|
+
x ^= (x << 23n) & 0xffffffffffffffffn;
|
|
360
|
+
rng.b = x ^ y ^ (x >> 17n) ^ (y >> 26n);
|
|
361
|
+
return Number((rng.b + y) & 0x1fffffffffffffn) / 2 ** 53;
|
|
362
|
+
}
|
|
363
|
+
/** A whole number from 1 to n. */
|
|
364
|
+
function randomInt(n) {
|
|
365
|
+
return Math.floor(nextRandom() * n) + 1;
|
|
366
|
+
}
|
|
367
|
+
native('random', '["A random value: an integer from 1 to it, a decimal up to it, a shuffled series." value [any-value!] /seed "Start the sequence from this value; gives nothing" /only "Pick one element of a series"]', (_t, f) => {
|
|
368
|
+
const v = f.args[0];
|
|
369
|
+
if (f.args[1] === TRUE) {
|
|
370
|
+
let h = 0x811c9dc5n;
|
|
371
|
+
for (const ch of mold(v))
|
|
372
|
+
h = ((h ^ BigInt(ch.codePointAt(0))) * 0x100000001b3n) & 0xffffffffffffffffn;
|
|
373
|
+
rng.a = h | 1n;
|
|
374
|
+
rng.b = (h * 0x9e3779b97f4a7c15n) & 0xffffffffffffffffn | 1n;
|
|
375
|
+
for (let i = 0; i < 8; i++)
|
|
376
|
+
nextRandom();
|
|
377
|
+
return value(UNSET);
|
|
378
|
+
}
|
|
379
|
+
switch (v.t) {
|
|
380
|
+
case K.Integer: {
|
|
381
|
+
const n = I64.toNumber(v.v);
|
|
382
|
+
if (n === 0)
|
|
383
|
+
return value(integer(0));
|
|
384
|
+
return value(integer(Math.sign(n) * randomInt(Math.abs(n))));
|
|
385
|
+
}
|
|
386
|
+
case K.Decimal: return value(decimal(nextRandom() * v.v));
|
|
387
|
+
case K.Percent: return value(percent(nextRandom() * v.v));
|
|
388
|
+
case K.Money: return value(money(decFromNumber(Math.floor(nextRandom() * decToNumber(v.v) * 100) / 100), v.cur));
|
|
389
|
+
case K.Logic: return value(logic(nextRandom() < 0.5));
|
|
390
|
+
case K.Time: return value(time(Math.floor(nextRandom() * v.v)));
|
|
391
|
+
case K.Char: return value({ t: K.Char, v: randomInt(v.v) });
|
|
392
|
+
}
|
|
393
|
+
if (isSeries(v.t)) {
|
|
394
|
+
const s = v;
|
|
395
|
+
const n = s.s.data.length - s.i;
|
|
396
|
+
if (f.args[2] === TRUE) {
|
|
397
|
+
if (n <= 0)
|
|
398
|
+
return value(NONE);
|
|
399
|
+
return value(pick(v, integer(randomInt(n))));
|
|
400
|
+
}
|
|
401
|
+
ownMeta(s.s);
|
|
402
|
+
const arrays = [s.s.data, s.s.nl, s.s.pos].filter(x => x !== undefined);
|
|
403
|
+
for (let i = n - 1; i > 0; i--) {
|
|
404
|
+
const j = Math.floor(nextRandom() * (i + 1));
|
|
405
|
+
for (const arr of arrays)
|
|
406
|
+
[arr[s.i + i], arr[s.i + j]] = [arr[s.i + j], arr[s.i + i]];
|
|
407
|
+
}
|
|
408
|
+
return value(v);
|
|
409
|
+
}
|
|
410
|
+
return fail('script', 'bad-random', `cannot make a random ${kindName(v.t)}`);
|
|
411
|
+
});
|
|
412
|
+
// --- bit shifts --------------------------------------------------------------------
|
|
413
|
+
//
|
|
414
|
+
// As in Red: shift goes right (keeping the sign) unless /left; /logical
|
|
415
|
+
// shifts zeros in from the left. Bits shifted out of the 64 are lost.
|
|
416
|
+
function shiftBits(x, n, left, logical) {
|
|
417
|
+
const v = BigInt(x);
|
|
418
|
+
const by = BigInt(Math.max(0, Math.min(64, n)));
|
|
419
|
+
let r;
|
|
420
|
+
if (left)
|
|
421
|
+
r = BigInt.asIntN(64, v << by);
|
|
422
|
+
else if (logical)
|
|
423
|
+
r = BigInt.asIntN(64, BigInt.asUintN(64, v) >> by);
|
|
424
|
+
else
|
|
425
|
+
r = v >> by;
|
|
426
|
+
return integer(I64.fromBig(r));
|
|
427
|
+
}
|
|
428
|
+
const SHIFT_ARGS = 'data [integer!] bits [integer!] "How many bits"';
|
|
429
|
+
native('shift', `["Shifts the bits of an integer right, or left with /left." ${SHIFT_ARGS} /left "Shift to the left" /logical "Shift zeros in, not the sign"]`, (_t, f) => value(shiftBits(f.args[0].v, I64.toNumber(f.args[1].v), f.args[2] === TRUE, f.args[3] === TRUE)));
|
|
430
|
+
native('shift-left', `["Shifts the bits of an integer left." ${SHIFT_ARGS}]`, (_t, f) => value(shiftBits(f.args[0].v, I64.toNumber(f.args[1].v), true, false)));
|
|
431
|
+
native('shift-right', `["Shifts the bits of an integer right, keeping its sign." ${SHIFT_ARGS}]`, (_t, f) => value(shiftBits(f.args[0].v, I64.toNumber(f.args[1].v), false, false)));
|
|
432
|
+
native('shift-logical', `["Shifts the bits of an integer right, shifting zeros in." ${SHIFT_ARGS}]`, (_t, f) => value(shiftBits(f.args[0].v, I64.toNumber(f.args[1].v), false, true)));
|
|
433
|
+
// --- comparison ------------------------------------------------------------------
|
|
434
|
+
native('equal?', '["True if the values are equal." a [any-type!] b [any-type!]]', (_t, f) => value(logic(isEqual(f.args[0], f.args[1]))));
|
|
435
|
+
native('not-equal?', '["True if the values are not equal." a [any-type!] b [any-type!]]', (_t, f) => value(logic(!isEqual(f.args[0], f.args[1]))));
|
|
436
|
+
native('strict-equal?', '["True if the values are equal and of the same type and case." a [any-type!] b [any-type!]]', (_t, f) => value(logic(strictEqual(f.args[0], f.args[1]))));
|
|
437
|
+
native('same?', '["True if the values are the very same: the same series at the same position, the same object." a [any-type!] b [any-type!]]', (_t, f) => value(logic(sameValue(f.args[0], f.args[1]))));
|
|
438
|
+
native('strict-not-equal?', '["True if the values are not strictly equal." a [any-type!] b [any-type!]]', (_t, f) => value(logic(!strictEqual(f.args[0], f.args[1]))));
|
|
439
|
+
function ordered(a, b, test) {
|
|
440
|
+
const c = compare(a, b);
|
|
441
|
+
if (c === null)
|
|
442
|
+
return fail('script', 'bad-compare', `cannot compare ${kindName(a.t)} with ${kindName(b.t)}`);
|
|
443
|
+
return value(logic(test(c)));
|
|
444
|
+
}
|
|
445
|
+
native('lesser?', '["True if the first value is less than the second." a [any-value!] b [any-value!]]', (_t, f) => ordered(f.args[0], f.args[1], c => c < 0));
|
|
446
|
+
native('greater?', '["True if the first value is more than the second." a [any-value!] b [any-value!]]', (_t, f) => ordered(f.args[0], f.args[1], c => c > 0));
|
|
447
|
+
native('lesser-or-equal?', '["True if the first value is less than or equal to the second." a [any-value!] b [any-value!]]', (_t, f) => ordered(f.args[0], f.args[1], c => c <= 0));
|
|
448
|
+
native('greater-or-equal?', '["True if the first value is more than or equal to the second." a [any-value!] b [any-value!]]', (_t, f) => ordered(f.args[0], f.args[1], c => c >= 0));
|
|
449
|
+
function pickBy(a, b, wantMax) {
|
|
450
|
+
const c = compare(a, b);
|
|
451
|
+
if (c === null)
|
|
452
|
+
return fail('script', 'bad-compare', `cannot compare ${kindName(a.t)} with ${kindName(b.t)}`);
|
|
453
|
+
return value((c >= 0) === wantMax ? a : b);
|
|
454
|
+
}
|
|
455
|
+
native('max', '["The greater of two values." a [any-value!] b [any-value!]]', (_t, f) => pickBy(f.args[0], f.args[1], true));
|
|
456
|
+
native('min', '["The lesser of two values." a [any-value!] b [any-value!]]', (_t, f) => pickBy(f.args[0], f.args[1], false));
|
|
457
|
+
// --- logic -----------------------------------------------------------------------
|
|
458
|
+
/** Integers combine bitwise; anything else as logic by truthiness. */
|
|
459
|
+
function logicOp(a, b, int, bool) {
|
|
460
|
+
if (a.t === K.Integer && b.t === K.Integer) {
|
|
461
|
+
const r = I64.fromBig(BigInt.asIntN(64, int(BigInt(a.v), BigInt(b.v))));
|
|
462
|
+
return r === null ? overflow() : value(integer(r));
|
|
463
|
+
}
|
|
464
|
+
if (a.t === K.Binary && b.t === K.Binary) {
|
|
465
|
+
// byte by byte; as in R3, the shorter one repeats to the length of the longer
|
|
466
|
+
const x = a.s.data.slice(a.i);
|
|
467
|
+
const y = b.s.data.slice(b.i);
|
|
468
|
+
const out = [];
|
|
469
|
+
const n = Math.max(x.length, y.length);
|
|
470
|
+
for (let i = 0; i < n && x.length > 0 && y.length > 0; i++) {
|
|
471
|
+
out.push(Number(int(BigInt(x[i % x.length]), BigInt(y[i % y.length])) & 255n));
|
|
472
|
+
}
|
|
473
|
+
return value({ t: K.Binary, s: { data: out }, i: 0 });
|
|
474
|
+
}
|
|
475
|
+
return value(logic(bool(truthy(a), truthy(b))));
|
|
476
|
+
}
|
|
477
|
+
native('and~', '["Both values: bitwise for integers and binaries, logical for anything else." a [any-value!] b [any-value!]]', (_t, f) => logicOp(f.args[0], f.args[1], (x, y) => x & y, (x, y) => x && y));
|
|
478
|
+
native('or~', '["Either value: bitwise for integers and binaries, logical for anything else." a [any-value!] b [any-value!]]', (_t, f) => logicOp(f.args[0], f.args[1], (x, y) => x | y, (x, y) => x || y));
|
|
479
|
+
native('xor~', '["One value or the other but not both: bitwise for integers and binaries, logical for anything else." a [any-value!] b [any-value!]]', (_t, f) => logicOp(f.args[0], f.args[1], (x, y) => x ^ y, (x, y) => x !== y));
|
|
480
|
+
// --- operators -------------------------------------------------------------------
|
|
481
|
+
op('+', 'add');
|
|
482
|
+
op('-', 'subtract');
|
|
483
|
+
op('*', 'multiply');
|
|
484
|
+
op('/', 'divide');
|
|
485
|
+
op('//', 'remainder');
|
|
486
|
+
op('**', 'power');
|
|
487
|
+
op('<<', 'shift-left');
|
|
488
|
+
op('>>', 'shift-right');
|
|
489
|
+
op('>>>', 'shift-logical');
|
|
490
|
+
op('=', 'equal?');
|
|
491
|
+
op('<>', 'not-equal?');
|
|
492
|
+
op('==', 'strict-equal?');
|
|
493
|
+
op('!==', 'strict-not-equal?');
|
|
494
|
+
op('<', 'lesser?');
|
|
495
|
+
op('>', 'greater?');
|
|
496
|
+
op('<=', 'lesser-or-equal?');
|
|
497
|
+
op('>=', 'greater-or-equal?');
|
|
498
|
+
op('and', 'and~');
|
|
499
|
+
op('or', 'or~');
|
|
500
|
+
op('xor', 'xor~');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|