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
| // before: (add 20 (subtract 4 2)) // after: add(2, subtract(4, 2))
// 分词,处理每个字符内容 function generateToken(str) { let current = 0; let tokens = []; while(current < str.length) { let char = str[current];
if (char === "(") { tokens.push({ type: "paren", value: "(" }); current++; continue; }
if (char === ")") { tokens.push({ type: "paren", value: ")" }); current++; continue; }
if (/\s/.test(char)) { current++; continue; }
if (/[0-9]/.test(char)) { let numberValue = ''; while(/[0-9]/.test(char)) { numberValue += char; char = str[++current]; } tokens.push({ type: 'number', value: numberValue }); continue; }
if (/[a-z]/.test(char)) { let stringValue = ''; while(/[a-z]/.test(char)) { stringValue += char; char = str[++current]; } tokens.push({ type: 'name', value: stringValue }); continue; }
throw new TypeError("未能识别的字符"); }
return tokens; }
// AST 生成 function generateAST(tokens) { let current = 0;
let ast = { type: "Program", body: [], };
function walk() { let token = tokens[current]; if (token.type === 'number') { current++; return { type: "NumberLiteral", 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; }
// AST 转化: babel 插件需要我们处理的部分 function transformer(ast) { let newAst = { type: "Program", body: [], };
ast._context = newAst.body;
DFS(ast, { NumberLiteral: { enter(node, parent) { parent._context.push({ type: "NumberLiteral", 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; }
// AST 遍历 function DFS(ast, visitor) {
function traverseArray(children, parent) { children.forEach(child => traverseNode(child, parent)); }
function traverseNode(node, parent) { let 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": break; } if (methods && methods.exit) { methods.exit(node, parent); } } return traverseNode(ast, null); }
// ast -> code 生成代码过程 function generate(ast) { switch(ast.type) { case "Program": return ast.body.map(subAst => generate(subAst)).join('\n'); case "ExpressionStatement": return generate(ast.expression) + ";"; case "CallExpression": return generate(ast.callee) + "(" +ast.arguments.map(arg => generate(arg)).join(', ') + ")"; case "Identifier": return ast.name; case "NumberLiteral": return ast.value; } }
function parser(input) { const tokens = generateToken(input); console.log(tokens); const ast = generateAST(tokens); console.log(JSON.stringify(ast, null, 2)); const newAst = transformer(ast); console.log(JSON.stringify(newAst, null, 2)); const code = generate(newAst); return code; }
module.exports = parser;
|