-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcodeGenerator.js
42 lines (41 loc) · 1.39 KB
/
codeGenerator.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
// 根据 AST 生成代码
export default function codeGenerator(ast) {
const generateSpecs = {
Program: node => {
return node.body.map(child => codeGenerator(child)).join(';');
},
ExpressionStatement: node => {
return `"${codeGenerator(node.expression)}"`;
},
Literal: node => {
return node.value;
},
VariableDeclaration: node => {
// 暂时只处理每行只定义一个变量的情况
return `${node.kind} ${codeGenerator(node.declarations[0])}`;
},
VariableDeclarator: node => {
return `${codeGenerator(node.id)} = ${codeGenerator(node.init)}`;
},
Identifier: node => {
return node.name;
},
FunctionExpression: node => {
const functionName = codeGenerator(node.id);
const functionParams = node.params.map(param => codeGenerator(param)).join(', ');
const functionBody = codeGenerator(node.body);
return `function ${functionName}(${functionParams}) ${functionBody}`;
},
BlockStatement: node => {
const statementBody = `${node.body.map(child => codeGenerator(child)).join(';')};`;
return `{${statementBody}};`;
},
ReturnStatement: node => {
return `return ${codeGenerator(node.argument)}`;
},
BinaryExpression: node => {
return `${codeGenerator(node.left)} ${node.operator} ${codeGenerator(node.right)}`;
},
};
return generateSpecs[ast.type](ast);
}