-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parselets.cpp
1382 lines (1153 loc) · 37.2 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
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 "Parselets.h"
#include "Expressions.h"
#include "Parser.h"
#include "UniquePtr.h"
#include <string>
using namespace Jet;
//todo: fix this being a parser hack that drops potential tokens
Token ParseType(Parser* parser, bool parse_arrays = true)
{
Token name = parser->Consume(TokenType::Name);
std::string out = name.text;
//parse namespaces
while (parser->MatchAndConsume(TokenType::Scope))
{
out += "::";
out += parser->Consume(TokenType::Name).text;
}
while (parser->MatchAndConsume(TokenType::Asterisk))//parse pointers
out += '*';
if (parser->MatchAndConsume(TokenType::LessThan))//parse templates
{
out += "<";
//recursively parse the rest
bool first = true;
do
{
if (first == false)
out += ",";
first = false;
out += ParseType(parser).text;
} while (parser->MatchAndConsume(TokenType::Comma));
parser->ConsumeTemplateGT();
//parser->Consume(TokenType::GreaterThan);
out += ">";
}
else if (parser->MatchAndConsume(TokenType::LeftParen))
{
out += "(";
if (!parser->MatchAndConsume(TokenType::RightParen))
{
//recursively parse the rest
bool first = true;
do
{
if (first == false)
out += ",";
first = false;
out += ParseType(parser).text;
} while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::RightParen);
}
out += ")";
}
while (parser->MatchAndConsume(TokenType::Asterisk))//parse pointers
out += '*';
//parse arrays
if (parse_arrays && parser->MatchAndConsume(TokenType::LeftBracket))
{
if (parser->MatchAndConsume(TokenType::RightBracket))
{
//its an array with no size
out += "[]";
}
else
{
//its an array type
//read the number
auto size = parser->ParseExpression(Precedence::ASSIGNMENT);
auto tok = parser->Consume(TokenType::RightBracket);
if (auto s = dynamic_cast<NumberExpression*>(size))
{
auto n = s->GetValue();
if (n.type != NumberExpression::Number::Int)
{
parser->Error("Cannot size array with a non integer value", tok);
}
else if (n.data.i <= 0)
{
parser->Error("Cannot size array with a zero or negative size", tok);
}
out += "[" + std::to_string(n.data.i) + "]";
}
else
{
parser->Error("Cannot size array with a non constant size", tok);
}
}
}
Token ret;
ret.column = name.column;
ret.line = name.line;
ret.trivia_length = name.trivia_length;
ret.text_ptr = name.text_ptr;
ret.text = out;
ret.type = TokenType::Name;
return ret;
}
Token ParseTrait(Parser* parser)
{
Token name = parser->Consume(TokenType::Name);
std::string out = name.text;
//parse templates
if (parser->MatchAndConsume(TokenType::LessThan))
{
out += "<";
//recursively parse the rest
bool first = true;
do
{
if (first == false)
out += ",";
first = false;
out += ParseType(parser).text;
} while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::GreaterThan);
out += ">";
}
Token ret;
ret.column = name.column;
ret.line = name.line;
ret.trivia_length = name.trivia_length;
ret.text_ptr = name.text_ptr;
ret.text = out;
return ret;
}
Expression* NameParselet::parse(Parser* parser, Token token)
{
if (parser->Match(TokenType::TemplateBegin))
{
//check if the token after the next this is a , or a > if it is, then im a template
//Token ahead = parser->LookAhead(1);
//if (ahead.type != TokenType::Not)//ahead.type != TokenType::Comma && ahead.type != TokenType::GreaterThan)
//return new NameExpression(token);
auto begin_tok = parser->Consume();
//parser->Consume(TokenType::LessThan);
//token.text += "<";
//parse it as if it is a template
std::vector<Token>* templates = new std::vector < Token >();
bool first = true;
do
{
//if (first)
// first = false;
//else
// token.text += ",";
Token tname = ::ParseType(parser);
//token.text += tname.text;
templates->push_back(tname);
} while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::GreaterThan);
//token.text += ">";
//actually, just treat me as a nameexpression, that should work
//idk, but what to do with the template stuff
//ok need to use these templates
return new NameExpression(token, templates);
}
return new NameExpression(token);
}
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;
parser->Error("AssignParselet: Left hand side must be a storable location!", token);
}
return new AssignExpression(token, left, right);
}
Expression* ScopeParselet::parse(Parser* parser, Expression* left, Token token)
{
Expression* right = parser->ParseExpression(Precedence::ASSIGNMENT - 1/*assignment prcedence -1 */);
return new ScopedExpression(token, left, right);
}
Expression* OperatorAssignParselet::parse(Parser* parser, Expression* left, Token token)
{
if (dynamic_cast<IStorableExpression*>(left) == 0)
parser->Error("OperatorAssignParselet: Left hand side must be a storable location!", token);
Expression* right = parser->ParseExpression(Precedence::ASSIGNMENT - 1);
return new OperatorAssignExpression(token, left, right);
}
Expression* PrefixOperatorParselet::parse(Parser* parser, Token token)
{
Expression* right = parser->ParseExpression(precedence);
if (right == 0)
parser->Error("PrefixOperatorParselet: Right hand side missing!", token);
return new PrefixExpression(token, right);
}
Expression* SizeofParselet::parse(Parser* parser, Token token)
{
Token left = parser->Consume(TokenType::LeftParen);
Token type = ::ParseType(parser);
Token right = parser->Consume(TokenType::RightParen);
return new SizeofExpression(token, left, type, right);
}
Expression* TypeofParselet::parse(Parser* parser, Token token)
{
Token left = parser->Consume(TokenType::LeftParen);
auto x = parser->ParseExpression(0);
Token right = parser->Consume(TokenType::RightParen);
return new TypeofExpression(token, left, x, right);
}
Expression* CastParselet::parse(Parser* parser, Token token)
{
Token type = ::ParseType(parser);
Token end = parser->Consume(TokenType::GreaterThan);
auto right = parser->ParseExpression(Precedence::PREFIX);
if (right == 0)
parser->Error("CastParselet: Right hand side missing!", token);
return new CastExpression(type, token, right, end);
}
Expression* BinaryOperatorParselet::parse(Parser* parser, Expression* left, Token token)
{
Expression* right = parser->ParseExpression(precedence - (isRight ? 1 : 0));
if (right == 0)
parser->Error("BinaryOperatorParselet: Right hand side missing!", token);
return new OperatorExpression(left, token, right);
}
Expression* GroupParselet::parse(Parser* parser, Token token)
{
UniquePtr<Expression*> exp = parser->ParseExpression();
auto end = parser->Consume(TokenType::RightParen);
return new GroupExpression(token, exp.Release(), end);
}
Expression* WhileParselet::parse(Parser* parser, Token token)
{
auto ob = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> condition = parser->ParseExpression();
auto cb = parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->ParseBlock());
return new WhileExpression(token, ob, condition.Release(), cb, block);
}
Expression* CaseParselet::parse(Parser* parser, Token token)
{
Token number = parser->Consume(TokenType::Number);
Token colon = parser->Consume(TokenType::Colon);
return new CaseExpression(token, number, colon);
}
Expression* DefaultParselet::parse(Parser* parser, Token token)
{
auto colon = parser->Consume(TokenType::Colon);
return new DefaultExpression(token, colon);
}
Expression* ForParselet::parse(Parser* parser, Token token)
{
auto ob = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> initial = 0;
if (!parser->Match(TokenType::Semicolon))
initial = parser->ParseStatement(false);
auto s1 = parser->Consume();
UniquePtr<Expression*> condition = 0;
if (!parser->Match(TokenType::Semicolon))
condition = parser->ParseStatement(false);
auto s2 = parser->Consume();
UniquePtr<Expression*> increment = 0;
if (!parser->Match(TokenType::RightParen))
increment = parser->ParseExpression();
auto cb = parser->Consume(TokenType::RightParen);
auto block = new ScopeExpression(parser->ParseBlock());
return new ForExpression(token, ob, initial.Release(), s1, condition.Release(), s2, increment.Release(), cb, block);
}
Expression* SwitchParselet::parse(Parser* parser, Token token)
{
auto ob = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> var = parser->ParseExpression();
auto cb = parser->Consume(TokenType::RightParen);
BlockExpression* block = parser->ParseBlock(false);
return new SwitchExpression(token, ob, var.Release(), cb, block);
}
Expression* UnionParselet::parse(Parser* parser, Token token)
{
Token name = parser->Consume(TokenType::Name);
Token equals = parser->Consume(TokenType::Assign);
std::vector<std::pair<Token, Token>> elements;
while (parser->LookAhead().type != TokenType::Semicolon)
{
//parse in each block
Token name = parser->Consume(TokenType::Name);
Token bor = parser->LookAhead();
if (bor.type == TokenType::BOr)
{
parser->Consume();
elements.push_back({ name, bor });
continue;
}
elements.push_back({ name, Token() });
break;
}
return new UnionExpression(token, name, equals, std::move(elements));
}
Expression* MatchParselet::parse(Parser* parser, Token token)
{
Token open = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> var = parser->ParseExpression();
Token close = parser->Consume(TokenType::RightParen);
Token ob = parser->Consume(TokenType::LeftBrace);
std::vector<MatchCase> cases;
while (!parser->Match(TokenType::RightBrace))
{
//parse in each block
if (parser->LookAhead().type == TokenType::Default)
{
Token def = parser->Consume();
auto tok = parser->Consume(TokenType::Assign);//fix this not being one token
parser->Consume(TokenType::GreaterThan);
tok.text += ">";
BlockExpression* block = parser->ParseBlock(true);
cases.push_back({ def, def, tok, block });
break;
}
Token type = parser->Consume(TokenType::Name);
Token name = parser->Consume(TokenType::Name);
Token tok = parser->Consume(TokenType::Assign);
parser->Consume(TokenType::GreaterThan);
tok.text += ">";
BlockExpression* block = parser->ParseBlock(true);
cases.push_back({ type, name, tok, block });
}
Token cb = parser->Consume();
return new MatchExpression(token, open, close, var.Release(), ob, std::move(cases), cb);
}
Expression* IfParselet::parse(Parser* parser, Token token)
{
std::vector<Branch*> branches;
//take parens
auto ob = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> ifcondition = parser->ParseExpression();
auto cb = parser->Consume(TokenType::RightParen);
BlockExpression* ifblock = parser->ParseBlock(true);
branches.push_back(new Branch(token, ob, cb, new ScopeExpression(ifblock), ifcondition.Release()));
Branch* Else = 0;
while (true)
{
//look for elses
Token t = parser->LookAhead();
if (t.type == TokenType::ElseIf)
{
parser->Consume();
//keep going
auto ob = parser->Consume(TokenType::LeftParen);
UniquePtr<Expression*> condition = parser->ParseExpression();
auto cb = parser->Consume(TokenType::RightParen);
BlockExpression* block = parser->ParseBlock(true);
branches.push_back(new Branch(t, ob, cb, new ScopeExpression(block), condition.Release()));
}
else if (t.type == TokenType::Else)
{
parser->Consume();
//its an else
BlockExpression* block = parser->ParseBlock(true);
Else = new Branch(t, Token(), Token(), new ScopeExpression(block), 0);
break;
}
else
break;//nothing else
}
return new IfExpression(token, std::move(branches), Else);
}
Expression* TraitParselet::parse(Parser* parser, Token token)
{
Token name = parser->Consume(TokenType::Name);
Token tcb, tob;
//parse templates
std::vector<TraitTemplate>* templated = 0;
if (parser->Match(TokenType::LessThan))
{
tob = parser->Consume();
templated = new std::vector < TraitTemplate > ;
//parse types and stuff
do
{
Token ttname = parser->Consume(TokenType::Name);
Token tname = parser->LookAhead();
if (tname.type == TokenType::Comma)
{
parser->Consume();
templated->push_back({ ttname, tname });
}
else
{
templated->push_back({ ttname, Token() });
break;
}
} while (true);
tcb = parser->Consume(TokenType::GreaterThan);
}
auto ob = parser->Consume(TokenType::LeftBrace);
std::vector<TraitFunction> functions;
while (parser->Match(TokenType::RightBrace) == false)
{
TraitFunction func;
Token tfunc = parser->Consume(TokenType::Function);
func.ret_type = ::ParseType(parser);
func.name = parser->Consume(TokenType::Name);
func.func_token = tfunc;
func.open_brace = parser->Consume(TokenType::LeftParen);
bool first = true;
while (parser->Match(TokenType::RightParen) == false)
{
if (first)
{
first = false;
//parse args
auto type = ::ParseType(parser);
func.args.push_back({ type, parser->Consume(TokenType::Name), Token() });
}
else
{
auto comma = parser->Consume(TokenType::Comma);
//parse args
auto type = ::ParseType(parser);
func.args.push_back({ type, parser->Consume(TokenType::Name), comma });
}
}
func.close_brace = parser->Consume();
func.semicolon = parser->Consume(TokenType::Semicolon);
functions.push_back(func);
}
auto cb = parser->Consume();
return new TraitExpression(token, name, tob, tcb, ob, std::move(functions), templated, cb);
}
Expression* StructParselet::parse(Parser* parser, Token token)
{
Token name = parser->Consume(TokenType::Name);
Token ob, cb;
//parse templates
std::vector<StructTemplate>* templated = 0;
if (parser->Match(TokenType::LessThan))
{
ob = parser->Consume();
templated = new std::vector < StructTemplate > ;
//parse types and stuff
do
{
Token ttname = ParseTrait(parser);
Token tname = parser->Consume(TokenType::Name);
Token comma = parser->LookAhead();
if (comma.type == TokenType::Comma)
{
templated->push_back({ ttname, tname, comma });
parser->Consume();
}
else
{
templated->push_back({ ttname, tname, Token() });
break;
}
} while (true);
cb = parser->Consume(TokenType::GreaterThan);
}
//parse base type
Token base_name, colon;
if (parser->Match(TokenType::Colon))
{
colon = parser->Consume();
base_name = ::ParseType(parser, false);
}
auto start = parser->Consume(TokenType::LeftBrace);
std::vector<StructMember> members;
Token end;
while (true)
{
if (parser->Match(TokenType::RightBrace))
{
end = parser->Consume(TokenType::RightBrace);
break;
}
//first read type
if (parser->LookAhead().type == TokenType::Function || parser->LookAhead().type == TokenType::Virtual)
{
//parse the function
auto* expr = parser->ParseStatement(true);
if (auto fun = dynamic_cast<FunctionExpression*>(expr))
{
StructMember member;
member.type = StructMember::FunctionMember;
member.function = fun;
members.push_back(member);
continue;
}
parser->Error("Member function definition must have body!", token);
}
else if (parser->Match(TokenType::Generator))
{
parser->Error("Struct member generators not yet implemented!", token);
//parse the function
auto* expr = parser->ParseStatement(true);
if (auto fun = dynamic_cast<FunctionExpression*>(expr))
{
StructMember member;
member.type = StructMember::FunctionMember;
member.function = fun;
members.push_back(member);
continue;
}
parser->Error("Member function definition must have body!", token);
}
else if (parser->Match(TokenType::Struct))
{
//parse the function
auto* expr = parser->ParseStatement(true);
if (auto fun = dynamic_cast<StructExpression*>(expr))
{
for (auto ii : fun->members)
{
if (ii.type == StructMember::FunctionMember)
parser->Error("Member struct definitions cannot have member functions!", token);
}
StructMember member;
member.type = StructMember::DefinitionMember;
member.definition = fun;
members.push_back(member);
continue;
}
parser->Error("Member struct definition must have body!", token);
}
Token type = ::ParseType(parser);
Token name = parser->Consume(TokenType::Name);//then read name
if (parser->MatchAndConsume(TokenType::LeftBracket))
{
//its an array type
//read the number
auto size = parser->ParseExpression(Precedence::ASSIGNMENT);
parser->Consume(TokenType::RightBracket);
if (auto s = dynamic_cast<NumberExpression*>(size))
{
auto n = s->GetValue();
if (n.type != NumberExpression::Number::Int)
{
parser->Error("Cannot size array with a non integer value", token);
}
else if (n.data.i <= 0)
{
parser->Error("Cannot size array with a zero or negative size", token);
}
type.text += "[" + std::to_string(n.data.i) + "]";
}
else
{
parser->Error("Cannot size array with a non constant size", token);
}
}
Token semicolon = parser->Consume(TokenType::Semicolon);
StructMember member;
member.type = StructMember::VariableMember;
member.variable = { type, name, semicolon };
members.push_back(member);
}
return new StructExpression(token, name, start, end, ob, std::move(members), /*elements, functions,*/ templated, cb, colon, base_name);
}
bool IsValidFunctionNameToken(TokenType op)
{
//todo: speed this up somehow
if (op == TokenType::Name)
return true;
else if (op == TokenType::Plus)
return true;
else if (op == TokenType::Minus)
return true;
else if (op == TokenType::Slash)
return true;
else if (op == TokenType::Asterisk)
return true;
else if (op == TokenType::LeftShift)
return true;
else if (op == TokenType::RightShift)
return true;
else if (op == TokenType::BOr)
return true;
else if (op == TokenType::BAnd)
return true;
else if (op == TokenType::Xor)
return true;
else if (op == TokenType::Modulo)
return true;
else if (op == TokenType::LeftBracket)
return true;
else if (op == TokenType::LessThan)
return true;
else if (op == TokenType::GreaterThan)
return true;
else if (op == TokenType::GreaterThanEqual)
return true;
else if (op == TokenType::LessThanEqual)
return true;
else if (op == TokenType::Equals)
return true;
else if (op == TokenType::Assign)
return true;
else if (op == TokenType::AddAssign)
return true;
return false;
}
//maybe add a way to make non - nullable non - freeable pointers
//add superconst to represent const int* const x;
//still gotta solve operator problem with passing operands as pointers
Expression* FunctionParselet::parse(Parser* parser, Token token)
{
//read in type
Token ret_type = ::ParseType(parser);
//todo fix this not allowing binary not operator
Token name;
if (parser->Match(TokenType::BNot))
{
Token t = parser->Consume();
Token rname = parser->Consume(TokenType::Name);
if (rname.trivia_length > 0)
parser->Error("Cannot have whitespace between ~ and the classname in destructors", rname);
name = rname;
name.text = "~" + rname.text;
name.text_ptr -= 1;
name.trivia_length = t.trivia_length;
}
else
{
name = parser->Consume(TokenType::Name);
}
auto arguments = new std::vector < FunctionArg > ;
Token stru, colons;
if (parser->Match(TokenType::Scope))
{
//its a extension method definition
colons = parser->Consume();
stru = name;
name = parser->Consume(TokenType::Name);//parse the real function name
}
Token oper;
if (name.text == "operator")
{
//parse in the operator
oper = name;
name = parser->Consume();
if (name.type == TokenType::LeftBracket)
{
auto name2 = parser->Consume(TokenType::RightBracket);
name.text += ']';
}
}
//check that the name is ok
if (stru.text.length() && stru.type != TokenType::Name)
parser->Error("Invalid struct name", stru);
else if (!IsValidFunctionNameToken(name.type))
parser->Error("Not a valid operator overload", name);
//look for templates
std::vector<std::pair<Token, Token>>* templated = 0;
if (parser->MatchAndConsume(TokenType::LessThan))
{
templated = new std::vector < std::pair<Token, Token> > ;
//parse types and stuff
do
{
Token ttname = ParseTrait(parser);
Token tname = parser->Consume(TokenType::Name);
templated->push_back({ ttname, tname });
} while (parser->MatchAndConsume(TokenType::Comma));
parser->Consume(TokenType::GreaterThan);
}
NameExpression* varargs = 0;
Token ob = parser->Consume(TokenType::LeftParen);
if (!parser->Match(TokenType::RightParen))
{
do
{
Token type = ::ParseType(parser);
Token name = parser->Consume();
Token comma = parser->LookAhead();
if (name.type == TokenType::Name)
{
if (comma.type == TokenType::Comma)
arguments->push_back({ type, name, comma });
else
arguments->push_back({ type, name, Token() });
}
else
{
std::string str = "Token not as expected! Expected name or got: " + name.text;
parser->Error(str, name);
}
} while (parser->MatchAndConsume(TokenType::Comma));
}
Token cb = parser->Consume(TokenType::RightParen);
//check that there are a proper number of arguments if this is a operator overload
if (name.type != TokenType::Name)
{
//todo: fix unary minus
if ((name.type == TokenType::Decrement
|| name.type == TokenType::Increment
|| name.type == TokenType::BNot
/*|| name.type == TokenType::Minus*/) && arguments->size() != 0)
{
//todo put more unary ops here
parser->Error("Wrong number of arguments for operator overload '" + name.text + "' expected 0 got " + std::to_string(arguments->size()), (*arguments)[arguments->size() - 1].name);
}
else if (name.text == "[]" && arguments->size() != 1 && arguments->size() != 0)//.type == TokenType::DoubleBracket)//[] operator
{
parser->Error("Wrong number of arguments for operator overload '" + name.text + "' expected 1 or 0 got " + std::to_string(arguments->size()), (*arguments)[arguments->size() - 1].name);
}
else if (arguments->size() != 1)
{
//binary ops
parser->Error("Wrong number of arguments for operator overload '" + name.text + "' expected 1 got " + std::to_string(arguments->size()), (*arguments)[arguments->size() - 1].name);
}
}
auto block = new ScopeExpression(parser->ParseBlock());
return new FunctionExpression(token, name, ret_type, token.type == TokenType::Generator, arguments, block, /*varargs,*/ stru, colons, templated, 0, ob, cb, oper);
}
Expression* ExternParselet::parse(Parser* parser, Token token)
{
auto fun = parser->Consume(TokenType::Function);
Token ret_type = ::ParseType(parser);
Token name = parser->Consume();
if (name.type == TokenType::Free)
{
name.type = TokenType::Name;
}
else if (name.type != TokenType::Name)
{
std::string str = "Token Not As Expected! Expected: " + TokenToString[TokenType::Name] + " Got: " + name.text;
//it was probably forgotten, insert dummy
//fabricate a fake token
parser->Error(str, name);//need to make this throw again
//if (name.type != TokenType::Semicolon)
throw 7;
}
auto arguments = new std::vector < ExternArg > ;
std::string stru;
if (parser->MatchAndConsume(TokenType::Scope))
{
//its a struct definition
stru = name.text;
bool destructor = parser->MatchAndConsume(TokenType::BNot);
name = parser->Consume();
Token oper;
if (name.text == "operator")
{
//parse in the operator
oper = name;
name = parser->Consume();
if (name.type == TokenType::LeftBracket)
{
auto name2 = parser->Consume(TokenType::RightBracket);
name.text += ']';
}
}
//check that the name is ok
//if (stru.text.length() && stru.type != TokenType::Name)
// parser->Error("Invalid struct name", stru);
/*else*/ if (!IsValidFunctionNameToken(name.type))
parser->Error("Not a valid operator overload", name);
if (destructor)
name.text = "~" + name.text;
}
NameExpression* varargs = 0;
auto ob = parser->Consume(TokenType::LeftParen);
Token cb;
if (!parser->Match(TokenType::RightParen))
{
do
{
Token type = ::ParseType(parser);
Token name = parser->Consume(TokenType::Name);
if (name.type == TokenType::Name)
{
auto comma = parser->LookAhead();
if (comma.type == TokenType::Comma)
arguments->push_back({ type, name, comma });
else
arguments->push_back({ type, name, Token() });
}
/*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 = "Token not as expected! Expected name or got: " + name.text;
parser->Error(str, name);
}
} while (parser->MatchAndConsume(TokenType::Comma));
cb = parser->Consume(TokenType::RightParen);
}
else
{
cb = parser->Consume();
}
return new ExternExpression(token, fun, name, ret_type, ob, arguments, cb, stru);
}
Expression* LambdaAndAttributeParselet::parse(Parser* parser, Token token)
{
//if im not in a function parser, see where I am
//check if im an attribute
if (parser->LookAhead().type == TokenType::Name && (parser->LookAhead(1).type == TokenType::LeftParen || parser->LookAhead(1).type == TokenType::RightBracket))
{
//its an attribute
Token attribute = parser->Consume();
if (parser->MatchAndConsume(TokenType::LeftParen))
{
//handle arguments for the attribute
}
auto cb = parser->Consume(TokenType::RightBracket);
//parse the next thing
auto thing = parser->ParseStatement(false);
//ok, add the attribute to the list ok, we need an attribute expression
auto attr = new AttributeExpression(token, attribute, cb, thing);
if (dynamic_cast<ExternExpression*>(thing))
return attr;
else if (dynamic_cast<FunctionExpression*>(thing))
return attr;
parser->Error("Attributes can only be applied to extern and function expressions.", attribute);
return attr;
}
//parse captures
auto captures = new std::vector < Token > ;
if (parser->LookAhead().type != TokenType::RightBracket)
{
do
{
Token name = parser->Consume(TokenType::Number);
captures->push_back(name);
break;
} while (parser->MatchAndConsume(TokenType::Comma));
}
if (captures->size() == 0)
{
delete captures;
captures = 0;
}
parser->Consume(TokenType::RightBracket);
Token ob = parser->Consume(TokenType::LeftParen);
auto arguments = new std::vector < FunctionArg > ;
if (!parser->Match(TokenType::RightParen))
{
do
{
Token type = ::ParseType(parser);
Token name = parser->Consume();
Token comma = parser->LookAhead();
if (name.type == TokenType::Name)
{