functionalscript 0.2.4 → 0.2.6

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 (63) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +1 -1
  3. package/crypto/README.md +3 -0
  4. package/{prime_field → crypto/prime_field}/module.f.mjs +2 -2
  5. package/{secp → crypto/secp}/module.f.mjs +2 -2
  6. package/{sha2 → crypto/sha2}/module.f.mjs +4 -4
  7. package/{sha2 → crypto/sha2}/test.f.mjs +2 -2
  8. package/djs/module.f.mjs +4 -1
  9. package/djs/parser/module.f.mjs +122 -45
  10. package/djs/parser/test.f.mjs +30 -4
  11. package/djs/tokenizer/module.f.mjs +6 -2
  12. package/djs/tokenizer/test.f.mjs +34 -0
  13. package/html/README.md +10 -9
  14. package/html/module.f.mjs +18 -33
  15. package/html/test.f.mjs +6 -6
  16. package/issues/05-publish.md +17 -22
  17. package/issues/README.md +13 -2
  18. package/js/tokenizer/module.f.mjs +75 -26
  19. package/js/tokenizer/test.f.mjs +42 -0
  20. package/json/parser/test.f.mjs +6 -0
  21. package/jsr.json +4 -4
  22. package/nanvm-lib/src/extension.rs +1 -1
  23. package/nanvm-lib/src/nanenum.rs +22 -20
  24. package/out/{prime_field → crypto/prime_field}/module.f.d.mts +1 -1
  25. package/out/{prime_field → crypto/prime_field}/module.f.mjs +2 -2
  26. package/out/{secp → crypto/secp}/module.f.d.mts +1 -1
  27. package/out/{secp → crypto/secp}/module.f.mjs +2 -2
  28. package/out/{sha2 → crypto/sha2}/module.f.d.mts +3 -3
  29. package/out/{sha2 → crypto/sha2}/module.f.mjs +4 -4
  30. package/out/{sha2 → crypto/sha2}/test.f.mjs +2 -2
  31. package/out/djs/module.f.d.mts +1 -1
  32. package/out/djs/module.f.mjs +5 -1
  33. package/out/djs/parser/module.f.d.mts +1 -1
  34. package/out/djs/parser/module.f.mjs +105 -89
  35. package/out/djs/parser/test.f.d.mts +1 -0
  36. package/out/djs/parser/test.f.mjs +38 -4
  37. package/out/djs/tokenizer/module.f.d.mts +2 -2
  38. package/out/djs/tokenizer/module.f.mjs +6 -2
  39. package/out/djs/tokenizer/test.f.d.mts +1 -0
  40. package/out/djs/tokenizer/test.f.mjs +50 -0
  41. package/out/html/module.f.d.mts +8 -6
  42. package/out/html/module.f.mjs +17 -31
  43. package/out/html/test.f.mjs +5 -5
  44. package/out/js/tokenizer/module.f.d.mts +15 -3
  45. package/out/js/tokenizer/module.f.mjs +65 -22
  46. package/out/js/tokenizer/test.f.d.mts +1 -0
  47. package/out/js/tokenizer/test.f.mjs +62 -0
  48. package/out/json/parser/test.f.mjs +8 -0
  49. package/out/types/bit_vec/module.f.d.mts +108 -22
  50. package/out/types/bit_vec/module.f.mjs +142 -31
  51. package/out/types/bit_vec/test.f.d.mts +20 -6
  52. package/out/types/bit_vec/test.f.mjs +160 -10
  53. package/package.json +1 -1
  54. package/types/bit_vec/README.md +18 -0
  55. package/types/bit_vec/module.f.mjs +147 -31
  56. package/types/bit_vec/test.f.mjs +101 -13
  57. /package/{prime_field → crypto/prime_field}/test.f.mjs +0 -0
  58. /package/{secp → crypto/secp}/test.f.mjs +0 -0
  59. /package/out/{prime_field → crypto/prime_field}/test.f.d.mts +0 -0
  60. /package/out/{prime_field → crypto/prime_field}/test.f.mjs +0 -0
  61. /package/out/{secp → crypto/secp}/test.f.d.mts +0 -0
  62. /package/out/{secp → crypto/secp}/test.f.mjs +0 -0
  63. /package/out/{sha2 → crypto/sha2}/test.f.d.mts +0 -0
