forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.js
61 lines (54 loc) · 1.79 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
//@ts-check
var fs = require('fs')
var path = require('path')
function* range(n) {
for (var i = 0; i < n; ++i) {
yield i
}
}
var constructors = (n) => {
return [...range(n)].map((x => `| A${x} \n`)).reduce((x, y) => x + y)
}
var polyConstructors = (n) => {
return [...range(n)].map((x => `| \`variant${x} \n`)).reduce((x, y) => x + y)
}
var matches = (n) => {
return [...range(n)].map((x => `| A${x} -> ${x} \n `)).reduce((x, y) => x + y)
}
var xs = (n) => {
return [...range(n)].map((x => `| A${x} -> "A${x}" \n `)).reduce((x, y) => x + y)
}
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
}
var polyCode = (n) =>{
var content = `type t = [ \n ${polyConstructors(n)}\n ] [@@bs.deriving jsMapper] `
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()