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/formula.hpp"
19 #include "formula/callable.hpp"
20 
21 #include <set>
22 #include <string>
23 
24 namespace wfl {
25 
27  explicit call_stack_manager(const std::string& str);
29  static std::string get();
30 };
31 
33 public:
34  explicit formula_expression(const std::string& name = "") : name_(name) {}
35  virtual ~formula_expression() {}
36  variant evaluate(const formula_callable& variables, formula_debugger *fdb = nullptr) const {
38  if (fdb!=nullptr) {
39  return evaluate_arg_callback(*fdb,*this,variables);
40  } else {
41  return execute(variables,fdb);
42  }
43  }
44 
45  std::string get_name() const { return name_; }
46  virtual std::string str() const = 0;
47 private:
48  virtual variant execute(const formula_callable& variables, formula_debugger *fdb = nullptr) const = 0;
50  friend class formula_debugger;
51 };
52 
53 typedef std::shared_ptr<formula_expression> expression_ptr;
54 
56 public:
57  typedef std::vector<expression_ptr> args_list;
59  const std::string& name,
60  const args_list& args,
61  int min_args=-1, int max_args=-1)
62  : formula_expression(name), args_(args)
63  {
64  if(min_args >= 0 && args_.size() < static_cast<size_t>(min_args)) {
65  throw formula_error("Too few arguments", "", "", 0);
66  }
67 
68  if(max_args >= 0 && args_.size() > static_cast<size_t>(max_args)) {
69  throw formula_error("Too many arguments", "", "", 0);
70  }
71  }
72  virtual std::string str() const;
73 protected:
74  const args_list& args() const { return args_; }
75 private:
76  args_list args_;
77 };
78 
82 
83  variant get_value(const std::string& key) const override;
84 
85  void get_inputs(formula_input_vector& inputs) const override;
86 public:
87  explicit key_value_pair(const variant& key, const variant& value) : key_(key), value_(value) {}
88 
89  void serialize_to_string(std::string& str) const override;
90 };
91 
93 public:
94  explicit 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);
95 private:
96  variant execute(const formula_callable& variables, formula_debugger *fdb) const;
99  std::vector<std::string> arg_names_;
101 };
102 
103 typedef std::shared_ptr<function_expression> function_expression_ptr;
104 
106 protected:
108 public:
109  formula_function(const std::string name) : name_(name) {}
110  virtual function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const = 0;
111  virtual ~formula_function() {}
112 };
113 
117  std::vector<std::string> args_;
118 public:
119  user_formula_function(const std::string& name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector<std::string>& args)
120  : formula_function(name)
121  , formula_(formula)
122  , precondition_(precondition)
123  , args_(args)
124  {}
125 
126  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const;
127 };
128 
129 template<typename T>
131 public:
133  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const {
134  return function_expression_ptr(new T(args));
135  }
136 };
137 
138 typedef std::shared_ptr<formula_function> formula_function_ptr;
139 typedef std::map<std::string, formula_function_ptr> functions_map;
140 
142  std::shared_ptr<function_symbol_table> parent;
143  functions_map custom_formulas_;
146 public:
147  explicit function_symbol_table(std::shared_ptr<function_symbol_table> parent = nullptr);
148  void add_function(const std::string& name, formula_function_ptr fcn);
149  expression_ptr create_function(const std::string& fn, const std::vector<expression_ptr>& args) const;
150  std::set<std::string> get_function_names() const;
151  bool empty() {return custom_formulas_.empty() && (parent == nullptr || parent->empty());}
152  static std::shared_ptr<function_symbol_table> get_builtins();
153 };
154 
156 public:
157  action_function_symbol_table(std::shared_ptr<function_symbol_table> parent = nullptr);
158 };
159 
160 /// Declares a function `name` in the local function table `functions_table`.
161 /// The function must be defined by a `name_function` class which is accessible in the current scope.
162 #define DECLARE_WFL_FUNCTION(name) functions_table.add_function(#name, \
163  formula_function_ptr(new builtin_formula_function<name##_function>(#name)))
164 
166 public:
168  : arg_()
169  {
170  }
171 
172  wrapper_formula(expression_ptr arg)
173  : formula_expression(arg ? arg->get_name() : ""), arg_(arg)
174  {
175  }
176 
178  {
179  }
180 
181  virtual std::string str() const
182  {
183  if (arg_) {
184  return arg_->str();
185  } else {
186  return "";
187  }
188  }
189 private:
190  virtual variant execute(const formula_callable& variables, formula_debugger *fdb = nullptr) const
191  {
192  if (arg_) {
193  return arg_->evaluate(variables,fdb);
194  } else {
195  return variant();
196  }
197  }
198  expression_ptr arg_;
199 };
200 
201 }
variant get_value(const std::string &key) const override
Definition: function.cpp:1582
const_formula_ptr formula_
Definition: function.hpp:115
virtual ~wrapper_formula()
Definition: function.hpp:177
void serialize_to_string(std::string &str) const override
Definition: function.cpp:1601
wrapper_formula(expression_ptr arg)
Definition: function.hpp:172
std::vector< char_t > string
virtual ~formula_expression()
Definition: function.hpp:35
virtual std::string str() const =0
key_value_pair(const variant &key, const variant &value)
Definition: function.hpp:87
std::vector< expression_ptr > args_list
Definition: function.hpp:57
void add_function(const std::string &name, formula_function_ptr fcn)
Definition: function.cpp:1661
std::set< std::string > get_function_names() const
Definition: function.cpp:1683
expression_ptr create_function(const std::string &fn, const std::vector< expression_ptr > &args) const
Definition: function.cpp:1666
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:1609
std::vector< formula_input > formula_input_vector
Class that manages AIs for all sides and manages AI redeployment.
Definition: manager.hpp:118
const_formula_ptr formula_
Definition: function.hpp:97
formula_input_vector inputs() const
Definition: callable.hpp:62
std::shared_ptr< formula_function > formula_function_ptr
Definition: function.hpp:138
builtin_formula_function(const std::string &name)
Definition: function.hpp:132
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:58
user_formula_function(const std::string &name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector< std::string > &args)
Definition: function.hpp:119
call_stack_manager(const std::string &str)
Definition: function.cpp:40
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:103
static std::shared_ptr< function_symbol_table > get_builtins()
Definition: function.cpp:1695
formula_expression(const std::string &name="")
Definition: function.hpp:34
virtual std::string str() const
Definition: function.cpp:59
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:190
const_formula_ptr precondition_
Definition: function.hpp:116
std::shared_ptr< function_symbol_table > parent
Definition: function.hpp:142
virtual std::string str() const
Definition: function.hpp:181
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.cpp:1654
action_function_symbol_table(std::shared_ptr< function_symbol_table > parent=nullptr)
Definition: function.cpp:1771
const args_list & args() const
Definition: function.hpp:74
const_formula_ptr precondition_
Definition: function.hpp:98
std::string get_name() const
Definition: function.hpp:45
expression_ptr arg_
Definition: function.hpp:198
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:1595
std::vector< std::string > arg_names_
Definition: function.hpp:99
const std::string name_
Definition: function.hpp:49
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:36
function_symbol_table(builtins_tag_t)
Definition: function.hpp:145
Definition: contexts.hpp:42
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.hpp:133
formula_function(const std::string name)
Definition: function.hpp:109
virtual ~formula_function()
Definition: function.hpp:111
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:139
variant execute(const formula_callable &variables, formula_debugger *fdb) const
Definition: function.cpp:1622
functions_map custom_formulas_
Definition: function.hpp:143
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const =0
std::vector< std::string > args_
Definition: function.hpp:117