@@ -1,5 +1,6 @@
1
1
  // @ts-self-types="./module.f.d.mts"
2
2
  import { log2 } from '../bigint/module.f.mjs';
3
+ import { flip } from '../function/module.f.mjs';
3
4
  /**
4
5
  * A vector of bits represented as a `bigint`.
5
6
  *
@@ -12,72 +13,182 @@ export const empty = 1n;
12
13
  /**
13
14
  * Calculates the length of the given vector of bits.
14
15
  */
15
- export const len = log2;
16
+ export const length = log2;
16
17
  /**
17
- * Extract an unsigned integer of the given `uintLen` from the given vector.
18
+ * Creates a vector of bits of the given `len` and the given unsigned integer.
18
19
  *
19
- * @type {(uintLen: bigint) => (v: Vec) => bigint}
20
+ * @type {(len: bigint) => (ui: bigint) => Vec}
20
21
  *
21
22
  * @example
22
23
  *
23
24
  * ```js
24
- * const vector = 0b110101n;
25
- * const extract3Bits = uint(3n);
26
- * const result = extract3Bits(vector); // result is 0b101n (5n)
25
+ * const vec4 = vec(4n)
26
+ * const v0 = vec4(5n) // 0x15n
27
+ * const v1 = vec4(0x5FEn) // 0x1En
27
28
  * ```
28
29
  */
