-
Notifications
You must be signed in to change notification settings - Fork 1
/
pureexpr.h
289 lines (255 loc) · 7.98 KB
/
pureexpr.h
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
#pragma once
#include <tuple>
#include "pureargs.h"
#include "purempl.h"
#include "pureargmap.h"
namespace pure
{
struct Expression
{
};
template <typename F, typename Args>
struct result_type
{
//typedef void type;
};
template <typename T>
struct ConstExpression : public Expression
{
typedef T type;
T t;
ConstExpression(T t) : t(t) {}
};
template <typename T, int ... NArgs, typename ... CallArgs>
ConstExpression<T> arg_bind(ConstExpression<T> e, const argmap<mpl::seq<NArgs...>, CallArgs...>&)
{
return e;
}
template <int N>
struct VarExpression;
namespace detail
{
template <int N, int ... NArgs, typename ... CallArgs>
auto var_bind(argmap<mpl::seq<NArgs...>, CallArgs...> arg, mpl::bool_<true>)
-> ConstExpression<typename arg_type<N, decltype(arg)>::type>
{
return ConstExpression<typename arg_type<N, decltype(arg)>::type>(arg_get<N>(arg));
}
template <int N, int ... NArgs, typename ... CallArgs>
auto var_bind(argmap<mpl::seq<NArgs...>, CallArgs...> arg, mpl::bool_<false>)
-> VarExpression<N>
{
return VarExpression<N>(Arg<N>());
}
}
template <int N>
struct VarExpression : public Expression
{
static const int value = N;
VarExpression(Arg<N>){}
};
template <int N, int ... NArgs, typename ... CallArgs>
auto arg_bind(VarExpression<N>, argmap<mpl::seq<NArgs...>, CallArgs...> arg)
-> decltype(detail::var_bind<N>(arg, mpl::bool_<mpl::seq_contains<N,mpl::seq<NArgs...>>::value>()))
{
return detail::var_bind<N>(arg, mpl::bool_<mpl::seq_contains<N,mpl::seq<NArgs...>>::value>());
}
#define BINARY_OPERATOR(op, className) \
template <typename L, typename R>\
struct className : public Expression\
{\
L l;\
R r;\
className(L l, R r):l(l),r(r){}\
\
};\
template <typename L, typename R, int ... NArgs, typename ... CallArgs> \
auto arg_bind(className<L,R> e, argmap<mpl::seq<NArgs...>, CallArgs...> arg) \
-> className<decltype(arg_bind(e.l,arg)), decltype(arg_bind(e.r,arg))>\
{ \
return className<decltype(arg_bind(e.l,arg)), decltype(arg_bind(e.r,arg))>(arg_bind(e.l,arg), arg_bind(e.r,arg)); \
} \
template <typename L, typename R>\
className<\
PURE_PROMOTE(L),\
PURE_PROMOTE(R)\
> operator op(L l, R r)\
{\
typedef PURE_PROMOTE(L) LE;\
typedef PURE_PROMOTE(R) RE;\
return className<LE, RE>(LE(l), RE(r));\
}\
template <typename L, typename R, typename Args>\
struct result_type<className<L, R>, Args>\
{\
typedef decltype(typename result_type<L, Args>::type() op typename result_type<R, Args>::type()) type;\
};\
template <typename L, typename R, typename Arg>\
auto eval(className<L, R> e, Arg arg)\
-> decltype(eval(e.l, arg) op eval(e.r, arg))\
{\
return eval(e.l, arg) op eval(e.r, arg);\
}
//template <typename L, typename R>
//struct AddExpression : public Expression
//{
//L l;
//R r;
//AddExpression(L l, R r):l(l),r(r){}
//};
template <typename F, typename ... CallArgs>
struct CallExpression : public Expression
{
F f;
std::tuple<CallArgs...> callArgs;
CallExpression(F f, CallArgs... _callArgs):f(f), callArgs(_callArgs...){}
};
namespace detail
{
template <typename F, typename ... CallArgs, int ... NArgs, typename ... Args, int ... S>
auto calle_bind_helper(CallExpression<F, CallArgs...> e, const argmap<mpl::seq<NArgs...>, Args...>& arg, mpl::seq<S...>)
-> CallExpression<
decltype(arg_bind(e.f,arg)),
decltype(arg_bind(std::get<S>(e.callArgs),arg))...
>
{
return CallExpression<
decltype(arg_bind(e.f,arg)),
decltype(arg_bind(std::get<S>(e.callArgs),arg))...
>
(arg_bind(e.f,arg), arg_bind(std::get<S>(e.callArgs),arg)...);
}
}
template <typename F, typename ... CallArgs, int ... NArgs, typename ... Args>
auto arg_bind(CallExpression<F, CallArgs...> e, const argmap<mpl::seq<NArgs...>, Args...>& arg)
-> decltype(detail::calle_bind_helper(e, arg, typename mpl::count<sizeof...(CallArgs)>::type()))
{
return detail::calle_bind_helper(e, arg, typename mpl::count<sizeof...(CallArgs)>::type());
}
template <typename T>
struct PromoteToExpression
{
typedef typename mpl::if_<std::is_base_of<Expression, T>, T, ConstExpression<T>>::type type;
};
template <int N>
struct PromoteToExpression<Arg<N>>
{
typedef VarExpression<N> type;
};
template <typename Expr, int ...Args>
struct Lambda;
template <typename Expr, int ...Args>
struct PromoteToExpression<Lambda<Expr, Args...>>
{
//typedef ConstExpression<Lambda<Expr, Args...>> type;
typedef Lambda<Expr, Args...> type;
};
#define PURE_PROMOTE(T) typename PromoteToExpression<T>::type
//template <typename L, typename R>
//AddExpression<
//PURE_PROMOTE(L),
//PURE_PROMOTE(R)
//> operator +(L l, R r)
//{
//typedef PURE_PROMOTE(L) LE;
//typedef PURE_PROMOTE(R) RE;
//return AddExpression<LE, RE>(LE(l), RE(r));
//}
template <typename Expr, typename Args, int... NArgs>
struct result_type<Lambda<Expr, NArgs...>, Args>
{
typedef Lambda<Expr, NArgs...> type;
};
template <typename T, typename Args>
struct result_type<ConstExpression<T>, Args>
{
typedef T type;
};
template <int N, typename Args>
struct result_type<VarExpression<N>, Args>
{
typedef typename arg_type<N, Args>::type type;
};
//template <typename L, typename R, typename Args>
//struct result_type<AddExpression<L, R>, Args>
//{
//typedef decltype(typename result_type<L, Args>::type()+typename result_type<R, Args>::type()) type;
//};
template <typename F, typename ... CallArgs, typename AMSeq, typename ... AMTypes>
struct result_type<CallExpression<F, CallArgs...>, argmap<AMSeq, AMTypes...>>
{
typedef typename result_type<F, argmap<AMSeq, AMTypes...>>::type L;
typedef typename result_type<
typename L::expr_type,
argmap<
decltype(seq_cat(typename L::arg_bind_type(), AMSeq())),
typename result_type<CallArgs, argmap<AMSeq, AMTypes...>>::type..., AMTypes...>
>::type type;
};
template <typename T, typename Arg>
T eval(ConstExpression<T> e, Arg arg)
{
return e.t;
}
//template <typename L, typename R, typename Arg>
//auto eval(AddExpression<L, R> e, Arg arg)
//-> decltype(eval(e.l, arg) + eval(e.r, arg))
//{
//return eval(e.l, arg) + eval(e.r, arg);
//}
template <int N, typename Arg>
typename arg_type<N, Arg>::type eval(VarExpression<N> e, Arg arg)
{
return arg_get<N>(arg);
}
template <typename F, typename Arg, typename ... CallArgs, int ... S>
std::tuple<typename result_type<CallArgs, Arg>::type...>
compute_call_args_helper(CallExpression<F, CallArgs...>ce, Arg arg, mpl::seq<S...>)
{
return std::tuple<typename result_type<CallArgs, Arg>::type...>(
eval(std::get<S>(ce.callArgs),arg)...
);
}
template <typename F, typename Arg, typename ... CallArgs>
std::tuple<typename result_type<CallArgs, Arg>::type...>
compute_call_args(CallExpression<F, CallArgs...>ce, Arg arg)
{
return compute_call_args_helper(ce, arg, typename mpl::count<sizeof...(CallArgs)>::type());
}
template <typename OrigianlArg, typename Arg, typename F, int ... NArgs, int ... S>
auto call_helper(Lambda<F, NArgs...> l, Arg arg, OrigianlArg oarg, mpl::seq<S...>)
-> typename result_type<typename PromoteToExpression<F>::type,
decltype(make_argmap(arg, mpl::seq<NArgs...>()))
>::type
{
return eval(l.e, argmap_cat(
make_argmap(arg, mpl::seq<NArgs...>()),
oarg
));
//return l(std::get<S>(arg)...);
}
template <typename Arg, typename F, int ... NArgs>
auto eval(Lambda<F, NArgs...> l, Arg arg)
-> decltype(arg_bind(l, arg))
{
return arg_bind(l, arg);
}
template <typename F, typename Arg, typename ... CallArgs>
auto eval(CallExpression<F, CallArgs...> e, Arg arg)
-> typename result_type<CallExpression<F, CallArgs...>, Arg>::type
{
return
call_helper(
eval(
typename PromoteToExpression<F>::type(e.f),
arg
),
compute_call_args(e, arg), arg, typename mpl::count<sizeof...(CallArgs)>::type());
}
BINARY_OPERATOR(+, AddExpression)
BINARY_OPERATOR(-, SubExpression)
BINARY_OPERATOR(*, MulExpression)
BINARY_OPERATOR(/, DivExpression)
//BINARY_OPERATOR(<<, LShiftExpression)
//BINARY_OPERATOR(>>, RShiftExpression)
}