-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.cpp
170 lines (133 loc) · 4.47 KB
/
parser.cpp
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
#include "parser.hpp"
#include "common.hpp"
#include "scanner.hpp"
#include "llvm/ADT/STLExtras.h"
#include <map>
static std::map<char, int> BinopPrecedence;
static int GetTokPrecedence() {
if (!isascii(getToken()))
return -1;
int TokPrec = BinopPrecedence[getToken()];
if (TokPrec <= 0) return -1;
return TokPrec;
}
static std::unique_ptr<ExprAST> ParseExpression();
static std::unique_ptr<ExprAST> ParseNumberExpr() {
auto Result = llvm::make_unique<NumberExprAST>(getNumber());
getNextToken();
return std::move(Result);
}
static std::unique_ptr<ExprAST> ParseParenExpr() {
getNextToken();
auto V = ParseExpression();
if (!V)
return nullptr;
if (getToken() != ')')
return LogError("expected ')'");
getNextToken();
return V;
}
static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
std::string IdName = getIdentifier();
getNextToken();
if (getToken() != '(')
return llvm::make_unique<VariableExprAST>(IdName);
getNextToken();
std::vector<std::unique_ptr<ExprAST>> Args;
if (getToken() != ')') {
while(1) {
if (auto Arg = ParseExpression())
Args.push_back(std::move(Arg));
else
return nullptr;
if (getToken() == ')')
break;
if (getToken() != ',')
return LogError("Expected ')' or ',' in argument list");
getNextToken();
}
}
getNextToken();
return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
}
static std::unique_ptr<ExprAST> ParsePrimary() {
switch (getToken()) {
default:
return LogError("unknown token when expecting an expression");
case tok_identifier:
return ParseIdentifierExpr();
case tok_number:
return ParseNumberExpr();
case '(':
return ParseParenExpr();
}
}
// ExprPrec : the minimal operator precedence that the function is allowed to eat.
static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
std::unique_ptr<ExprAST> LHS) {
while(1) {
int TokPrec = GetTokPrecedence();
// if TokPrec is less than the minimal precedence, we are done.
if (TokPrec < ExprPrec)
return LHS;
// otherwise, we consume it
int BinOp = getToken();
getNextToken();
auto RHS = ParsePrimary();
if (!RHS)
return nullptr;
// if next op has HIGHER precedence, handle them first
int NextPrec = GetTokPrecedence();
if (TokPrec < NextPrec) {
// TokPrec + 1 to make sure it ONLY handle the HIGHER-precedence-op.
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<ExprAST> ParseExpression() {
auto LHS = ParsePrimary();
if (!LHS)
return nullptr;
return ParseBinOpRHS(0, std::move(LHS));
}
static std::unique_ptr<PrototypeAST> ParsePrototype() {
if (getToken() != tok_identifier)
return LogErrorP("Expected funciotn name in prototype");
std::string FnName = getIdentifier();
getNextToken();
if (getToken() != '(')
return LogErrorP("Expected '(' in prototype");
std::vector<std::string> ArgNames;
while(getNextToken() == tok_identifier)
ArgNames.push_back(getIdentifier());
if (getToken() != ')')
return LogErrorP("Expected ')' in prototype");
getNextToken();
return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
}
std::unique_ptr<FunctionAST> ParseDefinition() {
getNextToken();
auto Proto = ParsePrototype();
if (!Proto) return nullptr;
if (auto E = ParseExpression())
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
return nullptr;
}
std::unique_ptr<PrototypeAST> ParseExtern() {
getNextToken();
return ParsePrototype();
}
std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
if (auto E = ParseExpression()) {
auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
}
return nullptr;
}
void setBinopPrecedence(char op, int prec) {
BinopPrecedence[op] = prec;
}