forked from QC-L/babel-react-to-miniapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
288 lines (253 loc) · 5.67 KB
/
parse.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
const operationStr = `(add 4 (subtract 5 3))`;
console.log('原代码字符串: ', operationStr);
function tokenizer(input) {
var current = 0;
var tokens = [];
while(current < input.length) {
var char = input[current];
// 判断左括号
if (char === '(') {
var token = {
type: 'paren',
value: char
}
tokens.push(token);
current++;
continue;
}
// 判断右括号
if (char === ')') {
var token = {
type: 'paren',
value: char
}
tokens.push(token);
current++;
continue;
}
// 判断是否为空格
var WHITESPACE = /\s/;
if (WHITESPACE.test(char)) {
current++;
continue;
}
// 当遇到数字时, 从这里开始
var NUMBER = /[0-9]/;
if (NUMBER.test(char)) {
var value = '';
while(NUMBER.test(char)) {
value += char;
char = input[++current];
}
var token = {
type: 'number',
value: value
}
tokens.push(token);
continue;
}
var LETTERS = /[a-z]/i;
if (LETTERS.test(char)) {
var value = '';
while(LETTERS.test(char)) {
value += char;
char = input[++current];
}
var token = {
type: 'name',
value: value
}
tokens.push(token);
continue;
}
throw new Error('I dont know what this character is: ' + char);
}
return tokens;
}
// var tokens = tokenizer(operationStr);
// console.log('生成的词法数组为:');
// console.log(tokens);
// 生成 AST
function parser(tokens) {
let current = 0;
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);
}
let ast = {
type: 'Program',
body: []
}
while (current < tokens.length) {
ast.body.push(walk());
}
return ast;
}
// let ast = parser(tokens);
// console.log('生成的 AST 结构为: ');
// console.dir(ast, {
// color: true,
// depth: null
// });
// AST 遍历器
// 访问器模式
function traverser(ast, visitor) {
function traverseArray(array, parent) {
array.forEach(function (child) {
traverseNode(child, parent);
})
}
function traverseNode(node, parent) {
let method = visitor[node.type];
if (method && method.enter) {
method.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 (method && method.exit) {
method.exit(node, parent);
}
}
traverseNode(ast, null);
}
// 转换器
function transformer(ast) {
let newAst = {
type: 'Program',
body: []
};
ast._content = newAst.body;
traverser(ast, {
CallExpression: {
enter(node, parent) {
let expression = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: node.name
},
arguments: []
}
node._content = expression.arguments;
if (parent.type !== 'CallExpression') {
expression = {
type: 'ExpressionStatement',
expression: expression
}
}
parent._content.push(expression);
}
},
NumberLiteral: {
enter(node, parent) {
parent._content.push({
type: 'NumberLiteral',
value: node.value
})
}
},
StringLiteral: {
enter(node, parent) {
parent._content.push({
type: 'StringLiteral',
value: node.value
})
}
}
});
return newAst;
}
// let newAst = transformer(ast);
// console.log('新生成的 AST 结构为: ');
// console.dir(newAst, {
// color: true,
// depth: null
// });
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) {
// 词法分析,生成词法结构
let tokens = tokenizer(input);
// 生成 AST
let ast = parser(tokens);
// 转译代码,生成新 AST
let newAst = transformer(ast);
// 生成新的代码
let output = codeGenerator(newAst);
return output;
}
var output = compiler(operationStr);
console.log('转译后的代码: ', output);
function add(a, b) {
console.log(a, b);
return a + b;
}
function subtract(a, b) {
console.log(a, b);
return a - b;
}
eval(output);