forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_fold.js
221 lines (212 loc) · 5.56 KB
/
record_fold.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//@ts-check
var assert = require("assert");
var node_types = require("./node_types");
var { init, setDiff } = node_types;
/**
*
* @typedef {import('./node_types').Node} Node
* @typedef {import("./node_types").Names} Names
* @typedef {import ("./node_types").Type} Type
* @typedef {import("./types").Obj} Obj
*/
/**
*
* @param {{name:string, def:Node}} typedef
* @param {Names} allNames
* @returns {string}
*/
function mkMethod({ name, def }, allNames) {
return `let ${name} : 'a . ('a,${name}) fn = ${mkBody(def, allNames)} `;
}
var skip = `unknown`;
/**
* @param {Node} def
* @param {Names} allNames
*/
function mkBody(def, allNames) {
// @ts-ignore
assert(def !== undefined);
switch (def.type) {
case "type_constructor_path":
case "constructed_type":
case "tuple_type":
return mkStructuralTy(def, allNames).eta;
case "record_declaration":
var len = def.children.length;
var args = init(len, (i) => `_x${i}`);
var pat_exp = init(len, (i) => {
return `${def.children[i].mainText} = ${args[i]}`;
});
/**
* @type {string[]}
*/
var body = args
.map((x, i) => {
var ty = def.children[i].children[1];
return mkBodyApply(ty, allNames, x);
})
.filter(Boolean);
return `fun _self st { ${pat_exp.join(";")}} -> ${body.join(" ")} st`;
case "variant_declaration":
var len = def.children.length;
var branches = def.children.map((branch) => mkBranch(branch, allNames));
return `fun _self st -> function \n| ${branches.join("\n|")}`;
default:
throw new Error(`unkonwn ${def.type}`);
}
}
/**
* @type {Obj}
*/
var skip_obj = {
eta: skip,
beta(x) {
return `${skip} ${x}`;
},
};
/**
*
*
* @param {Node} def
* @param {Names} allNames
* The code fragments should have two operations
* - eta-expanded
* needed due to `self` is missing
* @returns {Obj}
*/
function mkStructuralTy(def, allNames) {
switch (def.type) {
case "type_constructor_path":
var basic = node_types.isSupported(def, allNames);
switch (basic.kind) {
case "no":
return skip_obj;
case "exclude":
case "yes":
var code =
basic.kind === "yes" ? `_self.${basic.name}` : `${basic.name}`;
return {
eta: `(fun _self arg -> ${code} _self arg)`,
beta(x) {
return `let st = ${code} _self st ${x} in`;
},
method: code,
};
}
case "constructed_type":
// FIXME
var [list, base] = [...def.children].reverse();
switch (list.text) {
case "option":
case "list":
var inner = mkStructuralTy(base, allNames);
if (inner === skip_obj) {
return skip_obj;
}
// return `${list.text} (${inner})`;
var inner_code = inner.method;
if (inner_code === undefined) {
inner_code = `(${inner.eta})`;
}
return {
eta: `fun _self st arg -> ${list.text} ${inner_code} _self st arg`,
beta(x) {
return `let st = ${list.text} ${inner_code} _self st ${x} in`;
},
};
default:
throw new Error(`unsupported high order type ${list.text}`);
}
case "tuple_type":
var len = def.children.length;
var args = init(len, (i) => `_x${i}`);
var body = args
.map((x, i) => mkBodyApply(def.children[i], allNames, x))
.filter(Boolean);
var snippet = `(${args.join(",")}) -> ${body.join(" ")} st `;
return {
eta: `(fun _self st ${snippet})`,
beta(x) {
// This code path seems to be not hit
return `let st = (fun ${snippet}) ${x} in `;
},
};
default:
throw new Error(`unsupported structural type ${def.type}`);
}
}
/**
*
* @param {Node} ty
* @param {Names} allNames
* @param {string} arg
*/
function mkBodyApply(ty, allNames, arg) {
var fn = mkStructuralTy(ty, allNames);
if (fn === skip_obj) {
return ``;
}
return fn.beta(arg);
}
/**
*
* @param {Node} branch
* branch is constructor_declaration
* @param {Names} allNames
* @returns {string}
*/
function mkBranch(branch, allNames) {
// @ts-ignore
assert(branch?.type === "constructor_declaration");
var [{ text }, ...rest] = branch.children;
// TODO: add inline record support
var len = rest.length;
if (len === 0) {
return `${text} -> st`;
}
var args = init(len, (i) => `_x${i}`);
var pat_exp = `${text} ( ${args.join(",")}) `;
var body = args
.map((x, i) => {
var ty = rest[i];
return mkBodyApply(ty, allNames, x);
})
.filter(Boolean);
if (body.length === 0) {
return `${text} _ -> st`;
}
return `${pat_exp} -> \n ${body.join(" ")} st`;
}
/**
* @param {Type} type
* @returns {string}
*/
function make(type) {
var { types: typedefs, names } = type;
var customNames = setDiff(names.all, names.excludes);
var output = typedefs.map((x) => mkMethod(x, names));
var o = `
open J
let [@inline] unknown _ st _ = st
let [@inline] option sub self st = fun v ->
match v with
| None -> st
| Some v -> sub self st v
let rec list sub self st = fun x ->
match x with
| [] -> st
| x::xs ->
let st = sub self st x in
list sub self st xs
type 'state iter = {
${customNames.map((x) => ` ${x} : ('state,${x}) fn`).join(";\n")}
}
and ('state,'a) fn = 'state iter -> 'state -> 'a -> 'state
${output.join("\n")}
let super : 'state iter = {
${customNames.map((x) => ` ${x}`).join(";\n")}
}
`;
return o;
}
exports.make = make;