-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
282 lines (266 loc) · 8.4 KB
/
compiler.cpp
File metadata and controls
282 lines (266 loc) · 8.4 KB
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
#include <fstream>
using namespace std;
#include <ctype.h>
#include "compiler.h"
int AKCompiler::tokenizer(const string& input, TokenptrVector& tokens) {
int current = 0;
while (current < input.size()) {
char c = input.at(current);
if (DEF_IS_Left_Semi(c)) {
Token* t = new Token(Token_Type_Semi, "(");
tokens.push_back(t);
current++;
continue;
}
if (DEF_IS_Right_Semi(c)) {
Token* t = new Token(Token_Type_Semi, ")");
tokens.push_back(t);
current++;
continue;
}
if (DEF_IS_Left_Quote(c)) {
Token* t = new Token(Token_Type_Quote, "{");
tokens.push_back(t);
current++;
continue;
}
if (DEF_IS_Right_Quote(c)) {
Token* t = new Token(Token_Type_Quote, "}");
tokens.push_back(t);
current++;
continue;
}
if (DEF_IS_Empty(c)) {
current++;
continue;
}
if (DEF_IS_NEXT_LINE(c)) {
current++;
continue;
}
if (DEF_IS_DIGIT(c)) {
string value;
while (DEF_IS_DIGIT(c) && current < input.size()) {
value += c;
current++;
if (current >= input.size()) break;
c = input.at(current);
}
Token* t = new Token(Token_Type_Number, value);
tokens.push_back(t);
continue;
}
if (DEF_IS_String(c)) {
string value;
c = input.at(++current);
while (!DEF_IS_String(c) && current <= input.size()) {
value += c;
current++;
if (current >= input.size()) break;
c = input[current];
}
c = input[++current];
Token* t = new Token(Token_Type_String, value);
tokens.push_back(t);
continue;
}
if (DEF_IS_MARK(c)) {
string value;
c = input.at(++current);
while (DEF_IS_Empty(c)) {
current++;
if (current >= input.size()) break;
c = input.at(current);
}
while (current <= input.size()) {
value += c;
current++;
if (current >= input.size()) break;
c = input.at(current);
if (DEF_IS_NEXT_LINE(c)) {
current++;
break;
}
}
Token* t = new Token(Token_Type_Comment, value);
tokens.push_back(t);
continue;
}
if (DEF_IS_LETTERS(c)) {
string value;
while (DEF_IS_LETTERS(c) && current <= input.size()) {
value += c;
current++;
if (current >= input.size()) break;
c = input.at(current);
}
Token* t = new Token(Token_Type_Letter, value);
tokens.push_back(t);
continue;
}
throw string(__func__) + " Unknown type: " + c;
}
return tokens.size();
}
AStruct* AKCompiler::walk(TokenptrVector& tokens, uint32_t& current) {
Token* token = tokens[current];
if (Token_Type_Comment == token->type) {
current++;
return new AStruct(Ast_Type_Comment, token->value);
}
if (Token_Type_Letter == token->type) {
current++;
return new AStruct(Ast_Type_Symbol, token->value);
}
if (Token_Type_Number == token->type) {
current++;
return new AStruct(Ast_Type_Number, token->value);
}
if (Token_Type_String == token->type) {
current++;
return new AStruct(Ast_Type_String, token->value);
}
if (Token_Type_Semi == token->type && "(" == token->value) {
ASTptrVector* children = new ASTptrVector;
children->push_back(new AStruct(Ast_Type_Semi, "("));
AStruct* node = new AStruct(Ast_Type_SExpr, "", children);
token = tokens.at(++current);
while (
current < tokens.size() &&
(
Token_Type_Semi != token->type ||
(Token_Type_Semi == token->type && ")" != token->value)
)
) {
AStruct* child_node = walk(tokens, current);
node->children->push_back(child_node);
if (current >= tokens.size()) break;
token = tokens.at(current);
}
current++;
node->children->push_back(new AStruct(Ast_Type_Semi, ")"));
return node;
}
if (Token_Type_Quote == token->type && "{" == token->value) {
ASTptrVector* children = new ASTptrVector;
children->push_back(new AStruct(Ast_type_Quote, "{"));
AStruct* node = new AStruct(Ast_Type_QExpr, "", children);
token = tokens.at(++current);
while (
current < tokens.size() &&
(
Token_Type_Quote != token->type ||
(Token_Type_Quote == token->type && "}" != token->value)
)
) {
AStruct* child_node = walk(tokens, current);
node->children->push_back(child_node);
if (current >= tokens.size()) break;
token = tokens.at(current);
}
current++;
node->children->push_back(new AStruct(Ast_type_Quote, "}"));
return node;
}
cout << __func__ << " Unknown type: " << token->type << endl;
exit(1);
}
AStruct& AKCompiler::aster(TokenptrVector& tokens) {
uint32_t current = 0;
ASTptrVector* children = new ASTptrVector;
AStruct* ast = new AStruct(Ast_Type_Program, ">", children);
while (current < tokens.size()) {
AStruct* child = walk(tokens, current);
ast->children->push_back(child);
}
return *ast;
}
AStruct& AKCompiler::compiler(const string& input) {
TokenptrVector tokens;
try {
tokenizer(input, tokens);
} catch (const string& error) {
AStruct* astruct = new AStruct(Ast_Type_Error);
astruct->error = error;
return *astruct;
}
return aster(tokens);
}
AStruct& AKCompiler::loadfile(const string& filename) {
ifstream ifs(filename, ios::in | ios::binary);
if (!ifs.is_open()) {
AStruct* astruct = new AStruct(Ast_Type_Error);
astruct->error = "文件打开失败: " + filename;
return *astruct;
}
ifs.seekg(0, ios::end);
size_t pos = 0;
pos = ifs.tellg();
ifs.seekg(0, ios::beg);
char buf[pos];
ifs.read((char*)buf, sizeof(buf));
ifs.close();
return compiler(buf);
}
#pragma mark ---
static void print_indent(uint32_t indent = 0) {
for (int i = 0; i < indent; i++) {
cout << " ";
}
}
static string type2name(Ast_Type type) {
switch (type) {
case Ast_Type_Error: return "Error";
case Ast_Type_Program: return ">";
case Ast_Type_Comment: return "Comment";
case Ast_Type_Number: return "Number";
case Ast_Type_String: return "String";
case Ast_Type_Symbol: return "Symbol";
case Ast_Type_SExpr: return "Sexpr";
case Ast_Type_Semi: return "SemiColon";
case Ast_Type_QExpr: return "Qexpr";
case Ast_type_Quote: return "Quote";
default:
return "Unknown Type!" + type;
}
}
#pragma mark ---
void AStruct::expr_print(uint32_t indent) {
print_indent(indent);
cout << type2name(type) << " : " << endl;
for (ASTptrVector::iterator it = children->begin(); it < children->end(); it++) {
AStruct* child = *it;
child->print(indent + 1);
}
}
void AStruct::print(uint32_t indent) {
switch (type) {
case Ast_Type_Program:
case Ast_Type_SExpr:
case Ast_Type_QExpr:
expr_print(indent);
break;
case Ast_Type_Error:
cout << type2name(type) << " : " << error << endl;
break;
default:
print_indent(indent);
cout << type2name(type) << " : " << content << endl;
}
}
void AStruct::exprDeleteNode() {
for (ASTptrVector::iterator it = children->begin(); it != children->end(); it++) {
(*it)->deleteNode();
}
delete children;
}
void AStruct::deleteNode() {
switch (type) {
case Ast_Type_Program:
case Ast_Type_SExpr:
case Ast_Type_QExpr:
exprDeleteNode();
break;
}
delete this;
}