-
Notifications
You must be signed in to change notification settings - Fork 1
/
translator.js
366 lines (320 loc) · 12.9 KB
/
translator.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
let safe_symbol = symbol => {
let replacements = [
[/-/, "__MINUS__"],
[/\+/, "__PLUS__"],
[/!/, "__BANG__"],
[/@/, "__AT__"],
[/#/, "__POUND__"],
[/\$/, "__DOLLAR__"],
[/%/, "__PERCENT__"],
[/\^/, "__CARET__"],
[/&/, "__AMPERSAND__"],
[/\*/, "__ASTERISK__"],
[/=/, "__EQUALS__"],
[/'/, "__QUOTE__"],
[/\?/, "__QUESTION_MARK__"],
[/\//, "__FORWARD_SLASH__"],
[/\\/, "__BACKSLASH__"],
[/</, "__LESS_THAN__"],
[/>/, "__GREATER_THAN__"]
];
return replacements.reduce((acc, [pattern, replacement]) => {
return acc.replace(new RegExp(pattern, "g"), replacement);
}, symbol);
};
let conjoin_children = (depth, symbolWhitelist = new Set()) => {
return (acc, child, j, children) => {
let tree = [child];
let translation = translate(tree, depth + 1, null, symbolWhitelist);
let delimiter = "";
if (children[j + 1]) {
delimiter = ",";
}
return acc + translation + delimiter;
};
};
// Turns [1,2,3,4,5,6] into [[1,2],[3,4],[5,6]].
let pairwise = array => {
if (array.length === 0) {
return [];
}
let rest = array.slice(2);
let result = [array[0], array[1]];
return [result, ...pairwise(rest)];
};
let last = coll => coll[coll.length - 1];
let wrapInBlock = ast => ["block-declaration", [ast]];
let translate = (ast, depth = 0, parentType = null, symbolWhitelist = new Set()) => {
return ast.reduce((acc, curr, i) => {
let [type, children] = curr;
let result;
switch(type) {
case "symbol": {
let symbol = children;
let symbolCheck = symbol;
if (symbol.startsWith("...")) {
symbolCheck = symbol.slice(3);
}
if (symbolWhitelist.size && !symbolWhitelist.has(symbolCheck) && !symbolCheck.startsWith(".")) {
throw `Cannot access symbol ${symbolCheck} in strict function. Pass ${symbolCheck} in fn! declaration instead or declare it using let block`;
}
// If what follows is a function declaration,
// we can discard this symbol, because later on
// we only need to say () => {}, not fn () => {}.
if (symbol === "fn" || symbol === "fn!") {
result = "";
} else {
result = safe_symbol(symbol);
}
break;
}
case "boolean-literal": {
result = children;
break;
}
case "null-literal": {
result = "null";
break;
}
case "string": {
result = "`" + children + "`";
break;
}
case "keyword": {
result = "Symbol.for(\"" + children + "\")";
break;
}
case "number": {
result = children;
break;
}
case "array-literal": {
result = "[" +
children.reduce(conjoin_children(depth, symbolWhitelist), "")
+ "]";
break;
}
case "map-literal": {
result = "new Map([" +
pairwise(children)
.map(pairs => ["array-literal", pairs])
.reduce(conjoin_children(depth, symbolWhitelist), "")
+ "])";
break;
}
case "set-literal": {
result = "new Set([" +
children.reduce(conjoin_children(depth, symbolWhitelist), "")
+ "])";
break;
}
case "block-declaration": {
result = "{" + translate(children, 0, "block-declaration", symbolWhitelist) + "}";
break;
}
case "function-call": {
let [_, [__, invocable], [___, fnArgs]] = curr;
// Is this a function *declaration*?
if (invocable[0] === "symbol" && (invocable[1] === "fn" || invocable[1] === "fn!")) {
let fnBlock = fnArgs[fnArgs.length - 1];
let args = fnArgs.slice(0, fnArgs.length - 1);
if (!fnBlock) {
throw("Function declaration requires at least one argument");
} else if (fnBlock[0] !== "block-declaration") {
fnBlock = wrapInBlock(fnBlock);
}
let whitelist;
// fn! designated a special function that is virtually guaranteed
// to be pure because the compiler raises an exception when the
// function body accesses a symbol the function wasn't explicitly
// passed (I got this idea from Brian Will, he briefly mentioned it
// in one of his videos, but I can't remember which one:
// https://www.youtube.com/user/briantwill/)
if (invocable[1] === "fn!" || symbolWhitelist.size) {
let standardSymbols = ["fn", "fn!", "let", "def", "if"];
whitelist = new Set(
args
.map(last)
.map(arg => arg.startsWith("...") ? arg.slice(3) : arg)
.concat(standardSymbols)
);
} else {
whitelist = new Set();
}
// Always add existing whitelist (called "symbolWhitelist") to new whitelist
whitelist = new Set([...whitelist, ...symbolWhitelist]);
result = "((" +
args.reduce(conjoin_children(depth), "")
+ ") => " + translate([fnBlock], depth + 1, null, whitelist) + ")";
// No, it's really a function *invocation*
} else {
// We want to invoke an operator
if (invocable[1] === "invoke-operator") {
if (fnArgs.length === 2) {
// If it's the throw operator, we need to wrap it
// in a self-invoking fn, or else preceding it with
// a return statement as we do later on will cause JS
// to complain.
if (fnArgs[0][1] === "throw") {
result = `(() => {${fnArgs[0][1]} ${translate([fnArgs[1]], depth + 1, null, symbolWhitelist)}})()`;
} else {
result = `${fnArgs[0][1]} ${translate([fnArgs[1]], depth + 1, null, symbolWhitelist)}`;
}
} else if (fnArgs.length === 3) {
result = `(${translate([fnArgs[1]], depth + 1, null, symbolWhitelist)} ${fnArgs[0][1]} ${translate([fnArgs[2]], depth + 1, null, symbolWhitelist)})`;
} else {
throw `Operators require exactly one or two arguments. ${fnArgs.length - 1} given`;
}
// We want to declare a variable
} else if (invocable[1] === "def") {
let varName = fnArgs[0];
let val = fnArgs[1];
if (symbolWhitelist.size) {
// TODO: Why is mutation necessary here?
// It fails when I pass
// new Set([...symbolWhitelist, varName[1]])
// to the second call to translate below
// instead of just symbolWhitelist.
symbolWhitelist.add(varName[1]);
}
result = `let ${translate([varName], depth + 1)} = ${translate([val], depth + 1, null, symbolWhitelist)}`;
// We want to set a field
} else if (invocable[1] === "set-property") {
let settable = fnArgs[0];
let field = fnArgs[1];
let val = fnArgs[2];
result = `((${translate([settable], depth + 1, null, symbolWhitelist)})[(${translate([field], depth + 1, null, symbolWhitelist)})] = (${translate([val], depth + 1, null, symbolWhitelist)}))`;
// We are declaring a conditional
// TODO: Use the truthy? fn from core
// to evaluate the if condition; otherwise,
// empty strings and 0 will evaluate to false here,
// even though they don't anywhere else using the
// core fns, which would get very confusing for
// consumders of this language.
} else if (invocable[1] === "if") {
if (fnArgs.length < 2) {
throw "If form requires at least two arguments";
}
let condition = fnArgs[0];
let trueBranch = fnArgs[1];
let falseBranch = fnArgs[2];
if (trueBranch[0] !== "block-declaration") {
trueBranch = wrapInBlock(trueBranch);
}
let translatedCondition = translate([condition], depth + 1, null, symbolWhitelist);
translatedCondition = `((${translatedCondition}) !== false && (${translatedCondition}) !== null && (${translatedCondition}) !== undefined)`;
if (falseBranch) {
if (falseBranch[0] !== "block-declaration") {
falseBranch = wrapInBlock(falseBranch);
}
result = `(${translatedCondition} ?
(() => ${translate([trueBranch], depth + 1, null, symbolWhitelist)})()
:
(() => ${translate([falseBranch], depth + 1, null, symbolWhitelist)})())`;
} else {
result = `(${translatedCondition} ?
(() => ${translate([trueBranch], depth + 1, null, symbolWhitelist)})() : undefined)`;
}
// We are declaring variables in a let-block
} else if (invocable[1] === "let") {
if (fnArgs[0][1].length % 2 !== 0) {
throw "Uneven number of elements in let";
}
let declarations = pairwise(fnArgs[0][1]);
let blockDeclaration = fnArgs[1];
if (blockDeclaration[0] !== "block-declaration") {
blockDeclaration = wrapInBlock(blockDeclaration);
}
block = blockDeclaration[1];
// First, we transform the declarations and block
// into a new structure which we can then translate.
// (Doing this feels macro-ish. I need to look into
// supporting macros. Maybe all special forms can
// be rewritten to use macros instead and then they
// don't even need to be part of the compiler.)
let build = (declarations, block) => {
if (declarations.length === 0) {
return block;
}
let declaration = declarations[0];
let rest = declarations.slice(1);
return [
[
"function-call",
[
"invocable",
[
"function-call",
[
"invocable",
["symbol", "fn"]
],
[
"argument-list",
[
declaration[0],
[
"block-declaration",
build(rest, block)
]
]
]
]
],
[
"argument-list",
[declaration[1]]
]
]
]
};
let tree = build(declarations, block);
result = "(" + translate(tree, depth + 1, null, symbolWhitelist) + ")";
// We are reading or setting a field
} else if (invocable[1][0] === ".") {
// We are reading a field
if (invocable[1][1] === "-") {
invocable[1] = invocable[1].slice(2);
invocable[0] = "string";
result = `((${translate([fnArgs[0]], depth + 1, null, symbolWhitelist)})[(${translate([invocable], depth + 1, null, symbolWhitelist)})])`;
} else {
// We are invoking an instance method
result = "(" + "(" + translate([fnArgs[0]], depth + 1, null, symbolWhitelist) + ")" + translate([invocable], depth + 1, null, symbolWhitelist) + "(" + fnArgs.slice(1).reduce(conjoin_children(depth, symbolWhitelist), "") + ")" + ")";
}
// We are instantiating a constructor
} else if (invocable[1][invocable[1].length - 1] === ".") {
invocable[1] = invocable[1].slice(0, -1);
result = `(new ${translate([invocable], depth + 1, null, symbolWhitelist)}(${fnArgs.reduce(conjoin_children(depth, symbolWhitelist), "")}))`;
} else {
result = "(" + translate([invocable], depth + 1, null, symbolWhitelist) + ")" + "(" +
fnArgs.reduce(conjoin_children(depth, symbolWhitelist), "")
+ ")";
}
}
break;
}
}
// TODO: Prepend last child in fn block with string "return."
// It may be fine to just do so in *any* block as long as I
// only ever use blocks with functions.
// Determine if we want to add a semicolon
// and line break at the end. We only want
// to do that if we are still at the top
// level and the next element in the ast is
// not a function call (otherwise we're putting
// a semicolon between a function's name and
// its parentheses).
let eol = depth === 0;
// If we are inside a block declaration, we want
// to prepend a return statement to the last
// element.
let returnStatement;
if (parentType === "block-declaration" && i === ast.length - 1) {
returnStatement = "return ";
} else {
returnStatement = "";
}
return acc + returnStatement + result + (eol ? ";\n" : " ");
}, "");
};
module.exports = { translate, safe_symbol };