-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
269 lines (230 loc) · 5.32 KB
/
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
// * 它产生的 Token 看起来或许是这样的:
// *
// * [
// * { 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: 'paren', value: ')' },
// * { type: 'paren', value: ')' }
// * ]
// *
// * 它的抽象语法树(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'
// * }]
// * }]
// * }]
// * }
// */
//词法解析 获取tokens
function tokenizer (input) {
let tokens = [];
let current = 0;
while (current < input.length) {
let char = input[current];
if (char === '(') {
tokens.push({
type: 'paren',
value: '('
})
current++;
continue;
}
if (char === ')') {
tokens.push({
type: 'paren',
value: ')'
})
current++;
continue;
}
let WHITESPACE = /\s/ //空格
if (WHITESPACE.test(char)) {
current++;
continue;
}
//获取连接的数字
let NUMBER_REG = /\d/
if (NUMBER_REG.test(char)) {
let value = '';
while (NUMBER_REG.test(char)) {
value += char;
char = input[++current];
}
tokens.push({
type: 'number',
value
})
continue
}
//获取连接的字符串
let WORD_REG = /\w/
if (WORD_REG.test(char)) {
let value = '';
while (WORD_REG.test(char)) {
value += char;
char = input[++current];
}
tokens.push({
type: 'name',
value
})
continue
}
}
return tokens;
console.log('tokens: ', tokens);
}
//语法解析 得到 ast
function parser (tokens) {
let current = 0;
function walk () {
let node = tokens[current];
if (node.type === 'number') {
current++;
return {
type: 'NumberLiteral',
value: node.value
}
}
if (node.type === 'paren' && node.value === '(') {
let token = tokens[++current];
let node = {
type: 'CallExpression',
name: token.value,
params: []
}
// current++;
token = tokens[++current]
console.log('current: ', current);
while (token.type != 'paren' || (token.type === 'paren' && token.value !== ')')) {
node.params.push(walk())
// current++;
token = tokens[current]
}
current++;
return node;
}
}
let ast = {
type: 'Program',
body: []
}
while (current < tokens.length) {
ast.body.push(walk())
current++;
}
return ast;
}
//遍历器
function traverser (ast, visitor) {
function traverArr (arr, parent) {
arr.forEach(node => {
traverNode(node, parent)
})
}
function traverNode (node, parent) {
let method = visitor[node.type];
if (method) {
method(node, parent);
}
switch (node.type) {
case 'Program':
traverArr(node.body, node);
break;
case 'CallExpression':
traverArr(node.params, node);
break;
case 'NumberLiteral':
break;
default:
new TypeError(node.type)
}
}
traverNode(ast, null)
}
//ast 转换成 newAst
function transformer (ast) {
let newAst = {
type: 'Program',
body: [],
}
ast._context = newAst.body;
traverser(ast, {
NumberLiteral: function (node, parent) {
parent._context.push({
type: 'NumberLiteral',
value: node.value
})
},
CallExpression: function (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 codeGenerator (ast) {
switch (ast.type) {
case 'Program':
return ast.body.map(codeGenerator);
case 'ExpressionStatement':
return codeGenerator(ast.expression) + ';';
case 'CallExpression':
return (codeGenerator(ast.callee) + '(' + ast.arguments.map(codeGenerator).join(',') + ')');
case 'NumberLiteral':
return ast.value;
case 'Identifier':
return ast.name
}
}
function compiler (input) {
let tokens = tokenizer(input);
let ast = parser(tokens);
console.log('ast: ', JSON.stringify(ast));
let newAst = transformer(ast);
console.log('newAst: ', JSON.stringify(newAst));
let output = codeGenerator(newAst)
return output;
}
module.exports = {
tokenizer,
parser,
transformer,
codeGenerator,
compiler
}