-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.cpp
299 lines (244 loc) · 8.75 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
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
#include <jetscript/Parser.h>
#include <jetscript/Token.h>
#include <jetscript/UniquePtr.h>
using namespace Jet;
const char* Jet::Operator(TokenType t)
{
if (t == TokenType::Plus)
return "+";
else if (t == TokenType::Minus)
return "-";
else if (t == TokenType::Asterisk)
return "*";
else if (t == TokenType::Slash)
return "/";
else if (t == TokenType::Modulo)
return "%";
else if (t == TokenType::Comma)
return ",";
else if (t == TokenType::Increment)
return "++";
else if (t == TokenType::Decrement)
return "--";
else if (t == TokenType::Equals)
return "==";
else if (t == TokenType::NotEqual)
return "!=";
else if (t == TokenType::Semicolon)
return ";";
else if (t == TokenType::RightBrace)
return "}";
return "";
}
Parser::Parser(Lexer* l)
{
this->lexer = l;
this->filename = l->filename;
this->Register(TokenType::Name, new NameParselet());
this->Register(TokenType::Number, new NumberParselet());
this->Register(TokenType::String, new StringParselet());
this->Register(TokenType::Assign, new AssignParselet());
this->Register(TokenType::LeftParen, new GroupParselet());
this->Register(TokenType::Swap, new SwapParselet());
this->Register(TokenType::Colon, new MemberParselet());
this->Register(TokenType::Dot, new MemberParselet());
this->Register(TokenType::LeftBrace, new ObjectParselet());
//array/index stuffs
this->Register(TokenType::LeftBracket, new ArrayParselet());
this->Register(TokenType::LeftBracket, new IndexParselet());//postfix
//operator assign
this->Register(TokenType::AddAssign, new OperatorAssignParselet());
this->Register(TokenType::SubtractAssign, new OperatorAssignParselet());
this->Register(TokenType::MultiplyAssign, new OperatorAssignParselet());
this->Register(TokenType::DivideAssign, new OperatorAssignParselet());
this->Register(TokenType::AndAssign, new OperatorAssignParselet());
this->Register(TokenType::OrAssign, new OperatorAssignParselet());
this->Register(TokenType::XorAssign, new OperatorAssignParselet());
//prefix stuff
this->Register(TokenType::Increment, new PrefixOperatorParselet(Precedence::PREFIX));
this->Register(TokenType::Decrement, new PrefixOperatorParselet(Precedence::PREFIX));
this->Register(TokenType::Minus, new PrefixOperatorParselet(Precedence::PREFIX));
this->Register(TokenType::BNot, new PrefixOperatorParselet(Precedence::PREFIX));
//postfix stuff
this->Register(TokenType::Increment, new PostfixOperatorParselet(Precedence::POSTFIX));
this->Register(TokenType::Decrement, new PostfixOperatorParselet(Precedence::POSTFIX));
//boolean stuff
this->Register(TokenType::Equals, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
this->Register(TokenType::NotEqual, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
this->Register(TokenType::LessThan, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
this->Register(TokenType::GreaterThan, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
this->Register(TokenType::LessThanEqual, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
this->Register(TokenType::GreaterThanEqual, new BinaryOperatorParselet(Precedence::CONDITIONAL, false));
//logical and/or
this->Register(TokenType::And, new BinaryOperatorParselet(Precedence::LOGICAL, false));
this->Register(TokenType::Or, new BinaryOperatorParselet(Precedence::LOGICAL, false));
//math
this->Register(TokenType::Plus, new BinaryOperatorParselet(Precedence::SUM, false));
this->Register(TokenType::Minus, new BinaryOperatorParselet(Precedence::SUM, false));
this->Register(TokenType::Asterisk, new BinaryOperatorParselet(Precedence::PRODUCT, false));
this->Register(TokenType::Slash, new BinaryOperatorParselet(Precedence::PRODUCT, false));
this->Register(TokenType::Modulo, new BinaryOperatorParselet(Precedence::PRODUCT, false));
this->Register(TokenType::BOr, new BinaryOperatorParselet(Precedence::BINARY, false));//or
this->Register(TokenType::BAnd, new BinaryOperatorParselet(Precedence::BINARY, false));//and
this->Register(TokenType::Xor, new BinaryOperatorParselet(Precedence::BINARY, false));
this->Register(TokenType::LeftShift, new BinaryOperatorParselet(Precedence::BINARY, false));
this->Register(TokenType::RightShift, new BinaryOperatorParselet(Precedence::BINARY, false));
//add parser for includes k
//function stuff
this->Register(TokenType::LeftParen, new CallParselet());
//lambda
this->Register(TokenType::Function, new LambdaParselet());
//this->Register(TokenType::LeftParen, new LambdaParselet());
//statements
this->Register(TokenType::While, new WhileParselet());
this->Register(TokenType::If, new IfParselet());
this->Register(TokenType::Function, new FunctionParselet());
this->Register(TokenType::Ret, new ReturnParselet());
this->Register(TokenType::For, new ForParselet());
this->Register(TokenType::Local, new LocalParselet());
this->Register(TokenType::Break, new BreakParselet());
this->Register(TokenType::Continue, new ContinueParselet());
this->Register(TokenType::Const, new ConstParselet());
this->Register(TokenType::Null, new NullParselet());
this->Register(TokenType::Yield, new YieldParselet());
this->Register(TokenType::Yield, new InlineYieldParselet());
this->Register(TokenType::Resume, new ResumeParselet());
this->Register(TokenType::Resume, new ResumePrefixParselet());
}
Parser::~Parser()
{
for (auto ii: this->mInfixParselets)
delete ii.second;
for (auto ii: this->mPrefixParselets)
delete ii.second;
for (auto ii: this->mStatementParselets)
delete ii.second;
};
Expression* Parser::parseExpression(int precedence)
{
Token token = Consume();
PrefixParselet* prefix = mPrefixParselets[token.getType()];
if (prefix == 0)
{
std::string str = "ParseExpression: No Parser Found for: " + token.getText();
throw CompilerException(this->filename, token.line, str);//printf("Consume: TokenType not as expected!\n");
}
Expression* left = prefix->parse(this, token);
while (precedence < getPrecedence())
{
token = Consume();
InfixParselet* infix = mInfixParselets[token.getType()];
left = infix->parse(this, left, token);
}
return left;
}
Expression* Parser::ParseStatement(bool takeTrailingSemicolon)//call this until out of tokens (hit EOF)
{
Token token = LookAhead();
StatementParselet* statement = mStatementParselets[token.getType()];
if (statement == 0)
{
UniquePtr<Expression*> result(parseExpression());
if (takeTrailingSemicolon)
Consume(TokenType::Semicolon);
return result.Release();
}
token = Consume();
UniquePtr<Expression*> result(statement->parse(this, token));
if (takeTrailingSemicolon && statement->TrailingSemicolon)
Consume(TokenType::Semicolon);
return result.Release();
}
BlockExpression* Parser::parseBlock(bool allowsingle)
{
std::vector<Expression*> statements;
if (allowsingle && !Match(TokenType::LeftBrace))
{
statements.push_back(this->ParseStatement());
return new BlockExpression(std::move(statements));
}
Consume(TokenType::LeftBrace);
while (!Match(TokenType::RightBrace))
{
statements.push_back(this->ParseStatement());
}
Consume(TokenType::RightBrace);
return new BlockExpression(std::move(statements));
}
BlockExpression* Parser::parseAll()
{
std::vector<Expression*> statements;
while (!Match(TokenType::EoF))
{
statements.push_back(this->ParseStatement());
}
auto n = new BlockExpression(std::move(statements));
n->SetParent(0);//go through and setup parents
return n;
}
Token Parser::Consume()
{
auto temp = LookAhead();
mRead.pop_front();
return temp;
}
Token Parser::Consume(TokenType expected)
{
auto temp = LookAhead();
if (temp.getType() != expected)
{
std::string str = "Consume: TokenType not as expected! Expected: " + TokenToString[expected] + " Got: " + temp.text;
throw CompilerException(this->filename, temp.line, str);
}
mRead.pop_front();
return temp;
}
Token Parser::LookAhead(unsigned int num)
{
while (num >= mRead.size())
mRead.push_back(lexer->Next());
int c = 0;
for (auto ii: mRead)
{
if (c++ == num)
return ii;
}
return Token(0, TokenType::EoF, "EOF");
}
bool Parser::Match(TokenType expected)
{
Token token = LookAhead();
if (token.getType() != expected)
{
return false;
}
return true;
}
bool Parser::MatchAndConsume(TokenType expected)
{
Token token = LookAhead();
if (token.getType() != expected)
{
return false;
}
mRead.pop_front();
return true;
}
void Parser::Register(TokenType token, InfixParselet* parselet)
{
this->mInfixParselets[token] = parselet;
}
void Parser::Register(TokenType token, PrefixParselet* parselet)
{
this->mPrefixParselets[token] = parselet;
}
void Parser::Register(TokenType token, StatementParselet* parselet)
{
this->mStatementParselets[token] = parselet;
}
int Parser::getPrecedence() {
InfixParselet* parser = mInfixParselets[LookAhead(0).getType()];
if (parser != 0)
return parser->getPrecedence();
return 0;
}