-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththe_namingless_programming_language.cpp
1381 lines (1299 loc) · 44.4 KB
/
the_namingless_programming_language.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 <cassert>
#include <iostream>
#include <filesystem>
#include <limits>
#include <vector>
#include <string>
#include <deque>
#include <numeric>
#include <functional>
#include <fstream>
#include <streambuf>
using namespace std;
namespace Strings {
// split
vector<string> split(const string& Line, const string& Coma){
vector<string> ret;
size_t coma_pos = 0;
size_t coma_len = Coma.length();
while(coma_pos != string::npos){
size_t new_coma_pos = Line.find(Coma, coma_pos);
if(new_coma_pos != string::npos){
ret.push_back(Line.substr(coma_pos, new_coma_pos-coma_pos));
coma_pos = new_coma_pos+coma_len;
}else{
ret.push_back(Line.substr(coma_pos, string::npos));
coma_pos = string::npos;
}
}
return ret;
}
// join
struct join_with{
string operator()(const string& S1, const string& S2){
return (S1 + the_) + S2;
}
explicit join_with(const string& the) : the_(the){}
private:
string the_;
};
string join(const vector<string>& Lines, const string& Coma){
string ret;
if(!Lines.empty()){
join_with joiner = join_with(Coma);
ret = Lines.front() + accumulate(Lines.begin()+1, Lines.end(), string(), joiner);
return ret;
}
return ret;
}
// replace
string replace(const string& Line, const string& Coma1, const string& Coma2){
return join(split(Line, Coma1), Coma2);
}
}
namespace Numbers {
// unsigned calc routines
string ensure_point(const string& S){
if(S.find_first_of(',') == string::npos){
return S+',';
}else{
return S;
}
}
string ensure_minus(const string& S){
if(S[0] != '-'){
return "-"+S;
}else{
return S;
}
}
string ensure_zero(const string& S){
if(S == "-0"){
return "0";
}else{
return S;
}
}
string invert(const string& S){
if(S[0] != '-'){
return "-"+S;
}else{
return S.substr(1, string::npos);
}
}
void align_to_each_other_with_0(const string& S1, const string& S2, string& NS1, string& NS2){
size_t PL1 = S1.find_first_of(',');
size_t PR1 = S1.length() - PL1;
size_t PL2 = S2.find_first_of(',');
size_t PR2 = S2.length() - PL2;
size_t PLM = max(PL1, PL2);
size_t PRM = max(PR1, PR2);
NS1.assign(PLM+PRM+1, '0');
NS1.replace(PLM-PL1+1, S1.length(), S1);
NS2.assign(PLM+PRM+1, '0');
NS2.replace(PLM-PL2+1, S2.length(), S2);
}
string trim_left(const string& S){
string t = S.substr(S.find_first_not_of('0'), string::npos);
if(t == ""){
return "0";
}
if(t[0] == ','){
return "0"+t;
}
return t;
}
string trim_right(const string& S){
string t = S.substr(0, S.find_last_not_of('0') + 1);
if(t[t.length()-1] == ','){
return t.substr(0, t.length()-1);
}
return t;
}
string trim(const string& S){
string s_or_null = trim_right(trim_left(S));
if(s_or_null == ""){
return "0";
}else{
return s_or_null;
}
}
string us_add(const string& S1, const string& S2){
string EPS1 = ensure_point(S1);
string EPS2 = ensure_point(S2);
string AS1, AS2;
align_to_each_other_with_0(EPS1, EPS2, AS1, AS2);
deque<char> Res;
char Add = 0;
string::reverse_iterator It1 = AS1.rbegin();
string::reverse_iterator It2 = AS2.rbegin();
string::reverse_iterator ItE1 = AS1.rend();
for(; It1 != ItE1; ++It1, ++It2){
if(*It1 == ','){
Res.push_front(',');
}else{
char n = (*It1 -'0') + (*It2 -'0') + Add;
if(n > 9){
n -= 10;
Add = 1;
}else{
Add = 0;
}
Res.push_front(n + '0');
}
}
return trim( string(Res.begin(), Res.end()) );
}
string us_sub(const string& S1, const string& S2){
string EPS1 = ensure_point(S1);
string EPS2 = ensure_point(S2);
string AS1, AS2, Sign;
align_to_each_other_with_0(EPS1, EPS2, AS1, AS2);
if(AS1.compare(AS2) < 0){
AS1.swap(AS2);
Sign = "-";
}else{
Sign = "";
}
deque<char> Res;
char Sub = 0;
string::reverse_iterator It1 = AS1.rbegin();
string::reverse_iterator It2 = AS2.rbegin();
string::reverse_iterator ItE1 = AS1.rend();
for(; It1 != ItE1; ++It1, ++It2){
if(*It1 == ','){
Res.push_front(',');
}else{
char n = 10 + (*It1 -'0') - (*It2 -'0') - Sub;
if(n > 9){
n -= 10;
Sub = 0;
}else{
Sub = 1;
}
Res.push_front(n + '0');
}
}
return Sign + trim( string(Res.begin(), Res.end()) );
}
string us_mul1(const string& S, char C){
char c = C - '0';
string EPS = ensure_point(S);
string AS = "0"+EPS;
deque<char> Res;
char Add = 0;
string::reverse_iterator It = AS.rbegin();
string::reverse_iterator ItE = AS.rend();
for(; It != ItE; ++It){
if(*It == ','){
Res.push_front(',');
}else{
char n = (*It -'0') * c + Add;
Add = n / 10;
n = n % 10;
Res.push_front(n + '0');
}
}
return trim_right(string(Res.begin(), Res.end()));
}
string mul_10(const string& N){
size_t point = N.find_first_of(',');
if(point == string::npos){
return N+"0";
}else{
string M=N;
M[point] = M[point+1];
M[point+1] = ',';
return trim_right(M);
}
}
string mul_10(const string& N, unsigned int P){
if(P == 0){
return N;
}else{
return mul_10(mul_10(N), P-1);
}
}
string div_10(const string& N){
size_t point = N.find_first_of(',');
string M;
if(point == string::npos){
M = N + N[N.length()-1];
M[N.length()-1] = ',';
}else{
M = N;
M[point] = M[point-1];
M[point-1] = ',';
}
if(M[0]==','){
return "0"+M;
}else{
return M;
}
}
string div_10(const string& N, unsigned int P){
if(P == 0){
return N;
}else{
return div_10(div_10(N), P-1);
}
}
string us_mul(const string& S1, const string& S2){
if(S1 == "0" || S2 == "0"){
return "0";
}
if(S1.length() < S2.length()){
return us_mul(S2, S1);
}else{
size_t point = S2.find_first_of(',');
unsigned int shift;
if(point == string::npos){
shift = 0;
}else{
shift = S2.length() - point -1;
}
string CS2 = Strings::replace(S2, ".", "");
string::reverse_iterator It = CS2.rbegin();
string::reverse_iterator ItE = CS2.rend();
vector<string> to_sum;
to_sum.reserve(CS2.length());
for(unsigned int i = 0; It != ItE; ++It, ++i){
to_sum.push_back( mul_10( us_mul1(S1, *It), i));
}
return div_10( accumulate(to_sum.begin(), to_sum.end(), string("0"), us_add), shift);
}
}
bool int_less(const string& S1, const string& S2){
if(S1.length() < S2.length())return true;
if(S1.length() > S2.length())return false;
if(S1.compare(S2) < 0)return true;
return false;
}
string int_div(const string& S1, const string& S2){
if(int_less(S1, S2)){
return "0";
}
string dmuls[10];
dmuls[0] = "0";
for(unsigned int i = 1; i <= 9; i++){
dmuls[i] = us_mul1(S2, '0' + i);
}
string n = S1;
string res;
size_t l1 = S1.length();
size_t l2 = S2.length();
for(int i = l1-l2; i >= 0; i--){
int j = 9;
string to_subtract = "0";
for(; j > 0 ; j--){
to_subtract = trim_left(mul_10(dmuls[j], i));
if(!int_less(n, to_subtract)){
break;
}
}
if(j>0){
n = us_sub(n, to_subtract);
}
res += ('0'+j);
}
return trim_left(res);
}
string us_div(const string& S1, const string& S2){
size_t point1 = S1.find_first_of(',');
unsigned int after_point1 = (point1 == string::npos)? 0 : S1.length()-point1-1;
size_t point2 = S2.find_first_of(',');
unsigned int after_point2 = (point2 == string::npos)? 0 : S2.length()-point2-1;
unsigned int max_after_point = max(after_point1, after_point2);
string IS1 = mul_10(S1, max_after_point*2);
string IS2 = mul_10(S2, max_after_point);
return div_10( int_div(IS1, IS2), max_after_point);
}
// add sub mul div
string add(const string& S1, const string& S2){
if(S1 == "" || S2 == ""){
cerr << ("Bad arguments: for addition \"" + S1 + "\" + \"" + S2 + "\"\n");
}
bool SN1 = S1[0] == '-';
string US1 = SN1 ? S1.substr(1, string::npos) : S1;
bool SN2 = S2[0] == '-';
string US2 = SN2 ? S2.substr(1, string::npos) : S2;
if( !SN1 && !SN2 ){
return us_add(US1, US2);
}else if( SN1 && !SN2 ){
return us_sub(US2, US1);
}else if( !SN1 && SN2 ){
return us_sub(US1, US2);
}else{
return ensure_minus( us_add(US1, US2) );
}
}
string sub(const string& S1, const string& S2){
if(S1 == ""){
cerr << ("Bad arguments: for subtraction \"" + S1 + "\" - \"" + S2 + "\"\n");
}
bool SN1 = S1[0] == '-';
string US1 = SN1 ? S1.substr(1, string::npos) : S1;
string RS2 = (S2=="") ? "0" : S2; // unary fix
bool SN2 = RS2[0] == '-';
string US2 = SN2 ? RS2.substr(1, string::npos) : RS2;
if( !SN1 && !SN2 ){
return us_sub(US1, US2);
}else if( SN1 && !SN2 ){
return ensure_minus( us_add(US2, US1) );
}else if( !SN1 && SN2 ){
return us_add(US1, US2);
}else{
return ensure_zero( invert( us_sub(US1, US2) ) );
}
}
string mul(const string& S1, const string& S2){
if(S1 == "" || S2 == ""){
cerr << ("Bad arguments: for multiplication \"" + S1 + "\" * \"" + S2 + "\"\n");
}
if(S1 == "0" || S2 == "0"){
return "0";
}
bool SN1 = S1[0] == '-';
string US1 = SN1 ? S1.substr(1, string::npos) : S1;
bool SN2 = S2[0] == '-';
string US2 = SN2 ? S2.substr(1, string::npos) : S2;
if( (!SN1 && !SN2) || (SN1 && SN2) ){
return us_mul(US1, US2);
}else{
return ensure_minus( us_mul(US1, US2) );
}
}
string div(const string& S1, const string& S2){
if(S1 == "" || S2 == ""){
cerr << ("Bad arguments: for division \"" + S1 + "\" / \"" + S2 + "\"\n");
}
if(S1 == "0"){
return "0";
}
if(S2 == "0"){
cerr << ("Bad arguments: zero division in calculating: \"" + S1 + "\" / \"" + S2 + "\"\n");
}
bool SN1 = S1[0] == '-';
string US1 = SN1 ? S1.substr(1, string::npos) : S1;
bool SN2 = S2[0] == '-';
string US2 = SN2 ? S2.substr(1, string::npos) : S2;
if( (!SN1 && !SN2) || (SN1 && SN2) ){
return us_div(US1, US2);
}else{
return ensure_zero( ensure_minus( us_div(US1, US2) ) );
}
}
bool is_a_number(const string& S) {
if(S.empty())
return false;
if(S == "-")
return false;
if((S[0] >= '0' && S[0] <= '9') || S[0] == '-') { // starts with -, 0..9
auto comas = 0u;
for(size_t i = 1u; i < S.size(); ++i) {
if(S[i] == ',') {
comas++;
if(comas > 1)
return false;
} else if(S[i] >= '0' && S[i] <= '9') {
} else {
return false;
}
}
return true;
}
return false;
}
}
struct The {
bool is_leaf = true;
char leaf = 0x00;
vector<The> branches;
};
int rank_of(const The& could_be_a_tensor) {
if(could_be_a_tensor.is_leaf) // leaf
return 0;
if(could_be_a_tensor.branches.empty()) // empty list
return 1;
return 1 + rank_of(could_be_a_tensor.branches[0]); // tensor
}
string to_string(const The& a, int level = 0) {
if(a.is_leaf) // letter to string
return string() + a.leaf;
string output;
if(rank_of(a) == 1) { // padding
for(auto i = 0; i < level; ++i)
output += '\t';
}
for(const The& branch: a.branches) { // strings
output += to_string(branch, level + 1);
}
output += '\n';
return output;
}
string rank_1_to_string(const The& a) {
if(rank_of(a) != 1) {
cerr << "Argument error: rank of the string-convertible structure should be 1\n";
return string();
}
string output;
for(const auto& b: a.branches) {
output += b.leaf;
}
return output;
}
The string_to_rank_1(const string& s) {
The a;
a.is_leaf = false;
a.branches.resize(s.size());
for(auto i = 0u; i < s.size(); ++i) {
a.branches[i].leaf = s[i];
}
return a;
}
vector<string> rank_2_to_strings(const The& a) {
vector<string> output;
if(rank_of(a) != 2) {
cerr << "Argument error: rank of the string-array-convertible structure should be 2\n";
return output;
}
for(const auto& b: a.branches) {
output.push_back(rank_1_to_string(b));
}
return output;
}
The strings_to_rank_2(const vector<string>& ss) {
The a;
a.is_leaf = false;
a.branches.resize(ss.size());
for(auto i = 0u; i < ss.size(); ++i) {
a.branches[i] = string_to_rank_1(ss[i]);
}
return a;
}
The binary_rank_to_rank(const The& left, const The& right, int left_rank, int right_rank, function<The (const The&, const The&)> f2) {
if(rank_of(left) == left_rank && rank_of(right) == right_rank) {
return f2(left, right);
} else if(rank_of(left) == left_rank){
The a;
a.is_leaf = false;
for(auto b: right.branches) {
a.branches.push_back(binary_rank_to_rank(left, b, left_rank, right_rank, f2));
}
return a;
}else if(rank_of(right) == right_rank){
The a;
a.is_leaf = false;
for(auto b: left.branches) {
a.branches.push_back(binary_rank_to_rank(b, right, left_rank, right_rank, f2));
}
return a;
} else {
if(left.branches.size() != right.branches.size()) {
cerr << "Rank error in a binary operation: argument sizes don't match.\n";
return The();
}
const auto element_count = left.branches.size();
The a;
a.is_leaf = false;
for(auto i = 0; i < element_count; ++i) {
const auto lb = left.branches[i];
const auto rb = right.branches[i];
a.branches.push_back(binary_rank_to_rank(lb, rb, left_rank, right_rank, f2));
}
return a;
}
}
// for most operations like "+" or "split"
The binary_1_to_1(const The& left, const The& right, function<The (const The&, const The&)> f2) {
return binary_rank_to_rank(left, right, 1, 1, f2);
}
// for things like "join"
The binary_2_to_1(const The& left, const The& right, function<The (const The&, const The&)> f2) {
return binary_rank_to_rank(left, right, 2, 1, f2);
}
// exclusively V (filter)
The filtered(const The& left, const The& right) {
if(rank_of(left) == 2 && rank_of(right) == 2) {
The result;
result.is_leaf = false;
if(left.branches.size() != right.branches.size()) {
cerr << "Rank error in V: filter size don't match the size of the filtered array.\n";
return The();
}
for(size_t i = 0u; i < left.branches.size(); ++i) {
if(right.branches[i].branches.size() != 1) {
cerr << "Filter error in V: filter values shoud be either '0' or '1' exclusively. A value of rank " << rank_of(right.branches[i]) << " found instead.\n";
return The();
}
if(right.branches[i].branches[0].leaf == '1') {
result.branches.push_back(left.branches[i]);
} else if(right.branches[i].branches[0].leaf == '0') {
// don't push back
} else {
cerr << "Filter error in V: filter values shoud be either '0' or '1' exclusively. A value of '" << right.branches[i].branches[0].leaf << "' found instead.\n";
return The();
}
}
return result;
} else if(rank_of(left) == 2){
The a;
a.is_leaf = false;
for(auto b: right.branches) {
a.branches.push_back(filtered(left, b));
}
return a;
}else if(rank_of(right) == 2){
The a;
a.is_leaf = false;
for(auto b: left.branches) {
a.branches.push_back(filtered(b, right));
}
return a;
} else {
The a;
a.is_leaf = false;
for(auto lb: left.branches) {
The la;
la.is_leaf = false;
for(auto rb: right.branches) {
la.branches.push_back(filtered(lb, rb));
}
a.branches.push_back(la);
}
return a;
}
}
// unary operations
The unary_rank(const The& the, int rank, function<The (const The&)> f1) {
if(rank_of(the) == rank){
return f1(the);
} else {
The a;
a.is_leaf = false;
for(auto b: the.branches) {
a.branches.push_back(unary_rank(b, rank, f1));
}
return a;
}
}
// e.g. for file load
The unary_1(const The& the, function<The (const The&)> f1) {
return unary_rank(the, 1, f1);
}
// does all the tree consist of "Booleans"?
bool invertable(const The& t) {
if(t.is_leaf) {
if(t.leaf == '0' || t.leaf == '1')
return true;
else
return false;
} else {
for(auto& b: t.branches)
if(!invertable(b))
return false;
return true;
}
}
// invert all the tree (given that it does consist of "Booleans")
void invert(The& t) {
if(t.is_leaf) {
if(t.leaf == '1')
t.leaf = '0';
else
t.leaf = '1';
} else {
for(auto& b: t.branches)
invert(b);
}
}
// remove all but the targeted by index element for selected depth
void select_by_index_and_depth(The& the, int index, int depth) {
if(depth == 0) {
if(the.branches.size() <= index) {
cerr << "Index error: select by index and depth found a branch with not enough branches\n";
return;
}
the = the.branches[index];
}
for(auto& branch : the.branches)
select_by_index_and_depth(branch, index, depth-1);
}
void tests();
// dispatch the operation
void execute(The& left, The& right) {
if(left.branches.empty())
return; // we're done!
if(left.branches.back().is_leaf && left.branches.back().leaf == '_') {
left.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: the command has 0 no arguments\n";
return;
}
if(!right.branches.back().is_leaf) {
cerr << "Syntax error: a tensor can't be the command's argument\n";
return;
}
if(right.branches.back().leaf == '.') { // exit
right.branches.pop_back();
return;
}
// non-return operations
if(right.branches.back().leaf == 'U') { // underscore
right.branches.back().leaf ='_';
execute(left, right);
} else if(right.branches.back().leaf == 'Z') { // slash
right.branches.back().leaf ='/';
execute(left, right);
} else if(right.branches.back().leaf == 'N') { // backslash
right.branches.back().leaf ='\\';
execute(left, right);
} else if(right.branches.back().leaf == 'J') { // line break
right.branches.back().leaf = '\n';
execute(left, right);
} else if(right.branches.back().leaf == 'i') { // dot
right.branches.back().leaf = '.';
execute(left, right);
} else if(right.branches.back().leaf == 'L') { // space
right.branches.back().leaf = ' ';
execute(left, right);
} else if(right.branches.back().leaf == 'I') { // single quote
right.branches.back().leaf = '\'';
execute(left, right);
} else if(right.branches.back().leaf == 'Y') { // double quote
right.branches.back().leaf = '\"';
execute(left, right);
} else if(right.branches.back().leaf == '^') { // elevate all the last elements of the same rank
right.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: ^ has nothing to elevate\n";
return;
}
The elevated;
elevated.is_leaf = false;
auto target_rank = rank_of(right.branches.back());
int index_of_first_element_with_fitting_rank = right.branches.size() - 2;
while(index_of_first_element_with_fitting_rank >= 0 && rank_of(right.branches[index_of_first_element_with_fitting_rank]) == target_rank) {
--index_of_first_element_with_fitting_rank;
}
++index_of_first_element_with_fitting_rank;
for(auto i = index_of_first_element_with_fitting_rank; i < right.branches.size(); ++i) {
elevated.branches.push_back(right.branches[i]);
}
right.branches.erase(right.branches.begin() + index_of_first_element_with_fitting_rank, right.branches.end());
right.branches.push_back(elevated);
execute(left, right);
} else if(right.branches.back().leaf == '|') { // put an element of the current branch on top by index
right.branches.pop_back(); // throw away the bracket
if(right.branches.size() < 1) {
cerr << "Syntax error: ' operation (AKA atbitrary access) has no index\n";
return;
}
const auto& index_container = right.branches.back();
auto index = stoi(rank_1_to_string(index_container));
right.branches.pop_back(); // throw away the index container
right.branches.push_back(*(right.branches.rbegin() + index));
execute(left, right);
} else if(right.branches.back().leaf == '#') { // remove all but the targeted by index element for the selected depth
right.branches.pop_back(); // throw away the bracket
if(right.branches.size() < 2) {
cerr << "Syntax error: \" operation (AKA select by index and depth) requires both index and depth arguments\n";
return;
}
auto depth = stoi(rank_1_to_string(right.branches.back()));
auto index = stoi(rank_1_to_string(right.branches[right.branches.size()-2]));
right.branches.pop_back(); // throw away the depth container
right.branches.pop_back(); // throw away the index container
if(depth > 0)
select_by_index_and_depth(right.branches.back(), index, depth-1);
else
select_by_index_and_depth(right, index, depth);
execute(left, right);
} else if(right.branches.back().leaf == 'm') { // replicate an item multiple times
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: m operation (AKA replicate) need two arguments - an item to replicate and a number of replications\n";
return;
}
auto item = right.branches[right.branches.size() - 2];
auto n_replications = stoi(rank_1_to_string(right.branches[right.branches.size() - 1]));
The replications;
replications.is_leaf = false;
for(auto i = 0; i < n_replications; ++i)
replications.branches.push_back(item);
right.branches.pop_back();
right.branches.back() = replications;
execute(left, right);
} else if(right.branches.back().leaf == 'H') { // duplicate the last element
right.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: H operation (AKA dup) has no argument to duplicate\n";
return;
}
right.branches.push_back(right.branches.back());
execute(left, right);
} else if(right.branches.back().leaf == 'X') { // drop the last element
right.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: X operation (AKA drop) has no argument to drop\n";
return;
}
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == 'G') { // swap the last two elements
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: G operation (AKA swap) has not enough arguments to swap\n";
return;
}
const auto last_branch = right.branches.size() - 1;
swap(right.branches[last_branch], right.branches[last_branch - 1]);
execute(left, right);
} else if(right.branches.back().leaf == 'A') { // elevate an empty element
right.branches.back().is_leaf = false;
right.branches.back().branches.clear();
execute(left, right);
} else if(right.branches.back().leaf == '$') { // count
right.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: $ operation (AKA count) has no argument to count things into\n";
return;
}
right.branches.back() = string_to_rank_1(to_string(right.branches.back().branches.size()));
execute(left, right);
} else if(right.branches.back().leaf == 'v') { // deelevate last element
right.branches.pop_back();
if(right.branches.size() < 1) {
cerr << "Syntax error: v operation (AKA deelevate) has no argument to deelevate\n";
return;
}
auto the_copy = right.branches.back();
right.branches.pop_back();
for(const auto& one_in_copy: the_copy.branches)
right.branches.push_back(one_in_copy);
execute(left, right);
} else if(right.branches.back().leaf == '+') { // addition
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation + requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return string_to_rank_1(Numbers::add(ls, rs));
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '-') { // subtraction
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation - requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return string_to_rank_1(Numbers::sub(ls, rs));
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == 'x') { // multiplication
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation x (AKA *) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return string_to_rank_1(Numbers::mul(ls, rs));
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == 'z') { // division
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation z (AKA /) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return string_to_rank_1(Numbers::div(ls, rs));
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '=') { // equal?
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation = (AKA equal?) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return (ls == rs) ? string_to_rank_1("1") : string_to_rank_1("0");
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '%') { // numerically equal?
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation % (AKA numerical equal?) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
if(Numbers::is_a_number(ls) && Numbers::is_a_number(rs)) {
auto difference = Numbers::sub(ls, rs);
return (difference == "0") ? string_to_rank_1("1") : string_to_rank_1("0");
} else {
cerr << "Syntax error: binary operation % (AKA numerical equal?) requires 2 arguments to be number-interpretable\n";
return The();
}
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '<') { // less?
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation < (AKA less) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
if(Numbers::is_a_number(ls) && Numbers::is_a_number(rs)) {
auto difference = Numbers::sub(ls, rs);
return (difference[0] == '-') ? string_to_rank_1("1") : string_to_rank_1("0");
} else {
cerr << "Syntax error: binary operation < (AKA less) requires 2 arguments to be number-interpretable\n";
return The();
}
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '>') { // greater?
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation > (AKA greater) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
if(Numbers::is_a_number(ls) && Numbers::is_a_number(rs)) {
auto difference = Numbers::sub(rs, ls);
return (difference[0] == '-') ? string_to_rank_1("1") : string_to_rank_1("0");
} else {
cerr << "Syntax error: binary operation > (AKA greater) requires 2 arguments to be number-interpretable\n";
return The();
}
});
right.branches.pop_back();
execute(left, right);
} else if(right.branches.back().leaf == '(') { // substring?
right.branches.pop_back();
if(right.branches.size() < 2) {
cerr << "Syntax error: binary operation ( (AKA substring) requires 2 arguments\n";
return;
}
auto l = right.branches[right.branches.size() - 2];
auto r = right.branches[right.branches.size() - 1];
// computation goes to the left argument, the right one will be then popped
right.branches[right.branches.size() - 2] = binary_1_to_1(l, r, [](const The& lt, const The& rt) -> The{
auto ls = rank_1_to_string(lt);
auto rs = rank_1_to_string(rt);
return (rs.find(ls) != std::string::npos) ? string_to_rank_1("1") : string_to_rank_1("0");
});