14 #include "util/timespan.hpp" 15 #include "util/span.hpp" 16 #include "util/string_view.hpp" 68 bool is_unary( token_e );
69 bool is_binary( token_e );
70 std::vector<expr_token_t> parse_tokens(
action_t* action, util::string_view expr_str );
71 void print_tokens( util::span<const expr_token_t> tokens,
sim_t* sim );
72 bool convert_to_rpn( std::vector<expr_token_t>& tokens );
73 std::unique_ptr<expr_t> build_player_expression_tree(
74 player_t& player, std::vector<expression::expr_token_t>& tokens,
82 expr_t( util::string_view name, expression::token_e op = expression::TOK_UNKNOWN )
84 #if !defined( NDEBUG ) 86 id_( get_global_id() ),
94 virtual ~
expr_t() =
default;
96 const char* name()
const 98 #if !defined( NDEBUG ) 101 return "anonymous expression";
106 #if !defined( NDEBUG ) 123 return is_constant() && evaluate() != 0.0;
127 return is_constant() && evaluate() == 0.0;
131 return t.total_seconds();
133 template <
typename T>
134 static double coerce( T t )
136 return static_cast<double>( t );
139 static std::unique_ptr<expr_t> parse(
action_t*, util::string_view expr_str,
140 bool optimize =
false );
142 static std::unique_ptr<expr_t> create_constant( util::string_view name, T
value );
144 static void optimize_expression(std::unique_ptr<expr_t>&
expression,
bool analyze_further =
false,
int spacing = 0);
146 static void optimize_expression(std::unique_ptr<expr_t>& expression,
sim_t& sim);
148 virtual double evaluate() = 0;
150 virtual bool is_constant()
155 virtual bool is_analyze_expression()
160 expression::token_e op_;
166 virtual std::unique_ptr<expr_t> build_optimized_expression(
bool ,
int )
170 #if !defined( NDEBUG ) 184 :
expr_t( name, expression::TOK_NUM ), value( value_ )
188 double evaluate()
override 193 bool is_constant()
override 202 template <
typename T>
212 double evaluate()
override 219 template <
typename T>
220 inline std::unique_ptr<expr_t> make_ref_expr( util::string_view name, T& t )
222 return std::make_unique<ref_expr_t<T>>( name,
const_cast<const T&
>( t ) );
228 template <
typename F>
232 template <
typename U = F>
233 fn_expr_t( util::string_view name, U&& f_ ) :
expr_t( name ), f( std::forward<U>( f_ ) )
239 double evaluate()
override 241 return coerce( f() );
248 std::vector<std::unique_ptr<expr_t>> proxy_expr;
249 std::string suffix_expr_str;
253 util::string_view expr_str );
255 double evaluate()
override;
259 template <
typename F>
260 inline std::unique_ptr<expr_t> make_fn_expr( util::string_view name, F&& f )
262 return std::make_unique<fn_expr_t<std::decay_t<F>>>( name, std::forward<F>( f ) );
268 template <
typename F,
typename T>
269 inline std::unique_ptr<expr_t> make_mem_fn_expr( util::string_view name, T& t, F f )
271 return make_fn_expr( name, std::bind( std::mem_fn( f ), &t ) );
275 inline std::unique_ptr<expr_t> expr_t::create_constant( util::string_view name, T
value )
277 return std::make_unique<const_expr_t>( name, coerce(value) );
Definition: expressions.cpp:12
Definition: expressions.hpp:229
Definition: expressions.hpp:62
Definition: expressions.hpp:245
Definition: action.hpp:47
Definition: expressions.hpp:178
Class for representing InGame time.
Definition: timespan.hpp:37
Definition: player.hpp:109
Action expression.
Definition: expressions.hpp:79
Definition: expressions.hpp:203
Simulation engine.
Definition: sim.hpp:61