forked from freeorion/freeorion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticRules.cpp
245 lines (227 loc) · 10.7 KB
/
ArithmeticRules.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
#include "ValueRefParser.h"
#include "MovableEnvelope.h"
#include "../universe/ValueRefs.h"
#include <boost/phoenix.hpp>
namespace parse::detail {
template <typename T>
arithmetic_rules<T>::arithmetic_rules(
const std::string& type_name,
const parse::lexer& tok,
parse::detail::Labeller& label,
const parse::detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar
) :
statistic_type_enum(tok)
{
using boost::phoenix::construct;
using boost::phoenix::new_;
using boost::phoenix::push_back;
boost::spirit::qi::_1_type _1;
boost::spirit::qi::_2_type _2;
boost::spirit::qi::_4_type _4;
boost::spirit::qi::_a_type _a;
boost::spirit::qi::_b_type _b;
boost::spirit::qi::_c_type _c;
boost::spirit::qi::_d_type _d;
boost::spirit::qi::_val_type _val;
boost::spirit::qi::lit_type lit;
boost::spirit::qi::_pass_type _pass;
const boost::phoenix::function<construct_movable> construct_movable_;
const boost::phoenix::function<deconstruct_movable> deconstruct_movable_;
const boost::phoenix::function<deconstruct_movable_vector> deconstruct_movable_vector_;
named_lookup_expr
= (
tok.Named_ >> tok.Value_ >> tok.Lookup_
>> label(tok.name_)
>> tok.string
) [ _val = construct_movable_(new_<ValueRef::NamedRef<T>>(_4, boost::phoenix::val(/*is_lookup_only*/true))) ]
;
functional_expr
= (
(
(
tok.sin_ [ _c = ValueRef::OpType::SINE ] // single-parameter math functions
| tok.cos_ [ _c = ValueRef::OpType::COSINE ]
| tok.log_ [ _c = ValueRef::OpType::LOGARITHM ]
| tok.NoOp_ [ _c = ValueRef::OpType::NOOP ] // pass-through does nothing, for debug purposes
| tok.abs_ [ _c = ValueRef::OpType::ABS ]
| tok.round_ [ _c = ValueRef::OpType::ROUND_NEAREST ]
| tok.ceil_ [ _c = ValueRef::OpType::ROUND_UP ]
| tok.floor_ [ _c = ValueRef::OpType::ROUND_DOWN ]
| tok.sign_ [ _c = ValueRef::OpType::SIGN ]
)
>> ('(' > expr > ')') [ _val = construct_movable_(new_<ValueRef::Operation<T>>(_c, deconstruct_movable_(_1, _pass))) ]
)
| (
tok.RandomNumber_ [ _c = ValueRef::OpType::RANDOM_UNIFORM ] // random number requires a min and max value
> ( '(' > expr > ',' > expr > ')' ) [ _val = construct_movable_(
new_<ValueRef::Operation<T>>(_c, deconstruct_movable_(_1, _pass), deconstruct_movable_(_2, _pass))) ]
)
| (
(
tok.OneOf_ [ _c = ValueRef::OpType::RANDOM_PICK ] // oneof, min, or max can take any number or operands
| tok.min_ [ _c = ValueRef::OpType::MINIMUM ]
| tok.max_ [ _c = ValueRef::OpType::MAXIMUM ]
)
>> ( '(' >> expr [ push_back(_d, _1) ]
>>(*(',' > expr [ push_back(_d, _1) ] )) >> ')' )
[ _val = construct_movable_(new_<ValueRef::Operation<T>>(_c, deconstruct_movable_vector_(_d, _pass))) ]
)
| (
lit('(') >> expr [ push_back(_d, _1) ]
>> (
( lit("==") [ _c = ValueRef::OpType::COMPARE_EQUAL ]
| lit('=') [ _c = ValueRef::OpType::COMPARE_EQUAL ]
| lit(">=") [ _c = ValueRef::OpType::COMPARE_GREATER_THAN_OR_EQUAL ]
| lit('>') [ _c = ValueRef::OpType::COMPARE_GREATER_THAN ]
| lit("<=") [ _c = ValueRef::OpType::COMPARE_LESS_THAN_OR_EQUAL ]
| lit('<') [ _c = ValueRef::OpType::COMPARE_LESS_THAN ]
| lit("!=") [ _c = ValueRef::OpType::COMPARE_NOT_EQUAL ]
)
> expr [ push_back(_d, _1) ]
)
> (
lit(')')
| (
(lit('?') > expr [ push_back(_d, _1) ])
> (
lit(')')
| ( lit(':') > expr [ push_back(_d, _1) ] > ')' )
)
)
) [ _val = construct_movable_(new_<ValueRef::Operation<T>>(_c, deconstruct_movable_vector_(_d, _pass))) ]
)
| (
lit('-') >> functional_expr
// single parameter math function with a function expression
// rather than any arbitrary expression as parameter, because
// negating more general expressions can be ambiguous
[ _val = construct_movable_(new_<ValueRef::Operation<T>>(ValueRef::OpType::NEGATE, deconstruct_movable_(_1, _pass))) ]
)
| (
primary_expr [ _val = _1 ]
)
)
;
exponential_expr
= (
functional_expr [ _a = _1 ]
>>
-( '^'
>> functional_expr [
_b = construct_movable_(new_<ValueRef::Operation<T>>(
ValueRef::OpType::EXPONENTIATE,
deconstruct_movable_(_a, _pass),
deconstruct_movable_(_1, _pass) )) ,
_a = _b]
)
) [ _val = _a ]
;
multiplicative_expr
= (
exponential_expr [ _a = _1 ]
>>
*(
(
(
lit('*') [ _c = ValueRef::OpType::TIMES ]
| lit('/') [ _c = ValueRef::OpType::DIVIDE ]
| lit('%') [ _c = ValueRef::OpType::REMAINDER ]
)
>> exponential_expr [
_b = construct_movable_(new_<ValueRef::Operation<T>>(
_c,
deconstruct_movable_(_a, _pass),
deconstruct_movable_(_1, _pass))) ]
) [ _a = _b ]
)
) [ _val = _a ]
;
additive_expr
= (
multiplicative_expr [ _a = _1 ]
>>
*(
(
(
lit('+') [ _c = ValueRef::OpType::PLUS ]
| lit('-') [ _c = ValueRef::OpType::MINUS ]
)
>> multiplicative_expr [
_b = construct_movable_(new_<ValueRef::Operation<T>>(
_c,
deconstruct_movable_(_a, _pass),
deconstruct_movable_(_1, _pass))) ]
) [ _a = _b ]
)
) [ _val = _a ]
;
statistic_collection_expr
= (tok.Statistic_
>> ( tok.Count_ [ _b = ValueRef::StatisticType::COUNT ]
| tok.If_ [ _b = ValueRef::StatisticType::IF ]
)
)
> label(tok.condition_) > condition_parser
[ _val = construct_movable_(new_<ValueRef::Statistic<T>>(
deconstruct_movable_(_a, _pass), _b, deconstruct_movable_(_1, _pass))) ]
;
statistic_value_expr
= (tok.Statistic_ >> statistic_type_enum [ _b = _1 ])
>> label(tok.value_)
>> (
(
statistic_value_ref_expr [ _a = _1 ]
> label(tok.condition_) > condition_parser
[ _val = construct_movable_(new_<ValueRef::Statistic<T, T>>(
deconstruct_movable_(_a, _pass), _b, deconstruct_movable_(_1, _pass))) ]
)
| (
string_grammar [ _c = _1 ]
> label(tok.condition_) > condition_parser
[ _val = construct_movable_(new_<ValueRef::Statistic<T, std::string>>(
deconstruct_movable_(_c, _pass), _b, deconstruct_movable_(_1, _pass))) ]
)
);
statistic_expr
= statistic_collection_expr
| statistic_value_expr
;
expr
= additive_expr
;
#if DEBUG_VALUEREF_PARSERS
debug(named_lookup_expr);
debug(functional_expr);
debug(exponential_expr);
debug(multiplicative_expr);
debug(additive_expr);
debug(primary_expr);
debug(statistic_value_ref_expr);
debug(statistic_collection_expr);
debug(statistic_value_expr);
debug(statistic_expr);
debug(expr);
#endif
named_lookup_expr.name(type_name + " nominal lookup expression");
functional_expr.name(type_name + " function expression");
exponential_expr.name(type_name + " exponential expression");
multiplicative_expr.name(type_name + " multiplication expression");
additive_expr.name(type_name + " additive expression");
statistic_value_ref_expr.name(type_name + " statistic value reference");
statistic_collection_expr.name(type_name + " collection statistic");
statistic_value_expr.name(type_name + " value statistic");
statistic_expr.name("real number statistic");
primary_expr.name(type_name + " expression");
expr.name(type_name + " expression");
}
// Explicit instantiation to prevent costly recompilation in multiple units
template arithmetic_rules<double>::arithmetic_rules(
const std::string& type_name, const parse::lexer& tok, parse::detail::Labeller& label,
const parse::detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
template arithmetic_rules<int>::arithmetic_rules(
const std::string& type_name, const parse::lexer& tok, parse::detail::Labeller& label,
const parse::detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
}