The Battle for Wesnoth  1.19.7+dev
formula.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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);
38  formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols = nullptr);
39 
40  static variant evaluate(
41  const const_formula_ptr& f,
42  const formula_callable& variables,
43  formula_debugger* fdb = nullptr,
44  variant default_res = variant(0))
45  {
46  if(f) {
47  return f->evaluate(variables, fdb);
48  } else {
49  return default_res;
50  }
51  }
52 
53  static formula_ptr create_optional_formula(const std::string& str, function_symbol_table* symbols = nullptr);
54 
55  variant evaluate(const formula_callable& variables, formula_debugger* fdb = nullptr) const
56  {
57  if(fdb != nullptr) {
58  return evaluate_formula_callback(*fdb, *this, variables);
59  } else {
60  return execute(variables, fdb);
61  }
62  }
63 
64  variant evaluate(formula_debugger* fdb = nullptr) const
65  {
66  if(fdb != nullptr) {
67  return evaluate_formula_callback(*fdb,*this);
68  } else {
69  return execute(fdb);
70  }
71  }
72 
73  const std::string& str() const { return str_; }
74 
75  static const char* const id_chars;
76 
77 private:
78  variant execute(const formula_callable& variables, formula_debugger* fdb = nullptr) const;
79  variant execute(formula_debugger* fdb) const;
80 
82  std::string str_;
83  // Can't be a unique_ptr because function_symbol_table is an incomplete type,
84  // and the header it's declared in depends on this one.
85  const std::shared_ptr<function_symbol_table> managed_symbols_;
87 
88  friend class formula_debugger;
89 };
90 
91 struct formula_error : public game::error
92 {
94  : error()
95  , type()
96  , formula()
97  , filename()
98  , line(0)
99  {}
100 
101  formula_error(const std::string& type, const std::string& formula,
102  const std::string& file, int line);
103 
104  ~formula_error() noexcept {}
105 
106  std::string type;
107  std::string formula;
108  std::string filename;
109  int line;
110 };
111 
112 } // namespace wfl
class responsible for parsing the provided text into tokens and tracking information about the curren...
Definition: tokenizer.hpp:99
const std::shared_ptr< function_symbol_table > managed_symbols_
Definition: formula.hpp:85
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:40
std::string str_
Definition: formula.hpp:82
variant evaluate(formula_debugger *fdb=nullptr) const
Definition: formula.hpp:64
expression_ptr expr_
Definition: formula.hpp:81
function_symbol_table * symbols_
Definition: formula.hpp:86
static formula_ptr create_optional_formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:252
formula(const std::string &str, function_symbol_table *symbols=nullptr)
Definition: formula.cpp:101
const std::string & str() const
Definition: formula.hpp:73
variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.cpp:261
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.hpp:55
static const char *const id_chars
Definition: formula.hpp:75
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:106
std::string formula
Definition: formula.hpp:107
~formula_error() noexcept
Definition: formula.hpp:104
std::string filename
Definition: formula.hpp:108
#define f