functionalscript 0.4.2 → 0.4.4
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 +1 -1
- package/bnf/data/module.f.d.ts +12 -0
- package/bnf/data/module.f.js +85 -0
- package/bnf/data/test.f.d.ts +4 -0
- package/bnf/data/test.f.js +8 -0
- package/bnf/module.f.d.ts +55 -0
- package/bnf/module.f.js +98 -0
- package/bnf/test.f.d.ts +4 -0
- package/bnf/test.f.js +7 -0
- package/bnf/testlib.f.d.ts +3 -0
- package/bnf/{tag/test.f.js → testlib.f.js} +48 -44
- package/crypto/hmac/module.f.d.ts +28 -0
- package/crypto/hmac/module.f.js +60 -0
- package/crypto/hmac/test.f.d.ts +6 -0
- package/crypto/hmac/test.f.js +24 -0
- package/crypto/secp/module.f.d.ts +8 -0
- package/crypto/secp/module.f.js +30 -0
- package/crypto/secp/test.f.d.ts +3 -0
- package/crypto/secp/test.f.js +67 -3
- package/crypto/sha2/module.f.d.ts +10 -0
- package/crypto/sha2/module.f.js +11 -2
- package/crypto/sha2/test.f.js +28 -21
- package/djs/ast/module.f.d.ts +10 -0
- package/djs/ast/module.f.js +52 -0
- package/djs/ast/test.f.d.ts +8 -0
- package/djs/ast/test.f.js +41 -0
- package/djs/module.f.d.ts +5 -15
- package/djs/module.f.js +1 -62
- package/djs/parser/module.f.d.ts +9 -9
- package/djs/parser/module.f.js +7 -8
- package/djs/parser/test.f.js +97 -97
- package/djs/serializer/module.f.d.ts +8 -4
- package/djs/serializer/module.f.js +9 -27
- package/djs/{test.f.d.ts → serializer/test.f.d.ts} +3 -3
- package/djs/{test.f.js → serializer/test.f.js} +13 -13
- package/djs/tokenizer/test.f.js +2 -2
- package/djs/transpiler/module.f.d.ts +15 -0
- package/djs/transpiler/module.f.js +57 -0
- package/djs/transpiler/test.f.d.ts +8 -0
- package/djs/transpiler/test.f.js +74 -0
- package/io/module.f.d.ts +12 -0
- package/io/virtual-io.f.d.ts +3 -0
- package/io/virtual-io.f.js +10 -0
- package/js/tokenizer/test.f.js +2 -2
- package/json/module.f.d.ts +2 -1
- package/json/tokenizer/test.f.js +2 -2
- package/nanvm-lib/tests/test.f.js +4 -4
- package/package.json +2 -2
- package/path/module.f.d.ts +2 -0
- package/path/module.f.js +34 -0
- package/path/test.f.d.ts +5 -0
- package/path/test.f.js +49 -0
- package/text/sgr/module.f.d.ts +19 -1
- package/text/sgr/module.f.js +26 -1
- package/text/sgr/test.f.d.ts +2 -0
- package/text/sgr/test.f.js +8 -0
- package/text/utf16/module.f.d.ts +116 -0
- package/text/utf16/module.f.js +285 -0
- package/text/utf16/test.f.d.ts +4 -0
- package/text/utf16/test.f.js +28 -0
- package/types/bigint/module.f.d.ts +83 -2
- package/types/bigint/module.f.js +98 -2
- package/types/bigint/test.f.d.ts +4 -0
- package/types/bigint/test.f.js +66 -6
- package/types/bit_vec/module.f.d.ts +1 -1
- package/types/monoid/module.f.d.ts +3 -1
- package/types/monoid/module.f.js +2 -0
- package/types/number/module.f.d.ts +4 -0
- package/types/number/module.f.js +16 -0
- package/types/number/test.f.d.ts +1 -0
- package/types/number/test.f.js +19 -3
- package/types/object/module.f.d.ts +18 -0
- package/types/object/module.f.js +1 -1
- package/bnf/tag/module.f.d.ts +0 -30
- package/bnf/tag/module.f.js +0 -37
- /package/{bnf/tag/test.f.d.ts → io/module.f.js} +0 -0
package/crypto/secp/test.f.js
CHANGED
|
@@ -1,4 +1,58 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { prime_field } from "../prime_field/module.f.js";
|
|
2
|
+
import { curve, secp256k1, secp192r1, secp256r1, eq, secp384r1, secp521r1 } from "./module.f.js";
|
|
3
|
+
const poker = (param) => () => {
|
|
4
|
+
// (c ^ x) ^ y = c ^ (x * y)
|
|
5
|
+
// c ^ ((x * y) * (1/x * 1/y)) = c
|
|
6
|
+
const { g, n } = param;
|
|
7
|
+
const { mul, y } = curve(param);
|
|
8
|
+
const f = (m) => (pList) => pList.map(i => mul(i)(m));
|
|
9
|
+
//
|
|
10
|
+
const pf = prime_field(n);
|
|
11
|
+
// 0 1 2 3 4 5 6 7
|
|
12
|
+
const sA = 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefn % n;
|
|
13
|
+
const sB = 0xfedcba98fedcba98fedcba98fedcba98fedcba98fedcba98fedcba98fedcba98n % n;
|
|
14
|
+
// "22d3ad011aec6aabdb3d3d47636f3e2859de02298c87a496"
|
|
15
|
+
// "2b359de5cfb5937a5610d565dceaef2a760ceeaec96e68140757f0c8371534e0"
|
|
16
|
+
// "1359162ede91207ccaea1de94afc63c1db5a967c1e6e21f91ef9f077f20a46b6"
|
|
17
|
+
const rA = pf.reciprocal(sA);
|
|
18
|
+
// "e1e768c7427cf5bafd58756df9b54b9ec2558201f129f4ab"
|
|
19
|
+
// "edaf7ede285c3da723c54fcdaa3b631f626681f884d8f41fae55c4f552bb551e"
|
|
20
|
+
// "6ca248e88c124478975b57c4c3ca682bd8be0f0d9f11593d01273d9ceebdb735"
|
|
21
|
+
const rB = pf.reciprocal(sB);
|
|
22
|
+
//
|
|
23
|
+
let d = [];
|
|
24
|
+
for (let i = 0n; i < 52n; ++i) {
|
|
25
|
+
let nonce = 0n; // can be a random number in a range [`0`, `p >> 6n`).
|
|
26
|
+
let x = 0n;
|
|
27
|
+
let yi;
|
|
28
|
+
while (true) {
|
|
29
|
+
x = i | (nonce << 6n);
|
|
30
|
+
yi = y(x);
|
|
31
|
+
if (yi !== null) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
++nonce;
|
|
35
|
+
}
|
|
36
|
+
d = [...d, [x, yi]];
|
|
37
|
+
}
|
|
38
|
+
//
|
|
39
|
+
const dA = f(sA)(d);
|
|
40
|
+
const dAB = f(sB)(dA);
|
|
41
|
+
const dB = f(rA)(dAB);
|
|
42
|
+
const dN = f(rB)(dB);
|
|
43
|
+
//
|
|
44
|
+
let m = 0n;
|
|
45
|
+
for (const p of dN) {
|
|
46
|
+
if (p === null) {
|
|
47
|
+
throw 'null';
|
|
48
|
+
}
|
|
49
|
+
const x = p[0] & 0x3fn;
|
|
50
|
+
if (x !== m) {
|
|
51
|
+
throw [p[0], x, m];
|
|
52
|
+
}
|
|
53
|
+
++m;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
2
56
|
export default {
|
|
3
57
|
example: () => {
|
|
4
58
|
const curveParams = {
|
|
@@ -17,7 +71,7 @@ export default {
|
|
|
17
71
|
const test_curve = c => {
|
|
18
72
|
const { g } = c;
|
|
19
73
|
const { mul, neg, pf: { abs }, y: yf, nf: { p: n } } = curve(c);
|
|
20
|
-
const point_check = p => {
|
|
74
|
+
const point_check = (p) => {
|
|
21
75
|
if (p === null) {
|
|
22
76
|
throw 'p === null';
|
|
23
77
|
}
|
|
@@ -32,7 +86,7 @@ export default {
|
|
|
32
86
|
};
|
|
33
87
|
point_check(g);
|
|
34
88
|
point_check(neg(g));
|
|
35
|
-
const test_mul = p => {
|
|
89
|
+
const test_mul = (p) => {
|
|
36
90
|
if (mul(p)(0n) !== null) {
|
|
37
91
|
throw 'O';
|
|
38
92
|
}
|
|
@@ -68,5 +122,15 @@ export default {
|
|
|
68
122
|
test_curve(secp256k1);
|
|
69
123
|
test_curve(secp192r1);
|
|
70
124
|
test_curve(secp256r1);
|
|
125
|
+
test_curve(secp384r1);
|
|
126
|
+
test_curve(secp521r1);
|
|
127
|
+
},
|
|
128
|
+
poker: () => {
|
|
129
|
+
const c = {
|
|
130
|
+
secp192r1,
|
|
131
|
+
//secp256k1,
|
|
132
|
+
//secp256r1,
|
|
133
|
+
};
|
|
134
|
+
return Object.fromEntries(Object.entries(c).map(([k, v]) => [k, poker(v)]));
|
|
71
135
|
}
|
|
72
136
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Array16, Array8 } from '../../types/array/module.f.ts';
|
|
2
2
|
import { type Vec } from '../../types/bit_vec/module.f.ts';
|
|
3
|
+
import { type List } from '../../types/list/module.f.ts';
|
|
3
4
|
export type V8 = Array8<bigint>;
|
|
4
5
|
export type V16 = Array16<bigint>;
|
|
5
6
|
/**
|
|
@@ -40,6 +41,14 @@ export type Base = {
|
|
|
40
41
|
* ```
|
|
41
42
|
*/
|
|
42
43
|
export type Sha2 = {
|
|
44
|
+
/**
|
|
45
|
+
* A hash length.
|
|
46
|
+
*/
|
|
47
|
+
readonly hashLength: bigint;
|
|
48
|
+
/**
|
|
49
|
+
* An internal block length.
|
|
50
|
+
*/
|
|
51
|
+
readonly blockLength: bigint;
|
|
43
52
|
/**
|
|
44
53
|
* Initial state of the SHA-2 algorithm.
|
|
45
54
|
*/
|
|
@@ -60,6 +69,7 @@ export type Sha2 = {
|
|
|
60
69
|
*/
|
|
61
70
|
readonly end: (state: State) => bigint;
|
|
62
71
|
};
|
|
72
|
+
export declare const computeSync: ({ append, init, end }: Sha2) => (list: List<Vec>) => Vec;
|
|
63
73
|
export declare const base32: Base;
|
|
64
74
|
export declare const base64: Base;
|
|
65
75
|
/** SHA-256 */
|
package/crypto/sha2/module.f.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { mask } from "../../types/bigint/module.f.js";
|
|
2
2
|
import { vec, length, empty, msb } from "../../types/bit_vec/module.f.js";
|
|
3
|
+
import { flip } from "../../types/function/module.f.js";
|
|
4
|
+
import { fold } from "../../types/list/module.f.js";
|
|
3
5
|
const { concat, popFront, front } = msb;
|
|
4
6
|
const lastOne = vec(1n)(1n);
|
|
5
7
|
const base = ({ logBitLen, k, bs0, bs1, ss0, ss1 }) => {
|
|
@@ -138,6 +140,7 @@ const base = ({ logBitLen, k, bs0, bs1, ss0, ss1 }) => {
|
|
|
138
140
|
},
|
|
139
141
|
end: (hashLength) => {
|
|
140
142
|
const offset = (bitLength << 3n) - hashLength;
|
|
143
|
+
const result = vec(hashLength);
|
|
141
144
|
return (state) => {
|
|
142
145
|
const { len, remainder } = state;
|
|
143
146
|
let { hash } = state;
|
|
@@ -148,12 +151,14 @@ const base = ({ logBitLen, k, bs0, bs1, ss0, ss1 }) => {
|
|
|
148
151
|
hash = compress(hash)(u);
|
|
149
152
|
u = 0n;
|
|
150
153
|
}
|
|
151
|
-
return fromV8(compress(hash)(u | (len + rLen))) >> offset;
|
|
154
|
+
return result(fromV8(compress(hash)(u | (len + rLen))) >> offset);
|
|
152
155
|
};
|
|
153
156
|
}
|
|
154
157
|
};
|
|
155
158
|
};
|
|
156
|
-
const sha2 = ({ append, end }, hash, hashLength) => ({
|
|
159
|
+
const sha2 = ({ append, end, chunkLength }, hash, hashLength) => ({
|
|
160
|
+
hashLength,
|
|
161
|
+
blockLength: chunkLength,
|
|
157
162
|
init: {
|
|
158
163
|
hash,
|
|
159
164
|
len: 0n,
|
|
@@ -162,6 +167,10 @@ const sha2 = ({ append, end }, hash, hashLength) => ({
|
|
|
162
167
|
append,
|
|
163
168
|
end: end(hashLength),
|
|
164
169
|
});
|
|
170
|
+
export const computeSync = ({ append, init, end }) => {
|
|
171
|
+
const f = fold(flip(append))(init);
|
|
172
|
+
return (list) => end(f(list));
|
|
173
|
+
};
|
|
165
174
|
export const base32 = base({
|
|
166
175
|
logBitLen: 5n,
|
|
167
176
|
k: [
|
package/crypto/sha2/test.f.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { msbUtf8 } from "../../text/module.f.js";
|
|
2
2
|
import { empty, msb, vec } from "../../types/bit_vec/module.f.js";
|
|
3
|
+
import { map } from "../../types/list/module.f.js";
|
|
3
4
|
import { repeat } from "../../types/monoid/module.f.js";
|
|
4
|
-
import { base32, base64, sha224, sha256, sha384, sha512, sha512x224, sha512x256, } from "./module.f.js";
|
|
5
|
+
import { base32, base64, computeSync, sha224, sha256, sha384, sha512, sha512x224, sha512x256, } from "./module.f.js";
|
|
5
6
|
const { concat: beConcat } = msb;
|
|
6
7
|
const checkEmpty = ({ init, end }) => (x) => {
|
|
7
8
|
const result = end(init);
|
|
@@ -22,7 +23,7 @@ export default {
|
|
|
22
23
|
const result = fromV8(compress(sha256.init.hash)(e));
|
|
23
24
|
const x = 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855n;
|
|
24
25
|
if (result !== x) {
|
|
25
|
-
throw [result, x];
|
|
26
|
+
throw [result.toString(16), x.toString(16)];
|
|
26
27
|
}
|
|
27
28
|
},
|
|
28
29
|
s224: () => {
|
|
@@ -70,29 +71,35 @@ export default {
|
|
|
70
71
|
}
|
|
71
72
|
},
|
|
72
73
|
sha2: {
|
|
73
|
-
sha256: () => checkEmpty(sha256)(
|
|
74
|
-
sha224: () => checkEmpty(sha224)(
|
|
75
|
-
sha512: () => checkEmpty(sha512)(
|
|
76
|
-
sha384: () => checkEmpty(sha384)(
|
|
77
|
-
sha512x256: () => checkEmpty(sha512x256)(
|
|
78
|
-
sha512x224: () => checkEmpty(sha512x224)(
|
|
74
|
+
sha256: () => checkEmpty(sha256)(0x1e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855n),
|
|
75
|
+
sha224: () => checkEmpty(sha224)(0x1d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42fn),
|
|
76
|
+
sha512: () => checkEmpty(sha512)(0x1cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3en),
|
|
77
|
+
sha384: () => checkEmpty(sha384)(0x138b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95bn),
|
|
78
|
+
sha512x256: () => checkEmpty(sha512x256)(0x1c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967an),
|
|
79
|
+
sha512x224: () => checkEmpty(sha512x224)(0x16ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4n),
|
|
79
80
|
},
|
|
80
81
|
utf8: [
|
|
81
82
|
() => {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
const e = 0x1730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525n;
|
|
84
|
+
{
|
|
85
|
+
const s = msbUtf8("The quick brown fox jumps over the lazy dog");
|
|
86
|
+
const h = computeSync(sha224)([s]);
|
|
87
|
+
if (h !== e) {
|
|
88
|
+
throw h;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
{
|
|
92
|
+
const s = ['The', ' quick', ' brown', ' fox', ' jumps', ' over', ' the', ' lazy', ' dog'];
|
|
93
|
+
const h = computeSync(sha224)(map(msbUtf8)(s));
|
|
94
|
+
if (h !== e) {
|
|
95
|
+
throw h;
|
|
96
|
+
}
|
|
88
97
|
}
|
|
89
98
|
},
|
|
90
99
|
() => {
|
|
91
100
|
const s = msbUtf8("The quick brown fox jumps over the lazy dog.");
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const h = sha224.end(state);
|
|
95
|
-
if (h !== 0x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4cn) {
|
|
101
|
+
const h = computeSync(sha224)([s]);
|
|
102
|
+
if (h !== 0x1619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4cn) {
|
|
96
103
|
throw h;
|
|
97
104
|
}
|
|
98
105
|
},
|
|
@@ -104,7 +111,7 @@ export default {
|
|
|
104
111
|
let state = sha256.init;
|
|
105
112
|
state = sha256.append(state)(s);
|
|
106
113
|
const h = sha256.end(state);
|
|
107
|
-
if (h !==
|
|
114
|
+
if (h !== 0x1b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9n) {
|
|
108
115
|
throw h;
|
|
109
116
|
}
|
|
110
117
|
}
|
|
@@ -117,7 +124,7 @@ export default {
|
|
|
117
124
|
let state = sha256.init;
|
|
118
125
|
state = sha256.append(state)(r);
|
|
119
126
|
const h = sha256.end(state);
|
|
120
|
-
if (h >> 224n !==
|
|
127
|
+
if (h >> 224n !== 0x18a83665fn) {
|
|
121
128
|
throw h;
|
|
122
129
|
}
|
|
123
130
|
},
|
|
@@ -126,7 +133,7 @@ export default {
|
|
|
126
133
|
let state = sha256.init;
|
|
127
134
|
state = sha256.append(state)(r);
|
|
128
135
|
const h = sha256.end(state);
|
|
129
|
-
if (h !==
|
|
136
|
+
if (h !== 0x13138bb9bc78df27c473ecfd1410f7bd45ebac1f59cf3ff9cfe4db77aab7aedd3n) {
|
|
130
137
|
throw h;
|
|
131
138
|
}
|
|
132
139
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type * as djs from '../module.f.ts';
|
|
2
|
+
export type AstModule = [readonly string[], AstBody];
|
|
3
|
+
export type AstConst = djs.Primitive | AstModuleRef | AstArray | AstObject;
|
|
4
|
+
export type AstModuleRef = ['aref' | 'cref', number];
|
|
5
|
+
export type AstArray = ['array', readonly AstConst[]];
|
|
6
|
+
export type AstObject = {
|
|
7
|
+
readonly [k in string]: AstConst;
|
|
8
|
+
};
|
|
9
|
+
export type AstBody = readonly AstConst[];
|
|
10
|
+
export declare const run: (body: AstBody) => (args: djs.Array) => djs.Unknown;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { concat, fold, last, map, take, toArray } from "../../types/list/module.f.js";
|
|
2
|
+
import { fromEntries } from "../../types/object/module.f.js";
|
|
3
|
+
const { entries } = Object;
|
|
4
|
+
const foldOp = ast => state => {
|
|
5
|
+
const djs = toDjs(state)(ast);
|
|
6
|
+
return { ...state, consts: concat(state.consts)([djs]) };
|
|
7
|
+
};
|
|
8
|
+
const foldAstObjectOp = entry => state => {
|
|
9
|
+
const e = concat(state.entries)([[entry[0], (toDjs(state.runState)(entry[1]))]]);
|
|
10
|
+
return { ...state, entries: e };
|
|
11
|
+
};
|
|
12
|
+
const toDjs = state => ast => {
|
|
13
|
+
switch (typeof ast) {
|
|
14
|
+
case 'boolean':
|
|
15
|
+
case 'number':
|
|
16
|
+
case 'string':
|
|
17
|
+
case 'bigint': {
|
|
18
|
+
return ast;
|
|
19
|
+
}
|
|
20
|
+
default: {
|
|
21
|
+
if (ast === null) {
|
|
22
|
+
return ast;
|
|
23
|
+
}
|
|
24
|
+
if (ast === undefined) {
|
|
25
|
+
return ast;
|
|
26
|
+
}
|
|
27
|
+
if (ast instanceof Array) {
|
|
28
|
+
switch (ast[0]) {
|
|
29
|
+
case 'aref': {
|
|
30
|
+
return state.args[ast[1]];
|
|
31
|
+
}
|
|
32
|
+
case 'cref': {
|
|
33
|
+
return last(null)(take(ast[1] + 1)(state.consts));
|
|
34
|
+
}
|
|
35
|
+
case 'array': {
|
|
36
|
+
return toArray(map(toDjs(state))(ast[1]));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const e = fold(foldAstObjectOp)({ runState: state, entries: null })(entries(ast)).entries;
|
|
41
|
+
return fromEntries(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export const run = body => args => {
|
|
46
|
+
const state = fold(foldOp)({ body, args, consts: null })(body);
|
|
47
|
+
return last(null)(state.consts);
|
|
48
|
+
};
|
|
49
|
+
// for functions
|
|
50
|
+
// export const astBodyToAstConst
|
|
51
|
+
// :(body: AstBody) => (args: AstArray) => AstConst
|
|
52
|
+
// = body => args => todo()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as list from "../../types/object/module.f.js";
|
|
2
|
+
const { sort } = list;
|
|
3
|
+
import * as shared from "./module.f.js";
|
|
4
|
+
import { stringify } from "../serializer/module.f.js";
|
|
5
|
+
export default {
|
|
6
|
+
test: () => {
|
|
7
|
+
const djs = shared.run([1])([]);
|
|
8
|
+
const result = stringify(sort)(djs);
|
|
9
|
+
if (result !== '1') {
|
|
10
|
+
throw result;
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
testCref: () => {
|
|
14
|
+
const djs = shared.run([1, 2, 3, 4, 5, ['cref', 3]])([11, 12, 13, 14, 15]);
|
|
15
|
+
const result = stringify(sort)(djs);
|
|
16
|
+
if (result !== '4') {
|
|
17
|
+
throw result;
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
testAref: () => {
|
|
21
|
+
const djs = shared.run([1, 2, 3, 4, 5, ['aref', 3]])([11, 12, 13, 14, 15]);
|
|
22
|
+
const result = stringify(sort)(djs);
|
|
23
|
+
if (result !== '14') {
|
|
24
|
+
throw result;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
testArray: () => {
|
|
28
|
+
const djs = shared.run([1, 2, 3, 4, 5, ['array', [['aref', 3], ['cref', 3]]]])([11, 12, 13, 14, 15]);
|
|
29
|
+
const result = stringify(sort)(djs);
|
|
30
|
+
if (result !== '[14,4]') {
|
|
31
|
+
throw result;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
testObj: () => {
|
|
35
|
+
const djs = shared.run([1, 2, 3, 4, 5, { "key": { "key2": ['array', [['aref', 3], ['cref', 3]]] } }])([11, 12, 13, 14, 15]);
|
|
36
|
+
const result = stringify(sort)(djs);
|
|
37
|
+
if (result !== '{"key":{"key2":[14,4]}}') {
|
|
38
|
+
throw result;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
package/djs/module.f.d.ts
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
type Object = {
|
|
1
|
+
import type { Primitive as JsonPrimitive } from '../json/module.f.ts';
|
|
2
|
+
export type Object = {
|
|
4
3
|
readonly [k in string]: Unknown;
|
|
5
4
|
};
|
|
6
|
-
type Array = readonly Unknown[];
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
type Entries = list.List<Entry>;
|
|
10
|
-
type MapEntries = (entries: Entries) => Entries;
|
|
11
|
-
export declare const serialize: (mapEntries: MapEntries) => (value: Unknown) => list.List<string>;
|
|
12
|
-
/**
|
|
13
|
-
* The standard `JSON.stringify` rules determined by
|
|
14
|
-
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
15
|
-
*/
|
|
16
|
-
export declare const stringify: (mapEntries: MapEntries) => (value: Unknown) => string;
|
|
17
|
-
export {};
|
|
5
|
+
export type Array = readonly Unknown[];
|
|
6
|
+
export type Primitive = JsonPrimitive | bigint | undefined;
|
|
7
|
+
export type Unknown = Primitive | Object | Array;
|
package/djs/module.f.js
CHANGED
|
@@ -1,62 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const { flat, map } = list;
|
|
3
|
-
import * as string from "../types/string/module.f.js";
|
|
4
|
-
const { concat } = string;
|
|
5
|
-
import * as f from "../types/function/module.f.js";
|
|
6
|
-
const { compose, fn } = f;
|
|
7
|
-
const { entries } = Object;
|
|
8
|
-
import * as bi from "../types/bigint/module.f.js";
|
|
9
|
-
const { serialize: bigintSerialize } = bi;
|
|
10
|
-
import * as j from "../json/serializer/module.f.js";
|
|
11
|
-
const { objectWrap, arrayWrap, stringSerialize, numberSerialize, nullSerialize, boolSerialize } = j;
|
|
12
|
-
import * as djs from "./serializer/module.f.js";
|
|
13
|
-
const { undefinedSerialize } = djs;
|
|
14
|
-
const colon = [':'];
|
|
15
|
-
export const serialize = sort => {
|
|
16
|
-
const propertySerialize = ([k, v]) => flat([
|
|
17
|
-
stringSerialize(k),
|
|
18
|
-
colon,
|
|
19
|
-
f(v)
|
|
20
|
-
]);
|
|
21
|
-
const mapPropertySerialize = map(propertySerialize);
|
|
22
|
-
const objectSerialize = fn(entries)
|
|
23
|
-
.then(sort)
|
|
24
|
-
.then(mapPropertySerialize)
|
|
25
|
-
.then(objectWrap)
|
|
26
|
-
.result;
|
|
27
|
-
const f = value => {
|
|
28
|
-
switch (typeof value) {
|
|
29
|
-
case 'boolean': {
|
|
30
|
-
return boolSerialize(value);
|
|
31
|
-
}
|
|
32
|
-
case 'number': {
|
|
33
|
-
return numberSerialize(value);
|
|
34
|
-
}
|
|
35
|
-
case 'string': {
|
|
36
|
-
return stringSerialize(value);
|
|
37
|
-
}
|
|
38
|
-
case 'bigint': {
|
|
39
|
-
return [bigintSerialize(value)];
|
|
40
|
-
}
|
|
41
|
-
default: {
|
|
42
|
-
if (value === null) {
|
|
43
|
-
return nullSerialize;
|
|
44
|
-
}
|
|
45
|
-
if (value === undefined) {
|
|
46
|
-
return undefinedSerialize;
|
|
47
|
-
}
|
|
48
|
-
if (value instanceof Array) {
|
|
49
|
-
return arraySerialize(value);
|
|
50
|
-
}
|
|
51
|
-
return objectSerialize(value);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
const arraySerialize = compose(map(f))(arrayWrap);
|
|
56
|
-
return f;
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* The standard `JSON.stringify` rules determined by
|
|
60
|
-
* https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
|
|
61
|
-
*/
|
|
62
|
-
export const stringify = sort => compose(serialize(sort))(concat);
|
|
1
|
+
export {};
|
package/djs/parser/module.f.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as result from '../../types/result/module.f.ts';
|
|
2
2
|
import { type List } from '../../types/list/module.f.ts';
|
|
3
|
-
import type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
readonly
|
|
3
|
+
import type { DjsToken } from '../tokenizer/module.f.ts';
|
|
4
|
+
import { type Map } from '../../types/map/module.f.ts';
|
|
5
|
+
import type { Fs } from '../../io/module.f.ts';
|
|
6
|
+
import type { AstModule } from '../ast/module.f.ts';
|
|
7
|
+
export type ParseContext = {
|
|
8
|
+
readonly fs: Fs;
|
|
9
|
+
readonly complete: Map<result.Result<AstModule, string>>;
|
|
10
|
+
readonly stack: List<string>;
|
|
10
11
|
};
|
|
11
|
-
export declare const
|
|
12
|
-
export {};
|
|
12
|
+
export declare const parseFromTokens: (tokenList: List<DjsToken>) => result.Result<AstModule, string>;
|
package/djs/parser/module.f.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as result from "../../types/result/module.f.js";
|
|
2
2
|
import { fold, first, drop, toArray, length, concat } from "../../types/list/module.f.js";
|
|
3
3
|
import { setReplace, at } from "../../types/map/module.f.js";
|
|
4
|
-
import
|
|
5
|
-
const { fromMap } = o;
|
|
4
|
+
import { fromMap } from "../../types/object/module.f.js";
|
|
6
5
|
const parseInitialOp = token => state => {
|
|
7
6
|
switch (token.kind) {
|
|
8
7
|
case 'ws':
|
|
@@ -59,8 +58,8 @@ const parseConstOp = token => state => {
|
|
|
59
58
|
case 'id': {
|
|
60
59
|
if (at(token.value)(state.module.refs) !== null)
|
|
61
60
|
return { state: 'error', message: 'duplicate id' };
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
const cref = ['cref', length(state.module.consts)];
|
|
62
|
+
const refs = setReplace(token.value)(cref)(state.module.refs);
|
|
64
63
|
return { ...state, state: 'const+name', module: { ...state.module, refs: refs } };
|
|
65
64
|
}
|
|
66
65
|
default: return { state: 'error', message: 'unexpected token' };
|
|
@@ -86,8 +85,8 @@ const parseImportOp = token => state => {
|
|
|
86
85
|
if (at(token.value)(state.module.refs) !== null) {
|
|
87
86
|
return { state: 'error', message: 'duplicate id' };
|
|
88
87
|
}
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
const aref = ['aref', length(state.module.modules)];
|
|
89
|
+
const refs = setReplace(token.value)(aref)(state.module.refs);
|
|
91
90
|
return { ...state, state: 'import+name', module: { ...state.module, refs: refs } };
|
|
92
91
|
}
|
|
93
92
|
default: return { state: 'error', message: 'unexpected token' };
|
|
@@ -130,7 +129,7 @@ const pushKey = state => key => {
|
|
|
130
129
|
};
|
|
131
130
|
const pushValue = state => value => {
|
|
132
131
|
if (state.top === null) {
|
|
133
|
-
|
|
132
|
+
const consts = concat(state.module.consts)([value]);
|
|
134
133
|
switch (state.state) {
|
|
135
134
|
case 'exportValue': return { ...state, state: 'result', module: { ...state.module, consts: consts } };
|
|
136
135
|
case 'constValue': return { ...state, state: 'nl', module: { ...state.module, consts: consts } };
|
|
@@ -321,7 +320,7 @@ const foldOp = token => state => {
|
|
|
321
320
|
}
|
|
322
321
|
}
|
|
323
322
|
};
|
|
324
|
-
export const
|
|
323
|
+
export const parseFromTokens = (tokenList) => {
|
|
325
324
|
const state = fold(foldOp)({ state: '', module: { refs: null, modules: null, consts: null } })(tokenList);
|
|
326
325
|
switch (state.state) {
|
|
327
326
|
case 'result': return result.ok([toArray(state.module.modules), toArray(state.module.consts)]);
|