-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathneo.codegen.js
463 lines (427 loc) · 12.5 KB
/
neo.codegen.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// neo.codegen.js
// Douglas Crockford
// 2022-07-29
/*property
abs, alphameric, array, break, call, char, code, create, def, export, fail,
false, forEach, fraction, id, if, import, integer, isArray, join, length,
let, loop, map, neg, not, null, number, origin, push, record, repeat,
replace, return, startsWith, stone, string, stringify, text, true, twoth,
var, wunth, zeroth
*/
import big_float from "./big_float.js";
import $NEO from "./neo.runtime.js";
function make_set(array, value = true) {
const object = Object.create(null);
array.forEach(function (element) {
object[element] = value;
});
return $NEO.stone(object);
}
const boolean_operator = make_set([
"array?", "boolean?", "function?", "integer?", "not", "number?", "record?",
"stone?", "text?", "true", "=", "≠", "<", ">", "≤", "≥", "/\\", "\\/"
]);
const reserved = make_set([
"arguments", "await", "break", "case", "catch", "class", "const",
"continue", "debugger", "default", "delete", "do", "else", "enum", "eval",
"export", "extends", "false", "finally", "for", "function", "if",
"implements", "import", "in", "Infinity", "instanceof", "interface", "let",
"NaN", "new", "null", "package", "private", "protected", "public", "return",
"static", "super", "switch", "this", "throw", "true", "try", "typeof",
"undefined", "var", "void", "while", "with", "yield"
]);
const primordial = $NEO.stone({
"abs": "$NEO.abs",
"array": "$NEO.array",
"array?": "Array.isArray",
"bit and": "$NEO.bitand",
"bit mask": "$NEO.bitmask",
"bit or": "$NEO.bitor",
"bit shift down": "$NEO.bitdown",
"bit shift up": "$NEO.bitup",
"bit xor": "$NEO.bitxor",
"boolean?": "$NEO.boolean_",
"char": "$NEO.char",
"code": "$NEO.code",
"false": "false",
"fraction": "$NEO.fraction",
"function?": "$NEO.function_",
"integer": "$NEO.integer",
"integer?": "$NEO.integer_",
"length": "$NEO.length",
"neg": "$NEO.neg",
"not": "$NEO.not",
"null": "undefined",
"number": "$NEO.make",
"number?": "$NEO.is_big_float",
"record": "$NEO.record",
"record?": "$NEO.record_",
"stone": "$NEO.stone",
"stone?": "Object.isFrozen",
"text": "$NEO.text",
"text?": "$NEO.text_",
"true": "true"
});
let indentation;
function indent() {
indentation += 4;
}
function outdent() {
indentation -= 4;
}
function begin() {
// At the beginning of each line we emit a line break and padding.
return "\n" + " ".repeat(indentation);
}
let front_matter;
let operator_transform;
let statement_transform;
let unique;
const rx_space_question = /[\u0020?]/g;
function mangle(name) {
// JavaScript does not allow space or '?' in identifiers, so we
// replace them with '_'. We give reserved words a '$' prefix.
// So 'what me worry?' becomes 'what_me_worry_', and 'class' becomes '$class'.
return (
reserved[name] === true
? "$" + name
: name.replace(rx_space_question, "_")
);
}
const rx_minus_point = /[\-.]/g;
function numgle(number) {
// We make big decimal literals look as natural as possible by making them into
// constants. A constant name start with '$'. A '-' or '.' is replaced with '_'.
// So, '1' becomes '$1', '98.6' becomes '$98_6', and '-1.011e-5' becomes
// '$_1_011e_5'.
const text = big_float.string(number.number);
const name = "$" + text.replace(rx_minus_point, "_");
if (unique[name] !== true) {
unique[name] = true;
front_matter.push(
"const " + name + " = $NEO.number(\"" + text + "\");\n"
);
}
return name;
}
function op(thing) {
const transform = operator_transform[thing.id];
return (
typeof transform === "string"
? (
thing.zeroth === undefined
? transform
: transform + "(" + expression(thing.zeroth) + (
thing.wunth === undefined
? ""
: ", " + expression(thing.wunth)
) + ")"
)
: transform(thing)
);
}
function expression(thing) {
if (thing.id === "(number)") {
return numgle(thing);
}
if (thing.id === "(text)") {
return JSON.stringify(thing.text);
}
if (thing.alphameric) {
return (
thing.origin === undefined
? primordial[thing.id]
: mangle(thing.id)
);
}
return op(thing);
}
function array_literal(array) {
return "[" + array.map(function (element) {
return (
Array.isArray(element)
? array_literal(element)
: expression(element)
);
}).join(", ") + "]";
}
function record_literal(array) {
indent();
const padding = begin();
const string = "(function (o) {" + array.map(function (element) {
return padding + (
typeof element.zeroth === "string"
? (
"o["
+ JSON.stringify(element.zeroth)
+ "] = "
+ expression(element.wunth)
+ ";"
)
: (
"$NEO.set(o, "
+ expression(element.zeroth)
+ ", "
+ expression(element.wunth)
+ ");"
)
);
}).join("") + padding + "return o;";
outdent();
return string + begin() + "}(Object.create(null)))";
}
function assert_boolean(thing) {
const string = expression(thing);
return (
(
boolean_operator[thing.id] === true
|| (
thing.zeroth !== undefined
&& thing.zeroth.origin === undefined
&& boolean_operator[thing.zeroth.id]
)
)
? string
: "$NEO.assert_boolean(" + string + ")"
);
}
function statements(array) {
const padding = begin();
return array.map(function (statement) {
return padding + statement_transform[statement.id](statement);
}).join("");
}
function block(array) {
indent();
const string = statements(array);
outdent();
return "{" + string + begin() + "}";
}
statement_transform = $NEO.stone({
break: function (ignore) {
return "break;";
},
call: function (thing) {
return expression(thing.zeroth) + ";";
},
def: function (thing) {
return (
"var " + expression(thing.zeroth)
+ " = " + expression(thing.wunth) + ";"
);
},
export: function (thing) {
const exportation = expression(thing.zeroth);
return "export default " + (
exportation.startsWith("$NEO.stone(")
? exportation
: "$NEO.stone(" + exportation + ")"
) + ";";
},
fail: function () {
return "throw $NEO.fail(\"fail\");";
},
if: function if_statement(thing) {
return (
"if ("
+ assert_boolean(thing.zeroth)
+ ") "
+ block(thing.wunth)
+ (
thing.twoth === undefined
? ""
: " else " + (
thing.twoth.id === "if"
? if_statement(thing.twoth)
: block(thing.twoth)
)
)
);
},
import: function (thing) {
return (
"import " + expression(thing.zeroth)
+ " from " + expression(thing.wunth) + ";"
);
},
let: function (thing) {
const right = (
thing.wunth.id === "[]"
? expression(thing.wunth.zeroth) + ".pop();"
: expression(thing.wunth)
);
if (thing.zeroth.id === "[]") {
return expression(thing.zeroth.zeroth) + ".push(" + right + ");";
}
if (thing.zeroth.id === ".") {
return (
"$NEO.set(" + expression(thing.zeroth.zeroth)
+ ", " + JSON.stringify(thing.zeroth.wunth.id)
+ ", " + right + ");"
);
}
if (thing.zeroth.id === "[") {
return (
"$NEO.set(" + expression(thing.zeroth.zeroth)
+ ", " + expression(thing.zeroth.wunth)
+ ", " + right + ");"
);
}
return expression(thing.zeroth) + " = " + right + ";";
},
loop: function (thing) {
return "while (true) " + block(thing.zeroth);
},
return: function (thing) {
return "return " + expression(thing.zeroth) + ";";
},
var: function (thing) {
return "var " + expression(thing.zeroth) + (
thing.wunth === undefined
? ";"
: " = " + expression(thing.wunth) + ";"
);
}
});
const functino = $NEO.stone({
"?": "$NEO.ternary",
"|": "$NEO.default",
"/\\": "$NEO.and",
"\\/": "$NEO.or",
"=": "$NEO.eq",
"≠": "$NEO.ne",
"<": "$NEO.lt",
"≥": "$NEO.ge",
">": "$NEO.gt",
"≤": "$NEO.le",
"~": "$NEO.cat",
"≈": "$NEO.cats",
"+": "$NEO.add",
"-": "$NEO.sub",
">>": "$NEO.max",
"<<": "$NEO.min",
"*": "$NEO.mul",
"/": "$NEO.div",
"[]": "$NEO.get",
"(": "$NEO.resolve"
});
operator_transform = $NEO.stone({
"?": function (thing) {
indent();
let padding = begin();
let string = (
"(" + padding + assert_boolean(thing.zeroth)
+ padding + "? " + expression(thing.wunth)
+ padding + ": " + expression(thing.twoth)
);
outdent();
return string + begin() + ")";
},
"/\\": function (thing) {
return (
"(" + assert_boolean(thing.zeroth)
+ " && " + assert_boolean(thing.wunth)
+ ")"
);
},
"\\/": function (thing) {
return (
"(" + assert_boolean(thing.zeroth)
+ " || " + assert_boolean(thing.wunth)
+ ")"
);
},
"=": "$NEO.eq",
"≠": "$NEO.ne",
"<": "$NEO.lt",
"≥": "$NEO.ge",
">": "$NEO.gt",
"≤": "$NEO.le",
"~": "$NEO.cat",
"≈": "$NEO.cats",
"+": "$NEO.add",
"-": "$NEO.sub",
">>": "$NEO.max",
"<<": "$NEO.min",
"*": "$NEO.mul",
"/": "$NEO.div",
"|": function (thing) {
return (
"(function (_0) {"
+ "return (_0 === undefined) ? "
+ expression(thing.wunth) + " : _0);}("
+ expression(thing.zeroth) + "))"
);
},
"...": function (thing) {
return "..." + expression(thing.zeroth);
},
".": function (thing) {
return (
"$NEO.get(" + expression(thing.zeroth)
+ ", \"" + thing.wunth.id + "\")"
);
},
"[": function (thing) {
if (thing.wunth === undefined) {
return array_literal(thing.zeroth);
}
return (
"$NEO.get(" + expression(thing.zeroth)
+ ", " + expression(thing.wunth) + ")"
);
},
"{": function (thing) {
return record_literal(thing.zeroth);
},
"(": function (thing) {
return (
expression(thing.zeroth) + "("
+ thing.wunth.map(expression).join(", ") + ")"
);
},
"[]": "[]",
"{}": "Object.create(null)",
"ƒ": function (thing) {
if (typeof thing.zeroth === "string") {
return functino[thing.zeroth];
}
return "$NEO.stone(function (" + thing.zeroth.map(function (param) {
if (param.id === "...") {
return "..." + mangle(param.zeroth.id);
}
if (param.id === "|") {
return (
mangle(param.zeroth.id) + " = " + expression(param.wunth)
);
}
return mangle(param.id);
}).join(", ") + ") " + (function () {
if (Array.isArray(thing.wunth)) {
if (thing.twoth !== undefined) {
let padding = begin();
let body;
indent();
body = (
"{" + begin() + "try " + block(thing.wunth)
+ " catch (ignore) " + block(thing.twoth)
+ padding + "}"
);
outdent();
return body;
}
return block(thing.wunth);
}
return "{return " + expression(thing.wunth) + ";}";
}()) + ")";
}
});
export default $NEO.stone(function codegen(tree) {
front_matter = [
"import $NEO from \"./neo.runtime.js\"\n"
];
indentation = 0;
unique = Object.create(null);
const bulk = statements(tree.zeroth);
return front_matter.join("") + bulk;
});