language: C# (mono-2.8)
date: 109 days 17 hours ago
link:
可見度: public
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MathScripter.Engine
{
    public class CodeBlock
    {
        //this represents something inside "{" and "}".
        //should have the following:
        //dictionary of value names & their values <string, Expression> <- sort and search by string.
        //another dictionary which is all the global variables...
        //[to be changed when I add collections...]
        //also, input parameters.
        public Dictionary<string, CodeBlock> Functions; //functions it can call...
        public Dictionary<string, Expression> Params, Variables, GlobalVariables;
        public Expression ReturnValue; //to be set at the end... if nothing then zero.
        public string[] Commands; //every line is interpreted on-demand. lol, this sucks. fuck ya.
        public bool Finished;
        public int CurrentLine;
        public string Name;
        private int stringCount = 0;
        private bool ranOnce = false;
 
        public CodeBlock()
        {
            //you need to put stuff yourself.
        }
 
        public static List<string> NiceSplit(string s, char at, char LEFT = '{', char RIGHT = '}', char left = '[', char right = ']')
        {
            var l = new List<string>();
            var count = 0;
            var COUNT = 0;
            for (var i = 0; i < s.Length; ++i)
            {
                if (count == 0 && COUNT == 0 && s[i] == at)
                {
                    l.Add(s.Remove(i));
                    if (i == s.Length - 1)
                        return l;
                    s = s.Substring(i + 1);
                    i = -1;
                }
                else if (s[i] == left)
                    ++count;
                else if (s[i] == right)
                    --count;
                else if (s[i] == LEFT)
                    ++COUNT;
                else if (s[i] == RIGHT)
                    --COUNT;
            }
            l.Add(s);
            for(var i = 0; i < l.Count; ++i)
                if (l[i] == "")
                {
                    l.RemoveAt(i);
                    --i;
                }
            return l;
        }
        public Expression Do(string command)
        {
            if (string.IsNullOrEmpty(command))
                return new Number(0);
            //types:
            //var a = XXX -- adds to Variables
            //a = XXX -- if exists, changes
            //return XXX -- sets ReturnValue and ends the program.
            //XXX -- probably a function, just execute the fucking code.
            if (command.StartsWith("var"))
            {
                var name = command.Remove(0, command.IndexOf(' ') + 1);
                name = name.Replace(" ", "");
                name = name.Substring(0, name.IndexOf('='));
                if (!Variables.ContainsKey(name))
                {
                    var value = command.Remove(0, command.IndexOf('=') + 1).Replace(" ", "");
                    Variables.Add(name, Do(value));
                    return Variables.Last().Value.Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                }
                else
                    Variables[name] = Do(command.Remove(0, command.IndexOf('=') + 1).Replace(" ", "")).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
            }
            if (command.StartsWith("return"))
            {
                this.Finished = true;
                var tmp = Do(command.Remove(0, command.IndexOf(' ') + 1)).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                this.ReturnValue = tmp;
                return tmp;
            }
            //so its either a "set" or "execute"
            if (command.Contains('='))
            {
                //might be set...
                var upTo = command.Remove(command.IndexOf('=')).Replace(" ", "");
                var toDo = command.Remove(0, command.IndexOf('=')+1);
                if (Params.ContainsKey(upTo))
                {
                    Params[upTo] = Do(toDo);
                    return Params[upTo].Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                }
                else if (Variables.ContainsKey(upTo))
                {
                    Variables[upTo] = Do(toDo);
                    return Variables[upTo].Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                }
                else if (GlobalVariables.ContainsKey(upTo))
                {
                    GlobalVariables[upTo] = Do(toDo);
                    return GlobalVariables[upTo].Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                }
            }
 
            //this is some sort of a combination between functions. [worst case]
            //e.g: print[2+s*(2+k[])]
            //therefore, we will treat whole functions
            var e = Expression.Parse(command, this).Simplify();
            var l = e is Operation ? (e as Operation).AllNumbers().Cast<Expression>().ToList() : new List<Expression>(new Expression[] { e });
            var sum = new List<Expression>();
            if (l.Count == 1)
            {
                if (l[0] is Number)
                {
                    var c = l[0] as Number;
                    if (c.MultipliedBy.Count == 0)
                        return c;
                    Expression collector = new Number(1);
                    foreach (var v in c.MultipliedBy)
                    {
                        if (!v.Contains('['))
                        {
                            collector = collector.Multiply(new Number(1, new List<string>(new string[] { v }))).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                            continue;
                        }
                        var name = v.Remove(v.IndexOf('['));
                        var parametersT = NiceSplit(v.Substring(v.IndexOf('[') + 1, v.Length - v.IndexOf('[') - 2), ',');
                        var parameters = new Dictionary<string, Expression>(parametersT.Count + 1);
                        if (name.ToLower() == "if")
                        {
                            if (parametersT.Count <= 1)
                                return new Number(0);
                            var tmp = (this.Do(parametersT[0]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify());
                            if (!(tmp is Number))
                                return new Number(0);
                            if ((int)(tmp as Number).Value != 0)
                                this.Do(parametersT[1]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                            return new Number(1);
                        }
                        if (name.ToLower() == "ifnot")
                        {
                            if (parametersT.Count <= 1)
                                return new Number(0);
                            var tmp = (this.Do(parametersT[0]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify());
                            if (!(tmp is Number))
                                return new Number(0);
                            if ((int)(tmp as Number).Value == 0)
                                this.Do(parametersT[1]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
                            return new Number(1);
                        }
                        for (var i = 0; i < parametersT.Count; ++i)
                            parameters.Add("_" + i, this.Do(parametersT[i]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify());
                        if (name == "print")
                        {
                            var sb = new StringBuilder();
                            foreach (var k in parameters)
                                sb.AppendFormat("{0}\n", k.Value.ToString().Replace("(", "").Replace(")", ""));
                            Console.Write(sb);
                        }
                        else if (name == "printStr")
                        {
                            var sb = new StringBuilder();
                            foreach (var k in parameters)
                            {
                                if (k.Value is Number)
                                    sb.AppendFormat("'{0}'\n",(char)((k.Value as Number).Value));
                                else if (!(k.Value is Collection))
                                    sb.AppendFormat("{0}\n", k.Value.ToString().Replace("(", "").Replace(")", ""));
                                else
                                {
                                    sb.Append("\"");
                                    foreach (var a in (k.Value as Collection).Items)
                                    {
                                        if (a is Number)
                                            sb.AppendFormat("{0}", (char)((a as Number).Value));
                                        else
                                            sb.AppendFormat("{0}", a);
                                    }
                                    sb.Append("\"\n");
                                }
                            }
                            Console.Write(sb);
                        }
                        else if (name == "square")
                        {
                            if (parameters.Count == 0)
                                collector = collector.Multiply(new Number(0)).Simplify();
                            if (parameters.Count >= 1)
                                collector = collector.Multiply(parameters["_0"].Clone().Multiply(parameters["_0"]).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify());
                        }
                        else if (name == "isEqual")
                        {
                            if (parameters.Count != 2)
                                return new Number(0);
                            return parameters["_0"].EqualTo(parameters["_1"]) ? new Number(1) : new Number(0);
                        }
                        else if (name == "elementAt")
                        {
                            if (parameters.Count >= 1 && !(parameters["_0"] is Collection))
                                return parameters["_0"];
                            if (parameters.Count <= 1 || !(parameters["_0"] is Collection) || !(parameters["_1"] is Number))
                                return new Number(0);
                            return (parameters["_0"] as Collection).Items[(int)((parameters["_1"] as Number).Value)];
                        }
                        else if (name == "setAt")
                        {
                            if (parameters.Count < 3 || !(parameters["_1"] is Number) || !(parameters["_0"] is Collection))
                                return new Number();
                            var a = (int)(parameters["_1"] as Number).Value;
                            (parameters["_0"] as Collection).Items[a] = parameters["_2"];
                            return parameters["_1"];
                        }
                        else if (name == "removeAt")
                        {
                            if (parameters.Count < 2 || !(parameters["_1"] is Number) || !(parameters["_0"] is Collection))
                                return new Number();
                            var a = (int)(parameters["_1"] as Number).Value;
                            (parameters["_0"] as Collection).Items.RemoveAt(a);
                            return parameters["_0"];
                        }
                        else if (name == "count")
                        {
                            if (parameters.Count < 1 || !(parameters["_0"] is Collection))
                                return new Number();
                            return new Number((parameters["_0"] as Collection).Items.Count);
                        }
                        else if (name == "add")
                        {
                            if (parameters.Count < 2)
                                return new Number();
                            if (!(parameters["_0"] is Collection))
                                return new Collection(new List<Expression>(new Expression[] { parameters["_0"], parameters["_1"] }));
                            (parameters["_0"] as Collection).Items.Add(parameters["_1"]);
                            return parameters["_0"];
                        }
                        else if (name == "copy")
                        {
                            if(parameters.Count < 1)
                                return new Number();
                            return parameters["_0"].Clone();
                        }
                        else if (name == "compare")
                        {
                            if (parameters.Count != 2)
                                return new Number(0);
                            var one = parameters["_0"];
                            var two = parameters["_1"];
                            if (one.EqualTo(two))
                                return new Number(0);
                            if (!(one is Number && two is Number) || !Expression.SameType(one, two))
                                return new Number(0);
                            return (one as Number).Value - (two as Number).Value > 0 ? new Number(1) : new Number(-1);
                        }
                        else if (name == "digits")
                        {
                            if (parameters.Count == 0 || !(parameters["_0"] is Number))
                                return new Number(0);
                            var value = Math.Abs((int)((parameters["_0"] as Number).Value));
                            var col = new Collection();
                            while (value > 0)
                            {
                                col.Items.Add(new Number(value % 10));
                                value /= 10;
                            }
                            col.Items.Reverse();
                            return col;
                        }
                        else if (name == "goto")
                        {
                            if (parameters.Count == 0)
                                Console.WriteLine("goto must receive atleast one parameter, and it will use the VALUE of the FIRST NUMBER, FIRST PARAMETER.");
                            else
                            {
                                var p = parameters["_0"];
                                if (p is Number)
                                    this.CurrentLine = (int)((p as Number).Value) - 1;
                                else if (p is Collection)
                                {
                                    Console.WriteLine("Can't goto a Collection...");
                                }
                                else if (p is Operation)
                                {
                                    var P = (p as Operation).AllNumbers();
                                    if (P[0] is Operation)
                                        Console.WriteLine("Can't goto a Collection...");
                                    else
                                        this.CurrentLine = (int)((P[0] as Number).Value) - 1;
                                }
                                return new Number(0);
                            }
                        }
                        else if (Functions.ContainsKey(name))
                        {
                            var f = Functions[name];
                            f.Params = parameters;
                            f.Run();
                            f.Finished = false;
                            var retValue = f.ReturnValue == null ? new Number(0) : f.ReturnValue.Clone();
                            collector = collector.Multiply(retValue.Multiply(new Number(c.Value)).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify());
                        }
                        else
                            Console.WriteLine("No such function as '{0}'", name);
                    }
                    return collector.Multiply(new Number(c.Value));
                }
                else
                    return l[0];
            }
            else foreach (var v in l)
                {
                    if (!(v is Number))
                    {
                        sum.Add(v);
                        continue;
                    }
                    var V = v as Number;
                    if (V.MultipliedBy.Count == 0)
                    {
                        sum.Add(V);
                        continue;
                    }
                    Expression MulMe = new Number(V.Value);
                    for (var I = 0; I < V.MultipliedBy.Count; ++I)
                    {
                        if (!V.MultipliedBy[I].Contains('['))
                            MulMe = MulMe.Multiply(new Number(1, new List<string>(new string[] { V.MultipliedBy[I] }))).Simplify();
                        else
                        {
                            var tmp = Do(V.MultipliedBy[I]).Multiply(new Number(1)).Simplify();
                            MulMe = MulMe.Multiply(tmp).Simplify();
                        }
                    }
                    if (MulMe is Operation)
                        sum.AddRange((MulMe as Operation).AllNumbers());
                    else
                        sum.Add(MulMe);
                }
            return Operation.FromList(sum).Simplify().SolveFor(this.Params).Simplify().SolveFor(this.Variables).Simplify().SolveFor(this.GlobalVariables).Simplify();
        }
        private string TakeOutStrings(string s)
        {
            if (!s.Contains("\""))
                return s;
            for (var i = 0; i < s.Length; ++i)
            {
                if (s[i] == '\"')
                {
                    var arr = new Collection();
                    var start = i;
                    var end = s.IndexOf('\"', start + 1);
                    string output;
                    if (start == end - 1)
                        output = "";
                    else
                        output = s.Substring(start + 1, end - start - 1);
                    foreach (var c in output)
                        arr.Items.Add(new Number((int)c));
                    this.Variables.Add("_str" + this.stringCount, arr);
                    s = s.Replace("\"" + output + "\"", "_str" + this.stringCount);
                    ++this.stringCount;
                    i = 0;
                }
            }
            return s;
        }
        private void TakeOutStrings()
        {
            for (var i = 0; i < this.Commands.Length; ++i)
                this.Commands[i] = TakeOutStrings(this.Commands[i]);
        }
        public void Run()
        {
            if (!ranOnce)
                this.TakeOutStrings();
            else
                this.CurrentLine = 0;
            ranOnce = true;
            for (; this.CurrentLine< this.Commands.Length && !this.Finished; ++this.CurrentLine)
            {
                Do(this.Commands[this.CurrentLine]);
            }
        }
        public static CodeBlock Factory()
        {
            var c = new CodeBlock();
            c.Functions = new Dictionary<string, CodeBlock>();
            c.Params = new Dictionary<string, Expression>();
            c.Variables = new Dictionary<string, Expression>();
            c.GlobalVariables = new Dictionary<string, Expression>();
            c.Finished = false;
            c.Name = "function";
            c.CurrentLine = 0;
            return c;
        }
    }
    public class FullProgram
    {
        public Dictionary<string, CodeBlock> Functions;
        public Dictionary<string, Expression> GlobalVariables;
 
        public static FullProgram Parse(string[] lines)
        {
            var fp = new FullProgram();
            fp.Functions = new Dictionary<string, CodeBlock>();
            fp.GlobalVariables = new Dictionary<string, Expression>();
            //so, syntax rules:
            //Function *NAME* <- delcare a function
            //return value & parameters are dynamic
            //single { & } for enclosment.
            //thats it for now...
            for (var i = 0; i < lines.Length; ++i)
            {
                var clearLine = lines[i].TrimStart(new char[] { ' ' });
                if (!clearLine.StartsWith("function"))
                    continue;
                if (clearLine.Length == 8)
                    continue;
                var name = clearLine.Substring(clearLine.IndexOf(' ') + 1).Replace(" ", "");
                if (i == lines.Length)
                    break;
                while (lines[i].TrimStart(new char[] { ' ' }) != "{")
                    ++i;
                ++i;
                var count = 1;
                var start = i;
                for (; i < lines.Length && count != 0; ++i)
                    if (lines[i].TrimStart(new char[] { ' ' }) == "{")
                        ++count;
                    else if (lines[i].TrimStart(new char[] { ' ' }) == "}")
                        --count;
                var end = i - 1;
                var nLines = lines.Skip(start).Take(end - start); //lol
                var cb = CodeBlock.Factory();
                cb.Name = name;
                cb.Commands = nLines.ToArray();
                fp.Functions.Add(name, cb);
                --i;
            }
            for (var i = 0; i < fp.Functions.Count; ++i)
            {
                for (var j = 0; j < fp.Functions.Count; ++j)
                    if (j == i)
                        continue;
                    else
                        fp.Functions.Values.ElementAt(i).Functions.Add(fp.Functions.Values.ElementAt(j).Name, fp.Functions.Values.ElementAt(j));
            }
            return fp;
        }
 
        public Expression Go()
        {
            if (!Functions.ContainsKey("main"))
                throw new Exception("No 'main' function...");
            var main = Functions["main"];
            main.Run();
            return main.ReturnValue;
        }
    }
            //todo: Division simplification: common divison, factor, elimination.
    public abstract class Expression
    {
        public static readonly string Operators = @"+-*/";
        public abstract Expression Simplify();
        public abstract Expression SolveFor(Dictionary<string, Expression> Data);
        public abstract Expression Clone();
        public abstract bool EqualTo(Expression e);
        public abstract Expression Multiply(Expression e);
        public abstract Expression Divide(Expression e);
        public static string AddMul(string s)
        {
            for (var i = 1; i < s.Length; ++i)
                if (s[i] == '(' && !@"+-/*".Contains(s[i - 1]))
                    s = s.Insert(i, "*");
            return s;
        }
        public static string RemovePointlessBrackers(string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;
            if (s[0] == '(' && s.Last() == ')')
            {
                var c = 1;
                var i = 1;
                for (; i < s.Length && c != 0; ++i)
                    if (s[i] == '(')
                        ++c;
                    else if (s[i] == ')')
                        --c;
                if(c == 0 && i == s.Length)
                    return s.Substring(1, s.Length - 2);
            }
            return s;
        }
        public static string AddBrackets(string s)
        {
            //because the Scripting engine wants me to treat k[2+2*x] as a whole variable, and not convert to k[2+(2*x)], I must act now.
            int num = 0;
            foreach (char ch in @"+-*/")
            {
                num += TrueCount(s, ch);
            }
            if (num < 2)
                return s;
            List<int> list = new List<int>();
            for (int index = 0; index < s.Length; ++index)
            {
                if (s[index] == '[')
                {
                    var ccc = 1;
                    ++index;
                    for (; index < s.Length && ccc > 0; ++index)
                        if (s[index] == '[')
                            ++ccc;
                        else if (s[index] == ']')
                            --ccc;
                    --index;
                }
                if (index == s.Length)
                    break;
                if ((int)s[index] == 42 || (int)s[index] == 47)
                    list.Add(index);
            }
            list.Reverse();
            for (int index1 = 0; index1 < list.Count; ++index1)
            {
                int startIndex = list[index1] + 1;
                var ccc = 0;
                for (int index2 = 0; startIndex < s.Length && ((index2 != 0 || !Enumerable.Contains<char>((IEnumerable<char>)"+-*/", s[startIndex])) && (index2 != 0 || (int)s[startIndex] != 41)); ++startIndex)
                {
                    if ((int)s[startIndex] == 40)
                        ++index2;
                    else if ((int)s[startIndex] == 41)
                        --index2;
                    if (s[startIndex] == '[')
                    {
                        ++ccc;
                        ++startIndex;
                    }
                    for (; startIndex < s.Length && ccc > 0; ++startIndex)
                        if (s[startIndex] == '[')
                            ++ccc;
                        else if (s[startIndex] == ']')
                        {
                            --ccc;
                            if (ccc == 0)
                                --startIndex;
                        }
                }
                s = s.Insert(startIndex, ")");
                int index3 = list[index1] - 1;
                for (int index2 = 0; index3 >= 0 && ((index2 != 0 || !Enumerable.Contains<char>((IEnumerable<char>)"+-*/", s[index3])) && (index2 != 0 || (int)s[index3] != 40)); --index3)
                {
                    if ((int)s[index3] == 41)
                        ++index2;
                    else if ((int)s[index3] == 40)
                        --index2;
                    if (s[index3] == ']')
                    {
                        --ccc;
                        --index3;
                    }
                    else if (s[index3] == '[')
                    {
                        ++ccc;
                        --index3;
                    }
                    for (; index3 >= 0 && ccc != 0; --index3)
                        if (s[index3] == '[')
                            ++ccc;
                        else if (s[index3] == ']')
                        {
                            --ccc;
                            if (ccc == 0)
                                --index3;
                        }
                }
                s = index3 != -1 ? s.Insert(index3 + 1, "(") : "(" + s;
            }
            return s;
        }
 
        public static Expression Parse(string input, CodeBlock papa = null)
        {
            if (string.IsNullOrEmpty(input))
                return new Number(0);
            input = input.Replace(" ", "");
            if (input[0] == '{')
            {
                var count = 1;
                var next = 1;
                for (; next < input.Length && count != 0; ++next)
                    if (input[next] == '{')
                        ++count;
                    else if (input[next] == '}')
                        --count;
                --next;
                var arr = CodeBlock.NiceSplit(input.Remove(next).Remove(0, 1), ',', '{', '}');
                var coll = new Collection();
                foreach (var a in arr)
                {
                    if(papa == null)
                        coll.Items.Add(Expression.Parse(a));
                    else
                        coll.Items.Add(papa.Do(a)); //DOUBLE RECURSION!
                }
                if (next + 1 >= input.Length)
                    return coll;
                else
                    return new Operation(coll, Expression.Parse(input.Substring(next + 2), papa), (Op)Expression.Operators.IndexOf(input[next + 1]));
            }
            input = Expression.AddBrackets(input);
            input = Expression.RemovePointlessBrackers(input);
            if (TrueCount(input, '[') == 0 && Expression.IndexOfOperator(input, 0) == -1)
            {
                int result = 0;
                if (int.TryParse(input, out result))
                    return new Number(result);
                return (Expression)new Number(1.0, new List<string>(new string[1] { input }));
            }
            else
            {
                int start = 0;
                if (input[start] == '(')
                {
                    int num1 = start;
                    int num2 = 1;
                    int index1 = num1 + num2;
                    int num3 = num1;
                    for (int COUNT = 1; index1 < input.Length && COUNT != 0; ++index1)
                    {
                        if (input[index1] == '(')
                            ++COUNT;
                        else if (input[index1] == ')')
                            --COUNT;
                    }
                    Expression Left = Expression.Parse(input.Substring(num3 + 1, index1 - num3 - 2), papa);
                    if (index1 == input.Length)
                        return Left;
                    string str1 = Expression.Operators;
                    string str2 = input;
                    int index3 = index1;
                    int num4 = 1;
                    int num5 = index3 + num4;
                    int num6 = (int)str2[index3];
                    Op Type = (Op)str1.IndexOf((char)num6);
                    int num7 = num5;
                    int num8 = 1;
                    int num9 = num7 + num8;
                    int count = num7;
                    Expression Right = Expression.Parse(input.Remove(0, count));
                    return (Expression)new Operation(Left, Right, Type);
                }
                else if (Expression.IndexOfOperator(input, start) != -1)
                {
                    int length = Expression.IndexOfOperator(input, start);
                    return (Expression)new Operation(Expression.Parse(input.Substring(0, length), papa), Expression.Parse(input.Remove(0, length + 1), papa), (Op)Expression.Operators.IndexOf(input[length]));
                }
                else
                    return new Number(1, new List<string>(new string[] { input }));
            }
        }
        public static Dictionary<string, Expression> ParseInputs(string input)
        {
            //syntax:
            //NAME => DATA, NAME => DATA
            //y => 2+x, x => i+2, i => 5
            if (string.IsNullOrEmpty(input))
                return new Dictionary<string, Expression>();
            var parts = input.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var dic = new Dictionary<string, Expression>();
            foreach (var part in parts)
            {
                var tmp = part.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
                var name = tmp[0];
                var value = tmp[1];
                dic.Add(name, Expression.Parse(value));
            }
            return dic;
        }
        public static void SimplifyDictionary(Dictionary<string, Expression> d)
        {
            for (var i = 0; i < d.Count; ++i)
            {
                var t = d.ElementAt(i);
                d.Remove(t.Key);
                d.Add(t.Key, t.Value.Simplify().SolveFor(d).Simplify());
            }
        }
        public static string ExpressDic(Dictionary<string, Expression> d)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < d.Count; ++i)
                sb.AppendFormat("{0} => {1}{2}", d.ElementAt(i).Key, d.ElementAt(i).Value, i == d.Count - 1 ? "" : ", ");
            return sb.ToString();
        }
        static public bool SameType(Expression a, Expression b)
        {
            if (a is Number && b is Number)
            {
                //must be simplified!
                var A = a as Number;
                var B = b as Number;
                if (A.DividedBy.Count != B.DividedBy.Count || A.MultipliedBy.Count != B.MultipliedBy.Count)
                    return false;
                for (var i = 0; i < A.DividedBy.Count; ++i)
                    if (A.DividedBy[i] != B.DividedBy[i])
                        return false;
                for (var i = 0; i < A.MultipliedBy.Count; ++i)
                    if (A.MultipliedBy[i] != B.MultipliedBy[i])
                        return false;
                return true;
            }
            return false;
        }
        static private int TrueCount(string s, char of)
        {
            var sum = 0;
            for(var i = 0; i < s.Length; ++i)
            {
                if(s[i] == of)
                    ++sum;
                else if (s[i] == '[')
                {
                    ++i;
                    var count = 1;
                    for (; i < s.Length && count > 0; ++i)
                    {
                        if (s[i] == '[')
                            ++count;
                        else if (s[i] == ']')
                            --count;
                    }
                    --i;
                }
            }
            return sum;
        }
        static private int IndexOfOperator(string s, int start)
        {
            for (var i = start; i < s.Length; ++i)
                if (Operators.Contains(s[i]))
                    return i;
                else if (s[i] == '[')
                {
                    ++i;
                    var c = 1;
                    for (; i < s.Length && c > 0; ++i)
                        if (s[i] == '[')
                            ++c;
                        else if (s[i] == ']')
                            --c;
                    --i;
                }
            return -1;
        }
    }
 
    public enum Op { Add, Sub, Mul, Div };
 
    public class Operation : Expression
    {
        public Op Type;
        public Expression Left, Right;
 
        public Operation(Expression Left = null, Expression Right = null, Op Type = Op.Add)
        {
            this.Left = Left;
            this.Right = Right;
            this.Type = Type;
        }
 
        public override Expression Divide(Expression e)
        {
            this.Left = this.Left.Divide(e.Clone());
            this.Right = this.Right.Divide(e.Clone());
            return this.Simplify();
        }
        public override Expression Multiply(Expression e)
        {
            this.Left = this.Left.Multiply(e.Clone());
            this.Right = this.Right.Multiply(e.Clone());
            return this.Simplify();
        }
 
        public List<Expression> AllNumbers()
        {
            var l = new List<Expression>();
            if (this.Left is Number || this.Left is Collection)
                l.Add(this.Left);
            else
                l.AddRange((this.Left as Operation).AllNumbers());
 
            if (this.Right is Number || this.Right is Collection)
                l.Add(this.Right);
            else
                l.AddRange((this.Right as Operation).AllNumbers());
 
            return l;
        }
        public static Expression FromList(List<Number> l)
        {
            if (l.Count == 0)
                return new Number(0);
            else if (l.Count == 1)
                return l[0];
            else if (l.Count == 2)
                return new Operation(l[0], l[1], Op.Add);
            else
            {
                var t = l[0];
                l.RemoveAt(0);
                return new Operation(t, FromList(l), Op.Add);
            }
        }
        public static Expression FromList(List<Expression> l)
        {
            if (l.Count == 0)
                return new Number(0);
            else if (l.Count == 1)
                return l[0];
            else if (l.Count == 2)
                return new Operation(l[0], l[1], Op.Add);
            else
            {
                var t = l[0];
                l.RemoveAt(0);
                return new Operation(t, FromList(l), Op.Add);
            }
        }
        public static int CanBeDividedBy(List<Number> me, string byMe)
        {
            //the list is to represent a list of multipliers [so it's enough that one divisible by some part, and another is divisible by another part]
            for (var i = 0; i < me.Count; ++i)
            {
                if (me[i].MultipliedBy.Count == 0)
                    continue;
                if (me[i].MultipliedBy.Count == 1 && me[i].MultipliedBy[0] == byMe)
                    return i;
            }
            return -1;
        }
        public static List<List<Number>> Factor(List<List<Number>> l)
        {
            //hmm... CanBeDividedBy method.
            //the dividor must be a Number.
            //this also doesn't matter because it can't be anything else.
            var factorized = new List<List<Number>>();
            var allVariables = new List<string>();
            foreach (var a in l)
                foreach (var b in a)
                    if (b.MultipliedBy.Count == 1 && !allVariables.Contains(b.MultipliedBy[0]))
                        allVariables.Add(b.MultipliedBy[0]);
            //lol, nested foreach's are cool
            
            return factorized;
        }
        public Expression SimplifyDivs()
        {
            throw new NotImplementedException("DONT! Wait for it...");
        }
        public override Expression Simplify()
        {
            //three steps:
            //multiply / devide
            //convert all subs to adds
            //add same-types
            //fin
            this.Left = this.Left.Simplify().Clone();
            this.Right = this.Right.Simplify().Clone();
            if (this.Type == Op.Add || this.Type == Op.Sub)
            {
                if (this.Left.EqualTo(new Number(0)))
                    return this.Right;
                if(this.Right.EqualTo(new Number(0)))
                    return this.Left;
            }
            if (this.Right is Collection && this.Left is Collection && this.Type == Op.Add)
            {
                var newRight = this.Left.Clone() as Collection;
                foreach (var t in (this.Right as Collection).Items)
                    newRight.Items.Add(t);
                return newRight.Simplify();
            }
            if (this.Left is Collection || this.Right is Collection)
                return this;
            if (this.Type == Op.Mul)//first step
                return this.Left.Multiply(this.Right).Simplify();
            if (this.Type == Op.Div)
                return this.Left.Divide(this.Right).Simplify();
 
 
            if (this.Type == Op.Add)//seconds & third
            {
                if (Expression.SameType(this.Left, this.Right))
                    return new Number((this.Left as Number).Value + (this.Right as Number).Value, (this.Left as Number).MultipliedBy, (this.Left as Number).DividedBy);
            }
            if (this.Type == Op.Sub)
            {
                if (Expression.SameType(this.Left, this.Right))
                    return new Number((this.Left as Number).Value - (this.Right as Number).Value, (this.Left as Number).MultipliedBy, (this.Left as Number).DividedBy);
                this.Type = Op.Add;
                this.Right = this.Right.Multiply(new Number(-1));
            }
            //forth step; convert to array [all relationships are addition]
            var exps = new List<Expression>();
            if (this.Left is Number)
                exps.Add(this.Left as Number);
            else
                exps.AddRange((this.Left as Operation).AllNumbers());
            if (this.Right is Number)
                exps.Add(this.Right as Number);
            else
                exps.AddRange((this.Right as Operation).AllNumbers());
            for (var i = 0; i < exps.Count; ++i)
                for (var j = i + 1; j < exps.Count; ++j)
                    if (Expression.SameType(exps[i], exps[j]))
                    {
                        (exps[i] as Number).Value += (exps[j] as Number).Value;
                        exps.RemoveAt(j);
                        --j;
                    }
            if (exps.Count == 1)
                return exps[0];
            return FromList(exps);
        }
        public override Expression SolveFor(Dictionary<string, Expression> Data)
        {
            //MUST be simplified, aka only ADD operations. else, results would suck.
            //does NOT simplify by itself. you need to do it by calling Simplify().
            if (Data.Count == 0)
                return this.Clone();
            var nums = this.AllNumbers();
            var exps = new List<Expression>();
            var output = new List<Expression>();
            for (var i = 0; i < nums.Count; ++i)
                exps.Add(nums[i].SolveFor(Data).Simplify());
            foreach (var e in exps)
                if (e is Number || e is Collection)
                    output.Add(e);
                else
                    output.AddRange((e as Operation).AllNumbers());
            return FromList(output);
        }
        public override Expression Clone()
        {
            return new Operation(this.Left.Clone(), this.Right.Clone(), this.Type);
        }
        public override bool EqualTo(Expression e)
        {
            var E = e as Operation;
            if (E == null)
                return false;
            return E.Type == this.Type && this.Left.EqualTo(E.Left) && this.Right.EqualTo(E.Right);
        }
        public override string ToString()
        {
            return ToString(false);
        }
        public string ToString(bool ignoreBrackets = false)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("{0} {1} {2}", Left is Operation ? string.Format("({0})", Left) : Left.ToString(), OpToStr(Type), Right is Operation ? string.Format("({0})", Right) : Right.ToString());
            return ignoreBrackets ? sb.ToString().Replace("(", "").Replace(")", "") : sb.ToString();
        }
        public string OpToStr(Op Type)
        {
            return Expression.Operators[(int)Type].ToString();
        }
    }
    public class Number : Expression
    {
        public double Value;
        public List<string> MultipliedBy;
        public List<Expression> DividedBy;
 
        public Number(double Value = 0, List<string> MultipliedBy = null, List<Expression> DividedBy = null)
        {
            this.Value = Value;
            if (MultipliedBy == null)
                this.MultipliedBy = new List<string>();
            else
                this.MultipliedBy = MultipliedBy;
            if (DividedBy == null)
                this.DividedBy = new List<Expression>();
            else
                this.DividedBy = DividedBy;
        }
 
        public override Expression Multiply(Expression e)
        {
            if (this.Value == 0)
                return new Number(0);
            if (e is Number)
            {
                var E = e as Number;
                this.Value *= E.Value;
                if (this.Value == 0)
                    return new Number(0);
                this.MultipliedBy.AddRange(E.MultipliedBy);
                this.DividedBy.AddRange(E.DividedBy);
                return this;
            }
            return e.Multiply(this);
        }
        public override Expression Divide(Expression e)
        {
            if (this.Value == 0)
                return new Number(0);
            this.DividedBy.Add(e.Clone().Simplify());
            return this.Simplify();
        }
 
        public int SearchByName(string s)
        {
            for(var i = 0; i < this.DividedBy.Count; ++i)
                if(this.DividedBy[i] is Number && (this.DividedBy[i] as Number).MultipliedBy.Remove(s))
                    return i;
            return -1;
        }
        public override Expression Simplify()
        {
            //eliminating equal values in DIVIDEDBY and MULTIPLIEDBY
            //sorting
            if (this.Value == 0)
                return new Number(0);
            for (var i = 0; i < this.MultipliedBy.Count; ++i)
            {
                int tmp = this.SearchByName(this.MultipliedBy[i]);
                if (tmp != -1)
                {
                    this.MultipliedBy.RemoveAt(i);
                    --i;
                }
            }
            this.MultipliedBy.Sort();
            //also, a/(b/c) => (ac)/b
            var divisors = new List<Expression>();
            foreach (var div in this.DividedBy)
            {
                if (div is Number)
                {
                    var D = div as Number;
                    divisors.Add(D);
                    while (D.DividedBy.Count > 0)
                    {
                        divisors.Add(D.DividedBy[0].Simplify());
                        D.DividedBy.RemoveAt(0);
                    }
                }
                else
                {
                    divisors.Add(div);
                }
            }
            while(divisors.Count > 2)
            {
                var i = divisors.Count - 3;
                divisors[i] = divisors[i].Multiply(divisors[i + 2].Clone().Simplify());
                divisors.RemoveAt(i + 2);
            }
            if (divisors.Count == 2)
            {
                var t = divisors.Last();
                this.DividedBy = new List<Expression>(new Expression[] { divisors[0] });
                return this.Multiply(t);
            }
            this.DividedBy = divisors;
            return this;
        }
        public override Expression SolveFor(Dictionary<string, Expression> Data)
        {
            //condition: must be simplified. don't know how will work for unsimplified data.
            //todo: (new) divide!
            if (Data.Count == 0)
                return this.Clone();
            Expression n = this.Clone() as Number;
            foreach (var s in Data)
            {
                if (n is Number)
                {
                    var N = n as Number;
                    if (N.MultipliedBy.Contains(s.Key))
                    {
                        N.MultipliedBy.Remove(s.Key);
                        if (s.Value is Collection)
                        {
                            n = s.Value.Multiply(n);
                        }
                        else
                            n = N.Multiply(s.Value.Clone()).Simplify().SolveFor(Data).Simplify();
                    }
                }
                else if (n is Operation)
                {
                    var N = n as Operation;
                    var j = N.AllNumbers().Cast<Expression>().ToList();
                    for (var i = 0; i < j.Count; ++i)
                        j[i] = j[i].SolveFor(Data).Simplify();
                    n = Operation.FromList(j);
                }
            }
            return n;
        }
        public override Expression Clone()
        {
            return new Number(this.Value, this.MultipliedBy.ToList(), this.DividedBy.ToList());
        }
        public override bool EqualTo(Expression e)
        {
            var E = e as Number;
            if (E == null)
                return false;
            if (this.Value == E.Value)
            {
                if (this.MultipliedBy.Count == E.MultipliedBy.Count && this.DividedBy.Count == E.DividedBy.Count)
                {
                    for (var i = 0; i < this.MultipliedBy.Count; ++i)
                        if (this.MultipliedBy[i] != E.MultipliedBy[i])
                            return false;
                    var lol = this.DividedBy.Count;
                    for (var i = 0; i < this.DividedBy.Count; ++i)
                        for(var j = i + 1; j < this.DividedBy.Count; ++j)
                            if (this.DividedBy[i].EqualTo(E.DividedBy[j]))
                            {
                                --lol;
                                break;
                            }
                    return lol == 0;
                }
            }
            return false;
        }
        public override string ToString()
        {
            if (this.Value == 0)
                return "0";
            var a = (this.Value == 1 ? (this.DividedBy.Count != 0 || this.MultipliedBy.Count != 0 ? "" : this.Value.ToString()) : this.Value.ToString());
            var simple = new Dictionary<string, int>();
            foreach (var m in this.MultipliedBy)
            {
                if (simple.ContainsKey(m))
                    simple[m]++;
                else
                    simple.Add(m, 1);
            }
            foreach (var s in simple)
                a = a + " * " + s.Key + (s.Value == 1 ? "" : "^" + s.Value);
            if (a.StartsWith(" * "))
                a = a.Remove(0, 3);
            return a;
        }
        public static string ListSum(List<string> l, string inBetween)
        {
            if(l == null || l.Count == 0)
                return "";
            if(l.Count == 1)
                return l[0];
            var sb = new StringBuilder(l[0]);
            for (var i = 1; i < l.Count; ++i)
                sb.AppendFormat("{0}{1}", inBetween, l[i]);
            return sb.ToString();
        }
        public static string ListSum(List<Expression> l, string inBetween)
        {
            if (l == null || l.Count == 0)
                return "";
            if (l.Count == 1)
                return l[0].ToString();
            var sb = new StringBuilder(l[0].ToString());
            for (var i = 1; i < l.Count; ++i)
                sb.AppendFormat("{0}{1}", inBetween, l[i]);
            return sb.ToString();
        }
    }
    public class Collection : Expression
    {
        public List<Expression> Items;
        public Expression Multiplier;
 
        public Collection(List<Expression> Items = null, Expression Multiplier = null)
        {
            this.Items = Items;
            this.Multiplier = Multiplier;
            if (this.Items == null)
                this.Items = new List<Expression>();
            if (this.Multiplier == null)
                this.Multiplier = new Number(1);
        }
 
        public override Expression Clone()
        {
            var c = new Collection();
            c.Multiplier = this.Multiplier.Clone();
            for (var i = 0; i < this.Items.Count; ++i)
                c.Items.Add(this.Items[i].Clone());
            return c;
        }
        public override Expression Simplify()
        {
            this.Multiplier = this.Multiplier.Simplify();
            for (var i = 0; i < this.Items.Count; ++i)
                this.Items[i] = this.Items[i].Simplify();
            return this;
        }
        public override bool EqualTo(Expression e)
        {
            if (e is Collection)
            {
                var E = e as Collection;
                if (E.Items.Count != this.Items.Count)
                    return false;
                var NEW = this.Items.ToList();
                bool flag = false;
                for (var i = 0; i < E.Items.Count; ++i)
                {
                    flag = false;
                    for (var j = 0; j < NEW.Count; ++j)
                        if (NEW[j].EqualTo(E.Items[i]))
                        {
                            NEW.RemoveAt(j);
                            flag = true;
                            break;
                        }
                    if (!flag)
                        return false;
                }
                return true;
            }
            else if (this.Items.Count == 1)
                return e.EqualTo(this.Items[0]);
            return false;
        }
        public override Expression Divide(Expression e)
        {
            this.Multiplier = this.Multiplier.Divide(e).Simplify();
            return this;
        }
        public override Expression Multiply(Expression e)
        {
            this.Multiplier = this.Multiplier.Multiply(e).Simplify();
            return this;
        }
        public override Expression SolveFor(Dictionary<string, Expression> Data)
        {
            this.Multiplier = this.Multiplier.SolveFor(Data).Simplify();
            for (var i = 0; i < this.Items.Count; ++i)
                this.Items[i] = this.Items[i].SolveFor(Data).Simplify();
            return this;
        }
        public override string ToString()
        {
            if (this.Items.Count == 0)
                return "{ }";
            if (this.Items.Count == 1)
                return "{ " + this.Items[0] + " }";
            var sb = new StringBuilder();
            sb.Append("{ " + this.Items[0]);
            for (var i = 1; i < this.Items.Count; ++i)
                sb.AppendFormat(" ,{0}", this.Items[i]);
            sb.Append(" }");
            return sb.ToString();
        }
    }
        class Program
    {
        static void Main(string[] args)
        {
            while (true) 
            {
                Console.WriteLine("Will read lines until line will be XXX.");
                var curr = Console.ReadLine();
                var l = new List<string>();
                while (curr != "XXX")
                {
                    l.Add(curr);
                    curr = Console.ReadLine();
                }
                Console.Clear();
                foreach (var L in l)
                    Console.WriteLine(L);
                Console.WriteLine("\nParsing...");
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var fp = FullProgram.Parse(l.ToArray());
                sw.Stop();
                Console.WriteLine("Parsed in {0}ms. Executing...\n", sw.ElapsedMilliseconds);
                sw = System.Diagnostics.Stopwatch.StartNew();
                var output = fp.Go();
                Console.WriteLine("\n\nFinished executing in {0}ms. Return expression is {1}.",sw.ElapsedMilliseconds, output == null ? "null" : output.ToString());
                Console.ReadLine();
                Console.Clear();
            }
        }
    }
}
  • upload with new input
  • 結果: 執行期間發生錯誤(RE)     time: 5.99s    記憶體: 125696 kB     信號(Signal): 9 (SIGKILL)

    function main
    {
    var a = x
    a = a * x + x * (k + b) * (k + x*y*y*y)
    print[a]
    var x = 2
    print[a]
    var k = 3
    print[a]
    var b = 5
    print[a]
    var y = 123
    print[a]
    }
    XXX
    Will read lines until line will be XXX.
    function main
    {
    var a = x
    a = a * x + x * (k + b) * (k + x*y*y*y)
    print[a]
    var x = 2
    print[a]
    var k = 3
    print[a]
    var b = 5
    print[a]
    var y = 123
    print[a]
    }
    
    Parsing...
    Parsed in 14ms. Executing...
    
    x^2 + k^2 * x + k * x^2 * y^3 + b * k * x + b * x^2 * y^3
    4 + 2 * k^2 + 4 * k * y^3 + 2 * b * k + 4 * b * y^3
    22 + 12 * y^3 + 6 * b + 4 * b * y^3
    52 + 32 * y^3
    59547796
    
    
    Finished executing in 33ms. Return expression is null.
    Will read lines until line will be XXX.
    
LOOK!