这是第一个标题

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#include "parser.h"

std::shared_ptr<Program> Parser::ParseProgram() {

auto program = std::make_shared<Program>();
if (tok.tokenType != TokenType::eof) {
program->node = ParseBlockStmt();
}
Expect(TokenType::eof);
return program;
}

std::shared_ptr<AstNode> Parser::ParseStmt() {
/// null stmt
if (tok.tokenType == TokenType::semi) {
Advance();
return nullptr;
}
/// decl stmt
if (IsTypeName(tok.tokenType)) {
return ParseDeclStmt();
}
/// if stmt
else if(tok.tokenType == TokenType::kw_if) {
return ParseIfStmt();
}
/// block stmt
else if(tok.tokenType == TokenType::l_brace) {
return ParseBlockStmt();
}
/// for stmt
else if (tok.tokenType == TokenType::kw_for) {
return ParseForStmt();
}
/// break stmt
else if (tok.tokenType == TokenType::kw_break) {
return ParseBreakStmt();
}
/// continue stmt
else if (tok.tokenType == TokenType::kw_continue) {
return ParseContinueStmt();
}
/// expr stmt
else {
return ParseExprStmt();
}
}

std::shared_ptr<AstNode> Parser::ParseBlockStmt() {
sema.EnterScope();
auto blockStmt = std::make_shared<BlockStmt>();

Consume(TokenType::l_brace);
while (tok.tokenType != TokenType::r_brace) {
blockStmt->nodeVec.push_back(ParseStmt());
}
Consume(TokenType::r_brace);

sema.ExitScope();

return blockStmt;
}

std::shared_ptr<CType> Parser::ParseDeclSpec() {
if (tok.tokenType == TokenType::kw_int) {
Consume(TokenType::kw_int);
return CType::IntType;
}
GetDiagEngine().Report(llvm::SMLoc::getFromPointer(tok.ptr), diag::err_type);
return nullptr;
}
/**
declarator ::= "*"* direct-declarator
direct-declarator ::= identifier | "(" declarator ")" | direct-declarator "[" assign "]"
*/

/// int a[3][4];
/// baseType -> int
std::shared_ptr<CType> Parser::DirectDeclaratorArraySuffix(std::shared_ptr<CType> baseType) {
if (tok.tokenType != TokenType::l_bracket) {
return baseType;
}
Consume(TokenType::l_bracket);
Expect(TokenType::number);
int count = tok.value;
Consume(TokenType::number);
Consume(TokenType::r_bracket);
return std::make_shared<CArrayType>(DirectDeclaratorArraySuffix(baseType), count);
}

std::shared_ptr<CType> Parser::DirectDeclaratorSuffix(std::shared_ptr<CType> baseType) {
if (tok.tokenType == TokenType::l_bracket) {
return DirectDeclaratorArraySuffix(baseType);
}
/// func
return baseType;
}

std::shared_ptr<AstNode> Parser::DirectDeclarator(std::shared_ptr<CType> baseType) {
std::shared_ptr<AstNode> declNode;
if (tok.tokenType == TokenType::l_parent) {
Token beginTok = tok;
lexer.SaveState();
sema.SetMode(Sema::Mode::Skip);
Consume(TokenType::l_parent);
Declarator(CType::IntType);
Consume(TokenType::r_parent);

baseType = DirectDeclaratorSuffix(baseType);

lexer.RestoreState();
sema.SetMode(Sema::Mode::Normal);
tok = beginTok;

Consume(TokenType::l_parent);
declNode = Declarator(baseType);
Consume(TokenType::r_parent);
DirectDeclaratorSuffix(CType::IntType);
}else if (tok.tokenType == TokenType::identifier){
Token iden = tok;
Consume(TokenType::identifier);
baseType = DirectDeclaratorSuffix(baseType);
declNode = sema.SemaVariableDeclNode(iden, baseType);
}else {
GetDiagEngine().Report(llvm::SMLoc::getFromPointer(tok.ptr), diag::err_expected_ex, "identifier or '('");
}

if (tok.tokenType == TokenType::equal) {
Advance();
VariableDecl*varDecl = llvm::dyn_cast<VariableDecl>(declNode.get());
// varDecl->init = ParseAssignExpr();
std::vector<int> offsetList;
ParseInitializer(varDecl->initValues, declNode->ty, offsetList, false);
}
return declNode;
}

