-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParser.h
509 lines (431 loc) · 11.8 KB
/
Parser.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#ifndef __PARSER_H__
#define __PARSER_H__
#include "AST.h"
#include "Lexer.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
using namespace llvm;
static int CurTok;
static std::map<char, int> BinopPrecedence;
static int getNextToken() { return CurTok = gettok(); }
static std::unique_ptr<StatAST> ParseExpression();
std::unique_ptr<StatAST> LogError(const char *Str);
static std::unique_ptr<StatAST> ParseNumberExpr();
static std::unique_ptr<StatAST> ParseParenExpr();
static std::unique_ptr<DecAST> ParseDec();
std::unique_ptr<StatAST> LogError(const char *Str);
std::unique_ptr<PrototypeAST> LogErrorP(const char *Str);
std::unique_ptr<StatAST> LogErrorS(const char *Str);
std::unique_ptr<DecAST> LogErrorD(const char *Str);
static std::unique_ptr<StatAST> ParseStatement();
//解析如下格式的表达式:
// identifer || identifier(expression list)
static std::unique_ptr<StatAST> ParseIdentifierExpr() {
std::string IdName = IdentifierStr;
getNextToken();
//解析成变量表达式
if (CurTok != '(')
return llvm::make_unique<VariableExprAST>(IdName);
// 解析成函数调用表达式
getNextToken();
std::vector<std::unique_ptr<StatAST>> Args;
if (CurTok != ')') {
while (true) {
if (auto Arg = ParseExpression())
Args.push_back(std::move(Arg));
else
return nullptr;
if (CurTok == ')')
break;
if (CurTok != ',')
return LogErrorS("Expected ')' or ',' in argument list");
getNextToken();
}
}
getNextToken();
return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
}
//解析取反表达式
static std::unique_ptr<StatAST> ParseNegExpr() {
getNextToken();
std::unique_ptr<StatAST> Exp = ParseExpression();
if (!Exp)
return nullptr;
return llvm::make_unique<NegExprAST>(std::move(Exp));
}
//解析成 标识符表达式、整数表达式、括号表达式中的一种
static std::unique_ptr<StatAST> ParsePrimary() {
switch (CurTok) {
default:
return LogError("unknown token when expecting an expression");
case VARIABLE:
return ParseIdentifierExpr();
case INTEGER:
return ParseNumberExpr();
case '(':
return ParseParenExpr();
case '-':
return ParseNegExpr();
}
}
//GetTokPrecedence - Get the precedence of the pending binary operator token.
static int GetTokPrecedence() {
if (!isascii(CurTok))
return -1;
// Make sure it's a declared binop.
int TokPrec = BinopPrecedence[CurTok];
if (TokPrec <= 0)
return -1;
return TokPrec;
}
//解析二元表达式
//参数 :
//ExprPrec 左部运算符优先级
//LHS 左部操作数
// 递归得到可以结合的右部,循环得到一个整体二元表达式
static std::unique_ptr<StatAST> ParseBinOpRHS(int ExprPrec,
std::unique_ptr<StatAST> LHS) {
while (true) {
int TokPrec = GetTokPrecedence();
// 当右部没有运算符或右部运算符优先级小于左部运算符优先级时 退出循环和递归
if (TokPrec < ExprPrec)
return LHS;
if(CurTok == '}')
return LHS;
// 保存左部运算符
int BinOp = CurTok;
getNextToken();
// 得到右部表达式
auto RHS = ParsePrimary();
if (!RHS)
return nullptr;
// 如果该右部表达式不与该左部表达式结合 那么递归得到右部表达式
int NextPrec = GetTokPrecedence();
if (TokPrec < NextPrec) {
RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
if (!RHS)
return nullptr;
}
// 将左右部结合成新的左部
LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),
std::move(RHS));
}
}
// 解析得到表达式
static std::unique_ptr<StatAST> ParseExpression() {
auto LHS = ParsePrimary();
if (!LHS)
return nullptr;
return ParseBinOpRHS(0, std::move(LHS));
}
// numberexpr ::= number
static std::unique_ptr<StatAST> ParseNumberExpr() {
auto Result = llvm::make_unique<NumberExprAST>(NumberVal);
//略过数字获取下一个输入
getNextToken();
return std::move(Result);
}
//declaration::=VAR variable_list
static std::unique_ptr<DecAST> ParseDec() {
//eat 'VAR'
getNextToken();
std::vector<std::string> varNames;
//保证至少有一个变量的名字
if (CurTok != VARIABLE) {
return LogErrorD("expected identifier after VAR");
}
while (true)
{
varNames.push_back(IdentifierStr);
//eat VARIABLE
getNextToken();
if (CurTok != ',')
break;
getNextToken();
if (CurTok != VARIABLE) {
return LogErrorD("expected identifier list after VAR");
}
}
auto Body = nullptr;
return llvm::make_unique<DecAST>(std::move(varNames), std::move(Body));
}
//null_statement::=CONTINUE
static std::unique_ptr<StatAST> ParseNullStat() {
getNextToken();
return llvm::make_unique<NullStatAST>();
}
//block::='{' declaration_list statement_list '}'
static std::unique_ptr<StatAST> ParseBlock() {
//存储变量声明语句及其他语句
std::vector<std::unique_ptr<DecAST>> DecList;
std::vector<std::unique_ptr<StatAST>> StatList;
getNextToken(); //eat '{'
if (CurTok == VAR) {
auto varDec = ParseDec();
DecList.push_back(std::move(varDec));
}
while (CurTok != '}') {
if (CurTok == VAR) {
LogErrorS("Can't declare VAR here!");
}
else if (CurTok == '{') {
ParseBlock();
}
else if (CurTok == CONTINUE) {
getNextToken();
}
else {
auto statResult = ParseStatement();
StatList.push_back(std::move(statResult));
}
}
getNextToken(); //eat '}'
return llvm::make_unique<BlockStatAST>(std::move(DecList), std::move(StatList));
}
//prototype ::= VARIABLE '(' parameter_list ')'
static std::unique_ptr<PrototypeAST> ParsePrototype() {
if (CurTok != VARIABLE)
return LogErrorP("Expected function name in prototype");
std::string FnName = IdentifierStr;
getNextToken();
if (CurTok != '(')
return LogErrorP("Expected '(' in prototype");
std::vector<std::string> ArgNames;
getNextToken();
while (CurTok == VARIABLE)
{
ArgNames.push_back(IdentifierStr);
getNextToken();
if (CurTok == ',')
getNextToken();
}
if (CurTok != ')')
return LogErrorP("Expected ')' in prototype");
// success.
getNextToken(); // eat ')'.
return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
}
//function ::= FUNC VARIABLE '(' parameter_lst ')' statement
static std::unique_ptr<FunctionAST> ParseFunc()
{
getNextToken(); // eat FUNC.
auto Proto = ParsePrototype();
if (!Proto)
return nullptr;
auto E = ParseStatement();
if (!E)
return nullptr;
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
}
//解析括号中的表达式
static std::unique_ptr<StatAST> ParseParenExpr() {
// 过滤'('
getNextToken();
auto V = ParseExpression();
if (!V)
return nullptr;
if (CurTok != ')')
return LogError("expected ')'");
// 过滤')'
getNextToken();
return V;
}
//解析 IF Statement
static std::unique_ptr<StatAST> ParseIfStat() {
getNextToken(); // eat the IF.
// condition.
auto Cond = ParseExpression();
if (!Cond)
return nullptr;
if (CurTok != THEN)
return LogErrorS("expected THEN");
getNextToken(); // eat the THEN
auto Then = ParseStatement();
if (!Then)
return nullptr;
std::unique_ptr<StatAST> Else = nullptr;
if (CurTok == ELSE) {
getNextToken();
Else = ParseStatement();
if (!Else)
return nullptr;
}
else if(CurTok != FI)
return LogErrorS("expected FI or ELSE");
getNextToken();
return llvm::make_unique<IfStatAST>(std::move(Cond), std::move(Then),
std::move(Else));
}
//PRINT,能输出变量和函数调用的值
static std::unique_ptr<StatAST> ParsePrintStat()
{
std::string text = "";
std::vector<std::unique_ptr<StatAST>> expr;
getNextToken();//eat PRINT
while(CurTok == VARIABLE || CurTok == TEXT || CurTok == '('
|| CurTok == '-' || CurTok == INTEGER)
{
if(CurTok == TEXT)
{
text += IdentifierStr;
getNextToken();
}
else
{
text += " %d ";
expr.push_back(std::move(ParseExpression()));
}
if(CurTok != ',')
break;
getNextToken(); //eat ','
}
return llvm::make_unique<PrintStatAST>(text, std::move(expr));
}
//解析 RETURN Statement
static std::unique_ptr<StatAST> ParseRetStat() {
getNextToken();
auto Val = ParseExpression();
if (!Val)
return nullptr;
return llvm::make_unique<RetStatAST>(std::move(Val));
}
//解析 赋值语句
static std::unique_ptr<StatAST> ParseAssStat() {
auto a = ParseIdentifierExpr();
VariableExprAST* Name = (VariableExprAST*)a.get();
auto NameV = llvm::make_unique<VariableExprAST>(Name->getName());
if (!Name)
return nullptr;
if (CurTok != ASSIGN_SYMBOL)
return LogErrorS("need := in assignment statment");
getNextToken();
auto Expression = ParseExpression();
if (!Expression)
return nullptr;
return llvm::make_unique<AssStatAST>(std::move(NameV), std::move(Expression));
}
//解析while语句
static std::unique_ptr<StatAST> ParseWhileStat()
{
getNextToken();//eat WHILE
auto E = ParseExpression();
if(!E)
return nullptr;
if(CurTok != DO)
return LogErrorS("expect DO in WHILE statement");
getNextToken();//eat DO
auto S = ParseStatement();
if(!S)
return nullptr;
if(CurTok != DONE)
return LogErrorS("expect DONE in WHILE statement");
getNextToken();//eat DONE
return llvm::make_unique<WhileStatAST>(std::move(E), std::move(S));
}
static std::unique_ptr<StatAST> ParseStatement()
{
switch (CurTok) {
case IF:
return ParseIfStat();
break;
case PRINT:
return ParsePrintStat();
case RETURN:
return ParseRetStat();
case VAR:
return ParseDec();
break;
case '{':
return ParseBlock();
break;
case CONTINUE:
return ParseNullStat();
case WHILE:
return ParseWhileStat();
break;
default:
auto E = ParseAssStat();
return E;
}
}
//解析程序结构
static std::unique_ptr<ProgramAST> ParseProgramAST() {
//接受程序中函数的语法树
std::vector<std::unique_ptr<FunctionAST>> Functions;
//循环解析程序中所有函数
while (CurTok != TOK_EOF) {
auto Func=ParseFunc();
Functions.push_back(std::move(Func));
}
return llvm::make_unique<ProgramAST>(std::move(Functions));
}
//错误信息打印
std::unique_ptr<StatAST> LogError(const char *Str) {
fprintf(stderr, "Error: %s\n", Str);
return nullptr;
}
std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
LogError(Str);
return nullptr;
}
std::unique_ptr<StatAST> LogErrorS(const char *Str) {
fprintf(stderr, "Error: %s\n", Str);
return nullptr;
}
std::unique_ptr<DecAST> LogErrorD(const char *Str) {
fprintf(stderr, "Error: %s\n", Str);
return nullptr;
}
// Top-Level parsing
static void HandleFuncDefinition() {
if (auto FnAST = ParseFunc()) {
FnAST->codegen();
}
else {
// Skip token for error recovery.
getNextToken();
}
}
//声明printf函数
static void DeclarePrintfFunc()
{
std::vector<llvm::Type *> printf_arg_types;
printf_arg_types.push_back(Builder.getInt8Ty()->getPointerTo());
FunctionType *printType = FunctionType::get(
IntegerType::getInt32Ty(TheContext), printf_arg_types, true);
printFunc = llvm::Function::Create(printType, llvm::Function::ExternalLinkage,
llvm::Twine("printf"), TheModule);
printFunc->setCallingConv(llvm::CallingConv::C);
std::vector<std::string> ArgNames;
FunctionProtos["printf"] = std::move(llvm::make_unique<PrototypeAST>("printf", std::move(ArgNames)));
}
//program ::= function_list
static void MainLoop() {
DeclarePrintfFunc();
while(CurTok != TOK_EOF)
HandleFuncDefinition();
if (emitIR)
{
std::string IRstr;
FILE *IRFile = fopen("IRCode.ll", "w");
raw_string_ostream *rawStr = new raw_string_ostream(IRstr);
TheModule->print(*rawStr, nullptr);
fprintf(IRFile, "%s\n", rawStr->str().c_str());
}
if(!emitObj)
{
Function *main = getFunction("main");
if (!main)
printf("main is null");
std::string errStr;
ExecutionEngine *EE = EngineBuilder(std::move(Owner)).setErrorStr(&errStr).create();
if (!EE)
{
errs() << "Failed to construct ExecutionEngine: " << errStr << "\n";
return;
}
std::vector<GenericValue> noarg;
GenericValue gv = EE->runFunction(main, noarg);
}
}
#endif