forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.js
98 lines (90 loc) · 1.99 KB
/
tmp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//@ts-check
var fs = require("fs");
var path = require("path");
/**
*
* @param {number} n
*/
function* range(n) {
for (var i = 0; i < n; ++i) {
yield i;
}
}
/**
*
* @param {number} n
*/
var constructors = n => {
return [...range(n)].map(x => `| A${x} \n`).reduce((x, y) => x + y);
};
/**
*
* @param {number} n
*/
var polyConstructors = n => {
return [...range(n)].map(x => `| \`variant${x} \n`).reduce((x, y) => x + y);
};
/**
*
* @param {number} n
*/
var matches = n => {
return [...range(n)].map(x => `| A${x} -> ${x} \n `).reduce((x, y) => x + y);
};
/**
*
* @param {number} n
*/
var xs = n => {
return [...range(n)]
.map(x => `| A${x} -> "A${x}" \n `)
.reduce((x, y) => x + y);
};
/**
*
* @param {number} n
*/
var code = n => {
var content = `type t = \n ${constructors(n)}`;
var match = `let to_enum = function\n ${matches(n)}`;
var to_string = `let to_string = function\n ${xs(n)}`;
return content + match + to_string;
};
/**
*
* @param {number} n
*/
var polyCode = n => {
var content = `type t = [ \n ${polyConstructors(
n
)}\n ] [@@bs.deriving jsConverter] `;
var eq = `
let eq (x : t option) (y: t option) =
match x with
| Some x ->
(match y with None -> false | Some y -> x = y)
| None -> y = None
`;
var assertions = [...range(n)]
.map(x => `\n;;assert (tToJs \`variant${x} = "variant${x}")`)
.reduce((x, y) => x + y);
var assertions2 = [...range(n)]
.map(x => `\n;;assert (eq (tFromJs "variant${x}") (Some \`variant${x}))`)
.reduce((x, y) => x + y);
var assertions3 = `\n;;assert (eq (tFromJs "xx") None) \n`;
return content + eq + assertions + assertions2 + assertions3;
};
var run = () => {
fs.writeFileSync(
path.join(__dirname, "..", "jscomp", "test", "big_enum.ml"),
code(300),
"utf8"
);
};
var runPol = () => {
fs.writeFileSync(
path.join(__dirname, "..", "jscomp", "test", "big_polyvar_test.ml"),
polyCode(300)
);
};
runPol();