@magnusopera/fscript 0.75.0

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 (79) hide show
  1. package/README.md +5 -0
  2. package/dist/FScript.Language/Ast.js +234 -0
  3. package/dist/FScript.Language/BuiltinFunctions.js +2039 -0
  4. package/dist/FScript.Language/BuiltinSignatures.js +24 -0
  5. package/dist/FScript.Language/BuiltinTypes.js +13 -0
  6. package/dist/FScript.Language/Error.js +74 -0
  7. package/dist/FScript.Language/Eval.js +2404 -0
  8. package/dist/FScript.Language/IncludeResolver.js +806 -0
  9. package/dist/FScript.Language/Lexer.js +1067 -0
  10. package/dist/FScript.Language/Library.js +28 -0
  11. package/dist/FScript.Language/Parser.js +2368 -0
  12. package/dist/FScript.Language/Pretty.js +115 -0
  13. package/dist/FScript.Language/Span.js +33 -0
  14. package/dist/FScript.Language/Token.js +32 -0
  15. package/dist/FScript.Language/TypeInfer.js +2561 -0
  16. package/dist/FScript.Language/Types.js +205 -0
  17. package/dist/FScript.Language/Value.js +91 -0
  18. package/dist/Library.js +732 -0
  19. package/dist/fable_modules/fable-library-js.5.2.0/Array.js +1347 -0
  20. package/dist/fable_modules/fable-library-js.5.2.0/Async.js +134 -0
  21. package/dist/fable_modules/fable-library-js.5.2.0/AsyncBuilder.js +183 -0
  22. package/dist/fable_modules/fable-library-js.5.2.0/BigInt.js +306 -0
  23. package/dist/fable_modules/fable-library-js.5.2.0/BitConverter.js +138 -0
  24. package/dist/fable_modules/fable-library-js.5.2.0/Boolean.js +22 -0
  25. package/dist/fable_modules/fable-library-js.5.2.0/CHANGELOG.md +234 -0
  26. package/dist/fable_modules/fable-library-js.5.2.0/Char.js +202 -0
  27. package/dist/fable_modules/fable-library-js.5.2.0/Choice.js +184 -0
  28. package/dist/fable_modules/fable-library-js.5.2.0/CollectionUtil.js +182 -0
  29. package/dist/fable_modules/fable-library-js.5.2.0/ConditionalWeakTable.js +21 -0
  30. package/dist/fable_modules/fable-library-js.5.2.0/Date.js +841 -0
  31. package/dist/fable_modules/fable-library-js.5.2.0/DateOffset.js +261 -0
  32. package/dist/fable_modules/fable-library-js.5.2.0/DateOnly.js +124 -0
  33. package/dist/fable_modules/fable-library-js.5.2.0/Decimal.js +238 -0
  34. package/dist/fable_modules/fable-library-js.5.2.0/Double.js +47 -0
  35. package/dist/fable_modules/fable-library-js.5.2.0/Encoding.js +185 -0
  36. package/dist/fable_modules/fable-library-js.5.2.0/Event.js +93 -0
  37. package/dist/fable_modules/fable-library-js.5.2.0/FSharp.Collections.js +45 -0
  38. package/dist/fable_modules/fable-library-js.5.2.0/FSharp.Core.CompilerServices.js +27 -0
  39. package/dist/fable_modules/fable-library-js.5.2.0/FSharp.Core.js +156 -0
  40. package/dist/fable_modules/fable-library-js.5.2.0/Global.js +29 -0
  41. package/dist/fable_modules/fable-library-js.5.2.0/Guid.js +161 -0
  42. package/dist/fable_modules/fable-library-js.5.2.0/Int32.js +135 -0
  43. package/dist/fable_modules/fable-library-js.5.2.0/List.js +1323 -0
  44. package/dist/fable_modules/fable-library-js.5.2.0/Long.js +47 -0
  45. package/dist/fable_modules/fable-library-js.5.2.0/MailboxProcessor.js +94 -0
  46. package/dist/fable_modules/fable-library-js.5.2.0/Map.js +1396 -0
  47. package/dist/fable_modules/fable-library-js.5.2.0/MapUtil.js +130 -0
  48. package/dist/fable_modules/fable-library-js.5.2.0/MutableMap.js +339 -0
  49. package/dist/fable_modules/fable-library-js.5.2.0/MutableSet.js +233 -0
  50. package/dist/fable_modules/fable-library-js.5.2.0/Native.js +14 -0
  51. package/dist/fable_modules/fable-library-js.5.2.0/Numeric.js +75 -0
  52. package/dist/fable_modules/fable-library-js.5.2.0/Observable.js +113 -0
  53. package/dist/fable_modules/fable-library-js.5.2.0/Option.js +104 -0
  54. package/dist/fable_modules/fable-library-js.5.2.0/README.md +3 -0
  55. package/dist/fable_modules/fable-library-js.5.2.0/Random.js +172 -0
  56. package/dist/fable_modules/fable-library-js.5.2.0/Range.js +45 -0
  57. package/dist/fable_modules/fable-library-js.5.2.0/Reflection.js +479 -0
  58. package/dist/fable_modules/fable-library-js.5.2.0/RegExp.js +141 -0
  59. package/dist/fable_modules/fable-library-js.5.2.0/Result.js +165 -0
  60. package/dist/fable_modules/fable-library-js.5.2.0/Seq.js +1417 -0
  61. package/dist/fable_modules/fable-library-js.5.2.0/Seq2.js +111 -0
  62. package/dist/fable_modules/fable-library-js.5.2.0/Set.js +1816 -0
  63. package/dist/fable_modules/fable-library-js.5.2.0/String.js +674 -0
  64. package/dist/fable_modules/fable-library-js.5.2.0/System.Collections.Generic.js +333 -0
  65. package/dist/fable_modules/fable-library-js.5.2.0/System.Text.js +200 -0
  66. package/dist/fable_modules/fable-library-js.5.2.0/System.js +283 -0
  67. package/dist/fable_modules/fable-library-js.5.2.0/TimeOnly.js +122 -0
  68. package/dist/fable_modules/fable-library-js.5.2.0/TimeSpan.js +180 -0
  69. package/dist/fable_modules/fable-library-js.5.2.0/Timer.js +67 -0
  70. package/dist/fable_modules/fable-library-js.5.2.0/Types.js +198 -0
  71. package/dist/fable_modules/fable-library-js.5.2.0/Unicode.13.0.0.js +4 -0
  72. package/dist/fable_modules/fable-library-js.5.2.0/Uri.js +165 -0
  73. package/dist/fable_modules/fable-library-js.5.2.0/Util.js +795 -0
  74. package/dist/fable_modules/fable-library-js.5.2.0/big.d.ts +338 -0
  75. package/dist/fable_modules/fable-library-js.5.2.0/lib/big.js +825 -0
  76. package/dist/fable_modules/fable-library-js.5.2.0/package.json +24 -0
  77. package/dist/fable_modules/fable-library-js.5.2.0/quotation.js +618 -0
  78. package/dist/fable_modules/project_cracked.json +1 -0
  79. package/package.json +36 -0