29
- export const uint = uintLen => {
30
- const mask = (1n << uintLen) - 1n;
31
- return data => data & mask;
30
+ export const vec = len => {
31
+ if (len <= 0n) {
32
+ return () => empty;
33
+ }
34
+ const stop = 1n << len;
35
+ const mask = stop - 1n;
36
+ return data => stop | (data & mask);
32
37
  };
38
+ /** @type {(len: bigint) => bigint} */
39
+ const mask = len => (1n << len) - 1n;
33
40
  /**
34
- * Creates a vector of bits of the given `vecLen` from the given unsigned integer.
41
+ * Returns the unsigned integer of the given vector by removing a stop bit.
35
42
  *
36
- * @type {(vecLen: bigint) => (ui: bigint) => Vec}
43
+ * @type {(len: Vec) => bigint}
37
44
  *
38
45
  * @example
39
46
  *
40
47
  * ```js
41
- * const createVector = vec(4n);
42
- * const vector = createVector(5n); // vector is 0b10101n
48
+ * const vector = vec(8n)(0x5n) // 0x105n
49
+ * const result = uint(vector); // result is 0x5n
43
50
  * ```
44
51
  */
45
- export const vec = vecLen => {
46
- const stop = 1n << vecLen;
47
- const mask = stop - 1n;
48
- return data => (data & mask) | stop;
49
- };
52
+ export const uint = v => v ^ (1n << length(v));
50
53
  /**
51
- * Appends the vector `b` to the back of the vector `a`.
54
+ * Extract the least significant unsigned integer from the given vector.
52
55
  *
53
- * @type {(a: Vec) => (b: Vec) => Vec}
56
+ * @type {(uintLen: bigint) => (v: Vec) => bigint}
54
57
  *
55
58
  * @example
56
59
  *
57
60
  * ```js
58
- * const vec8 = vec(8n)
59
- * const a = vec8(0x345n)
60
- * const b = vec8(0x789n)
61
- * const ab = appendBack(a)(b) // 0x18945n
61
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
62
+ * const result = uintLsb(4n)(vector); // result is 5n
63
+ * const result2 = uintLsb(16n)(vector); // result2 is 0xF5n
62
64
  * ```
63
65
  */
64
- export const appendBack = a => {
65
- const aLen = len(a);
66
- return b => a | (b << aLen);
66
+ export const uintLsb = len => {
67
+ const m = mask(len);
68
+ return v => {
69
+ const result = v & m;
70
+ return result === v ? uint(v) : result;
71
+ };
67
72
  };
68
73
  /**
69
- * Removes the first `len` bits from the given vector.
74
+ * Removes the first `len` least significant bits from the given vector.
70
75
  *
71
76
  * @type {(len: bigint) => (v: Vec) => Vec}
72
77
  *
73
78
  * @example
74
79
  *
75
80
  * ```js
76
- * const v = vec(17n)(0x12345n) // v = 0x32345n
77
- * const r = removeFront(9n)(v) // r = 0x191n
81
+ * const v = vec(16n)(0x3456n) // 0x13456n
82
+ * const r = removeLsb(4n)(v) // 0x1345n
83
+ * const r2 = removeLsb(24n)(v) // 0x1n
78
84
  * ```
79
85
  */
80
- export const removeFront = len => v => {
86
+ export const removeLsb = len => v => {
81
87
  const r = v >> len;
82
88
  return r === 0n ? empty : r;
83
89
  };
90
+ /**
91
+ * Extracts the least significant unsigned integer and removes it from the vector.
92
+ *
93
+ * @type {(len: bigint) => (v: Vec) => [bigint, Vec]}
94
+ *
95
+ * @example
96
+ *
97
+ * ```js
98
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
99
+ * const [result, rest] = popUintLsb(4n)(vector); // result is 5n, rest is 0x1Fn
100
+ * const [result2, rest2] = popUintLsb(16n)(vector); // result2 is 0xF5n, rest2 is 1n
101
+ * ```
102
+ */
103
+ export const popUintLsb = len => {
104
+ const m = mask(len);
105
+ return v => {
106
+ const result = v & m;
107
+ return result === v ? [uint(v), empty] : [result, v >> len];
108
+ };
109
+ };
110
+ /**
111
+ * Extract the most significant unsigned integer of the given `len` from the given vector.
112
+ *
113
+ * @type {(len: bigint) => (v: Vec) => bigint}
114
+ *
115
+ * @example
116
+ *
117
+ * ```js
118
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
119
+ * const result = uintMsb(4n)(vector); // result is 0xFn
120
+ * const result2 = uintMsb(16n)(vector); // result2 is 0xF500n
121
+ * ```
122
+ */
123
+ export const uintMsb = len => {
124
+ const m = mask(len);
125
+ return v => (v >> (length(v) - len)) & m;
126
+ };
127
+ /**
128
+ * Removes the first `len` most significant bits from the given vector.
129
+ *
130
+ * @type {(len: bigint) => (v: Vec) => Vec}
131
+ *
132
+ * @example
133
+ *
134
+ * ```js
135
+ * const v = vec(16n)(0x3456n) // 0x13456n
136
+ * const r = removeMsb(4n)(v) // 0x1456n
137
+ * const r2 = removeMsb(24n)(v) // 0x1n
138
+ * ```
139
+ */
140
+ export const removeMsb = len => v => vec(length(v) - len)(v);
141
+ /**
142
+ * Extracts the most significant unsigned integer and removes it from the vector.
143
+ *
144
+ * @type {(len: bigint) => (v: Vec) => [bigint, Vec]}
145
+ *
146
+ * @example
147
+ *
148
+ * ```js
149
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
150
+ * const [result, rest] = popUintMsb(4n)(vector); // [0xFn, 0x15n]
151
+ * const [result2, rest2] = popUintMsb(16n)(vector); // [0xF500n, 1n]
152
+ * ```
153
+ */
154
+ export const popUintMsb = len => {
155
+ const m = mask(len);
156
+ return v => {
157
+ const d = length(v) - len;
158
+ return [(v >> d) & m, vec(d)(v)];
159
+ };
160
+ };
161
+ /**
162
+ * Concat the given vectors of bits. The first vector is the least significant.
163
+ *
164
+ * @type {(a: Vec) => (b: Vec) => Vec}
165
+ *
166
+ * @example
167
+ *
168
+ * ```js
169
+ * const u8 = vec(8n)
170
+ * const a = u8(0x45n) // 0x145n
171
+ * const b = u8(0x89n) // 0x189n
172
+ * const ab = concatLsb(a)(b) // 0x18945n
173
+ * ```
174
+ */
175
+ export const concatLsb = a => {
176
+ const aLen = length(a);
177
+ const m = mask(aLen);
178
+ return b => (b << aLen) | (a & m);
179
+ };
180
+ /**
181
+ * Concat the given vectors of bits. The first vector is the most significant.
182
+ *
183
+ * @type {(b: Vec) => (a: Vec) => Vec}
184
+ *
185
+ * @example
186
+ *
187
+ * ```js
188
+ * const u8 = vec(8n)
189
+ * const a = u8(0x45n) // 0x145n
190
+ * const b = u8(0x89n) // 0x189n
191
+ * const ab = concatMsb(a)(b) // 0x14589n
192
+ * ```
193
+ */
194
+ export const concatMsb = flip(concatLsb);
@@ -1,9 +1,23 @@
1
1
  declare namespace _default {
2
- function uintExample(): void;
3
- function vecExample(): void;
4
- function length(): void;
5
- function bitset(): void;
6
- function appendBack(): void;
7
- function removeBack(): void;
2
+ export namespace examples {
3
+ function vec(): void;
4
+ function uint(): void;
5
+ function uintLsb(): void;
6
+ function uintMsb(): void;
7
+ function popUintLsb(): void;
8
+ function popUintMsb(): void;
9
+ function concatLsb(): void;
10
+ function concatMsb(): void;
11
+ function removeLsb(): void;
12
+ function removeMsb(): void;
13
+ }
14
+ export function uintLsb_1(): void;
15
+ export { uintLsb_1 as uintLsb };
16
+ export function uintSmall(): void;
17
+ export function vecExample(): void;
18
+ export function length(): void;
19
+ export function bitset(): void;
20
+ export function appendBack(): void;
21
+ export function removeBack(): void;
8
22
  }
9
23
  export default _default;
@@ -1,13 +1,163 @@
1
- import { empty, len, appendBack, vec, uint, removeFront } from './module.f.mjs';
1
+ import { empty, vec, length, concatLsb, uintLsb, uintMsb, removeLsb, concatMsb, removeMsb, uint, popUintLsb, popUintMsb } from './module.f.mjs';
2
2
  export default {
3
- uintExample: () => {
4
- const vector = 53n;
5
- const extract3Bits = uint(3n);
3
+ examples: {
4
+ vec: () => {
5
+ const vec4 = vec(4n);
6
+ const v0 = vec4(5n); // 0x15n
7
+ if (v0 !== 0x15n) {
8
+ throw v0;
9
+ }
10
+ const v1 = vec4(0x5fen); // 0x1En
11
+ if (v1 !== 0x1en) {
12
+ throw v1;
13
+ }
14
+ },
15
+ uint: () => {
16
+ const vector = vec(8n)(0x5n); // 0x105n
17
+ if (vector !== 0x105n) {
18
+ throw vector;
19
+ }
20
+ const result = uint(vector); // result is 0x5n
21
+ if (result !== 0x5n) {
22
+ throw result;
23
+ }
24
+ },
25
+ uintLsb: () => {
26
+ const vector = vec(8n)(0xf5n); // 0x1F5n
27
+ if (vector !== 0x1f5n) {
28
+ throw vector;
29
+ }
30
+ const result = uintLsb(4n)(vector); // result is 5n
31
+ if (result !== 5n) {
32
+ throw result;
33
+ }
34
+ const result2 = uintLsb(16n)(vector); // result2 is 0xF5n
35
+ if (result2 !== 0xf5n) {
36
+ throw result2;
37
+ }
38
+ },
39
+ uintMsb: () => {
40
+ const vector = vec(8n)(0xf5n); // 0x1F5n
41
+ if (vector !== 0x1f5n) {
42
+ throw vector;
43
+ }
44
+ const result = uintMsb(4n)(vector); // result is 0xFn
45
+ if (result !== 0xfn) {
46
+ throw result;
47
+ }
48
+ const result2 = uintMsb(16n)(vector); // result2 is 0xF500n
49
+ if (result2 !== 0xf500n) {
50
+ throw result2;
51
+ }
52
+ },
53
+ popUintLsb: () => {
54
+ const vector = vec(8n)(0xf5n); // 0x1F5n
55
+ const [result, rest] = popUintLsb(4n)(vector); // result is 5n, rest is 0x1Fn
56
+ if (result !== 5n) {
57
+ throw result;
58
+ }
59
+ if (rest !== 0x1fn) {
60
+ throw rest;
61
+ }
62
+ const [result2, rest2] = popUintLsb(16n)(vector); // result2 is 0xF5n, rest2 is 1n
63
+ if (result2 !== 0xf5n) {
64
+ throw result2;
65
+ }
66
+ if (rest2 !== 1n) {
67
+ throw rest2;
68
+ }
69
+ },
70
+ popUintMsb: () => {
71
+ const vector = vec(8n)(0xf5n); // 0x1F5n
72
+ const [result, rest] = popUintMsb(4n)(vector); // [0xFn, 0x15n]
73
+ if (result !== 0xfn) {
74
+ throw result;
75
+ }
76
+ if (rest !== 0x15n) {
77
+ throw rest;
78
+ }
79
+ const [result2, rest2] = popUintMsb(16n)(vector); // [0xF500n, 1n]
80
+ if (result2 !== 0xf500n) {
81
+ throw result2;
82
+ }
83
+ if (rest2 !== 1n) {
84
+ throw rest2;
85
+ }
86
+ },
87
+ concatLsb: () => {
88
+ const u8 = vec(8n);
89
+ const a = u8(0x45n); // 0x145n
90
+ if (a !== 0x145n) {
91
+ throw a;
92
+ }
93
+ const b = u8(0x89n); // 0x189n
94
+ if (b !== 0x189n) {
95
+ throw b;
96
+ }
97
+ const ab = concatLsb(a)(b); // 0x18945n
98
+ if (ab !== 0x18945n) {
99
+ throw ab;
100
+ }
101
+ },
102
+ concatMsb: () => {
103
+ const u8 = vec(8n);
104
+ const a = u8(0x45n); // 0x145n
105
+ if (a !== 0x145n) {
106
+ throw a;
107
+ }
108
+ const b = u8(0x89n); // 0x189n
109
+ if (b !== 0x189n) {
110
+ throw b;
111
+ }
112
+ const ab = concatMsb(a)(b); // 0x14589n
113
+ if (ab !== 0x14589n) {
114
+ throw ab;
115
+ }
116
+ },
117
+ removeLsb: () => {
118
+ const v = vec(16n)(0x3456n); // 0x13456n
119
+ if (v !== 0x13456n) {
120
+ throw v;
121
+ }
122
+ const r = removeLsb(4n)(v); // 0x1345n
123
+ if (r !== 0x1345n) {
124
+ throw r;
125
+ }
126
+ const r2 = removeLsb(24n)(v); // 0x1n
127
+ if (r2 !== 0x1n) {
128
+ throw r2;
129
+ }
130
+ },
131
+ removeMsb: () => {
132
+ const v = vec(16n)(0x3456n); // 0x13456n
133
+ if (v !== 0x13456n) {
134
+ throw v;
135
+ }
136
+ const r = removeMsb(4n)(v); // 0x1456n
137
+ if (r !== 0x1456n) {
138
+ throw r;
139
+ }
140
+ const r2 = removeMsb(24n)(v); // 0x1n
141
+ if (r2 !== 0x1n) {
142
+ throw r2;
143
+ }
144
+ }
145
+ },
146
+ uintLsb: () => {
147
+ const vector = 117n;
148
+ const extract3Bits = uintLsb(3n);
6
149
  const result = extract3Bits(vector); // result is 0b101n (5n)
7
150
  if (result !== 5n) {
8
151
  throw result;
9
152
  }
10
153
  },
154
+ uintSmall: () => {
155
+ const vector = 3n;
156
+ const extract3Bits = uintLsb(3n)(vector);
157
+ if (extract3Bits !== 1n) {
158
+ throw extract3Bits;
159
+ }
160
+ },
11
161
  vecExample: () => {
12
162
  const createVector = vec(4n);
13
163
  const vector = createVector(5n); // vector is 0b10101n
@@ -16,7 +166,7 @@ export default {
16
166
  }
17
167
  },
18
168
  length: () => {
19
- const i = len(empty);
169
+ const i = length(empty);
20
170
  if (i !== 0n) {
21
171
  throw i;
22
172
  }
@@ -26,10 +176,10 @@ export default {
26
176
  if (v !== 0x1fen) {
27
177
  throw v;
28
178
  }
29
- if (len(v) !== 8n) {
179
+ if (length(v) !== 8n) {
30
180
  throw 'len';
31
181
  }
32
- const u = uint(8n)(v);
182
+ const u = uintLsb(8n)(v);
33
183
  if (u !== 0xfen) {
34
184
  throw v;
35
185
  }
@@ -38,11 +188,11 @@ export default {
38
188
  const vec8 = vec(8n);
39
189
  const a = vec8(0x345n);
40
190
  const b = vec8(0x789n);
41
- const ab = appendBack(a)(b);
191
+ const ab = concatLsb(a)(b);
42
192
  if (ab !== 0x18945n) {
43
193
  throw ab;
44
194
  }
45
- const s = len(ab);
195
+ const s = length(ab);
46
196
  if (s !== 16n) {
47
197
  throw `appendBack: ${s}`;
48
198
  }
@@ -52,7 +202,7 @@ export default {
52
202
  if (v !== 0x32345n) {
53
203
  throw v.toString(16);
54
204
  }
55
- const r = removeFront(9n)(v);
205
+ const r = removeLsb(9n)(v);
56
206
  if (r !== 0x191n) {
57
207
  throw r.toString(16);
58
208
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "description": "FunctionalScript is a functional subset of JavaScript",
6
6
  "scripts": {
@@ -0,0 +1,18 @@
1
+ # Vector of Bits
2
+
3
+ A **vector of bits** is represented as a `bigint`. The value is always positive, and an **empty vector** is denoted by `1n`.
4
+
5
+ ## Key Characteristics
6
+
7
+ - The **most significant bit** with a value of `1` is called the **`stop bit`**.
8
+ - The **`stop bit`** marks the boundary of the vector.
9
+ - The **`length`** of a vector represents the number of meaningful bits (excluding the **`stop bit`**).
10
+ - An **empty vector** is represented by `1n`, with the implicit **`stop bit`** as the only bit.
11
+
12
+ ## Example
13
+
14
+ | Vector | Binary Representation | `length` | Vector Items |
15
+ |---------------|-----------------------|----------|--------------|
16
+ | `0b1001n` | `0b1001n` | 3 | `001` |
17
+ | `0x1FF` | `0b111111111` | 8 | `11111111` |
18
+ | `1n` (empty) | `0b1` | 0 | (none) |