/
base_primitives.cpp
313 lines (253 loc) · 9.51 KB
/
base_primitives.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) 2017 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <phylanx/config.hpp>
#include <phylanx/execution_tree/primitives/base_primitive.hpp>
#include <phylanx/ir/node_data.hpp>
#include <phylanx/util/optional.hpp>
#include <phylanx/util/serialization/optional.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/components.hpp>
#include <string>
#include <utility>
///////////////////////////////////////////////////////////////////////////////
// Add factory registration functionality
HPX_REGISTER_COMPONENT_MODULE()
///////////////////////////////////////////////////////////////////////////////
// Serialization support for the base_file actions
typedef phylanx::execution_tree::primitives::base_primitive base_primitive_type;
HPX_REGISTER_ACTION(base_primitive_type::eval_action,
phylanx_primitive_eval_action)
HPX_REGISTER_ACTION(base_primitive_type::store_action,
phylanx_primitive_store_action)
HPX_DEFINE_GET_COMPONENT_TYPE(base_primitive_type)
///////////////////////////////////////////////////////////////////////////////
namespace phylanx { namespace execution_tree
{
///////////////////////////////////////////////////////////////////////////
hpx::future<primitive_result_type> primitive::eval() const
{
using action_type = primitives::base_primitive::eval_action;
return hpx::async(action_type(), this->base_type::get_id());
}
hpx::future<void> primitive::store(primitive_result_type const& data)
{
using action_type = primitives::base_primitive::store_action;
return hpx::async(action_type(), this->base_type::get_id(), data);
}
void primitive::store(hpx::launch::sync_policy,
primitive_result_type const& data)
{
return store(data).get();
}
///////////////////////////////////////////////////////////////////////////
primitive_result_type extract_literal_value(
primitive_argument_type const& val)
{
switch (val.index())
{
case 0: // nil
return ast::nil{};
case 1: // bool
return util::get<1>(val);
case 2: // std::uint64_t
return util::get<2>(val);
case 3: // std::string
return util::get<3>(val);
case 4: // phylanx::ir::node_data<double>
return util::get<4>(val);
case 5: HPX_FALLTHROUGH; // primitive
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_literal_value",
"primitive_argument_type does not hold a literal value type");
}
primitive_result_type extract_literal_value(
primitive_result_type && val)
{
switch (val.index())
{
case 0: // nil
return ast::nil{};
case 1: // bool
return util::get<1>(val);
case 2: // std::uint64_t
return util::get<2>(val);
case 3: // std::string
return util::get<3>(std::move(val));
case 4: // phylanx::ir::node_data<double>
return util::get<4>(std::move(val));
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_literal_value",
"primitive_result_type does not hold a literal value type");
}
///////////////////////////////////////////////////////////////////////////
ir::node_data<double> extract_numeric_value(
primitive_argument_type const& val)
{
switch (val.index())
{
case 1: // bool
return ir::node_data<double>{double(util::get<1>(val))};
case 2: // std::uint64_t
return ir::node_data<double>{double(util::get<2>(val))};
case 4: // phylanx::ir::node_data<double>
return util::get<4>(val);
case 0: HPX_FALLTHROUGH; // nil
case 3: HPX_FALLTHROUGH; // string
case 5: HPX_FALLTHROUGH; // primitive
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_numeric_value",
"primitive_argument_type does not hold a numeric value type");
}
ir::node_data<double> extract_numeric_value(
primitive_result_type && val)
{
switch (val.index())
{
case 1: // bool
return ir::node_data<double>{double(util::get<1>(val))};
case 2: // std::uint64_t
return ir::node_data<double>{double(util::get<2>(val))};
case 4: // phylanx::ir::node_data<double>
return util::get<4>(std::move(val));
case 0: HPX_FALLTHROUGH; // nil
case 3: HPX_FALLTHROUGH; // string
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_numeric_value",
"primitive_result_type does not hold a numeric value type");
}
///////////////////////////////////////////////////////////////////////////
std::uint8_t extract_boolean_value(primitive_argument_type const& val)
{
switch (val.index())
{
case 1: // bool
return util::get<1>(val);
case 2: // std::uint64_t
return util::get<2>(val) != 0;
case 4: // phylanx::ir::node_data<double>
return bool(util::get<4>(val));
case 0: HPX_FALLTHROUGH; // nil
case 3: HPX_FALLTHROUGH; // string
case 5: HPX_FALLTHROUGH; // primitive
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_numeric_value",
"primitive_argument_type does not hold a boolean value type");
}
std::uint8_t extract_boolean_value(primitive_result_type const& val)
{
switch (val.index())
{
case 1: // bool
return util::get<1>(val);
case 2: // std::uint64_t
return util::get<2>(val) != 0;
case 4: // phylanx::ir::node_data<double>
return bool(util::get<4>(val));
case 0: HPX_FALLTHROUGH; // nil
case 3: HPX_FALLTHROUGH; // string
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_numeric_value",
"primitive_result_type does not hold a boolean value type");
}
///////////////////////////////////////////////////////////////////////////
primitive primitive_operand(primitive_argument_type const& val)
{
primitive const* p = util::get_if<primitive>(&val);
if (p != nullptr)
return *p;
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::extract_primitive",
"primitive_value_type does not hold a primitive");
}
bool is_primitive_operand(primitive_argument_type const& val)
{
return util::get_if<primitive>(&val) != nullptr;
}
///////////////////////////////////////////////////////////////////////////
hpx::future<primitive_result_type> literal_operand(
primitive_argument_type const& val)
{
primitive const* p = util::get_if<primitive>(&val);
if (p != nullptr)
{
return p->eval().then(
[](hpx::future<primitive_result_type> && f)
{
return extract_literal_value(f.get());
});
}
HPX_ASSERT(valid(val));
return hpx::make_ready_future(extract_literal_value(val));
}
hpx::future<ir::node_data<double>> numeric_operand(
primitive_argument_type const& val)
{
primitive const* p = util::get_if<primitive>(&val);
if (p != nullptr)
{
return p->eval().then(
[](hpx::future<primitive_result_type> && f)
{
return extract_numeric_value(f.get());
});
}
HPX_ASSERT(valid(val));
return hpx::make_ready_future(extract_numeric_value(val));
}
hpx::future<std::uint8_t> boolean_operand(primitive_argument_type const& val)
{
primitive const* p = util::get_if<primitive>(&val);
if (p != nullptr)
{
return p->eval().then(
[](hpx::future<primitive_result_type> && f)
{
return extract_boolean_value(f.get());
});
}
HPX_ASSERT(valid(val));
return hpx::make_ready_future(extract_boolean_value(val));
}
///////////////////////////////////////////////////////////////////////////
primitive_argument_type to_primitive_value_type(primitive_result_type&& val)
{
switch (val.index())
{
case 0:
return ast::nil{};
case 1: // bool
return util::get<1>(std::move(val));
case 2: // std::uint64_t
return util::get<2>(std::move(val));
case 3: // std::string
return util::get<3>(std::move(val));
case 4: // phylanx::ir::node_data<double>
return util::get<4>(std::move(val));
default:
break;
}
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::primitives::to_primitive_value_type",
"unsupported primitive_result_type");
}
}}