-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcambda.hh
1828 lines (1552 loc) · 64.6 KB
/
cambda.hh
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
/*
* cambda - Write lambdas in C++ within an embedded Lisp-like language. Header-only. Very 'constexpr'-friendly. The c in cambda stands for constexpr.
*
* https://github.com/aaronmcdaid/cambda
*/
#include<utility>
#include<tuple>
#include<algorithm> // just for std::min
#include<string>
using std::size_t;
namespace cambda_utils {
template<int i>
struct priority_tag;
template<int i>
struct priority_tag : public priority_tag<i-1> {};
template<>
struct priority_tag<0> {};
template<char ... chars>
struct char_pack {
constexpr static char c_str0_[] = {chars..., '\0'};
constexpr static size_t size() { return sizeof...(chars); }
constexpr static char const (&c_str0(void)) [size()+1] { return c_str0_; }
constexpr static char at(size_t i) { return c_str0_[i]; }
constexpr static char last() { return c_str0_[ size()-1 ]; }
auto constexpr
operator==(cambda_utils:: char_pack<chars...> )
-> std::true_type
{ return {}; }
template<char ... other_chars>
auto constexpr
operator==( cambda_utils:: char_pack<other_chars...> )
-> std::false_type
{ return {}; }
};
template<char ... chars>
constexpr char char_pack<chars...>:: c_str0_[]; // storage for this data member
template< typename Stream, char ... c>
Stream &
operator<<(Stream &o, cambda_utils::char_pack<c...> s)
{
o << s.c_str0();
return o;
}
constexpr
int char_pack_to_number(cambda_utils::char_pack<>, int prefix_already_processed)
{ return prefix_already_processed; }
// this is to deal with literals such as '15c', which should be returned as
// std::integral_constant<int, 15>{} from the simplifier
constexpr
int char_pack_to_number(cambda_utils::char_pack<'c'>, int prefix_already_processed)
{ return prefix_already_processed; }
template<char one_digit>
constexpr
double char_pack_to_number(cambda_utils::char_pack<'.', one_digit>, int prefix_already_processed)
{ return prefix_already_processed + 0.1 * (one_digit-'0'); }
template<char digit1, char digit2, char ... digits>
constexpr
double char_pack_to_number(cambda_utils::char_pack<'.', digit1, digit2, digits...>, int prefix_already_processed)
{
return prefix_already_processed
+ 0.1 * (digit1-'0')
+ 0.1 * char_pack_to_number(cambda_utils::char_pack<'.', digit2, digits...>{}, 0)
;
}
template<char next_digit
, char ... c
, typename = std::enable_if_t< (next_digit >= '0' && next_digit <= '9') >
>
constexpr auto
char_pack_to_number(cambda_utils::char_pack<next_digit, c...>, int prefix_already_processed = 0)
{
static_assert( next_digit >= '0' && next_digit <= '9' ,"");
return char_pack_to_number(cambda_utils::char_pack<c...>{}, 10*prefix_already_processed + (next_digit-'0'));
}
/* id_t
* ====
*
* useful to convert some errors into SFINAE. For example, if you have a
* trailing return type
* -> decltype(x.foo())
* where the type of 'x' is not deduced, then you will get a hard error
* instead of SFINAE. If this is undesirable, and you want this method
* to dissappear if 'x' does not have a 'foo' method, then you can do
* this instead:
* template< typename id = cambda_utils:: id_t >
* auto bar()
* -> decltype( id{}(x) .foo())
* {
* ...
* }
* You replace 'x' with 'id{}(x)' (which is essentially a no-op), and
* put 'id' in template arguments with the suitable default.
*
*/
struct id_t
{
template<typename T>
constexpr T
operator() (T&& t) const
{ return t; }
};
/*
* void_t
* ======
*/
namespace detail { template<typename ...T> struct voider { using type = void; }; }
template<typename ...T>
using void_t = typename detail:: voider<T...>:: type;
/* concat_nontype_pack and reverse_pack
* =================== ============
* Some utilities for packs of non-types
*/
template< typename T
, typename L
, typename R>
struct concat_nontype_pack;
template< typename T
, T ... left
, T ... right
, template<T...> class tmplt
>
struct concat_nontype_pack < T
, tmplt<left...>
, tmplt<right...>>
{ using type = tmplt<left..., right...>; };
template< typename T , typename Pack >
struct reverse_pack;
template< typename T , template<T...> class tmplt >
struct reverse_pack<T, tmplt<> >
{ using type = tmplt<>; };
template< typename T , template<T...> class tmplt
, T first
, T ... c>
struct reverse_pack<T, tmplt<first,c...>>
{
using tail_reversed = typename reverse_pack<T, tmplt<c...> > :: type;
using type = typename cambda_utils:: concat_nontype_pack < T, tail_reversed , tmplt<first> >::type;
};
/* equal_string_array and equal_array
* ================== ===========
*/
constexpr bool
equal_string_array ( const char *l
, const char *r )
{
for(int i=0; ; ++i)
{
if(l[i] == '\0' && r[i] == '\0')
return true;
if(l[i] == '\0' || r[i] == '\0')
return false;
if(l[i] != r[i])
return false;
}
}
template< typename T , size_t N1, size_t N2 >
constexpr bool
equal_array ( T const (&l) [N1]
, T const (&r) [N2] )
{
if(N1 != N2)
return false;
for(size_t i=0; i<N1 ; ++i)
{
if(l[i] != r[i])
return false;
}
return true;
}
/*
* scalable_make_index_sequence
* ============================
* Because clang 3.8 doesn't like std::make_index_sequence with more than about 256.
* So I'll make my own one
*
*/
namespace detail {
template< size_t N
, typename = void > /* this is pointless, just to allow me to make a very general specialization */
struct scalable_make_index_sequence_helper;
template<>
struct scalable_make_index_sequence_helper<0>
{ using type = std::index_sequence<>; };
template<>
struct scalable_make_index_sequence_helper<1>
{ using type = std::index_sequence<0>; };
template< size_t add_me_to_the_right_hand_side
, size_t ... Ls
, size_t ... Rs >
auto
concat_index_packs ( std::index_sequence<Ls...>
, std::index_sequence<Rs...> )
-> std::index_sequence<Ls..., add_me_to_the_right_hand_side + Rs...>
{ return {}; }
template<size_t N>
struct scalable_make_index_sequence_helper<N>
{
static_assert(N >= 2, "");
constexpr static size_t mid = N/2;
using left = typename scalable_make_index_sequence_helper< mid > :: type;
using right_shifted = typename scalable_make_index_sequence_helper< N-mid > :: type;
using type = decltype(concat_index_packs<mid>(left{}, right_shifted{}));
};
} // namespace detail
template<size_t N>
using scalable_make_index_sequence = typename detail:: scalable_make_index_sequence_helper<N> :: type;
static_assert(std::is_same< scalable_make_index_sequence<0> , std::make_index_sequence<0> >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<1> , std::make_index_sequence<1> >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<2> , std::make_index_sequence<2> >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<3> , std::make_index_sequence<3> >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<4> , std::make_index_sequence<4> >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<42 > , std::make_index_sequence<42 > >{} ,"");
static_assert(std::is_same< scalable_make_index_sequence<200> , std::make_index_sequence<200> >{} ,"");
/* type_t
* ======
* Used only for the 'typeof' cambda function. Handy though,
* it can be accepted by 'fix'
*/
template<typename T>
struct type_t {
using type = T;
};
/* my_forward_as_tuple
* ===================
* Not for &&-refs. Values and &-refs are OK though
*/
template<typename ... T>
constexpr auto
my_forward_as_tuple(T && ... t)
-> std::tuple<T ...>
{ return std::tuple<T ...>{ std::forward<T>(t) ... }; }
template<typename>
struct template_unpack_types {};
template < template <class...> class Tmpl
, typename ... T >
struct template_unpack_types< Tmpl <T...>>
{
using type = template_unpack_types;
template< typename F>
auto static
apply(F f) // call function 'f', with each T in turn
-> decltype(auto)
{
return f(cambda_utils::type_t<T>{} ...);
}
}; // 'type' is just itself
template<typename T>
using template_unpack_t = typename template_unpack_types<T>::type;
} // namespace cambda_utils
namespace cambda {
/* types_t
* =======
* This is used a lot in this. It's simply to store a list of
* types. The AST (Abstract Syntax Tree) that is parsed from the
* input string is represented as an empty object whose type
* encodes everything. A set of recursive 'types_t' instantiations.
*/
template<typename ... T>
struct types_t {
template<typename Prepend>
using prepend = types_t<Prepend, T...>;
constexpr static size_t size = sizeof...(T);
using is_a_types_t_object = void; // no really needed. Just to help an assertion in 'grouped_t'
};
/*
* UDL for _charpack. Convenient in this header file, no need to use
* it from outside.
*/
template<typename T, T ... chars>
constexpr auto
operator"" _charpack ()
-> cambda_utils:: char_pack<chars...>
{ return {}; }
namespace parsing {
// Open up the 'parsing' namespace for a few lines here,
// these few functions are useful when defining 'grouped_T'
bool constexpr is_whitespace (char c) { return c==' ' || c=='\t' || c=='\n'; }
bool constexpr is_opener (char c) { return c=='(' || c=='[' || c=='{'; }
bool constexpr is_closer (char c) { return c==')' || c==']' || c=='}'; }
bool constexpr is_grouper (char c) { return is_opener(c) || is_closer(c); }
bool constexpr is_digit_constexpr (char c) { return c >= '0' && c <= '9'; }
char constexpr opener_to_closer (char c) { switch(c) {
break; case '(': return ')';
break; case '[': return ']';
break; case '{': return '}';
}
return -1; // should never get here
}
} // namespace parsing
/*
* grouped_t
* =========
* The contents of any (...), {...}, or [...] will
* be stored in this.
*/
template< char c
, typename T // T will always be an instance of types_t<U...>. See assertion below
>
struct grouped_t {
static_assert(parsing::is_opener(c) ,"");
constexpr static char my_closer = parsing::opener_to_closer(c);
static_assert(my_closer != -1 ,"");
// finally, we confirm T is an instance of types_t<U...>
static_assert(std::is_same<void, typename T::is_a_types_t_object>{} ,"");
};
/* Now to finally start on the parsing. First, tokenizing */
namespace parsing {
template< typename C >
auto constexpr
find_next_token(C, size_t o)
->std::pair<size_t,size_t>
{
// first, skip whitespace
while(C::at(o) != '\0' && is_whitespace(C::at(o)))
{
while(C::at(o) != '\0' && is_whitespace(C::at(o)))
++o;
// a one-line comment begins with whitespace followed by #{},
// and runs until the the end of the line
// (foo bar) #{} this is a comment
// If you, for some reason, want this to not be interpreted as a comment,
// then put a space after the '#', or between the '{' and '}'
if ( C::at(o) == '#'
&& C::at(o+1) == '('
&& C::at(o+2) == ')')
{
while(C::at(o) != '\n')
++o;
}
}
size_t const start = o;
// then, check if we're at the end
if(C::at(o) == '\0')
return std::make_pair(start,start); // empty token, i.e. we're finished
// if it's a grouper, (){}[], then we have a single-character token
if(is_grouper(C::at(o)))
return std::make_pair(start, start+1);
// literal strings are special. We quote them with single quotes instead
// of double quotes, simply to make them easier to embed within C++.
// The contents of a literal are entirely read in raw, except that two
// consecutive single-quotes are interpreted as one such quote.
// This allows any string to be specified
//
// A trailing c on the end means a "constant string",
// kind of like std::integral_constant for strings
if(C::at(o) == '\'') {
++o;
while(true)
{
if(C::at(o) == '\'' && C::at(o+1) == '\'')
{ o+=2; continue; }
if(C::at(o) == '\'')
{
o+=1;
if(C::at(o) == 'c')
o+=1;
break;
}
++o;
}
return std::make_pair(start, o);
}
// if we get this far, we just have a string of non-special characters.
// We must read them all in
while (C::at(o) != '\0' && !is_whitespace(C::at(o)) && !is_grouper(C::at(o)))
++o;
return std:: make_pair(start, o);
}
template<typename T>
auto
get_last_type( ::cambda:: types_t<T> )
-> T
{ return {}; }
template<typename S, typename T, typename ... U>
auto
get_last_type( ::cambda::types_t<S, T, U...> )
{
return get_last_type( ::cambda::types_t<T,U...> {});
}
template<typename T>
auto
drop_last_type( types_t<T> )
-> types_t<>
{ return {}; }
template<typename S, typename T, typename ... U>
auto
drop_last_type( types_t<S, T, U...> )
{
using return_type =
typename decltype( drop_last_type(std::declval< types_t<T,U...> >()) )
// , but then prepend S to the result:
::template prepend<S>;
return return_type{};
}
// a few toString overloads just to print the output after parsing to help debugging
constexpr int indent_each_time = 4;
template<char ... c>
std::string
toString( cambda_utils::char_pack<c...> s, int = 0)
{ return s.c_str0(); }
std::string
toString( types_t<> , int = 0)
{ return ""; }
template<typename S>
std::string
toString( types_t<S> , int indent = 0)
{ return toString( S{}, indent); }
template<typename R, typename S, typename ...T>
std::string
toString( types_t<R, S, T...> , int indent = 0)
{
return toString( R{}, indent)
+ "\n" + std::string(indent, ' ')
+ toString( types_t<S, T...>{}, indent);
}
template<char c, typename T>
std::string
toString(grouped_t<c,T> grp, int indent = 0)
{
return std::string{c}
+ std::string(indent_each_time-1, ' ')
+ toString(T{}, indent+indent_each_time)
+ std::string{grp.my_closer} // the closer appears on the same line as the last item
;
}
template<typename E>
constexpr static size_t
count_the_terms(E e)
{
size_t n = 0;
size_t o = 0;
while(true) {
auto tk = find_next_token(e, o);
if(tk.first == tk.second)
break;
o = tk.second;
++n;
}
return n;
}
template< size_t number_of_terms >
struct all_token_pairs_t
{
size_t starts[number_of_terms+1]; // extra one for the special zero-width 'end' token
size_t ends [number_of_terms+1];
};
template< size_t number_of_terms
, typename E>
auto constexpr
get_all_token_pairs(E e)
-> all_token_pairs_t<number_of_terms>
{
auto first_token = find_next_token(e, 0);
all_token_pairs_t<number_of_terms> res{};
res.starts[0] = first_token.first;
res.ends [0] = first_token.second;
for(size_t i = 1; i<=number_of_terms; ++i)
{
auto next_token = find_next_token(e, res.ends[i-1]);
res.starts[i] = next_token.first;
res.ends [i] = next_token.second;
}
return res;
}
template<typename E>
struct all_the_terms_as_types
{
constexpr all_the_terms_as_types(){}
constexpr static E e {};
constexpr static auto number_of_terms = count_the_terms(e);
constexpr static auto all_token_pairs = get_all_token_pairs<number_of_terms>(e);
static_assert(all_token_pairs.starts[number_of_terms] == all_token_pairs.ends[number_of_terms] ,"");
static_assert(all_token_pairs.starts[number_of_terms-1] < all_token_pairs.ends[number_of_terms-1] ,"");
template<size_t I, size_t ...J>
static auto constexpr
just_one_token_as_a_string(std::index_sequence<J...>)
{
constexpr size_t offset_of_first_char_of_this_token = all_token_pairs.starts[I];
return cambda_utils::char_pack< E::at(offset_of_first_char_of_this_token + J) ... >{};
}
template<size_t ...I>
static auto constexpr
every_token_as_a_string(std::index_sequence<I...>)
{
return types_t<
decltype(just_one_token_as_a_string<I>(
std::make_index_sequence<
all_token_pairs.ends[I]
-all_token_pairs.starts[I]
>{}))
... // across all the tokens
>{};
}
using all_the_terms_t = decltype(every_token_as_a_string( cambda_utils::scalable_make_index_sequence<number_of_terms>{} ));
};
/* parse_many_things.
* =================
* Returns two 'types_t', everything up to the next grouper, and the 'rest'
*
* Many cases to be handled. There is a template specialization for each
*/
template<typename, typename = void> /* second type is for 'void_t' */
struct parse_many_things;
// Simplest case is an empty list:
template<>
struct parse_many_things<cambda::types_t<>, void>
{
using me = types_t<>;
using rest = types_t<>;
};
// Any symbol that is not a grouper is simply prepended to the rest of the list they're currently in:
template<typename First, typename ... T>
struct parse_many_things<types_t<
First, T...
>
, std::enable_if_t< !is_grouper( First::at(0) )>
>
{
static_assert(!is_grouper(First::at(0)) ,"");
using future = parse_many_things<types_t<T...>>;
using me = typename future :: me :: template prepend<First>;
using rest = typename future :: rest;
};
// but ')' closes the list:
template<char c, typename ... T>
struct parse_many_things<types_t<
cambda_utils::char_pack<c>, T...
>
, std::enable_if_t<is_closer(c)>
>
{
using me = types_t< cambda_utils::char_pack<c> >;
using rest = types_t<T...>;
};
// ... but '('/'{'/'[' open the list. This is more complex:
template<char o, typename ... T>
struct parse_many_things<types_t<
cambda_utils::char_pack<o>, T...
>
, std::enable_if_t<is_opener(o)>
>
{
using next_and_future = parse_many_things<types_t<T...>>;
// next_and_future::me is the rest of my group
// next_and_future::rest is the distant future, which must be parsed here too
using mygroup_with_the_closer = typename next_and_future::me;
static_assert(std::is_same<void, typename mygroup_with_the_closer :: is_a_types_t_object>{} ,"");
// Check that the openers and closers "match". See the static_assert
using my_closer = decltype(::cambda::parsing::get_last_type(mygroup_with_the_closer{}));
static_assert( my_closer::size() == 1 ,""); // it must be one character ...
static_assert( is_closer(my_closer::at(0)) ,""); // .. one of })]
static_assert( my_closer::at(0) == opener_to_closer(o) ,"opener and closer should match, i.e. (...) or {...} or (...}");
using mygroup_without_the_closer = decltype(drop_last_type(mygroup_with_the_closer{}));
using mygroup = grouped_t<o, mygroup_without_the_closer >;
using future = parse_many_things<typename next_and_future:: rest>;
using me = typename future :: me :: template prepend< mygroup >;
using rest = typename future :: rest;
};
template<typename ...T>
constexpr static auto
parser(types_t<T...>)
{
return parse_many_things<types_t<T...>> {};
}
template<typename E>
auto constexpr
parse_ast(E )
{
//using all_the_terms_t = typename parse_flat_list_of_terms<E, 0>::all_the_terms; /*This was the broken version - clang doesn't like this*/
using all_the_terms_t = typename parsing::all_the_terms_as_types<E>::all_the_terms_t;
auto parsed = parsing::parser( all_the_terms_t{} );
static_assert(std::is_same< typename decltype(parsed) :: rest , types_t<>>{} ,"");
using x = typename decltype(parsed) :: me;
return x{};
}
}// namespace parsing
} // namespace ?
namespace cambda {
template<typename>
struct is_valid_tuple_of_libs
{ constexpr static bool value = false; };
template<typename ... T>
struct is_valid_tuple_of_libs<std::tuple<T...>>
{
constexpr static
bool value = std::min(std::initializer_list<bool>{!std::is_rvalue_reference<T>{} ... } );
};
template<typename ... T>
struct is_valid_tuple_of_libs<const std::tuple<T...>>
: public is_valid_tuple_of_libs<std::tuple<T...>>
{};
template<typename T>
constexpr bool
is_valid_tuple_of_libs_v = is_valid_tuple_of_libs<T>::value;
struct nil_t { }; // to be returned if you write () in cambda, i.e. "()"_cambda()
template< typename T
, typename = void /* for void_t */>
struct simplifier;
template<typename Libs, typename T>
constexpr auto
call_the_simplifier(Libs &libs, T t)
->decltype(simplifier<T>::simplify(libs, t) )
{
static_assert(is_valid_tuple_of_libs_v<Libs> ,"");
return simplifier<T>::simplify(libs, t);
}
// simplifier for all digits
template<char first_digit, char ...c>
struct simplifier < cambda_utils::char_pack<first_digit, c...>
, cambda_utils::void_t<std::enable_if_t<
parsing::is_digit_constexpr(first_digit)
&& parsing::is_digit_constexpr(cambda_utils::char_pack<first_digit, c...> :: last())
>>>
{
template<typename Libs>
static auto constexpr
simplify(Libs &, cambda_utils::char_pack<first_digit, c...> digits)
{ return cambda_utils::char_pack_to_number(digits); }
};
// simplifier for digits with trailing 'c', for an integral constant
template<char first_digit, char ...c>
struct simplifier < cambda_utils::char_pack<first_digit, c...>
, cambda_utils::void_t<std::enable_if_t<
parsing::is_digit_constexpr(first_digit)
&& cambda_utils::char_pack<first_digit, c...> :: last() == 'c'
>>>
{
template<typename Libs>
static auto constexpr
simplify(Libs &, cambda_utils::char_pack<first_digit, c...>)
{ return std::integral_constant<int, cambda_utils::char_pack_to_number(cambda_utils::char_pack<first_digit, c...>{})>{}; }
};
namespace detail {
template<char ...c>
static auto constexpr
drop_leading_c(cambda_utils::char_pack<'c', c...>)
-> cambda_utils::char_pack<c...>
{ return {}; }
template<char ...c>
static auto constexpr
drop_leading_apostrophe(cambda_utils::char_pack<'\'', c...>)
-> cambda_utils::char_pack<c...>
{ return {}; }
template<char ...c>
static auto constexpr
reverse(cambda_utils::char_pack<c...>)
-> typename cambda_utils::reverse_pack<char, cambda_utils::char_pack<c...> >::type
{ return {}; }
constexpr char apostrophe = '\'';
template<typename T> // T is always a char_pack
struct squash_consecutive_apostrophes_struct;
// base case, the empty string
template<>
struct squash_consecutive_apostrophes_struct<cambda_utils::char_pack< >>
{ using type = cambda_utils::char_pack<>; };
template<char ...c>
struct squash_consecutive_apostrophes_struct<cambda_utils::char_pack< apostrophe,apostrophe, c... >>
{
using recursive_type = typename squash_consecutive_apostrophes_struct< cambda_utils::char_pack<c...> >::type;
using type = typename cambda_utils::concat_nontype_pack< char
, cambda_utils::char_pack<apostrophe>
//, cambda_utils::char_pack<c...>
, recursive_type
> :: type;
};
template<char next, char ...c>
struct squash_consecutive_apostrophes_struct<cambda_utils::char_pack< next, c... >>
{
static_assert(next != apostrophe ,"");
using recursive_type = typename squash_consecutive_apostrophes_struct< cambda_utils::char_pack<c...> >::type;
using type = typename cambda_utils::concat_nontype_pack< char
, cambda_utils::char_pack<next>
//, cambda_utils::char_pack<c...>
, recursive_type
> :: type;
};
template<char ...c>
static auto constexpr
squash_consecutive_apostrophes(cambda_utils::char_pack<c...>)
-> typename squash_consecutive_apostrophes_struct<cambda_utils::char_pack<c...>> :: type
{ return {}; }
template<char ...c>
static auto constexpr
parse_string_literal(cambda_utils::char_pack<'\'', c...> crpk)
{
return squash_consecutive_apostrophes( reverse(drop_leading_apostrophe( reverse (drop_leading_apostrophe(crpk) ))));
}
}
// simplifier for string literals
template<typename StringLiteral>
struct simplifier < StringLiteral
, cambda_utils::void_t<std::enable_if_t<
'\'' == StringLiteral::at(0)
&& '\'' == StringLiteral::last()
>>>
{
template<typename Libs>
static auto constexpr
simplify(Libs &,StringLiteral sl)
{ return detail::parse_string_literal(sl).c_str0(); }
};
// simplifier for string literals
template<typename StringLiteral>
struct simplifier < StringLiteral
, cambda_utils::void_t<std::enable_if_t<
'\'' == StringLiteral::at(0)
&& 'c' == StringLiteral::last()
>>>
{
template<typename Libs>
static auto constexpr
simplify(Libs &,StringLiteral sl)
{
(void)sl;
return detail::parse_string_literal(
detail::reverse(
detail::drop_leading_c(
detail::reverse(sl))));
}
};
namespace detail
{
/* has_static_get_simple_named_value */
template< typename Name, typename Lib>
static auto constexpr
has_static_get_simple_named_value_helper(cambda_utils::priority_tag<2>)
-> decltype(void(
std::declval<Lib&>().
static_get_simple_named_value(
std::declval<Lib&>(),
std::declval<Name&>())
)
, std:: true_type{}
)
{ return {}; }
template<typename Name, typename Lib>
static auto constexpr
has_static_get_simple_named_value_helper(cambda_utils::priority_tag<1>)
-> std:: false_type
{ return {}; }
template<typename Name, typename Lib>
static auto constexpr
has_static_get_simple_named_value()
-> decltype(auto)
{ return detail:: has_static_get_simple_named_value_helper<Name,Lib>(cambda_utils::priority_tag<9>{}); }
template<typename Name, typename ... OneLibAmongMany>
static auto constexpr
one_of_these_has_static_get_simple_named_value(std::tuple<OneLibAmongMany...> const &)
-> std::integral_constant<bool, std::max(std::initializer_list<bool>{ has_static_get_simple_named_value<Name, OneLibAmongMany>() ...})>
{ return {}; }
}
template< size_t IndexOfWhichLib
, typename Libs
, typename ... T
>
constexpr auto
search_through_the_libs_and_apply(cambda_utils::priority_tag<2>, Libs & libs, T && ... t)
-> decltype(std::get<IndexOfWhichLib>(std::move(libs))
.apply_after_simplification(
std::get<IndexOfWhichLib>(std::move(libs))
, libs
, std::forward<T>(t) ...
) )
{
static_assert(is_valid_tuple_of_libs_v<Libs> ,"");
return std::get<IndexOfWhichLib>(std::move(libs))
.apply_after_simplification(
std::get<IndexOfWhichLib>(std::move(libs))
, libs
, std::forward<T>(t) ...
);
}
template< size_t IndexOfWhichLib
, typename Libs
, size_t next_index = IndexOfWhichLib+1
, size_t max_sz = std::tuple_size<Libs>::value
, std::enable_if_t<(next_index < max_sz)>* =nullptr
, typename ... T
>
constexpr auto
search_through_the_libs_and_apply(cambda_utils::priority_tag<1>, Libs & libs, T && ... t)
->decltype(cambda::search_through_the_libs_and_apply<next_index>(cambda_utils::priority_tag<9>{}, libs, std::forward<T>(t) ... ) )
{
return cambda::search_through_the_libs_and_apply<next_index>(cambda_utils::priority_tag<9>{}, libs, std::forward<T>(t) ... );
}
template< size_t IndexOfWhichLib, typename Libs, typename Name>
auto constexpr
search_through_the_libs_and_lookup_get_simple_named_value(cambda_utils::priority_tag<2>, Libs & libs, Name name)
->decltype( std::get<IndexOfWhichLib>(std::move(libs))
.static_get_simple_named_value(
std::get<IndexOfWhichLib>(std::move(libs))
, name))
{
return
std::get<IndexOfWhichLib>(std::move(libs))
.static_get_simple_named_value(
std::get<IndexOfWhichLib>(std::move(libs))
, name);
}
template< size_t IndexOfWhichLib, typename Libs, typename Name>
auto constexpr
search_through_the_libs_and_lookup_get_simple_named_value(cambda_utils::priority_tag<1>, Libs & libs, Name name)
->decltype(auto)
{
constexpr size_t sz = std::tuple_size<Libs>::value;
static_assert(IndexOfWhichLib+1 < sz ,"");
return search_through_the_libs_and_lookup_get_simple_named_value<IndexOfWhichLib+1>(cambda_utils::priority_tag<9>{}, libs, name);
}
// simplifier for names.
// Two overloads, one for where 'get_simple_named_value' is present, and one
// to capture-and-store-and-forward to 'apply_after_simplification' later
template<typename Name>
struct simplifier < Name
, cambda_utils::void_t<std::enable_if_t<
!( parsing::is_digit_constexpr(Name::at(0)) )
&& !( '\'' == Name::at(0) )
>>>
{
static_assert(!parsing::is_grouper(Name::at(0)) ,"");
static_assert(!parsing::is_digit_constexpr(Name::at(0)) ,"");
static_assert( '\'' != Name::at(0) ,"");
template<typename Libs
, typename b_type = decltype(detail::one_of_these_has_static_get_simple_named_value<Name>(std::declval<Libs>()))
, std::enable_if_t<b_type::value, std::integral_constant<int,__LINE__>>* =nullptr
>
static auto constexpr
simplify(Libs & libs, Name name)
->decltype(cambda::search_through_the_libs_and_lookup_get_simple_named_value<0>(cambda_utils::priority_tag<9>{}, libs, name))
{
static_assert(is_valid_tuple_of_libs_v<Libs> ,"");
return cambda::search_through_the_libs_and_lookup_get_simple_named_value<0>(cambda_utils::priority_tag<9>{}, libs, name);
}