-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAMBDA_MACRO.cc
228 lines (193 loc) · 8.62 KB
/
CAMBDA_MACRO.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
#include<iostream>
#include"cambda.hh"
namespace CAMBDA_MACRO_support
{
constexpr
size_t
cx_strlen(char const *s)
{
size_t length =0;
while( (*s) != '\0' )
{
++s;
++length;
}
return length;
}
constexpr
char
string_at_with_upper_bound(const char *s, size_t i)
{
size_t len = cx_strlen(s);
if(i >= len)
return '\0';
return s[i];
}
/*
* 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 */
, size_t offset = 0> // add this to every entry
struct scalable_make_index_sequence_helper;
template<size_t offset>
struct scalable_make_index_sequence_helper<0, void, offset>
{ using type = std::index_sequence<>; };
template<size_t offset>
struct scalable_make_index_sequence_helper<1, void, offset>
{ using type = std::index_sequence<offset+0>; };
template<size_t offset>
struct scalable_make_index_sequence_helper<2, void, offset>
{ using type = std::index_sequence<offset+0, offset+1>; };
template< size_t ... Ls
, size_t ... Rs >
auto
concat_index_packs ( std::index_sequence<Ls...>
, std::index_sequence<Rs...> )
-> std::index_sequence<Ls..., Rs...>
{ return {}; }
template<size_t N, size_t offset>
struct scalable_make_index_sequence_helper<N, void, offset>
{
static_assert(N > 2, "");
constexpr static size_t mid = N/2;
using left = typename
scalable_make_index_sequence_helper< mid,void,offset > :: type;
using right_shifted = typename
scalable_make_index_sequence_helper< N-mid,void,offset+mid> :: type;
using type = decltype(concat_index_packs(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<200> , std::make_index_sequence<200> >{} ,"");
template<char ... chars>
struct temporary_holder_for_pack_of_characters
{
static constexpr
char arr[] = {chars...};
static constexpr size_t size() { return sizeof...(chars); }
static_assert(arr[size()-2] == '\0' ,"");
constexpr static
size_t
number_of_non_nulls = cx_strlen(arr);
//static_assert(number_of_non_nulls == 11 ,"");
template<size_t ... NonNullIndices>
auto static constexpr
extract_all_the_nonnull_characters(std::index_sequence<NonNullIndices...>)
{
//static_assert(number_of_non_nulls == 11 ,"");
static_assert(number_of_non_nulls == sizeof...(NonNullIndices) ,"");
char nn[sizeof...(NonNullIndices)]{ arr[NonNullIndices]... };
(void)nn;
return cambda_utils::char_pack< arr[NonNullIndices]... >{};
};
constexpr static
auto trailing_nulls_removed = extract_all_the_nonnull_characters( scalable_make_index_sequence<number_of_non_nulls>{});
constexpr static
auto compiled_CAMBDA_object()
{
return ::cambda::make_cambda_object_from_the_string_literal(
::cambda::parse_ast(trailing_nulls_removed)
, ::cambda::starter_lib_v);
}
template<char ... non_null_chars>
constexpr static
auto compiled_BINDING_object(cambda_utils::char_pack<non_null_chars...>)
{
return ::cambda::binding_name<::cambda::capture_policy:: byValue, non_null_chars...>{};
}
constexpr static
auto compiled_BINDING_object()
{
return compiled_BINDING_object(trailing_nulls_removed);
}
};
}
#define TEN_LOOKUPS(string_literal, base_offset) \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+0), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+1), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+2), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+3), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+4), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+5), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+6), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+7), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+8), \
CAMBDA_MACRO_support::string_at_with_upper_bound(string_literal,base_offset+9),
#define ONE_HUNDRED_LOOKUPS(string_literal, base_offset) \
TEN_LOOKUPS(string_literal, base_offset+0) \
TEN_LOOKUPS(string_literal, base_offset+10) \
TEN_LOOKUPS(string_literal, base_offset+20) \
TEN_LOOKUPS(string_literal, base_offset+30) \
TEN_LOOKUPS(string_literal, base_offset+40) \
TEN_LOOKUPS(string_literal, base_offset+50) \
TEN_LOOKUPS(string_literal, base_offset+60) \
TEN_LOOKUPS(string_literal, base_offset+70) \
TEN_LOOKUPS(string_literal, base_offset+80) \
TEN_LOOKUPS(string_literal, base_offset+90)
#define ONE_THOUSAND_LOOKUPS(string_literal, base_offset) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+0) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+100) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+200) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+300) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+400) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+500) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+600) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+700) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+800) \
ONE_HUNDRED_LOOKUPS(string_literal, base_offset+900)
#define CAMBDA_MACRO(string_literal) \
( CAMBDA_MACRO_support::temporary_holder_for_pack_of_characters < \
ONE_THOUSAND_LOOKUPS( string_literal ,0) '\0'>{} \
.compiled_CAMBDA_object() )
#define BINDING_MACRO(string_literal) \
( CAMBDA_MACRO_support::temporary_holder_for_pack_of_characters < \
ONE_THOUSAND_LOOKUPS( string_literal ,0) '\0'>{} \
.compiled_BINDING_object() )
/*
* Macros defined above. The remainder of this file is for testing
*/
constexpr auto a = CAMBDA_MACRO("15")(); // a is 15
constexpr auto b = CAMBDA_MACRO("(+ 8 7)")(); // Function call. This is addition. b is 15
constexpr auto c = CAMBDA_MACRO("(* 8 7)")(); // Multiplication
constexpr auto d = CAMBDA_MACRO("{8 * 7}")(); // If there are two args, use {} instead
constexpr auto e = CAMBDA_MACRO("{ {8 * 7} + {6 * 3} }")(); // Nested application
static_assert(a == 15 ,"");
static_assert(b == 15 ,"");
static_assert(c == 56 ,"");
static_assert(d == 56 ,"");
static_assert(e == 74 ,"");
static_assert(56088 == CAMBDA_MACRO( R"--(
([] [left] 123)
([] [right] 456)
(* left right)
)--")
() ,"");
auto constexpr
test_lambda_with_binding()
{
int y = 0;
auto cambda_lambda_bound =
CAMBDA_MACRO("(lambda [x] [{y = 2} {x * x}])")
[BINDING_MACRO("y") &= y]
()
;
auto res = cambda_lambda_bound(10);
return y * res;
}
static_assert(test_lambda_with_binding() == 200 ,"");
int main()
{
// We do nothing here, this is just to be compiled to confirm that static_assert's worked
}