-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib-base-main2.cc
1962 lines (1843 loc) · 105 KB
/
lib-base-main2.cc
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
// lib-base-main2.cc -- base facilities
/* Copyright (C) 2018, 2019, 2020 Alexey Protasov (AKA Alex or rusini)
This file is part of MANOOL.
MANOOL is free software: you can redistribute it and/or modify it under the terms of the version 3 of the GNU General Public License
as published by the Free Software Foundation (and only version 3).
MANOOL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with MANOOL. If not, see <https://www.gnu.org/licenses/>. */
# include "config.tcc"
# include "mnl-lib-base.hh"
# include "base.tcc"
# include <deque>
# include <exception>
namespace MNL_AUX_UUID { using namespace aux;
namespace aux {
using std::make_shared; // <memory>
using std::deque; using std::list;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace aux {
code optimize(expr_lit<>);
code optimize(expr_set<>), optimize(expr_move<>);
code optimize(expr_ifelse<>), optimize(expr_if<>), optimize(expr_and<>), optimize(expr_or<>), optimize(expr_while<>), optimize(expr_on<>);
code optimize(expr_att);
} // namespace aux
namespace aux { namespace {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class comp_set { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
if (form.size() != 3) err_compile("invalid form", _loc);
return optimize(expr_set<>{compile_lval(form[1], _loc), compile_rval(form[2], _loc)});
}
};
class comp_move { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
if (form.size() != 2) err_compile("invalid form", _loc);
return optimize(expr_move<>{compile_lval(form[1], _loc)});
}
};
class comp_deref { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
if (form.size() != 2) err_compile("invalid form", _loc);
struct expr { MNL_LVALUE(true)
code arg; loc _loc;
public:
MNL_INLINE val execute(bool) const {
auto arg = this->arg.execute();
try { return MNL_SYM("^")(move(arg)); } catch (...) { trace_execute(_loc); }
}
MNL_INLINE void exec_in(val &&value) const {
val argv[]{arg.execute(), move(value)};
try { MNL_SYM("Set")(std::extent<decltype(argv)>::value, argv); } catch (...) { trace_exec_in(_loc); }
}
MNL_INLINE val exec_out() const {
val argv_out[2], argv[2]{arg.execute()};
try { MNL_SYM("Set")(std::extent<decltype(argv)>::value, argv, argv_out); } catch (...) { trace_exec_out(_loc); }
return move(argv_out[1]);
}
};
return expr{compile_rval(form[1], _loc), _loc};
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class comp_if { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {if C then B else B; B; ...}
if (form.size() >= 6); else goto opt2;
if (form[2] == MNL_SYM("then")); else goto opt2;
if (form[4] == MNL_SYM("else")); else goto opt2;
return optimize(expr_ifelse<>{compile_rval(form[1], _loc), compile_rval(form[3], _loc), compile_rval(form + 5, _loc), _loc});
opt2: // {if C then B; B; ...}
if (form.size() >= 4); else goto opt3;
if (form[2] == MNL_SYM("then")); else goto opt3;
return optimize(expr_if<>{compile_rval(form[1], _loc), compile_rval(form + 3, _loc), _loc});
opt3:
err_compile("invalid form", _loc);
}
};
class comp_and { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
if (form.size() != 3) err_compile("invalid form", _loc);
return optimize(expr_and<>{compile_rval(form[1], _loc), compile_rval(form[2], _loc), _loc});
}
};
class comp_or { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
if (form.size() != 3) err_compile("invalid form", _loc);
return optimize(expr_or<> {compile_rval(form[1], _loc), compile_rval(form[2], _loc), _loc});
}
};
class comp_while { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {while C do B; B; ...}
if (form.size() >= 4); else goto opt2;
if (form[2] == MNL_SYM("do")); else goto opt2;
return optimize(expr_while<>{compile_rval(form[1], _loc), compile_rval(form + 3, _loc), _loc});
opt2:
err_compile("invalid form", _loc);
}
};
class comp_repeat { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {repeat N do B; B; ...}
if (form.size() >= 4); else goto opt2;
if (form[2] == MNL_SYM("do")); else goto opt2;
{ struct expr { MNL_RVALUE()
code count, body; loc _loc;
MNL_INLINE val execute(bool fast_sig) const {
auto count = safe_cast<long long>(_loc, this->count.execute());
if (MNL_UNLIKELY(count < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
for (MNL_IF_WITH_MT(auto &sig_state = mnl::sig_state); count && (body.execute(fast_sig), !sig_state.first); --count);
return {};
}
};
return expr{compile_rval(form[1], _loc), compile_rval(form + 3, _loc), _loc};
}
opt2: // {repeat B; B; ...}
if (form.size() >= 2); else goto opt2;
{ struct expr { MNL_RVALUE()
code body;
MNL_INLINE val execute(bool fast_sig) const {
for (MNL_IF_WITH_MT(auto &sig_state = mnl::sig_state); body.execute(fast_sig), !sig_state.first;);
return {};
}
};
return expr{compile_rval(form + 1, _loc)};
}
opt3:
err_compile("invalid form", _loc);
}
};
class comp_on { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {on K with I do B after B; B; ...}
if (form.size() >= 8); else goto opt2;
if (form[2] == MNL_SYM("with")); else goto opt2;
if (form[4] == MNL_SYM("do")); else goto opt2;
if (form[6] == MNL_SYM("after")); else goto opt2;
if (test<sym>(form[3])); else goto opt2;
if (tmp_cnt == lim<decltype(tmp_cnt)>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
{ auto key = compile_rval(form[1], _loc);
auto overriden_ent = symtab[cast<const sym &>(form[3])]; symtab.update(cast<const sym &>(form[3]), expr_tmp{tmp_cnt++});
auto inserted_tmp_id = tmp_ids.insert(cast<const sym &>(form[3])).second;
auto trap = compile_rval(form[5], _loc);
if (inserted_tmp_id) tmp_ids.erase(cast<const sym &>(form[3]));
--tmp_cnt, symtab.update(cast<const sym &>(form[3]), move(overriden_ent));
return optimize(expr_on<>{move(key), move(trap), compile_rval(form + 7, _loc), _loc});
}
opt2:
err_compile("invalid form", _loc);
}
};
class comp_signal { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
struct expr { MNL_RVALUE()
code key, value; loc _loc;
MNL_INLINE val execute(bool fast_sig) const {
sig_state = {safe_cast<const sym &>(_loc, this->key.execute()), value.execute()};
if (MNL_LIKELY(fast_sig)) return {}; // fast unwinding
auto sig = move(sig_state); sig_state.first = {}; throw move(sig); // slow unwinding
}
};
opt1: // {signal K with V}
if (form.size() == 4); else goto opt2;
if (form[2] == MNL_SYM("with")); else goto opt2;
return expr{compile_rval(form[1], _loc), compile_rval(form[3], _loc), _loc};
opt2:
err_compile("invalid form", _loc);
}
};
class comp_for { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {for {I = V; I = V; ...} do B; B; ...}
{ if (form.size() >= 4); else goto opt2;
if (form[1].is_list() && form[1].size() >= 1); else goto opt2;
if (form[2] == MNL_SYM("do")); else goto opt2;
for (auto &&el: form[1]) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=") && test<sym>(el[1])); else goto opt2;
}
{ if (tmp_cnt + (long)form[1].size() > lim<decltype(tmp_cnt)>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el[1])])
tab.update(cast<const sym &>(el[1]), true); else err_compile("ambiguous bindings", _loc);
}
{ vector<code> view; view.reserve(form[1].size()); for (auto &&el: form[1]) view.push_back(compile_rval(el[2], _loc));
deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(el[1])]),
symtab.update(cast<const sym &>(el[1]), expr_tmp{tmp_cnt++});
vector<sym> inserted_tmp_ids;
for (auto &&el: form[1]) if (tmp_ids.insert(cast<const sym &>(el[1])).second) inserted_tmp_ids.push_back(cast<const sym &>(el[1]));
auto body = compile_rval(form + 3, _loc);
for (auto &&el: inserted_tmp_ids) tmp_ids.erase(move(el));
tmp_cnt -= form[1].size();
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
if (view.size() == 1) {
struct expr { MNL_RVALUE()
code view, body; loc _loc;
MNL_INLINE val execute(bool fast_sig) const {
auto view = this->view.execute();
tmp_stk.push_back({});
struct _ { MNL_INLINE ~_() { tmp_stk.pop_back(); } } _;
MNL_IF_WITH_MT(auto &tmp_stk = mnl::tmp_stk; auto &sig_state = mnl::sig_state;)
if (MNL_UNLIKELY(test<range<>>(view)))
for (auto lo = cast<const range<> &>(view).lo, hi = cast<const range<> &>(view).hi;;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = lo++, body.execute(fast_sig), sig_state.first )) return {};
if (MNL_UNLIKELY(test<range<true>>(view)))
for (auto lo = cast<const range<true> &>(view).lo, hi = cast<const range<true> &>(view).hi;;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = --hi, body.execute(fast_sig), sig_state.first )) return {};
if (MNL_UNLIKELY(test<vector<val>>(view)))
for (auto lo = cast<const vector<val> &>(view).begin(), hi = cast<const vector<val> &>(view).end();;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = *lo++, body.execute(fast_sig), sig_state.first )) return {};
// else
view = MNL_SYM("Elems")(_loc, move(view));
if (MNL_UNLIKELY(test<vector<val>>(view)))
for (auto lo = cast<const vector<val> &>(view).begin(), hi = cast<const vector<val> &>(view).end();;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = *lo++, body.execute(fast_sig), sig_state.first )) return {};
if (MNL_UNLIKELY(test<string>(view)))
for (auto lo = cast<const string &>(view).begin(), hi = cast<const string &>(view).end();;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = *lo++, body.execute(fast_sig), sig_state.first )) return {};
if (MNL_UNLIKELY(test<range<>>(view)))
for (auto lo = cast<const range<> &>(view).lo, hi = cast<const range<> &>(view).hi;;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = lo++, body.execute(fast_sig), sig_state.first )) return {};
if (MNL_UNLIKELY(test<range<true>>(view)))
for (auto lo = cast<const range<true> &>(view).lo, hi = cast<const range<true> &>(view).hi;;)
if (!MNL_LIKELY(lo != hi) || MNL_UNLIKELY( tmp_stk.back() = --hi, body.execute(fast_sig), sig_state.first )) return {};
// else
auto hi = safe_cast<long long>(_loc, MNL_SYM("Size")(_loc, view));
if (MNL_UNLIKELY(hi < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
for (long long lo = 0; lo < hi; ++lo) {
try { tmp_stk.back() = view(_loc, lo); }
catch (decltype(::mnl::sig_state) &sig) { if (sig.first == MNL_SYM("EndOfData")) return {}; throw; }
if (MNL_UNLIKELY( body.execute(fast_sig), sig_state.first )) return {};
}
return {};
}
};
return expr{move(view.front()), move(body), _loc};
}
if (view.size() == 2) {
struct expr { MNL_RVALUE()
code view0, view1, body; loc _loc;
MNL_INLINE val execute(bool fast_sig) const {
auto view0 = MNL_SYM("Elems")(_loc, this->view0.execute()), view1 = MNL_SYM("Elems")(_loc, this->view1.execute());
auto size = safe_cast<long long>(_loc, MNL_SYM("Size")(_loc, view0));
if (MNL_UNLIKELY(size < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
{ auto _size = safe_cast<long long>(_loc, MNL_SYM("Size")(_loc, view1));
if (MNL_UNLIKELY(_size < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
if (MNL_UNLIKELY(_size < size)) size = _size;
}
tmp_stk.resize(tmp_stk.size() + 2);
struct _ { MNL_INLINE ~_() { tmp_stk.pop_back(), tmp_stk.pop_back(); } } _;
MNL_IF_WITH_MT(auto &tmp_stk = mnl::tmp_stk; auto &sig_state = mnl::sig_state;)
for (long long sn = 0; sn < size; ++sn) {
try { tmp_stk.end()[-2] = view0(_loc, sn), tmp_stk.end()[-1] = view1(_loc, sn); }
catch (decltype(::mnl::sig_state) &sig) { if (sig.first == MNL_SYM("EndOfData")) return {}; throw; }
if (MNL_UNLIKELY( body.execute(fast_sig), sig_state.first )) return {};
}
return {};
}
};
return expr{move(view[0]), move(view[1]), move(body), _loc};
}
{ struct expr { MNL_RVALUE()
vector<code> view; code body; loc _loc;
MNL_INLINE val execute(bool fast_sig) const {
struct view: vector<val> { MNL_INLINE ~view() { while (!empty()) pop_back(); } } view;
view.reserve(this->view.size()); for (auto &&el: this->view) view.push_back(MNL_SYM("Elems")(_loc, el.execute()));
auto size = safe_cast<long long>(_loc, MNL_SYM("Size")(_loc, view.front()));
if (MNL_UNLIKELY(size < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
for (auto it = view.begin() + 1; it != view.end(); ++it)
{ auto _size = safe_cast<long long>(_loc, MNL_SYM("Size")(_loc, *it));
if (MNL_UNLIKELY(_size < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
if (MNL_UNLIKELY(_size < size)) size = _size;
}
tmp_stk.resize(tmp_stk.size() + view.size());
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _{(int)view.size()};
MNL_IF_WITH_MT(auto &tmp_stk = mnl::tmp_stk; auto &sig_state = mnl::sig_state;)
for (long long sn1 = 0; sn1 < size; ++sn1) {
try { for (int sn2 = 0; sn2 < (int)view.size(); ++sn2) (tmp_stk.end() - view.size())[sn2] = view[sn2](_loc, sn1); }
catch (decltype(::mnl::sig_state) &sig) { if (sig.first == MNL_SYM("EndOfData")) return {}; throw; }
if (MNL_UNLIKELY( body.execute(fast_sig), sig_state.first )) return {};
}
return {};
}
};
return expr{move(view), move(body), _loc};
}
}
opt2:
err_compile("invalid form", _loc);
}
};
class comp_case { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {case V of {K = B; ...} else B; B; ...}
{ if (form.size() >= 6); else goto opt2;
if (form[2] == MNL_SYM("of")); else goto opt2;
if (form[4] == MNL_SYM("else")); else goto opt2;
for (auto &&el: form[3]) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=")); else goto opt2;
}
{ if (form[3].size() > lim<unsigned char>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ auto key = compile_rval(form[1], _loc);
set<sym> descr; vector<code> arms;
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<sym> keys; for (auto &&el: form[3])
if (keys.push_back(eval_sym(el[1], _loc)), !descr.insert(keys.back()).second) err_compile("ambiguous bindings", _loc);
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
{ sym::tab<> tab; for (auto &&el: form[3]) tab.update(move(keys.front()), compile_rval(el[2], _loc)), keys.pop_front();
arms.reserve(descr.size()); for (auto &&el: descr) arms.push_back(tab[el]);
}
struct expr { MNL_LVALUE(_is_lvalue())
code key; record_descr descr; vector<code> arms; code else_arm;
public:
MNL_INLINE val execute(bool fast_sig) const {
auto key = this->key.execute();
return (MNL_LIKELY(test<sym>(key)) && descr.has(cast<const sym &>(key)) ?
arms[descr[cast<const sym &>(key)]] : else_arm).execute(fast_sig);
}
MNL_INLINE void exec_in(val &&value) const {
auto key = this->key.execute();
(MNL_LIKELY(test<sym>(key)) && descr.has(cast<const sym &>(key)) ?
arms[descr[cast<const sym &>(key)]] : else_arm).exec_in(move(value));
}
MNL_INLINE val exec_out() const {
auto key = this->key.execute();
return (MNL_LIKELY(test<sym>(key)) && descr.has(cast<const sym &>(key)) ?
arms[descr[cast<const sym &>(key)]] : else_arm).exec_out();
}
private:
MNL_INLINE bool _is_lvalue() const noexcept
{ for (auto &&el: arms) if (!el.is_lvalue()) return false; return else_arm.is_lvalue(); }
};
return expr{move(key), move(descr), move(arms), compile_rval(form + 5, _loc)};
}
opt2: // {case V of K = B; K = B; ...}
{ if (form.size() >= 4); else goto opt3;
if (form[2] == MNL_SYM("of")); else goto opt3;
for (auto &&el: form + 3) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=")); else goto opt3;
}
{ if (form.size() - 3 > lim<unsigned char>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ auto key = compile_rval(form[1], _loc);
set<sym> descr; vector<code> arms;
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<sym> keys; for (auto &&el: form + 3)
if (keys.push_back(eval_sym(el[1], _loc)), !descr.insert(keys.back()).second) err_compile("ambiguous bindings", _loc);
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
{ sym::tab<> tab; for (auto &&el: form + 3) tab.update(move(keys.front()), compile_rval(el[2], _loc)), keys.pop_front();
arms.reserve(descr.size()); for (auto &&el: descr) arms.push_back(tab[el]);
}
struct expr { MNL_LVALUE(_is_lvalue())
code key; record_descr descr; vector<code> arms; loc _loc;
public:
MNL_INLINE val execute(bool fast_sig) const {
auto key = this->key.execute();
if (MNL_UNLIKELY(!test<sym>(key))) MNL_ERR_LOC(_loc, MNL_SYM("TypeMismatch"));
if (MNL_UNLIKELY(!descr.has(cast<const sym &>(key)))) MNL_ERR_LOC(_loc, MNL_SYM("KeyLookupFailed"));
return arms[descr[cast<const sym &>(key)]].execute(fast_sig);
}
MNL_INLINE void exec_in(val &&value) const {
auto key = this->key.execute();
if (MNL_UNLIKELY(!test<sym>(key))) MNL_ERR_LOC(_loc, MNL_SYM("TypeMismatch"));
if (MNL_UNLIKELY(!descr.has(cast<const sym &>(key)))) MNL_ERR_LOC(_loc, MNL_SYM("KeyLookupFailed"));
arms[descr[cast<const sym &>(key)]].exec_in(move(value));
}
MNL_INLINE val exec_out() const {
auto key = this->key.execute();
if (MNL_UNLIKELY(!test<sym>(key))) MNL_ERR_LOC(_loc, MNL_SYM("TypeMismatch"));
if (MNL_UNLIKELY(!descr.has(cast<const sym &>(key)))) MNL_ERR_LOC(_loc, MNL_SYM("KeyLookupFailed"));
return arms[descr[cast<const sym &>(key)]].exec_out();
}
private:
MNL_INLINE bool _is_lvalue() const noexcept
{ for (auto &&el: arms) if (!el.is_lvalue()) return false; return true; }
};
return expr{move(key), move(descr), move(arms), _loc};
}
opt3:
err_compile("invalid form", _loc);
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class comp_proc { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {proc {I; ...} as B; B; ...}
{ if (form.size() >= 4); else goto opt2;
if (form[1].is_list()); else goto opt2;
if (form[2] == MNL_SYM("as")); else goto opt2;
for (auto &&el: form[1]) if (test<sym>(el)); else goto opt2;
}
{ if (form[1].size() > val::max_argc) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el)])
tab.update(cast<const sym &>(el), true); else err_compile("ambiguous bindings", _loc);
}
{ deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(el)]),
symtab.update(cast<const sym &>(el), expr_tmp{tmp_cnt++});
for (auto &&el: form[1]) tmp_ids.insert(cast<const sym &>(el));
auto body = compile_rval(form + 3, _loc);
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el), move(overriden_ents.front())), overriden_ents.pop_front();
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
switch (form[1].size()) {
# define MNL_M1(ARG_COUNT) \
MNL_INLINE val invoke(val &&self, const sym &op, int argc, val argv[], val *) { \
stk_check(); \
if (MNL_UNLIKELY(op != MNL_SYM("Apply"))) return self.default_invoke(op, argc, argv); \
if (MNL_UNLIKELY(argc != ARG_COUNT)) MNL_ERR(MNL_SYM("InvalidInvocation")); \
struct _ { \
decltype(tmp_frm) saved_tmp_frm; int sn; \
MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); tmp_frm = move(saved_tmp_frm); } \
} _; \
_.saved_tmp_frm = move(tmp_frm), tmp_frm = tmp_stk.size(); \
for (_.sn = 0; _.sn < ARG_COUNT; ++_.sn) tmp_stk.push_back(move(argv[_.sn])); \
return body.execute(); \
} \
// end # define MNL_M1(ARG_COUNT)
{ struct proc { const int arg_count; code body; MNL_M1(arg_count) };
default: return optimize(expr_lit<>{proc{(int)form[1].size(), move(body)}});
}
# define MNL_M2(ARG_COUNT) \
{ struct proc { code body; MNL_M1(ARG_COUNT) }; \
case ARG_COUNT: return optimize(expr_lit<>{proc{move(body)}}); \
} \
// end # define MNL_M2(ARG_COUNT)
MNL_M2(0) MNL_M2(1) MNL_M2(2) MNL_M2(3) MNL_M2(4) MNL_M2(5) MNL_M2(6)
# undef MNL_M2
# undef MNL_M1
}
}
opt2: // {proc {I,I?; ...} as B; B; ...}
{ if (form.size() >= 4); else goto opt3;
if (form[1].is_list()); else goto opt3;
if (form[2] == MNL_SYM("as")); else goto opt3;
for (auto &&el: form[1]) if (test<sym>(el) ||
el.is_list() && el.size() == 2 && el[0] == MNL_SYM("?") && test<sym>(el[1])); else goto opt3;
}
{ if (form[1].size() > val::max_argc) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(test<sym>(el) ? el : el[1])])
tab.update(cast<const sym &>(test<sym>(el) ? el : el[1]), true); else err_compile("ambiguous bindings", _loc);
}
{ deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(test<sym>(el) ? el : el[1])]),
symtab.update(cast<const sym &>(test<sym>(el) ? el : el[1]), expr_tmp{tmp_cnt++});
for (auto &&el: form[1]) tmp_ids.insert(cast<const sym &>(test<sym>(el) ? el : el[1]));
vector<unsigned char> mode; for (auto &&el: form[1]) mode.push_back(!test<sym>(el));
auto body = compile_rval(form + 3, _loc);
for (auto &&el: form[1]) symtab.update(cast<const sym &>(test<sym>(el) ? el : el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
struct proc {
vector<unsigned char> mode; code body;
public:
MNL_INLINE val invoke(val &&self, const sym &op, int argc, val argv[], val *argv_out) {
stk_check();
if (MNL_UNLIKELY(op != MNL_SYM("Apply"))) return self.default_invoke(op, argc, argv);
if (MNL_UNLIKELY(argc != (int)mode.size())) MNL_ERR(MNL_SYM("InvalidInvocation"));
auto saved_tmp_frm = move(tmp_frm); tmp_frm = tmp_stk.size();
int sn; for (sn = 0; sn < argc; ++sn) tmp_stk.push_back(move(argv[sn]));
try {
auto res = body.execute();
if (!argv_out) for (; sn; --sn) tmp_stk.pop_back();
else for (; sn; tmp_stk.pop_back()) if (MNL_UNLIKELY(mode[--sn])) argv_out[sn].swap(tmp_stk.back());
tmp_frm = move(saved_tmp_frm);
return res;
} catch (...) {
while (sn--) tmp_stk.pop_back(); tmp_frm = move(saved_tmp_frm);
throw;
}
}
};
return optimize(expr_lit<>{proc{move(mode), move(body)}});
}
opt3:
err_compile("invalid form", _loc);
}
};
class comp_var { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {var {I; ...} in B; B; ...}
{ if (form.size() >= 4); else goto opt2;
if (form[1].is_list()); else goto opt2;
if (form[2] == MNL_SYM("in")); else goto opt2;
for (auto &&el: form[1]) if (test<sym>(el)); else goto opt2;
}
{ if (tmp_cnt + (long)form[1].size() > lim<decltype(tmp_cnt)>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el)])
tab.update(cast<const sym &>(el), true); else err_compile("ambiguous bindings", _loc);
}
{ deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(el)]),
symtab.update(cast<const sym &>(el), expr_tmp{tmp_cnt++});
vector<sym> inserted_tmp_ids;
for (auto &&el: form[1]) if (tmp_ids.insert(cast<const sym &>(el)).second) inserted_tmp_ids.push_back(cast<const sym &>(el));
auto body = compile_rval(form + 3, _loc);
for (auto &&el: inserted_tmp_ids) tmp_ids.erase(move(el));
tmp_cnt -= form[1].size();
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el), move(overriden_ents.front())), overriden_ents.pop_front();
switch (form[1].size()) {
case 0: return move(body);
# define MNL_M1(VAR_COUNT) \
MNL_INLINE val execute(bool fast_sig) const { \
tmp_stk.resize(tmp_stk.size() + VAR_COUNT); \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _{VAR_COUNT}; \
return body.execute(fast_sig); \
} \
MNL_INLINE void exec_in(val &&value) const { \
tmp_stk.resize(tmp_stk.size() + VAR_COUNT); \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _{VAR_COUNT}; \
body.exec_in(move(value)); \
} \
MNL_INLINE val exec_out() const { \
tmp_stk.resize(tmp_stk.size() + VAR_COUNT); \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _{VAR_COUNT}; \
return body.exec_out(); \
} \
// end # define MNL_M1(VAR_COUNT)
{ struct expr { MNL_LVALUE(body.is_lvalue()) const int var_count; code body; MNL_M1(var_count) };
default: return expr{(int)form[1].size(), move(body)};
}
# define MNL_M2(VAR_COUNT) \
{ struct expr { MNL_LVALUE(body.is_lvalue()) code body; MNL_M1(VAR_COUNT) }; \
case VAR_COUNT: return expr{move(body)}; \
} \
// end # define MNL_M2(VAR_COUNT)
MNL_M2(1) MNL_M2(2) MNL_M2(3) MNL_M2(4) MNL_M2(5) MNL_M2(6) MNL_M2(7) MNL_M2(8)
# undef MNL_M2
# undef MNL_M1
}
}
opt2: // {var {I = V; ...} in B; B; ...}
{ if (form.size() >= 4); else goto opt3;
if (form[1].is_list()); else goto opt3;
if (form[2] == MNL_SYM("in")); else goto opt3;
for (auto &&el: form[1]) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=") && test<sym>(el[1])); else goto opt3;
}
{ if (tmp_cnt + (long)form[1].size() > lim<decltype(tmp_cnt)>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el[1])])
tab.update(cast<const sym &>(el[1]), true); else err_compile("ambiguous bindings", _loc);
}
{ vector<code> init; init.reserve(form[1].size()); for (auto &&el: form[1]) init.push_back(compile_rval(el[2], _loc));
deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(el[1])]),
symtab.update(cast<const sym &>(el[1]), expr_tmp{tmp_cnt++});
vector<sym> inserted_tmp_ids;
for (auto &&el: form[1]) if (tmp_ids.insert(cast<const sym &>(el[1])).second) inserted_tmp_ids.push_back(cast<const sym &>(el[1]));
auto body = compile_rval(form + 3, _loc);
for (auto &&el: inserted_tmp_ids) tmp_ids.erase(move(el));
tmp_cnt -= form[1].size();
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
switch (form[1].size()) {
# define MNL_M1(VAR_COUNT) \
MNL_INLINE val execute(bool fast_sig) const { \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _; \
for (_.sn = 0; _.sn < VAR_COUNT; ++_.sn) tmp_stk.push_back(init[_.sn].execute()); \
return body.execute(fast_sig); \
} \
MNL_INLINE void exec_in(val &&value) const { \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _; \
for (_.sn = 0; _.sn < VAR_COUNT; ++_.sn) tmp_stk.push_back(init[_.sn].execute()); \
body.exec_in(move(value)); \
} \
MNL_INLINE val exec_out() const { \
struct _ { int sn; MNL_INLINE ~_() { for (; sn; --sn) tmp_stk.pop_back(); } } _; \
for (_.sn = 0; _.sn < VAR_COUNT; ++_.sn) tmp_stk.push_back(init[_.sn].execute()); \
return body.exec_out(); \
} \
// end # define MNL_M1(VAR_COUNT)
{ struct expr { MNL_LVALUE(body.is_lvalue()) vector<code> init; code body; MNL_M1((int)init.size()) };
default: return expr{move(init), move(body)};
}
# define MNL_M2(VAR_COUNT) struct expr { MNL_LVALUE(body.is_lvalue()) code init[VAR_COUNT]; code body; MNL_M1(VAR_COUNT) };
{ MNL_M2(1) case 1: return expr{move(init[0]), move(body)}; }
{ MNL_M2(2) case 2: return expr{move(init[0]), move(init[1]), move(body)}; }
{ MNL_M2(3) case 3: return expr{move(init[0]), move(init[1]), move(init[2]), move(body)}; }
{ MNL_M2(4) case 4: return expr{move(init[0]), move(init[1]), move(init[2]), move(init[3]), move(body)}; }
{ MNL_M2(5) case 5: return expr{move(init[0]), move(init[1]), move(init[2]), move(init[3]), move(init[4]), move(body)}; }
{ MNL_M2(6) case 6: return expr{move(init[0]), move(init[1]), move(init[2]), move(init[3]), move(init[4]), move(init[5]), move(body)}; }
# undef MNL_M2
# undef MNL_M1
}
}
opt3:
err_compile("invalid form", _loc);
}
};
class comp_let { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {let {I = E; ...} in E}, {let {I = E; ...} in B; B; B; ...}
{ if (form.size() >= 4); else goto opt2;
if (form[1].is_list()); else goto opt2;
if (form[2] == MNL_SYM("in")); else goto opt2;
for (auto &&el: form[1]) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=") && test<sym>(el[1])); else goto opt2;
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el[1])])
tab.update(cast<const sym &>(el[1]), true); else err_compile("ambiguous bindings", _loc);
}
return [&]()->code{
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<code> overriding_ents;
for (auto &&el: form[1]) { auto ent = pub::compile(el[2], _loc);
overriding_ents.push_back(ent.is_rvalue() ? optimize(expr_lit<>{ent.execute()}) : move(ent)); }
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
deque<code> overriden_ents;
for (auto &&el: form[1]) overriden_ents.push_back(symtab[cast<const sym &>(el[1])]),
symtab.update(cast<const sym &>(el[1]), move(overriding_ents.front())), overriding_ents.pop_front();
vector<sym> erased_tmp_ids;
for (auto &&el: form[1]) if (tmp_ids.erase(cast<const sym &>(el[1]))) erased_tmp_ids.push_back(cast<const sym &>(el[1]));
auto body = form.size() == 4 ? pub::compile(form[3], _loc) : compile_rval(form + 3, _loc);
for (auto &&el: erased_tmp_ids) tmp_ids.insert(move(el));
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
return body;
}();
opt2: // {let rec {I = V; ...} in E}, {let rec {I = V; ...} in B; B; B; ...}
{ if (form.size() >= 5); else goto opt3;
if (form[1] == MNL_SYM("rec")); else goto opt3;
if (form[2].is_list()); else goto opt3;
if (form[3] == MNL_SYM("in")); else goto opt3;
for (auto &&el: form[2]) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=") && test<sym>(el[1])); else goto opt3;
}
{ sym::tab<bool> tab; for (auto &&el: form[2]) if (!tab[cast<const sym &>(el[1])])
tab.update(cast<const sym &>(el[1]), true); else err_compile("ambiguous bindings", _loc);
}
return [&]()->code{
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
struct expr_inner_lit { MNL_RVALUE()
mutable /*atomic*/ bool defined{}; mutable val value;
public:
MNL_INLINE void set(const val &value) const noexcept {
this->value = value, MNL_IF_WITHOUT_MT(defined = true) MNL_IF_WITH_MT(__atomic_store_n(&defined, true, __ATOMIC_RELEASE));
}
MNL_INLINE val execute(bool) const {
if (MNL_LIKELY(MNL_IF_WITHOUT_MT(defined) MNL_IF_WITH_MT(__atomic_load_n(&defined, __ATOMIC_ACQUIRE)))) return value;
MNL_ERR(MNL_SYM("LetRecUndefined"));
}
};
deque<code> overriden_ents;
for (auto &&el: form[2]) overriden_ents.push_back(symtab[cast<const sym &>(el[1])]),
symtab.update(cast<const sym &>(el[1]), expr_inner_lit{});
deque<val> overriding_values;
for (auto &&el: form[2]) overriding_values.push_back(compile_rval(el[2], _loc).execute());
for (auto &&el: form[2]) cast<const expr_inner_lit &>(symtab[cast<const sym &>(el[1])]).set(overriding_values.front()),
overriding_values.push_back(val(overriding_values.front())), overriding_values.pop_front(); // resource leak possible, by design
for (auto &&el: form[2]) symtab.update(cast<const sym &>(el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
for (auto &&el: form[2]) overriden_ents.push_back(symtab[cast<const sym &>(el[1])]),
symtab.update(cast<const sym &>(el[1]), optimize(expr_lit<>{move(overriding_values.front())})), overriding_values.pop_front();
vector<sym> erased_tmp_ids;
for (auto &&el: form[2]) if (tmp_ids.erase(cast<const sym &>(el[1]))) erased_tmp_ids.push_back(cast<const sym &>(el[1]));
auto body = form.size() == 5 ? pub::compile(form[4], _loc) : compile_rval(form + 4, _loc);
for (auto &&el: erased_tmp_ids) tmp_ids.insert(move(el));
for (auto &&el: form[2]) symtab.update(cast<const sym &>(el[1]), move(overriden_ents.front())), overriden_ents.pop_front();
return body;
}();
opt3:
err_compile("invalid form", _loc);
}
};
class comp_export { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {export I; I; ...}
{ if (form.size() >= 1); else goto opt2;
for (auto &&el: form + 1) if (test<sym>(el)); else goto opt2;
}
{ sym::tab<bool> tab; for (auto &&el: form + 1) if (!tab[cast<const sym &>(el)])
tab.update(cast<const sym &>(el), true); else err_compile("ambiguous bindings", _loc);
}
{ vector<pair<sym, code>> bind; bind.reserve(form.size() - 1);
for (auto &&el: form + 1) bind.push_back(make_pair(cast<const sym &>(el), symtab[cast<const sym &>(el)]));
return expr_export{move(bind)};
}
opt2:
err_compile("invalid form", _loc);
}
};
class comp_scope { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {scope {I; ...} in E}, {scope {I; ...} in B; B; B; ...}
{ if (form.size() >= 4); else goto opt2;
if (form[1].is_list()); else goto opt2;
if (form[2] == MNL_SYM("in")); else goto opt2;
for (auto &&el: form[1]) if (test<sym>(el)); else goto opt2;
}
{ sym::tab<bool> tab; for (auto &&el: form[1]) if (!tab[cast<const sym &>(el)])
tab.update(cast<const sym &>(el), true); else err_compile("ambiguous bindings", _loc);
}
return [&]()->code{
auto saved_symtab = move(symtab); symtab.clear();
for (auto &&el: form[1]) symtab.update(cast<const sym &>(el), saved_symtab[cast<const sym &>(el)]);
auto body = form.size() == 4 ? pub::compile(form[3], _loc) : compile_rval(form + 3, _loc);
symtab = move(saved_symtab);
return body;
}();
opt2:
err_compile("invalid form", _loc);
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class comp_array { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {array}
{ if (form.size() == 1); else goto opt2;
}
{ static const auto res = optimize(expr_lit<>{vector<val>{}}); return res;
}
opt2: // {array of V; V; ...}
{ if (form.size() >= 3); else goto opt3;
if (form[1] == MNL_SYM("of")); else goto opt3;
}
{ vector<code> elems; elems.reserve(form.size() - 2);
for (auto &&el: form + 2) elems.push_back(compile_rval(el, _loc));
switch (elems.size()) {
{ struct expr { MNL_RVALUE()
vector<code> elems;
MNL_INLINE val execute(bool) const {
val res = vector<val>{}; cast<vector<val> &>(res).reserve(elems.size());
for (auto &&el: elems) cast<vector<val> &>(res).push_back(el.execute()); return res;
}
};
default: return expr{move(elems)};
}
{ struct expr { MNL_RVALUE()
code elems[1];
MNL_INLINE val execute(bool) const {
val res = vector<val>{}; cast<vector<val> &>(res).reserve(std::extent<decltype(elems)>::value);
cast<vector<val> &>(res).push_back(elems[0].execute()); return res;
}
};
case 1: return expr{move(elems[0])};
}
{ struct expr { MNL_RVALUE()
code elems[2];
MNL_INLINE val execute(bool) const {
val res = vector<val>{}; cast<vector<val> &>(res).reserve(std::extent<decltype(elems)>::value);
cast<vector<val> &>(res).push_back(elems[0].execute()); cast<vector<val> &>(res).push_back(elems[1].execute()); return res;
}
};
case 2: return expr{move(elems[0]), move(elems[1])};
}
{ struct expr { MNL_RVALUE()
code elems[3];
MNL_INLINE val execute(bool) const {
val res = vector<val>{}; cast<vector<val> &>(res).reserve(std::extent<decltype(elems)>::value);
cast<vector<val> &>(res).push_back(elems[0].execute()); cast<vector<val> &>(res).push_back(elems[1].execute());
cast<vector<val> &>(res).push_back(elems[2].execute()); return res;
}
};
case 3: return expr{move(elems[0]), move(elems[1]), move(elems[2])};
}
{ struct expr { MNL_RVALUE()
code elems[4];
MNL_INLINE val execute(bool) const {
val res = vector<val>{}; cast<vector<val> &>(res).reserve(std::extent<decltype(elems)>::value);
cast<vector<val> &>(res).push_back(elems[0].execute()); cast<vector<val> &>(res).push_back(elems[1].execute());
cast<vector<val> &>(res).push_back(elems[2].execute()); cast<vector<val> &>(res).push_back(elems[3].execute()); return res;
}
};
case 4: return expr{move(elems[0]), move(elems[1]), move(elems[2]), move(elems[3])};
}
}
}
opt3: // {array N of V}
{ if (form.size() == 4); else goto opt4;
if (form[2] == MNL_SYM("of")); else goto opt4;
}
{ struct expr { MNL_RVALUE()
code size, elem; loc _loc;
MNL_INLINE val execute(bool) const {
auto size = this->size.execute();
if (MNL_UNLIKELY(!test<long long>(size))) MNL_ERR_LOC(_loc, MNL_SYM("TypeMismatch"));
if (MNL_UNLIKELY(cast<long long>(size) < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
auto elem = this->elem.execute();
return [&]()->vector<val>{ try { return vector<val>(MNL_LIKELY(cast<long long>(size) <= lim<vector<val>::size_type>::max()) ?
cast<long long>(size) : throw std::bad_alloc{}, elem); } catch (...) { trace_execute(_loc); } }(); // do trace alloc errors
}
};
return expr{compile_rval(form[1], _loc), compile_rval(form[3], _loc), _loc};
}
opt4: // {array N}
{ if (form.size() == 2); else goto opt5;
}
{ struct expr { MNL_RVALUE()
code size; loc _loc;
MNL_INLINE val execute(bool) const {
auto size = this->size.execute();
if (MNL_UNLIKELY(!test<long long>(size))) MNL_ERR_LOC(_loc, MNL_SYM("TypeMismatch"));
if (MNL_UNLIKELY(cast<long long>(size) < 0)) MNL_ERR_LOC(_loc, MNL_SYM("ConstraintViolation"));
return [&]()->vector<val>{ try { return vector<val>(MNL_LIKELY(cast<long long>(size) <= lim<vector<val>::size_type>::max()) ?
cast<long long>(size) : throw std::bad_alloc{}); } catch (...) { trace_execute(_loc); } }(); // do trace alloc errors
}
};
return expr{compile_rval(form[1], _loc), _loc};
}
opt5:
err_compile("invalid form", _loc);
}
};
class comp_record { MNL_NONVALUE()
MNL_INLINE static code compile(code &&, const form &form, const loc &_loc) {
opt1: // {record utils K; K; ...}
{ if (form.size() >= 3); else goto opt2;
if (form[1] == MNL_SYM("utils")); else goto opt2;
}
{ if (form.size() - 2 > lim<unsigned char>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ set<sym> descr;
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
for (auto &&el: form + 2)
if (!descr.insert(eval_sym(el, _loc)).second) err_compile("ambiguous bindings", _loc);
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
switch (descr.size()) {
# define MNL_M(CASE, SIZE) \
{ struct proc { \
record_descr descr; \
MNL_INLINE val invoke(val &&self, const sym &op, int argc, val argv[], val *) { \
if (MNL_UNLIKELY(op != MNL_SYM("Apply"))) return self.default_invoke(op, argc, argv); \
if (MNL_UNLIKELY(argc != 1)) MNL_ERR(MNL_SYM("InvalidInvocation")); \
return test<_record<SIZE>>(argv[0]) && cast<const _record<SIZE> &>(argv[0]).descr == descr; \
} \
}; \
CASE SIZE: return expr_export{{"IsInst", optimize(expr_lit<>{proc{move(descr)}})}}; \
} \
// end # define MNL_M(CASE, SIZE)
MNL_M(default,)
MNL_M(case, 0x1) MNL_M(case, 0x2) MNL_M(case, 0x3) MNL_M(case, 0x4)
MNL_M(case, 0x5) MNL_M(case, 0x6) MNL_M(case, 0x7) MNL_M(case, 0x8)
MNL_M(case, 0x9) MNL_M(case, 0xA) MNL_M(case, 0xB) MNL_M(case, 0xC)
# undef MNL_M
}
}
opt2: // {record of K = V; K = V; ...}
{ if (form.size() >= 3); else goto opt3;
if (form[1] == MNL_SYM("of")); else goto opt3;
for (auto &&el: form + 2) if (el.is_list() && el.size() == 3 && el[0] == MNL_SYM("=")); else goto opt3;
}
{ if (form.size() - 2 > lim<unsigned char>::max()) MNL_ERR(MNL_SYM("LimitExceeded"));
}
{ set<sym> descr; vector<code> items;
deque<code> saved_tmp_ents;
for (auto &&el: tmp_ids) saved_tmp_ents.push_back(symtab[el]), symtab.update(el, {});
auto saved_tmp_cnt = move(tmp_cnt); tmp_cnt = 0;
auto saved_tmp_ids = move(tmp_ids); tmp_ids.clear();
deque<sym> keys; for (auto &&el: form + 2)
if (keys.push_back(eval_sym(el[1], _loc)), !descr.insert(keys.back()).second) err_compile("ambiguous bindings", _loc);
tmp_ids = move(saved_tmp_ids);
tmp_cnt = move(saved_tmp_cnt);
for (auto &&el: tmp_ids) symtab.update(el, move(saved_tmp_ents.front())), saved_tmp_ents.pop_front();
{ sym::tab<> tab; for (auto &&el: form + 2) tab.update(move(keys.front()), compile_rval(el[2], _loc)), keys.pop_front();
items.reserve(descr.size()); for (auto &&el: descr) items.push_back(tab[el]);
}
switch (descr.size()) {
{ struct expr { MNL_RVALUE() // more than 12 items
record_descr descr; vector<code> items;
MNL_INLINE val execute(bool) const {
val res = _record<>{descr}; cast<_record<> &>(res).items.reserve(items.size());
for (auto &&el: items) cast<_record<> &>(res).items.push_back(el.execute()); return res;
}
};
default: return expr{move(descr), move(items)};
}
{ struct expr { MNL_RVALUE() // 1 item
record_descr descr; code items[0x1];
MNL_INLINE val execute(bool) const {
return _record<0x1>{descr,
items[0x0].execute()};
}
};
case 0x1: return expr{move(descr),
move(items[0x0])};
}
{ struct expr { MNL_RVALUE() // 2 items
record_descr descr; code items[0x2];
MNL_INLINE val execute(bool) const {
return _record<0x2>{descr,
items[0x0].execute(), items[0x1].execute()};
}
};
case 0x2: return expr{move(descr),
move(items[0x0]), move(items[0x1])};
}
{ struct expr { MNL_RVALUE() // 3 items
record_descr descr; code items[0x3];
MNL_INLINE val execute(bool) const {
return _record<0x3>{descr,