-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny-compiler.js
199 lines (186 loc) · 5.04 KB
/
tiny-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
// 将源代码拆分成一系列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;
}
if (char === ":") {
tokens.push({
type: "colon",
value: ":"
});
current++;
continue;
}
if (char === ",") {
tokens.push({
type: "comma",
value: ","
});
current++;
continue;
}
if (char === ";") {
// tokens.push({
// type: "semicolon",
// value: ";"
// });
current++;
continue;
}
// 去除空格
let WHITESPACE = /\s/;
if (WHITESPACE.test(char)) {
current++;
continue;
}
// 解析字符串,为了方便暂时只处理双引号的字符串
if (char === '"') {
let value = "";
char = input[++current];
while (char !== '"') {
value += char;
char = input[++current];
}
current++;
tokens.push({
type: "string",
value
});
continue;
}
// 处理函数、变量名
let LETTERS = /[a-z]/i;
if (LETTERS.test(char)) {
let value = "";
while (LETTERS.test(char)) {
value += char;
char = input[++current];
}
tokens.push({ type: "name", value });
continue;
}
throw new TypeError("I dont know what this character is: " + char);
}
return tokens;
}
// 将词法分析获得的tokens解析成AST
function parser(tokens) {
let current = 0;
let ast = {
type: "Program",
body: []
};
while (current < tokens.length) {
ast.body.push(walk());
}
return ast;
function walk() {
let token = tokens[current];
if (token.type === "string") {
current++;
return {
type: "StringLiteral",
value: token.value
};
}
if (token.type === "semicolon") {
current++;
return {
type: "LineEnd",
value: token.value
};
}
if (token.type === "comma" || token.type === "colon") {
current++;
return walk();
}
if (token.type === "name") {
let next = tokens[current + 1];
// 函数调用类型
if (next.type === "paren" && next.value === "(") {
let node = {
type: "CallExpression",
name: token.value,
params: []
};
token = tokens[++current];
++current;
while (token.type !== "paren" || token.value !== ")") {
let param = walk();
if (param) {
node.params.push(param);
}
token = tokens[current];
}
++current;
return node;
} else if (next.type === "colon") {
++current;
// 命名参数
let node = {
type: "NameParam",
name: token.value,
value: walk()
};
return node;
}
}
throw new TypeError(token.type);
}
}
// 根据ast拼接新的代码字符串
function codeGenerator(node, index, nodeList) {
switch (node.type) {
case "Program":
return node.body.map(codeGenerator).join("\n");
case "CallExpression":
return node.name +
"(" +
node.params.map(codeGenerator).join(", ") +
")";
// 处理命名参数形式
case "NameParam":
let str = "";
if (index === 0) {
str += "{";
}
str += node.name + ":" + codeGenerator(node.value);
if (index === nodeList.length - 1) {
str += "}";
}
return str;
case "StringLiteral":
return '"' + node.value + '"';
default:
console.log("node", node);
throw new TypeError(node.type);
}
}
function compiler(input) {
let tokens = tokenizer(input);
let ast = parser(tokens);
let output = codeGenerator(ast);
return output;
}
module.exports = {
tokenizer,
parser,
compiler
};