This repository has been archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmin-compiler.js
532 lines (470 loc) · 11.4 KB
/
min-compiler.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* @Author: Jsonz
* @Date: 2018-01-15 15:57:45
* @Last Modified by: Jsonz
* @Last Modified time: 2018-01-15 16:04:30
* @original Project: https://github.com/thejameskyle/the-super-tiny-compiler
* @line: https://jsonz1993.github.io/2018/01/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E6%9E%81%E7%AE%80%E7%BC%96%E8%AF%91%E5%99%A8-%E2%80%94%E2%80%94-%E4%BA%86%E8%A7%A3%E7%BC%96%E8%AF%91%E5%99%A8%E5%8E%9F%E7%90%86/
*/
const assert = require('assert');
const input = '(add 2 (subtract 4 2 "djwaqp"))';
const tokens = [{ type: 'paren', value: '(' },
{ type: 'name', value: 'add' },
{ type: 'number', value: '2' },
{ type: 'paren', value: '(' },
{ type: 'name', value: 'subtract' },
{ type: 'number', value: '4' },
{ type: 'number', value: '2' },
{ type: 'string', value: 'djwaqp' },
{ type: 'paren', value: ')' },
{ type: 'paren', value: ')' }];
const ast = {
"type": "Program",
"body": [{
"type": "CallExpression",
"name": "add",
"params": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"name": "subtract",
"params": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}]
};
const newAst = {
"type": "Program",
"body": [{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "add"
},
"arguments": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "subtract"
},
"arguments": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}
}]
};
const output = 'add(2, subtract(4, 2, "djwaqp"));';
/*
1. 有一个值存着当前的光标 `current`
2. 有一个数组用来存放按类型分出来的值 `tokens`
3. 对文本进行循环, 取 `current` 的值做 分类型处理
1. `(`
2. `)`
3. 空格
4. 数字
5. 字符串
6. 方法名
4. 将处理后的值存进数组 `token` 并返回
*/
/*
input: (add 2 (subtract 4 2 "djwaqp"))
output: [ { type: 'paren', value: '(' },
{ type: 'name', value: 'add' },
{ type: 'number', value: '2' },
{ type: 'paren', value: '(' },
{ type: 'name', value: 'subtract' },
{ type: 'number', value: '4' },
{ type: 'number', value: '2' },
{ type: 'string', value: 'djwaqp' },
{ type: 'paren', value: ')' },
{ type: 'paren', value: ')' } ]
*/
function tokenizer(input) {
let current = 0;
const tokens = [];
while (current < input.length) {
let char = input[current];
if (char === "(" || char === ")") {
tokens.push({
type: 'paren',
value: char
});
current++;
continue;
}
const WHITESPACE = /\s/;
if (WHITESPACE.test(char)) {
current++;
continue;
}
const LETTERS = /[a-z]/i;
if (LETTERS.test(char)) {
let name = "";
while (LETTERS.test(char)) {
name += char;
current++;
char = input[current];
}
tokens.push({
type: 'name',
value: name
});
continue;
}
const NUMBERS = /[0-9]/;
if (NUMBERS.test(char)) {
let numbers = "";
while(NUMBERS.test(char)) {
numbers += char;
current++;
char = input[current];
}
tokens.push({
type: 'number',
value: numbers
});
continue;
}
if (char === '"') {
let string = '';
current++;
char = input[current];
while(char !== '"') {
string += char;
current++;
char = input[current];
}
tokens.push({
type: 'string',
value: string
});
current++;
continue;
}
throw new TypeError('不知道你输入的是什么鬼东西 ' + char);
}
return tokens;
}
/*
1. 首先也要有一个 `current` 对 `tokens` 进行遍历,每一项 `token` 进行分析处理
2. 有一棵树,顶级结构为 `{type: 'Program' , body: [...]}`
3. 根据 `token.type` 进行相应的归类处理
input: [ { type: 'paren', value: '(' },
{ type: 'name', value: 'add' },
{ type: 'number', value: '2' },
{ type: 'paren', value: '(' },
{ type: 'name', value: 'subtract' },
{ type: 'number', value: '4' },
{ type: 'number', value: '2' },
{ type: 'string', value: 'djwaqp' },
{ type: 'paren', value: ')' },
{ type: 'paren', value: ')' }
]
output: {
"type": "Program",
"body": [{
"type": "CallExpression",
"name": "ADD",
"params": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"name": "subtract",
"params": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}]
}
*/
function parser(tokens) {
let current = 0;
const ast = {
type: 'Program',
body: []
};
function walk() {
let token = tokens[current];
if (token.type === 'number') {
current ++;
return {
type: 'NumberLiteral',
value: token.value
}
}
if (token.type === 'string') {
current ++;
return {
type: 'StringLiteral',
value: token.value
}
}
if (token.type === 'paren' && token.value === '(') {
token = tokens[++current];
let node = {
type: "CallExpression",
name: token.value,
params: []
}
token = tokens[++current];
while ((token.type !== 'paren') || (token.type === 'paren' && token.value !== ')')) {
node.params.push(walk());
token = tokens[current];
}
current++;
return node;
}
throw new TypeError(token.type);
}
while(current < tokens.length) {
ast.body.push(walk());
}
return ast;
}
/*
1. 首先,要有一颗树,和 ast 一样。 顶级结构为 `{ type: 'Program', body: [...] }`
2. 在 `ast` 上建一个引用`_context`到 `newAst.body`;
3. 对 ast 树进行处理, 直接处理 `_context`
4. 对 ast 上 每个类型都做 enter 处理与 exit处理的钩子
1. Number 或 String 直接处理并添加到树节点上即可
2. CallExpression 则要创建一个数组去存参数
5. 对类型为:`Program` 或 `CallExpression` 对子级进行 递归 `4` 处理
6. 将处理后的 newAst 返回
input:
{
"type": "Program",
"body": [{
"type": "CallExpression",
"name": "ADD",
"params": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"name": "subtract",
"params": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}]
}
output:
{
"type": "Program",
"body": [{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "ADD"
},
"arguments": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "subtract"
},
"arguments": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}
}]
}
*/
function transformer(ast) {
const newAst = {
type: 'Program',
body: []
};
ast._context = newAst.body;
traverser(ast, {
NumberLiteral: {
enter(node, parent) {
parent._context.push({
type: 'NumberLiteral',
value: node.value
});
}
},
StringLiteral: {
enter(node, parent) {
parent._context.push({
type: 'StringLiteral',
value: node.value,
});
}
},
CallExpression: {
enter(node, parent) {
let expression = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: node.name
},
arguments: []
}
node._context = expression.arguments;
if (parent.type !== 'CallExpression') {
expression = {
type: 'ExpressionStatement',
expression: expression
}
}
parent._context.push(expression);
}
}
});
return newAst;
}
function traverser(node, visitor) {
function traverseArray(nodeArr, parent) {
nodeArr.forEach(child => traverseNode(child, parent));
}
function traverseNode(node, parent) {
const methods = visitor[node.type];
if (methods && methods.enter) {
methods.enter(node, parent);
}
switch (node.type) {
case 'Program':
traverseArray(node.body, node);
break;
case 'CallExpression':
traverseArray(node.params, node);
break;
case 'NumberLiteral':
case 'StringLiteral':
break;
default:
throw new TypeError(node.type);
}
if (methods && methods.exit) {
methods.exit(node, parent);
}
}
traverseNode(node, null);
}
/*
1. 根据 node.type 做对应逻辑:
2. `Program` => 对 node.body 进行递归
3. `ExpressionStatement`=> 对 node.expression 进行处理
4. `CallExpression` => 对 node.callee 与 node.arguments 进行处理
5. `Identifier` && `NumberLiteral` && `StringLiteral` 直接返回对应的字段
input:
{
"type": "Program",
"body": [{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "ADD"
},
"arguments": [{
"type": "NumberLiteral",
"value": "2"
}, {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "subtract"
},
"arguments": [{
"type": "NumberLiteral",
"value": "4"
}, {
"type": "NumberLiteral",
"value": "2"
}, {
"type": "StringLiteral",
"value": "djwaqp"
}]
}]
}
}]
}
output:
add(2, subtract(4, 2, "djwaqp"));
*/
function codeGenerator(node) {
switch (node.type) {
case 'Program':
return node.body.map(codeGenerator).join('\n');
case 'ExpressionStatement':
return codeGenerator(node.expression) + ';';
case 'CallExpression':
return (codeGenerator(node.callee) +'(' + node.arguments.map(codeGenerator).join(', ') + ')');
case 'Identifier':
return node.name;
case 'NumberLiteral':
return node.value;
case 'StringLiteral':
return '"' + node.value + '"';
default:
throw new TypeError(node.type);
}
}
function compiler(input) {
return codeGenerator(transformer(parser(tokenizer(input))));
}
const _tokens = tokenizer(input);
const _ast = parser(_tokens);
const _newAst = transformer(_ast);
const _output = codeGenerator(_newAst);
assert.deepStrictEqual(_tokens, tokens, 'Tokenizer should turn `input` string into `tokens` array');
assert.deepStrictEqual(parser(_tokens), ast, 'Parser should turn `tokens` array into `ast`');
assert.deepStrictEqual(transformer(_ast), newAst, 'Transformer should turn `ast` into a `newAst`');
assert.deepStrictEqual(codeGenerator(_newAst), output, 'CodeGenerator should turn `newAst` into `output`');
assert.deepStrictEqual(compiler(input), output, ' compiler ');
console.log('Jsonz yoyo ');