bool Parser::ParseInitializer(std::vector<std::shared_ptr<VariableDecl::InitValue>> &arr, std::shared_ptr<CType> declType, std::vector<int> &offsetList, bool hasLBrace) {
/// {}
if (tok.tokenType == TokenType::r_brace) {
if (!hasLBrace) {
/// 会报错
GetDiagEngine().Report(llvm::SMLoc::getFromPointer(tok.ptr), diag::err_miss, "miss '{'");
}
return true;
}
if (tok.tokenType == TokenType::l_brace) {
Consume(TokenType::l_brace);

if (declType->GetKind() == CType::TY_Array) {
CArrayType *arrType = llvm::dyn_cast<CArrayType>(declType.get());
int size = arrType->GetElementCount();
/// int a[10] = {1,2,3};
for (int i = 0; i < size; ++i) {
if (i > 0 && (tok.tokenType == TokenType::comma)) {
Consume(TokenType::comma);
}
offsetList.push_back(i);
bool end = ParseInitializer(arr, arrType->GetElementType(), offsetList, true);
offsetList.pop_back();
if (end) {
break;
}
}
}
Consume(TokenType::r_brace);
}else {
Token tmp = tok;
// assign
auto node = ParseAssignExpr();

auto initValue = sema.SemaDeclInitValue(declType, node, offsetList, tmp);
arr.push_back(initValue);
}
return false;
}

std::shared_ptr<AstNode> Parser::Declarator(std::shared_ptr<CType> baseType) {
while (tok.tokenType == TokenType::star) {
Consume(TokenType::star);
baseType = std::make_shared<CPointType>(baseType);
}
return DirectDeclarator(baseType);
}

std::shared_ptr<AstNode> Parser::ParseDeclStmt() {

auto baseTy = ParseDeclSpec();

/// int ;
/// 无意义的声明
if (tok.tokenType == TokenType::semi) {
Consume(TokenType::semi);
return nullptr;
}

auto decl = std::make_shared<DeclStmt>();
/// int a,b=3;
/// a,b=3;

int i = 0;
while (tok.tokenType != TokenType::semi) {
if (i++ > 0) {
assert(Consume(TokenType::comma));
}
decl->nodeVec.push_back(Declarator(baseTy));
}

Consume(TokenType::semi);

return decl;
}

std::shared_ptr<AstNode> Parser::ParseExprStmt() {
auto expr = ParseExpr();
Consume(TokenType::semi);
return expr;
}

// if-stmt : "if" "(" expr ")" stmt ( "else" stmt )?
std::shared_ptr<AstNode> Parser::ParseIfStmt() {
Consume(TokenType::kw_if);
Consume(TokenType::l_parent);
auto condExpr = ParseExpr();
Consume(TokenType::r_parent);
auto thenStmt = ParseStmt();
std::shared_ptr<AstNode> elseStmt = nullptr;
/// peek tok is 'else'
if (tok.tokenType == TokenType::kw_else) {
Consume(TokenType::kw_else);
elseStmt = ParseStmt();
}
return sema.SemaIfStmtNode(condExpr, thenStmt, elseStmt);
}

std::shared_ptr<AstNode> Parser::ParseForStmt() {
Consume(TokenType::kw_for);
Consume(TokenType::l_parent);

sema.EnterScope();
auto node = std::make_shared<ForStmt>();

breakNodes.push_back(node);
continueNodes.push_back(node);

std::shared_ptr<AstNode> initNode = nullptr;
std::shared_ptr<AstNode> condNode = nullptr;
std::shared_ptr<AstNode> incNode = nullptr;
std::shared_ptr<AstNode> bodyNode = nullptr;

if (IsTypeName(tok.tokenType)) {
initNode = ParseDeclStmt();
}else {
if (tok.tokenType != TokenType::semi) {
initNode = ParseExpr();
}
Consume(TokenType::semi);
}

if (tok.tokenType != TokenType::semi) {
condNode = ParseExpr();
}
Consume(TokenType::semi);

if (tok.tokenType != TokenType::r_parent) {
incNode = ParseExpr();
}
Consume(TokenType::r_parent);

bodyNode = ParseStmt();

node->initNode = initNode;
node->condNode = condNode;
node->incNode = incNode;
node->bodyNode = bodyNode;

breakNodes.pop_back();
continueNodes.pop_back();

sema.ExitScope();
return node;
}

