The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
function.hpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2008 - 2017 by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "formula/callable.hpp"
19 #include "formula/formula.hpp"
20 
21 #include <set>
22 #include <string>
23 
24 namespace wfl
25 {
26 /** Helper macro to declare an associated class for a WFL function. */
27 #define DEFINE_WFL_FUNCTION(name, min_args, max_args) \
28  class name##_function : public function_expression \
29  { \
30  public: \
31  explicit name##_function(const args_list& args) \
32  : function_expression(#name, args, min_args, max_args) \
33  { \
34  } \
35  \
36  private: \
37  variant execute(const formula_callable& variables, formula_debugger* fdb) const; \
38  }; \
39  \
40  variant name##_function::execute(const formula_callable& variables, formula_debugger* fdb) const
41 
42 
43 /**
44  * Declares a function `name` in the local function table `functions_table`.
45  * The function must be defined by a `name_function` class which is accessible in the current scope.
46  */
47 #define DECLARE_WFL_FUNCTION(name) \
48  functions_table.add_function(#name, std::make_shared<builtin_formula_function<name##_function>>(#name))
49 
51 {
52  explicit call_stack_manager(const std::string& str);
54 
55  static std::string get();
56 };
57 
59 {
60 public:
61  explicit formula_expression(const std::string& name = "")
62  : name_(name)
63  {
64  }
65 
67  {
68  }
69 
70  variant evaluate(const formula_callable& variables, formula_debugger* fdb = nullptr) const
71  {
73 
74  if(fdb != nullptr) {
75  return evaluate_arg_callback(*fdb, *this, variables);
76  } else {
77  return execute(variables, fdb);
78  }
79  }
80 
82  {
83  return name_;
84  }
85 
86  virtual std::string str() const = 0;
87 
88 private:
89  virtual variant execute(const formula_callable& variables, formula_debugger* fdb = nullptr) const = 0;
90 
92  friend class formula_debugger;
93 };
94 
95 typedef std::shared_ptr<formula_expression> expression_ptr;
96 
98 {
99 public:
100  typedef std::vector<expression_ptr> args_list;
101 
102  explicit function_expression(const std::string& name, const args_list& args, int min_args = -1, int max_args = -1)
103  : formula_expression(name)
104  , args_(args)
105  {
106  if(min_args >= 0 && args_.size() < static_cast<size_t>(min_args)) {
107  throw formula_error("Too few arguments", "", "", 0);
108  }
109 
110  if(max_args >= 0 && args_.size() > static_cast<size_t>(max_args)) {
111  throw formula_error("Too many arguments", "", "", 0);
112  }
113  }
114 
115  virtual std::string str() const;
116 
117 protected:
118  const args_list& args() const
119  {
120  return args_;
121  }
122 
123 private:
124  args_list args_;
125 };
126 
128 {
129 public:
130  explicit key_value_pair(const variant& key, const variant& value)
131  : key_(key)
132  , value_(value)
133  {
134  }
135 
136  void serialize_to_string(std::string& str) const override;
137 
138 private:
141 
142  variant get_value(const std::string& key) const override;
143 
144  void get_inputs(formula_input_vector& inputs) const override;
145 };
146 
148 {
149 public:
151  const args_list& args,
153  const_formula_ptr precondition,
154  const std::vector<std::string>& arg_names);
155 
156 private:
157  variant execute(const formula_callable& variables, formula_debugger* fdb) const;
158 
161 
162  std::vector<std::string> arg_names_;
163 
165 };
166 
167 typedef std::shared_ptr<function_expression> function_expression_ptr;
168 
170 {
171 public:
173  : name_(name)
174  {
175  }
176 
177  virtual function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const = 0;
178 
180  {
181  }
182 
183 protected:
185 };
186 
188 {
189 public:
192  const_formula_ptr precondition,
193  const std::vector<std::string>& args)
194  : formula_function(name)
195  , formula_(formula)
196  , precondition_(precondition)
197  , args_(args)
198  {
199  }
200 
201  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const;
202 
203 private:
206  std::vector<std::string> args_;
207 };
208 
209 template<typename T>
211 {
212 public:
214  : formula_function(name)
215  {
216  }
217 
218  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const
219  {
220  return std::make_shared<T>(args);
221  }
222 };
223 
224 typedef std::shared_ptr<formula_function> formula_function_ptr;
225 typedef std::map<std::string, formula_function_ptr> functions_map;
226 
228 {
229 public:
230  explicit function_symbol_table(std::shared_ptr<function_symbol_table> parent = nullptr);
231 
232  void add_function(const std::string& name, formula_function_ptr&& fcn);
233 
234  expression_ptr create_function(const std::string& fn, const std::vector<expression_ptr>& args) const;
235 
236  std::set<std::string> get_function_names() const;
237 
238  bool empty() const
239  {
240  return custom_formulas_.empty() && (parent == nullptr || parent->empty());
241  }
242 
243  static std::shared_ptr<function_symbol_table> get_builtins();
244 
245 private:
246  std::shared_ptr<function_symbol_table> parent;
247  functions_map custom_formulas_;
248 
251  {
252  }
253 };
254 
256 {
257 public:
258  action_function_symbol_table(std::shared_ptr<function_symbol_table> parent = nullptr);
259 };
260 
262 {
263 public:
265  : arg_()
266  {
267  }
268 
269  wrapper_formula(expression_ptr arg)
270  : formula_expression(arg ? arg->get_name() : "")
271  , arg_(arg)
272  {
273  }
274 
276  {
277  }
278 
279  virtual std::string str() const
280  {
281  if(arg_) {
282  return arg_->str();
283  } else {
284  return "";
285  }
286  }
287 
288 private:
289  virtual variant execute(const formula_callable& variables, formula_debugger* fdb = nullptr) const
290  {
291  if(arg_) {
292  return arg_->evaluate(variables, fdb);
293  } else {
294  return variant();
295  }
296  }
297 
298  expression_ptr arg_;
299 };
300 } // end namespace wfl
variant get_value(const std::string &key) const override
Definition: function.cpp:1333
const_formula_ptr formula_
Definition: function.hpp:204
virtual ~wrapper_formula()
Definition: function.hpp:275
void serialize_to_string(std::string &str) const override
Definition: function.cpp:1350
wrapper_formula(expression_ptr arg)
Definition: function.hpp:269
std::vector< char_t > string
virtual ~formula_expression()
Definition: function.hpp:66
virtual std::string str() const =0
key_value_pair(const variant &key, const variant &value)
Definition: function.hpp:130
std::vector< expression_ptr > args_list
Definition: function.hpp:100
std::set< std::string > get_function_names() const
Definition: function.cpp:1452
expression_ptr create_function(const std::string &fn, const std::vector< expression_ptr > &args) const
Definition: function.cpp:1434
virtual function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const =0
formula_function_expression(const std::string &name, const args_list &args, const_formula_ptr formula, const_formula_ptr precondition, const std::vector< std::string > &arg_names)
Definition: function.cpp:1359
std::vector< formula_input > formula_input_vector
Class that manages AIs for all sides and manages AI redeployment.
Definition: manager.hpp:118
formula_input_vector inputs() const
Definition: callable.hpp:62
std::shared_ptr< formula_function > formula_function_ptr
Definition: function.hpp:224
builtin_formula_function(const std::string &name)
Definition: function.hpp:213
std::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:28
function_expression(const std::string &name, const args_list &args, int min_args=-1, int max_args=-1)
Definition: function.hpp:102
user_formula_function(const std::string &name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector< std::string > &args)
Definition: function.hpp:190
call_stack_manager(const std::string &str)
Definition: function.cpp:41
variant evaluate_arg_callback(formula_debugger &fdb, const formula_expression &expression, const formula_callable &variables)
std::shared_ptr< function_expression > function_expression_ptr
Definition: function.hpp:167
static std::shared_ptr< function_symbol_table > get_builtins()
Definition: function.cpp:1466
formula_expression(const std::string &name="")
Definition: function.hpp:61
virtual std::string str() const
Definition: function.cpp:63
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:289
const_formula_ptr precondition_
Definition: function.hpp:205
std::shared_ptr< function_symbol_table > parent
Definition: function.hpp:246
function_symbol_table(std::shared_ptr< function_symbol_table > parent=nullptr)
Definition: function.cpp:1424
virtual std::string str() const
Definition: function.hpp:279
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.cpp:1418
action_function_symbol_table(std::shared_ptr< function_symbol_table > parent=nullptr)
Definition: function.cpp:1544
const args_list & args() const
Definition: function.hpp:118
const_formula_ptr precondition_
Definition: function.hpp:160
std::string get_name() const
Definition: function.hpp:81
expression_ptr arg_
Definition: function.hpp:298
std::shared_ptr< const formula > const_formula_ptr
Definition: formula_fwd.hpp:23
void get_inputs(formula_input_vector &inputs) const override
Definition: function.cpp:1344
std::vector< std::string > arg_names_
Definition: function.hpp:162
const std::string name_
Definition: function.hpp:91
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:70
function_symbol_table(builtins_tag_t)
Definition: function.hpp:250
Definition: contexts.hpp:42
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.hpp:218
formula_function(const std::string name)
Definition: function.hpp:172
virtual ~formula_function()
Definition: function.hpp:179
void add_function(const std::string &name, formula_function_ptr &&fcn)
Definition: function.cpp:1429
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
std::map< std::string, formula_function_ptr > functions_map
Definition: function.hpp:225
variant execute(const formula_callable &variables, formula_debugger *fdb) const
Definition: function.cpp:1379
functions_map custom_formulas_
Definition: function.hpp:247
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const =0
std::vector< std::string > args_
Definition: function.hpp:206