-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
298 lines (281 loc) · 7.17 KB
/
index.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
/**
* 尝试做一个小的编译器
* 实例代码转换(add 2 4) 转换到add(2, 4)
* 第一步token化
*/
const LETTERS = /\w/i
const NUMBER = /\d/i
const WHITESPACE = /\s/i
/**
*
* Tokens might look something like this:
*
* [
* { 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: ')' },
* ]
*
* @param {*} input
* @returns
*/
function tokenizer(input) {
let tokens = []
const length = input.length
let i = 0
while (i < length) {
let letter = input[i]
/* 处理特殊的标识符 */
if (letter === '(') {
tokens.push({
type: 'paren',
value: '('
})
i++
continue
}
if (letter === ')') {
tokens.push({
type: 'paren',
value: ')'
})
i++
continue
}
/* 处理空白字符 */
if (WHITESPACE.test(letter)) {
i++
continue
}
/* 处理数字参数 */
if (NUMBER.test(letter)) {
let value = ''
while (NUMBER.test(letter)) {
value += letter
letter = input[++i]
}
tokens.push({
type: 'number',
value
})
continue
}
/* 处理字符参数 */
if (letter === '"') {
let value = ''
letter = input[++i]
while (letter !== '"') {
value += letter
}
tokens.push({ type: 'string', value })
continue
}
/* 处理函数名 */
if (LETTERS.test(letter)) {
let value = ''
while (LETTERS.test(letter)) {
value += letter
letter = input[++i]
}
const token = {
type: 'name',
value: value
}
tokens.push(token)
continue
}
throw new TypeError('I dont know what this character is: ' + letter)
}
return tokens
}
function parse(tokens) {
const ast = {
type: 'Program',
body: []
}
const len = tokens.length
let i = 0
function walk() {
while (i < len) {
let token = tokens[i]
// let node = {}
// 处理数字
if (token.type === 'number') {
return {
type: 'NumberLiteral',
value: token.value
}
}
if (token.type === 'string') {
return {
type: 'StringLiteral',
value: token.value
}
}
if (token.type === 'paren' && token.value === '(') {
// 获取add
token = tokens[++i]
let node = {
type: 'CallExpression',
name: token.value,
params: []
}
token = tokens[++i]
while (
token.type !== 'paren' ||
(token.type === 'paren' && token.value !== ')')
) {
node.params.push(walk())
token = tokens[++i]
}
return node
}
if (token.value === ')') {
i++
}
}
}
ast.body.push(walk())
return ast
}
/*
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'
}]
}]
}]
};
*/
function traverser(ast, visitor) {
function transArray(array, parent) {
array.forEach(node => {
transNode(node, parent)
})
}
function transNode(node, parent) {
const methods = visitor[node.type]
if (methods && methods.enter) {
methods.enter(node, parent)
}
switch (node.type) {
case 'Program':
transArray(node.body, node)
break
case 'CallExpression':
transArray(node.params, node)
break
case 'NumberLiteral':
case 'StringLiteral':
break
default:
break
}
if (methods && methods.exit) {
methods.exit(node, parent)
}
}
transNode(ast, null)
}
function transform(ast) {
let newAst = {
type: 'Program',
body: []
}
ast._context = newAst.body
traverser(ast, {
NumberLiteral: {
enter(node, parent) {
parent._context.push({
type: 'NumberLiteral',
value: node.value
})
},
exit(node, parent) {}
},
StringLiteral: {
enter(node, parent) {
parent._context.push({
type: 'StringLiteral',
value: node.value
})
},
exit(node, parent) {}
},
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)
},
exit(node, parent) {}
}
})
return newAst
}
function codeGen(node) {
switch (node.type) {
case 'Program':
return `${node.body.map(codeGen)};`
case 'ExpressionStatement':
return codeGen(node.expression)
case 'CallExpression':
const args = node.arguments.map(codeGen)
return `${codeGen(node.callee)}(${args.join(', ')})`
case 'Identifier':
return node.name
case 'NumberLiteral':
return node.value
case 'StringLiteral':
return `'${node.value}'`
default:
break
}
}
function compiler(input) {
const tokens = tokenizer(input)
const ast = parse(tokens)
const newAst = transform(ast)
const output = codeGen(newAst)
return output
}
module.exports = {
tokenizer,
parse,
transform,
codeGen,
compiler
}