std::shared_ptr<AstNode> Parser::ParseBreakStmt() {
if (breakNodes.size() == 0) {
GetDiagEngine().Report(llvm::SMLoc::getFromPointer(tok.ptr), diag::err_break_stmt);
}
Consume(TokenType::kw_break);
auto node = std::make_shared<BreakStmt>();
node->target = breakNodes.back();
Consume(TokenType::semi);
return node;
}

std::shared_ptr<AstNode> Parser::ParseContinueStmt() {
if (breakNodes.size() == 0) {
GetDiagEngine().Report(llvm::SMLoc::getFromPointer(tok.ptr), diag::err_continue_stmt);
}
Consume(TokenType::kw_continue);
auto node = std::make_shared<ContinueStmt>();
node->target = continueNodes.back();
Consume(TokenType::semi);
return node;
}

/// 左结合
/// expr : assign-expr | add-expr
/// assign-expr: identifier "=" expr
/// add-expr : mult-expr (("+" | "-") mult-expr)*

/// LLn
std::shared_ptr<AstNode> Parser::ParseExpr() {
auto left = ParseAssignExpr();
while (tok.tokenType == TokenType::comma) {
Consume(TokenType::comma);
auto right = ParseAssignExpr();
left = sema.SemaBinaryExprNode(left, right, BinaryOp::comma);
}
return left;
}

std::shared_ptr<AstNode> Parser::ParseEqualExpr() {
auto left = ParseRelationalExpr();
while (tok.tokenType == TokenType::equal_equal || tok.tokenType == TokenType::not_equal) {
BinaryOp op;
if (tok.tokenType == TokenType::equal_equal) {
op = BinaryOp::equal;
}else {
op = BinaryOp::not_equal;
}
Advance();
left = sema.SemaBinaryExprNode(left, ParseRelationalExpr(), op);
}
return left;
}

std::shared_ptr<AstNode> Parser::ParseRelationalExpr() {
auto left = ParseShiftExpr();
while (tok.tokenType == TokenType::less || tok.tokenType == TokenType::less_equal ||
tok.tokenType == TokenType::greater || tok.tokenType == TokenType::greater_equal) {
BinaryOp op;
if (tok.tokenType == TokenType::less) {
op = BinaryOp::less;
}else if (tok.tokenType == TokenType::less_equal){
op = BinaryOp::less_equal;
}else if (tok.tokenType == TokenType::greater){
op = BinaryOp::greater;
}else if (tok.tokenType == TokenType::greater_equal){
op = BinaryOp::greater_equal;
}
Advance();
left = sema.SemaBinaryExprNode(left, ParseShiftExpr(), op);
}
return left;
}

std::shared_ptr<AstNode> Parser::ParseAddExpr() {
/// add-expr : mult-expr (("+" | "-") mult-expr)*
auto left = ParseMultiExpr();
while (tok.tokenType == TokenType::plus || tok.tokenType == TokenType::minus) {
BinaryOp op;
if (tok.tokenType == TokenType::plus) {
op = BinaryOp::add;
}else {
op = BinaryOp::sub;
}
Advance();
left = sema.SemaBinaryExprNode(left, ParseMultiExpr(), op);
}
return left;
}

/// 左结合
std::shared_ptr<AstNode> Parser::ParseMultiExpr() {
auto left = ParseUnaryExpr();
while (tok.tokenType == TokenType::star ||
tok.tokenType == TokenType::slash ||
tok.tokenType == TokenType::percent) {
BinaryOp op;
if (tok.tokenType == TokenType::star) {
op = BinaryOp::mul;
}else if (tok.tokenType == TokenType::slash) {
op = BinaryOp::div;
}
else {
op = BinaryOp::mod;
}
Advance();
left = sema.SemaBinaryExprNode(left, ParseUnaryExpr(), op);
}
return left;
}

