-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
782 lines (691 loc) · 17.9 KB
/
Copy pathparser.cpp
File metadata and controls
782 lines (691 loc) · 17.9 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <cmath>
#include "parser.h"
///////////////////////////////////////////////////////////////////////////////
// 1 PARSER
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 1.1 LEXER
///////////////////////////////////////////////////////////////////////////////
void InitTokenizer(TokenizerState *state, const char *code)
{
state->begin = code;
state->end = code + strlen(code);
state->it = code;
state->buffer_pos = 0;
state->state = 0;
}
enum {
STATE_COMMENT,
STATE_IDENTIFIER,
STATE_STRING,
STATE_INTEGER,
STATE_FLOAT,
};
int ReadWhitespace(const char **it, const char *end)
{
while (*it != end && **it != '\0' &&
(**it == ' ' || **it == '\t' || **it == '\r' || **it == '\n'))
{
(*it)++;
}
return *it == end || **it == '\0' ? -1 : 0;
}
void CopyChar(TokenizerState *state)
{
state->buffer[state->buffer_pos++] = *state->it++;
}
void CopyCharAs(TokenizerState *state, char c)
{
state->buffer[state->buffer_pos++] = c;
state->it++;
}
bool IsIdentifierChar(char c)
{
if ((c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c == '_'))
return true;
return false;
}
bool IsNumeric(char c)
{
if (c >= '0' && c <= '9')
return true;
return false;
}
bool IsNumericFloat(char c)
{
if (c >= '0' && c <= '9')
return true;
if ((c == '.') || (c == 'e'))
return true;
return false;
}
struct
{
const char *str;
int type;
} specializations[] =
{
{ "declare", TOK_DECLARE },
{ "if", TOK_IF },
{ "for", TOK_FOR },
};
bool SpecalizeIdentifier(Token *t)
{
for (int i = 0; i < sizeof(specializations)/sizeof(specializations[0]); i++)
{
if (strcmp(t->string, specializations[i].str) == 0)
{
t->type = specializations[i].type;
return true;
}
}
return false;
}
Token GetToken(TokenizerState *state)
{
Token t;
t.type = TOK_EOF;
char c = '\0';
while (true)
{
if (ReadWhitespace(&state->it, state->end) != 0)
{
t.pos = (int)(state->it - state->begin);
t.type = TOK_EOF;
return t;
}
t.pos = (int)(state->it - state->begin);
c = *state->it;
//printf("pos %ld, next char %c\n", state->it - state->begin, c);
switch (c)
{
case '"': state->state = STATE_STRING; break;
case '=': t.type = TOK_EQUALS; state->it++; return t;
case ',': t.type = TOK_COMMA; state->it++; return t;
case ':': t.type = TOK_COLON; state->it++; return t;
case ';': t.type = TOK_SEMICOLON; state->it++; return t;
case '(': t.type = TOK_LEFT_PAREN; state->it++; return t;
case ')': t.type = TOK_RIGHT_PAREN; state->it++; return t;
case '[': t.type = TOK_LEFT_BRACKET; state->it++; return t;
case ']': t.type = TOK_RIGHT_BRACKET; state->it++; return t;
case '{': t.type = TOK_LEFT_BRACE; state->it++; return t;
case '}': t.type = TOK_RIGHT_BRACE; state->it++; return t;
case '+': t.type = TOK_PLUS; state->it++; return t;
case '-': t.type = TOK_MINUS; state->it++; return t;
case '*': t.type = TOK_TIMES; state->it++; return t;
case '/': if ((state->it + 1 < state->end) && (state->it[1] == '/'))
{
state->state = STATE_COMMENT;
state->it += 2;
break;
}
else
{
t.type = TOK_DIV; state->it++; return t;
}
default:
if (c >= '0' && c <= '9')
{
state->state = STATE_INTEGER;
}
else
{
state->state = STATE_IDENTIFIER;
}
break;
}
state->buffer_pos = 0;
rescan:
switch (state->state)
{
case STATE_COMMENT:
while (state->it != state->end && *state->it != '\r' && *state->it != '\n')
state->it++;
break;
case STATE_IDENTIFIER:
while (state->it != state->end && IsIdentifierChar(*state->it))
{
CopyChar(state);
}
state->buffer[state->buffer_pos] = '\0';
t.type = TOK_IDENTIFIER;
strcpy(t.string, state->buffer);
SpecalizeIdentifier(&t);
return t;
case STATE_STRING:
state->it++;
while (state->it != state->end)
{
char c = *state->it;
if (c == '\"')
{
state->it++;
break;
}
else if (c == '\\')
{
state->it++;
if (state->it == state->end)
{
t.type = TOK_ERROR;
strcpy(t.string, "Error while parsing string\n");
t.pos = (int)(state->it - 1 - state->begin);
return t;
}
switch (*state->it)
{
case '"':
case '\\':
CopyChar(state);
break;
case 'n':
CopyCharAs(state, '\n');
break;
case 'r':
CopyCharAs(state, '\r');
break;
case 't':
CopyCharAs(state, '\t');
break;
case 'x':
break;
}
}
else
{
CopyChar(state);
}
}
state->buffer[state->buffer_pos] = '\0';
t.type = TOK_STRING;
strcpy(t.string, state->buffer);
return t;
break;
case STATE_INTEGER:
while (state->it != state->end && IsNumeric(*state->it))
{
CopyChar(state);
}
if (state->it != state->end && *state->it == '.')
{
state->state = STATE_FLOAT;
goto rescan;
}
state->buffer[state->buffer_pos] = '\0';
t.type = TOK_INTEGER;
t.integer = strtoul(state->buffer, NULL, 10);
return t;
case STATE_FLOAT:
while (state->it != state->end && IsNumericFloat(*state->it))
{
CopyChar(state);
}
state->buffer[state->buffer_pos] = '\0';
t.type = TOK_FLOAT;
t.floatval = strtod(state->buffer, NULL);
return t;
}
}
}
Token PeekToken(TokenizerState *state)
{
return state->t1;
}
Token PopToken(TokenizerState *state)
{
Token ret = state->t1;
state->t1 = GetToken(state);
return ret;
}
///////////////////////////////////////////////////////////////////////////////
// 1.2 PARSER
///////////////////////////////////////////////////////////////////////////////
void InitParser(TokenizerState *state)
{
state->t1 = GetToken(state);
state->paren_count = 0;
}
AstNode *MakeAstNode(int type, Token t)
{
AstNode *node = new AstNode;
memset(node, 0, sizeof(AstNode));
node->type = type;
node->token = t;
return node;
}
AstNode *AddAstNode(AstNode **first, AstNode **last, AstNode *node)
{
if (*first == NULL)
{
*first = *last = node;
}
else
{
(*last)->next = node;
*last = node;
}
return node;
}
void CodeError(const char *code_begin, const char *code_end, int pos, const char *fmt, ...)
{
char tmp[1024];
va_list list;
va_start(list, fmt);
vsnprintf(tmp, sizeof(tmp), fmt, list);
va_end(list);
int line = 1;
const char *line_start = code_begin;
const char *it = code_begin;
while (it != code_end && (it - code_begin) < pos)
{
if (*it == '\n')
{
line++;
line_start = it + 1;
}
it++;
}
int column = (int)(it - line_start);
const char *line_end = line_start;
while (it != code_end)
{
if ((*it == '\n') || (*it == '\r'))
{
line_end = it;
break;
}
it++;
}
fprintf(stderr, "Line %d, column %d: %s", line, column + 1, tmp);
fprintf(stderr, "%.*s\n", (int)(line_end - line_start), line_start);
for (int i = 0; i < column; i++)
fprintf(stderr, " ");
fprintf(stderr, "^\n");
}
int GetPrecedence(int type)
{
switch (type)
{
case TOK_PLUS:
case TOK_MINUS:
return 1;
case TOK_TIMES:
case TOK_DIV:
return 10;
default:
return -1;
}
}
void ParseList(AstNode **node, TokenizerState *state, int separator, int end_token);
void ParseBody(AstNode **node, TokenizerState *state);
AstNode *ParseExpression(TokenizerState *state, int end_token, int end_token2, int current_precedence)
{
AstNode *expr = NULL;
while (true)
{
Token t1 = PeekToken(state);
if (t1.type == end_token)
return expr;
if (t1.type == end_token2)
return expr;
if (t1.type == TOK_EOF)
return expr;
if (GetPrecedence(t1.type) >= 0 && current_precedence > GetPrecedence(t1.type))
{
if (t1.type != TOK_MINUS)
return expr;
}
PopToken(state);
// printf("token %d\n", t1.type);
switch (t1.type)
{
case TOK_EQUALS:
{
if (!expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
if (expr->type == NODE_IDENTIFIER)
{
AstNode *ret = MakeAstNode(NODE_ASSIGN, t1);
ret->args = expr;
expr = ret;
expr->args2 = ParseExpression(state, TOK_SEMICOLON, -1, 0);
}
else if (expr->type == NODE_FUNCALL)
{
for (AstNode *node = expr->args; node; node = node->next)
{
if (node->type != NODE_IDENTIFIER)
{
CodeError(state->begin, state->end, node->token.pos, "Error: In function definition argument must be a variable\n");
return NULL;
}
}
expr->type = NODE_FUNCTION_DEFINITION;
expr->function_name = expr->token;
Token t3 = PeekToken(state);
if (t3.type == TOK_LEFT_BRACE)
{
PopToken(state);
/* function definition */
ParseBody(&expr->args2, state);
}
else
{
expr->args2 = ParseExpression(state, TOK_SEMICOLON, -1, 0);
}
return expr;
}
}
break;
case TOK_MINUS:
if (!expr) /* allow single argument negation */
{
AstNode *node1 = ParseExpression(state, end_token, end_token2, 15);
if (!node1)
{
CodeError(state->begin, state->end, t1.pos, "Error: Missing argument\n");
return NULL;
}
expr = MakeAstNode(NODE_NEGATE, t1);
expr->args = node1;
break;
}
case TOK_PLUS:
case TOK_TIMES:
case TOK_DIV:
{
if (!expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
AstNode *node1 = expr;
AstNode *node2 = ParseExpression(state, end_token, end_token2, GetPrecedence(t1.type));
if (!node2)
{
CodeError(state->begin, state->end, t1.pos, "Error: Failed to parse second operand\n");
return NULL;
}
expr = MakeAstNode(t1.type == TOK_PLUS ? NODE_ADD :
t1.type == TOK_MINUS ? NODE_SUBTRACT : t1.type == TOK_TIMES ? NODE_MULTIPLY : NODE_DIVIDE, t1);
expr->args = node1;
node1->next = node2;
}
break;
case TOK_LEFT_PAREN:
state->paren_count++;
expr = ParseExpression(state, TOK_RIGHT_PAREN, -1, 0);
if (!expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Empty parenthesis\n");
return NULL;
}
else
{
PopToken(state);
state->paren_count--;
}
break;
case TOK_INTEGER:
{
if (expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
expr = MakeAstNode(NODE_INTEGER, t1);
}
break;
case TOK_FLOAT:
{
if (expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
expr = MakeAstNode(NODE_FLOAT, t1);
}
break;
case TOK_STRING:
{
if (expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
expr = MakeAstNode(NODE_STRING, t1);
}
break;
case TOK_DECLARE:
{
if (expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
Token t2 = PeekToken(state);
if (t2.type == TOK_LEFT_PAREN)
{
PopToken(state);
expr = MakeAstNode(NODE_DECLARE, t1);
ParseList(&expr->args, state, TOK_COMMA, TOK_RIGHT_PAREN);
}
else
{
return NULL;
}
}
break;
case TOK_IDENTIFIER:
{
Token t2 = PeekToken(state);
if (t2.type == TOK_LEFT_PAREN)
{
PopToken(state);
expr = MakeAstNode(NODE_FUNCALL, t1);
ParseList(&expr->args, state, TOK_COMMA, TOK_RIGHT_PAREN);
}
else
{
if (expr)
{
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
}
expr = MakeAstNode(NODE_IDENTIFIER, t1);
}
}
break;
default:
CodeError(state->begin, state->end, t1.pos, "Error: Unexpected token\n");
return NULL;
break;
}
}
return expr;
}
void ParseList(AstNode **node, TokenizerState *state, int separator, int end_token)
{
AstNode *last = NULL;
while (true)
{
Token t1 = PeekToken(state);
if (t1.type == end_token)
{
PopToken(state);
return;
}
else if (t1.type == TOK_COMMA)
{
PopToken(state);
}
else if (t1.type == TOK_EOF)
{
CodeError(state->begin, state->end, t1.pos, "Error: Missing token\n");
return;
}
else
{
AstNode *expr = ParseExpression(state, separator, end_token, 0);
if (expr)
{
AddAstNode(node, &last, expr);
}
}
}
}
void ParseBody(AstNode **node, TokenizerState *state)
{
AstNode *last = NULL;
while (true)
{
Token t1 = PeekToken(state);
if (t1.type == TOK_RIGHT_BRACE)
{
PopToken(state);
return;
}
else if (t1.type == TOK_SEMICOLON)
{
PopToken(state);
}
else if (t1.type == TOK_EOF)
{
CodeError(state->begin, state->end, t1.pos, "Error: Missing token\n");
return;
}
else
{
AstNode *expr = ParseExpression(state, TOK_SEMICOLON, TOK_RIGHT_BRACE, 0);
if (expr)
{
AddAstNode(node, &last, expr);
}
}
}
}
AstNode *ParseToplevel(TokenizerState *state)
{
AstNode *first = NULL;
AstNode *last = NULL;
while (true)
{
AstNode *expr = ParseExpression(state, TOK_SEMICOLON, -1, 0);
if (expr)
{
AddAstNode(&first, &last, expr);
}
Token t1 = PopToken(state);
if (t1.type == TOK_EOF)
return first;
}
return first;
}
const char *NodeToString(int type)
{
switch (type)
{
case NODE_FUNCALL: return "FUNCALL";
case NODE_INTEGER: return "INTEGER";
case NODE_FLOAT: return "FLOAT";
case NODE_STRING: return "STRING";
case NODE_ADD: return "ADD";
case NODE_MULTIPLY: return "MULTIPLY";
case NODE_SUBTRACT: return "SUBTRACT";
case NODE_DIVIDE: return "DIVIDE";
case NODE_NEGATE: return "NEGATE";
case NODE_IDENTIFIER: return "IDENTIFIER";
case NODE_DECLARE: return "DECLARE";
case NODE_FUNCTION_DEFINITION: return "FUNCTION_DEFINITION";
case NODE_ASSIGN: return "ASSIGN";
}
return "?";
}
void PrintAstNode1(AstNode *node, int level)
{
while (node != NULL)
{
for (int i = 0; i < level; i++)
printf(" ");
printf("Node: %d (%s)", node->type, NodeToString(node->type));
switch (node->type)
{
case NODE_INTEGER:
printf(" value %lu\n", node->token.integer);
break;
case NODE_FLOAT:
printf(" value %f\n", node->token.floatval);
break;
case NODE_STRING:
printf(" value \"%s\"\n", node->token.string);
break;
case NODE_FUNCALL:
printf(" %s\n", node->token.string);
if (node->args)
PrintAstNode1(node->args, level + 1);
break;
case NODE_IDENTIFIER:
printf(" value %s\n", node->token.string);
break;
case NODE_ASSIGN:
printf(" var:\n");
if (node->args)
PrintAstNode1(node->args, level + 1);
printf(" value:\n");
if (node->args2)
PrintAstNode1(node->args2, level + 1);
break;
case NODE_DECLARE:
printf(" declaration name \"%s\" args:\n", node->function_name.string);
if (node->args)
PrintAstNode1(node->args, level + 1);
break;
case NODE_FUNCTION_DEFINITION:
printf(" function name \"%s\" args:\n", node->function_name.string);
if (node->args)
PrintAstNode1(node->args, level + 1);
for (int i = 0; i < level; i++)
printf(" ");
printf(" function body:\n");
if (node->args2)
PrintAstNode1(node->args2, level + 1);
break;
default:
printf("\n");
if (node->args)
PrintAstNode1(node->args, level + 1);
break;
}
node = node->next;
}
}
void PrintAstNode(AstNode *node)
{
printf("AstNode %p:\n", node);
if (node)
{
PrintAstNode1(node, 1);
}
}
void FreeAstNode(AstNode *node)
{
while (node != NULL)
{
if (node->args)
FreeAstNode(node->args);
if (node->args2)
FreeAstNode(node->args2);
AstNode *next = node->next;
delete node;
node = next;
}
}