@@ -0,0 +1,261 @@
1
+ /**
2
+ * DateTimeOffset functions.
3
+ *
4
+ * Note: DateOffset instances are always DateObjects in local
5
+ * timezone (because JS dates are all kinds of messed up).
6
+ * A local date returns UTC epoch when `.getTime()` is called.
7
+ *
8
+ * However, this means that in order to construct an UTC date
9
+ * from a DateOffset with offset of +5 hours, you first need
10
+ * to subtract those 5 hours, than add the "local" offset.
11
+ * As said, all kinds of messed up.
12
+ *
13
+ * Basically; invariant: date.getTime() always return UTC time.
14
+ */
15
+ import { fromFloat64, toFloat64 } from "./BigInt.js";
16
+ import DateTime, { create as createDate, daysInMonth, parseRaw, ticksToUnixEpochMilliseconds, unixEpochMillisecondsToTicks } from "./Date.js";
17
+ import { Exception, compareDates, DateTimeKind } from "./Util.js";
18
+ export default function DateTimeOffset(value, offset) {
19
+ checkOffsetInRange(offset);
20
+ const d = new Date(value);
21
+ d.offset = offset != null ? offset : new Date().getTimezoneOffset() * -60000;
22
+ return d;
23
+ }
24
+ export function offset(value) {
25
+ return value.offset || 0;
26
+ }
27
+ function checkOffsetInRange(offset) {
28
+ if (offset != null && offset !== 0) {
29
+ if (offset % 60000 !== 0) {
30
+ throw new Exception("Offset must be specified in whole minutes.");
31
+ }
32
+ if (Math.abs(offset / 3600000) > 14) {
33
+ throw new Exception("Offset must be within plus or minus 14 hours.");
34
+ }
35
+ }
36
+ }
37
+ export function fromDate(date, offset) {
38
+ let offset2 = 0;
39
+ switch (date.kind) {
40
+ case DateTimeKind.Utc:
41
+ if (offset != null && offset !== 0) {
42
+ throw new Exception("The UTC Offset for Utc DateTime instances must be 0.");
43
+ }
44
+ offset2 = 0;
45
+ break;
46
+ case DateTimeKind.Local:
47
+ offset2 = date.getTimezoneOffset() * -60000;
48
+ if (offset != null && offset !== offset2) {
49
+ throw new Exception("The UTC Offset of the local dateTime parameter does not match the offset argument.");
50
+ }
51
+ break;
52
+ case DateTimeKind.Unspecified:
53
+ default:
54
+ if (offset == null) {
55
+ offset2 = date.getTimezoneOffset() * -60000;
56
+ }
57
+ else {
58
+ offset2 = offset;
59
+ }
60
+ break;
61
+ }
62
+ return DateTimeOffset(date.getTime(), offset2);
63
+ }
64
+ export function fromDateTime(dateOnly, timeOnly, offset) {
65
+ return DateTimeOffset(dateOnly.getTime() - offset + timeOnly, offset);
66
+ }
67
+ export function fromTicks(ticks, offset) {
68
+ const ms = ticksToUnixEpochMilliseconds(ticks) - offset;
69
+ return DateTimeOffset(ms, offset);
70
+ }
71
+ export function fromUnixTimeMilliseconds(ms) {
72
+ return DateTimeOffset(toFloat64(ms), 0);
73
+ }
74
+ export function fromUnixTimeSeconds(seconds) {
75
+ return DateTimeOffset(toFloat64(seconds * 1000n), 0);
76
+ }
77
+ export function getUtcTicks(date) {
78
+ return unixEpochMillisecondsToTicks(date.getTime(), 0);
79
+ }
80
+ export function minValue() {
81
+ // This is "0001-01-01T00:00:00.000Z", actual JS min value is -8640000000000000
82
+ return DateTimeOffset(-62135596800000, 0);
83
+ }
84
+ export function maxValue() {
85
+ // This is "9999-12-31T23:59:59.999Z", actual JS max value is 8640000000000000
86
+ return DateTimeOffset(253402300799999, 0);
87
+ }
88
+ export function parse(str) {
89
+ const [date, offsetMatch] = parseRaw(str);
90
+ const offset = offsetMatch == null
91
+ ? date.getTimezoneOffset() * -60000
92
+ : (offsetMatch === "Z" ? 0 : offsetMatch * 60000);
93
+ return DateTimeOffset(date.getTime(), offset);
94
+ }
95
+ export function tryParse(v, defValue) {
96
+ try {
97
+ defValue.contents = parse(v);
98
+ return true;
99
+ }
100
+ catch (_err) {
101
+ return false;
102
+ }
103
+ }
104
+ export function create(year, month, day, h, m, s, ms, offset) {
105
+ if (offset == null) {
106
+ offset = ms;
107
+ ms = 0;
108
+ }
109
+ checkOffsetInRange(offset);
110
+ let date = new Date(Date.UTC(year, month - 1, day, h, m, s, ms) - offset);
111
+ if (year <= 99) {
112
+ date.setUTCFullYear(year, month - 1, day);
113
+ }
114
+ const dateValue = date.getTime();
115
+ if (isNaN(dateValue)) {
116
+ throw new Exception("The parameters describe an unrepresentable Date");
117
+ }
118
+ return DateTimeOffset(dateValue, offset);
119
+ }
120
+ export function now() {
121
+ const date = new Date();
122
+ const offset = date.getTimezoneOffset() * -60000;
123
+ return DateTimeOffset(date.getTime(), offset);
124
+ }
125
+ export function utcNow() {
126
+ const date = new Date();
127
+ // const offset = date.getTimezoneOffset() * -60_000;
128
+ return DateTimeOffset(date.getTime(), 0);
129
+ }
130
+ export function toUniversalTime(date) {
131
+ return DateTime(date.getTime(), DateTimeKind.Utc);
132
+ }
133
+ export function toLocalTime(date) {
134
+ return DateTime(date.getTime(), DateTimeKind.Local);
135
+ }
136
+ export function localDateTime(date) {
137
+ return DateTime(date.getTime(), DateTimeKind.Local);
138
+ }
139
+ export function timeOfDay(d) {
140
+ const d2 = new Date(d.getTime() + offset(d));
141
+ return d2.getUTCHours() * 3600000
142
+ + d2.getUTCMinutes() * 60000
143
+ + d2.getUTCSeconds() * 1000
144
+ + d2.getUTCMilliseconds();
145
+ }
146
+ export function date(d) {
147
+ const d2 = new Date(d.getTime() + offset(d));
148
+ return createDate(d2.getUTCFullYear(), d2.getUTCMonth() + 1, d2.getUTCDate(), 0, 0, 0, 0);
149
+ }
150
+ export function day(d) {
151
+ return new Date(d.getTime() + offset(d)).getUTCDate();
152
+ }
153
+ export function hour(d) {
154
+ return new Date(d.getTime() + offset(d)).getUTCHours();
155
+ }
156
+ export function millisecond(d) {
157
+ return new Date(d.getTime() + offset(d)).getUTCMilliseconds();
158
+ }
159
+ export function minute(d) {
160
+ return new Date(d.getTime() + offset(d)).getUTCMinutes();
161
+ }
162
+ export function month(d) {
163
+ return new Date(d.getTime() + offset(d)).getUTCMonth() + 1;
164
+ }
165
+ export function second(d) {
166
+ return new Date(d.getTime() + offset(d)).getUTCSeconds();
167
+ }
168
+ export function year(d) {
169
+ return new Date(d.getTime() + offset(d)).getUTCFullYear();
170
+ }
171
+ export function dayOfWeek(d) {
172
+ return new Date(d.getTime() + offset(d)).getUTCDay();
173
+ }
174
+ export function dayOfYear(d) {
175
+ const d2 = new Date(d.getTime() + offset(d));
176
+ const _year = d2.getUTCFullYear();
177
+ const _month = d2.getUTCMonth() + 1;
178
+ let _day = d2.getUTCDate();
179
+ for (let i = 1; i < _month; i++) {
180
+ _day += daysInMonth(_year, i);
181
+ }
182
+ return _day;
183
+ }
184
+ export function add(d, ts) {
185
+ return DateTimeOffset(d.getTime() + ts, offset(d));
186
+ }
187
+ export function addDays(d, v) {
188
+ return add(d, v * 86400000);
189
+ }
190
+ export function addHours(d, v) {
191
+ return add(d, v * 3600000);
192
+ }
193
+ export function addMinutes(d, v) {
194
+ return add(d, v * 60000);
195
+ }
196
+ export function addSeconds(d, v) {
197
+ return add(d, v * 1000);
198
+ }
199
+ export function addMilliseconds(d, v) {
200
+ return add(d, v);
201
+ }
202
+ export function addTicks(d, v) {
203
+ return add(d, toFloat64(v / 10000n));
204
+ }
205
+ export function addYears(d, v) {
206
+ const newMonth = d.getUTCMonth() + 1;
207
+ const newYear = d.getUTCFullYear() + v;
208
+ const _daysInMonth = daysInMonth(newYear, newMonth);
209
+ const newDay = Math.min(_daysInMonth, d.getUTCDate());
210
+ return create(newYear, newMonth, newDay, d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds(), offset(d));
211
+ }
212
+ export function addMonths(d, v) {
213
+ const d2 = new Date(d.getTime() + offset(d));
214
+ let newMonth = d2.getUTCMonth() + 1 + v;
215
+ let newMonth_ = 0;
216
+ let yearOffset = 0;
217
+ if (newMonth > 12) {
218
+ newMonth_ = newMonth % 12;
219
+ yearOffset = Math.floor(newMonth / 12);
220
+ newMonth = newMonth_;
221
+ }
222
+ else if (newMonth < 1) {
223
+ newMonth_ = 12 + newMonth % 12;
224
+ yearOffset = Math.floor(newMonth / 12) + (newMonth_ === 12 ? -1 : 0);
225
+ newMonth = newMonth_;
226
+ }
227
+ const newYear = d2.getUTCFullYear() + yearOffset;
228
+ const _daysInMonth = daysInMonth(newYear, newMonth);
229
+ const newDay = Math.min(_daysInMonth, d2.getUTCDate());
230
+ return create(newYear, newMonth, newDay, d2.getUTCHours(), d2.getUTCMinutes(), d2.getUTCSeconds(), d2.getUTCMilliseconds(), offset(d));
231
+ }
232
+ export function subtract(d, that) {
233
+ return typeof that === "number"
234
+ ? DateTimeOffset(d.getTime() - that, offset(d))
235
+ : d.getTime() - that.getTime();
236
+ }
237
+ export function equals(d1, d2) {
238
+ return d1.getTime() === d2.getTime();
239
+ }
240
+ export function equalsExact(d1, d2) {
241
+ return d1.getTime() === d2.getTime() && d1.offset === d2.offset;
242
+ }
243
+ export function compare(d1, d2) {
244
+ return compareDates(d1, d2);
245
+ }
246
+ export const compareTo = compare;
247
+ export function op_Addition(x, y) {
248
+ return add(x, y);
249
+ }
250
+ export function op_Subtraction(x, y) {
251
+ return subtract(x, y);
252
+ }
253
+ export function toOffset(d, offset) {
254
+ return DateTimeOffset(d.getTime(), offset);
255
+ }
256
+ export function toUnixTimeMilliseconds(d) {
257
+ return fromFloat64(d.getTime());
258
+ }
259
+ export function toUnixTimeSeconds(d) {
260
+ return fromFloat64(d.getTime() / 1000.0);
261
+ }
@@ -0,0 +1,124 @@
1
+ import { DateTime, getTicks, dayOfYear as Date_dayOfYear, year as Date_year, month as Date_month, day as Date_day, daysInMonth as Date_daysInMonth, ticksToUnixEpochMilliseconds } from "./Date.js";
2
+ import { Exception, DateTimeKind, padWithZeros } from "./Util.js";
3
+ export function fromUnixMilliseconds(value) {
4
+ return DateTime(value, DateTimeKind.Utc);
5
+ }
6
+ export function create(year, month, day) {
7
+ const d = fromUnixMilliseconds(Date.UTC(year, month - 1, day));
8
+ if (year <= 99) {
9
+ d.setUTCFullYear(year);
10
+ }
11
+ return d;
12
+ }
13
+ export function maxValue() {
14
+ // This is "9999-12-31T00:00:00.000Z"
15
+ return fromUnixMilliseconds(253402214400000);
16
+ }
17
+ export function minValue() {
18
+ // This is "0001-01-01T00:00:00.000Z"
19
+ return fromUnixMilliseconds(-62135596800000);
20
+ }
21
+ export function dayNumber(d) {
22
+ return Number((getTicks(d) / 864000000000n));
23
+ }
24
+ export function fromDayNumber(dayNumber) {
25
+ const ticks = 864000000000n * BigInt(dayNumber);
26
+ return fromUnixMilliseconds(ticksToUnixEpochMilliseconds(ticks));
27
+ }
28
+ export function fromDateTime(d) {
29
+ return create(Date_year(d), Date_month(d), Date_day(d));
30
+ }
31
+ export function day(d) {
32
+ return d.getUTCDate();
33
+ }
34
+ export function month(d) {
35
+ return d.getUTCMonth() + 1;
36
+ }
37
+ export function year(d) {
38
+ return d.getUTCFullYear();
39
+ }
40
+ export function dayOfWeek(d) {
41
+ return d.getUTCDay();
42
+ }
43
+ export function dayOfYear(d) {
44
+ return Date_dayOfYear(d);
45
+ }
46
+ export function toDateTime(d, time, kind = DateTimeKind.Unspecified) {
47
+ return DateTime(d.getTime() + time + (kind !== DateTimeKind.Utc ? d.getTimezoneOffset() : 0) * 60000, kind);
48
+ }
49
+ export function toString(d, format = "d", _provider) {
50
+ if (["d", "o", "O"].indexOf(format) === -1) {
51
+ throw new Exception("Custom formats are not supported");
52
+ }
53
+ const y = padWithZeros(year(d), 4);
54
+ const m = padWithZeros(month(d), 2);
55
+ const dd = padWithZeros(day(d), 2);
56
+ return format === "d" ? `${m}/${dd}/${y}` : `${y}-${m}-${dd}`;
57
+ }
58
+ export function parse(str) {
59
+ function fail() {
60
+ throw new Exception(`String '${str}' was not recognized as a valid DateOnly.`);
61
+ }
62
+ // Allowed separators: . , / -
63
+ // TODO whitespace alone as the separator
64
+ //
65
+ // Whitespace around separators
66
+ //
67
+ // Allowed format types:
68
+ // yyyy/mm/dd
69
+ // mm/dd/yyyy
70
+ // mm/dd
71
+ // mm/yyyy
72
+ // yyyy/mm
73
+ const r = /^\s*(\d{1,4})(?:\s*[.,-\/]\s*(\d{1,2}))?\s*[.,-\/]\s*(\d{1,4})\s*$/.exec(str);
74
+ if (r != null) {
75
+ let y = 0;
76
+ let m = 0;
77
+ let d = 1;
78
+ if (r[2] == null) {
79
+ if (r[1].length < 3) {
80
+ if (r[3].length < 3) {
81
+ // 12/30 = December 30, {CurrentYear}
82
+ y = new Date().getFullYear();
83
+ m = +r[1];
84
+ d = +r[3];
85
+ }
86
+ else {
87
+ // 12/2000 = December 1, 2000
88
+ m = +r[1];
89
+ y = +r[3];
90
+ }
91
+ }
92
+ else {
93
+ if (r[3].length > 2)
94
+ fail();
95
+ // 2000/12 = December 1, 2000
96
+ y = +r[1];
97
+ m = +r[3];
98
+ }
99
+ }
100
+ else {
101
+ // 2000/1/30 or 1/30/2000
102
+ const yearFirst = r[1].length > 2;
103
+ const yTmp = r[yearFirst ? 1 : 3];
104
+ y = +yTmp;
105
+ // year 0-29 is 2000-2029, 30-99 is 1930-1999
106
+ if (yTmp.length < 3)
107
+ y += y >= 30 ? 1900 : 2000;
108
+ m = +r[yearFirst ? 2 : 1];
109
+ d = +r[yearFirst ? 3 : 2];
110
+ }
111
+ if (y > 0 && m > 0 && m < 13 && d > 0 && d <= Date_daysInMonth(y, m))
112
+ return create(y, m, d);
113
+ }
114
+ return fail();
115
+ }
116
+ export function tryParse(v, defValue) {
117
+ try {
118
+ defValue.contents = parse(v);
119
+ return true;
120
+ }
121
+ catch {
122
+ return false;
123
+ }
124
+ }
@@ -0,0 +1,238 @@
1
+ import Decimal from "./lib/big.js";
2
+ import { symbol } from "./Numeric.js";
3
+ import { FSharpRef } from "./Types.js";
4
+ import { Exception, combineHashCodes } from "./Util.js";
5
+ import { fromDecimal } from "./BigInt.js";
6
+ import * as bigInt from "./BigInt.js";
7
+ Decimal.prototype.GetHashCode = function () {
8
+ return combineHashCodes([this.s, this.e].concat(this.c));
9
+ };
10
+ Decimal.prototype.Equals = function (x) {
11
+ return !this.cmp(x);
12
+ };
13
+ Decimal.prototype.CompareTo = function (x) {
14
+ return this.cmp(x);
15
+ };
16
+ Decimal.prototype[symbol] = function () {
17
+ const _this = this;
18
+ return {
19
+ multiply: (y) => _this.mul(y),
20
+ toPrecision: (sd) => _this.toPrecision(sd),
21
+ toExponential: (dp) => _this.toExponential(dp),
22
+ toFixed: (dp) => _this.toFixed(dp),
23
+ toHex: () => (Number(_this) >>> 0).toString(16),
24
+ };
25
+ };
26
+ export default Decimal;
27
+ export const get_Zero = new Decimal(0);
28
+ export const get_One = new Decimal(1);
29
+ export const get_MinusOne = new Decimal(-1);
30
+ export const get_MaxValue = new Decimal("79228162514264337593543950335");
31
+ export const get_MinValue = new Decimal("-79228162514264337593543950335");
32
+ export function compare(x, y) {
33
+ return x.cmp(y);
34
+ }
35
+ export function equals(x, y) {
36
+ return !x.cmp(y);
37
+ }
38
+ export function abs(x) { return x.abs(); }
39
+ export function sign(x) { return x.lt(get_Zero) ? -1 : x.gt(get_Zero) ? 1 : 0; }
40
+ export function max(x, y) { return x.gt(y) ? x : y; }
41
+ export function min(x, y) { return x.lt(y) ? x : y; }
42
+ export function maxMagnitude(x, y) { return abs(x).gt(abs(y)) ? x : y; }
43
+ export function minMagnitude(x, y) { return abs(x).lt(abs(y)) ? x : y; }
44
+ export function clamp(x, min, max) {
45
+ return x.lt(min) ? min : x.gt(max) ? max : x;
46
+ }
47
+ export function round(x, digits = 0) {
48
+ return x.round(digits, 2 /* ROUND_HALF_EVEN */);
49
+ }
50
+ export function truncate(x) {
51
+ return x.round(0, 0 /* ROUND_DOWN */);
52
+ }
53
+ export function ceiling(x) {
54
+ return x.round(0, x.cmp(0) >= 0 ? 3 /* ROUND_UP */ : 0 /* ROUND_DOWN */);
55
+ }
56
+ export function floor(x) {
57
+ return x.round(0, x.cmp(0) >= 0 ? 0 /* ROUND_DOWN */ : 3 /* ROUND_UP */);
58
+ }
59
+ export function pow(x, n) {
60
+ return x.pow(n);
61
+ }
62
+ export function sqrt(x) {
63
+ return x.sqrt();
64
+ }
65
+ export function op_Addition(x, y) {
66
+ return x.add(y);
67
+ }
68
+ export function op_Subtraction(x, y) {
69
+ return x.sub(y);
70
+ }
71
+ export function op_Multiply(x, y) {
72
+ return x.mul(y);
73
+ }
74
+ export function op_Division(x, y) {
75
+ return x.div(y);
76
+ }
77
+ export function op_Modulus(x, y) {
78
+ return x.mod(y);
79
+ }
80
+ export function op_UnaryNegation(x) {
81
+ const x2 = new Decimal(x);
82
+ x2.s = -x2.s || 0;
83
+ return x2;
84
+ }
85
+ export function op_UnaryPlus(x) {
86
+ return x;
87
+ }
88
+ export const add = op_Addition;
89
+ export const subtract = op_Subtraction;
90
+ export const multiply = op_Multiply;
91
+ export const divide = op_Division;
92
+ export const remainder = op_Modulus;
93
+ export const negate = op_UnaryNegation;
94
+ export function toString(x) {
95
+ return x.toString();
96
+ }
97
+ export function tryParse(str, defValue) {
98
+ try {
99
+ defValue.contents = new Decimal(str.trim());
100
+ return true;
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
106
+ export function parse(str) {
107
+ const defValue = new FSharpRef(get_Zero);
108
+ if (tryParse(str, defValue)) {
109
+ return defValue.contents;
110
+ }
111
+ else {
112
+ throw new Exception(`The input string ${str} was not in a correct format.`);
113
+ }
114
+ }
115
+ export function toNumber(x) {
116
+ return +x;
117
+ }
118
+ export function toChar(x) {
119
+ const n = toNumber(x);
120
+ if (n < 0 || n > 65535 || isNaN(n)) {
121
+ throw new Exception("Value was either too large or too small for a character.");
122
+ }
123
+ return String.fromCharCode(n);
124
+ }
125
+ export function toInt8(x) { return bigInt.toInt8(fromDecimal(x)); }
126
+ export function toUInt8(x) { return bigInt.toUInt8(fromDecimal(x)); }
127
+ export function toInt16(x) { return bigInt.toInt16(fromDecimal(x)); }
128
+ export function toUInt16(x) { return bigInt.toUInt16(fromDecimal(x)); }
129
+ export function toInt32(x) { return bigInt.toInt32(fromDecimal(x)); }
130
+ export function toUInt32(x) { return bigInt.toUInt32(fromDecimal(x)); }
131
+ export function toInt64(x) { return bigInt.toInt64(fromDecimal(x)); }
132
+ export function toUInt64(x) { return bigInt.toUInt64(fromDecimal(x)); }
133
+ export function toInt128(x) { return bigInt.toInt128(fromDecimal(x)); }
134
+ export function toUInt128(x) { return bigInt.toUInt128(fromDecimal(x)); }
135
+ export function toNativeInt(x) { return bigInt.toNativeInt(fromDecimal(x)); }
136
+ export function toUNativeInt(x) { return bigInt.toUNativeInt(fromDecimal(x)); }
137
+ export function toFloat16(x) { return toNumber(x); }
138
+ export function toFloat32(x) { return toNumber(x); }
139
+ export function toFloat64(x) { return toNumber(x); }
140
+ function decimalToHex(dec, bitSize) {
141
+ const hex = new Uint8Array(bitSize / 4 | 0);
142
+ let hexCount = 1;
143
+ for (let d = 0; d < dec.length; d++) {
144
+ let value = dec[d];
145
+ for (let i = 0; i < hexCount; i++) {
146
+ const digit = hex[i] * 10 + value | 0;
147
+ hex[i] = digit & 0xF;
148
+ value = digit >> 4;
149
+ }
150
+ if (value !== 0) {
151
+ hex[hexCount++] = value;
152
+ }
153
+ }
154
+ return hex.slice(0, hexCount); // digits in reverse order
155
+ }
156
+ function hexToDecimal(hex, bitSize) {
157
+ const dec = new Uint8Array(bitSize * 301 / 1000 + 1 | 0);
158
+ let decCount = 1;
159
+ for (let d = hex.length - 1; d >= 0; d--) {
160
+ let carry = hex[d];
161
+ for (let i = 0; i < decCount; i++) {
162
+ const val = dec[i] * 16 + carry | 0;
163
+ dec[i] = (val % 10) | 0;
164
+ carry = (val / 10) | 0;
165
+ }
166
+ while (carry > 0) {
167
+ dec[decCount++] = (carry % 10) | 0;
168
+ carry = (carry / 10) | 0;
169
+ }
170
+ }
171
+ return dec.slice(0, decCount); // digits in reverse order
172
+ }
173
+ function setInt32Bits(hexDigits, bits, offset) {
174
+ for (let i = 0; i < 8; i++) {
175
+ hexDigits[offset + i] = (bits >> (i * 4)) & 0xF;
176
+ }
177
+ }
178
+ function getInt32Bits(hexDigits, offset) {
179
+ let bits = 0;
180
+ for (let i = 0; i < 8; i++) {
181
+ bits = bits | (hexDigits[offset + i] << (i * 4));
182
+ }
183
+ return bits;
184
+ }
185
+ export function fromIntArray(bits) {
186
+ return fromInts(bits[0], bits[1], bits[2], bits[3]);
187
+ }
188
+ export function fromInts(low, mid, high, signExp) {
189
+ const isNegative = signExp < 0;
190
+ const scale = (signExp >> 16) & 0x7F;
191
+ return fromParts(low, mid, high, isNegative, scale);
192
+ }
193
+ export function fromParts(low, mid, high, isNegative, scale) {
194
+ const bitSize = 96;
195
+ const hexDigits = new Uint8Array(bitSize / 4);
196
+ setInt32Bits(hexDigits, low, 0);
197
+ setInt32Bits(hexDigits, mid, 8);
198
+ setInt32Bits(hexDigits, high, 16);
199
+ const decDigits = hexToDecimal(hexDigits, bitSize);
200
+ scale = scale & 0x7F;
201
+ const big = new Decimal(0);
202
+ big.c = Array.from(decDigits.reverse());
203
+ big.e = decDigits.length - scale - 1;
204
+ big.s = isNegative ? -1 : 1;
205
+ const d = new Decimal(big);
206
+ return d;
207
+ }
208
+ export function getBits(d) {
209
+ const bitSize = 96;
210
+ const decStr = d.toString();
211
+ const absStr = decStr[0] === "-" ? decStr.slice(1) : decStr;
212
+ const dotPos = absStr.indexOf(".");
213
+ const scale = dotPos < 0 ? 0 : absStr.length - dotPos - 1;
214
+ const mantissaStr = absStr.replace(".", "");
215
+ const decDigits = Uint8Array.from(mantissaStr, ch => ch.charCodeAt(0) - 48);
216
+ const hexDigits = decimalToHex(decDigits, bitSize);
217
+ const low = getInt32Bits(hexDigits, 0);
218
+ const mid = getInt32Bits(hexDigits, 8);
219
+ const high = getInt32Bits(hexDigits, 16);
220
+ const signExp = ((scale & 0x7F) << 16) | (d.s < 0 ? 0x80000000 : 0);
221
+ return [low, mid, high, signExp];
222
+ }
223
+ // export function makeRangeStepFunction(step: Decimal, last: Decimal) {
224
+ // const stepComparedWithZero = step.cmp(get_Zero);
225
+ // if (stepComparedWithZero === 0) {
226
+ // throw new Exception("The step of a range cannot be zero");
227
+ // }
228
+ // const stepGreaterThanZero = stepComparedWithZero > 0;
229
+ // return (x: Decimal) => {
230
+ // const comparedWithLast = x.cmp(last);
231
+ // if ((stepGreaterThanZero && comparedWithLast <= 0)
232
+ // || (!stepGreaterThanZero && comparedWithLast >= 0)) {
233
+ // return [x, op_Addition(x, step)];
234
+ // } else {
235
+ // return undefined;
236
+ // }
237
+ // };
238
+ // }
@@ -0,0 +1,47 @@
1
+ import { FSharpRef } from "./Types.js";
2
+ import { Exception } from "./Util.js";
3
+ export function tryParse(str, defValue) {
4
+ // TODO: test if value is valid and in range
5
+ if (str != null && /\S/.test(str)) {
6
+ const v = +str.replace("_", "");
7
+ if (!Number.isNaN(v)) {
8
+ defValue.contents = v;
9
+ return true;
10
+ }
11
+ }
12
+ return false;
13
+ }
14
+ export function parse(str) {
15
+ const defValue = new FSharpRef(0);
16
+ if (tryParse(str, defValue)) {
17
+ return defValue.contents;
18
+ }
19
+ else {
20
+ throw new Exception(`The input string ${str} was not in a correct format.`);
21
+ }
22
+ }
23
+ // JS Number.isFinite function evals false for NaN
24
+ export function isPositiveInfinity(x) {
25
+ return x === Number.POSITIVE_INFINITY;
26
+ }
27
+ export function isNegativeInfinity(x) {
28
+ return x === Number.NEGATIVE_INFINITY;
29
+ }
30
+ export function isInfinity(x) {
31
+ return x === Number.POSITIVE_INFINITY || x === Number.NEGATIVE_INFINITY;
32
+ }
33
+ export function max(x, y) {
34
+ return x > y ? x : y;
35
+ }
36
+ export function min(x, y) {
37
+ return x < y ? x : y;
38
+ }
39
+ export function maxMagnitude(x, y) {
40
+ return Math.abs(x) > Math.abs(y) ? x : y;
41
+ }
42
+ export function minMagnitude(x, y) {
43
+ return Math.abs(x) < Math.abs(y) ? x : y;
44
+ }
45
+ export function clamp(x, min, max) {
46
+ return x < min ? min : x > max ? max : x;
47
+ }