The Battle for Wesnoth  1.19.10+dev
formula.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://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/debugger_fwd.hpp"
19 #include "formula/formula_fwd.hpp"
20 #include "formula/tokenizer.hpp"
21 #include "formula/variant.hpp"
22 #include <memory>
23 
24 namespace wfl
25 {
26 class formula_expression;
27 class function_symbol_table;
28 
29 using expression_ptr = std::shared_ptr<formula_expression>;
30 
31 // Namespace alias for shorter typing
32 namespace tk = tokenizer;
33 
34 class formula
35 {
36 public:
37  formula(const std::string& str, function_symbol_table* symbols = nullptr, bool manage_symbols = false);
38  formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols = nullptr);
39 
40  formula(formula&&) = default;
41  formula& operator=(formula&&) = default;
42 
43  // Since function_symbol_table is an incomplete type at this point,
44  // a destructor must be defined out-of-line once its definition is
45  // complete, otherwise compilation fails when used with unique_ptr.
47 
48  static variant evaluate(
49  const const_formula_ptr& f,
50  const formula_callable& variables,
51  formula_debugger* fdb = nullptr,
52  variant default_res = variant(0))
53  {
54  if(f) {
55  return f->evaluate(variables, fdb);
56  } else {
57  return default_res;
58  }
59  }
60 
61  static formula_ptr create_optional_formula(const std::string& str, function_symbol_table* symbols = nullptr);
62 
63  variant evaluate(const formula_callable& variables, formula_debugger* fdb = nullptr) const
64  {
65  if(fdb != nullptr) {
66  return evaluate_formula_callback(*fdb, *this, variables);
67  } else {
68  return execute(variables, fdb);
69  }
70  }
71 
72  variant evaluate(formula_debugger* fdb = nullptr) const
73  {
74  if(fdb != nullptr) {
75  return evaluate_formula_callback(*fdb,*this);
76  } else {
77  return execute(fdb);
78  }
79  }
80 
81  const std::string& str() const { return str_; }
82 
83  static const char* const id_chars;
84 
85 private:
86  variant execute(const formula_callable& variables, formula_debugger* fdb = nullptr) const;
87  variant execute(formula_debugger* fdb) const;
88 
90  std::string str_;
91  std::unique_ptr<function_symbol_table> managed_symbols_;
93 
94  friend class formula_debugger;
95 };
96 
97 struct formula_error : public game::error
98 {
100  : error()
101  , type()
102  , formula()
103  , filename()
104  , line(0)
105  {}
106 
107  formula_error(const std::string& type, const std::string& formula,
108  const std::string& file, int line);
109 
110  ~formula_error() noexcept {}
111 
112  std::string type;
113  std::string formula;
114  std::string filename;
115  int line;
116 };
117 
118 } // namespace wfl
class responsible for parsing the provided text into tokens and tracking information about the curren...
Definition: tokenizer.hpp:99
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:48
formula & operator=(formula &&)=default
formula(const std::string &str, function_symbol_table *symbols=nullptr, bool manage_symbols=false)
Definition: formula.cpp:101
std::string str_
Definition: formula.hpp:90
variant evaluate(formula_debugger *fdb=nullptr) const
Definition: formula.hpp:72
expression_ptr expr_
Definition: formula.hpp:89
function_symbol_table * symbols_
Definition: formula.hpp:92
static formula_ptr create_optional_formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:255
std::unique_ptr< function_symbol_table > managed_symbols_
Definition: formula.hpp:91
formula(formula &&)=default
const std::string & str() const
Definition: formula.hpp:81
variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.cpp:264
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.hpp:63
static const char *const id_chars
Definition: formula.hpp:83
Formula AI debugger, forward.
Definition: contexts.hpp:43
std::shared_ptr< const formula > const_formula_ptr
Definition: formula_fwd.hpp:24
std::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:29
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:22
variant evaluate_formula_callback(formula_debugger &fdb, const formula &f, const formula_callable &variables)
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string type
Definition: formula.hpp:112
std::string formula
Definition: formula.hpp:113
~formula_error() noexcept
Definition: formula.hpp:110
std::string filename
Definition: formula.hpp:114
#define f