std::shared_ptr<AstNode> Parser::ParseUnaryExpr() {
if (!IsUnaryOperator()) {
return ParsePostFixExpr();
}

/// check sizeof
if (tok.tokenType == TokenType::kw_sizeof) {
bool isTypeName = false;
Consume(TokenType::kw_sizeof);

if (tok.tokenType == TokenType::l_parent) {
lexer.SaveState();
Token nextTok;
lexer.NextToken(nextTok);
if (IsTypeName(nextTok.tokenType)) {
isTypeName = true;
}
lexer.RestoreState();
}

if (isTypeName) {

Consume(TokenType::l_parent);
auto type = ParseType();
Consume(TokenType::r_parent);
return sema.SemaSizeofExprNode(nullptr, type);
}else {
auto node = ParseUnaryExpr();
return sema.SemaSizeofExprNode(node, nullptr);
}
}

UnaryOp op;
switch (tok.tokenType)
{
case TokenType::plus:
op = UnaryOp::positive;
break;
case TokenType::minus:
op = UnaryOp::negative;
break;
case TokenType::star:
op = UnaryOp::deref;
break;
case TokenType::amp:
op = UnaryOp::addr;
break;
case TokenType::plus_plus:
op = UnaryOp::inc;
break;
case TokenType::minus_minus:
op = UnaryOp::dec;
break;
case TokenType::exclaim:
op = UnaryOp::logical_not;
break;
case TokenType::tilde:
op = UnaryOp::bitwise_not;
break;
default:
break;
}
Advance();
Token tmp = tok;
return sema.SemaUnaryExprNode(ParseUnaryExpr(), op, tmp);
}

/// a[3][5]
std::shared_ptr<AstNode> Parser::ParsePostFixExpr() {
auto left = ParsePrimary();
for (;;) {
if (tok.tokenType == TokenType::plus_plus) {
left = sema.SemaPostIncExprNode(left, tok);
Consume(TokenType::plus_plus);
continue;
}
if (tok.tokenType == TokenType::minus_minus) {
left = sema.SemaPostDecExprNode(left, tok);
Consume(TokenType::minus_minus);
continue;
}
if (tok.tokenType == TokenType::l_bracket) {
Token tmp = tok;
Consume(TokenType::l_bracket);
auto node = ParseExpr();
Consume(TokenType::r_bracket);
left = sema.SemaPostSubscriptNode(left, node, tmp);
continue;
}

break;
}
return left;
}

std::shared_ptr<AstNode> Parser::ParsePrimary() {
if (tok.tokenType == TokenType::l_parent) {
Advance();
auto expr = ParseExpr();
assert (Expect(TokenType::r_parent));
Advance();
return expr;
} else if (tok.tokenType == TokenType::identifier) {
auto expr = sema.SemaVariableAccessNode(tok);
Advance();
return expr;
}
else {
Expect(TokenType::number);
auto factor = sema.SemaNumberExprNode(tok, tok.ty);
Advance();
return factor;
}
}

/// a = b = 3;
std::shared_ptr<AstNode> Parser::ParseAssignExpr() {
auto left = ParseConditionalExpr();

if (!IsAssignOperator()) {
return left;
}

BinaryOp op;
switch (tok.tokenType)
{
case TokenType::equal:
op = BinaryOp::assign;
break;
case TokenType::plus_equal:
op = BinaryOp::add_assign;
break;
case TokenType::minus_equal:
op = BinaryOp::sub_assign;
break;
case TokenType::star_equal:
op = BinaryOp::mul_assign;
break;
case TokenType::slash_equal:
op = BinaryOp::div_assign;
break;
case TokenType::percent_equal:
op = BinaryOp::mod_assign;
break;
case TokenType::pipe_equal:
op = BinaryOp::bitwise_or_assign;
break;
case TokenType::amp_equal:
op = BinaryOp::bitwise_and_assign;
break;
case TokenType::caret_equal:
op = BinaryOp::bitwise_xor_assign;
break;
case TokenType::less_less_equal:
op = BinaryOp::left_shift_assign;
break;
case TokenType::greater_greater_equal:
op = BinaryOp::right_shift_assign;
break;
default:
break;
}
Advance();
return sema.SemaBinaryExprNode(left, ParseAssignExpr(), op);
}


std::shared_ptr<AstNode> Parser::ParseConditionalExpr() {
auto left = ParseLogOrExpr();
if (tok.tokenType != TokenType::question) {
return left;
}
Token tmp = tok;
Consume(TokenType::question);
auto then = ParseExpr();
Consume(TokenType::colon);
auto els = ParseConditionalExpr();
return sema.SemaThreeExprNode(left, then, els, tmp);
}

std::shared_ptr<AstNode> Parser::ParseLogOrExpr() {
auto left = ParseLogAndExpr();
while (tok.tokenType == TokenType::pipepipe) {
BinaryOp op = BinaryOp::logical_or;
Advance();
left = sema.SemaBinaryExprNode(left, ParseLogAndExpr(), op);
}
return left;
}

