-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeartParser.cs
948 lines (822 loc) · 27.3 KB
/
HeartParser.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class HeartParser
{
private string Input;
public HeartParser(string input)
{
Input = input;
}
public class PieceTokenizer
{
Dictionary<Guid, int> tracking = new Dictionary<Guid, int>();
private string[] charz;
private int curIndex = 0;
public PieceTokenizer(string[] f)
{
charz = f;
}
public string GetNextPiece()
{
return charz[curIndex++];
}
public string CurrentPiece(int ind = 0)
{
if (charz.Length <= curIndex + ind)
return "";
return charz[curIndex + ind];
}
public Guid reStart()
{
Guid f;
tracking.Add(f = Guid.NewGuid(), curIndex);
return f;
}
public void goBack(Guid g)
{
curIndex = tracking[g];
tracking.Remove(g);
}
public void Progress()
{
curIndex++;
}
public bool done()
{
return charz.Length == curIndex;
}
}
public class VariableTracker{
private string[] Variables = new string[1024];
private int index = 0;
public string NewVariable()
{
return Variables[index++] = newv();
}
public string LastVariable()
{
return Variables[index--] ;
}
private int curC = 0;
private string newv() {
return "vvv" + (curC++);
}
}
public void Run()
{
var toparse = getParsable().Distinct();
var s = getToks().ToArray();
var f = new PieceTokenizer(s);
D_Document doc;
if ((doc = makeDocument(f)) == null)
{
throw new Exception("bad document");
}
StringBuilder classes = new StringBuilder();
StringBuilder methods = new StringBuilder();
VariableTracker vt=new VariableTracker();
foreach (var dItem in doc.Items)
{
var v = buildPiece(vt,dItem.Piece, dItem);
classes.AppendFormat(
@"public class D_{0}
{{
{1}
}}", dItem.Word.Value, v.Item1);
methods.AppendFormat(
@"
private D_{0} make{0}(PieceTokenizer f)
{{
var g = f.reStart();
D_{0} e = new D_{0}();
{1}
}}",
dItem.Word.Value, v.Item2);
}
}
private Tuple<string, string> buildPiece(VariableTracker vt, D_Piece piece, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
switch (piece.PieceClause)
{
case D_Piece.D_PieceClause.PieceWrap:
v = buildPieceWrap(vt, piece.PieceWrap, item);
cls.Append(v.Item1);
method.Append(v.Item2);
break;
case D_Piece.D_PieceClause.PieceKernalOr:
v = buildPieceKernalOr(vt, piece.PieceKernalOr, item);
cls.Append(v.Item1);
method.Append(v.Item2);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceWrap(VariableTracker vt, D_PieceWrap pieceWrap, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
if (pieceWrap.HasPieceExtras)
{
switch (pieceWrap.PieceExtras.PieceExtrasClause)
{
case D_PieceExtras.D_PieceExtrasClause.QuestionMark:
break;
case D_PieceExtras.D_PieceExtrasClause.DotDot:
method.AppendLine("while(true) {");
v = buildPieceInformation(vt, pieceWrap.PieceInformation, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
method.AppendLine("}");
cls.AppendLine("public List<D_" + item.Word.Value + "> " + item.Word.Value + "s = new List<D_" + item.Word.Value + ">();");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceInformation(VariableTracker vt, D_PieceInformation pieceInformation, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
switch (pieceInformation.PieceInformationClause)
{
case D_PieceInformation.D_PieceInformationClause.JustPiece:
v = buildJustPiece(vt, pieceInformation.JustPiece, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
break;
case D_PieceInformation.D_PieceInformationClause.PieceWithParam:
v = buildPieceWithParam(vt, pieceInformation.PieceWithParam, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceWithParam(VariableTracker vt, D_PieceWithParam pieceWrap, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
v = buildPiece(vt, pieceWrap.Piece1, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
v = buildPiece(vt, pieceWrap.Piece2, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2 + "conitnue; else break;");
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildJustPiece(VariableTracker vt, D_JustPiece justPiece, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v = buildPiece(vt, justPiece.Piece, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceKernalOr(VariableTracker vt, D_PieceKernalOr pieceWrap, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
foreach (var addition in pieceWrap.PieceKernalAdditions)
{
v = buildPieceKernalAddition(vt, addition, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceKernalAddition(VariableTracker vt, D_PieceKernalAddition pieceWrap, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
foreach (var contents in pieceWrap.PieceContents)
{
v = buildPieceContents(vt, contents, item);
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
}
for (int i = 0; i < pieceWrap.PieceContents.Count; i++) {
method.AppendLine("}");
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
private Tuple<string, string> buildPieceContents(VariableTracker vt, D_PieceContent pieceWrap, D_Item item)
{
StringBuilder cls = new StringBuilder();
StringBuilder method = new StringBuilder();
Tuple<string, string> v;
switch (pieceWrap.PieceContentClause) {
case D_PieceContent.D_PieceContentClause.Word:
var d = vt.NewVariable();
method.AppendLine("D_" + pieceWrap.Word.Value + " "+d+";\r\n if(("+d+" = make" + pieceWrap.Word.Value + "(f))!=null){");
break;
case D_PieceContent.D_PieceContentClause.String:
method.AppendLine("if(isToken(f,\""+pieceWrap.String.Value+"\")){");
break;
case D_PieceContent.D_PieceContentClause.Piece:
v = buildPiece(vt, pieceWrap.Piece, item);
method.AppendLine("{");
cls.AppendLine(v.Item1);
method.AppendLine(v.Item2);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new Tuple<string, string>(cls.ToString(), method.ToString());
}
public class D_Document
{
public List<D_Item> Items = new List<D_Item>();
}
public class D_Item
{
public D_Word Word;
public D_Piece Piece;
}
public class D_Piece
{
public enum D_PieceClause
{
PieceWrap, PieceKernalOr
}
public D_PieceClause PieceClause;
public D_PieceWrap PieceWrap;
public D_PieceKernalOr PieceKernalOr;
}
public class D_PieceWrap
{
public D_PieceInformation PieceInformation;
public bool HasPieceExtras;
public D_PieceExtras PieceExtras;
}
public class D_PieceExtras
{
public enum D_PieceExtrasClause
{
QuestionMark, DotDot
}
public D_PieceExtrasClause PieceExtrasClause;
}
public class D_PieceInformation
{
public enum D_PieceInformationClause
{
JustPiece, PieceWithParam
}
public D_PieceInformationClause PieceInformationClause;
public D_JustPiece JustPiece;
public D_PieceWithParam PieceWithParam;
}
public class D_JustPiece
{
public D_Piece Piece;
}
public class D_PieceWithParam
{
public D_Piece Piece1; public D_Piece Piece2;
}
public class D_PieceKernalOr
{
public List<D_PieceKernalAddition> PieceKernalAdditions = new List<D_PieceKernalAddition>();
}
public class D_PieceKernalAddition
{
public List<D_PieceContent> PieceContents = new List<D_PieceContent>();
}
public class D_PieceContent
{
public enum D_PieceContentClause
{
Word, String, Piece
}
public D_PieceContentClause PieceContentClause;
public D_Word Word;
public D_String String;
public D_Piece Piece;
}
private D_Document makeDocument(PieceTokenizer f)
{
var g = f.reStart();
D_Document doc = new D_Document();
while (true)
{
D_Item c;
if ((c = makeItem(f)) != null)
{
doc.Items.Add(c);
if (isToken(f, "\n"))
continue;
else continue;
return doc;
}
else
return doc;
}
}
private D_Item makeItem(PieceTokenizer f)
{
var g = f.reStart();
D_Item item = new D_Item();
string w1;
if ((w1 = getWord(f)) != null)
{
item.Word = new D_Word();
item.Word.Value = w1;
if (isToken(f, "="))
{
if ((item.Piece = makePiece(f)) != null)
{
return item;
}
}
}
f.goBack(g);
return null;
}
private D_Piece makePiece(PieceTokenizer f)
{
var g = f.reStart();
D_Piece piece = new D_Piece();
D_PieceWrap d; D_PieceKernalOr c;
if ((d = makePieceWrap(f)) != null)
{
piece.PieceClause = D_Piece.D_PieceClause.PieceWrap;
piece.PieceWrap = d;
}
else if ((c = makePieceKernalOr(f)) != null)
{
piece.PieceClause = D_Piece.D_PieceClause.PieceKernalOr;
piece.PieceKernalOr = c;
}
else return null;
return piece;
}
private D_PieceWrap makePieceWrap(PieceTokenizer f)
{
var g = f.reStart();
D_PieceWrap piece = new D_PieceWrap();
D_PieceInformation c;
D_PieceExtras d;
if ((c = makePieceInformation(f)) != null)
{
piece.PieceInformation = c;
if ((d = makePieceExtras(f)) != null)
{
piece.HasPieceExtras = true;
piece.PieceExtras = d;
}
else
piece.HasPieceExtras = false;
}
else
{
f.goBack(g);
return null;
}
return piece;
}
private D_PieceInformation makePieceInformation(PieceTokenizer f)
{
var g = f.reStart();
D_JustPiece c;
D_PieceWithParam d;
D_PieceInformation piece = new D_PieceInformation();
if ((c = makeJustPiece(f)) != null)
{
piece.PieceInformationClause = D_PieceInformation.D_PieceInformationClause.JustPiece;
piece.JustPiece = c;
}
else if ((d = makePieceWithParam(f)) != null)
{
piece.PieceInformationClause = D_PieceInformation.D_PieceInformationClause.PieceWithParam;
piece.PieceWithParam = d;
}
else
{
f.goBack(g);
return null;
}
return piece;
}
private D_JustPiece makeJustPiece(PieceTokenizer f)
{
var g = f.reStart();
D_JustPiece piece = new D_JustPiece();
D_Piece c;
if (isToken(f, "("))
{
if ((c = makePiece(f)) != null)
{
piece.Piece = c;
if (isToken(f, ")"))
{
return piece;
}
}
}
f.goBack(g);
return null;
}
private D_PieceWithParam makePieceWithParam(PieceTokenizer f)
{
var g = f.reStart();
D_PieceWithParam piece = new D_PieceWithParam();
D_Piece c;
D_Piece d;
if (isToken(f, "("))
{
if ((c = makePiece(f)) != null)
{
piece.Piece1 = c;
if (isToken(f, ","))
{
if ((d = makePiece(f)) != null)
{
piece.Piece2 = d;
if (isToken(f, ")"))
{
return piece;
}
}
}
}
}
f.goBack(g);
return null;
}
private D_PieceExtras makePieceExtras(PieceTokenizer f)
{
var g = f.reStart();
D_PieceExtras piece = new D_PieceExtras();
if (isToken(f, "?"))
{
piece.PieceExtrasClause = D_PieceExtras.D_PieceExtrasClause.QuestionMark;
}
else if (isToken(f, ".") && isToken(f, "."))
{
piece.PieceExtrasClause = D_PieceExtras.D_PieceExtrasClause.DotDot;
}
else
{
f.goBack(g);
return null;
}
return piece;
}
private D_PieceKernalOr makePieceKernalOr(PieceTokenizer f)
{
var g = f.reStart();
D_PieceKernalOr piece = new D_PieceKernalOr();
while (true)
{
D_PieceKernalAddition c;
if ((c = makePieceKernalAddition(f)) != null)
{
piece.PieceKernalAdditions.Add(c);
if (isToken(f, "|"))
{
continue;
}
return piece;
}
else
{
f.goBack(g);
return null;
}
}
}
private D_PieceKernalAddition makePieceKernalAddition(PieceTokenizer f)
{
var g = f.reStart();
D_PieceKernalAddition piece = new D_PieceKernalAddition();
while (true)
{
D_PieceContent c;
if ((c = makePieceContent(f)) != null)
{
piece.PieceContents.Add(c);
if (isToken(f, "+"))
{
continue;
}
return piece;
}
else
{
f.goBack(g);
return null;
}
}
}
private D_PieceContent makePieceContent(PieceTokenizer f)
{
var g = f.reStart();
D_PieceContent piece = new D_PieceContent();
D_Piece c;
string w1;
if ((w1 = getWord(f)) != null)
{
piece.PieceContentClause = D_PieceContent.D_PieceContentClause.Word;
piece.Word = new D_Word();
piece.Word.Value = w1;
}
else if (isString(f))
{
piece.PieceContentClause = D_PieceContent.D_PieceContentClause.String;
piece.String = new D_String();
piece.String.Value = f.CurrentPiece();
}
else if ((c = makePiece(f)) != null)
{
piece.PieceContentClause = D_PieceContent.D_PieceContentClause.Piece;
piece.Piece = c;
}
else
{
f.goBack(g);
return null;
}
return piece;
}
public class D_Word
{
public string Value;
}
public class D_String
{
public string Value;
}
public bool isBool(PieceTokenizer f, bool advance = true)
{
bool d;
bool fc = bool.TryParse(f.CurrentPiece(), out d);
if (fc && advance)
f.Progress();
return fc;
}
public bool isString(PieceTokenizer f, bool advance = true)
{
bool fc = f.CurrentPiece().StartsWith("\"") && f.CurrentPiece().EndsWith("\"");
if (fc && advance)
f.Progress();
return fc;
}
public bool isNumber(PieceTokenizer f, bool advance = true)
{
int d;
bool fc = int.TryParse(f.CurrentPiece(), out d);
if (fc && advance)
f.Progress();
return fc;
}
public bool isToken(PieceTokenizer f, string tok, bool advance = true)
{
int d;
bool fc = f.CurrentPiece() == tok;
if (fc && advance)
f.Progress();
return fc;
}
private bool isAToken(PieceTokenizer f, bool advance = true)
{
bool j = isToken(f, "+", false) || isToken(f, "(", false) || isToken(f, ")", false) || isToken(f, "|", false) || isToken(f, "?", false);
if (j)
{
if (advance)
f.Progress();
}
return j;
}
public string getWord(PieceTokenizer f, bool advance = true)
{
bool j = !isAToken(f, false) && !isBool(f, false) && !isNumber(f, false) && !isString(f, false);
if (j)
{
string d;
Console.WriteLine(d = f.CurrentPiece());
if (advance)
f.Progress();
return d;
}
return null;
}
public IEnumerable<string> getToks()
{
string cur = "";
char[] toks = new char[] { '.', '=', '+', '|', ')', '(', ',' };
char lastTok = ' ';
bool wasNewLine = false;
bool inStr = false;
foreach (var v in Input)
{
if (!inStr && toks.Contains(v))
{
if (cur != "") yield return cur;
cur = "";
lastTok = v;
yield return v.ToString();
continue;
}
if (v == '\"')
{
inStr = !inStr;
}
if (v == ' ' || v == '\t')
{
if (cur == "")
continue;
else
{
yield return cur;
cur = "";
continue;
}
}
else if (v == '\r')
{
if (cur != "")
{
yield return cur;
}
cur = v.ToString();
continue;
}
else
{
cur += v;
}
if (cur == "\r\n")
{
if (lastTok == '|')
{
cur = "";
continue;
}
else
{
if (!wasNewLine)
{
yield return "\n";
}
wasNewLine = true;
cur = "";
continue;
}
}
else wasNewLine = false;
}
}
private string[] getParsable()
{
var sts = new List<string>();
sts.Add("\t");
sts.Add("\r\n");
int inString = -1;
for (int i = 0; i < Input.Length; i++)
{
if (Input[i] == '\"')
{
if (inString == -1)
{
inString = i + 1;
}
else
{
sts.Add(Input.Substring(inString, (i - inString)));
inString = -1;
}
}
}
return sts.ToArray();
}
public class PToken
{
public PTokenType Type;
}
}
public enum PTokenType { }
}
/*
public bool isItem(PieceTokenizer f, bool advance = true)
{
var g = f.reStart();
bool good = false;
if (isWord(f))
{
if (isToken(f, "="))
{
if (isPiece(f))
{
good = true;
}
}
}
if (good)
{
}
else
{
f.goBack(g);
}
return good;
}
private bool isPiece(PieceTokenizer f, bool advance = true)
{
Console.WriteLine(f.CurrentPiece());
var g = f.reStart();
bool good = false;
if ((isPieceOr1(f) || isPieceOr2(f)) && (isToken(f, "?") || (isToken(f, ".") && isToken(f, ".")) || true))
{
good = true;
}
if (!good || isToken(f, "+") || isToken(f, "|"))
{
while (true)
{
good = false;
bool g1 = false;
while (true)
{
g1 = false;
if (isWord(f) || isString(f) || isPiece(f))
{
g1 = true;
if (isToken(f, "+"))
{
continue;
}
else break;
}
else break;
}
if (g1)
{
good = true;
if (isToken(f, "|"))
{
continue;
}
else break;
}
else break;
}
}
if (good)
{
}
else
f.goBack(g);
return good;
}
private bool isPieceOr1(PieceTokenizer f, bool advance = true)
{
var g = f.reStart();
bool good = false;
if (isToken(f, "("))
if (isPiece(f))
if (isToken(f, ")"))
good = true;
if (good)
{
}
else
f.goBack(g);
return good;
}
private bool isPieceOr2(PieceTokenizer f, bool advance = true)
{
var g = f.reStart();
bool good = false;
if (isToken(f, "("))
if (isPiece(f))
if (isToken(f, ","))
if (isPiece(f))
if (isToken(f, ")"))
good = true;
if (good)
{
}
else
f.goBack(g);
return good;
}
*/