-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
511 lines (398 loc) · 12.7 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
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
#include <string>
#include <fstream>
#include <streambuf>
#include <vector>
#include <unordered_map>
#include <functional>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "value.cpp"
using namespace asmjit;
union ProgramValue
{
int64_t integer;
bool boolean;
char *str;
std::vector<std::unique_ptr<ProgramData>> *children;
};
enum ProgramType
{
TYPE_INTEGER,
TYPE_BOOLEAN,
TYPE_STR,
TYPE_IDENTIFIER,
TYPE_INDEX,
TYPE_FUNCTION,
TYPE_FUNCTION_DEF,
TYPE_ASSIGNMENT,
TYPE_RETURN,
TYPE_BLOCK,
TYPE_IF,
TYPE_WHILE,
TYPE_EQUALITY,
TYPE_NOTEQUALITY,
TYPE_LT,
TYPE_MULT,
TYPE_DIV,
TYPE_ADD,
TYPE_SUB,
};
struct ProgramData
{
ProgramType type;
ProgramValue value;
};
struct ParserResult
{
bool success;
std::string remainder;
std::unique_ptr<ProgramData> data;
};
ParserResult failure()
{
return {false, "", nullptr};
}
bool is_letter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool is_number(char c)
{
return (c >= '0' && c <= '9');
}
bool is_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
void eat_whitespace(std::string &program)
{
while (!program.empty() && is_whitespace(program.at(0)))
{
program.erase(0, 1);
}
}
char* strdup(const char* str)
{
char* newstr = (char*) malloc( strlen( str) + 1);
if (newstr) {
strcpy( newstr, str);
}
return newstr;
}
ParserResult success(std::string program, ProgramData &data)
{
eat_whitespace(program);
return {true, program, std::make_unique<ProgramData>(data)};
}
std::function<ParserResult(std::string)> any(std::vector<std::function<ParserResult(std::string)>> parsers)
{
return [=](std::string program)->ParserResult {
for (auto it = parsers.begin(); it != parsers.end(); ++it)
{
ParserResult result = (*it)(program);
if (result.success)
{
return result;
}
}
return failure();
};
}
std::function<ParserResult(std::string)> match(std::string match_str)
{
return [=](std::string program)->ParserResult {
if (program.find(match_str) != 0)
return failure();
ProgramData data = { TYPE_STR, { .str = strdup(match_str.c_str()) } };
return success(program.substr(match_str.size()), data);
};
}
std::function<std::vector<ParserResult>(std::string)> seq(std::vector<std::function<ParserResult(std::string)>> parsers)
{
return [=](std::string program)->std::vector<ParserResult> {
std::vector<ParserResult> results;
for (auto it = parsers.begin(); it != parsers.end(); ++it)
{
ParserResult result = (*it)(program);
if (!result.success)
{
return {};
}
else
{
program = result.remainder;
results.push_back(std::move(result));
}
}
return results;
};
}
ParserResult expression(std::string program);
ParserResult index(std::string program, ParserResult result);
ParserResult function(std::string program, ParserResult caller)
{
ParserResult res = match("(")(program);
if (!res.success)
return caller;
program = res.remainder;
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
ParserResult result = expression(program);
while (result.success)
{
children->push_back(std::move(result.data));
program = result.remainder;
result = match(",")(program);
if (!result.success)
break;
result = expression(result.remainder);
}
res = match(")")(program);
if (!res.success)
return caller;
children->insert(children->begin(), std::move(caller.data));
ProgramData data = { TYPE_FUNCTION, { .children = children } };
auto s = success(res.remainder, data);
return index(s.remainder, std::move(s));
}
ParserResult identifier(std::string program);
ParserResult index(std::string program, ParserResult result)
{
if (program.empty() || program.at(0) != '.')
return result;
program.erase(0, 1);
ParserResult iden = identifier(program);
if (!iden.success)
return result;
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(result.data));
children->push_back(std::move(iden.data));
ProgramData data = { TYPE_INDEX, { .children = children } };
return success(iden.remainder, data);
}
ParserResult identifier(std::string program)
{
std::string id;
while (!program.empty() && (is_letter(program.at(0)) || (!id.empty() && program.at(0) == '_')))
{
id += program.at(0);
program.erase(0, 1);
}
if (id.empty())
return failure();
ProgramData data = { TYPE_IDENTIFIER, { .str = strdup(id.c_str()) } };
auto s = success(program, data);
return index(s.remainder, std::move(s));
}
ParserResult number(std::string program)
{
std::string id;
if (!program.empty() && program.at(0) == '-')
{
id += program.at(0);
program.erase(0, 1);
}
while (!program.empty() && is_number(program.at(0)))
{
id += program.at(0);
program.erase(0, 1);
}
if (id.empty() || id == "-")
return failure();
ProgramData data = { TYPE_INTEGER, { .integer = std::atoi(id.c_str()) } };
return success(program, data);
}
ParserResult addop(std::string program);
ParserResult atom(std::string program)
{
ParserResult result = any({number, identifier})(program);
if (result.success)
{
while (true) {
auto len = result.remainder.size();
result = function(result.remainder, std::move(result));
if (len == result.remainder.size())
return result;
}
}
std::vector<ParserResult> results = seq({match("("), addop, match(")")})(program);
if (results.empty())
return failure();
results[1].remainder = results[2].remainder;
return function(results[1].remainder, std::move(results[1]));
}
ParserResult term(std::string program)
{
ParserResult result = atom(program);
if (!result.success)
return failure();
while (true) {
ParserResult op = any({match("*"), match("/")})(result.remainder);
if (!op.success)
break;
ParserResult rh = atom(op.remainder);
if (!rh.success)
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(result.data));
children->push_back(std::move(rh.data));
ProgramData data = { strcmp(op.data->value.str, "*") == 0 ? TYPE_MULT : TYPE_DIV, { .children = children } };
result = success(rh.remainder, data);
}
return result;
}
ParserResult addop(std::string program)
{
ParserResult result = term(program);
if (!result.success)
return failure();
while (true) {
ParserResult op = any({match("+"), match("-")})(result.remainder);
if (!op.success)
break;
ParserResult rh = term(op.remainder);
if (!rh.success)
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(result.data));
children->push_back(std::move(rh.data));
ProgramData data = { strcmp(op.data->value.str, "+") == 0 ? TYPE_ADD : TYPE_SUB, { .children = children } };
result = success(rh.remainder, data);
}
return result;
}
ParserResult noncompare_expression(std::string program)
{
return any({addop})(program);
}
ParserResult equality(std::string program)
{
std::vector<ParserResult> results = seq({noncompare_expression, any({match("=="), match("!="), match("<")}), noncompare_expression})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[0].data));
children->push_back(std::move(results[2].data));
auto comp = results[1].data->value.str;
ProgramType type;
if (strcmp(comp, "==") == 0)
type = TYPE_EQUALITY;
else if (strcmp(comp, "!=") == 0)
type = TYPE_NOTEQUALITY;
else if (strcmp(comp, "<") == 0)
type = TYPE_LT;
ProgramData data = {type, { .children = children } };
return success(results[2].remainder, data);
}
ParserResult function_definition(std::string program);
ParserResult expression(std::string program)
{
return any({function_definition, equality, noncompare_expression})(program);
}
ParserResult assignment(std::string program)
{
std::vector<ParserResult> results = seq({identifier, match("="), expression})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[0].data));
children->push_back(std::move(results[2].data));
ProgramData data = { TYPE_ASSIGNMENT, { .children = children } };
return success(results[2].remainder, data);
}
ParserResult block(std::string program);
ParserResult function_definition(std::string program)
{
std::vector<ParserResult> results = seq({match("function"), match("("), match(")"), match("{"), block, match("}")})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[4].data));
program = results[results.size()-1].remainder;
ProgramData data = { TYPE_FUNCTION_DEF, { .children = children } };
return success(program, data);
}
ParserResult if_statement(std::string program)
{
std::vector<ParserResult> results = seq({match("if"), match("("), expression, match(")"), match("{"), block, match("}")})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[2].data));
children->push_back(std::move(results[5].data));
program = results[results.size()-1].remainder;
// std::vector<ParserResult> results = seq({match("else")})(program);
ParserResult res = match("else")(program);
if (res.success)
{
program = res.remainder;
res = match("{")(program);
if (res.success)
{
program = res.remainder;
ParserResult else_block = block(program);
if (!else_block.success)
return failure();
program = else_block.remainder;
res = match("}")(program);
if (!res.success)
return failure();
program = res.remainder;
children->push_back(std::move(else_block.data));
}
else
{
res = if_statement(program);
if (!res.success)
return failure();
program = res.remainder;
children->push_back(std::move(res.data));
}
}
else
{
children->push_back(nullptr);
}
ProgramData data = { TYPE_IF, { .children = children } };
return success(program, data);
}
ParserResult while_loop(std::string program)
{
std::vector<ParserResult> results = seq({match("while"), match("("), expression, match(")"), match("{"), block, match("}")})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[2].data));
children->push_back(std::move(results[5].data));
ProgramData data = { TYPE_WHILE, { .children = children } };
return success(results[results.size()-1].remainder, data);
}
ParserResult return_statement(std::string program)
{
std::vector<ParserResult> results = seq({match("return"), expression})(program);
if (results.empty())
return failure();
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
children->push_back(std::move(results[1].data));
ProgramData data = { TYPE_RETURN, { .children = children } };
return success(results[results.size()-1].remainder, data);
}
ParserResult statement(std::string program)
{
return any({return_statement, assignment, if_statement, while_loop, expression})(program);
}
ParserResult block(std::string program)
{
auto *children = new std::vector<std::unique_ptr<ProgramData>>();
while (true)
{
ParserResult result = statement(program);
if (!result.success)
break;
children->push_back(std::move(result.data));
program = result.remainder;
}
ProgramData data = { TYPE_BLOCK, { .children = children } };
return success(program, data);
}