std::shared_ptr<AstNode> Parser::ParseLogAndExpr() {
auto left = ParseBitOrExpr();
while (tok.tokenType == TokenType::ampamp) {
BinaryOp op = BinaryOp::logical_and;
Advance();
left = sema.SemaBinaryExprNode(left, ParseBitOrExpr(), op);
}
return left;
}
std::shared_ptr<AstNode> Parser::ParseBitOrExpr() {
auto left = ParseBitXorExpr();
while (tok.tokenType == TokenType::pipe) {
BinaryOp op = BinaryOp::bitwise_or;
Advance();
left = sema.SemaBinaryExprNode(left, ParseBitXorExpr(), op);
}
return left;
}
std::shared_ptr<AstNode> Parser::ParseBitXorExpr() {
auto left = ParseBitAndExpr();
while (tok.tokenType == TokenType::caret) {
BinaryOp op = BinaryOp::bitwise_xor;
Advance();
left = sema.SemaBinaryExprNode(left, ParseBitAndExpr(), op);
}
return left;
}
std::shared_ptr<AstNode> Parser::ParseBitAndExpr() {
auto left = ParseEqualExpr();
while (tok.tokenType == TokenType::amp) {
BinaryOp op = BinaryOp::bitwise_and;
Advance();
left = sema.SemaBinaryExprNode(left, ParseEqualExpr(), op);
}
return left;
}
std::shared_ptr<AstNode> Parser::ParseShiftExpr() {
auto left = ParseAddExpr();
while (tok.tokenType == TokenType::less_less || tok.tokenType == TokenType::greater_greater) {
BinaryOp op;
if (tok.tokenType == TokenType::less_less) {
op = BinaryOp::left_shift;
}else {
op = BinaryOp::right_shift;
}
Advance();
left = sema.SemaBinaryExprNode(left, ParseAddExpr(), op);
}
return left;
}

bool Parser::IsAssignOperator() {
return tok.tokenType == TokenType::equal
|| tok.tokenType == TokenType::plus_equal
|| tok.tokenType == TokenType::minus_equal
|| tok.tokenType == TokenType::star_equal
|| tok.tokenType == TokenType::slash_equal
|| tok.tokenType == TokenType::percent_equal
|| tok.tokenType == TokenType::pipe_equal
|| tok.tokenType == TokenType::amp_equal
|| tok.tokenType == TokenType::caret_equal
|| tok.tokenType == TokenType::less_less_equal
|| tok.tokenType == TokenType::greater_greater_equal;
}


bool Parser::IsUnaryOperator() {
return tok.tokenType == TokenType::plus_plus
|| tok.tokenType == TokenType::minus_minus
|| tok.tokenType == TokenType::plus
|| tok.tokenType == TokenType::minus
|| tok.tokenType == TokenType::star
|| tok.tokenType == TokenType::amp
|| tok.tokenType == TokenType::exclaim
|| tok.tokenType == TokenType::tilde
|| tok.tokenType == TokenType::kw_sizeof;
}

/// @brief sizeof (int* [5][6]);
std::shared_ptr<CType> Parser::ParseType() {
std::shared_ptr<CType> baseType = ParseDeclSpec();
assert(baseType);

while (tok.tokenType == TokenType::star) {
baseType = std::make_shared<CPointType>(baseType);
Consume(TokenType::star);
}

if (tok.tokenType == TokenType::l_bracket) {
baseType = DirectDeclaratorArraySuffix(baseType);
}

return baseType;
}

bool Parser::IsTypeName(TokenType tokenType) {
if (tokenType == TokenType::kw_int) {
return true;
}
return false;
}

bool Parser::Expect(TokenType tokenType) {
if (tok.tokenType == tokenType) {
return true;
}
GetDiagEngine().Report(
llvm::SMLoc::getFromPointer(tok.ptr),
diag::err_expected,
Token::GetSpellingText(tokenType),
llvm::StringRef(tok.ptr, tok.len));
return false;
}

bool Parser::Consume(TokenType tokenType) {
if (Expect(tokenType)) {
Advance();
return true;
}
return false;
}

void Parser::Advance() {
lexer.NextToken(tok);
}

这是第二个标题

你好啊

这是2.1

我很好