-
Notifications
You must be signed in to change notification settings - Fork 4
/
jtc.cpp
2788 lines (2523 loc) · 95.5 KB
/
jtc.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
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <array>
#include <functional>
#include <unordered_map>
#include <stdexcept>
#include <memory>
// utility
template<class T>
bool isint(T x) {
return x == std::trunc(x);
}
std::string unescape(const std::string &str) {
std::string result;
for(auto i = str.begin();i!=str.end();++i) {
if(*i == '\\') {
switch(*(++i)) {
case 'a': result += '\a'; break;
case 'b': result += '\b'; break;
case 'f': result += '\f'; break;
case 'n': result += '\n'; break;
case 'r': result += '\r'; break;
case 't': result += '\t'; break;
case 'v': result += '\v'; break;
case '\'': result += '\''; break;
case '\"': result += '\"'; break;
}
} else {
result += *i;
}
}
return result;
}
enum TokenType {
NUMBER, IDENTIFIER, STRING, LEFT_PAREN, RIGHT_PAREN, LEFT_BRACKET,
RIGHT_BRACKET, LEFT_BRACE, RIGHT_BRACE, PLUS, DASH, STAR, PERCENT,
SLASH, PLUS_EQUAL, DASH_EQUAL, STAR_EQUAL, PERCENT_EQUAL,
SLASH_EQUAL,NOT, COMMA, SEMICOLON, DOT, COLON, EQUAL, EQ_OP,
NOT_EQUAL, GREATER, GREATER_EQUAL, LESS, LESS_EQUAL, LOGICAL_AND,
LOGICAL_OR, IF, ELSE, WHILE, FOR, RETURN, BREAK, CONTINUE, FUNCTION,
EXPRESSION, STATEMENT, ROOT, NIL, EVAL, PARSE_STATEMENT,
PARSE_EXPRESSION, GLOBAL, LOCAL, PRINT, TYPE, SIZE
};
const std::unordered_map<std::string, TokenType> keywords = {
{"if", IF}, {"else", ELSE}, {"while", WHILE}, {"for", FOR},
{"return", RETURN}, {"break", BREAK}, {"continue", CONTINUE},
{"function", FUNCTION}, {"nil", NIL}, {"expression", EXPRESSION},
{"statement", STATEMENT}, {"eval", EVAL},
{"parse_statement", PARSE_STATEMENT},
{"parse_expression", PARSE_EXPRESSION}, {"global", GLOBAL},
{"local", LOCAL}, {"root", ROOT}, {"type", TYPE}, {"size", SIZE}
};
template<class Iter>
struct Token {
Token(TokenType type_, Iter begin_, Iter end_)
: type(type_), begin(begin_), end(end_) { }
const TokenType type;
const Iter begin, end;
};
// helper to find line & column position on demand
template<class Iter>
std::pair<int, int> line_column(Iter begin, Iter pos) {
int line = 1, column = 0;
for(Iter i = begin;i!=pos;++i) {
if(*i == '\n'){
++line;
column = 0;
}
++column;
}
return std::make_pair(line, column);
}
template<class Iter>
std::shared_ptr<std::vector<Token<Iter>>> tokenize(Iter begin, Iter end)
{
auto tokens_ptr = std::make_shared<std::vector<Token<Iter>>>();
std::vector<Token<Iter>> &tokens = *tokens_ptr;
Iter i = begin;
while(i!=end)
{
Iter start = i;
switch(*i)
{
case ' ': case '\t': case '\n': // ignore whitespaces
++i; break;
case '(':
tokens.emplace_back(LEFT_PAREN, i, i+1); ++i; break;
case ')':
tokens.emplace_back(RIGHT_PAREN, i, i+1); ++i; break;
case '[':
tokens.emplace_back(LEFT_BRACKET, i, i+1); ++i; break;
case ']':
tokens.emplace_back(RIGHT_BRACKET, i, i+1); ++i; break;
case '{':
tokens.emplace_back(LEFT_BRACE, i, i+1); ++i; break;
case '}':
tokens.emplace_back(RIGHT_BRACE, i, i+1); ++i; break;
case ',':
tokens.emplace_back(COMMA, i, i+1); ++i; break;
case '.':
tokens.emplace_back(DOT, i, i+1); ++i; break;
case ';':
tokens.emplace_back(SEMICOLON, i, i+1); ++i; break;
case ':':
tokens.emplace_back(COLON, i, i+1); ++i; break;
case '+':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(PLUS_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(PLUS, i, i+1); ++i;
}
break;
case '-':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(DASH_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(DASH, i, i+1); ++i;
}
break;
case '*':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(STAR_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(STAR, i, i+1); ++i;
}
break;
case '%':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(PERCENT_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(PERCENT, i, i+1); ++i;
}
break;
case '/':
if(i+1 != end && *(i+1) == '/') {
++i; while(i!=end && *i != '\n') ++i; ++i;
} else if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(SLASH_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(SLASH, i, i+1); ++i;
}
break;
case '<':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(LESS_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(LESS, i, i+1); ++i;
}
break;
case '>':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(GREATER_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(GREATER, i, i+1); ++i;
}
break;
case '=':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(EQ_OP, i, i+2); i+=2;
} else {
tokens.emplace_back(EQUAL, i, i+1); ++i;
}
break;
case '!':
if(i+1 != end && *(i+1) == '='){
tokens.emplace_back(NOT_EQUAL, i, i+2); i+=2;
} else {
tokens.emplace_back(NOT, i, i+1); i+=2;
}
break;
case '&':
if(i+1 != end && *(i+1) == '&'){
tokens.emplace_back(LOGICAL_AND, i, i+2); i+=2;
} else {
auto lc = line_column(begin, i);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": unrecognized symbol: '" + *i + "'");
}
break;
case '|':
if(i+1 != end && *(i+1) == '|'){
tokens.emplace_back(LOGICAL_OR, i, i+2); i+=2;
} else {
auto lc = line_column(begin, i);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": unrecognized symbol: '" + *i + "'");
}
break;
case '"':
++i;
while(i!=end && !(*i == '"' && *(i-1) != '\\')) ++i;
++i;
tokens.emplace_back(STRING, start, i);
break;
default:
if(std::isdigit(*i)) {
while(i!=end && std::isdigit(*i)) ++i;
if(i!=end && *i=='.') {
++i;
while(i!=end && std::isdigit(*i)) ++i;
}
if(i!=end && *i=='e') {
++i;
if(i!=end && *i=='-') ++i;
while(i!=end && std::isdigit(*i)) ++i;
}
tokens.emplace_back(NUMBER, start, i);
break;
} else if(std::isalpha(*i) || *i == '_') {
++i;
while(i!=end && (std::isalnum(*i) || *i == '_')) ++i;
auto kw = keywords.find(std::string(start, i));
if(kw == keywords.end())
tokens.emplace_back(IDENTIFIER, start, i);
else
tokens.emplace_back(kw->second, start, i);
break;
} else {
auto lc = line_column(begin, i);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": unrecognized symbol: '" + *i + "'");
}
}
}
return tokens_ptr;
}
// Abstract Syntax Tree node types
template<class Iter, class R, class F>
struct ASTNode : public std::enable_shared_from_this<ASTNode<Iter,R,F>>{
Iter begin, end;
virtual R accept(F&) = 0;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t) = 0;
virtual size_t size() const = 0;
virtual void inject_dependencies() = 0;
std::shared_ptr<std::string> source;
std::shared_ptr<std::vector<Token<std::string::iterator>>> tokens;
std::weak_ptr<ASTNode<Iter,R,F>> root;
};
template<class Iter, class R, class F>
struct Variadic : public ASTNode<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::vector<std::shared_ptr<ASTNode<Iter,R,F>>> children;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t i) { return children[i]; }
virtual size_t size() const { return children.size(); }
virtual void inject_dependencies() {
for(auto& child : children) {
child->root = this->root;
child->source = this->source;
child->tokens = this->tokens;
child->inject_dependencies();
}
}
};
template<class Iter, class R, class F>
struct Ternary : public ASTNode<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::array<std::shared_ptr<ASTNode<Iter,R,F>>, 3> children;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t i) { return children[i]; }
virtual size_t size() const { return children.size(); }
virtual void inject_dependencies() {
for(auto& child : children) {
child->root = this->root;
child->source = this->source;
child->tokens = this->tokens;
child->inject_dependencies();
}
}
};
template<class Iter, class R, class F>
struct Binary : public ASTNode<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::array<std::shared_ptr<ASTNode<Iter,R,F>>, 2> children;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t i) { return children[i]; }
virtual size_t size() const { return children.size(); }
virtual void inject_dependencies() {
for(auto& child : children) {
child->root = this->root;
child->source = this->source;
child->tokens = this->tokens;
child->inject_dependencies();
}
}
};
template<class Iter, class R, class F>
struct Unary : public ASTNode<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::array<std::shared_ptr<ASTNode<Iter,R,F>>, 1> children;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t i) { return children[i]; }
virtual size_t size() const { return children.size(); }
virtual void inject_dependencies() {
for(auto& child : children) {
child->root = this->root;
child->source = this->source;
child->tokens = this->tokens;
child->inject_dependencies();
}
}
};
template<class Iter, class R, class F>
struct Nullary : public ASTNode<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::array<std::shared_ptr<ASTNode<Iter,R,F>>, 0> children;
virtual std::shared_ptr<ASTNode<Iter,R,F>>& operator[](size_t i) { return children[i]; }
virtual size_t size() const { return children.size(); }
virtual void inject_dependencies() {
for(auto& child : children) {
child->root = this->root;
child->source = this->source;
child->tokens = this->tokens;
child->inject_dependencies();
}
}
};
template<class Iter, class R, class F>
struct Variable : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
R name;
};
template<class Iter, class R, class F>
struct GlobalVariable : public Variable<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LocalVariable : public Variable<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct FieldName : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
R name;
};
template<class Iter, class R, class F>
struct Nop : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
double value;
};
template<class Iter, class R, class F>
struct Constant : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
R value;
};
template<class Iter, class R, class F>
struct Nil : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct String : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
R value;
};
template<class Iter, class R, class F>
struct FieldAccess : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct FieldAssignment : public Ternary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct AddFieldAssignment : public FieldAssignment<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct SubFieldAssignment : public FieldAssignment<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct MulFieldAssignment : public FieldAssignment<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct DivFieldAssignment : public FieldAssignment<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ModFieldAssignment : public FieldAssignment<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Call : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct MemberCall : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct EvalStatement : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Eval : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ParseStatement : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ParseExpression : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct UnaryExpression : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct UnaryPlusExpression : public UnaryExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct UnaryMinusExpression : public UnaryExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct UnaryNotExpression : public UnaryExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct MultiplicativeExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct MultiplyExpression : public MultiplicativeExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct DivideExpression : public MultiplicativeExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ModuloExpression : public MultiplicativeExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct AdditiveExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct AddExpression : public AdditiveExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct SubtractExpression : public AdditiveExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct RelationalExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LessExpression : public RelationalExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LessEqualExpression : public RelationalExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct GreaterExpression : public RelationalExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct GreaterEqualExpression : public RelationalExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct EqualityExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct EqualExpression : public EqualityExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct NotEqualExpression : public EqualityExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LogicalAndExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LogicalOrExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct AssignmentExpression : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct GlobalAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct LocalAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct AddAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct SubAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct MulAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct DivAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ModAssignmentExpression : public AssignmentExpression<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct InitializerAssignmentExpression : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Block : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct TableInitializer : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ArrayInitializer : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct IdentifierList : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Function : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Tree : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Root : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Type : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct Size : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct StatementList : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct IfStatement : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct WhileStatement : public Binary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ForStatement : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ForEachStatement : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ReturnStatement : public Variadic<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ContinueStatement : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct BreakStatement : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct ExpressionStatement : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class Iter, class R, class F>
struct BuiltinFunction : public Nullary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
std::function<void(std::vector<R>&)> function;
};
template<class Iter, class R, class F>
struct Parens : public Unary<Iter,R,F> {
virtual R accept(F &f) { return f(*this); }
};
template<class R, class F>
class Parser {
public:
typedef std::vector<Token<std::string::iterator>>::iterator Iter;
typedef std::shared_ptr<ASTNode<Iter,R,F>> return_t;
template<class StrIter>
return_t operator()(StrIter sbegin, StrIter send)
{
auto source = std::make_shared<std::string>(sbegin, send);
auto tokens = tokenize(source->begin(), source->end());
this->begin = tokens->begin();
this->end = tokens->end();
i = this->begin;
accepted = this->end;
auto node = statement_list();
node->source = source;
node->tokens = tokens;
node->root = node;
node->inject_dependencies();
return node;
}
template<class StrIter>
return_t parse_expression(StrIter sbegin, StrIter send)
{
auto source = std::make_shared<std::string>(sbegin, send);
auto tokens = tokenize(source->begin(), source->end());
this->begin = tokens->begin();
this->end = tokens->end();
i = this->begin;
accepted = this->end;
auto node = expression();
node->source = source;
node->tokens = tokens;
node->root = node;
node->inject_dependencies();
return node;
}
private:
Iter i, accepted;
Iter begin;
Iter end;
void advance() {
if(i==end) {
Iter last = end-1;
auto lc = line_column(begin->begin, last->end);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": unexpected end of input at");
} else {
++i;
}
}
void rewind(Iter pos) {
i = pos;
}
bool peek(TokenType t, int amount = 0) {
return i+amount != end && (i+amount)->type == t;
}
bool accept(TokenType t) {
if(i != end && i->type == t) {
accepted = i; advance();
return true;
} else {
return false;
}
}
bool end_of_input() {
return i == end;
}
bool expect(TokenType t) {
if(accept(t)) {
return true;
} else {
i = std::min(i, end-1);
auto lc = line_column(begin->begin, i->begin);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": unexpected token '" + std::string(i->begin, i->end) + "'");
return false;
}
}
void expect(const std::string &expected) {
i = std::min(i, end-1);
auto lc = line_column(begin->begin, i->begin);
throw std::runtime_error(std::to_string(lc.first)+":"+std::to_string(lc.second)+": expected " + expected+ " but got '" + std::string(i->begin, i->end) + "'");
}
return_t variable() {
expect(IDENTIFIER);
std::shared_ptr<Variable<Iter,R,F>> node = std::make_shared<Variable<Iter,R,F>>();
node->begin = accepted;
node->end = accepted+1;
node->name = R(accepted->begin, accepted->end);
return std::move(node);
}
return_t field_name() {
expect(IDENTIFIER);
std::shared_ptr<FieldName<Iter,R,F>> node = std::make_shared<FieldName<Iter,R,F>>();
node->begin = accepted;
node->end = accepted+1;
node->name = R(accepted->begin, accepted->end);
return std::move(node);
}
return_t identifier_list() {
std::shared_ptr<IdentifierList<Iter,R,F>> node = std::make_shared<IdentifierList<Iter,R,F>>();
node->begin = i;
expect(LEFT_PAREN);
while(!accept(RIGHT_PAREN)) {
node->children.push_back(field_name());
if(!peek(RIGHT_PAREN))
accept(COMMA);
}
node->end = accepted+1;
return std::move(node);
}
return_t function() {
std::shared_ptr<Function<Iter,R,F>> node = std::make_shared<Function<Iter,R,F>>();
node->begin = i;
expect(FUNCTION);
node->children[0] = identifier_list();
node->children[1] = statement();
node->end = accepted+1;
return std::move(node);
}
return_t parse_expression() {
std::shared_ptr<ParseExpression<Iter,R,F>> node = std::make_shared<ParseExpression<Iter,R,F>>();
node->begin = i;
expect(PARSE_EXPRESSION);
expect(LEFT_PAREN);
node->children[0] = expression();
expect(RIGHT_PAREN);
node->end = accepted+1;
return std::move(node);
}
return_t parse_statement() {
std::shared_ptr<ParseStatement<Iter,R,F>> node = std::make_shared<ParseStatement<Iter,R,F>>();
node->begin = i;
expect(PARSE_STATEMENT);
expect(LEFT_PAREN);
node->children[0] = expression();
expect(RIGHT_PAREN);
node->end = accepted+1;
return std::move(node);
}
return_t tree() {
std::shared_ptr<Tree<Iter,R,F>> node = std::make_shared<Tree<Iter,R,F>>();
node->begin = i;
if(accept(EXPRESSION)) {
expect(LEFT_PAREN);
node->children[0] = expression();
expect(RIGHT_PAREN);
} else {
expect(STATEMENT);
expect(LEFT_PAREN);
node->children[0] = statement();
expect(RIGHT_PAREN);
}
node->end = accepted+1;
return std::move(node);
}
return_t root() {
std::shared_ptr<Root<Iter,R,F>> node = std::make_shared<Root<Iter,R,F>>();
node->begin = i;
expect(ROOT);
if(accept(LEFT_PAREN)) {
node->children.push_back(expression());
expect(RIGHT_PAREN);
}
node->end = accepted+1;
return std::move(node);
}
return_t eval() {
std::shared_ptr<Eval<Iter,R,F>> node = std::make_shared<Eval<Iter,R,F>>();
node->begin = i;
expect(EVAL);
expect(LEFT_PAREN);
node->children[0] = unary_expression();
expect(RIGHT_PAREN);
node->end = accepted+1;
return std::move(node);
}
return_t type() {
std::shared_ptr<Type<Iter,R,F>> node = std::make_shared<Type<Iter,R,F>>();
node->begin = i;
expect(TYPE);
if(accept(LEFT_PAREN)) {
node->children[0] = expression();
expect(RIGHT_PAREN);
}
node->end = accepted+1;
return std::move(node);
}
return_t size() {
std::shared_ptr<Size<Iter,R,F>> node = std::make_shared<Size<Iter,R,F>>();
node->begin = i;
expect(SIZE);
if(accept(LEFT_PAREN)) {
node->children[0] = expression();
expect(RIGHT_PAREN);
}
node->end = accepted+1;
return std::move(node);
}
return_t table_initializer() {
std::shared_ptr<TableInitializer<Iter,R,F>> node = std::make_shared<TableInitializer<Iter,R,F>>();
node->begin = i;
expect(LEFT_BRACE);
while(!accept(RIGHT_BRACE)) {
node->children.push_back(initializer_assignment_expression());
if(!peek(RIGHT_BRACE))
accept(COMMA);
}
node->end = accepted+1;
return std::move(node);
}
return_t array_initializer() {
std::shared_ptr<ArrayInitializer<Iter,R,F>> node = std::make_shared<ArrayInitializer<Iter,R,F>>();
node->begin = i;
expect(LEFT_BRACKET);
while(!accept(RIGHT_BRACKET)) {
node->children.push_back(logical_or_expression());
if(!peek(RIGHT_BRACKET))
accept(COMMA);
}
node->end = accepted+1;
return std::move(node);
}
return_t primary_expression() {
if (peek(IDENTIFIER) || peek(GLOBAL) || peek(LOCAL)) {
return variable();
} else if (accept(NUMBER)) {
std::shared_ptr<Constant<Iter,R,F>> node = std::make_shared<Constant<Iter,R,F>>();
node->begin = accepted;
node->end = accepted+1;
node->value = std::stod(std::string(accepted->begin, accepted->end));
return std::move(node);
} else if (accept(NIL)) {
std::shared_ptr<Nil<Iter,R,F>> node = std::make_shared<Nil<Iter,R,F>>();
node->begin = accepted;
node->end = accepted+1;
return std::move(node);
} else if (accept(STRING)) {
std::shared_ptr<String<Iter,R,F>> node = std::make_shared<String<Iter,R,F>>();
node->begin = accepted;
node->end = accepted+1;
node->value = R(unescape(std::string(accepted->begin+1, accepted->end-1)));
return std::move(node);
} else if (accept(LEFT_PAREN)) {
std::shared_ptr<Parens<Iter,R,F>> node = std::make_shared<Parens<Iter,R,F>>();
node->begin = accepted;
node->children[0] = expression();
expect(RIGHT_PAREN);
node->end = accepted+1;
return std::move(node);
} else if (peek(LEFT_BRACE)) {
return table_initializer();
} else if (peek(LEFT_BRACKET)) {
return array_initializer();
} else if (peek(EXPRESSION) || peek(STATEMENT)) {
return tree();
} else if (peek(ROOT)) {
return root();
} else if (peek(TYPE)) {
return type();
} else if (peek(EVAL)) {
return eval();
} else if (peek(SIZE)) {
return size();
} else if (peek(PARSE_EXPRESSION)) {
return parse_expression();
} else if (peek(PARSE_STATEMENT)) {
return parse_statement();
} else if (peek(FUNCTION)) {
return function();
} else {
expect("primary expression");
return nullptr;
}
}
return_t postfix_expression() {
Iter from = i;
return_t left = primary_expression();
while(true) {
if(accept(LEFT_BRACKET)) {
return_t right = expression();
expect(RIGHT_BRACKET);
if(peek(EQUAL) || peek(PLUS_EQUAL) || peek(DASH_EQUAL) ||
peek(STAR_EQUAL) || peek(SLASH_EQUAL) || peek(PERCENT_EQUAL)) {
std::shared_ptr<FieldAssignment<Iter,R,F>> node;