-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParselets.cpp
426 lines (341 loc) · 12.6 KB
/
Parselets.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
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
#include <jetscript/Parselets.h>
#include <jetscript/Expressions.h>
#include <jetscript/Parser.h>
#include <jetscript/UniquePtr.h>
#include <string>
using namespace Jet;
Expression* NameParselet::parse(Parser* parser, Token token)
{
if (parser->MatchAndConsume(TokenType::LeftBracket))
{
//array index
UniquePtr<Expression*> index = parser->parseExpression();
parser->Consume(TokenType::RightBracket);
return new IndexExpression(new NameExpression(token.text), index.Release(), token);
}
else
return new NameExpression(token.text);
}
Expression* AssignParselet::parse(Parser* parser, Expression* left, Token token)
{
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);
if (dynamic_cast<IStorableExpression*>(left) == 0)
{
delete right;
throw CompilerException(parser->filename, token.line, "AssignParselet: Left hand side must be a storable location!");
}
return new AssignExpression(left, right);
}
Expression* OperatorAssignParselet::parse(Parser* parser, Expression* left, Token token)
{
if (dynamic_cast<IStorableExpression*>(left) == 0)
throw CompilerException(parser->filename, token.line, "OperatorAssignParselet: Left hand side must be a storable location!");
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);
return new OperatorAssignExpression(token, left, right);
}
Expression* SwapParselet::parse(Parser* parser, Expression* left, Token token)
{
if (dynamic_cast<IStorableExpression*>(left) == 0)
throw CompilerException(parser->filename, token.line, "SwapParselet: Left hand side must be a storable location!");
UniquePtr<Expression*> right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);
if (dynamic_cast<IStorableExpression*>((Expression*)right) == 0)
throw CompilerException(parser->filename, token.line, "SwapParselet: Right hand side must be a storable location!");
return new SwapExpression(left, right.Release());
}
Expression* PrefixOperatorParselet::parse(Parser* parser, Token token)
{
Expression* right = parser->parseExpression(precedence);
if (right == 0)
throw CompilerException(parser->filename, token.line, "PrefixOperatorParselet: Right hand side missing!");
return new PrefixExpression(token, right);
}
Expression* BinaryOperatorParselet::parse(Parser* parser, Expression* left, Token token)
{
Expression* right = parser->parseExpression(precedence - (isRight ? 1 : 0));
if (right == 0)
throw CompilerException(parser->filename, token.line, "BinaryOperatorParselet: Right hand side missing!");
return new OperatorExpression(left, token, right);
}
Expression* GroupParselet::parse(Parser* parser, Token token)
{
UniquePtr<Expression*> exp = parser->parseExpression();
parser->Consume(TokenType::RightParen);
return exp.Release();
}
Expression* WhileParselet::parse(Parser* parser, Token token)
{
parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> condition = parser->parseExpression();
parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->parseBlock());
return new WhileExpression(token, condition.Release(), block);
}
Expression* ForParselet::parse(Parser* parser, Token token)
{
parser->Consume(TokenType::LeftParen);
if (parser->LookAhead().type == TokenType::Local)
{
if (parser->LookAhead(1).type == TokenType::Name)
{
Token n = parser->LookAhead(2);
if (n.type == TokenType::Name && n.text == "in")
{
//ok its a foreach loop
parser->Consume();
auto name = parser->Consume();
parser->Consume();
UniquePtr<Expression*> container = parser->parseExpression();
parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->parseBlock());
return new ForEachExpression(name, container.Release(), block);
}
}
}
UniquePtr<Expression*> initial = parser->ParseStatement(true);
UniquePtr<Expression*> condition = parser->ParseStatement(true);
UniquePtr<Expression*> increment = parser->parseExpression();
parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->parseBlock());
return new ForExpression(token, initial.Release(), condition.Release(), increment.Release(), block);
}
Expression* IfParselet::parse(Parser* parser, Token token)
{
std::vector<Branch*> branches;
//take parens
parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> ifcondition = parser->parseExpression();
parser->Consume(TokenType::RightParen);
BlockExpression* ifblock = parser->parseBlock(true);
branches.push_back(new Branch(ifblock, ifcondition.Release()));
Branch* Else = 0;
while(true)
{
//look for elses
if (parser->MatchAndConsume(TokenType::ElseIf))
{
//keep going
parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> condition = parser->parseExpression();
parser->Consume(TokenType::RightParen);
BlockExpression* block = parser->parseBlock(true);
branches.push_back(new Branch(block, condition.Release()));
}
else if (parser->MatchAndConsume(TokenType::Else))
{
//its an else
BlockExpression* block = parser->parseBlock(true);
Else = new Branch(block, 0);
break;
}
else
break;//nothing else
}
return new IfExpression(token, std::move(branches), Else);
}
Expression* FunctionParselet::parse(Parser* parser, Token token)
{
auto name = new NameExpression(parser->Consume(TokenType::Name).getText());
auto arguments = new std::vector<Expression*>;
NameExpression* varargs = 0;
parser->Consume(TokenType::LeftParen);
if (!parser->MatchAndConsume(TokenType::RightParen))
{
do
{
Token name = parser->Consume();
if (name.type == TokenType::Name)
{
arguments->push_back(new NameExpression(name.text));
}
else if (name.type == TokenType::Ellipses)
{
varargs = new NameExpression(parser->Consume(TokenType::Name).getText());
break;//this is end of parsing arguments
}
else
{
std::string str = "Consume: TokenType not as expected! Expected Name or Ellises Got: " + name.text;
throw CompilerException(parser->filename, name.line, str);
}
}
while(parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::RightParen);
}
auto block = new ScopeExpression(parser->parseBlock());
return new FunctionExpression(token, name, arguments, block, varargs);
}
Expression* LambdaParselet::parse(Parser* parser, Token token)
{
parser->Consume(TokenType::LeftParen);
NameExpression* varargs = 0;
auto arguments = new std::vector<Expression*>;
if (parser->LookAhead().type != TokenType::RightParen)
{
do
{
Token name = parser->Consume();
if (name.type == TokenType::Name)
{
arguments->push_back(new NameExpression(name.getText()));
}
else if (name.type == TokenType::Ellipses)
{
varargs = new NameExpression(parser->Consume(TokenType::Name).getText());
break;//this is end of parsing arguments
}
else
{
std::string str = "Consume: TokenType not as expected! Expected Name or Ellises Got: " + name.text;
throw CompilerException(parser->filename, name.line, str);
}
}
while(parser->MatchAndConsume(TokenType::Comma));
}
parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->parseBlock());
return new FunctionExpression(token, 0, arguments, block, varargs);
}
Expression* CallParselet::parse(Parser* parser, Expression* left, Token token)
{
UniquePtr<std::vector<Expression*>*> arguments = new std::vector<Expression*>;
if (!parser->MatchAndConsume(TokenType::RightParen))
{
do
{
arguments->push_back(parser->parseExpression(Precedence::ASSIGNMENT));
}
while( parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::RightParen);
}
return new CallExpression(token, left, arguments.Release());
}
Expression* ReturnParselet::parse(Parser* parser, Token token)
{
Expression* right = 0;
if (parser->Match(TokenType::Semicolon) == false)
right = parser->parseExpression(Precedence::ASSIGNMENT);
return new ReturnExpression(token, right);
}
Expression* LocalParselet::parse(Parser* parser, Token token)
{
UniquePtr<std::vector<Token>*> names = new std::vector<Token>;
do
{
Token name = parser->Consume(TokenType::Name);
names->push_back(name);
}
while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::Assign);//its possible this wont be here and it may just be a mentioning, but no assignment
//handle multiple comma expressions
UniquePtr<std::vector<Expression*>*> rights = new std::vector<Expression*>;
do
{
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);
rights->push_back(right);
}
while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::Semicolon);
return new LocalExpression(names.Release(), rights.Release());
}
Expression* ConstParselet::parse(Parser* parser, Token token)
{
throw CompilerException("", 0, "Const keyword not implemented!");
std::vector<Token>* names = new std::vector<Token>;
do
{
Token name = parser->Consume(TokenType::Name);
names->push_back(name);
}
while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::Assign);//its possible this wont be here and it may just be a mentioning, but no assignment
//do somethign with multiple comma expressions
std::vector<Expression*>* rights = new std::vector<Expression*>;
do
{
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);
rights->push_back(right);
}
while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::Semicolon);
//do stuff with this and store and what not
//need to add this variable to this's block expression
return new LocalExpression(names, rights);
}
Expression* ArrayParselet::parse(Parser* parser, Token token)
{
std::vector<Expression*> inits;// = new std::vector<Expression*>;
while(parser->LookAhead().getType() != TokenType::RightBracket)
{
Expression* e = parser->parseExpression(2);
inits.push_back(e);
if (!parser->MatchAndConsume(TokenType::Comma))//check if more
break;//we are done
}
parser->Consume(TokenType::RightBracket);
return new ArrayExpression(std::move(inits));
}
Expression* IndexParselet::parse(Parser* parser, Expression* left, Token token)
{
UniquePtr<Expression*> index = parser->parseExpression();
parser->Consume(TokenType::RightBracket);
return new IndexExpression(left, index.Release(), token);
}
Expression* MemberParselet::parse(Parser* parser, Expression* left, Token token)
{
//this is for const members
Expression* member = parser->parseExpression(Precedence::CALL);
UniquePtr<NameExpression*> name = dynamic_cast<NameExpression*>(member);
if (name == 0)
{
delete member; delete left;
throw CompilerException(parser->filename, token.line, "Cannot access member name that is not a string");
}
auto ret = new IndexExpression(left, new StringExpression(name->GetName()), token);
return ret;
}
Expression* ObjectParselet::parse(Parser* parser, Token token)
{
if (parser->MatchAndConsume(TokenType::RightBrace))
{
//we are done, return null object
return new ObjectExpression();
}
//parse initial values
std::vector<std::pair<std::string, Expression*>>* inits = new std::vector<std::pair<std::string, Expression*>>;
while(parser->LookAhead().type == TokenType::Name || parser->LookAhead().type == TokenType::String || parser->LookAhead().type == TokenType::Number)
{
Token name = parser->Consume();
parser->Consume(TokenType::Assign);
//parse the data;
Expression* e = parser->parseExpression(Precedence::LOGICAL);
inits->push_back(std::pair<std::string, Expression*>(name.text, e));
if (!parser->MatchAndConsume(TokenType::Comma))//is there more to parse?
break;//we are done
}
parser->Consume(TokenType::RightBrace);//end part
return new ObjectExpression(inits);
};
Expression* YieldParselet::parse(Parser* parser, Token token)
{
Expression* right = 0;
if (parser->Match(TokenType::Semicolon) == false)
right = parser->parseExpression(Precedence::ASSIGNMENT);
return new YieldExpression(token, right);
}
Expression* InlineYieldParselet::parse(Parser* parser, Token token)
{
Expression* right = 0;
if (parser->Match(TokenType::Semicolon) == false && parser->LookAhead().type != TokenType::RightParen)
right = parser->parseExpression(Precedence::ASSIGNMENT);
return new YieldExpression(token, right);
}
Expression* ResumeParselet::parse(Parser* parser, Token token)
{
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT);
return new ResumeExpression(token, right);
}
Expression* ResumePrefixParselet::parse(Parser* parser, Token token)
{
Expression* right = parser->parseExpression(Precedence::ASSIGNMENT);
return new ResumeExpression(token, right);
}